OSDN Git Service

Re-work hot plug event handling.
[android-x86/external-IA-Hardware-Composer.git] / os / android / iahwc2.cpp
1 /*
2  * Copyright (C) 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 ATRACE_TAG ATRACE_TAG_GRAPHICS
18
19 #include "iahwc2.h"
20 #include "utils_android.h"
21
22 #include <inttypes.h>
23
24 #include <cutils/log.h>
25 #include <cutils/properties.h>
26 #include <hardware/hardware.h>
27 #include <hardware/hwcomposer2.h>
28
29 #include <gpudevice.h>
30 #include <hwcdefs.h>
31 #include <nativedisplay.h>
32
33 #include <string>
34 #include <memory>
35 #include <algorithm>
36 #include <vector>
37
38 namespace android {
39
40 class IAVsyncCallback : public hwcomposer::VsyncCallback {
41  public:
42   IAVsyncCallback(hwc2_callback_data_t data, hwc2_function_pointer_t hook)
43       : data_(data), hook_(hook) {
44   }
45
46   void Callback(uint32_t display, int64_t timestamp) {
47     auto hook = reinterpret_cast<HWC2_PFN_VSYNC>(hook_);
48     hook(data_, display, timestamp);
49   }
50
51  private:
52   hwc2_callback_data_t data_;
53   hwc2_function_pointer_t hook_;
54 };
55
56 class IARefreshCallback : public hwcomposer::RefreshCallback {
57  public:
58   IARefreshCallback(hwc2_callback_data_t data, hwc2_function_pointer_t hook)
59       : data_(data), hook_(hook) {
60   }
61
62   void Callback(uint32_t display) {
63     auto hook = reinterpret_cast<HWC2_PFN_REFRESH>(hook_);
64     hook(data_, display);
65   }
66
67  private:
68   hwc2_callback_data_t data_;
69   hwc2_function_pointer_t hook_;
70 };
71
72 class IAHotPlugEventCallback : public hwcomposer::HotPlugCallback {
73  public:
74   IAHotPlugEventCallback(hwc2_callback_data_t data,
75                          hwc2_function_pointer_t hook)
76       : data_(data), hook_(hook) {
77   }
78
79   void Callback(uint32_t display, bool connected) {
80     auto hook = reinterpret_cast<HWC2_PFN_HOTPLUG>(hook_);
81     int32_t status = static_cast<int32_t>(HWC2::Connection::Connected);
82     if (!connected) {
83       status = static_cast<int32_t>(HWC2::Connection::Disconnected);
84     }
85
86     IHOTPLUGEVENTTRACE(
87         "IAHotPlugEventCallback called displayid: %d status: %d \n", display,
88         status);
89     hook(data_, display, status);
90   }
91
92  private:
93   hwc2_callback_data_t data_;
94   hwc2_function_pointer_t hook_;
95 };
96
97 IAHWC2::IAHWC2() {
98   common.tag = HARDWARE_DEVICE_TAG;
99   common.version = HWC_DEVICE_API_VERSION_2_0;
100   common.close = HookDevClose;
101   getCapabilities = HookDevGetCapabilities;
102   getFunction = HookDevGetFunction;
103 }
104
105 HWC2::Error IAHWC2::Init() {
106   char value[PROPERTY_VALUE_MAX];
107   property_get("board.disable.explicit.sync", value, "0");
108   disable_explicit_sync_ = atoi(value);
109   if (disable_explicit_sync_)
110     ALOGI("EXPLICIT SYNC support is disabled");
111   else
112     ALOGI("EXPLICIT SYNC support is enabled");
113
114   if (!device_.Initialize()) {
115     ALOGE("Can't initialize drm object.");
116     return HWC2::Error::NoResources;
117   }
118
119   std::vector<NativeDisplay *> displays = device_.GetAllDisplays();
120   size_t size = displays.size();
121   NativeDisplay *primary_display = NULL;
122   for (size_t i = 0; i < size; i++) {
123     hwcomposer::NativeDisplay *display = displays.at(i);
124     if (display->IsConnected()) {
125       primary_display = display;
126       break;
127     }
128   }
129
130   if (!primary_display) {
131     primary_display = displays.at(0);
132   }
133
134   primary_display_.Init(primary_display, 0, disable_explicit_sync_);
135
136   uint32_t external_display_id = 1;
137   for (size_t i = 0; i < size; ++i) {
138     hwcomposer::NativeDisplay *display = displays.at(i);
139     if (primary_display == display) {
140       continue;
141     }
142
143     std::unique_ptr<HwcDisplay> temp(new HwcDisplay());
144     temp->Init(display, external_display_id, disable_explicit_sync_);
145     extended_displays_.emplace_back(std::move(temp));
146     external_display_id++;
147   }
148
149   // Start the hwc service
150   // FIXME(IAHWC-76): On Android, with userdebug on Joule this is causing
151   //        to hang in a loop while cold boot before going to home screen
152   // hwcService_.Start(*this);
153
154   return HWC2::Error::None;
155 }
156
157 template <typename... Args>
158 static inline HWC2::Error unsupported(char const *func, Args... /*args*/) {
159   ALOGV("Unsupported function: %s", func);
160   return HWC2::Error::Unsupported;
161 }
162
163 static inline void supported(char const *func) {
164   ALOGV("supported function: %s", func);
165 }
166
167 HWC2::Error IAHWC2::CreateVirtualDisplay(uint32_t width, uint32_t height,
168                                          int32_t *format,
169                                          hwc2_display_t *display) {
170   *display = (hwc2_display_t)HWC_DISPLAY_VIRTUAL;
171   virtual_display_.InitVirtualDisplay(device_.GetVirtualDisplay(), width,
172                                       height, disable_explicit_sync_);
173   if (*format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
174     // fallback to RGBA_8888, align with framework requirement
175     *format = HAL_PIXEL_FORMAT_RGBA_8888;
176   }
177
178   return HWC2::Error::None;
179 }
180
181 HWC2::Error IAHWC2::DestroyVirtualDisplay(hwc2_display_t display) {
182   if (display != (hwc2_display_t)HWC_DISPLAY_VIRTUAL) {
183     ALOGE("Not Virtual Display Type in DestroyVirtualDisplay");
184     return HWC2::Error::BadDisplay;
185   }
186
187   return HWC2::Error::None;
188 }
189
190 void IAHWC2::Dump(uint32_t *size, char *buffer) {
191   // TODO: Implement dump
192   unsupported(__func__, size, buffer);
193 }
194
195 uint32_t IAHWC2::GetMaxVirtualDisplayCount() {
196   return 1;
197 }
198
199 HWC2::Error IAHWC2::RegisterCallback(int32_t descriptor,
200                                      hwc2_callback_data_t data,
201                                      hwc2_function_pointer_t function) {
202   supported(__func__);
203   auto callback = static_cast<HWC2::Callback>(descriptor);
204   size_t size = extended_displays_.size();
205
206   switch (callback) {
207     case HWC2::Callback::Hotplug: {
208       primary_display_.RegisterHotPlugCallback(data, function);
209       for (size_t i = 0; i < size; ++i) {
210         IAHWC2::HwcDisplay *display = extended_displays_.at(i).get();
211         display->RegisterHotPlugCallback(data, function);
212         // TODO: Remove this when we want to add support for more than 2
213         // Displays.
214         break;
215       }
216
217       break;
218     }
219     case HWC2::Callback::Vsync: {
220       primary_display_.RegisterVsyncCallback(data, function);
221       for (size_t i = 0; i < size; ++i) {
222         IAHWC2::HwcDisplay *display = extended_displays_.at(i).get();
223         display->RegisterVsyncCallback(data, function);
224       }
225
226       break;
227     }
228     case HWC2::Callback::Refresh: {
229       primary_display_.RegisterRefreshCallback(data, function);
230       for (size_t i = 0; i < size; ++i) {
231         IAHWC2::HwcDisplay *display = extended_displays_.at(i).get();
232         display->RegisterRefreshCallback(data, function);
233       }
234
235       break;
236     }
237     default:
238       break;
239   }
240   return HWC2::Error::None;
241 }
242
243 IAHWC2::HwcDisplay::HwcDisplay() {
244   supported(__func__);
245 }
246
247 // This function will be called only for Virtual Display Init
248 HWC2::Error IAHWC2::HwcDisplay::InitVirtualDisplay(
249     hwcomposer::NativeDisplay *display, uint32_t width, uint32_t height,
250     bool disable_explicit_sync) {
251   supported(__func__);
252   display_ = display;
253   type_ = HWC2::DisplayType::Virtual;
254   handle_ = HWC_DISPLAY_VIRTUAL;
255   display_->InitVirtualDisplay(width, height);
256   disable_explicit_sync_ = disable_explicit_sync;
257   display_->SetExplicitSyncSupport(disable_explicit_sync_);
258   return HWC2::Error::None;
259 }
260
261 HWC2::Error IAHWC2::HwcDisplay::Init(hwcomposer::NativeDisplay *display,
262                                      int display_index,
263                                      bool disable_explicit_sync) {
264   supported(__func__);
265   display_ = display;
266   type_ = HWC2::DisplayType::Physical;
267   if (display_index == 0) {
268     handle_ = HWC_DISPLAY_PRIMARY;
269   } else {
270     handle_ = HWC_DISPLAY_EXTERNAL;
271   }
272
273   disable_explicit_sync_ = disable_explicit_sync;
274   display_->SetExplicitSyncSupport(disable_explicit_sync_);
275   if (!display_->IsConnected()) {
276     return HWC2::Error::None;
277   }
278   // Fetch the number of modes from the display
279   uint32_t num_configs;
280   HWC2::Error err = GetDisplayConfigs(&num_configs, NULL);
281   if (err != HWC2::Error::None || !num_configs)
282     return err;
283
284   // Grab the first mode, we'll choose this as the active mode
285   hwc2_config_t default_config;
286   num_configs = 1;
287   err = GetDisplayConfigs(&num_configs, &default_config);
288   if (err != HWC2::Error::None)
289     return err;
290
291   return SetActiveConfig(default_config);
292 }
293
294 HWC2::Error IAHWC2::HwcDisplay::RegisterVsyncCallback(
295     hwc2_callback_data_t data, hwc2_function_pointer_t func) {
296   supported(__func__);
297   auto callback = std::make_shared<IAVsyncCallback>(data, func);
298   int ret = display_->RegisterVsyncCallback(std::move(callback),
299                                             static_cast<int>(handle_));
300   if (ret) {
301     ALOGE("Failed to register callback d=%" PRIu64 " ret=%d", handle_, ret);
302     return HWC2::Error::BadDisplay;
303   }
304   return HWC2::Error::None;
305 }
306
307 HWC2::Error IAHWC2::HwcDisplay::RegisterRefreshCallback(
308     hwc2_callback_data_t data, hwc2_function_pointer_t func) {
309   supported(__func__);
310   auto callback = std::make_shared<IARefreshCallback>(data, func);
311   display_->RegisterRefreshCallback(std::move(callback),
312                                     static_cast<int>(handle_));
313   return HWC2::Error::None;
314 }
315
316 HWC2::Error IAHWC2::HwcDisplay::RegisterHotPlugCallback(
317     hwc2_callback_data_t data, hwc2_function_pointer_t func) {
318   supported(__func__);
319   auto callback = std::make_shared<IAHotPlugEventCallback>(data, func);
320   display_->RegisterHotPlugCallback(std::move(callback),
321                                     static_cast<int>(handle_));
322   return HWC2::Error::None;
323 }
324
325 HWC2::Error IAHWC2::HwcDisplay::AcceptDisplayChanges() {
326   supported(__func__);
327   if (!checkValidateDisplay) {
328     ALOGV("AcceptChanges failed, not validated");
329     return HWC2::Error::NotValidated;
330   }
331
332   for (std::pair<const hwc2_layer_t, IAHWC2::Hwc2Layer> &l : layers_)
333     l.second.accept_type_change();
334
335   // reset the value to false
336   checkValidateDisplay = false;
337   return HWC2::Error::None;
338 }
339
340 HWC2::Error IAHWC2::HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
341   supported(__func__);
342   layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), IAHWC2::Hwc2Layer());
343   *layer = static_cast<hwc2_layer_t>(layer_idx_);
344   ++layer_idx_;
345   return HWC2::Error::None;
346 }
347
348 HWC2::Error IAHWC2::HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
349   supported(__func__);
350   layers_.erase(layer);
351   return HWC2::Error::None;
352 }
353
354 HWC2::Error IAHWC2::HwcDisplay::GetActiveConfig(hwc2_config_t *config) {
355   supported(__func__);
356   if (!display_->GetActiveConfig(config))
357     return HWC2::Error::BadConfig;
358
359   return HWC2::Error::None;
360 }
361
362 HWC2::Error IAHWC2::HwcDisplay::GetChangedCompositionTypes(
363     uint32_t *num_elements, hwc2_layer_t *layers, int32_t *types) {
364   supported(__func__);
365   uint32_t num_changes = 0;
366   for (std::pair<const hwc2_layer_t, IAHWC2::Hwc2Layer> &l : layers_) {
367     if (l.second.type_changed()) {
368       if (layers && num_changes < *num_elements)
369         layers[num_changes] = l.first;
370       if (types && num_changes < *num_elements)
371         types[num_changes] = static_cast<int32_t>(l.second.validated_type());
372       ++num_changes;
373     }
374   }
375   if (!layers && !types)
376     *num_elements = num_changes;
377   return HWC2::Error::None;
378 }
379
380 HWC2::Error IAHWC2::HwcDisplay::GetClientTargetSupport(uint32_t width,
381                                                        uint32_t height,
382                                                        int32_t format,
383                                                        int32_t dataspace) {
384   if (width != display_->Width() || height != display_->Height()) {
385     return HWC2::Error::Unsupported;
386   }
387
388   if (format == HAL_PIXEL_FORMAT_RGBA_8888 &&
389       (dataspace == HAL_DATASPACE_UNKNOWN ||
390        dataspace == HAL_DATASPACE_STANDARD_UNSPECIFIED)) {
391     return HWC2::Error::None;
392   } else {
393     // Convert HAL to fourcc-based DRM formats
394     uint32_t drm_format = GetDrmFormatFromHALFormat(format);
395     if (display_->CheckPlaneFormat(drm_format) &&
396         (dataspace == HAL_DATASPACE_UNKNOWN ||
397          dataspace == HAL_DATASPACE_STANDARD_UNSPECIFIED))
398       return HWC2::Error::None;
399   }
400
401   return HWC2::Error::Unsupported;
402 }
403
404 HWC2::Error IAHWC2::HwcDisplay::GetColorModes(uint32_t *num_modes,
405                                               int32_t *modes) {
406   supported(__func__);
407   if (!modes)
408     *num_modes = 1;
409 #ifndef DISABLE_NATIVE_COLOR_MODES
410   if (modes)
411     *modes = HAL_COLOR_MODE_NATIVE;
412 #endif
413
414   return HWC2::Error::None;
415 }
416
417 HWC2::Error IAHWC2::HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
418                                                     int32_t attribute_in,
419                                                     int32_t *value) {
420   supported(__func__);
421   auto attribute = static_cast<HWC2::Attribute>(attribute_in);
422   switch (attribute) {
423     case HWC2::Attribute::Width:
424       display_->GetDisplayAttribute(
425           config, hwcomposer::HWCDisplayAttribute::kWidth, value);
426       break;
427     case HWC2::Attribute::Height:
428       display_->GetDisplayAttribute(
429           config, hwcomposer::HWCDisplayAttribute::kHeight, value);
430       break;
431     case HWC2::Attribute::VsyncPeriod:
432       // in nanoseconds
433       display_->GetDisplayAttribute(
434           config, hwcomposer::HWCDisplayAttribute::kRefreshRate, value);
435       break;
436     case HWC2::Attribute::DpiX:
437       // Dots per 1000 inches
438       display_->GetDisplayAttribute(
439           config, hwcomposer::HWCDisplayAttribute::kDpiX, value);
440       break;
441     case HWC2::Attribute::DpiY:
442       // Dots per 1000 inches
443       display_->GetDisplayAttribute(
444           config, hwcomposer::HWCDisplayAttribute::kDpiY, value);
445       break;
446     default:
447       *value = -1;
448       return HWC2::Error::BadConfig;
449   }
450   return HWC2::Error::None;
451 }
452
453 HWC2::Error IAHWC2::HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
454                                                   hwc2_config_t *configs) {
455   supported(__func__);
456   if (!display_->GetDisplayConfigs(num_configs, configs))
457     return HWC2::Error::BadDisplay;
458
459   return HWC2::Error::None;
460 }
461
462 HWC2::Error IAHWC2::HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
463   supported(__func__);
464   if (!display_->GetDisplayName(size, name))
465     return HWC2::Error::BadDisplay;
466
467   return HWC2::Error::None;
468 }
469
470 HWC2::Error IAHWC2::HwcDisplay::GetDisplayRequests(int32_t *display_requests,
471                                                    uint32_t *num_elements,
472                                                    hwc2_layer_t *layers,
473                                                    int32_t *layer_requests) {
474   supported(__func__);
475   // TODO: I think virtual display should request
476   //      HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
477   unsupported(__func__, display_requests, num_elements, layers, layer_requests);
478   *num_elements = 0;
479   return HWC2::Error::None;
480 }
481
482 HWC2::Error IAHWC2::HwcDisplay::GetDisplayType(int32_t *type) {
483   supported(__func__);
484   *type = static_cast<int32_t>(type_);
485   return HWC2::Error::None;
486 }
487
488 HWC2::Error IAHWC2::HwcDisplay::GetDozeSupport(int32_t *support) {
489   supported(__func__);
490   *support = true;
491   return HWC2::Error::None;
492 }
493
494 HWC2::Error IAHWC2::HwcDisplay::GetHdrCapabilities(
495     uint32_t *num_types, int32_t * /*types*/, float * /*max_luminance*/,
496     float * /*max_average_luminance*/, float * /*min_luminance*/) {
497   supported(__func__);
498   *num_types = 0;
499   return HWC2::Error::None;
500 }
501
502 HWC2::Error IAHWC2::HwcDisplay::GetReleaseFences(uint32_t *num_elements,
503                                                  hwc2_layer_t *layers,
504                                                  int32_t *fences) {
505   supported(__func__);
506   if (layers == NULL || fences == NULL) {
507     *num_elements = layers_.size();
508     return HWC2::Error::None;
509   }
510
511   uint32_t num_layers = 0;
512   for (std::pair<const hwc2_layer_t, IAHWC2::Hwc2Layer> &l : layers_) {
513     ++num_layers;
514     if (num_layers > *num_elements) {
515       ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
516       return HWC2::Error::None;
517     }
518
519     layers[num_layers - 1] = l.first;
520     fences[num_layers - 1] = l.second.GetLayer()->GetReleaseFence();
521   }
522
523   *num_elements = num_layers;
524   return HWC2::Error::None;
525 }
526
527 HWC2::Error IAHWC2::HwcDisplay::PresentDisplay(int32_t *retire_fence) {
528   supported(__func__);
529   // order the layers by z-order
530   bool use_client_layer = false;
531   uint32_t client_z_order = 0;
532   bool use_cursor_layer = false;
533   uint32_t cursor_z_order = 0;
534   IAHWC2::Hwc2Layer *cursor_layer;
535   *retire_fence = -1;
536   std::map<uint32_t, IAHWC2::Hwc2Layer *> z_map;
537
538   // if the power mode is doze suspend then its the hint that the drawing
539   // into the display has suspended and remain in the low power state and
540   // continue displaying the current state and stop applying display
541   // update from the client
542   if (display_->PowerMode() == HWC2_POWER_MODE_DOZE_SUSPEND)
543     return HWC2::Error::None;
544   for (std::pair<const hwc2_layer_t, IAHWC2::Hwc2Layer> &l : layers_) {
545     switch (l.second.validated_type()) {
546       case HWC2::Composition::Device:
547         z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
548         break;
549       case HWC2::Composition::Cursor:
550         use_cursor_layer = true;
551         cursor_layer = &l.second;
552         cursor_z_order = l.second.z_order();
553         break;
554       case HWC2::Composition::Client:
555         // Place it at the z_order of the highest client layer
556         use_client_layer = true;
557         client_z_order = std::max(client_z_order, l.second.z_order());
558         break;
559       default:
560         continue;
561     }
562   }
563   if (use_client_layer && client_layer_.GetLayer() &&
564       client_layer_.GetLayer()->GetNativeHandle() &&
565       client_layer_.GetLayer()->GetNativeHandle()->handle_) {
566     z_map.emplace(std::make_pair(client_z_order, &client_layer_));
567   }
568
569   // Place the cursor at the highest z-order
570   if (use_cursor_layer) {
571     if (z_map.size()) {
572       if (z_map.rbegin()->second->z_order() > cursor_z_order)
573         cursor_z_order = (z_map.rbegin()->second->z_order()) + 1;
574     }
575     z_map.emplace(std::make_pair(cursor_z_order, cursor_layer));
576   }
577
578   std::vector<hwcomposer::HwcLayer *> layers;
579   // now that they're ordered by z, add them to the composition
580   for (std::pair<const uint32_t, IAHWC2::Hwc2Layer *> &l : z_map) {
581     layers.emplace_back(l.second->GetLayer());
582   }
583
584   if (layers.empty())
585     return HWC2::Error::None;
586
587   bool success = display_->Present(layers, retire_fence);
588   if (!success) {
589     ALOGE("Failed to set layers in the composition");
590     return HWC2::Error::BadLayer;
591   }
592
593   ++frame_no_;
594
595   return HWC2::Error::None;
596 }
597
598 HWC2::Error IAHWC2::HwcDisplay::SetActiveConfig(hwc2_config_t config) {
599   supported(__func__);
600   if (!display_->SetActiveConfig(config)) {
601     ALOGE("Could not find active mode for %d", config);
602     return HWC2::Error::BadConfig;
603   }
604
605   // Setup the client layer's dimensions
606   hwc_rect_t display_frame = {.left = 0,
607                               .top = 0,
608                               .right = static_cast<int>(display_->Width()),
609                               .bottom = static_cast<int>(display_->Height())};
610   client_layer_.SetLayerDisplayFrame(display_frame);
611   hwc_frect_t source_crop = {.left = 0.0f,
612                              .top = 0.0f,
613                              .right = display_->Width() + 0.0f,
614                              .bottom = display_->Height() + 0.0f};
615   client_layer_.SetLayerSourceCrop(source_crop);
616
617   return HWC2::Error::None;
618 }
619
620 HWC2::Error IAHWC2::HwcDisplay::SetClientTarget(buffer_handle_t target,
621                                                 int32_t acquire_fence,
622                                                 int32_t dataspace,
623                                                 hwc_region_t damage) {
624   supported(__func__);
625   client_layer_.set_buffer(target);
626   client_layer_.set_acquire_fence(acquire_fence);
627   client_layer_.SetLayerDataspace(dataspace);
628   client_layer_.SetLayerSurfaceDamage(damage);
629
630   return HWC2::Error::None;
631 }
632
633 HWC2::Error IAHWC2::HwcDisplay::SetColorMode(int32_t mode) {
634   supported(__func__);
635   color_mode_ = mode;
636   return HWC2::Error::None;
637 }
638
639 HWC2::Error IAHWC2::HwcDisplay::SetColorTransform(const float *matrix,
640                                                   int32_t hint) {
641   supported(__func__);
642   // TODO: Force client composition if we get this
643   return unsupported(__func__, matrix, hint);
644 }
645
646 HWC2::Error IAHWC2::HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
647                                                 int32_t release_fence) {
648   supported(__func__);
649
650   struct gralloc_handle *temp = new struct gralloc_handle();
651   temp->handle_ = buffer;
652   display_->SetOutputBuffer(temp, release_fence);
653   return HWC2::Error::None;
654 }
655
656 HWC2::Error IAHWC2::HwcDisplay::SetPowerMode(int32_t mode_in) {
657   supported(__func__);
658   uint32_t power_mode = 0;
659   auto mode = static_cast<HWC2::PowerMode>(mode_in);
660   switch (mode) {
661     case HWC2::PowerMode::Off:
662       power_mode = HWC2_POWER_MODE_OFF;
663       break;
664     case HWC2::PowerMode::Doze:
665       power_mode = HWC2_POWER_MODE_DOZE;
666       break;
667     case HWC2::PowerMode::DozeSuspend:
668       power_mode = HWC2_POWER_MODE_DOZE_SUSPEND;
669       break;
670     case HWC2::PowerMode::On:
671       power_mode = HWC2_POWER_MODE_ON;
672       break;
673     default:
674       ALOGI("Power mode %d is unsupported\n", mode);
675       return HWC2::Error::BadParameter;
676   };
677
678   display_->SetPowerMode(power_mode);
679
680   return HWC2::Error::None;
681 }
682
683 HWC2::Error IAHWC2::HwcDisplay::SetVsyncEnabled(int32_t enabled) {
684   supported(__func__);
685   display_->VSyncControl(enabled);
686   return HWC2::Error::None;
687 }
688
689 HWC2::Error IAHWC2::HwcDisplay::ValidateDisplay(uint32_t *num_types,
690                                                 uint32_t *num_requests) {
691   supported(__func__);
692   *num_types = 0;
693   *num_requests = 0;
694   for (std::pair<const hwc2_layer_t, IAHWC2::Hwc2Layer> &l : layers_) {
695     IAHWC2::Hwc2Layer &layer = l.second;
696     switch (layer.sf_type()) {
697       case HWC2::Composition::Sideband:
698         layer.set_validated_type(HWC2::Composition::Client);
699         ++*num_types;
700         break;
701       case HWC2::Composition::Cursor:
702         layer.set_validated_type(HWC2::Composition::Device);
703         ++*num_types;
704         break;
705       default:
706         if (disable_explicit_sync_ ||
707             display_->PowerMode() == HWC2_POWER_MODE_DOZE_SUSPEND) {
708           layer.set_validated_type(HWC2::Composition::Client);
709         } else {
710           layer.set_validated_type(layer.sf_type());
711         }
712         break;
713     }
714   }
715
716   checkValidateDisplay = true;
717   return HWC2::Error::None;
718 }
719
720 HWC2::Error IAHWC2::Hwc2Layer::SetCursorPosition(int32_t x, int32_t y) {
721   supported(__func__);
722   cursor_x_ = x;
723   cursor_y_ = y;
724   return HWC2::Error::None;
725 }
726
727 HWC2::Error IAHWC2::Hwc2Layer::SetLayerBlendMode(int32_t mode) {
728   supported(__func__);
729   switch (static_cast<HWC2::BlendMode>(mode)) {
730     case HWC2::BlendMode::None:
731       hwc_layer_.SetBlending(hwcomposer::HWCBlending::kBlendingNone);
732       break;
733     case HWC2::BlendMode::Premultiplied:
734       hwc_layer_.SetBlending(hwcomposer::HWCBlending::kBlendingPremult);
735       break;
736     case HWC2::BlendMode::Coverage:
737       hwc_layer_.SetBlending(hwcomposer::HWCBlending::kBlendingCoverage);
738       break;
739     default:
740       ALOGE("Unknown blending mode b=%d", mode);
741       hwc_layer_.SetBlending(hwcomposer::HWCBlending::kBlendingNone);
742       break;
743   }
744   return HWC2::Error::None;
745 }
746
747 HWC2::Error IAHWC2::Hwc2Layer::SetLayerBuffer(buffer_handle_t buffer,
748                                               int32_t acquire_fence) {
749   supported(__func__);
750
751   // The buffer and acquire_fence are handled elsewhere
752   if (sf_type_ == HWC2::Composition::Client ||
753       sf_type_ == HWC2::Composition::Sideband)
754     return HWC2::Error::None;
755
756   native_handle_.handle_ = buffer;
757   hwc_layer_.SetNativeHandle(&native_handle_);
758   if (acquire_fence > 0)
759     hwc_layer_.SetAcquireFence(dup(acquire_fence));
760   return HWC2::Error::None;
761 }
762
763 HWC2::Error IAHWC2::Hwc2Layer::SetLayerColor(hwc_color_t color) {
764   // We only support Opaque colors so far.
765   if (color.r == 0 && color.g == 0 && color.b == 0 && color.a == 255) {
766     sf_type_ = HWC2::Composition::SolidColor;
767     return HWC2::Error::None;
768   }
769
770   sf_type_ = HWC2::Composition::Client;
771   return HWC2::Error::None;
772 }
773
774 HWC2::Error IAHWC2::Hwc2Layer::SetLayerCompositionType(int32_t type) {
775   sf_type_ = static_cast<HWC2::Composition>(type);
776   return HWC2::Error::None;
777 }
778
779 HWC2::Error IAHWC2::Hwc2Layer::SetLayerDataspace(int32_t dataspace) {
780   supported(__func__);
781   dataspace_ = static_cast<android_dataspace_t>(dataspace);
782   return HWC2::Error::None;
783 }
784
785 HWC2::Error IAHWC2::Hwc2Layer::SetLayerDisplayFrame(hwc_rect_t frame) {
786   supported(__func__);
787   hwc_layer_.SetDisplayFrame(hwcomposer::HwcRect<int>(
788       frame.left, frame.top, frame.right, frame.bottom));
789   return HWC2::Error::None;
790 }
791
792 HWC2::Error IAHWC2::Hwc2Layer::SetLayerPlaneAlpha(float alpha) {
793   supported(__func__);
794   hwc_layer_.SetAlpha(static_cast<uint8_t>(255.0f * alpha + 0.5f));
795   return HWC2::Error::None;
796 }
797
798 HWC2::Error IAHWC2::Hwc2Layer::SetLayerSidebandStream(
799     const native_handle_t *stream) {
800   supported(__func__);
801   // TODO: We don't support sideband
802   return unsupported(__func__, stream);
803 }
804
805 HWC2::Error IAHWC2::Hwc2Layer::SetLayerSourceCrop(hwc_frect_t crop) {
806   supported(__func__);
807   hwc_layer_.SetSourceCrop(
808       hwcomposer::HwcRect<float>(crop.left, crop.top, crop.right, crop.bottom));
809   return HWC2::Error::None;
810 }
811
812 HWC2::Error IAHWC2::Hwc2Layer::SetLayerSurfaceDamage(hwc_region_t damage) {
813   uint32_t num_rects = damage.numRects;
814   hwcomposer::HwcRegion hwc_region;
815
816   for (size_t rect = 0; rect < num_rects; ++rect) {
817     hwc_region.emplace_back(damage.rects[rect].left, damage.rects[rect].top,
818                             damage.rects[rect].right,
819                             damage.rects[rect].bottom);
820   }
821
822   hwc_layer_.SetSurfaceDamage(hwc_region);
823
824   return HWC2::Error::None;
825 }
826
827 HWC2::Error IAHWC2::Hwc2Layer::SetLayerTransform(int32_t transform) {
828   supported(__func__);
829   // 270* and 180* cannot be combined with flips. More specifically, they
830   // already contain both horizontal and vertical flips, so those fields are
831   // redundant in this case. 90* rotation can be combined with either horizontal
832   // flip or vertical flip, so treat it differently
833   if (transform == HWC_TRANSFORM_ROT_270) {
834     transform = hwcomposer::HWCTransform::kRotate270;
835   } else if (transform == HWC_TRANSFORM_ROT_180) {
836     transform = hwcomposer::HWCTransform::kRotate180;
837   } else {
838     if (transform & HWC_TRANSFORM_FLIP_H)
839       transform |= hwcomposer::HWCTransform::kReflectX;
840     if (transform & HWC_TRANSFORM_FLIP_V)
841       transform |= hwcomposer::HWCTransform::kReflectY;
842     if (transform & HWC_TRANSFORM_ROT_90)
843       transform |= hwcomposer::HWCTransform::kRotate90;
844   }
845   hwc_layer_.SetTransform(transform);
846   return HWC2::Error::None;
847 }
848
849 HWC2::Error IAHWC2::Hwc2Layer::SetLayerVisibleRegion(hwc_region_t visible) {
850   uint32_t num_rects = visible.numRects;
851   hwcomposer::HwcRegion hwc_region;
852
853   for (size_t rect = 0; rect < num_rects; ++rect) {
854     hwc_region.emplace_back(visible.rects[rect].left, visible.rects[rect].top,
855                             visible.rects[rect].right,
856                             visible.rects[rect].bottom);
857   }
858
859   hwc_layer_.SetVisibleRegion(hwc_region);
860   return HWC2::Error::None;
861 }
862
863 HWC2::Error IAHWC2::Hwc2Layer::SetLayerZOrder(uint32_t order) {
864   supported(__func__);
865
866   hwc_layer_.SetLayerZOrder(order);
867   return HWC2::Error::None;
868 }
869
870 // static
871 int IAHWC2::HookDevClose(hw_device_t * /*dev*/) {
872   unsupported(__func__);
873   return 0;
874 }
875
876 // static
877 void IAHWC2::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
878                                     uint32_t *out_count,
879                                     int32_t * /*out_capabilities*/) {
880   supported(__func__);
881   *out_count = 0;
882 }
883
884 // static
885 hwc2_function_pointer_t IAHWC2::HookDevGetFunction(struct hwc2_device * /*dev*/,
886                                                    int32_t descriptor) {
887   supported(__func__);
888   auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
889   switch (func) {
890     // Device functions
891     case HWC2::FunctionDescriptor::CreateVirtualDisplay:
892       return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
893           DeviceHook<int32_t, decltype(&IAHWC2::CreateVirtualDisplay),
894                      &IAHWC2::CreateVirtualDisplay, uint32_t, uint32_t,
895                      int32_t *, hwc2_display_t *>);
896     case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
897       return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
898           DeviceHook<int32_t, decltype(&IAHWC2::DestroyVirtualDisplay),
899                      &IAHWC2::DestroyVirtualDisplay, hwc2_display_t>);
900     case HWC2::FunctionDescriptor::Dump:
901       return ToHook<HWC2_PFN_DUMP>(DeviceHook<
902           void, decltype(&IAHWC2::Dump), &IAHWC2::Dump, uint32_t *, char *>);
903     case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
904       return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
905           DeviceHook<uint32_t, decltype(&IAHWC2::GetMaxVirtualDisplayCount),
906                      &IAHWC2::GetMaxVirtualDisplayCount>);
907     case HWC2::FunctionDescriptor::RegisterCallback:
908       return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
909           DeviceHook<int32_t, decltype(&IAHWC2::RegisterCallback),
910                      &IAHWC2::RegisterCallback, int32_t, hwc2_callback_data_t,
911                      hwc2_function_pointer_t>);
912
913     // Display functions
914     case HWC2::FunctionDescriptor::AcceptDisplayChanges:
915       return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
916           DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
917                       &HwcDisplay::AcceptDisplayChanges>);
918     case HWC2::FunctionDescriptor::CreateLayer:
919       return ToHook<HWC2_PFN_CREATE_LAYER>(
920           DisplayHook<decltype(&HwcDisplay::CreateLayer),
921                       &HwcDisplay::CreateLayer, hwc2_layer_t *>);
922     case HWC2::FunctionDescriptor::DestroyLayer:
923       return ToHook<HWC2_PFN_DESTROY_LAYER>(
924           DisplayHook<decltype(&HwcDisplay::DestroyLayer),
925                       &HwcDisplay::DestroyLayer, hwc2_layer_t>);
926     case HWC2::FunctionDescriptor::GetActiveConfig:
927       return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
928           DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
929                       &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
930     case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
931       return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
932           DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
933                       &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
934                       hwc2_layer_t *, int32_t *>);
935     case HWC2::FunctionDescriptor::GetClientTargetSupport:
936       return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
937           DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
938                       &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
939                       int32_t, int32_t>);
940     case HWC2::FunctionDescriptor::GetColorModes:
941       return ToHook<HWC2_PFN_GET_COLOR_MODES>(
942           DisplayHook<decltype(&HwcDisplay::GetColorModes),
943                       &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
944     case HWC2::FunctionDescriptor::GetDisplayAttribute:
945       return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(DisplayHook<
946           decltype(&HwcDisplay::GetDisplayAttribute),
947           &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t, int32_t *>);
948     case HWC2::FunctionDescriptor::GetDisplayConfigs:
949       return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(DisplayHook<
950           decltype(&HwcDisplay::GetDisplayConfigs),
951           &HwcDisplay::GetDisplayConfigs, uint32_t *, hwc2_config_t *>);
952     case HWC2::FunctionDescriptor::GetDisplayName:
953       return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
954           DisplayHook<decltype(&HwcDisplay::GetDisplayName),
955                       &HwcDisplay::GetDisplayName, uint32_t *, char *>);
956     case HWC2::FunctionDescriptor::GetDisplayRequests:
957       return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
958           DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
959                       &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
960                       hwc2_layer_t *, int32_t *>);
961     case HWC2::FunctionDescriptor::GetDisplayType:
962       return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
963           DisplayHook<decltype(&HwcDisplay::GetDisplayType),
964                       &HwcDisplay::GetDisplayType, int32_t *>);
965     case HWC2::FunctionDescriptor::GetDozeSupport:
966       return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
967           DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
968                       &HwcDisplay::GetDozeSupport, int32_t *>);
969     case HWC2::FunctionDescriptor::GetHdrCapabilities:
970       return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
971           DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
972                       &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
973                       float *, float *, float *>);
974     case HWC2::FunctionDescriptor::GetReleaseFences:
975       return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
976           DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
977                       &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
978                       int32_t *>);
979     case HWC2::FunctionDescriptor::PresentDisplay:
980       return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
981           DisplayHook<decltype(&HwcDisplay::PresentDisplay),
982                       &HwcDisplay::PresentDisplay, int32_t *>);
983     case HWC2::FunctionDescriptor::SetActiveConfig:
984       return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
985           DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
986                       &HwcDisplay::SetActiveConfig, hwc2_config_t>);
987     case HWC2::FunctionDescriptor::SetClientTarget:
988       return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(DisplayHook<
989           decltype(&HwcDisplay::SetClientTarget), &HwcDisplay::SetClientTarget,
990           buffer_handle_t, int32_t, int32_t, hwc_region_t>);
991     case HWC2::FunctionDescriptor::SetColorMode:
992       return ToHook<HWC2_PFN_SET_COLOR_MODE>(
993           DisplayHook<decltype(&HwcDisplay::SetColorMode),
994                       &HwcDisplay::SetColorMode, int32_t>);
995     case HWC2::FunctionDescriptor::SetColorTransform:
996       return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
997           DisplayHook<decltype(&HwcDisplay::SetColorTransform),
998                       &HwcDisplay::SetColorTransform, const float *, int32_t>);
999     case HWC2::FunctionDescriptor::SetOutputBuffer:
1000       return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
1001           DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
1002                       &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
1003     case HWC2::FunctionDescriptor::SetPowerMode:
1004       return ToHook<HWC2_PFN_SET_POWER_MODE>(
1005           DisplayHook<decltype(&HwcDisplay::SetPowerMode),
1006                       &HwcDisplay::SetPowerMode, int32_t>);
1007     case HWC2::FunctionDescriptor::SetVsyncEnabled:
1008       return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
1009           DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
1010                       &HwcDisplay::SetVsyncEnabled, int32_t>);
1011     case HWC2::FunctionDescriptor::ValidateDisplay:
1012       return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
1013           DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
1014                       &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
1015
1016     // Layer functions
1017     case HWC2::FunctionDescriptor::SetCursorPosition:
1018       return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
1019           LayerHook<decltype(&Hwc2Layer::SetCursorPosition),
1020                     &Hwc2Layer::SetCursorPosition, int32_t, int32_t>);
1021     case HWC2::FunctionDescriptor::SetLayerBlendMode:
1022       return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
1023           LayerHook<decltype(&Hwc2Layer::SetLayerBlendMode),
1024                     &Hwc2Layer::SetLayerBlendMode, int32_t>);
1025     case HWC2::FunctionDescriptor::SetLayerBuffer:
1026       return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
1027           LayerHook<decltype(&Hwc2Layer::SetLayerBuffer),
1028                     &Hwc2Layer::SetLayerBuffer, buffer_handle_t, int32_t>);
1029     case HWC2::FunctionDescriptor::SetLayerColor:
1030       return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
1031           LayerHook<decltype(&Hwc2Layer::SetLayerColor),
1032                     &Hwc2Layer::SetLayerColor, hwc_color_t>);
1033     case HWC2::FunctionDescriptor::SetLayerCompositionType:
1034       return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
1035           LayerHook<decltype(&Hwc2Layer::SetLayerCompositionType),
1036                     &Hwc2Layer::SetLayerCompositionType, int32_t>);
1037     case HWC2::FunctionDescriptor::SetLayerDataspace:
1038       return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
1039           LayerHook<decltype(&Hwc2Layer::SetLayerDataspace),
1040                     &Hwc2Layer::SetLayerDataspace, int32_t>);
1041     case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
1042       return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
1043           LayerHook<decltype(&Hwc2Layer::SetLayerDisplayFrame),
1044                     &Hwc2Layer::SetLayerDisplayFrame, hwc_rect_t>);
1045     case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1046       return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1047           LayerHook<decltype(&Hwc2Layer::SetLayerPlaneAlpha),
1048                     &Hwc2Layer::SetLayerPlaneAlpha, float>);
1049     case HWC2::FunctionDescriptor::SetLayerSidebandStream:
1050       return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(LayerHook<
1051           decltype(&Hwc2Layer::SetLayerSidebandStream),
1052           &Hwc2Layer::SetLayerSidebandStream, const native_handle_t *>);
1053     case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1054       return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1055           LayerHook<decltype(&Hwc2Layer::SetLayerSourceCrop),
1056                     &Hwc2Layer::SetLayerSourceCrop, hwc_frect_t>);
1057     case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1058       return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1059           LayerHook<decltype(&Hwc2Layer::SetLayerSurfaceDamage),
1060                     &Hwc2Layer::SetLayerSurfaceDamage, hwc_region_t>);
1061     case HWC2::FunctionDescriptor::SetLayerTransform:
1062       return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1063           LayerHook<decltype(&Hwc2Layer::SetLayerTransform),
1064                     &Hwc2Layer::SetLayerTransform, int32_t>);
1065     case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1066       return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1067           LayerHook<decltype(&Hwc2Layer::SetLayerVisibleRegion),
1068                     &Hwc2Layer::SetLayerVisibleRegion, hwc_region_t>);
1069     case HWC2::FunctionDescriptor::SetLayerZOrder:
1070       return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1071           LayerHook<decltype(&Hwc2Layer::SetLayerZOrder),
1072                     &Hwc2Layer::SetLayerZOrder, uint32_t>);
1073     case HWC2::FunctionDescriptor::Invalid:
1074     default:
1075       return NULL;
1076   }
1077 }
1078
1079 // static
1080 int IAHWC2::HookDevOpen(const struct hw_module_t *module, const char *name,
1081                         struct hw_device_t **dev) {
1082   supported(__func__);
1083   if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
1084     ALOGE("Invalid module name- %s", name);
1085     return -EINVAL;
1086   }
1087
1088   std::unique_ptr<IAHWC2> ctx(new IAHWC2());
1089   if (!ctx) {
1090     ALOGE("Failed to allocate IAHWC2");
1091     return -ENOMEM;
1092   }
1093
1094   HWC2::Error err = ctx->Init();
1095   if (err != HWC2::Error::None) {
1096     ALOGE("Failed to initialize IAHWC2 err=%d\n", err);
1097     return -EINVAL;
1098   }
1099
1100   ctx->common.module = const_cast<hw_module_t *>(module);
1101   *dev = &ctx->common;
1102   ctx.release();
1103   return 0;
1104 }
1105
1106 hwcomposer::NativeDisplay *IAHWC2::GetExtendedDisplay(uint32_t dispIndex) {
1107   return extended_displays_.at(dispIndex)->GetDisplay();
1108 }
1109
1110 hwcomposer::NativeDisplay *IAHWC2::GetPrimaryDisplay() {
1111   return primary_display_.GetDisplay();
1112 }
1113
1114 hwcomposer::NativeDisplay *IAHWC2::HwcDisplay::GetDisplay() {
1115   return display_;
1116 }
1117
1118 }  // namespace android
1119
1120 static struct hw_module_methods_t hwc2_module_methods = {
1121     .open = android::IAHWC2::HookDevOpen,
1122 };
1123
1124 hw_module_t HAL_MODULE_INFO_SYM = {
1125     .tag = HARDWARE_MODULE_TAG,
1126     .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1127     .id = HWC_HARDWARE_MODULE_ID,
1128     .name = "IA-Hardware-Composer",
1129     .author = "The Android Open Source Project",
1130     .methods = &hwc2_module_methods,
1131     .dso = NULL,
1132     .reserved = {0},
1133 };