OSDN Git Service

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