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 "ClientFrameComposer.h"
18 
19 #include <android-base/parseint.h>
20 #include <android-base/properties.h>
21 #include <android-base/strings.h>
22 #include <android/hardware/graphics/common/1.0/types.h>
23 #include <drm_fourcc.h>
24 #include <libyuv.h>
25 #include <sync/sync.h>
26 #include <ui/GraphicBuffer.h>
27 #include <ui/GraphicBufferAllocator.h>
28 #include <ui/GraphicBufferMapper.h>
29 
30 #include "Display.h"
31 #include "Drm.h"
32 #include "Layer.h"
33 
34 namespace aidl::android::hardware::graphics::composer3::impl {
35 
init()36 HWC3::Error ClientFrameComposer::init() {
37     DEBUG_LOG("%s", __FUNCTION__);
38 
39     HWC3::Error error = mDrmClient.init();
40     if (error != HWC3::Error::None) {
41         ALOGE("%s: failed to initialize DrmClient", __FUNCTION__);
42         return error;
43     }
44 
45     return HWC3::Error::None;
46 }
47 
registerOnHotplugCallback(const HotplugCallback & cb)48 HWC3::Error ClientFrameComposer::registerOnHotplugCallback(const HotplugCallback& cb) {
49     return mDrmClient.registerOnHotplugCallback(cb);
50     return HWC3::Error::None;
51 }
52 
unregisterOnHotplugCallback()53 HWC3::Error ClientFrameComposer::unregisterOnHotplugCallback() {
54     return mDrmClient.unregisterOnHotplugCallback();
55 }
56 
onDisplayCreate(Display * display)57 HWC3::Error ClientFrameComposer::onDisplayCreate(Display* display) {
58     const auto displayId = display->getId();
59     DEBUG_LOG("%s display:%" PRIu64, __FUNCTION__, displayId);
60 
61     // Ensure created.
62     mDisplayInfos.emplace(displayId, DisplayInfo{});
63 
64     return HWC3::Error::None;
65 }
66 
onDisplayDestroy(Display * display)67 HWC3::Error ClientFrameComposer::onDisplayDestroy(Display* display) {
68     const auto displayId = display->getId();
69     DEBUG_LOG("%s display:%" PRIu64, __FUNCTION__, displayId);
70 
71     auto it = mDisplayInfos.find(displayId);
72     if (it == mDisplayInfos.end()) {
73         ALOGE("%s: display:%" PRIu64 " missing display buffers?", __FUNCTION__, displayId);
74         return HWC3::Error::BadDisplay;
75     }
76 
77     mDisplayInfos.erase(it);
78 
79     return HWC3::Error::None;
80 }
81 
onDisplayClientTargetSet(Display * display)82 HWC3::Error ClientFrameComposer::onDisplayClientTargetSet(Display* display) {
83     const auto displayId = display->getId();
84     DEBUG_LOG("%s display:%" PRIu64, __FUNCTION__, displayId);
85 
86     auto it = mDisplayInfos.find(displayId);
87     if (it == mDisplayInfos.end()) {
88         ALOGE("%s: display:%" PRIu64 " missing display buffers?", __FUNCTION__, displayId);
89         return HWC3::Error::BadDisplay;
90     }
91 
92     DisplayInfo& displayInfo = it->second;
93 
94     auto [drmBufferCreateError, drmBuffer] =
95         mDrmClient.create(display->getClientTarget().getBuffer());
96     if (drmBufferCreateError != HWC3::Error::None) {
97         ALOGE("%s: display:%" PRIu64 " failed to create client target drm buffer", __FUNCTION__,
98               displayId);
99         return HWC3::Error::NoResources;
100     }
101     displayInfo.clientTargetDrmBuffer = std::move(drmBuffer);
102 
103     return HWC3::Error::None;
104 }
105 
onActiveConfigChange(Display *)106 HWC3::Error ClientFrameComposer::onActiveConfigChange(Display* /*display*/) {
107     return HWC3::Error::None;
108 };
109 
validateDisplay(Display * display,DisplayChanges * outChanges)110 HWC3::Error ClientFrameComposer::validateDisplay(Display* display, DisplayChanges* outChanges) {
111     const auto displayId = display->getId();
112     DEBUG_LOG("%s display:%" PRIu64, __FUNCTION__, displayId);
113 
114     const std::vector<Layer*>& layers = display->getOrderedLayers();
115 
116     for (Layer* layer : layers) {
117         const auto layerId = layer->getId();
118         const auto layerCompositionType = layer->getCompositionType();
119 
120         if (layerCompositionType != Composition::CLIENT) {
121             outChanges->addLayerCompositionChange(displayId, layerId, Composition::CLIENT);
122             continue;
123         }
124     }
125 
126     return HWC3::Error::None;
127 }
128 
presentDisplay(Display * display,::android::base::unique_fd * outDisplayFence,std::unordered_map<int64_t,::android::base::unique_fd> *)129 HWC3::Error ClientFrameComposer::presentDisplay(
130     Display* display, ::android::base::unique_fd* outDisplayFence,
131     std::unordered_map<int64_t, ::android::base::unique_fd>* /*outLayerFences*/) {
132     const auto displayId = display->getId();
133     DEBUG_LOG("%s display:%" PRIu64, __FUNCTION__, displayId);
134 
135     auto displayInfoIt = mDisplayInfos.find(displayId);
136     if (displayInfoIt == mDisplayInfos.end()) {
137         ALOGE("%s: failed to find display buffers for display:%" PRIu64, __FUNCTION__, displayId);
138         return HWC3::Error::BadDisplay;
139     }
140 
141     DisplayInfo& displayInfo = displayInfoIt->second;
142     if (!displayInfo.clientTargetDrmBuffer) {
143         ALOGW("%s: display:%" PRIu64 " no client target set, nothing to present.", __FUNCTION__,
144               displayId);
145         return HWC3::Error::None;
146     }
147 
148     ::android::base::unique_fd fence = display->getClientTarget().getFence();
149 
150     auto [flushError, flushCompleteFence] = mDrmClient.flushToDisplay(
151         static_cast<uint32_t>(displayId), displayInfo.clientTargetDrmBuffer, fence);
152     if (flushError != HWC3::Error::None) {
153         ALOGE("%s: display:%" PRIu64 " failed to flush drm buffer" PRIu64, __FUNCTION__, displayId);
154     }
155 
156     *outDisplayFence = std::move(flushCompleteFence);
157     return flushError;
158 }
159 
160 }  // namespace aidl::android::hardware::graphics::composer3::impl
161