1 /*
2  * Copyright 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "DrmAtomicRequest.h"
18 
19 namespace aidl::android::hardware::graphics::composer3::impl {
20 
create()21 std::unique_ptr<DrmAtomicRequest> DrmAtomicRequest::create() {
22     drmModeAtomicReqPtr request = drmModeAtomicAlloc();
23     if (!request) {
24         return nullptr;
25     }
26 
27     return std::unique_ptr<DrmAtomicRequest>(new DrmAtomicRequest(request));
28 }
29 
~DrmAtomicRequest()30 DrmAtomicRequest::~DrmAtomicRequest() {
31     if (mRequest) {
32         drmModeAtomicFree(mRequest);
33     }
34 }
35 
Set(uint32_t objectId,const DrmProperty & prop,uint64_t value)36 bool DrmAtomicRequest::Set(uint32_t objectId, const DrmProperty& prop, uint64_t value) {
37     int ret = drmModeAtomicAddProperty(mRequest, objectId, prop.getId(), value);
38     if (ret < 0) {
39         ALOGE("%s: failed to set atomic request property %s to %" PRIu64 ": %s", __FUNCTION__,
40               prop.getName().c_str(), value, strerror(errno));
41         return false;
42     }
43     return true;
44 }
45 
Commit(::android::base::borrowed_fd drmFd)46 bool DrmAtomicRequest::Commit(::android::base::borrowed_fd drmFd) {
47     constexpr const uint32_t kCommitFlags = DRM_MODE_ATOMIC_ALLOW_MODESET;
48 
49     int ret = drmModeAtomicCommit(drmFd.get(), mRequest, kCommitFlags, 0);
50     if (ret) {
51         ALOGE("%s:%d: atomic commit failed: %s\n", __FUNCTION__, __LINE__, strerror(errno));
52         return false;
53     }
54 
55     return true;
56 }
57 
58 }  // namespace aidl::android::hardware::graphics::composer3::impl
59