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