Merge "Automatic mk -> bp."
[android/platform-hardware-interfaces.git] / graphics / composer / 2.1 / default / Hwc.cpp
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  */
17 #define LOG_TAG "HwcPassthrough"
19 #include "Hwc.h"
21 #include <chrono>
22 #include <type_traits>
23 #include <log/log.h>
25 #include "ComposerClient.h"
26 #include "hardware/fb.h"
27 #include "hardware/hwcomposer.h"
28 #include "hwc2on1adapter/HWC2On1Adapter.h"
29 #include "hwc2onfbadapter/HWC2OnFbAdapter.h"
31 using namespace std::chrono_literals;
33 namespace android {
34 namespace hardware {
35 namespace graphics {
36 namespace composer {
37 namespace V2_1 {
38 namespace implementation {
40 HwcHal::HwcHal(const hw_module_t* module)
41     : mDevice(nullptr), mDispatch(), mMustValidateDisplay(true), mAdapter() {
42     uint32_t majorVersion;
43     if (module->id && strcmp(module->id, GRALLOC_HARDWARE_MODULE_ID) == 0) {
44         majorVersion = initWithFb(module);
45     } else {
46         majorVersion = initWithHwc(module);
47     }
49     initCapabilities();
50     if (majorVersion >= 2 &&
51         hasCapability(Capability::PRESENT_FENCE_IS_NOT_RELIABLE)) {
52         ALOGE("Present fence must be reliable from HWC2 on.");
53         abort();
54     }
56     initDispatch();
57 }
59 HwcHal::~HwcHal()
60 {
61     hwc2_close(mDevice);
62 }
64 uint32_t HwcHal::initWithHwc(const hw_module_t* module)
65 {
66     // Determine what kind of module is available (HWC2 vs HWC1.X).
67     hw_device_t* device = nullptr;
68     int error = module->methods->open(module, HWC_HARDWARE_COMPOSER, &device);
69     if (error != 0) {
70         ALOGE("Failed to open HWC device (%s), aborting", strerror(-error));
71         abort();
72     }
73     uint32_t majorVersion = (device->version >> 24) & 0xF;
75     // If we don't have a HWC2, we need to wrap whatever we have in an adapter.
76     if (majorVersion != 2) {
77         uint32_t minorVersion = device->version & HARDWARE_API_VERSION_2_MAJ_MIN_MASK;
78         minorVersion = (minorVersion >> 16) & 0xF;
79         ALOGI("Found HWC implementation v%d.%d", majorVersion, minorVersion);
80         if (minorVersion < 1) {
81             ALOGE("Cannot adapt to HWC version %d.%d. Minimum supported is 1.1",
82                   majorVersion, minorVersion);
83             abort();
84         }
85         mAdapter = std::make_unique<HWC2On1Adapter>(
86                 reinterpret_cast<hwc_composer_device_1*>(device));
88         // Place the adapter in front of the device module.
89         mDevice = mAdapter.get();
90     } else {
91         mDevice = reinterpret_cast<hwc2_device_t*>(device);
92     }
94     return majorVersion;
95 }
97 uint32_t HwcHal::initWithFb(const hw_module_t* module)
98 {
99     framebuffer_device_t* fb_device;
100     int error = framebuffer_open(module, &fb_device);
101     if (error != 0) {
102         ALOGE("Failed to open FB device (%s), aborting", strerror(-error));
103         abort();
104     }
106     mFbAdapter = std::make_unique<HWC2OnFbAdapter>(fb_device);
107     mDevice = mFbAdapter.get();
109     return 0;
112 void HwcHal::initCapabilities()
114     uint32_t count = 0;
115     mDevice->getCapabilities(mDevice, &count, nullptr);
117     std::vector<Capability> caps(count);
118     mDevice->getCapabilities(mDevice, &count, reinterpret_cast<
119               std::underlying_type<Capability>::type*>(caps.data()));
120     caps.resize(count);
122     mCapabilities.insert(caps.cbegin(), caps.cend());
125 template<typename T>
126 void HwcHal::initDispatch(hwc2_function_descriptor_t desc, T* outPfn)
128     auto pfn = mDevice->getFunction(mDevice, desc);
129     if (!pfn) {
130         LOG_ALWAYS_FATAL("failed to get hwcomposer2 function %d", desc);
131     }
133     *outPfn = reinterpret_cast<T>(pfn);
136 void HwcHal::initDispatch()
138     initDispatch(HWC2_FUNCTION_ACCEPT_DISPLAY_CHANGES,
139             &mDispatch.acceptDisplayChanges);
140     initDispatch(HWC2_FUNCTION_CREATE_LAYER, &mDispatch.createLayer);
141     initDispatch(HWC2_FUNCTION_CREATE_VIRTUAL_DISPLAY,
142             &mDispatch.createVirtualDisplay);
143     initDispatch(HWC2_FUNCTION_DESTROY_LAYER, &mDispatch.destroyLayer);
144     initDispatch(HWC2_FUNCTION_DESTROY_VIRTUAL_DISPLAY,
145             &mDispatch.destroyVirtualDisplay);
146     initDispatch(HWC2_FUNCTION_DUMP, &mDispatch.dump);
147     initDispatch(HWC2_FUNCTION_GET_ACTIVE_CONFIG, &mDispatch.getActiveConfig);
148     initDispatch(HWC2_FUNCTION_GET_CHANGED_COMPOSITION_TYPES,
149             &mDispatch.getChangedCompositionTypes);
150     initDispatch(HWC2_FUNCTION_GET_CLIENT_TARGET_SUPPORT,
151             &mDispatch.getClientTargetSupport);
152     initDispatch(HWC2_FUNCTION_GET_COLOR_MODES, &mDispatch.getColorModes);
153     initDispatch(HWC2_FUNCTION_GET_DISPLAY_ATTRIBUTE,
154             &mDispatch.getDisplayAttribute);
155     initDispatch(HWC2_FUNCTION_GET_DISPLAY_CONFIGS,
156             &mDispatch.getDisplayConfigs);
157     initDispatch(HWC2_FUNCTION_GET_DISPLAY_NAME, &mDispatch.getDisplayName);
158     initDispatch(HWC2_FUNCTION_GET_DISPLAY_REQUESTS,
159             &mDispatch.getDisplayRequests);
160     initDispatch(HWC2_FUNCTION_GET_DISPLAY_TYPE, &mDispatch.getDisplayType);
161     initDispatch(HWC2_FUNCTION_GET_DOZE_SUPPORT, &mDispatch.getDozeSupport);
162     initDispatch(HWC2_FUNCTION_GET_HDR_CAPABILITIES,
163             &mDispatch.getHdrCapabilities);
164     initDispatch(HWC2_FUNCTION_GET_MAX_VIRTUAL_DISPLAY_COUNT,
165             &mDispatch.getMaxVirtualDisplayCount);
166     initDispatch(HWC2_FUNCTION_GET_RELEASE_FENCES,
167             &mDispatch.getReleaseFences);
168     initDispatch(HWC2_FUNCTION_PRESENT_DISPLAY, &mDispatch.presentDisplay);
169     initDispatch(HWC2_FUNCTION_REGISTER_CALLBACK,
170             &mDispatch.registerCallback);
171     initDispatch(HWC2_FUNCTION_SET_ACTIVE_CONFIG, &mDispatch.setActiveConfig);
172     initDispatch(HWC2_FUNCTION_SET_CLIENT_TARGET, &mDispatch.setClientTarget);
173     initDispatch(HWC2_FUNCTION_SET_COLOR_MODE, &mDispatch.setColorMode);
174     initDispatch(HWC2_FUNCTION_SET_COLOR_TRANSFORM,
175             &mDispatch.setColorTransform);
176     initDispatch(HWC2_FUNCTION_SET_CURSOR_POSITION,
177             &mDispatch.setCursorPosition);
178     initDispatch(HWC2_FUNCTION_SET_LAYER_BLEND_MODE,
179             &mDispatch.setLayerBlendMode);
180     initDispatch(HWC2_FUNCTION_SET_LAYER_BUFFER, &mDispatch.setLayerBuffer);
181     initDispatch(HWC2_FUNCTION_SET_LAYER_COLOR, &mDispatch.setLayerColor);
182     initDispatch(HWC2_FUNCTION_SET_LAYER_COMPOSITION_TYPE,
183             &mDispatch.setLayerCompositionType);
184     initDispatch(HWC2_FUNCTION_SET_LAYER_DATASPACE,
185             &mDispatch.setLayerDataspace);
186     initDispatch(HWC2_FUNCTION_SET_LAYER_DISPLAY_FRAME,
187             &mDispatch.setLayerDisplayFrame);
188     initDispatch(HWC2_FUNCTION_SET_LAYER_PLANE_ALPHA,
189             &mDispatch.setLayerPlaneAlpha);
191     if (hasCapability(Capability::SIDEBAND_STREAM)) {
192         initDispatch(HWC2_FUNCTION_SET_LAYER_SIDEBAND_STREAM,
193                 &mDispatch.setLayerSidebandStream);
194     }
196     initDispatch(HWC2_FUNCTION_SET_LAYER_SOURCE_CROP,
197             &mDispatch.setLayerSourceCrop);
198     initDispatch(HWC2_FUNCTION_SET_LAYER_SURFACE_DAMAGE,
199             &mDispatch.setLayerSurfaceDamage);
200     initDispatch(HWC2_FUNCTION_SET_LAYER_TRANSFORM,
201             &mDispatch.setLayerTransform);
202     initDispatch(HWC2_FUNCTION_SET_LAYER_VISIBLE_REGION,
203             &mDispatch.setLayerVisibleRegion);
204     initDispatch(HWC2_FUNCTION_SET_LAYER_Z_ORDER, &mDispatch.setLayerZOrder);
205     initDispatch(HWC2_FUNCTION_SET_OUTPUT_BUFFER, &mDispatch.setOutputBuffer);
206     initDispatch(HWC2_FUNCTION_SET_POWER_MODE, &mDispatch.setPowerMode);
207     initDispatch(HWC2_FUNCTION_SET_VSYNC_ENABLED, &mDispatch.setVsyncEnabled);
208     initDispatch(HWC2_FUNCTION_VALIDATE_DISPLAY, &mDispatch.validateDisplay);
211 bool HwcHal::hasCapability(Capability capability) const
213     return (mCapabilities.count(capability) > 0);
216 Return<void> HwcHal::getCapabilities(getCapabilities_cb hidl_cb)
218     std::vector<Capability> caps(
219             mCapabilities.cbegin(), mCapabilities.cend());
221     hidl_vec<Capability> caps_reply;
222     caps_reply.setToExternal(caps.data(), caps.size());
223     hidl_cb(caps_reply);
225     return Void();
228 Return<void> HwcHal::dumpDebugInfo(dumpDebugInfo_cb hidl_cb)
230     uint32_t len = 0;
231     mDispatch.dump(mDevice, &len, nullptr);
233     std::vector<char> buf(len + 1);
234     mDispatch.dump(mDevice, &len, buf.data());
235     buf.resize(len + 1);
236     buf[len] = '\0';
238     hidl_string buf_reply;
239     buf_reply.setToExternal(buf.data(), len);
240     hidl_cb(buf_reply);
242     return Void();
245 Return<void> HwcHal::createClient(createClient_cb hidl_cb)
247     Error err = Error::NONE;
248     sp<ComposerClient> client;
250     {
251         std::unique_lock<std::mutex> lock(mClientMutex);
253         if (mClient != nullptr) {
254             // In surface flinger we delete a composer client on one thread and
255             // then create a new client on another thread. Although surface
256             // flinger ensures the calls are made in that sequence (destroy and
257             // then create), sometimes the calls land in the composer service
258             // inverted (create and then destroy). Wait for a brief period to
259             // see if the existing client is destroyed.
260             ALOGI("HwcHal::createClient: Client already exists. Waiting for"
261                     " it to be destroyed.");
262             mClientDestroyedWait.wait_for(lock, 1s,
263                     [this] { return mClient == nullptr; });
264             std::string doneMsg = mClient == nullptr ?
265                     "Existing client was destroyed." :
266                     "Existing client was never destroyed!";
267             ALOGI("HwcHal::createClient: Done waiting. %s", doneMsg.c_str());
268         }
270         // only one client is allowed
271         if (mClient == nullptr) {
272             client = new ComposerClient(*this);
273             client->initialize();
274             mClient = client;
275         } else {
276             err = Error::NO_RESOURCES;
277         }
278     }
280     hidl_cb(err, client);
282     return Void();
285 sp<ComposerClient> HwcHal::getClient()
287     std::lock_guard<std::mutex> lock(mClientMutex);
288     return (mClient != nullptr) ? mClient.promote() : nullptr;
291 void HwcHal::removeClient()
293     std::lock_guard<std::mutex> lock(mClientMutex);
294     mClient = nullptr;
295     mClientDestroyedWait.notify_all();
298 void HwcHal::hotplugHook(hwc2_callback_data_t callbackData,
299         hwc2_display_t display, int32_t connected)
301     auto hal = reinterpret_cast<HwcHal*>(callbackData);
302     auto client = hal->getClient();
303     if (client != nullptr) {
304         client->onHotplug(display,
305                 static_cast<IComposerCallback::Connection>(connected));
306     }
309 void HwcHal::refreshHook(hwc2_callback_data_t callbackData,
310         hwc2_display_t display)
312     auto hal = reinterpret_cast<HwcHal*>(callbackData);
313     hal->mMustValidateDisplay = true;
315     auto client = hal->getClient();
316     if (client != nullptr) {
317         client->onRefresh(display);
318     }
321 void HwcHal::vsyncHook(hwc2_callback_data_t callbackData,
322         hwc2_display_t display, int64_t timestamp)
324     auto hal = reinterpret_cast<HwcHal*>(callbackData);
325     auto client = hal->getClient();
326     if (client != nullptr) {
327         client->onVsync(display, timestamp);
328     }
331 void HwcHal::enableCallback(bool enable)
333     if (enable) {
334         mMustValidateDisplay = true;
336         mDispatch.registerCallback(mDevice, HWC2_CALLBACK_HOTPLUG, this,
337                 reinterpret_cast<hwc2_function_pointer_t>(hotplugHook));
338         mDispatch.registerCallback(mDevice, HWC2_CALLBACK_REFRESH, this,
339                 reinterpret_cast<hwc2_function_pointer_t>(refreshHook));
340         mDispatch.registerCallback(mDevice, HWC2_CALLBACK_VSYNC, this,
341                 reinterpret_cast<hwc2_function_pointer_t>(vsyncHook));
342     } else {
343         mDispatch.registerCallback(mDevice, HWC2_CALLBACK_HOTPLUG, this,
344                 nullptr);
345         mDispatch.registerCallback(mDevice, HWC2_CALLBACK_REFRESH, this,
346                 nullptr);
347         mDispatch.registerCallback(mDevice, HWC2_CALLBACK_VSYNC, this,
348                 nullptr);
349     }
352 uint32_t HwcHal::getMaxVirtualDisplayCount()
354     return mDispatch.getMaxVirtualDisplayCount(mDevice);
357 Error HwcHal::createVirtualDisplay(uint32_t width, uint32_t height,
358     PixelFormat* format, Display* outDisplay)
360     int32_t hwc_format = static_cast<int32_t>(*format);
361     int32_t err = mDispatch.createVirtualDisplay(mDevice, width, height,
362             &hwc_format, outDisplay);
363     *format = static_cast<PixelFormat>(hwc_format);
365     return static_cast<Error>(err);
368 Error HwcHal::destroyVirtualDisplay(Display display)
370     int32_t err = mDispatch.destroyVirtualDisplay(mDevice, display);
371     return static_cast<Error>(err);
374 Error HwcHal::createLayer(Display display, Layer* outLayer)
376     int32_t err = mDispatch.createLayer(mDevice, display, outLayer);
377     return static_cast<Error>(err);
380 Error HwcHal::destroyLayer(Display display, Layer layer)
382     int32_t err = mDispatch.destroyLayer(mDevice, display, layer);
383     return static_cast<Error>(err);
386 Error HwcHal::getActiveConfig(Display display, Config* outConfig)
388     int32_t err = mDispatch.getActiveConfig(mDevice, display, outConfig);
389     return static_cast<Error>(err);
392 Error HwcHal::getClientTargetSupport(Display display,
393         uint32_t width, uint32_t height,
394         PixelFormat format, Dataspace dataspace)
396     int32_t err = mDispatch.getClientTargetSupport(mDevice, display,
397             width, height, static_cast<int32_t>(format),
398             static_cast<int32_t>(dataspace));
399     return static_cast<Error>(err);
402 Error HwcHal::getColorModes(Display display, hidl_vec<ColorMode>* outModes)
404     uint32_t count = 0;
405     int32_t err = mDispatch.getColorModes(mDevice, display, &count, nullptr);
406     if (err != HWC2_ERROR_NONE) {
407         return static_cast<Error>(err);
408     }
410     outModes->resize(count);
411     err = mDispatch.getColorModes(mDevice, display, &count,
412             reinterpret_cast<std::underlying_type<ColorMode>::type*>(
413                 outModes->data()));
414     if (err != HWC2_ERROR_NONE) {
415         *outModes = hidl_vec<ColorMode>();
416         return static_cast<Error>(err);
417     }
419     return Error::NONE;
422 Error HwcHal::getDisplayAttribute(Display display, Config config,
423         IComposerClient::Attribute attribute, int32_t* outValue)
425     int32_t err = mDispatch.getDisplayAttribute(mDevice, display, config,
426             static_cast<int32_t>(attribute), outValue);
427     return static_cast<Error>(err);
430 Error HwcHal::getDisplayConfigs(Display display, hidl_vec<Config>* outConfigs)
432     uint32_t count = 0;
433     int32_t err = mDispatch.getDisplayConfigs(mDevice, display,
434             &count, nullptr);
435     if (err != HWC2_ERROR_NONE) {
436         return static_cast<Error>(err);
437     }
439     outConfigs->resize(count);
440     err = mDispatch.getDisplayConfigs(mDevice, display,
441             &count, outConfigs->data());
442     if (err != HWC2_ERROR_NONE) {
443         *outConfigs = hidl_vec<Config>();
444         return static_cast<Error>(err);
445     }
447     return Error::NONE;
450 Error HwcHal::getDisplayName(Display display, hidl_string* outName)
452     uint32_t count = 0;
453     int32_t err = mDispatch.getDisplayName(mDevice, display, &count, nullptr);
454     if (err != HWC2_ERROR_NONE) {
455         return static_cast<Error>(err);
456     }
458     std::vector<char> buf(count + 1);
459     err = mDispatch.getDisplayName(mDevice, display, &count, buf.data());
460     if (err != HWC2_ERROR_NONE) {
461         return static_cast<Error>(err);
462     }
463     buf.resize(count + 1);
464     buf[count] = '\0';
466     *outName = buf.data();
468     return Error::NONE;
471 Error HwcHal::getDisplayType(Display display,
472         IComposerClient::DisplayType* outType)
474     int32_t hwc_type = HWC2_DISPLAY_TYPE_INVALID;
475     int32_t err = mDispatch.getDisplayType(mDevice, display, &hwc_type);
476     *outType = static_cast<IComposerClient::DisplayType>(hwc_type);
478     return static_cast<Error>(err);
481 Error HwcHal::getDozeSupport(Display display, bool* outSupport)
483     int32_t hwc_support = 0;
484     int32_t err = mDispatch.getDozeSupport(mDevice, display, &hwc_support);
485     *outSupport = hwc_support;
487     return static_cast<Error>(err);
490 Error HwcHal::getHdrCapabilities(Display display, hidl_vec<Hdr>* outTypes,
491         float* outMaxLuminance, float* outMaxAverageLuminance,
492         float* outMinLuminance)
494     uint32_t count = 0;
495     int32_t err = mDispatch.getHdrCapabilities(mDevice, display, &count,
496             nullptr, outMaxLuminance, outMaxAverageLuminance,
497             outMinLuminance);
498     if (err != HWC2_ERROR_NONE) {
499         return static_cast<Error>(err);
500     }
502     outTypes->resize(count);
503     err = mDispatch.getHdrCapabilities(mDevice, display, &count,
504             reinterpret_cast<std::underlying_type<Hdr>::type*>(
505                 outTypes->data()), outMaxLuminance,
506             outMaxAverageLuminance, outMinLuminance);
507     if (err != HWC2_ERROR_NONE) {
508         *outTypes = hidl_vec<Hdr>();
509         return static_cast<Error>(err);
510     }
512     return Error::NONE;
515 Error HwcHal::setActiveConfig(Display display, Config config)
517     int32_t err = mDispatch.setActiveConfig(mDevice, display, config);
518     return static_cast<Error>(err);
521 Error HwcHal::setColorMode(Display display, ColorMode mode)
523     int32_t err = mDispatch.setColorMode(mDevice, display,
524             static_cast<int32_t>(mode));
525     return static_cast<Error>(err);
528 Error HwcHal::setPowerMode(Display display, IComposerClient::PowerMode mode)
530     int32_t err = mDispatch.setPowerMode(mDevice, display,
531             static_cast<int32_t>(mode));
532     return static_cast<Error>(err);
535 Error HwcHal::setVsyncEnabled(Display display, IComposerClient::Vsync enabled)
537     int32_t err = mDispatch.setVsyncEnabled(mDevice, display,
538             static_cast<int32_t>(enabled));
539     return static_cast<Error>(err);
542 Error HwcHal::setColorTransform(Display display, const float* matrix,
543         int32_t hint)
545     int32_t err = mDispatch.setColorTransform(mDevice, display, matrix, hint);
546     return static_cast<Error>(err);
549 Error HwcHal::setClientTarget(Display display, buffer_handle_t target,
550         int32_t acquireFence, int32_t dataspace,
551         const std::vector<hwc_rect_t>& damage)
553     hwc_region region = { damage.size(), damage.data() };
554     int32_t err = mDispatch.setClientTarget(mDevice, display, target,
555             acquireFence, dataspace, region);
556     return static_cast<Error>(err);
559 Error HwcHal::setOutputBuffer(Display display, buffer_handle_t buffer,
560         int32_t releaseFence)
562     int32_t err = mDispatch.setOutputBuffer(mDevice, display, buffer,
563             releaseFence);
564     // unlike in setClientTarget, releaseFence is owned by us
565     if (err == HWC2_ERROR_NONE && releaseFence >= 0) {
566         close(releaseFence);
567     }
569     return static_cast<Error>(err);
572 Error HwcHal::validateDisplay(Display display,
573         std::vector<Layer>* outChangedLayers,
574         std::vector<IComposerClient::Composition>* outCompositionTypes,
575         uint32_t* outDisplayRequestMask,
576         std::vector<Layer>* outRequestedLayers,
577         std::vector<uint32_t>* outRequestMasks)
579     uint32_t types_count = 0;
580     uint32_t reqs_count = 0;
581     int32_t err = mDispatch.validateDisplay(mDevice, display,
582             &types_count, &reqs_count);
583     mMustValidateDisplay = false;
585     if (err != HWC2_ERROR_NONE && err != HWC2_ERROR_HAS_CHANGES) {
586         return static_cast<Error>(err);
587     }
589     err = mDispatch.getChangedCompositionTypes(mDevice, display,
590             &types_count, nullptr, nullptr);
591     if (err != HWC2_ERROR_NONE) {
592         return static_cast<Error>(err);
593     }
595     outChangedLayers->resize(types_count);
596     outCompositionTypes->resize(types_count);
597     err = mDispatch.getChangedCompositionTypes(mDevice, display,
598             &types_count, outChangedLayers->data(),
599             reinterpret_cast<
600             std::underlying_type<IComposerClient::Composition>::type*>(
601                 outCompositionTypes->data()));
602     if (err != HWC2_ERROR_NONE) {
603         outChangedLayers->clear();
604         outCompositionTypes->clear();
605         return static_cast<Error>(err);
606     }
608     int32_t display_reqs = 0;
609     err = mDispatch.getDisplayRequests(mDevice, display, &display_reqs,
610             &reqs_count, nullptr, nullptr);
611     if (err != HWC2_ERROR_NONE) {
612         outChangedLayers->clear();
613         outCompositionTypes->clear();
614         return static_cast<Error>(err);
615     }
617     outRequestedLayers->resize(reqs_count);
618     outRequestMasks->resize(reqs_count);
619     err = mDispatch.getDisplayRequests(mDevice, display, &display_reqs,
620             &reqs_count, outRequestedLayers->data(),
621             reinterpret_cast<int32_t*>(outRequestMasks->data()));
622     if (err != HWC2_ERROR_NONE) {
623         outChangedLayers->clear();
624         outCompositionTypes->clear();
626         outRequestedLayers->clear();
627         outRequestMasks->clear();
628         return static_cast<Error>(err);
629     }
631     *outDisplayRequestMask = display_reqs;
633     return static_cast<Error>(err);
636 Error HwcHal::acceptDisplayChanges(Display display)
638     int32_t err = mDispatch.acceptDisplayChanges(mDevice, display);
639     return static_cast<Error>(err);
642 Error HwcHal::presentDisplay(Display display, int32_t* outPresentFence,
643         std::vector<Layer>* outLayers, std::vector<int32_t>* outReleaseFences)
645     if (mMustValidateDisplay) {
646         return Error::NOT_VALIDATED;
647     }
649     *outPresentFence = -1;
650     int32_t err = mDispatch.presentDisplay(mDevice, display, outPresentFence);
651     if (err != HWC2_ERROR_NONE) {
652         return static_cast<Error>(err);
653     }
655     uint32_t count = 0;
656     err = mDispatch.getReleaseFences(mDevice, display, &count,
657             nullptr, nullptr);
658     if (err != HWC2_ERROR_NONE) {
659         ALOGW("failed to get release fences");
660         return Error::NONE;
661     }
663     outLayers->resize(count);
664     outReleaseFences->resize(count);
665     err = mDispatch.getReleaseFences(mDevice, display, &count,
666             outLayers->data(), outReleaseFences->data());
667     if (err != HWC2_ERROR_NONE) {
668         ALOGW("failed to get release fences");
669         outLayers->clear();
670         outReleaseFences->clear();
671         return Error::NONE;
672     }
674     return static_cast<Error>(err);
677 Error HwcHal::setLayerCursorPosition(Display display, Layer layer,
678         int32_t x, int32_t y)
680     int32_t err = mDispatch.setCursorPosition(mDevice, display, layer, x, y);
681     return static_cast<Error>(err);
684 Error HwcHal::setLayerBuffer(Display display, Layer layer,
685         buffer_handle_t buffer, int32_t acquireFence)
687     int32_t err = mDispatch.setLayerBuffer(mDevice, display, layer,
688             buffer, acquireFence);
689     return static_cast<Error>(err);
692 Error HwcHal::setLayerSurfaceDamage(Display display, Layer layer,
693         const std::vector<hwc_rect_t>& damage)
695     hwc_region region = { damage.size(), damage.data() };
696     int32_t err = mDispatch.setLayerSurfaceDamage(mDevice, display, layer,
697             region);
698     return static_cast<Error>(err);
701 Error HwcHal::setLayerBlendMode(Display display, Layer layer, int32_t mode)
703     int32_t err = mDispatch.setLayerBlendMode(mDevice, display, layer, mode);
704     return static_cast<Error>(err);
707 Error HwcHal::setLayerColor(Display display, Layer layer,
708         IComposerClient::Color color)
710     hwc_color_t hwc_color{color.r, color.g, color.b, color.a};
711     int32_t err = mDispatch.setLayerColor(mDevice, display, layer, hwc_color);
712     return static_cast<Error>(err);
715 Error HwcHal::setLayerCompositionType(Display display, Layer layer,
716         int32_t type)
718     int32_t err = mDispatch.setLayerCompositionType(mDevice, display, layer,
719             type);
720     return static_cast<Error>(err);
723 Error HwcHal::setLayerDataspace(Display display, Layer layer,
724         int32_t dataspace)
726     int32_t err = mDispatch.setLayerDataspace(mDevice, display, layer,
727             dataspace);
728     return static_cast<Error>(err);
731 Error HwcHal::setLayerDisplayFrame(Display display, Layer layer,
732         const hwc_rect_t& frame)
734     int32_t err = mDispatch.setLayerDisplayFrame(mDevice, display, layer,
735             frame);
736     return static_cast<Error>(err);
739 Error HwcHal::setLayerPlaneAlpha(Display display, Layer layer, float alpha)
741     int32_t err = mDispatch.setLayerPlaneAlpha(mDevice, display, layer,
742             alpha);
743     return static_cast<Error>(err);
746 Error HwcHal::setLayerSidebandStream(Display display, Layer layer,
747         buffer_handle_t stream)
749     int32_t err = mDispatch.setLayerSidebandStream(mDevice, display, layer,
750             stream);
751     return static_cast<Error>(err);
754 Error HwcHal::setLayerSourceCrop(Display display, Layer layer,
755         const hwc_frect_t& crop)
757     int32_t err = mDispatch.setLayerSourceCrop(mDevice, display, layer, crop);
758     return static_cast<Error>(err);
761 Error HwcHal::setLayerTransform(Display display, Layer layer,
762         int32_t transform)
764     int32_t err = mDispatch.setLayerTransform(mDevice, display, layer,
765             transform);
766     return static_cast<Error>(err);
769 Error HwcHal::setLayerVisibleRegion(Display display, Layer layer,
770         const std::vector<hwc_rect_t>& visible)
772     hwc_region_t region = { visible.size(), visible.data() };
773     int32_t err = mDispatch.setLayerVisibleRegion(mDevice, display, layer,
774             region);
775     return static_cast<Error>(err);
778 Error HwcHal::setLayerZOrder(Display display, Layer layer, uint32_t z)
780     int32_t err = mDispatch.setLayerZOrder(mDevice, display, layer, z);
781     return static_cast<Error>(err);
784 IComposer* HIDL_FETCH_IComposer(const char*)
786     const hw_module_t* module = nullptr;
787     int err = hw_get_module(HWC_HARDWARE_MODULE_ID, &module);
788     if (err) {
789         ALOGI("falling back to FB HAL");
790         err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
791     }
792     if (err) {
793         ALOGE("failed to get hwcomposer or fb module");
794         return nullptr;
795     }
797     return new HwcHal(module);
800 } // namespace implementation
801 } // namespace V2_1
802 } // namespace composer
803 } // namespace graphics
804 } // namespace hardware
805 } // namespace android