OSDN Git Service

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