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