1 /*
2  * Copyright 2016 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 #pragma once
18 
19 #ifndef LOG_TAG
20 #warning "HwcHal.h included without LOG_TAG"
21 #endif
22 
23 #include <atomic>
24 #include <cstring>  // for strerror
25 #include <type_traits>
26 #include <unordered_set>
27 #include <vector>
28 
29 #include <android/hardware/graphics/composer/2.1/IComposer.h>
30 #include <composer-hal/2.1/ComposerHal.h>
31 #define HWC2_INCLUDE_STRINGIFICATION
32 #define HWC2_USE_CPP11
33 #include <hardware/hwcomposer2.h>
34 #undef HWC2_INCLUDE_STRINGIFICATION
35 #undef HWC2_USE_CPP11
36 #include <log/log.h>
37 
38 namespace android {
39 namespace hardware {
40 namespace graphics {
41 namespace composer {
42 namespace V2_1 {
43 namespace passthrough {
44 
45 namespace detail {
46 
47 using android::hardware::graphics::common::V1_0::ColorMode;
48 using android::hardware::graphics::common::V1_0::ColorTransform;
49 using android::hardware::graphics::common::V1_0::Dataspace;
50 using android::hardware::graphics::common::V1_0::Hdr;
51 using android::hardware::graphics::common::V1_0::PixelFormat;
52 using android::hardware::graphics::common::V1_0::Transform;
53 
54 // HwcHalImpl implements V2_*::hal::ComposerHal on top of hwcomposer2
55 template <typename Hal>
56 class HwcHalImpl : public Hal {
57    public:
~HwcHalImpl()58     virtual ~HwcHalImpl() {
59         if (mDevice) {
60             hwc2_close(mDevice);
61         }
62     }
63 
initWithModule(const hw_module_t * module)64     bool initWithModule(const hw_module_t* module) {
65         hwc2_device_t* device;
66         int result = hwc2_open(module, &device);
67         if (result) {
68             ALOGE("failed to open hwcomposer2 device: %s", strerror(-result));
69             return false;
70         }
71 
72         return initWithDevice(std::move(device), true);
73     }
74 
initWithDevice(hwc2_device_t * device,bool requireReliablePresentFence)75     bool initWithDevice(hwc2_device_t* device, bool requireReliablePresentFence) {
76         // we own the device from this point on
77         mDevice = device;
78 
79         initCapabilities();
80         if (requireReliablePresentFence &&
81             hasCapability(HWC2_CAPABILITY_PRESENT_FENCE_IS_NOT_RELIABLE)) {
82             ALOGE("present fence must be reliable");
83             mDevice->common.close(&mDevice->common);
84             mDevice = nullptr;
85             return false;
86         }
87 
88         if (!initDispatch()) {
89             mDevice->common.close(&mDevice->common);
90             mDevice = nullptr;
91             return false;
92         }
93 
94         return true;
95     }
96 
hasCapability(hwc2_capability_t capability)97     bool hasCapability(hwc2_capability_t capability) override {
98         return (mCapabilities.count(capability) > 0);
99     }
100 
dumpDebugInfo()101     std::string dumpDebugInfo() override {
102         uint32_t len = 0;
103         mDispatch.dump(mDevice, &len, nullptr);
104 
105         std::vector<char> buf(len + 1);
106         mDispatch.dump(mDevice, &len, buf.data());
107         buf.resize(len + 1);
108         buf[len] = '\0';
109 
110         return buf.data();
111     }
112 
registerEventCallback(hal::ComposerHal::EventCallback * callback)113     void registerEventCallback(hal::ComposerHal::EventCallback* callback) override {
114         mEventCallback = callback;
115 
116         mDispatch.registerCallback(mDevice, HWC2_CALLBACK_HOTPLUG, this,
117                                    reinterpret_cast<hwc2_function_pointer_t>(hotplugHook));
118         mDispatch.registerCallback(mDevice, HWC2_CALLBACK_REFRESH, this,
119                                    reinterpret_cast<hwc2_function_pointer_t>(refreshHook));
120         mDispatch.registerCallback(mDevice, HWC2_CALLBACK_VSYNC, this,
121                                    reinterpret_cast<hwc2_function_pointer_t>(vsyncHook));
122     }
123 
unregisterEventCallback()124     void unregisterEventCallback() override {
125         // we assume the callback functions
126         //
127         //  - can be unregistered
128         //  - can be in-flight
129         //  - will never be called afterward
130         //
131         // which is likely incorrect
132         mDispatch.registerCallback(mDevice, HWC2_CALLBACK_HOTPLUG, this, nullptr);
133         mDispatch.registerCallback(mDevice, HWC2_CALLBACK_REFRESH, this, nullptr);
134         mDispatch.registerCallback(mDevice, HWC2_CALLBACK_VSYNC, this, nullptr);
135 
136         mEventCallback = nullptr;
137     }
138 
getMaxVirtualDisplayCount()139     uint32_t getMaxVirtualDisplayCount() override {
140         return mDispatch.getMaxVirtualDisplayCount(mDevice);
141     }
142 
createVirtualDisplay(uint32_t width,uint32_t height,PixelFormat * format,Display * outDisplay)143     Error createVirtualDisplay(uint32_t width, uint32_t height, PixelFormat* format,
144                                Display* outDisplay) override {
145         int32_t hwc_format = static_cast<int32_t>(*format);
146         int32_t err =
147             mDispatch.createVirtualDisplay(mDevice, width, height, &hwc_format, outDisplay);
148         *format = static_cast<PixelFormat>(hwc_format);
149 
150         return static_cast<Error>(err);
151     }
152 
destroyVirtualDisplay(Display display)153     Error destroyVirtualDisplay(Display display) override {
154         int32_t err = mDispatch.destroyVirtualDisplay(mDevice, display);
155         return static_cast<Error>(err);
156     }
157 
createLayer(Display display,Layer * outLayer)158     Error createLayer(Display display, Layer* outLayer) override {
159         int32_t err = mDispatch.createLayer(mDevice, display, outLayer);
160         return static_cast<Error>(err);
161     }
162 
destroyLayer(Display display,Layer layer)163     Error destroyLayer(Display display, Layer layer) override {
164         int32_t err = mDispatch.destroyLayer(mDevice, display, layer);
165         onLayerDestroyed(display, layer);
166         return static_cast<Error>(err);
167     }
168 
getActiveConfig(Display display,Config * outConfig)169     Error getActiveConfig(Display display, Config* outConfig) override {
170         int32_t err = mDispatch.getActiveConfig(mDevice, display, outConfig);
171         return static_cast<Error>(err);
172     }
173 
getClientTargetSupport(Display display,uint32_t width,uint32_t height,PixelFormat format,Dataspace dataspace)174     Error getClientTargetSupport(Display display, uint32_t width, uint32_t height,
175                                  PixelFormat format, Dataspace dataspace) override {
176         int32_t err = mDispatch.getClientTargetSupport(mDevice, display, width, height,
177                                                        static_cast<int32_t>(format),
178                                                        static_cast<int32_t>(dataspace));
179         return static_cast<Error>(err);
180     }
181 
getColorModes(Display display,hidl_vec<ColorMode> * outModes)182     Error getColorModes(Display display, hidl_vec<ColorMode>* outModes) override {
183         uint32_t count = 0;
184         int32_t err = mDispatch.getColorModes(mDevice, display, &count, nullptr);
185         if (err != HWC2_ERROR_NONE) {
186             return static_cast<Error>(err);
187         }
188 
189         outModes->resize(count);
190         err = mDispatch.getColorModes(
191             mDevice, display, &count,
192             reinterpret_cast<std::underlying_type<ColorMode>::type*>(outModes->data()));
193         if (err != HWC2_ERROR_NONE) {
194             *outModes = hidl_vec<ColorMode>();
195             return static_cast<Error>(err);
196         }
197 
198         return Error::NONE;
199     }
200 
getDisplayAttribute(Display display,Config config,IComposerClient::Attribute attribute,int32_t * outValue)201     Error getDisplayAttribute(Display display, Config config, IComposerClient::Attribute attribute,
202                               int32_t* outValue) override {
203         int32_t err = mDispatch.getDisplayAttribute(mDevice, display, config,
204                                                     static_cast<int32_t>(attribute), outValue);
205         return static_cast<Error>(err);
206     }
207 
getDisplayConfigs(Display display,hidl_vec<Config> * outConfigs)208     Error getDisplayConfigs(Display display, hidl_vec<Config>* outConfigs) override {
209         uint32_t count = 0;
210         int32_t err = mDispatch.getDisplayConfigs(mDevice, display, &count, nullptr);
211         if (err != HWC2_ERROR_NONE) {
212             return static_cast<Error>(err);
213         }
214 
215         outConfigs->resize(count);
216         err = mDispatch.getDisplayConfigs(mDevice, display, &count, outConfigs->data());
217         if (err != HWC2_ERROR_NONE) {
218             *outConfigs = hidl_vec<Config>();
219             return static_cast<Error>(err);
220         }
221 
222         return Error::NONE;
223     }
224 
getDisplayName(Display display,hidl_string * outName)225     Error getDisplayName(Display display, hidl_string* outName) override {
226         uint32_t count = 0;
227         int32_t err = mDispatch.getDisplayName(mDevice, display, &count, nullptr);
228         if (err != HWC2_ERROR_NONE) {
229             return static_cast<Error>(err);
230         }
231 
232         std::vector<char> buf(count + 1);
233         err = mDispatch.getDisplayName(mDevice, display, &count, buf.data());
234         if (err != HWC2_ERROR_NONE) {
235             return static_cast<Error>(err);
236         }
237         buf.resize(count + 1);
238         buf[count] = '\0';
239 
240         *outName = buf.data();
241 
242         return Error::NONE;
243     }
244 
getDisplayType(Display display,IComposerClient::DisplayType * outType)245     Error getDisplayType(Display display, IComposerClient::DisplayType* outType) override {
246         int32_t hwc_type = HWC2_DISPLAY_TYPE_INVALID;
247         int32_t err = mDispatch.getDisplayType(mDevice, display, &hwc_type);
248         *outType = static_cast<IComposerClient::DisplayType>(hwc_type);
249 
250         return static_cast<Error>(err);
251     }
252 
getDozeSupport(Display display,bool * outSupport)253     Error getDozeSupport(Display display, bool* outSupport) override {
254         int32_t hwc_support = 0;
255         int32_t err = mDispatch.getDozeSupport(mDevice, display, &hwc_support);
256         *outSupport = hwc_support;
257 
258         return static_cast<Error>(err);
259     }
260 
getHdrCapabilities(Display display,hidl_vec<Hdr> * outTypes,float * outMaxLuminance,float * outMaxAverageLuminance,float * outMinLuminance)261     Error getHdrCapabilities(Display display, hidl_vec<Hdr>* outTypes, float* outMaxLuminance,
262                              float* outMaxAverageLuminance, float* outMinLuminance) override {
263         uint32_t count = 0;
264         int32_t err =
265             mDispatch.getHdrCapabilities(mDevice, display, &count, nullptr, outMaxLuminance,
266                                          outMaxAverageLuminance, outMinLuminance);
267         if (err != HWC2_ERROR_NONE) {
268             return static_cast<Error>(err);
269         }
270 
271         outTypes->resize(count);
272         err = mDispatch.getHdrCapabilities(
273             mDevice, display, &count,
274             reinterpret_cast<std::underlying_type<Hdr>::type*>(outTypes->data()), outMaxLuminance,
275             outMaxAverageLuminance, outMinLuminance);
276         if (err != HWC2_ERROR_NONE) {
277             *outTypes = hidl_vec<Hdr>();
278             return static_cast<Error>(err);
279         }
280 
281         return Error::NONE;
282     }
283 
setActiveConfig(Display display,Config config)284     Error setActiveConfig(Display display, Config config) override {
285         int32_t err = mDispatch.setActiveConfig(mDevice, display, config);
286         return static_cast<Error>(err);
287     }
288 
setColorMode(Display display,ColorMode mode)289     Error setColorMode(Display display, ColorMode mode) override {
290         int32_t err = mDispatch.setColorMode(mDevice, display, static_cast<int32_t>(mode));
291         return static_cast<Error>(err);
292     }
293 
setPowerMode(Display display,IComposerClient::PowerMode mode)294     Error setPowerMode(Display display, IComposerClient::PowerMode mode) override {
295         int32_t err = mDispatch.setPowerMode(mDevice, display, static_cast<int32_t>(mode));
296         return static_cast<Error>(err);
297     }
298 
setVsyncEnabled(Display display,IComposerClient::Vsync enabled)299     Error setVsyncEnabled(Display display, IComposerClient::Vsync enabled) override {
300         int32_t err = mDispatch.setVsyncEnabled(mDevice, display, static_cast<int32_t>(enabled));
301         return static_cast<Error>(err);
302     }
303 
setColorTransform(Display display,const float * matrix,int32_t hint)304     Error setColorTransform(Display display, const float* matrix, int32_t hint) override {
305         int32_t err = mDispatch.setColorTransform(mDevice, display, matrix, hint);
306         return static_cast<Error>(err);
307     }
308 
setClientTarget(Display display,buffer_handle_t target,int32_t acquireFence,int32_t dataspace,const std::vector<hwc_rect_t> & damage)309     Error setClientTarget(Display display, buffer_handle_t target, int32_t acquireFence,
310                           int32_t dataspace, const std::vector<hwc_rect_t>& damage) override {
311         hwc_region region = {damage.size(), damage.data()};
312         int32_t err =
313             mDispatch.setClientTarget(mDevice, display, target, acquireFence, dataspace, region);
314         return static_cast<Error>(err);
315     }
316 
setOutputBuffer(Display display,buffer_handle_t buffer,int32_t releaseFence)317     Error setOutputBuffer(Display display, buffer_handle_t buffer, int32_t releaseFence) override {
318         int32_t err = mDispatch.setOutputBuffer(mDevice, display, buffer, releaseFence);
319         // unlike in setClientTarget, releaseFence is owned by us
320         if (err == HWC2_ERROR_NONE && releaseFence >= 0) {
321             close(releaseFence);
322         }
323 
324         return static_cast<Error>(err);
325     }
326 
validateDisplay(Display display,std::vector<Layer> * outChangedLayers,std::vector<IComposerClient::Composition> * outCompositionTypes,uint32_t * outDisplayRequestMask,std::vector<Layer> * outRequestedLayers,std::vector<uint32_t> * outRequestMasks)327     Error validateDisplay(Display display, std::vector<Layer>* outChangedLayers,
328                           std::vector<IComposerClient::Composition>* outCompositionTypes,
329                           uint32_t* outDisplayRequestMask, std::vector<Layer>* outRequestedLayers,
330                           std::vector<uint32_t>* outRequestMasks) override {
331         onBeforeValidateDisplay(display);
332         uint32_t typesCount = 0;
333         uint32_t reqsCount = 0;
334         int32_t err = mDispatch.validateDisplay(mDevice, display, &typesCount, &reqsCount);
335 
336         if (err != HWC2_ERROR_NONE && err != HWC2_ERROR_HAS_CHANGES) {
337             return static_cast<Error>(err);
338         }
339 
340         err = getChangedCompositionTypes(display, &typesCount, nullptr, nullptr);
341         if (err != HWC2_ERROR_NONE) {
342             return static_cast<Error>(err);
343         }
344 
345         std::vector<Layer> changedLayers(typesCount);
346         std::vector<IComposerClient::Composition> compositionTypes(typesCount);
347         err = getChangedCompositionTypes(display, &typesCount, changedLayers.data(),
348                                          compositionTypes.data());
349         if (err != HWC2_ERROR_NONE) {
350             return static_cast<Error>(err);
351         }
352 
353         int32_t displayReqs = 0;
354         err = mDispatch.getDisplayRequests(mDevice, display, &displayReqs, &reqsCount, nullptr,
355                                            nullptr);
356         if (err != HWC2_ERROR_NONE) {
357             return static_cast<Error>(err);
358         }
359 
360         std::vector<Layer> requestedLayers(reqsCount);
361         std::vector<uint32_t> requestMasks(reqsCount);
362         err = mDispatch.getDisplayRequests(mDevice, display, &displayReqs, &reqsCount,
363                                            requestedLayers.data(),
364                                            reinterpret_cast<int32_t*>(requestMasks.data()));
365         if (err != HWC2_ERROR_NONE) {
366             return static_cast<Error>(err);
367         }
368 
369         *outChangedLayers = std::move(changedLayers);
370         *outCompositionTypes = std::move(compositionTypes);
371         *outDisplayRequestMask = displayReqs;
372         *outRequestedLayers = std::move(requestedLayers);
373         *outRequestMasks = std::move(requestMasks);
374 
375         return static_cast<Error>(err);
376     }
377 
acceptDisplayChanges(Display display)378     Error acceptDisplayChanges(Display display) override {
379         int32_t err = mDispatch.acceptDisplayChanges(mDevice, display);
380         return static_cast<Error>(err);
381     }
382 
presentDisplay(Display display,int32_t * outPresentFence,std::vector<Layer> * outLayers,std::vector<int32_t> * outReleaseFences)383     Error presentDisplay(Display display, int32_t* outPresentFence, std::vector<Layer>* outLayers,
384                          std::vector<int32_t>* outReleaseFences) override {
385         *outPresentFence = -1;
386         int32_t err = mDispatch.presentDisplay(mDevice, display, outPresentFence);
387         if (err != HWC2_ERROR_NONE) {
388             return static_cast<Error>(err);
389         }
390 
391         uint32_t count = 0;
392         err = mDispatch.getReleaseFences(mDevice, display, &count, nullptr, nullptr);
393         if (err != HWC2_ERROR_NONE) {
394             ALOGW("failed to get release fences");
395             return Error::NONE;
396         }
397 
398         outLayers->resize(count);
399         outReleaseFences->resize(count);
400         err = mDispatch.getReleaseFences(mDevice, display, &count, outLayers->data(),
401                                          outReleaseFences->data());
402         if (err != HWC2_ERROR_NONE) {
403             ALOGW("failed to get release fences");
404             outLayers->clear();
405             outReleaseFences->clear();
406             return Error::NONE;
407         }
408 
409         return static_cast<Error>(err);
410     }
411 
setLayerCursorPosition(Display display,Layer layer,int32_t x,int32_t y)412     Error setLayerCursorPosition(Display display, Layer layer, int32_t x, int32_t y) override {
413         int32_t err = mDispatch.setCursorPosition(mDevice, display, layer, x, y);
414         return static_cast<Error>(err);
415     }
416 
setLayerBuffer(Display display,Layer layer,buffer_handle_t buffer,int32_t acquireFence)417     Error setLayerBuffer(Display display, Layer layer, buffer_handle_t buffer,
418                          int32_t acquireFence) override {
419         int32_t err = mDispatch.setLayerBuffer(mDevice, display, layer, buffer, acquireFence);
420         return static_cast<Error>(err);
421     }
422 
setLayerSurfaceDamage(Display display,Layer layer,const std::vector<hwc_rect_t> & damage)423     Error setLayerSurfaceDamage(Display display, Layer layer,
424                                 const std::vector<hwc_rect_t>& damage) override {
425         hwc_region region = {damage.size(), damage.data()};
426         int32_t err = mDispatch.setLayerSurfaceDamage(mDevice, display, layer, region);
427         return static_cast<Error>(err);
428     }
429 
setLayerBlendMode(Display display,Layer layer,int32_t mode)430     Error setLayerBlendMode(Display display, Layer layer, int32_t mode) override {
431         int32_t err = mDispatch.setLayerBlendMode(mDevice, display, layer, mode);
432         return static_cast<Error>(err);
433     }
434 
setLayerColor(Display display,Layer layer,IComposerClient::Color color)435     Error setLayerColor(Display display, Layer layer, IComposerClient::Color color) override {
436         hwc_color_t hwc_color{color.r, color.g, color.b, color.a};
437         int32_t err = mDispatch.setLayerColor(mDevice, display, layer, hwc_color);
438         return static_cast<Error>(err);
439     }
440 
setLayerCompositionType(Display display,Layer layer,int32_t type)441     Error setLayerCompositionType(Display display, Layer layer, int32_t type) override {
442         int32_t err = mDispatch.setLayerCompositionType(mDevice, display, layer, type);
443         return static_cast<Error>(err);
444     }
445 
setLayerDataspace(Display display,Layer layer,int32_t dataspace)446     Error setLayerDataspace(Display display, Layer layer, int32_t dataspace) override {
447         int32_t err = mDispatch.setLayerDataspace(mDevice, display, layer, dataspace);
448         return static_cast<Error>(err);
449     }
450 
setLayerDisplayFrame(Display display,Layer layer,const hwc_rect_t & frame)451     Error setLayerDisplayFrame(Display display, Layer layer, const hwc_rect_t& frame) override {
452         int32_t err = mDispatch.setLayerDisplayFrame(mDevice, display, layer, frame);
453         return static_cast<Error>(err);
454     }
455 
setLayerPlaneAlpha(Display display,Layer layer,float alpha)456     Error setLayerPlaneAlpha(Display display, Layer layer, float alpha) override {
457         int32_t err = mDispatch.setLayerPlaneAlpha(mDevice, display, layer, alpha);
458         return static_cast<Error>(err);
459     }
460 
setLayerSidebandStream(Display display,Layer layer,buffer_handle_t stream)461     Error setLayerSidebandStream(Display display, Layer layer, buffer_handle_t stream) override {
462         int32_t err = mDispatch.setLayerSidebandStream(mDevice, display, layer, stream);
463         return static_cast<Error>(err);
464     }
465 
setLayerSourceCrop(Display display,Layer layer,const hwc_frect_t & crop)466     Error setLayerSourceCrop(Display display, Layer layer, const hwc_frect_t& crop) override {
467         int32_t err = mDispatch.setLayerSourceCrop(mDevice, display, layer, crop);
468         return static_cast<Error>(err);
469     }
470 
setLayerTransform(Display display,Layer layer,int32_t transform)471     Error setLayerTransform(Display display, Layer layer, int32_t transform) override {
472         int32_t err = mDispatch.setLayerTransform(mDevice, display, layer, transform);
473         return static_cast<Error>(err);
474     }
475 
setLayerVisibleRegion(Display display,Layer layer,const std::vector<hwc_rect_t> & visible)476     Error setLayerVisibleRegion(Display display, Layer layer,
477                                 const std::vector<hwc_rect_t>& visible) override {
478         hwc_region_t region = {visible.size(), visible.data()};
479         int32_t err = mDispatch.setLayerVisibleRegion(mDevice, display, layer, region);
480         return static_cast<Error>(err);
481     }
482 
setLayerZOrder(Display display,Layer layer,uint32_t z)483     Error setLayerZOrder(Display display, Layer layer, uint32_t z) override {
484         int32_t err = mDispatch.setLayerZOrder(mDevice, display, layer, z);
485         return static_cast<Error>(err);
486     }
487 
488    protected:
initCapabilities()489     virtual void initCapabilities() {
490         uint32_t count = 0;
491         mDevice->getCapabilities(mDevice, &count, nullptr);
492 
493         std::vector<int32_t> caps(count);
494         mDevice->getCapabilities(mDevice, &count, caps.data());
495         caps.resize(count);
496 
497         mCapabilities.reserve(count);
498         for (auto cap : caps) {
499             mCapabilities.insert(static_cast<hwc2_capability_t>(cap));
500         }
501     }
502 
503     template <typename T>
initDispatch(hwc2_function_descriptor_t desc,T * outPfn)504     bool initDispatch(hwc2_function_descriptor_t desc, T* outPfn) {
505         auto pfn = mDevice->getFunction(mDevice, desc);
506         if (pfn) {
507             *outPfn = reinterpret_cast<T>(pfn);
508             return true;
509         } else {
510             ALOGE("failed to get hwcomposer2 function %d", desc);
511             return false;
512         }
513     }
514 
initDispatch()515     virtual bool initDispatch() {
516         if (!initDispatch(HWC2_FUNCTION_ACCEPT_DISPLAY_CHANGES, &mDispatch.acceptDisplayChanges) ||
517             !initDispatch(HWC2_FUNCTION_CREATE_LAYER, &mDispatch.createLayer) ||
518             !initDispatch(HWC2_FUNCTION_CREATE_VIRTUAL_DISPLAY, &mDispatch.createVirtualDisplay) ||
519             !initDispatch(HWC2_FUNCTION_DESTROY_LAYER, &mDispatch.destroyLayer) ||
520             !initDispatch(HWC2_FUNCTION_DESTROY_VIRTUAL_DISPLAY,
521                           &mDispatch.destroyVirtualDisplay) ||
522             !initDispatch(HWC2_FUNCTION_DUMP, &mDispatch.dump) ||
523             !initDispatch(HWC2_FUNCTION_GET_ACTIVE_CONFIG, &mDispatch.getActiveConfig) ||
524             !initDispatch(HWC2_FUNCTION_GET_CHANGED_COMPOSITION_TYPES,
525                           &mDispatch.getChangedCompositionTypes) ||
526             !initDispatch(HWC2_FUNCTION_GET_CLIENT_TARGET_SUPPORT,
527                           &mDispatch.getClientTargetSupport) ||
528             !initDispatch(HWC2_FUNCTION_GET_COLOR_MODES, &mDispatch.getColorModes) ||
529             !initDispatch(HWC2_FUNCTION_GET_DISPLAY_ATTRIBUTE, &mDispatch.getDisplayAttribute) ||
530             !initDispatch(HWC2_FUNCTION_GET_DISPLAY_CONFIGS, &mDispatch.getDisplayConfigs) ||
531             !initDispatch(HWC2_FUNCTION_GET_DISPLAY_NAME, &mDispatch.getDisplayName) ||
532             !initDispatch(HWC2_FUNCTION_GET_DISPLAY_REQUESTS, &mDispatch.getDisplayRequests) ||
533             !initDispatch(HWC2_FUNCTION_GET_DISPLAY_TYPE, &mDispatch.getDisplayType) ||
534             !initDispatch(HWC2_FUNCTION_GET_DOZE_SUPPORT, &mDispatch.getDozeSupport) ||
535             !initDispatch(HWC2_FUNCTION_GET_HDR_CAPABILITIES, &mDispatch.getHdrCapabilities) ||
536             !initDispatch(HWC2_FUNCTION_GET_MAX_VIRTUAL_DISPLAY_COUNT,
537                           &mDispatch.getMaxVirtualDisplayCount) ||
538             !initDispatch(HWC2_FUNCTION_GET_RELEASE_FENCES, &mDispatch.getReleaseFences) ||
539             !initDispatch(HWC2_FUNCTION_PRESENT_DISPLAY, &mDispatch.presentDisplay) ||
540             !initDispatch(HWC2_FUNCTION_REGISTER_CALLBACK, &mDispatch.registerCallback) ||
541             !initDispatch(HWC2_FUNCTION_SET_ACTIVE_CONFIG, &mDispatch.setActiveConfig) ||
542             !initDispatch(HWC2_FUNCTION_SET_CLIENT_TARGET, &mDispatch.setClientTarget) ||
543             !initDispatch(HWC2_FUNCTION_SET_COLOR_MODE, &mDispatch.setColorMode) ||
544             !initDispatch(HWC2_FUNCTION_SET_COLOR_TRANSFORM, &mDispatch.setColorTransform) ||
545             !initDispatch(HWC2_FUNCTION_SET_CURSOR_POSITION, &mDispatch.setCursorPosition) ||
546             !initDispatch(HWC2_FUNCTION_SET_LAYER_BLEND_MODE, &mDispatch.setLayerBlendMode) ||
547             !initDispatch(HWC2_FUNCTION_SET_LAYER_BUFFER, &mDispatch.setLayerBuffer) ||
548             !initDispatch(HWC2_FUNCTION_SET_LAYER_COLOR, &mDispatch.setLayerColor) ||
549             !initDispatch(HWC2_FUNCTION_SET_LAYER_COMPOSITION_TYPE,
550                           &mDispatch.setLayerCompositionType) ||
551             !initDispatch(HWC2_FUNCTION_SET_LAYER_DATASPACE, &mDispatch.setLayerDataspace) ||
552             !initDispatch(HWC2_FUNCTION_SET_LAYER_DISPLAY_FRAME, &mDispatch.setLayerDisplayFrame) ||
553             !initDispatch(HWC2_FUNCTION_SET_LAYER_PLANE_ALPHA, &mDispatch.setLayerPlaneAlpha)) {
554             return false;
555         }
556 
557         if (hasCapability(HWC2_CAPABILITY_SIDEBAND_STREAM)) {
558             if (!initDispatch(HWC2_FUNCTION_SET_LAYER_SIDEBAND_STREAM,
559                               &mDispatch.setLayerSidebandStream)) {
560                 return false;
561             }
562         }
563 
564         if (!initDispatch(HWC2_FUNCTION_SET_LAYER_SOURCE_CROP, &mDispatch.setLayerSourceCrop) ||
565             !initDispatch(HWC2_FUNCTION_SET_LAYER_SURFACE_DAMAGE,
566                           &mDispatch.setLayerSurfaceDamage) ||
567             !initDispatch(HWC2_FUNCTION_SET_LAYER_TRANSFORM, &mDispatch.setLayerTransform) ||
568             !initDispatch(HWC2_FUNCTION_SET_LAYER_VISIBLE_REGION,
569                           &mDispatch.setLayerVisibleRegion) ||
570             !initDispatch(HWC2_FUNCTION_SET_LAYER_Z_ORDER, &mDispatch.setLayerZOrder) ||
571             !initDispatch(HWC2_FUNCTION_SET_OUTPUT_BUFFER, &mDispatch.setOutputBuffer) ||
572             !initDispatch(HWC2_FUNCTION_SET_POWER_MODE, &mDispatch.setPowerMode) ||
573             !initDispatch(HWC2_FUNCTION_SET_VSYNC_ENABLED, &mDispatch.setVsyncEnabled) ||
574             !initDispatch(HWC2_FUNCTION_VALIDATE_DISPLAY, &mDispatch.validateDisplay)) {
575             return false;
576         }
577 
578         return true;
579     }
580 
getChangedCompositionTypes(Display display,uint32_t * outTypesCount,Layer * outChangedLayers,IComposerClient::Composition * outCompositionTypes)581     virtual int32_t getChangedCompositionTypes(Display display, uint32_t* outTypesCount,
582                                                Layer* outChangedLayers,
583                                                IComposerClient::Composition* outCompositionTypes) {
584         return getChangedCompositionTypesInternal(display, outTypesCount, outChangedLayers,
585                                                   outCompositionTypes);
586     }
onLayerDestroyed(Display,Layer)587     virtual void onLayerDestroyed(Display /* display */, Layer /* layer */) {}
onBeforeValidateDisplay(Display)588     virtual void onBeforeValidateDisplay(Display /* display */) {}
589 
hotplugHook(hwc2_callback_data_t callbackData,hwc2_display_t display,int32_t connected)590     static void hotplugHook(hwc2_callback_data_t callbackData, hwc2_display_t display,
591                             int32_t connected) {
592         auto hal = static_cast<HwcHalImpl*>(callbackData);
593         hal->mEventCallback->onHotplug(display,
594                                        static_cast<IComposerCallback::Connection>(connected));
595     }
596 
refreshHook(hwc2_callback_data_t callbackData,hwc2_display_t display)597     static void refreshHook(hwc2_callback_data_t callbackData, hwc2_display_t display) {
598         auto hal = static_cast<HwcHalImpl*>(callbackData);
599         hal->mEventCallback->onRefresh(display);
600     }
601 
vsyncHook(hwc2_callback_data_t callbackData,hwc2_display_t display,int64_t timestamp)602     static void vsyncHook(hwc2_callback_data_t callbackData, hwc2_display_t display,
603                           int64_t timestamp) {
604         auto hal = static_cast<HwcHalImpl*>(callbackData);
605         hal->mEventCallback->onVsync(display, timestamp);
606     }
607 
getChangedCompositionTypesInternal(Display display,uint32_t * outTypesCount,Layer * outChangedLayers,IComposerClient::Composition * outCompositionTypes)608     int32_t getChangedCompositionTypesInternal(Display display, uint32_t* outTypesCount,
609                                                Layer* outChangedLayers,
610                                                IComposerClient::Composition* outCompositionTypes) {
611         return mDispatch.getChangedCompositionTypes(
612                 mDevice, display, outTypesCount, outChangedLayers,
613                 reinterpret_cast<std::underlying_type<IComposerClient::Composition>::type*>(
614                         outCompositionTypes));
615     }
616 
617     hwc2_device_t* mDevice = nullptr;
618 
619     std::unordered_set<hwc2_capability_t> mCapabilities;
620 
621     struct {
622         HWC2_PFN_ACCEPT_DISPLAY_CHANGES acceptDisplayChanges;
623         HWC2_PFN_CREATE_LAYER createLayer;
624         HWC2_PFN_CREATE_VIRTUAL_DISPLAY createVirtualDisplay;
625         HWC2_PFN_DESTROY_LAYER destroyLayer;
626         HWC2_PFN_DESTROY_VIRTUAL_DISPLAY destroyVirtualDisplay;
627         HWC2_PFN_DUMP dump;
628         HWC2_PFN_GET_ACTIVE_CONFIG getActiveConfig;
629         HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES getChangedCompositionTypes;
630         HWC2_PFN_GET_CLIENT_TARGET_SUPPORT getClientTargetSupport;
631         HWC2_PFN_GET_COLOR_MODES getColorModes;
632         HWC2_PFN_GET_DISPLAY_ATTRIBUTE getDisplayAttribute;
633         HWC2_PFN_GET_DISPLAY_CONFIGS getDisplayConfigs;
634         HWC2_PFN_GET_DISPLAY_NAME getDisplayName;
635         HWC2_PFN_GET_DISPLAY_REQUESTS getDisplayRequests;
636         HWC2_PFN_GET_DISPLAY_TYPE getDisplayType;
637         HWC2_PFN_GET_DOZE_SUPPORT getDozeSupport;
638         HWC2_PFN_GET_HDR_CAPABILITIES getHdrCapabilities;
639         HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT getMaxVirtualDisplayCount;
640         HWC2_PFN_GET_RELEASE_FENCES getReleaseFences;
641         HWC2_PFN_PRESENT_DISPLAY presentDisplay;
642         HWC2_PFN_REGISTER_CALLBACK registerCallback;
643         HWC2_PFN_SET_ACTIVE_CONFIG setActiveConfig;
644         HWC2_PFN_SET_CLIENT_TARGET setClientTarget;
645         HWC2_PFN_SET_COLOR_MODE setColorMode;
646         HWC2_PFN_SET_COLOR_TRANSFORM setColorTransform;
647         HWC2_PFN_SET_CURSOR_POSITION setCursorPosition;
648         HWC2_PFN_SET_LAYER_BLEND_MODE setLayerBlendMode;
649         HWC2_PFN_SET_LAYER_BUFFER setLayerBuffer;
650         HWC2_PFN_SET_LAYER_COLOR setLayerColor;
651         HWC2_PFN_SET_LAYER_COMPOSITION_TYPE setLayerCompositionType;
652         HWC2_PFN_SET_LAYER_DATASPACE setLayerDataspace;
653         HWC2_PFN_SET_LAYER_DISPLAY_FRAME setLayerDisplayFrame;
654         HWC2_PFN_SET_LAYER_PLANE_ALPHA setLayerPlaneAlpha;
655         HWC2_PFN_SET_LAYER_SIDEBAND_STREAM setLayerSidebandStream;
656         HWC2_PFN_SET_LAYER_SOURCE_CROP setLayerSourceCrop;
657         HWC2_PFN_SET_LAYER_SURFACE_DAMAGE setLayerSurfaceDamage;
658         HWC2_PFN_SET_LAYER_TRANSFORM setLayerTransform;
659         HWC2_PFN_SET_LAYER_VISIBLE_REGION setLayerVisibleRegion;
660         HWC2_PFN_SET_LAYER_Z_ORDER setLayerZOrder;
661         HWC2_PFN_SET_OUTPUT_BUFFER setOutputBuffer;
662         HWC2_PFN_SET_POWER_MODE setPowerMode;
663         HWC2_PFN_SET_VSYNC_ENABLED setVsyncEnabled;
664         HWC2_PFN_VALIDATE_DISPLAY validateDisplay;
665     } mDispatch = {};
666 
667     hal::ComposerHal::EventCallback* mEventCallback = nullptr;
668 };
669 
670 }  // namespace detail
671 
672 using HwcHal = detail::HwcHalImpl<hal::ComposerHal>;
673 
674 }  // namespace passthrough
675 }  // namespace V2_1
676 }  // namespace composer
677 }  // namespace graphics
678 }  // namespace hardware
679 }  // namespace android
680