OSDN Git Service

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