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 #define LOG_TAG "HwcPassthrough"
18 
19 #include "Hwc.h"
20 
21 #include <type_traits>
22 #include <log/log.h>
23 
24 #include "ComposerClient.h"
25 #include "hardware/hwcomposer.h"
26 #include "hwc2on1adapter/HWC2On1Adapter.h"
27 
28 namespace android {
29 namespace hardware {
30 namespace graphics {
31 namespace composer {
32 namespace V2_1 {
33 namespace implementation {
34 
35 
HwcHal(const hw_module_t * module)36 HwcHal::HwcHal(const hw_module_t* module)
37     : mDevice(nullptr), mDispatch(), mAdapter()
38 {
39     // Determine what kind of module is available (HWC2 vs HWC1.X).
40     hw_device_t* device = nullptr;
41     int error = module->methods->open(module, HWC_HARDWARE_COMPOSER, &device);
42     if (error != 0) {
43         ALOGE("Failed to open HWC device (%s), aborting", strerror(-error));
44         abort();
45     }
46     uint32_t majorVersion = (device->version >> 24) & 0xF;
47 
48     // If we don't have a HWC2, we need to wrap whatever we have in an adapter.
49     if (majorVersion != 2) {
50         uint32_t minorVersion = device->version & HARDWARE_API_VERSION_2_MAJ_MIN_MASK;
51         minorVersion = (minorVersion >> 16) & 0xF;
52         ALOGI("Found HWC implementation v%d.%d", majorVersion, minorVersion);
53         if (minorVersion < 1) {
54             ALOGE("Cannot adapt to HWC version %d.%d. Minimum supported is 1.1",
55                   majorVersion, minorVersion);
56             abort();
57         }
58         mAdapter = std::make_unique<HWC2On1Adapter>(
59                 reinterpret_cast<hwc_composer_device_1*>(device));
60 
61         // Place the adapter in front of the device module.
62         mDevice = mAdapter.get();
63     } else {
64         mDevice = reinterpret_cast<hwc2_device_t*>(device);
65     }
66 
67     initCapabilities();
68     if (majorVersion >= 2 &&
69         hasCapability(Capability::PRESENT_FENCE_IS_NOT_RELIABLE)) {
70         ALOGE("Present fence must be reliable from HWC2 on.");
71         abort();
72     }
73 
74     initDispatch();
75 }
76 
~HwcHal()77 HwcHal::~HwcHal()
78 {
79     hwc2_close(mDevice);
80 }
81 
initCapabilities()82 void HwcHal::initCapabilities()
83 {
84     uint32_t count = 0;
85     mDevice->getCapabilities(mDevice, &count, nullptr);
86 
87     std::vector<Capability> caps(count);
88     mDevice->getCapabilities(mDevice, &count, reinterpret_cast<
89               std::underlying_type<Capability>::type*>(caps.data()));
90     caps.resize(count);
91 
92     mCapabilities.insert(caps.cbegin(), caps.cend());
93 }
94 
95 template<typename T>
initDispatch(hwc2_function_descriptor_t desc,T * outPfn)96 void HwcHal::initDispatch(hwc2_function_descriptor_t desc, T* outPfn)
97 {
98     auto pfn = mDevice->getFunction(mDevice, desc);
99     if (!pfn) {
100         LOG_ALWAYS_FATAL("failed to get hwcomposer2 function %d", desc);
101     }
102 
103     *outPfn = reinterpret_cast<T>(pfn);
104 }
105 
initDispatch()106 void HwcHal::initDispatch()
107 {
108     initDispatch(HWC2_FUNCTION_ACCEPT_DISPLAY_CHANGES,
109             &mDispatch.acceptDisplayChanges);
110     initDispatch(HWC2_FUNCTION_CREATE_LAYER, &mDispatch.createLayer);
111     initDispatch(HWC2_FUNCTION_CREATE_VIRTUAL_DISPLAY,
112             &mDispatch.createVirtualDisplay);
113     initDispatch(HWC2_FUNCTION_DESTROY_LAYER, &mDispatch.destroyLayer);
114     initDispatch(HWC2_FUNCTION_DESTROY_VIRTUAL_DISPLAY,
115             &mDispatch.destroyVirtualDisplay);
116     initDispatch(HWC2_FUNCTION_DUMP, &mDispatch.dump);
117     initDispatch(HWC2_FUNCTION_GET_ACTIVE_CONFIG, &mDispatch.getActiveConfig);
118     initDispatch(HWC2_FUNCTION_GET_CHANGED_COMPOSITION_TYPES,
119             &mDispatch.getChangedCompositionTypes);
120     initDispatch(HWC2_FUNCTION_GET_CLIENT_TARGET_SUPPORT,
121             &mDispatch.getClientTargetSupport);
122     initDispatch(HWC2_FUNCTION_GET_COLOR_MODES, &mDispatch.getColorModes);
123     initDispatch(HWC2_FUNCTION_GET_DISPLAY_ATTRIBUTE,
124             &mDispatch.getDisplayAttribute);
125     initDispatch(HWC2_FUNCTION_GET_DISPLAY_CONFIGS,
126             &mDispatch.getDisplayConfigs);
127     initDispatch(HWC2_FUNCTION_GET_DISPLAY_NAME, &mDispatch.getDisplayName);
128     initDispatch(HWC2_FUNCTION_GET_DISPLAY_REQUESTS,
129             &mDispatch.getDisplayRequests);
130     initDispatch(HWC2_FUNCTION_GET_DISPLAY_TYPE, &mDispatch.getDisplayType);
131     initDispatch(HWC2_FUNCTION_GET_DOZE_SUPPORT, &mDispatch.getDozeSupport);
132     initDispatch(HWC2_FUNCTION_GET_HDR_CAPABILITIES,
133             &mDispatch.getHdrCapabilities);
134     initDispatch(HWC2_FUNCTION_GET_MAX_VIRTUAL_DISPLAY_COUNT,
135             &mDispatch.getMaxVirtualDisplayCount);
136     initDispatch(HWC2_FUNCTION_GET_RELEASE_FENCES,
137             &mDispatch.getReleaseFences);
138     initDispatch(HWC2_FUNCTION_PRESENT_DISPLAY, &mDispatch.presentDisplay);
139     initDispatch(HWC2_FUNCTION_REGISTER_CALLBACK,
140             &mDispatch.registerCallback);
141     initDispatch(HWC2_FUNCTION_SET_ACTIVE_CONFIG, &mDispatch.setActiveConfig);
142     initDispatch(HWC2_FUNCTION_SET_CLIENT_TARGET, &mDispatch.setClientTarget);
143     initDispatch(HWC2_FUNCTION_SET_COLOR_MODE, &mDispatch.setColorMode);
144     initDispatch(HWC2_FUNCTION_SET_COLOR_TRANSFORM,
145             &mDispatch.setColorTransform);
146     initDispatch(HWC2_FUNCTION_SET_CURSOR_POSITION,
147             &mDispatch.setCursorPosition);
148     initDispatch(HWC2_FUNCTION_SET_LAYER_BLEND_MODE,
149             &mDispatch.setLayerBlendMode);
150     initDispatch(HWC2_FUNCTION_SET_LAYER_BUFFER, &mDispatch.setLayerBuffer);
151     initDispatch(HWC2_FUNCTION_SET_LAYER_COLOR, &mDispatch.setLayerColor);
152     initDispatch(HWC2_FUNCTION_SET_LAYER_COMPOSITION_TYPE,
153             &mDispatch.setLayerCompositionType);
154     initDispatch(HWC2_FUNCTION_SET_LAYER_DATASPACE,
155             &mDispatch.setLayerDataspace);
156     initDispatch(HWC2_FUNCTION_SET_LAYER_DISPLAY_FRAME,
157             &mDispatch.setLayerDisplayFrame);
158     initDispatch(HWC2_FUNCTION_SET_LAYER_PLANE_ALPHA,
159             &mDispatch.setLayerPlaneAlpha);
160 
161     if (hasCapability(Capability::SIDEBAND_STREAM)) {
162         initDispatch(HWC2_FUNCTION_SET_LAYER_SIDEBAND_STREAM,
163                 &mDispatch.setLayerSidebandStream);
164     }
165 
166     initDispatch(HWC2_FUNCTION_SET_LAYER_SOURCE_CROP,
167             &mDispatch.setLayerSourceCrop);
168     initDispatch(HWC2_FUNCTION_SET_LAYER_SURFACE_DAMAGE,
169             &mDispatch.setLayerSurfaceDamage);
170     initDispatch(HWC2_FUNCTION_SET_LAYER_TRANSFORM,
171             &mDispatch.setLayerTransform);
172     initDispatch(HWC2_FUNCTION_SET_LAYER_VISIBLE_REGION,
173             &mDispatch.setLayerVisibleRegion);
174     initDispatch(HWC2_FUNCTION_SET_LAYER_Z_ORDER, &mDispatch.setLayerZOrder);
175     initDispatch(HWC2_FUNCTION_SET_OUTPUT_BUFFER, &mDispatch.setOutputBuffer);
176     initDispatch(HWC2_FUNCTION_SET_POWER_MODE, &mDispatch.setPowerMode);
177     initDispatch(HWC2_FUNCTION_SET_VSYNC_ENABLED, &mDispatch.setVsyncEnabled);
178     initDispatch(HWC2_FUNCTION_VALIDATE_DISPLAY, &mDispatch.validateDisplay);
179 }
180 
hasCapability(Capability capability) const181 bool HwcHal::hasCapability(Capability capability) const
182 {
183     return (mCapabilities.count(capability) > 0);
184 }
185 
getCapabilities(getCapabilities_cb hidl_cb)186 Return<void> HwcHal::getCapabilities(getCapabilities_cb hidl_cb)
187 {
188     std::vector<Capability> caps(
189             mCapabilities.cbegin(), mCapabilities.cend());
190 
191     hidl_vec<Capability> caps_reply;
192     caps_reply.setToExternal(caps.data(), caps.size());
193     hidl_cb(caps_reply);
194 
195     return Void();
196 }
197 
dumpDebugInfo(dumpDebugInfo_cb hidl_cb)198 Return<void> HwcHal::dumpDebugInfo(dumpDebugInfo_cb hidl_cb)
199 {
200     uint32_t len = 0;
201     mDispatch.dump(mDevice, &len, nullptr);
202 
203     std::vector<char> buf(len + 1);
204     mDispatch.dump(mDevice, &len, buf.data());
205     buf.resize(len + 1);
206     buf[len] = '\0';
207 
208     hidl_string buf_reply;
209     buf_reply.setToExternal(buf.data(), len);
210     hidl_cb(buf_reply);
211 
212     return Void();
213 }
214 
createClient(createClient_cb hidl_cb)215 Return<void> HwcHal::createClient(createClient_cb hidl_cb)
216 {
217     Error err = Error::NONE;
218     sp<ComposerClient> client;
219 
220     {
221         std::lock_guard<std::mutex> lock(mClientMutex);
222 
223         // only one client is allowed
224         if (mClient == nullptr) {
225             client = new ComposerClient(*this);
226             client->initialize();
227             mClient = client;
228         } else {
229             err = Error::NO_RESOURCES;
230         }
231     }
232 
233     hidl_cb(err, client);
234 
235     return Void();
236 }
237 
getClient()238 sp<ComposerClient> HwcHal::getClient()
239 {
240     std::lock_guard<std::mutex> lock(mClientMutex);
241     return (mClient != nullptr) ? mClient.promote() : nullptr;
242 }
243 
removeClient()244 void HwcHal::removeClient()
245 {
246     std::lock_guard<std::mutex> lock(mClientMutex);
247     mClient = nullptr;
248 }
249 
hotplugHook(hwc2_callback_data_t callbackData,hwc2_display_t display,int32_t connected)250 void HwcHal::hotplugHook(hwc2_callback_data_t callbackData,
251         hwc2_display_t display, int32_t connected)
252 {
253     auto hal = reinterpret_cast<HwcHal*>(callbackData);
254     auto client = hal->getClient();
255     if (client != nullptr) {
256         client->onHotplug(display,
257                 static_cast<IComposerCallback::Connection>(connected));
258     }
259 }
260 
refreshHook(hwc2_callback_data_t callbackData,hwc2_display_t display)261 void HwcHal::refreshHook(hwc2_callback_data_t callbackData,
262         hwc2_display_t display)
263 {
264     auto hal = reinterpret_cast<HwcHal*>(callbackData);
265     auto client = hal->getClient();
266     if (client != nullptr) {
267         client->onRefresh(display);
268     }
269 }
270 
vsyncHook(hwc2_callback_data_t callbackData,hwc2_display_t display,int64_t timestamp)271 void HwcHal::vsyncHook(hwc2_callback_data_t callbackData,
272         hwc2_display_t display, int64_t timestamp)
273 {
274     auto hal = reinterpret_cast<HwcHal*>(callbackData);
275     auto client = hal->getClient();
276     if (client != nullptr) {
277         client->onVsync(display, timestamp);
278     }
279 }
280 
enableCallback(bool enable)281 void HwcHal::enableCallback(bool enable)
282 {
283     if (enable) {
284         mDispatch.registerCallback(mDevice, HWC2_CALLBACK_HOTPLUG, this,
285                 reinterpret_cast<hwc2_function_pointer_t>(hotplugHook));
286         mDispatch.registerCallback(mDevice, HWC2_CALLBACK_REFRESH, this,
287                 reinterpret_cast<hwc2_function_pointer_t>(refreshHook));
288         mDispatch.registerCallback(mDevice, HWC2_CALLBACK_VSYNC, this,
289                 reinterpret_cast<hwc2_function_pointer_t>(vsyncHook));
290     } else {
291         mDispatch.registerCallback(mDevice, HWC2_CALLBACK_HOTPLUG, this,
292                 nullptr);
293         mDispatch.registerCallback(mDevice, HWC2_CALLBACK_REFRESH, this,
294                 nullptr);
295         mDispatch.registerCallback(mDevice, HWC2_CALLBACK_VSYNC, this,
296                 nullptr);
297     }
298 }
299 
getMaxVirtualDisplayCount()300 uint32_t HwcHal::getMaxVirtualDisplayCount()
301 {
302     return mDispatch.getMaxVirtualDisplayCount(mDevice);
303 }
304 
createVirtualDisplay(uint32_t width,uint32_t height,PixelFormat * format,Display * outDisplay)305 Error HwcHal::createVirtualDisplay(uint32_t width, uint32_t height,
306     PixelFormat* format, Display* outDisplay)
307 {
308     int32_t hwc_format = static_cast<int32_t>(*format);
309     int32_t err = mDispatch.createVirtualDisplay(mDevice, width, height,
310             &hwc_format, outDisplay);
311     *format = static_cast<PixelFormat>(hwc_format);
312 
313     return static_cast<Error>(err);
314 }
315 
destroyVirtualDisplay(Display display)316 Error HwcHal::destroyVirtualDisplay(Display display)
317 {
318     int32_t err = mDispatch.destroyVirtualDisplay(mDevice, display);
319     return static_cast<Error>(err);
320 }
321 
createLayer(Display display,Layer * outLayer)322 Error HwcHal::createLayer(Display display, Layer* outLayer)
323 {
324     int32_t err = mDispatch.createLayer(mDevice, display, outLayer);
325     return static_cast<Error>(err);
326 }
327 
destroyLayer(Display display,Layer layer)328 Error HwcHal::destroyLayer(Display display, Layer layer)
329 {
330     int32_t err = mDispatch.destroyLayer(mDevice, display, layer);
331     return static_cast<Error>(err);
332 }
333 
getActiveConfig(Display display,Config * outConfig)334 Error HwcHal::getActiveConfig(Display display, Config* outConfig)
335 {
336     int32_t err = mDispatch.getActiveConfig(mDevice, display, outConfig);
337     return static_cast<Error>(err);
338 }
339 
getClientTargetSupport(Display display,uint32_t width,uint32_t height,PixelFormat format,Dataspace dataspace)340 Error HwcHal::getClientTargetSupport(Display display,
341         uint32_t width, uint32_t height,
342         PixelFormat format, Dataspace dataspace)
343 {
344     int32_t err = mDispatch.getClientTargetSupport(mDevice, display,
345             width, height, static_cast<int32_t>(format),
346             static_cast<int32_t>(dataspace));
347     return static_cast<Error>(err);
348 }
349 
getColorModes(Display display,hidl_vec<ColorMode> * outModes)350 Error HwcHal::getColorModes(Display display, hidl_vec<ColorMode>* outModes)
351 {
352     uint32_t count = 0;
353     int32_t err = mDispatch.getColorModes(mDevice, display, &count, nullptr);
354     if (err != HWC2_ERROR_NONE) {
355         return static_cast<Error>(err);
356     }
357 
358     outModes->resize(count);
359     err = mDispatch.getColorModes(mDevice, display, &count,
360             reinterpret_cast<std::underlying_type<ColorMode>::type*>(
361                 outModes->data()));
362     if (err != HWC2_ERROR_NONE) {
363         *outModes = hidl_vec<ColorMode>();
364         return static_cast<Error>(err);
365     }
366 
367     return Error::NONE;
368 }
369 
getDisplayAttribute(Display display,Config config,IComposerClient::Attribute attribute,int32_t * outValue)370 Error HwcHal::getDisplayAttribute(Display display, Config config,
371         IComposerClient::Attribute attribute, int32_t* outValue)
372 {
373     int32_t err = mDispatch.getDisplayAttribute(mDevice, display, config,
374             static_cast<int32_t>(attribute), outValue);
375     return static_cast<Error>(err);
376 }
377 
getDisplayConfigs(Display display,hidl_vec<Config> * outConfigs)378 Error HwcHal::getDisplayConfigs(Display display, hidl_vec<Config>* outConfigs)
379 {
380     uint32_t count = 0;
381     int32_t err = mDispatch.getDisplayConfigs(mDevice, display,
382             &count, nullptr);
383     if (err != HWC2_ERROR_NONE) {
384         return static_cast<Error>(err);
385     }
386 
387     outConfigs->resize(count);
388     err = mDispatch.getDisplayConfigs(mDevice, display,
389             &count, outConfigs->data());
390     if (err != HWC2_ERROR_NONE) {
391         *outConfigs = hidl_vec<Config>();
392         return static_cast<Error>(err);
393     }
394 
395     return Error::NONE;
396 }
397 
getDisplayName(Display display,hidl_string * outName)398 Error HwcHal::getDisplayName(Display display, hidl_string* outName)
399 {
400     uint32_t count = 0;
401     int32_t err = mDispatch.getDisplayName(mDevice, display, &count, nullptr);
402     if (err != HWC2_ERROR_NONE) {
403         return static_cast<Error>(err);
404     }
405 
406     std::vector<char> buf(count + 1);
407     err = mDispatch.getDisplayName(mDevice, display, &count, buf.data());
408     if (err != HWC2_ERROR_NONE) {
409         return static_cast<Error>(err);
410     }
411     buf.resize(count + 1);
412     buf[count] = '\0';
413 
414     *outName = buf.data();
415 
416     return Error::NONE;
417 }
418 
getDisplayType(Display display,IComposerClient::DisplayType * outType)419 Error HwcHal::getDisplayType(Display display,
420         IComposerClient::DisplayType* outType)
421 {
422     int32_t hwc_type = HWC2_DISPLAY_TYPE_INVALID;
423     int32_t err = mDispatch.getDisplayType(mDevice, display, &hwc_type);
424     *outType = static_cast<IComposerClient::DisplayType>(hwc_type);
425 
426     return static_cast<Error>(err);
427 }
428 
getDozeSupport(Display display,bool * outSupport)429 Error HwcHal::getDozeSupport(Display display, bool* outSupport)
430 {
431     int32_t hwc_support = 0;
432     int32_t err = mDispatch.getDozeSupport(mDevice, display, &hwc_support);
433     *outSupport = hwc_support;
434 
435     return static_cast<Error>(err);
436 }
437 
getHdrCapabilities(Display display,hidl_vec<Hdr> * outTypes,float * outMaxLuminance,float * outMaxAverageLuminance,float * outMinLuminance)438 Error HwcHal::getHdrCapabilities(Display display, hidl_vec<Hdr>* outTypes,
439         float* outMaxLuminance, float* outMaxAverageLuminance,
440         float* outMinLuminance)
441 {
442     uint32_t count = 0;
443     int32_t err = mDispatch.getHdrCapabilities(mDevice, display, &count,
444             nullptr, outMaxLuminance, outMaxAverageLuminance,
445             outMinLuminance);
446     if (err != HWC2_ERROR_NONE) {
447         return static_cast<Error>(err);
448     }
449 
450     outTypes->resize(count);
451     err = mDispatch.getHdrCapabilities(mDevice, display, &count,
452             reinterpret_cast<std::underlying_type<Hdr>::type*>(
453                 outTypes->data()), outMaxLuminance,
454             outMaxAverageLuminance, outMinLuminance);
455     if (err != HWC2_ERROR_NONE) {
456         *outTypes = hidl_vec<Hdr>();
457         return static_cast<Error>(err);
458     }
459 
460     return Error::NONE;
461 }
462 
setActiveConfig(Display display,Config config)463 Error HwcHal::setActiveConfig(Display display, Config config)
464 {
465     int32_t err = mDispatch.setActiveConfig(mDevice, display, config);
466     return static_cast<Error>(err);
467 }
468 
setColorMode(Display display,ColorMode mode)469 Error HwcHal::setColorMode(Display display, ColorMode mode)
470 {
471     int32_t err = mDispatch.setColorMode(mDevice, display,
472             static_cast<int32_t>(mode));
473     return static_cast<Error>(err);
474 }
475 
setPowerMode(Display display,IComposerClient::PowerMode mode)476 Error HwcHal::setPowerMode(Display display, IComposerClient::PowerMode mode)
477 {
478     int32_t err = mDispatch.setPowerMode(mDevice, display,
479             static_cast<int32_t>(mode));
480     return static_cast<Error>(err);
481 }
482 
setVsyncEnabled(Display display,IComposerClient::Vsync enabled)483 Error HwcHal::setVsyncEnabled(Display display, IComposerClient::Vsync enabled)
484 {
485     int32_t err = mDispatch.setVsyncEnabled(mDevice, display,
486             static_cast<int32_t>(enabled));
487     return static_cast<Error>(err);
488 }
489 
setColorTransform(Display display,const float * matrix,int32_t hint)490 Error HwcHal::setColorTransform(Display display, const float* matrix,
491         int32_t hint)
492 {
493     int32_t err = mDispatch.setColorTransform(mDevice, display, matrix, hint);
494     return static_cast<Error>(err);
495 }
496 
setClientTarget(Display display,buffer_handle_t target,int32_t acquireFence,int32_t dataspace,const std::vector<hwc_rect_t> & damage)497 Error HwcHal::setClientTarget(Display display, buffer_handle_t target,
498         int32_t acquireFence, int32_t dataspace,
499         const std::vector<hwc_rect_t>& damage)
500 {
501     hwc_region region = { damage.size(), damage.data() };
502     int32_t err = mDispatch.setClientTarget(mDevice, display, target,
503             acquireFence, dataspace, region);
504     return static_cast<Error>(err);
505 }
506 
setOutputBuffer(Display display,buffer_handle_t buffer,int32_t releaseFence)507 Error HwcHal::setOutputBuffer(Display display, buffer_handle_t buffer,
508         int32_t releaseFence)
509 {
510     int32_t err = mDispatch.setOutputBuffer(mDevice, display, buffer,
511             releaseFence);
512     // unlike in setClientTarget, releaseFence is owned by us
513     if (err == HWC2_ERROR_NONE && releaseFence >= 0) {
514         close(releaseFence);
515     }
516 
517     return static_cast<Error>(err);
518 }
519 
validateDisplay(Display display,std::vector<Layer> * outChangedLayers,std::vector<IComposerClient::Composition> * outCompositionTypes,uint32_t * outDisplayRequestMask,std::vector<Layer> * outRequestedLayers,std::vector<uint32_t> * outRequestMasks)520 Error HwcHal::validateDisplay(Display display,
521         std::vector<Layer>* outChangedLayers,
522         std::vector<IComposerClient::Composition>* outCompositionTypes,
523         uint32_t* outDisplayRequestMask,
524         std::vector<Layer>* outRequestedLayers,
525         std::vector<uint32_t>* outRequestMasks)
526 {
527     uint32_t types_count = 0;
528     uint32_t reqs_count = 0;
529     int32_t err = mDispatch.validateDisplay(mDevice, display,
530             &types_count, &reqs_count);
531     if (err != HWC2_ERROR_NONE && err != HWC2_ERROR_HAS_CHANGES) {
532         return static_cast<Error>(err);
533     }
534 
535     err = mDispatch.getChangedCompositionTypes(mDevice, display,
536             &types_count, nullptr, nullptr);
537     if (err != HWC2_ERROR_NONE) {
538         return static_cast<Error>(err);
539     }
540 
541     outChangedLayers->resize(types_count);
542     outCompositionTypes->resize(types_count);
543     err = mDispatch.getChangedCompositionTypes(mDevice, display,
544             &types_count, outChangedLayers->data(),
545             reinterpret_cast<
546             std::underlying_type<IComposerClient::Composition>::type*>(
547                 outCompositionTypes->data()));
548     if (err != HWC2_ERROR_NONE) {
549         outChangedLayers->clear();
550         outCompositionTypes->clear();
551         return static_cast<Error>(err);
552     }
553 
554     int32_t display_reqs = 0;
555     err = mDispatch.getDisplayRequests(mDevice, display, &display_reqs,
556             &reqs_count, nullptr, nullptr);
557     if (err != HWC2_ERROR_NONE) {
558         outChangedLayers->clear();
559         outCompositionTypes->clear();
560         return static_cast<Error>(err);
561     }
562 
563     outRequestedLayers->resize(reqs_count);
564     outRequestMasks->resize(reqs_count);
565     err = mDispatch.getDisplayRequests(mDevice, display, &display_reqs,
566             &reqs_count, outRequestedLayers->data(),
567             reinterpret_cast<int32_t*>(outRequestMasks->data()));
568     if (err != HWC2_ERROR_NONE) {
569         outChangedLayers->clear();
570         outCompositionTypes->clear();
571 
572         outRequestedLayers->clear();
573         outRequestMasks->clear();
574         return static_cast<Error>(err);
575     }
576 
577     *outDisplayRequestMask = display_reqs;
578 
579     return static_cast<Error>(err);
580 }
581 
acceptDisplayChanges(Display display)582 Error HwcHal::acceptDisplayChanges(Display display)
583 {
584     int32_t err = mDispatch.acceptDisplayChanges(mDevice, display);
585     return static_cast<Error>(err);
586 }
587 
presentDisplay(Display display,int32_t * outPresentFence,std::vector<Layer> * outLayers,std::vector<int32_t> * outReleaseFences)588 Error HwcHal::presentDisplay(Display display, int32_t* outPresentFence,
589         std::vector<Layer>* outLayers, std::vector<int32_t>* outReleaseFences)
590 {
591     *outPresentFence = -1;
592     int32_t err = mDispatch.presentDisplay(mDevice, display, outPresentFence);
593     if (err != HWC2_ERROR_NONE) {
594         return static_cast<Error>(err);
595     }
596 
597     uint32_t count = 0;
598     err = mDispatch.getReleaseFences(mDevice, display, &count,
599             nullptr, nullptr);
600     if (err != HWC2_ERROR_NONE) {
601         ALOGW("failed to get release fences");
602         return Error::NONE;
603     }
604 
605     outLayers->resize(count);
606     outReleaseFences->resize(count);
607     err = mDispatch.getReleaseFences(mDevice, display, &count,
608             outLayers->data(), outReleaseFences->data());
609     if (err != HWC2_ERROR_NONE) {
610         ALOGW("failed to get release fences");
611         outLayers->clear();
612         outReleaseFences->clear();
613         return Error::NONE;
614     }
615 
616     return static_cast<Error>(err);
617 }
618 
setLayerCursorPosition(Display display,Layer layer,int32_t x,int32_t y)619 Error HwcHal::setLayerCursorPosition(Display display, Layer layer,
620         int32_t x, int32_t y)
621 {
622     int32_t err = mDispatch.setCursorPosition(mDevice, display, layer, x, y);
623     return static_cast<Error>(err);
624 }
625 
setLayerBuffer(Display display,Layer layer,buffer_handle_t buffer,int32_t acquireFence)626 Error HwcHal::setLayerBuffer(Display display, Layer layer,
627         buffer_handle_t buffer, int32_t acquireFence)
628 {
629     int32_t err = mDispatch.setLayerBuffer(mDevice, display, layer,
630             buffer, acquireFence);
631     return static_cast<Error>(err);
632 }
633 
setLayerSurfaceDamage(Display display,Layer layer,const std::vector<hwc_rect_t> & damage)634 Error HwcHal::setLayerSurfaceDamage(Display display, Layer layer,
635         const std::vector<hwc_rect_t>& damage)
636 {
637     hwc_region region = { damage.size(), damage.data() };
638     int32_t err = mDispatch.setLayerSurfaceDamage(mDevice, display, layer,
639             region);
640     return static_cast<Error>(err);
641 }
642 
setLayerBlendMode(Display display,Layer layer,int32_t mode)643 Error HwcHal::setLayerBlendMode(Display display, Layer layer, int32_t mode)
644 {
645     int32_t err = mDispatch.setLayerBlendMode(mDevice, display, layer, mode);
646     return static_cast<Error>(err);
647 }
648 
setLayerColor(Display display,Layer layer,IComposerClient::Color color)649 Error HwcHal::setLayerColor(Display display, Layer layer,
650         IComposerClient::Color color)
651 {
652     hwc_color_t hwc_color{color.r, color.g, color.b, color.a};
653     int32_t err = mDispatch.setLayerColor(mDevice, display, layer, hwc_color);
654     return static_cast<Error>(err);
655 }
656 
setLayerCompositionType(Display display,Layer layer,int32_t type)657 Error HwcHal::setLayerCompositionType(Display display, Layer layer,
658         int32_t type)
659 {
660     int32_t err = mDispatch.setLayerCompositionType(mDevice, display, layer,
661             type);
662     return static_cast<Error>(err);
663 }
664 
setLayerDataspace(Display display,Layer layer,int32_t dataspace)665 Error HwcHal::setLayerDataspace(Display display, Layer layer,
666         int32_t dataspace)
667 {
668     int32_t err = mDispatch.setLayerDataspace(mDevice, display, layer,
669             dataspace);
670     return static_cast<Error>(err);
671 }
672 
setLayerDisplayFrame(Display display,Layer layer,const hwc_rect_t & frame)673 Error HwcHal::setLayerDisplayFrame(Display display, Layer layer,
674         const hwc_rect_t& frame)
675 {
676     int32_t err = mDispatch.setLayerDisplayFrame(mDevice, display, layer,
677             frame);
678     return static_cast<Error>(err);
679 }
680 
setLayerPlaneAlpha(Display display,Layer layer,float alpha)681 Error HwcHal::setLayerPlaneAlpha(Display display, Layer layer, float alpha)
682 {
683     int32_t err = mDispatch.setLayerPlaneAlpha(mDevice, display, layer,
684             alpha);
685     return static_cast<Error>(err);
686 }
687 
setLayerSidebandStream(Display display,Layer layer,buffer_handle_t stream)688 Error HwcHal::setLayerSidebandStream(Display display, Layer layer,
689         buffer_handle_t stream)
690 {
691     int32_t err = mDispatch.setLayerSidebandStream(mDevice, display, layer,
692             stream);
693     return static_cast<Error>(err);
694 }
695 
setLayerSourceCrop(Display display,Layer layer,const hwc_frect_t & crop)696 Error HwcHal::setLayerSourceCrop(Display display, Layer layer,
697         const hwc_frect_t& crop)
698 {
699     int32_t err = mDispatch.setLayerSourceCrop(mDevice, display, layer, crop);
700     return static_cast<Error>(err);
701 }
702 
setLayerTransform(Display display,Layer layer,int32_t transform)703 Error HwcHal::setLayerTransform(Display display, Layer layer,
704         int32_t transform)
705 {
706     int32_t err = mDispatch.setLayerTransform(mDevice, display, layer,
707             transform);
708     return static_cast<Error>(err);
709 }
710 
setLayerVisibleRegion(Display display,Layer layer,const std::vector<hwc_rect_t> & visible)711 Error HwcHal::setLayerVisibleRegion(Display display, Layer layer,
712         const std::vector<hwc_rect_t>& visible)
713 {
714     hwc_region_t region = { visible.size(), visible.data() };
715     int32_t err = mDispatch.setLayerVisibleRegion(mDevice, display, layer,
716             region);
717     return static_cast<Error>(err);
718 }
719 
setLayerZOrder(Display display,Layer layer,uint32_t z)720 Error HwcHal::setLayerZOrder(Display display, Layer layer, uint32_t z)
721 {
722     int32_t err = mDispatch.setLayerZOrder(mDevice, display, layer, z);
723     return static_cast<Error>(err);
724 }
725 
HIDL_FETCH_IComposer(const char *)726 IComposer* HIDL_FETCH_IComposer(const char*)
727 {
728     const hw_module_t* module = nullptr;
729     int err = hw_get_module(HWC_HARDWARE_MODULE_ID, &module);
730     if (err) {
731         ALOGE("failed to get hwcomposer module");
732         return nullptr;
733     }
734 
735     return new HwcHal(module);
736 }
737 
738 } // namespace implementation
739 } // namespace V2_1
740 } // namespace composer
741 } // namespace graphics
742 } // namespace hardware
743 } // namespace android
744