1 /*
2  * Copyright 2021, 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 "HalImpl.h"
18 
19 #include <aidl/android/hardware/graphics/composer3/IComposerCallback.h>
20 #include <android-base/logging.h>
21 #include <hardware/hwcomposer2.h>
22 
23 #include "ExynosDevice.h"
24 #include "ExynosDeviceModule.h"
25 #include "ExynosDisplay.h"
26 #include "ExynosHWCService.h"
27 #include "ExynosLayer.h"
28 #include "TranslateHwcAidl.h"
29 #include "Util.h"
30 
31 using namespace SOC_VERSION;
32 
33 namespace {
34 
35 static constexpr int32_t kMinComposerInterfaceVersionForVrrApi = 3;
36 static constexpr int32_t kMinComposerInterfaceVersionForHwcBatching = 3;
37 };
38 
39 namespace aidl::android::hardware::graphics::composer3::impl {
40 
create(int32_t composerInterfaceVersion)41 std::unique_ptr<IComposerHal> IComposerHal::create(int32_t composerInterfaceVersion) {
42     bool vrrApiSupported = composerInterfaceVersion >= kMinComposerInterfaceVersionForVrrApi;
43     bool batchingSupported = composerInterfaceVersion >= kMinComposerInterfaceVersionForHwcBatching;
44     auto device = std::make_unique<ExynosDeviceModule>(vrrApiSupported);
45     if (!device) {
46         return nullptr;
47     }
48     auto halImp = std::make_unique<HalImpl>(std::move(device), batchingSupported);
49     return halImp;
50 }
51 
52 namespace hook {
53 
hotplug(hwc2_callback_data_t callbackData,hwc2_display_t hwcDisplay,int32_t connected)54 void hotplug(hwc2_callback_data_t callbackData, hwc2_display_t hwcDisplay,
55                         int32_t connected) {
56     auto hal = static_cast<HalImpl*>(callbackData);
57     int64_t display;
58 
59     h2a::translate(hwcDisplay, display);
60     hal->getEventCallback()->onHotplug(display, connected == HWC2_CONNECTION_CONNECTED);
61 }
62 
refresh(hwc2_callback_data_t callbackData,hwc2_display_t hwcDisplay)63 void refresh(hwc2_callback_data_t callbackData, hwc2_display_t hwcDisplay) {
64     auto hal = static_cast<HalImpl*>(callbackData);
65     int64_t display;
66 
67     h2a::translate(hwcDisplay, display);
68     hal->getEventCallback()->onRefresh(display);
69 }
70 
vsync(hwc2_callback_data_t callbackData,hwc2_display_t hwcDisplay,int64_t timestamp,hwc2_vsync_period_t hwcVsyncPeriodNanos)71 void vsync(hwc2_callback_data_t callbackData, hwc2_display_t hwcDisplay,
72                            int64_t timestamp, hwc2_vsync_period_t hwcVsyncPeriodNanos) {
73     auto hal = static_cast<HalImpl*>(callbackData);
74     int64_t display;
75     int32_t vsyncPeriodNanos;
76 
77     h2a::translate(hwcDisplay, display);
78     h2a::translate(hwcVsyncPeriodNanos, vsyncPeriodNanos);
79     hal->getEventCallback()->onVsync(display, timestamp, vsyncPeriodNanos);
80 }
81 
vsyncPeriodTimingChanged(hwc2_callback_data_t callbackData,hwc2_display_t hwcDisplay,hwc_vsync_period_change_timeline_t * hwcTimeline)82 void vsyncPeriodTimingChanged(hwc2_callback_data_t callbackData,
83                                          hwc2_display_t hwcDisplay,
84                                          hwc_vsync_period_change_timeline_t* hwcTimeline) {
85     auto hal = static_cast<HalImpl*>(callbackData);
86     int64_t display;
87     VsyncPeriodChangeTimeline timeline;
88 
89     h2a::translate(hwcDisplay, display);
90     h2a::translate(*hwcTimeline, timeline);
91     hal->getEventCallback()->onVsyncPeriodTimingChanged(display, timeline);
92 }
93 
vsyncIdle(hwc2_callback_data_t callbackData,hwc2_display_t hwcDisplay)94 void vsyncIdle(hwc2_callback_data_t callbackData, hwc2_display_t hwcDisplay) {
95     auto hal = static_cast<HalImpl*>(callbackData);
96     int64_t display;
97 
98     h2a::translate(hwcDisplay, display);
99     hal->getEventCallback()->onVsyncIdle(display);
100 }
101 
seamlessPossible(hwc2_callback_data_t callbackData,hwc2_display_t hwcDisplay)102 void seamlessPossible(hwc2_callback_data_t callbackData, hwc2_display_t hwcDisplay) {
103     auto hal = static_cast<HalImpl*>(callbackData);
104     int64_t display;
105 
106     h2a::translate(hwcDisplay, display);
107     hal->getEventCallback()->onSeamlessPossible(display);
108 }
109 
refreshRateChangedDebug(hwc2_callback_data_t callbackData,hwc2_display_t hwcDisplay,hwc2_vsync_period_t hwcVsyncPeriodNanos,int32_t hwcRefreshPeriodNanos)110 void refreshRateChangedDebug(hwc2_callback_data_t callbackData, hwc2_display_t hwcDisplay,
111                              hwc2_vsync_period_t hwcVsyncPeriodNanos,
112                              int32_t hwcRefreshPeriodNanos) {
113     auto hal = static_cast<HalImpl*>(callbackData);
114     int64_t display;
115     int32_t vsyncPeriodNanos;
116 
117     h2a::translate(hwcDisplay, display);
118     h2a::translate(hwcVsyncPeriodNanos, vsyncPeriodNanos);
119     // TODO (b/314527560) Update refreshPeriodNanos for VRR display
120     hal->getEventCallback()->onRefreshRateChangedDebug(RefreshRateChangedDebugData{
121             .display = display,
122             .vsyncPeriodNanos = vsyncPeriodNanos,
123             .refreshPeriodNanos = hwcRefreshPeriodNanos,
124     });
125 }
126 
hotplugEvent(hwc2_callback_data_t callbackData,hwc2_display_t hwcDisplay,common::DisplayHotplugEvent hotplugEvent)127 void hotplugEvent(hwc2_callback_data_t callbackData, hwc2_display_t hwcDisplay,
128                   common::DisplayHotplugEvent hotplugEvent) {
129     auto hal = static_cast<HalImpl*>(callbackData);
130     int64_t display;
131 
132     h2a::translate(hwcDisplay, display);
133     hal->getEventCallback()->onHotplugEvent(display, hotplugEvent);
134 }
135 
hdcpLevelsChanged(hwc2_callback_data_t callbackData,hwc2_display_t hwcDisplay,HdcpLevels levels)136 void hdcpLevelsChanged(hwc2_callback_data_t callbackData, hwc2_display_t hwcDisplay,
137                        HdcpLevels levels) {
138     auto hal = static_cast<HalImpl*>(callbackData);
139     int64_t display;
140 
141     h2a::translate(hwcDisplay, display);
142     hal->getEventCallback()->onHdcpLevelsChanged(display, levels);
143 }
144 
145 } // nampesapce hook
146 
HalImpl(std::unique_ptr<ExynosDevice> device,bool batchingSupported)147 HalImpl::HalImpl(std::unique_ptr<ExynosDevice> device, bool batchingSupported)
148       : mDevice(std::move(device)) {
149     initCaps(batchingSupported);
150 #ifdef USES_HWC_SERVICES
151     LOG(DEBUG) << "Start HWCService";
152     mHwcCtx = std::make_unique<ExynosHWCCtx>();
153     memset(&mHwcCtx->base, 0, sizeof(mHwcCtx->base));
154     mHwcCtx->device = mDevice.get();
155 
156     auto hwcService = ::android::ExynosHWCService::getExynosHWCService();
157     hwcService->setExynosHWCCtx(mHwcCtx.get());
158     // This callback is for DP hotplug event if connected
159     // hwcService->setBootFinishedCallback(...);
160 #endif
161 }
162 
initCaps(bool batchingSupported)163 void HalImpl::initCaps(bool batchingSupported) {
164     uint32_t count = 0;
165     mDevice->getCapabilities(&count, nullptr);
166 
167     std::vector<int32_t> halCaps(count);
168     mDevice->getCapabilities(&count, halCaps.data());
169 
170     for (auto hwcCap : halCaps) {
171         Capability cap;
172         h2a::translate(hwcCap, cap);
173         mCaps.insert(cap);
174     }
175 
176     mCaps.insert(Capability::BOOT_DISPLAY_CONFIG);
177     mCaps.insert(Capability::REFRESH_RATE_CHANGED_CALLBACK_DEBUG);
178     if (batchingSupported) {
179         mCaps.insert(Capability::LAYER_LIFECYCLE_BATCH_COMMAND);
180     }
181 }
182 
getHalDisplay(int64_t display,ExynosDisplay * & halDisplay)183 int32_t HalImpl::getHalDisplay(int64_t display, ExynosDisplay*& halDisplay) {
184     hwc2_display_t hwcDisplay;
185     a2h::translate(display, hwcDisplay);
186     halDisplay = mDevice->getDisplay(static_cast<uint32_t>(hwcDisplay));
187 
188     if (!halDisplay) { [[unlikely]]
189         return HWC2_ERROR_BAD_DISPLAY;
190     }
191     return HWC2_ERROR_NONE;
192 }
193 
getHalLayer(int64_t display,int64_t layer,ExynosLayer * & halLayer)194 int32_t HalImpl::getHalLayer(int64_t display, int64_t layer, ExynosLayer*& halLayer) {
195     ExynosDisplay* halDisplay;
196     RET_IF_ERR(getHalDisplay(display, halDisplay));
197 
198     hwc2_layer_t mapped_layer;
199     RET_IF_ERR(layerSf2Hwc(display, layer, mapped_layer));
200     halLayer = halDisplay->checkLayer(mapped_layer);
201     if (!halLayer) { [[unlikely]]
202         return HWC2_ERROR_BAD_LAYER;
203     }
204 
205     return HWC2_ERROR_NONE;
206 }
207 
layerSf2Hwc(int64_t display,int64_t layer,hwc2_layer_t & outMappedLayer)208 int32_t HalImpl::layerSf2Hwc(int64_t display, int64_t layer, hwc2_layer_t& outMappedLayer) {
209     ExynosDisplay* halDisplay;
210     RET_IF_ERR(getHalDisplay(display, halDisplay));
211     auto iter = mSfLayerToHalLayerMap.find(layer);
212     if (iter == mSfLayerToHalLayerMap.end()) {
213         return HWC2_ERROR_BAD_LAYER;
214     }
215     outMappedLayer = iter->second;
216     return HWC2_ERROR_NONE;
217 }
218 
hasCapability(Capability cap)219 bool HalImpl::hasCapability(Capability cap) {
220     return mCaps.find(cap) != mCaps.end();
221 }
222 
getCapabilities(std::vector<Capability> * caps)223 void HalImpl::getCapabilities(std::vector<Capability>* caps) {
224     caps->clear();
225     caps->insert(caps->begin(), mCaps.begin(), mCaps.end());
226 }
227 
dumpDebugInfo(std::string * output,const std::vector<std::string> & args)228 void HalImpl::dumpDebugInfo(std::string* output, const std::vector<std::string>& args /* = {} */) {
229     if (output == nullptr) return;
230 
231     String8 result;
232     mDevice->dump(result, args);
233 
234     output->resize(result.size());
235     output->assign(result.c_str());
236 }
237 
registerEventCallback(EventCallback * callback)238 void HalImpl::registerEventCallback(EventCallback* callback) {
239     mEventCallback = callback;
240 
241     mDevice->registerCallback(HWC2_CALLBACK_HOTPLUG, this,
242                               reinterpret_cast<hwc2_function_pointer_t>(hook::hotplug));
243     mDevice->registerCallback(HWC2_CALLBACK_REFRESH, this,
244                               reinterpret_cast<hwc2_function_pointer_t>(hook::refresh));
245     mDevice->registerCallback(HWC2_CALLBACK_VSYNC_2_4, this,
246                      reinterpret_cast<hwc2_function_pointer_t>(hook::vsync));
247     mDevice->registerCallback(HWC2_CALLBACK_VSYNC_PERIOD_TIMING_CHANGED, this,
248                      reinterpret_cast<hwc2_function_pointer_t>(hook::vsyncPeriodTimingChanged));
249     mDevice->registerCallback(HWC2_CALLBACK_SEAMLESS_POSSIBLE, this,
250                      reinterpret_cast<hwc2_function_pointer_t>(hook::seamlessPossible));
251 
252     // register HWC3 Callback
253     mDevice->registerHwc3Callback(IComposerCallback::TRANSACTION_onVsyncIdle, this,
254                                   reinterpret_cast<hwc2_function_pointer_t>(hook::vsyncIdle));
255     mDevice->registerHwc3Callback(IComposerCallback::TRANSACTION_onRefreshRateChangedDebug, this,
256                                   reinterpret_cast<hwc2_function_pointer_t>(
257                                           hook::refreshRateChangedDebug));
258     // Don't register onHotplugEvent until it's available in nextfood (b/323291596)
259     // mDevice->registerHwc3Callback(IComposerCallback::TRANSACTION_onHotplugEvent, this,
260     //                             reinterpret_cast<hwc2_function_pointer_t>(hook::hotplugEvent));
261     // Don't register onHdcpLevelsChanged until it's available in nextfood
262     // mDevice->registerHwc3Callback(IComposerCallback::TRANSACTION_onHdcpLevelsChanged, this,
263     //                             reinterpret_cast<hwc2_function_pointer_t>(hook::hdcpLevelsChanged));
264 }
265 
unregisterEventCallback()266 void HalImpl::unregisterEventCallback() {
267     mDevice->registerCallback(HWC2_CALLBACK_HOTPLUG, this, nullptr);
268     mDevice->registerCallback(HWC2_CALLBACK_REFRESH, this, nullptr);
269     mDevice->registerCallback(HWC2_CALLBACK_VSYNC_2_4, this, nullptr);
270     mDevice->registerCallback(HWC2_CALLBACK_VSYNC_PERIOD_TIMING_CHANGED, this, nullptr);
271     mDevice->registerCallback(HWC2_CALLBACK_SEAMLESS_POSSIBLE, this, nullptr);
272 
273     // unregister HWC3 Callback
274     mDevice->registerHwc3Callback(IComposerCallback::TRANSACTION_onVsyncIdle, this, nullptr);
275     mDevice->registerHwc3Callback(IComposerCallback::TRANSACTION_onRefreshRateChangedDebug, this,
276                                   nullptr);
277     // Don't register onHotplugEvent until it's available in nextfood (b/323291596)
278     // mDevice->registerHwc3Callback(IComposerCallback::TRANSACTION_onHotplugEvent, this, nullptr);
279     // Don't register onHdcpLevelsChanged until it's available in nextfood
280     // mDevice->registerHwc3Callback(IComposerCallback::TRANSACTION_onHdcpLevelsChanged, this,
281     // nullptr);
282 
283     mEventCallback = nullptr;
284 }
285 
acceptDisplayChanges(int64_t display)286 int32_t HalImpl::acceptDisplayChanges(int64_t display) {
287     ExynosDisplay* halDisplay;
288     RET_IF_ERR(getHalDisplay(display, halDisplay));
289 
290     return halDisplay->acceptDisplayChanges();
291 }
292 
createLayer(int64_t display,int64_t * outLayer)293 int32_t HalImpl::createLayer(int64_t display, int64_t* outLayer) {
294     ExynosDisplay* halDisplay;
295     RET_IF_ERR(getHalDisplay(display, halDisplay));
296 
297     hwc2_layer_t hwcLayer = 0;
298     RET_IF_ERR(halDisplay->createLayer(&hwcLayer));
299 
300     h2a::translate(hwcLayer, *outLayer);
301     // Adding this to stay backward compatible with new batching command,
302     // if HWC supports batching, and create does not.
303     mSfLayerToHalLayerMap[*outLayer] = hwcLayer;
304     mHalLayerToSfLayerMap[hwcLayer] = *outLayer;
305     return HWC2_ERROR_NONE;
306 }
307 
batchedCreateDestroyLayer(int64_t display,int64_t layer,LayerLifecycleBatchCommandType cmd)308 int32_t HalImpl::batchedCreateDestroyLayer(int64_t display, int64_t layer,
309                                            LayerLifecycleBatchCommandType cmd) {
310     int32_t err = HWC2_ERROR_NONE;
311     ExynosDisplay* halDisplay;
312     RET_IF_ERR(getHalDisplay(display, halDisplay));
313     if (cmd == LayerLifecycleBatchCommandType::CREATE) {
314         if (mSfLayerToHalLayerMap.find(layer) != mSfLayerToHalLayerMap.end()) {
315             return HWC2_ERROR_BAD_LAYER;
316         }
317         hwc2_layer_t hwcLayer = 0;
318         RET_IF_ERR(halDisplay->createLayer(&hwcLayer));
319         int64_t hwclayerAidl;
320         h2a::translate(hwcLayer, hwclayerAidl);
321         mSfLayerToHalLayerMap[layer] = hwclayerAidl;
322 
323         mHalLayerToSfLayerMap[hwcLayer] = layer;
324     } else if (cmd == LayerLifecycleBatchCommandType::DESTROY) {
325         int64_t HalLayerAidl;
326         ExynosLayer* halLayer;
327         auto iter = mSfLayerToHalLayerMap.find(layer);
328         if (iter == mSfLayerToHalLayerMap.end()) {
329             return HWC2_ERROR_BAD_LAYER;
330         }
331         HalLayerAidl = iter->second;
332 
333         RET_IF_ERR(getHalLayer(display, layer, halLayer));
334         err = halDisplay->destroyLayer(reinterpret_cast<hwc2_layer_t>(halLayer));
335         if (err != HWC2_ERROR_NONE) {
336             ALOGW("HalImpl: destroyLayer failed with error: %u", err);
337         }
338         mSfLayerToHalLayerMap.erase(iter);
339         auto iterator = mHalLayerToSfLayerMap.find(reinterpret_cast<hwc2_layer_t>(halLayer));
340         if (iterator == mHalLayerToSfLayerMap.end()) {
341             return HWC2_ERROR_BAD_LAYER;
342         }
343 
344         mHalLayerToSfLayerMap.erase(iterator);
345     }
346     return err;
347 }
348 
destroyLayer(int64_t display,int64_t layer)349 int32_t HalImpl::destroyLayer(int64_t display, int64_t layer) {
350     int32_t err = HWC2_ERROR_NONE;
351     ExynosDisplay* halDisplay;
352     RET_IF_ERR(getHalDisplay(display, halDisplay));
353 
354     ExynosLayer *halLayer;
355     RET_IF_ERR(getHalLayer(display, layer, halLayer));
356     err = halDisplay->destroyLayer(reinterpret_cast<hwc2_layer_t>(halLayer));
357     auto iter = mSfLayerToHalLayerMap.find(layer);
358     if (iter != mSfLayerToHalLayerMap.end()) {
359         mSfLayerToHalLayerMap.erase(iter);
360     }
361     auto iterator = mHalLayerToSfLayerMap.find(reinterpret_cast<hwc2_layer_t>(halLayer));
362     if (iterator != mHalLayerToSfLayerMap.end()) {
363         mHalLayerToSfLayerMap.erase(iterator);
364     }
365     return err;
366 }
367 
createVirtualDisplay(uint32_t width,uint32_t height,AidlPixelFormat format,VirtualDisplay * outDisplay)368 int32_t HalImpl::createVirtualDisplay(uint32_t width, uint32_t height, AidlPixelFormat format,
369                                       VirtualDisplay* outDisplay) {
370     int32_t hwcFormat;
371     a2h::translate(format, hwcFormat);
372     hwc2_display_t hwcDisplay = getDisplayId(HWC_DISPLAY_VIRTUAL, 0);
373     auto halDisplay = mDevice->getDisplay(static_cast<uint32_t>(hwcDisplay));
374     if (!halDisplay) {
375         return HWC2_ERROR_BAD_PARAMETER;
376     }
377 
378     RET_IF_ERR(mDevice->createVirtualDisplay(width, height, &hwcFormat, halDisplay));
379 
380     h2a::translate(hwcDisplay, outDisplay->display);
381     h2a::translate(hwcFormat, outDisplay->format);
382 
383     return HWC2_ERROR_NONE;
384 }
385 
destroyVirtualDisplay(int64_t display)386 int32_t HalImpl::destroyVirtualDisplay(int64_t display) {
387     ExynosDisplay* halDisplay;
388     RET_IF_ERR(getHalDisplay(display, halDisplay));
389 
390     return mDevice->destroyVirtualDisplay(halDisplay);
391 }
392 
getActiveConfig(int64_t display,int32_t * outConfig)393 int32_t HalImpl::getActiveConfig(int64_t display, int32_t* outConfig) {
394     ExynosDisplay* halDisplay;
395     RET_IF_ERR(getHalDisplay(display, halDisplay));
396 
397     hwc2_config_t hwcConfig;
398     RET_IF_ERR(halDisplay->getActiveConfig(&hwcConfig));
399 
400     h2a::translate(hwcConfig, *outConfig);
401     return HWC2_ERROR_NONE;
402 }
403 
getColorModes(int64_t display,std::vector<ColorMode> * outModes)404 int32_t HalImpl::getColorModes(int64_t display, std::vector<ColorMode>* outModes) {
405     ExynosDisplay* halDisplay;
406     RET_IF_ERR(getHalDisplay(display, halDisplay));
407 
408     uint32_t count = 0;
409     RET_IF_ERR(halDisplay->getColorModes(&count, nullptr));
410 
411     std::vector<int32_t> hwcModes(count);
412     RET_IF_ERR(halDisplay->getColorModes(&count, hwcModes.data()));
413 
414     h2a::translate(hwcModes, *outModes);
415     return HWC2_ERROR_NONE;
416 }
417 
getDataspaceSaturationMatrix(common::Dataspace dataspace,std::vector<float> * matrix)418 int32_t HalImpl::getDataspaceSaturationMatrix([[maybe_unused]] common::Dataspace dataspace,
419                                               std::vector<float>* matrix) {
420     // Pixel HWC does not support dataspace saturation matrix, return unit matrix.
421     std::vector<float> unitMatrix = {
422         1.0f, 0.0f, 0.0f, 0.0f,
423         0.0f, 1.0f, 0.0f, 0.0f,
424         0.0f, 0.0f, 1.0f, 0.0f,
425         0.0f, 0.0f, 0.0f, 1.0f,
426     };
427 
428     *matrix = std::move(unitMatrix);
429     return HWC2_ERROR_NONE;
430 }
431 
getDisplayAttribute(int64_t display,int32_t config,DisplayAttribute attribute,int32_t * outValue)432 int32_t HalImpl::getDisplayAttribute(int64_t display, int32_t config,
433                                      DisplayAttribute attribute, int32_t* outValue) {
434     ExynosDisplay* halDisplay;
435     RET_IF_ERR(getHalDisplay(display, halDisplay));
436 
437     hwc2_config_t hwcConfig;
438     int32_t hwcAttr;
439     a2h::translate(config, hwcConfig);
440     a2h::translate(attribute, hwcAttr);
441 
442     auto err = halDisplay->getDisplayAttribute(hwcConfig, hwcAttr, outValue);
443     if (err != HWC2_ERROR_NONE && *outValue == -1) {
444         return HWC2_ERROR_BAD_PARAMETER;
445     }
446     return HWC2_ERROR_NONE;
447 }
448 
getDisplayBrightnessSupport(int64_t display,bool & outSupport)449 int32_t HalImpl::getDisplayBrightnessSupport(int64_t display, bool& outSupport) {
450     ExynosDisplay* halDisplay;
451     RET_IF_ERR(getHalDisplay(display, halDisplay));
452 
453     return halDisplay->getDisplayBrightnessSupport(&outSupport);
454 }
455 
getDisplayCapabilities(int64_t display,std::vector<DisplayCapability> * caps)456 int32_t HalImpl::getDisplayCapabilities(int64_t display,
457                                         std::vector<DisplayCapability>* caps) {
458     ExynosDisplay* halDisplay;
459     RET_IF_ERR(getHalDisplay(display, halDisplay));
460 
461     uint32_t count = 0;
462     RET_IF_ERR(halDisplay->getDisplayCapabilities(&count, nullptr));
463 
464     std::vector<uint32_t> hwcCaps(count);
465     RET_IF_ERR(halDisplay->getDisplayCapabilities(&count, hwcCaps.data()));
466 
467     h2a::translate(hwcCaps, *caps);
468     return HWC2_ERROR_NONE;
469 }
470 
getDisplayConfigs(int64_t display,std::vector<int32_t> * configs)471 int32_t HalImpl::getDisplayConfigs(int64_t display, std::vector<int32_t>* configs) {
472     ExynosDisplay* halDisplay;
473     RET_IF_ERR(getHalDisplay(display, halDisplay));
474 
475     uint32_t count = 0;
476     RET_IF_ERR(halDisplay->getDisplayConfigs(&count, nullptr));
477 
478     std::vector<hwc2_config_t> hwcConfigs(count);
479     RET_IF_ERR(halDisplay->getDisplayConfigs(&count, hwcConfigs.data()));
480 
481     h2a::translate(hwcConfigs, *configs);
482     return HWC2_ERROR_NONE;
483 }
484 
getDisplayConfigurations(int64_t display,int32_t,std::vector<DisplayConfiguration> * outConfigs)485 int32_t HalImpl::getDisplayConfigurations(int64_t display, int32_t,
486                                           std::vector<DisplayConfiguration>* outConfigs) {
487     ExynosDisplay* halDisplay;
488     RET_IF_ERR(getHalDisplay(display, halDisplay));
489 
490     std::vector<int32_t> configIds;
491     RET_IF_ERR(getDisplayConfigs(display, &configIds));
492 
493     for (const auto configId : configIds) {
494         DisplayConfiguration config;
495         config.configId = configId;
496         // Get required display attributes
497         RET_IF_ERR(getDisplayAttribute(display, configId, DisplayAttribute::WIDTH, &config.width));
498         RET_IF_ERR(
499                 getDisplayAttribute(display, configId, DisplayAttribute::HEIGHT, &config.height));
500         RET_IF_ERR(getDisplayAttribute(display, configId, DisplayAttribute::VSYNC_PERIOD,
501                                        &config.vsyncPeriod));
502         RET_IF_ERR(getDisplayAttribute(display, configId, DisplayAttribute::CONFIG_GROUP,
503                                        &config.configGroup));
504         // Get optional display attributes
505         int32_t dpiX, dpiY;
506         auto statusDpiX = getDisplayAttribute(display, configId, DisplayAttribute::DPI_X, &dpiX);
507         auto statusDpiY = getDisplayAttribute(display, configId, DisplayAttribute::DPI_Y, &dpiY);
508         // TODO(b/294120341): getDisplayAttribute for DPI should return dots per inch
509         if (statusDpiX == HWC2_ERROR_NONE && statusDpiY == HWC2_ERROR_NONE) {
510             config.dpi = {dpiX / 1000.0f, dpiY / 1000.0f};
511         }
512         // Determine whether there is a need to configure VRR.
513         hwc2_config_t hwcConfigId;
514         a2h::translate(configId, hwcConfigId);
515         std::optional<VrrConfig_t> vrrConfig = halDisplay->getVrrConfigs(hwcConfigId);
516         if (vrrConfig.has_value() && vrrConfig->isFullySupported) {
517             // TODO(b/290843234): complete the remaining values within vrrConfig.
518             VrrConfig hwc3VrrConfig;
519             VrrConfig::NotifyExpectedPresentConfig notifyExpectedPresentConfig;
520             hwc3VrrConfig.minFrameIntervalNs = vrrConfig->minFrameIntervalNs;
521             if (vrrConfig->notifyExpectedPresentConfig.has_value()) {
522                 notifyExpectedPresentConfig.headsUpNs =
523                         vrrConfig->notifyExpectedPresentConfig->HeadsUpNs;
524                 notifyExpectedPresentConfig.timeoutNs =
525                         vrrConfig->notifyExpectedPresentConfig->TimeoutNs;
526                 hwc3VrrConfig.notifyExpectedPresentConfig =
527                         std::make_optional(notifyExpectedPresentConfig);
528             }
529             config.vrrConfig = std::make_optional(hwc3VrrConfig);
530         }
531         outConfigs->push_back(config);
532     }
533 
534     return HWC2_ERROR_NONE;
535 }
536 
notifyExpectedPresent(int64_t display,const ClockMonotonicTimestamp & expectedPresentTime,int32_t frameIntervalNs)537 int32_t HalImpl::notifyExpectedPresent(int64_t display,
538                                        const ClockMonotonicTimestamp& expectedPresentTime,
539                                        int32_t frameIntervalNs) {
540     ExynosDisplay* halDisplay;
541     RET_IF_ERR(getHalDisplay(display, halDisplay));
542 
543     RET_IF_ERR(
544             halDisplay->notifyExpectedPresent(expectedPresentTime.timestampNanos, frameIntervalNs));
545     return HWC2_ERROR_NONE;
546 }
547 
getDisplayConnectionType(int64_t display,DisplayConnectionType * outType)548 int32_t HalImpl::getDisplayConnectionType(int64_t display, DisplayConnectionType* outType) {
549     ExynosDisplay* halDisplay;
550     RET_IF_ERR(getHalDisplay(display, halDisplay));
551 
552     uint32_t hwcType = HWC2_DISPLAY_CONNECTION_TYPE_INTERNAL;
553     RET_IF_ERR(halDisplay->getDisplayConnectionType(&hwcType));
554     h2a::translate(hwcType, *outType);
555 
556     return HWC2_ERROR_NONE;
557 }
558 
getDisplayIdentificationData(int64_t display,DisplayIdentification * id)559 int32_t HalImpl::getDisplayIdentificationData(int64_t display, DisplayIdentification *id) {
560     ExynosDisplay* halDisplay;
561     RET_IF_ERR(getHalDisplay(display, halDisplay));
562 
563     uint8_t port;
564     uint32_t count = 0;
565     RET_IF_ERR(halDisplay->getDisplayIdentificationData(&port, &count, nullptr));
566 
567     id->data.resize(count);
568     RET_IF_ERR(halDisplay->getDisplayIdentificationData(&port, &count, id->data.data()));
569 
570     h2a::translate(port, id->port);
571     return HWC2_ERROR_NONE;
572 }
573 
getDisplayName(int64_t display,std::string * outName)574 int32_t HalImpl::getDisplayName(int64_t display, std::string* outName) {
575     ExynosDisplay* halDisplay;
576     RET_IF_ERR(getHalDisplay(display, halDisplay));
577 
578     uint32_t count = 0;
579     RET_IF_ERR(halDisplay->getDisplayName(&count, nullptr));
580 
581     outName->resize(count);
582     RET_IF_ERR(halDisplay->getDisplayName(&count, outName->data()));
583 
584     return HWC2_ERROR_NONE;
585 }
586 
getDisplayVsyncPeriod(int64_t display,int32_t * outVsyncPeriod)587 int32_t HalImpl::getDisplayVsyncPeriod(int64_t display, int32_t* outVsyncPeriod) {
588     ExynosDisplay* halDisplay;
589     RET_IF_ERR(getHalDisplay(display, halDisplay));
590 
591     hwc2_vsync_period_t hwcVsyncPeriod;
592     RET_IF_ERR(halDisplay->getDisplayVsyncPeriod(&hwcVsyncPeriod));
593 
594     h2a::translate(hwcVsyncPeriod, *outVsyncPeriod);
595     return HWC2_ERROR_NONE;
596 }
597 
getDisplayedContentSample(int64_t display,int64_t maxFrames,int64_t timestamp,DisplayContentSample * samples)598 int32_t HalImpl::getDisplayedContentSample([[maybe_unused]] int64_t display,
599                                            [[maybe_unused]] int64_t maxFrames,
600                                            [[maybe_unused]] int64_t timestamp,
601                                            [[maybe_unused]] DisplayContentSample* samples) {
602     return HWC2_ERROR_UNSUPPORTED;
603 }
604 
getDisplayedContentSamplingAttributes(int64_t display,DisplayContentSamplingAttributes * attrs)605 int32_t HalImpl::getDisplayedContentSamplingAttributes(
606         [[maybe_unused]] int64_t display,
607         [[maybe_unused]] DisplayContentSamplingAttributes* attrs) {
608     return HWC2_ERROR_UNSUPPORTED;
609 }
610 
getDisplayPhysicalOrientation(int64_t display,common::Transform * orientation)611 int32_t HalImpl::getDisplayPhysicalOrientation(int64_t display,
612                                                common::Transform* orientation) {
613     ExynosDisplay* halDisplay;
614     RET_IF_ERR(getHalDisplay(display, halDisplay));
615 
616     HwcMountOrientation hwcOrientation;
617     RET_IF_ERR(halDisplay->getMountOrientation(&hwcOrientation));
618     h2a::translate(hwcOrientation, *orientation);
619 
620     return HWC2_ERROR_NONE;
621 }
622 
getDozeSupport(int64_t display,bool & support)623 int32_t HalImpl::getDozeSupport(int64_t display, bool& support) {
624     ExynosDisplay* halDisplay;
625     RET_IF_ERR(getHalDisplay(display, halDisplay));
626 
627     int32_t hwcSupport;
628     RET_IF_ERR(halDisplay->getDozeSupport(&hwcSupport));
629 
630     h2a::translate(hwcSupport, support);
631     return HWC2_ERROR_NONE;
632 }
633 
getHdrCapabilities(int64_t display,HdrCapabilities * caps)634 int32_t HalImpl::getHdrCapabilities(int64_t display, HdrCapabilities* caps) {
635     ExynosDisplay* halDisplay;
636     RET_IF_ERR(getHalDisplay(display, halDisplay));
637 
638     uint32_t count = 0;
639     RET_IF_ERR(halDisplay->getHdrCapabilities(&count, nullptr, &caps->maxLuminance,
640                                               &caps->maxAverageLuminance,
641                                               &caps->minLuminance));
642     std::vector<int32_t> hwcHdrTypes(count);
643     RET_IF_ERR(halDisplay->getHdrCapabilities(&count, hwcHdrTypes.data(),
644                                               &caps->maxLuminance,
645                                               &caps->maxAverageLuminance,
646                                               &caps->minLuminance));
647 
648     h2a::translate(hwcHdrTypes, caps->types);
649     return HWC2_ERROR_NONE;
650 }
651 
getOverlaySupport(OverlayProperties * caps)652 int32_t HalImpl::getOverlaySupport(OverlayProperties* caps) {
653     return mDevice->getOverlaySupport(caps);
654 }
655 
getMaxVirtualDisplayCount(int32_t * count)656 int32_t HalImpl::getMaxVirtualDisplayCount(int32_t* count) {
657     uint32_t hwcCount = mDevice->getMaxVirtualDisplayCount();
658     h2a::translate(hwcCount, *count);
659 
660     return HWC2_ERROR_NONE;
661 }
662 
getPerFrameMetadataKeys(int64_t display,std::vector<PerFrameMetadataKey> * keys)663 int32_t HalImpl::getPerFrameMetadataKeys(int64_t display,
664                                          std::vector<PerFrameMetadataKey>* keys) {
665     ExynosDisplay* halDisplay;
666     RET_IF_ERR(getHalDisplay(display, halDisplay));
667 
668     uint32_t numKeys = 0;
669     auto resManager = mDevice->mResourceManager;
670     if (resManager->hasHDR10PlusMPP()) {
671         numKeys = HWC2_HDR10_PLUS_SEI + 1;
672     } else {
673         numKeys = HWC2_MAX_FRAME_AVERAGE_LIGHT_LEVEL + 1;
674     }
675     for (uint32_t i = 0; i < numKeys; ++i) {
676         PerFrameMetadataKey key;
677         h2a::translate(i, key);
678         keys->push_back(key);
679     }
680 
681     return HWC2_ERROR_NONE;
682 }
683 
getReadbackBufferAttributes(int64_t display,ReadbackBufferAttributes * attrs)684 int32_t HalImpl::getReadbackBufferAttributes(int64_t display,
685                                              ReadbackBufferAttributes* attrs) {
686     ExynosDisplay* halDisplay;
687     RET_IF_ERR(getHalDisplay(display, halDisplay));
688 
689     int32_t format = -1;
690     int32_t dataspace = -1;
691     RET_IF_ERR(halDisplay->getReadbackBufferAttributes(&format, &dataspace));
692 
693     h2a::translate(format, attrs->format);
694     h2a::translate(dataspace, attrs->dataspace);
695 
696     return HWC2_ERROR_NONE;
697 }
698 
getReadbackBufferFence(int64_t display,ndk::ScopedFileDescriptor * acquireFence)699 int32_t HalImpl::getReadbackBufferFence(int64_t display,
700                                         ndk::ScopedFileDescriptor* acquireFence) {
701     ExynosDisplay* halDisplay;
702     RET_IF_ERR(getHalDisplay(display, halDisplay));
703 
704     int32_t fd = -1;
705     RET_IF_ERR(halDisplay->getReadbackBufferFence(&fd));
706 
707     h2a::translate(fd, *acquireFence);
708     return HWC2_ERROR_NONE;
709 }
710 
getRenderIntents(int64_t display,ColorMode mode,std::vector<RenderIntent> * intents)711 int32_t HalImpl::getRenderIntents(int64_t display, ColorMode mode,
712                                   std::vector<RenderIntent>* intents) {
713     ExynosDisplay* halDisplay;
714     RET_IF_ERR(getHalDisplay(display, halDisplay));
715 
716     int32_t hwcMode;
717     uint32_t count = 0;
718     a2h::translate(mode, hwcMode);
719     RET_IF_ERR(halDisplay->getRenderIntents(hwcMode, &count, nullptr));
720 
721     std::vector<int32_t> hwcIntents(count);
722     RET_IF_ERR(halDisplay->getRenderIntents(hwcMode, &count, hwcIntents.data()));
723 
724     h2a::translate(hwcIntents, *intents);
725     return HWC2_ERROR_NONE;
726 }
727 
getSupportedContentTypes(int64_t display,std::vector<ContentType> * types)728 int32_t HalImpl::getSupportedContentTypes(int64_t display, std::vector<ContentType>* types) {
729     ExynosDisplay* halDisplay;
730     RET_IF_ERR(getHalDisplay(display, halDisplay));
731 
732     uint32_t count = 0;
733     RET_IF_ERR(halDisplay->getSupportedContentTypes(&count, nullptr));
734 
735     std::vector<uint32_t> hwcTypes(count);
736     RET_IF_ERR(halDisplay->getSupportedContentTypes(&count, hwcTypes.data()));
737 
738     h2a::translate(hwcTypes, *types);
739     return HWC2_ERROR_NONE;
740 }
741 
flushDisplayBrightnessChange(int64_t display)742 int32_t HalImpl::flushDisplayBrightnessChange(int64_t display) {
743     ExynosDisplay* halDisplay;
744     RET_IF_ERR(getHalDisplay(display, halDisplay));
745 
746     return halDisplay->flushDisplayBrightnessChange();
747 }
748 
presentDisplay(int64_t display,ndk::ScopedFileDescriptor & fence,std::vector<int64_t> * outLayers,std::vector<ndk::ScopedFileDescriptor> * outReleaseFences)749 int32_t HalImpl::presentDisplay(int64_t display, ndk::ScopedFileDescriptor& fence,
750                        std::vector<int64_t>* outLayers,
751                        std::vector<ndk::ScopedFileDescriptor>* outReleaseFences) {
752     ExynosDisplay* halDisplay;
753     RET_IF_ERR(getHalDisplay(display, halDisplay));
754 
755    // TODO: not expect acceptDisplayChanges if there are no changes to accept
756     if (halDisplay->mRenderingState == RENDERING_STATE_VALIDATED) {
757         LOG(INFO) << halDisplay->mDisplayName.c_str()
758                    << ": acceptDisplayChanges was not called";
759         if (halDisplay->acceptDisplayChanges() != HWC2_ERROR_NONE) {
760             LOG(ERROR) << halDisplay->mDisplayName.c_str()
761             << ": acceptDisplayChanges is failed";
762         }
763     }
764 
765     int32_t hwcFence;
766     RET_IF_ERR(halDisplay->presentDisplay(&hwcFence));
767     h2a::translate(hwcFence, fence);
768 
769     uint32_t count = 0;
770     RET_IF_ERR(halDisplay->getReleaseFences(&count, nullptr, nullptr));
771 
772     std::vector<hwc2_layer_t> hwcLayers(count);
773     std::vector<int32_t> hwcFences(count);
774     RET_IF_ERR(halDisplay->getReleaseFences(&count, hwcLayers.data(), hwcFences.data()));
775     std::vector<int64_t> sfLayers(count);
776 
777     for (int i = 0; i < count; i++) {
778         auto iter = mHalLayerToSfLayerMap.find(hwcLayers[i]);
779         if (iter != mHalLayerToSfLayerMap.end()) {
780             sfLayers[i] = iter->second;
781         } else {
782             LOG(ERROR) << "HalImpl::presentDisplay incorrect hal mapping. ";
783         }
784     }
785     h2a::translate(sfLayers, *outLayers);
786     h2a::translate(hwcFences, *outReleaseFences);
787 
788     return HWC2_ERROR_NONE;
789 }
790 
setActiveConfig(int64_t display,int32_t config)791 int32_t HalImpl::setActiveConfig(int64_t display, int32_t config) {
792     ExynosDisplay* halDisplay;
793     RET_IF_ERR(getHalDisplay(display, halDisplay));
794 
795     hwc2_config_t hwcConfig;
796     a2h::translate(config, hwcConfig);
797     return halDisplay->setActiveConfig(hwcConfig);
798 }
799 
setActiveConfigWithConstraints(int64_t display,int32_t config,const VsyncPeriodChangeConstraints & vsyncPeriodChangeConstraints,VsyncPeriodChangeTimeline * timeline)800 int32_t HalImpl::setActiveConfigWithConstraints(
801             int64_t display, int32_t config,
802             const VsyncPeriodChangeConstraints& vsyncPeriodChangeConstraints,
803             VsyncPeriodChangeTimeline* timeline) {
804     ExynosDisplay* halDisplay;
805     RET_IF_ERR(getHalDisplay(display, halDisplay));
806 
807     hwc2_config_t hwcConfig;
808     hwc_vsync_period_change_constraints_t hwcConstraints;
809     hwc_vsync_period_change_timeline_t hwcTimeline;
810 
811     a2h::translate(config, hwcConfig);
812     a2h::translate(vsyncPeriodChangeConstraints, hwcConstraints);
813     RET_IF_ERR(halDisplay->setActiveConfigWithConstraints(hwcConfig, &hwcConstraints, &hwcTimeline));
814 
815     h2a::translate(hwcTimeline, *timeline);
816     return HWC2_ERROR_NONE;
817 }
818 
setBootDisplayConfig(int64_t display,int32_t config)819 int32_t HalImpl::setBootDisplayConfig(int64_t display, int32_t config) {
820     ExynosDisplay* halDisplay;
821     RET_IF_ERR(getHalDisplay(display, halDisplay));
822 
823     return halDisplay->setBootDisplayConfig(config);
824 }
825 
clearBootDisplayConfig(int64_t display)826 int32_t HalImpl::clearBootDisplayConfig(int64_t display) {
827     ExynosDisplay* halDisplay;
828     RET_IF_ERR(getHalDisplay(display, halDisplay));
829 
830     return halDisplay->clearBootDisplayConfig();
831 }
832 
getPreferredBootDisplayConfig(int64_t display,int32_t * config)833 int32_t HalImpl::getPreferredBootDisplayConfig(int64_t display, int32_t* config) {
834     ExynosDisplay* halDisplay;
835     RET_IF_ERR(getHalDisplay(display, halDisplay));
836 
837     return halDisplay->getPreferredBootDisplayConfig(config);
838 }
839 
getHdrConversionCapabilities(std::vector<common::HdrConversionCapability> *)840 int32_t HalImpl::getHdrConversionCapabilities(std::vector<common::HdrConversionCapability>*) {
841     return HWC2_ERROR_UNSUPPORTED;
842 }
843 
setHdrConversionStrategy(const common::HdrConversionStrategy &,common::Hdr *)844 int32_t HalImpl::setHdrConversionStrategy(const common::HdrConversionStrategy&, common::Hdr*) {
845     return HWC2_ERROR_UNSUPPORTED;
846 }
847 
setAutoLowLatencyMode(int64_t display,bool on)848 int32_t HalImpl::setAutoLowLatencyMode(int64_t display, bool on) {
849     ExynosDisplay* halDisplay;
850     RET_IF_ERR(getHalDisplay(display, halDisplay));
851 
852     return halDisplay->setAutoLowLatencyMode(on);
853 }
854 
setClientTarget(int64_t display,buffer_handle_t target,const ndk::ScopedFileDescriptor & fence,common::Dataspace dataspace,const std::vector<common::Rect> & damage)855 int32_t HalImpl::setClientTarget(int64_t display, buffer_handle_t target,
856                                  const ndk::ScopedFileDescriptor& fence,
857                                  common::Dataspace dataspace,
858                                  const std::vector<common::Rect>& damage) {
859     ExynosDisplay* halDisplay;
860     RET_IF_ERR(getHalDisplay(display, halDisplay));
861 
862     int32_t hwcFence;
863     int32_t hwcDataspace;
864     std::vector<hwc_rect_t> hwcDamage;
865 
866     a2h::translate(fence, hwcFence);
867     a2h::translate(dataspace, hwcDataspace);
868     a2h::translate(damage, hwcDamage);
869     hwc_region_t region = { hwcDamage.size(), hwcDamage.data() };
870     UNUSED(region);
871 
872     return halDisplay->setClientTarget(target, hwcFence, hwcDataspace);
873 }
874 
getHasClientComposition(int64_t display,bool & outHasClientComp)875 int32_t HalImpl::getHasClientComposition(int64_t display, bool& outHasClientComp) {
876     ExynosDisplay* halDisplay;
877     RET_IF_ERR(getHalDisplay(display, halDisplay));
878 
879     outHasClientComp = halDisplay->hasClientComposition();
880 
881     return HWC2_ERROR_NONE;
882 }
883 
setColorMode(int64_t display,ColorMode mode,RenderIntent intent)884 int32_t HalImpl::setColorMode(int64_t display, ColorMode mode, RenderIntent intent) {
885     ExynosDisplay* halDisplay;
886     RET_IF_ERR(getHalDisplay(display, halDisplay));
887 
888     int32_t hwcMode;
889     int32_t hwcIntent;
890 
891     a2h::translate(mode, hwcMode);
892     a2h::translate(intent, hwcIntent);
893     return halDisplay->setColorModeWithRenderIntent(hwcMode, hwcIntent);
894 }
895 
setColorTransform(int64_t display,const std::vector<float> & matrix)896 int32_t HalImpl::setColorTransform(int64_t display, const std::vector<float>& matrix) {
897     // clang-format off
898     constexpr std::array<float, 16> kIdentity = {
899         1.0f, 0.0f, 0.0f, 0.0f,
900         0.0f, 1.0f, 0.0f, 0.0f,
901         0.0f, 0.0f, 1.0f, 0.0f,
902         0.0f, 0.0f, 0.0f, 1.0f,
903     };
904     // clang-format on
905     const bool isIdentity = (std::equal(matrix.begin(), matrix.end(), kIdentity.begin()));
906     const common::ColorTransform hint = isIdentity ? common::ColorTransform::IDENTITY
907                                                    : common::ColorTransform::ARBITRARY_MATRIX;
908 
909     ExynosDisplay* halDisplay;
910     RET_IF_ERR(getHalDisplay(display, halDisplay));
911 
912     int32_t hwcHint;
913     a2h::translate(hint, hwcHint);
914     return halDisplay->setColorTransform(matrix.data(), hwcHint);
915 }
916 
setContentType(int64_t display,ContentType contentType)917 int32_t HalImpl::setContentType(int64_t display, ContentType contentType) {
918     ExynosDisplay* halDisplay;
919     RET_IF_ERR(getHalDisplay(display, halDisplay));
920 
921     int32_t type;
922     a2h::translate(contentType, type);
923     return halDisplay->setContentType(type);
924 }
925 
setDisplayBrightness(int64_t display,float brightness)926 int32_t HalImpl::setDisplayBrightness(int64_t display, float brightness) {
927     ExynosDisplay* halDisplay;
928     RET_IF_ERR(getHalDisplay(display, halDisplay));
929 
930     return halDisplay->setDisplayBrightness(brightness, true /* wait present */);
931 }
932 
setDisplayedContentSamplingEnabled(int64_t display,bool enable,FormatColorComponent componentMask,int64_t maxFrames)933 int32_t HalImpl::setDisplayedContentSamplingEnabled(
934         [[maybe_unused]] int64_t display,
935         [[maybe_unused]] bool enable,
936         [[maybe_unused]] FormatColorComponent componentMask,
937         [[maybe_unused]] int64_t maxFrames) {
938     return HWC2_ERROR_UNSUPPORTED;
939 }
940 
setLayerBlendMode(int64_t display,int64_t layer,common::BlendMode mode)941 int32_t HalImpl::setLayerBlendMode(int64_t display, int64_t layer, common::BlendMode mode) {
942     ExynosLayer *halLayer;
943     RET_IF_ERR(getHalLayer(display, layer, halLayer));
944 
945     int32_t hwcMode;
946     a2h::translate(mode, hwcMode);
947     return halLayer->setLayerBlendMode(hwcMode);
948 }
949 
setLayerBuffer(int64_t display,int64_t layer,buffer_handle_t buffer,const ndk::ScopedFileDescriptor & acquireFence)950 int32_t HalImpl::setLayerBuffer(int64_t display, int64_t layer, buffer_handle_t buffer,
951                                 const ndk::ScopedFileDescriptor& acquireFence) {
952     ExynosLayer *halLayer;
953     RET_IF_ERR(getHalLayer(display, layer, halLayer));
954 
955     int32_t hwcFd;
956     a2h::translate(acquireFence, hwcFd);
957 
958     return halLayer->setLayerBuffer(buffer, hwcFd);
959 }
960 
uncacheLayerBuffers(int64_t display,int64_t layer,const std::vector<buffer_handle_t> & buffers,std::vector<buffer_handle_t> & outClearableBuffers)961 int32_t HalImpl::uncacheLayerBuffers(int64_t display, int64_t layer,
962                                      const std::vector<buffer_handle_t>& buffers,
963                                      std::vector<buffer_handle_t>& outClearableBuffers) {
964     ExynosDisplay* halDisplay;
965     RET_IF_ERR(getHalDisplay(display, halDisplay));
966 
967     ExynosLayer* halLayer;
968     RET_IF_ERR(getHalLayer(display, layer, halLayer));
969 
970     return halDisplay->uncacheLayerBuffers(halLayer, buffers, outClearableBuffers);
971 }
972 
setLayerColor(int64_t display,int64_t layer,Color color)973 int32_t HalImpl::setLayerColor(int64_t display, int64_t layer, Color color) {
974     ExynosLayer *halLayer;
975     RET_IF_ERR(getHalLayer(display, layer, halLayer));
976 
977     hwc_color_t hwcColor;
978     a2h::translate(color, hwcColor);
979     return halLayer->setLayerColor(hwcColor);
980 }
981 
setLayerColorTransform(int64_t display,int64_t layer,const std::vector<float> & matrix)982 int32_t HalImpl::setLayerColorTransform(int64_t display, int64_t layer,
983                                         const std::vector<float>& matrix) {
984     ExynosLayer *halLayer;
985     RET_IF_ERR(getHalLayer(display, layer, halLayer));
986 
987     return halLayer->setLayerColorTransform(matrix.data());
988 }
989 
setLayerCompositionType(int64_t display,int64_t layer,Composition type)990 int32_t HalImpl::setLayerCompositionType(int64_t display, int64_t layer, Composition type) {
991     ExynosLayer *halLayer;
992     RET_IF_ERR(getHalLayer(display, layer, halLayer));
993 
994     int32_t hwcType;
995     a2h::translate(type, hwcType);
996     return halLayer->setLayerCompositionType(hwcType);
997 }
998 
setLayerCursorPosition(int64_t display,int64_t layer,int32_t x,int32_t y)999 int32_t HalImpl::setLayerCursorPosition(int64_t display, int64_t layer, int32_t x, int32_t y) {
1000     ExynosLayer *halLayer;
1001     RET_IF_ERR(getHalLayer(display, layer, halLayer));
1002 
1003     return halLayer->setCursorPosition(x, y);
1004 }
1005 
setLayerDataspace(int64_t display,int64_t layer,common::Dataspace dataspace)1006 int32_t HalImpl::setLayerDataspace(int64_t display, int64_t layer, common::Dataspace dataspace) {
1007     ExynosLayer *halLayer;
1008     RET_IF_ERR(getHalLayer(display, layer, halLayer));
1009 
1010     int32_t hwcDataspace;
1011     a2h::translate(dataspace, hwcDataspace);
1012     return halLayer->setLayerDataspace(hwcDataspace);
1013 }
1014 
setLayerDisplayFrame(int64_t display,int64_t layer,const common::Rect & frame)1015 int32_t HalImpl::setLayerDisplayFrame(int64_t display, int64_t layer, const common::Rect& frame) {
1016     ExynosLayer *halLayer;
1017     RET_IF_ERR(getHalLayer(display, layer, halLayer));
1018 
1019     hwc_rect_t hwcFrame;
1020     a2h::translate(frame, hwcFrame);
1021     return halLayer->setLayerDisplayFrame(hwcFrame);
1022 }
1023 
setLayerPerFrameMetadata(int64_t display,int64_t layer,const std::vector<std::optional<PerFrameMetadata>> & metadata)1024 int32_t HalImpl::setLayerPerFrameMetadata(int64_t display, int64_t layer,
1025                            const std::vector<std::optional<PerFrameMetadata>>& metadata) {
1026     ExynosLayer *halLayer;
1027     RET_IF_ERR(getHalLayer(display, layer, halLayer));
1028 
1029     uint32_t count = metadata.size();
1030     std::vector<int32_t> keys;
1031     std::vector<float> values;
1032 
1033     for (uint32_t ix = 0; ix < count; ++ix) {
1034         if (metadata[ix]) {
1035             int32_t key;
1036             a2h::translate(metadata[ix]->key, key);
1037             keys.push_back(key);
1038             values.push_back(metadata[ix]->value);
1039         }
1040     }
1041 
1042     return halLayer->setLayerPerFrameMetadata(count, keys.data(), values.data());
1043 }
1044 
setLayerPerFrameMetadataBlobs(int64_t display,int64_t layer,const std::vector<std::optional<PerFrameMetadataBlob>> & blobs)1045 int32_t HalImpl::setLayerPerFrameMetadataBlobs(int64_t display, int64_t layer,
1046                            const std::vector<std::optional<PerFrameMetadataBlob>>& blobs) {
1047     ExynosLayer *halLayer;
1048     RET_IF_ERR(getHalLayer(display, layer, halLayer));
1049 
1050     uint32_t count = blobs.size();
1051     std::vector<int32_t> keys;
1052     std::vector<uint32_t> sizes;
1053     std::vector<uint8_t> values;
1054 
1055     for (uint32_t ix = 0; ix < count; ++ix) {
1056         if (blobs[ix]) {
1057             int32_t key;
1058             a2h::translate(blobs[ix]->key, key);
1059             keys.push_back(key);
1060             sizes.push_back(blobs[ix]->blob.size());
1061             values.insert(values.end(), blobs[ix]->blob.begin(), blobs[ix]->blob.end());
1062         }
1063     }
1064 
1065     return halLayer->setLayerPerFrameMetadataBlobs(count, keys.data(), sizes.data(),
1066                                                    values.data());
1067 }
1068 
setLayerPlaneAlpha(int64_t display,int64_t layer,float alpha)1069 int32_t HalImpl::setLayerPlaneAlpha(int64_t display, int64_t layer, float alpha) {
1070     ExynosLayer *halLayer;
1071     RET_IF_ERR(getHalLayer(display, layer, halLayer));
1072 
1073     return halLayer->setLayerPlaneAlpha(alpha);
1074 }
1075 
setLayerSidebandStream(int64_t display,int64_t layer,buffer_handle_t stream)1076 int32_t HalImpl::setLayerSidebandStream([[maybe_unused]] int64_t display,
1077                                         [[maybe_unused]] int64_t layer,
1078                                         [[maybe_unused]] buffer_handle_t stream) {
1079     return HWC2_ERROR_UNSUPPORTED;
1080 }
1081 
setLayerSourceCrop(int64_t display,int64_t layer,const common::FRect & crop)1082 int32_t HalImpl::setLayerSourceCrop(int64_t display, int64_t layer, const common::FRect& crop) {
1083     ExynosLayer *halLayer;
1084     RET_IF_ERR(getHalLayer(display, layer, halLayer));
1085 
1086     hwc_frect_t hwcCrop;
1087     a2h::translate(crop, hwcCrop);
1088     return halLayer->setLayerSourceCrop(hwcCrop);
1089 }
1090 
setLayerSurfaceDamage(int64_t display,int64_t layer,const std::vector<std::optional<common::Rect>> & damage)1091 int32_t HalImpl::setLayerSurfaceDamage(int64_t display, int64_t layer,
1092                                   const std::vector<std::optional<common::Rect>>& damage) {
1093     ExynosLayer *halLayer;
1094     RET_IF_ERR(getHalLayer(display, layer, halLayer));
1095 
1096     std::vector<hwc_rect_t> hwcDamage;
1097     a2h::translate(damage, hwcDamage);
1098     hwc_region_t region = { hwcDamage.size(), hwcDamage.data() };
1099 
1100     return halLayer->setLayerSurfaceDamage(region);
1101 }
1102 
setLayerTransform(int64_t display,int64_t layer,common::Transform transform)1103 int32_t HalImpl::setLayerTransform(int64_t display, int64_t layer, common::Transform transform) {
1104     ExynosLayer *halLayer;
1105     RET_IF_ERR(getHalLayer(display, layer, halLayer));
1106 
1107     int32_t hwcTransform;
1108     a2h::translate(transform, hwcTransform);
1109 
1110     return halLayer->setLayerTransform(hwcTransform);
1111 }
1112 
setLayerVisibleRegion(int64_t display,int64_t layer,const std::vector<std::optional<common::Rect>> & visible)1113 int32_t HalImpl::setLayerVisibleRegion(int64_t display, int64_t layer,
1114                                const std::vector<std::optional<common::Rect>>& visible) {
1115     ExynosLayer *halLayer;
1116     RET_IF_ERR(getHalLayer(display, layer, halLayer));
1117 
1118     std::vector<hwc_rect_t> hwcVisible;
1119     a2h::translate(visible, hwcVisible);
1120     hwc_region_t region = { hwcVisible.size(), hwcVisible.data() };
1121 
1122     return halLayer->setLayerVisibleRegion(region);
1123 }
1124 
setLayerBrightness(int64_t display,int64_t layer,float brightness)1125 int32_t HalImpl::setLayerBrightness(int64_t display, int64_t layer, float brightness) {
1126     ExynosLayer *halLayer;
1127     RET_IF_ERR(getHalLayer(display, layer, halLayer));
1128 
1129     return halLayer->setLayerBrightness(brightness);
1130 }
1131 
setLayerZOrder(int64_t display,int64_t layer,uint32_t z)1132 int32_t HalImpl::setLayerZOrder(int64_t display, int64_t layer, uint32_t z) {
1133     ExynosLayer *halLayer;
1134     RET_IF_ERR(getHalLayer(display, layer, halLayer));
1135 
1136     return halLayer->setLayerZOrder(z);
1137 }
1138 
setOutputBuffer(int64_t display,buffer_handle_t buffer,const ndk::ScopedFileDescriptor & releaseFence)1139 int32_t HalImpl::setOutputBuffer(int64_t display, buffer_handle_t buffer,
1140                                  const ndk::ScopedFileDescriptor& releaseFence) {
1141     ExynosDisplay* halDisplay;
1142     RET_IF_ERR(getHalDisplay(display, halDisplay));
1143 
1144     int32_t hwcFence;
1145     a2h::translate(releaseFence, hwcFence);
1146 
1147     auto err = halDisplay->setOutputBuffer(buffer, hwcFence);
1148     // unlike in setClientTarget, releaseFence is owned by us
1149     if (err == HWC2_ERROR_NONE && hwcFence >= 0) {
1150         close(hwcFence);
1151     }
1152 
1153     return err;
1154 }
1155 
setPowerMode(int64_t display,PowerMode mode)1156 int32_t HalImpl::setPowerMode(int64_t display, PowerMode mode) {
1157     if (mode == PowerMode::ON_SUSPEND || mode == PowerMode::DOZE_SUSPEND) {
1158         return HWC2_ERROR_UNSUPPORTED;
1159     }
1160 
1161     ExynosDisplay* halDisplay;
1162     RET_IF_ERR(getHalDisplay(display, halDisplay));
1163 
1164     int32_t hwcMode;
1165     a2h::translate(mode, hwcMode);
1166     return halDisplay->setPowerMode(hwcMode);
1167 }
1168 
getPowerMode(int64_t display,std::optional<PowerMode> & outMode)1169 int32_t HalImpl::getPowerMode(int64_t display, std::optional<PowerMode>& outMode) {
1170     ExynosDisplay* halDisplay;
1171     RET_IF_ERR(getHalDisplay(display, halDisplay));
1172 
1173     auto mode = halDisplay->getPowerMode();
1174     h2a::translate(mode, outMode);
1175     return HWC2_ERROR_NONE;
1176 }
1177 
setReadbackBuffer(int64_t display,buffer_handle_t buffer,const ndk::ScopedFileDescriptor & releaseFence)1178 int32_t HalImpl::setReadbackBuffer(int64_t display, buffer_handle_t buffer,
1179                                    const ndk::ScopedFileDescriptor& releaseFence) {
1180     ExynosDisplay* halDisplay;
1181     RET_IF_ERR(getHalDisplay(display, halDisplay));
1182 
1183     int32_t hwcFence;
1184     a2h::translate(releaseFence, hwcFence);
1185 
1186     return halDisplay->setReadbackBuffer(buffer, hwcFence);
1187 }
1188 
setVsyncEnabled(int64_t display,bool enabled)1189 int32_t HalImpl::setVsyncEnabled(int64_t display, bool enabled) {
1190     ExynosDisplay* halDisplay;
1191     RET_IF_ERR(getHalDisplay(display, halDisplay));
1192 
1193     hwc2_vsync_t hwcEnable;
1194     a2h::translate(enabled, hwcEnable);
1195     return halDisplay->setVsyncEnabled(hwcEnable);
1196 }
1197 
setIdleTimerEnabled(int64_t display,int32_t timeout)1198 int32_t HalImpl::setIdleTimerEnabled(int64_t display, int32_t timeout) {
1199     ExynosDisplay* halDisplay;
1200     RET_IF_ERR(getHalDisplay(display, halDisplay));
1201 
1202     return halDisplay->setDisplayIdleTimer(timeout);
1203 }
1204 
validateDisplay(int64_t display,std::vector<int64_t> * outChangedLayers,std::vector<Composition> * outCompositionTypes,uint32_t * outDisplayRequestMask,std::vector<int64_t> * outRequestedLayers,std::vector<int32_t> * outRequestMasks,ClientTargetProperty * outClientTargetProperty,DimmingStage * outDimmingStage)1205 int32_t HalImpl::validateDisplay(int64_t display, std::vector<int64_t>* outChangedLayers,
1206                                  std::vector<Composition>* outCompositionTypes,
1207                                  uint32_t* outDisplayRequestMask,
1208                                  std::vector<int64_t>* outRequestedLayers,
1209                                  std::vector<int32_t>* outRequestMasks,
1210                                  ClientTargetProperty* outClientTargetProperty,
1211                                  DimmingStage* outDimmingStage) {
1212     ExynosDisplay* halDisplay;
1213     RET_IF_ERR(getHalDisplay(display, halDisplay));
1214 
1215     uint32_t typesCount = 0;
1216     uint32_t reqsCount = 0;
1217     auto err = halDisplay->validateDisplay(&typesCount, &reqsCount);
1218 
1219     if (err != HWC2_ERROR_NONE && err != HWC2_ERROR_HAS_CHANGES) {
1220         return err;
1221     }
1222 
1223     std::vector<hwc2_layer_t> hwcChangedLayers(typesCount);
1224     std::vector<int32_t> hwcCompositionTypes(typesCount);
1225     RET_IF_ERR(halDisplay->getChangedCompositionTypes(&typesCount, hwcChangedLayers.data(),
1226                                                       hwcCompositionTypes.data()));
1227 
1228     int32_t displayReqs;
1229     std::vector<hwc2_layer_t> hwcRequestedLayers(reqsCount);
1230     outRequestMasks->resize(reqsCount);
1231     RET_IF_ERR(halDisplay->getDisplayRequests(&displayReqs, &reqsCount,
1232                                               hwcRequestedLayers.data(), outRequestMasks->data()));
1233     std::vector<int64_t> sfChangedLayers(typesCount);
1234 
1235     for (int i = 0; i < typesCount; i++) {
1236         auto iter = mHalLayerToSfLayerMap.find(hwcChangedLayers[i]);
1237         if (iter != mHalLayerToSfLayerMap.end()) {
1238             sfChangedLayers[i] = iter->second;
1239         } else {
1240             LOG(ERROR) << "HalImpl::validateDisplay incorrect hal mapping. ";
1241         }
1242     }
1243     h2a::translate(sfChangedLayers, *outChangedLayers);
1244     h2a::translate(hwcCompositionTypes, *outCompositionTypes);
1245     *outDisplayRequestMask = displayReqs;
1246     std::vector<int64_t> sfRequestedLayers(reqsCount);
1247 
1248     for (int i = 0; i < reqsCount; i++) {
1249         auto iter = mHalLayerToSfLayerMap.find(hwcRequestedLayers[i]);
1250         if (iter != mHalLayerToSfLayerMap.end()) {
1251             sfRequestedLayers[i] = iter->second;
1252         } else {
1253             LOG(ERROR) << "HalImpl::validateDisplay incorrect hal mapping. ";
1254         }
1255     }
1256     h2a::translate(sfRequestedLayers, *outRequestedLayers);
1257     hwc_client_target_property hwcProperty;
1258     HwcDimmingStage hwcDimmingStage;
1259     if (!halDisplay->getClientTargetProperty(&hwcProperty, &hwcDimmingStage)) {
1260         h2a::translate(hwcDimmingStage, *outDimmingStage);
1261         h2a::translate(hwcProperty, *outClientTargetProperty);
1262     } // else ignore this error
1263 
1264     return err;
1265 }
1266 
setExpectedPresentTime(int64_t display,const std::optional<ClockMonotonicTimestamp> expectedPresentTime,int frameIntervalNs)1267 int HalImpl::setExpectedPresentTime(
1268         int64_t display, const std::optional<ClockMonotonicTimestamp> expectedPresentTime,
1269         int frameIntervalNs) {
1270     ExynosDisplay* halDisplay;
1271     RET_IF_ERR(getHalDisplay(display, halDisplay));
1272 
1273     if (!expectedPresentTime.has_value()) return HWC2_ERROR_NONE;
1274 
1275     if (halDisplay->getPendingExpectedPresentTime() != 0) {
1276         ALOGW("HalImpl: set expected present time multiple times in one frame");
1277     }
1278 
1279     halDisplay->setExpectedPresentTime(expectedPresentTime->timestampNanos, frameIntervalNs);
1280 
1281     return HWC2_ERROR_NONE;
1282 }
1283 
getRCDLayerSupport(int64_t display,bool & outSupport)1284 int32_t HalImpl::getRCDLayerSupport(int64_t display, bool& outSupport) {
1285     ExynosDisplay* halDisplay;
1286     RET_IF_ERR(getHalDisplay(display, halDisplay));
1287 
1288     return halDisplay->getRCDLayerSupport(outSupport);
1289 }
1290 
setLayerBlockingRegion(int64_t display,int64_t layer,const std::vector<std::optional<common::Rect>> & blockingRegion)1291 int32_t HalImpl::setLayerBlockingRegion(
1292         int64_t display, int64_t layer,
1293         const std::vector<std::optional<common::Rect>>& blockingRegion) {
1294     ExynosLayer* halLayer;
1295     RET_IF_ERR(getHalLayer(display, layer, halLayer));
1296 
1297     std::vector<hwc_rect_t> halBlockingRegion;
1298     a2h::translate(blockingRegion, halBlockingRegion);
1299 
1300     return halLayer->setLayerBlockingRegion(halBlockingRegion);
1301 }
1302 
getDisplayIdleTimerSupport(int64_t display,bool & outSupport)1303 int32_t HalImpl::getDisplayIdleTimerSupport(int64_t display, bool& outSupport) {
1304     ExynosDisplay* halDisplay;
1305     RET_IF_ERR(getHalDisplay(display, halDisplay));
1306 
1307     return halDisplay->getDisplayIdleTimerSupport(outSupport);
1308 }
1309 
getDisplayMultiThreadedPresentSupport(const int64_t & display,bool & outSupport)1310 int32_t HalImpl::getDisplayMultiThreadedPresentSupport(const int64_t& display, bool& outSupport) {
1311     ExynosDisplay* halDisplay;
1312     RET_IF_ERR(getHalDisplay(display, halDisplay));
1313 
1314     return halDisplay->getDisplayMultiThreadedPresentSupport(outSupport);
1315 }
1316 
setRefreshRateChangedCallbackDebugEnabled(int64_t display,bool enabled)1317 int32_t HalImpl::setRefreshRateChangedCallbackDebugEnabled(int64_t display, bool enabled) {
1318     ExynosDisplay* halDisplay;
1319     RET_IF_ERR(getHalDisplay(display, halDisplay));
1320 
1321     return halDisplay->setRefreshRateChangedCallbackDebugEnabled(enabled);
1322 }
1323 
getMaxLayerPictureProfiles(int64_t display,int32_t * outMaxProfiles)1324 int32_t HalImpl::getMaxLayerPictureProfiles([[maybe_unused]] int64_t display,
1325                                             [[maybe_unused]] int32_t* outMaxProfiles) {
1326     return HWC2_ERROR_UNSUPPORTED;
1327 }
1328 
1329 } // namespace aidl::android::hardware::graphics::composer3::impl
1330