OSDN Git Service

8a57f40cf4238c28a07d2dfbd932239774347367
[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     if ((l.second.validated_type() != HWC2::Composition::SolidColor) &&
622         (l.second.GetLayer()->GetNativeHandle() == NULL)) {
623       ETRACE(
624           "HWC don't support layer without buffer if not in type SolidColor");
625       continue;
626     }
627
628     switch (l.second.validated_type()) {
629       case HWC2::Composition::Device:
630       case HWC2::Composition::SolidColor:
631         z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
632         break;
633       case HWC2::Composition::Client:
634         // Place it at the z_order of the highest client layer
635         use_client_layer = true;
636         client_z_order = std::max(client_z_order, l.second.z_order());
637         break;
638       default:
639         continue;
640     }
641   }
642   if (use_client_layer && client_layer_.GetLayer() &&
643       client_layer_.GetLayer()->GetNativeHandle() &&
644       client_layer_.GetLayer()->GetNativeHandle()->handle_) {
645     z_map.emplace(std::make_pair(client_z_order, &client_layer_));
646   }
647
648   // Place the cursor at the highest z-order
649   if (use_cursor_layer) {
650     if (z_map.size()) {
651       if (z_map.rbegin()->second->z_order() > cursor_z_order)
652         cursor_z_order = z_map.rbegin()->second->z_order() + 1;
653       else if (client_z_order > cursor_z_order)
654         cursor_z_order = client_z_order + 1;
655     }
656     z_map.emplace(std::make_pair(cursor_z_order, cursor_layer));
657   }
658
659   std::vector<hwcomposer::HwcLayer *> layers;
660   // now that they're ordered by z, add them to the composition
661   for (std::pair<const uint32_t, IAHWC2::Hwc2Layer *> &l : z_map) {
662     layers.emplace_back(l.second->GetLayer());
663   }
664
665   if (layers.empty())
666     return HWC2::Error::None;
667
668   IHOTPLUGEVENTTRACE("PhysicalDisplay called for Display: %p \n", display_);
669
670   bool success = display_->Present(layers, retire_fence);
671   if (!success) {
672     ALOGE("Failed to set layers in the composition");
673     return HWC2::Error::BadLayer;
674   }
675
676   ++frame_no_;
677
678   return HWC2::Error::None;
679 }
680
681 HWC2::Error IAHWC2::HwcDisplay::SetActiveConfig(hwc2_config_t config) {
682   supported(__func__);
683   if (!display_->SetActiveConfig(config)) {
684     ALOGE("Could not find active mode for %d", config);
685     return HWC2::Error::BadConfig;
686   }
687
688   // Setup the client layer's dimensions
689   hwc_rect_t display_frame = {.left = 0,
690                               .top = 0,
691                               .right = static_cast<int>(display_->Width()),
692                               .bottom = static_cast<int>(display_->Height())};
693   client_layer_.SetLayerDisplayFrame(display_frame);
694   hwc_frect_t source_crop = {.left = 0.0f,
695                              .top = 0.0f,
696                              .right = display_->Width() + 0.0f,
697                              .bottom = display_->Height() + 0.0f};
698   client_layer_.SetLayerSourceCrop(source_crop);
699
700   return HWC2::Error::None;
701 }
702
703 HWC2::Error IAHWC2::HwcDisplay::SetClientTarget(buffer_handle_t target,
704                                                 int32_t acquire_fence,
705                                                 int32_t dataspace,
706                                                 hwc_region_t damage) {
707   supported(__func__);
708   client_layer_.set_buffer(target);
709   client_layer_.set_acquire_fence(acquire_fence);
710   client_layer_.SetLayerDataspace(dataspace);
711   client_layer_.SetLayerSurfaceDamage(damage);
712
713   return HWC2::Error::None;
714 }
715
716 HWC2::Error IAHWC2::HwcDisplay::SetColorMode(int32_t mode) {
717   supported(__func__);
718   // TODO: Use the parameter mode to set the color mode for the display to be
719   // used.
720
721   return HWC2::Error::None;
722 }
723
724 HWC2::Error IAHWC2::HwcDisplay::SetColorTransform(const float *matrix,
725                                                   int32_t hint) {
726   supported(__func__);
727   // TODO: Force client composition if we get this
728
729   if (hint != HAL_COLOR_TRANSFORM_IDENTITY &&
730       hint != HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX &&
731       hint != HAL_COLOR_TRANSFORM_VALUE_INVERSE &&
732       hint != HAL_COLOR_TRANSFORM_GRAYSCALE &&
733       hint != HAL_COLOR_TRANSFORM_CORRECT_PROTANOPIA &&
734       hint != HAL_COLOR_TRANSFORM_CORRECT_DEUTERANOPIA &&
735       hint != HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
736     return HWC2::Error::BadParameter;
737
738   display_->SetColorTransform(matrix, (HWCColorTransform)hint);
739
740   return HWC2::Error::None;
741 }
742
743 HWC2::Error IAHWC2::HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
744                                                 int32_t release_fence) {
745   supported(__func__);
746
747   struct gralloc_handle *temp = new struct gralloc_handle();
748   temp->handle_ = buffer;
749   display_->SetOutputBuffer(temp, release_fence);
750   return HWC2::Error::None;
751 }
752
753 HWC2::Error IAHWC2::HwcDisplay::SetPowerMode(int32_t mode_in) {
754   supported(__func__);
755   uint32_t power_mode = 0;
756   auto mode = static_cast<HWC2::PowerMode>(mode_in);
757   switch (mode) {
758     case HWC2::PowerMode::Off:
759       power_mode = HWC2_POWER_MODE_OFF;
760       break;
761     case HWC2::PowerMode::Doze:
762       power_mode = HWC2_POWER_MODE_DOZE;
763       break;
764     case HWC2::PowerMode::DozeSuspend:
765       power_mode = HWC2_POWER_MODE_DOZE_SUSPEND;
766       break;
767     case HWC2::PowerMode::On:
768       power_mode = HWC2_POWER_MODE_ON;
769       break;
770     default:
771       ALOGI("Power mode %d is unsupported\n", mode);
772       return HWC2::Error::BadParameter;
773   };
774
775   display_->SetPowerMode(power_mode);
776
777   return HWC2::Error::None;
778 }
779
780 HWC2::Error IAHWC2::HwcDisplay::SetVsyncEnabled(int32_t enabled) {
781   supported(__func__);
782   switch (enabled) {
783     case HWC2_VSYNC_ENABLE:
784       display_->VSyncControl(true);
785       break;
786     case HWC2_VSYNC_DISABLE:
787       display_->VSyncControl(false);
788       break;
789     default:
790       ALOGE("SetVsyncEnabled called with invalid parameter");
791       return HWC2::Error::BadParameter;
792   }
793   return HWC2::Error::None;
794 }
795
796 HWC2::Error IAHWC2::HwcDisplay::ValidateDisplay(uint32_t *num_types,
797                                                 uint32_t *num_requests) {
798   supported(__func__);
799   *num_types = 0;
800   *num_requests = 0;
801   for (std::pair<const hwc2_layer_t, IAHWC2::Hwc2Layer> &l : layers_) {
802     IAHWC2::Hwc2Layer &layer = l.second;
803
804     switch (layer.sf_type()) {
805       case HWC2::Composition::Sideband:
806         layer.set_validated_type(HWC2::Composition::Client);
807         ++*num_types;
808         break;
809       case HWC2::Composition::Cursor:
810         layer.set_validated_type(HWC2::Composition::Device);
811         ++*num_types;
812         break;
813       default:
814         if (disable_explicit_sync_ ||
815             display_->PowerMode() == HWC2_POWER_MODE_DOZE_SUSPEND) {
816           layer.set_validated_type(HWC2::Composition::Client);
817         } else {
818           layer.set_validated_type(layer.sf_type());
819         }
820         break;
821     }
822   }
823
824   check_validate_display_ = true;
825   return HWC2::Error::None;
826 }
827
828 HWC2::Error IAHWC2::Hwc2Layer::SetCursorPosition(int32_t /*x*/, int32_t /*y*/) {
829   supported(__func__);
830   return HWC2::Error::None;
831 }
832
833 HWC2::Error IAHWC2::Hwc2Layer::SetLayerBlendMode(int32_t mode) {
834   supported(__func__);
835   switch (static_cast<HWC2::BlendMode>(mode)) {
836     case HWC2::BlendMode::None:
837       hwc_layer_.SetBlending(hwcomposer::HWCBlending::kBlendingNone);
838       break;
839     case HWC2::BlendMode::Premultiplied:
840       hwc_layer_.SetBlending(hwcomposer::HWCBlending::kBlendingPremult);
841       break;
842     case HWC2::BlendMode::Coverage:
843       hwc_layer_.SetBlending(hwcomposer::HWCBlending::kBlendingCoverage);
844       break;
845     default:
846       ALOGE("Unknown blending mode b=%d", mode);
847       hwc_layer_.SetBlending(hwcomposer::HWCBlending::kBlendingNone);
848       break;
849   }
850   return HWC2::Error::None;
851 }
852
853 HWC2::Error IAHWC2::Hwc2Layer::SetLayerBuffer(buffer_handle_t buffer,
854                                               int32_t acquire_fence) {
855   supported(__func__);
856
857   // The buffer and acquire_fence are handled elsewhere
858   if (sf_type_ == HWC2::Composition::Client ||
859       sf_type_ == HWC2::Composition::Sideband)
860     return HWC2::Error::None;
861
862   native_handle_.handle_ = buffer;
863   hwc_layer_.SetNativeHandle(&native_handle_);
864   if (acquire_fence > 0)
865     hwc_layer_.SetAcquireFence(acquire_fence);
866   return HWC2::Error::None;
867 }
868
869 HWC2::Error IAHWC2::Hwc2Layer::SetLayerColor(hwc_color_t color) {
870   if (sf_type_ == HWC2::Composition::SolidColor) {
871     hwc_layer_.SetLayerCompositionType(hwcomposer::Composition_SolidColor);
872     uint32_t hwc_layer_color = (uint32_t)color.r << 24 |
873                                (uint32_t)color.g << 16 |
874                                (uint32_t)color.b << 8 | (uint32_t)color.a;
875     hwc_layer_.SetSolidColor(hwc_layer_color);
876   } else {
877     sf_type_ = HWC2::Composition::Client;
878   }
879   return HWC2::Error::None;
880 }
881
882 HWC2::Error IAHWC2::Hwc2Layer::SetLayerCompositionType(int32_t type) {
883   sf_type_ = static_cast<HWC2::Composition>(type);
884   if (sf_type_ == HWC2::Composition::Cursor) {
885     hwc_layer_.MarkAsCursorLayer();
886   }
887
888   return HWC2::Error::None;
889 }
890
891 HWC2::Error IAHWC2::Hwc2Layer::SetLayerDataspace(int32_t dataspace) {
892   supported(__func__);
893   dataspace_ = static_cast<android_dataspace_t>(dataspace);
894   return HWC2::Error::None;
895 }
896
897 HWC2::Error IAHWC2::Hwc2Layer::SetLayerDisplayFrame(hwc_rect_t frame) {
898   supported(__func__);
899   hwc_layer_.SetDisplayFrame(
900       hwcomposer::HwcRect<int>(frame.left, frame.top, frame.right,
901                                frame.bottom),
902       x_translation_, y_translation_);
903   return HWC2::Error::None;
904 }
905
906 HWC2::Error IAHWC2::Hwc2Layer::SetLayerPlaneAlpha(float alpha) {
907   supported(__func__);
908   hwc_layer_.SetAlpha(static_cast<uint8_t>(255.0f * alpha + 0.5f));
909   return HWC2::Error::None;
910 }
911
912 HWC2::Error IAHWC2::Hwc2Layer::SetLayerSidebandStream(
913     const native_handle_t *stream) {
914   supported(__func__);
915   // TODO: We don't support sideband
916   return unsupported(__func__, stream);
917 }
918
919 HWC2::Error IAHWC2::Hwc2Layer::SetLayerSourceCrop(hwc_frect_t crop) {
920   supported(__func__);
921   hwc_layer_.SetSourceCrop(
922       hwcomposer::HwcRect<float>(crop.left, crop.top, crop.right, crop.bottom));
923   return HWC2::Error::None;
924 }
925
926 HWC2::Error IAHWC2::Hwc2Layer::SetLayerSurfaceDamage(hwc_region_t damage) {
927   uint32_t num_rects = damage.numRects;
928   hwcomposer::HwcRegion hwc_region;
929
930   for (size_t rect = 0; rect < num_rects; ++rect) {
931     hwc_region.emplace_back(damage.rects[rect].left, damage.rects[rect].top,
932                             damage.rects[rect].right,
933                             damage.rects[rect].bottom);
934   }
935
936   hwc_layer_.SetSurfaceDamage(hwc_region);
937
938   return HWC2::Error::None;
939 }
940
941 HWC2::Error IAHWC2::Hwc2Layer::SetLayerTransform(int32_t transform) {
942   supported(__func__);
943   // 270* and 180* cannot be combined with flips. More specifically, they
944   // already contain both horizontal and vertical flips, so those fields are
945   // redundant in this case. 90* rotation can be combined with either horizontal
946   // flip or vertical flip, so treat it differently
947   int32_t temp = 0;
948   if (transform == HWC_TRANSFORM_ROT_270) {
949     temp = hwcomposer::HWCTransform::kTransform270;
950   } else if (transform == HWC_TRANSFORM_ROT_180) {
951     temp = hwcomposer::HWCTransform::kTransform180;
952   } else {
953     if (transform & HWC_TRANSFORM_FLIP_H)
954       temp |= hwcomposer::HWCTransform::kReflectX;
955     if (transform & HWC_TRANSFORM_FLIP_V)
956       temp |= hwcomposer::HWCTransform::kReflectY;
957     if (transform & HWC_TRANSFORM_ROT_90)
958       temp |= hwcomposer::HWCTransform::kTransform90;
959   }
960   hwc_layer_.SetTransform(temp);
961   return HWC2::Error::None;
962 }
963
964 HWC2::Error IAHWC2::Hwc2Layer::SetLayerVisibleRegion(hwc_region_t visible) {
965   uint32_t num_rects = visible.numRects;
966   hwcomposer::HwcRegion hwc_region;
967
968   for (size_t rect = 0; rect < num_rects; ++rect) {
969     hwc_region.emplace_back(visible.rects[rect].left, visible.rects[rect].top,
970                             visible.rects[rect].right,
971                             visible.rects[rect].bottom);
972   }
973
974   hwc_layer_.SetVisibleRegion(hwc_region);
975   return HWC2::Error::None;
976 }
977
978 HWC2::Error IAHWC2::Hwc2Layer::SetLayerZOrder(uint32_t order) {
979   supported(__func__);
980
981   hwc_layer_.SetLayerZOrder(order);
982   return HWC2::Error::None;
983 }
984
985 // static
986 int IAHWC2::HookDevClose(hw_device_t * /*dev*/) {
987   unsupported(__func__);
988   return 0;
989 }
990
991 // static
992 void IAHWC2::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
993                                     uint32_t *out_count,
994                                     int32_t * /*out_capabilities*/) {
995   supported(__func__);
996   *out_count = 0;
997 }
998
999 // static
1000 hwc2_function_pointer_t IAHWC2::HookDevGetFunction(struct hwc2_device * /*dev*/,
1001                                                    int32_t descriptor) {
1002   supported(__func__);
1003   auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
1004   switch (func) {
1005     // Device functions
1006     case HWC2::FunctionDescriptor::CreateVirtualDisplay:
1007       return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
1008           DeviceHook<int32_t, decltype(&IAHWC2::CreateVirtualDisplay),
1009                      &IAHWC2::CreateVirtualDisplay, uint32_t, uint32_t,
1010                      int32_t *, hwc2_display_t *>);
1011     case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
1012       return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
1013           DeviceHook<int32_t, decltype(&IAHWC2::DestroyVirtualDisplay),
1014                      &IAHWC2::DestroyVirtualDisplay, hwc2_display_t>);
1015     case HWC2::FunctionDescriptor::Dump:
1016       return ToHook<HWC2_PFN_DUMP>(DeviceHook<
1017           void, decltype(&IAHWC2::Dump), &IAHWC2::Dump, uint32_t *, char *>);
1018     case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
1019       return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
1020           DeviceHook<uint32_t, decltype(&IAHWC2::GetMaxVirtualDisplayCount),
1021                      &IAHWC2::GetMaxVirtualDisplayCount>);
1022     case HWC2::FunctionDescriptor::RegisterCallback:
1023       return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
1024           DeviceHook<int32_t, decltype(&IAHWC2::RegisterCallback),
1025                      &IAHWC2::RegisterCallback, int32_t, hwc2_callback_data_t,
1026                      hwc2_function_pointer_t>);
1027
1028     // Display functions
1029     case HWC2::FunctionDescriptor::AcceptDisplayChanges:
1030       return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
1031           DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
1032                       &HwcDisplay::AcceptDisplayChanges>);
1033     case HWC2::FunctionDescriptor::CreateLayer:
1034       return ToHook<HWC2_PFN_CREATE_LAYER>(
1035           DisplayHook<decltype(&HwcDisplay::CreateLayer),
1036                       &HwcDisplay::CreateLayer, hwc2_layer_t *>);
1037     case HWC2::FunctionDescriptor::DestroyLayer:
1038       return ToHook<HWC2_PFN_DESTROY_LAYER>(
1039           DisplayHook<decltype(&HwcDisplay::DestroyLayer),
1040                       &HwcDisplay::DestroyLayer, hwc2_layer_t>);
1041     case HWC2::FunctionDescriptor::GetActiveConfig:
1042       return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
1043           DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
1044                       &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
1045     case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
1046       return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
1047           DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
1048                       &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
1049                       hwc2_layer_t *, int32_t *>);
1050     case HWC2::FunctionDescriptor::GetClientTargetSupport:
1051       return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
1052           DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
1053                       &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
1054                       int32_t, int32_t>);
1055     case HWC2::FunctionDescriptor::GetColorModes:
1056       return ToHook<HWC2_PFN_GET_COLOR_MODES>(
1057           DisplayHook<decltype(&HwcDisplay::GetColorModes),
1058                       &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
1059     case HWC2::FunctionDescriptor::GetDisplayAttribute:
1060       return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(DisplayHook<
1061           decltype(&HwcDisplay::GetDisplayAttribute),
1062           &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t, int32_t *>);
1063     case HWC2::FunctionDescriptor::GetDisplayConfigs:
1064       return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(DisplayHook<
1065           decltype(&HwcDisplay::GetDisplayConfigs),
1066           &HwcDisplay::GetDisplayConfigs, uint32_t *, hwc2_config_t *>);
1067     case HWC2::FunctionDescriptor::GetDisplayName:
1068       return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
1069           DisplayHook<decltype(&HwcDisplay::GetDisplayName),
1070                       &HwcDisplay::GetDisplayName, uint32_t *, char *>);
1071     case HWC2::FunctionDescriptor::GetDisplayRequests:
1072       return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
1073           DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
1074                       &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
1075                       hwc2_layer_t *, int32_t *>);
1076     case HWC2::FunctionDescriptor::GetDisplayType:
1077       return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
1078           DisplayHook<decltype(&HwcDisplay::GetDisplayType),
1079                       &HwcDisplay::GetDisplayType, int32_t *>);
1080     case HWC2::FunctionDescriptor::GetDozeSupport:
1081       return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
1082           DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
1083                       &HwcDisplay::GetDozeSupport, int32_t *>);
1084     case HWC2::FunctionDescriptor::GetHdrCapabilities:
1085       return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
1086           DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
1087                       &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
1088                       float *, float *, float *>);
1089     case HWC2::FunctionDescriptor::GetReleaseFences:
1090       return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
1091           DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
1092                       &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
1093                       int32_t *>);
1094     case HWC2::FunctionDescriptor::PresentDisplay:
1095       return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
1096           DisplayHook<decltype(&HwcDisplay::PresentDisplay),
1097                       &HwcDisplay::PresentDisplay, int32_t *>);
1098     case HWC2::FunctionDescriptor::SetActiveConfig:
1099       return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
1100           DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
1101                       &HwcDisplay::SetActiveConfig, hwc2_config_t>);
1102     case HWC2::FunctionDescriptor::SetClientTarget:
1103       return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(DisplayHook<
1104           decltype(&HwcDisplay::SetClientTarget), &HwcDisplay::SetClientTarget,
1105           buffer_handle_t, int32_t, int32_t, hwc_region_t>);
1106     case HWC2::FunctionDescriptor::SetColorMode:
1107       return ToHook<HWC2_PFN_SET_COLOR_MODE>(
1108           DisplayHook<decltype(&HwcDisplay::SetColorMode),
1109                       &HwcDisplay::SetColorMode, int32_t>);
1110     case HWC2::FunctionDescriptor::SetColorTransform:
1111       return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
1112           DisplayHook<decltype(&HwcDisplay::SetColorTransform),
1113                       &HwcDisplay::SetColorTransform, const float *, int32_t>);
1114     case HWC2::FunctionDescriptor::SetOutputBuffer:
1115       return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
1116           DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
1117                       &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
1118     case HWC2::FunctionDescriptor::SetPowerMode:
1119       return ToHook<HWC2_PFN_SET_POWER_MODE>(
1120           DisplayHook<decltype(&HwcDisplay::SetPowerMode),
1121                       &HwcDisplay::SetPowerMode, int32_t>);
1122     case HWC2::FunctionDescriptor::SetVsyncEnabled:
1123       return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
1124           DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
1125                       &HwcDisplay::SetVsyncEnabled, int32_t>);
1126     case HWC2::FunctionDescriptor::ValidateDisplay:
1127       return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
1128           DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
1129                       &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
1130
1131     // Layer functions
1132     case HWC2::FunctionDescriptor::SetCursorPosition:
1133       return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
1134           LayerHook<decltype(&Hwc2Layer::SetCursorPosition),
1135                     &Hwc2Layer::SetCursorPosition, int32_t, int32_t>);
1136     case HWC2::FunctionDescriptor::SetLayerBlendMode:
1137       return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
1138           LayerHook<decltype(&Hwc2Layer::SetLayerBlendMode),
1139                     &Hwc2Layer::SetLayerBlendMode, int32_t>);
1140     case HWC2::FunctionDescriptor::SetLayerBuffer:
1141       return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
1142           LayerHook<decltype(&Hwc2Layer::SetLayerBuffer),
1143                     &Hwc2Layer::SetLayerBuffer, buffer_handle_t, int32_t>);
1144     case HWC2::FunctionDescriptor::SetLayerColor:
1145       return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
1146           LayerHook<decltype(&Hwc2Layer::SetLayerColor),
1147                     &Hwc2Layer::SetLayerColor, hwc_color_t>);
1148     case HWC2::FunctionDescriptor::SetLayerCompositionType:
1149       return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
1150           LayerHook<decltype(&Hwc2Layer::SetLayerCompositionType),
1151                     &Hwc2Layer::SetLayerCompositionType, int32_t>);
1152     case HWC2::FunctionDescriptor::SetLayerDataspace:
1153       return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
1154           LayerHook<decltype(&Hwc2Layer::SetLayerDataspace),
1155                     &Hwc2Layer::SetLayerDataspace, int32_t>);
1156     case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
1157       return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
1158           LayerHook<decltype(&Hwc2Layer::SetLayerDisplayFrame),
1159                     &Hwc2Layer::SetLayerDisplayFrame, hwc_rect_t>);
1160     case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1161       return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1162           LayerHook<decltype(&Hwc2Layer::SetLayerPlaneAlpha),
1163                     &Hwc2Layer::SetLayerPlaneAlpha, float>);
1164     case HWC2::FunctionDescriptor::SetLayerSidebandStream:
1165       return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(LayerHook<
1166           decltype(&Hwc2Layer::SetLayerSidebandStream),
1167           &Hwc2Layer::SetLayerSidebandStream, const native_handle_t *>);
1168     case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1169       return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1170           LayerHook<decltype(&Hwc2Layer::SetLayerSourceCrop),
1171                     &Hwc2Layer::SetLayerSourceCrop, hwc_frect_t>);
1172     case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1173       return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1174           LayerHook<decltype(&Hwc2Layer::SetLayerSurfaceDamage),
1175                     &Hwc2Layer::SetLayerSurfaceDamage, hwc_region_t>);
1176     case HWC2::FunctionDescriptor::SetLayerTransform:
1177       return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1178           LayerHook<decltype(&Hwc2Layer::SetLayerTransform),
1179                     &Hwc2Layer::SetLayerTransform, int32_t>);
1180     case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1181       return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1182           LayerHook<decltype(&Hwc2Layer::SetLayerVisibleRegion),
1183                     &Hwc2Layer::SetLayerVisibleRegion, hwc_region_t>);
1184     case HWC2::FunctionDescriptor::SetLayerZOrder:
1185       return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1186           LayerHook<decltype(&Hwc2Layer::SetLayerZOrder),
1187                     &Hwc2Layer::SetLayerZOrder, uint32_t>);
1188     case HWC2::FunctionDescriptor::Invalid:
1189     default:
1190       return NULL;
1191   }
1192 }
1193
1194 // static
1195 int IAHWC2::HookDevOpen(const struct hw_module_t *module, const char *name,
1196                         struct hw_device_t **dev) {
1197   supported(__func__);
1198   if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
1199     ALOGE("Invalid module name- %s", name);
1200     return -EINVAL;
1201   }
1202
1203   std::unique_ptr<IAHWC2> ctx(new IAHWC2());
1204   if (!ctx) {
1205     ALOGE("Failed to allocate IAHWC2");
1206     return -ENOMEM;
1207   }
1208
1209   HWC2::Error err = ctx->Init();
1210   if (err != HWC2::Error::None) {
1211     ALOGE("Failed to initialize IAHWC2 err=%d\n", err);
1212     return -EINVAL;
1213   }
1214
1215   ctx->common.module = const_cast<hw_module_t *>(module);
1216   *dev = &ctx->common;
1217   ctx.release();
1218   return 0;
1219 }
1220
1221 hwcomposer::NativeDisplay *IAHWC2::GetExtendedDisplay(uint32_t dispIndex) {
1222   return extended_displays_.at(dispIndex)->GetDisplay();
1223 }
1224
1225 hwcomposer::NativeDisplay *IAHWC2::GetPrimaryDisplay() {
1226   return primary_display_.GetDisplay();
1227 }
1228
1229 hwcomposer::NativeDisplay *IAHWC2::HwcDisplay::GetDisplay() {
1230   return display_;
1231 }
1232
1233 void IAHWC2::EnableHDCPSessionForDisplay(uint32_t connector,
1234                                          EHwcsContentType content_type) {
1235   HWCContentType type = kCONTENT_TYPE0;
1236   if (content_type == HWCS_CP_CONTENT_TYPE1) {
1237     type = kCONTENT_TYPE1;
1238   }
1239
1240   device_.EnableHDCPSessionForDisplay(connector, type);
1241 }
1242
1243 void IAHWC2::EnableHDCPSessionForAllDisplays(EHwcsContentType content_type) {
1244   HWCContentType type = kCONTENT_TYPE0;
1245   if (content_type == HWCS_CP_CONTENT_TYPE1) {
1246     type = kCONTENT_TYPE1;
1247   }
1248   device_.EnableHDCPSessionForAllDisplays(type);
1249 }
1250
1251 void IAHWC2::DisableHDCPSessionForDisplay(uint32_t connector) {
1252   device_.DisableHDCPSessionForDisplay(connector);
1253 }
1254
1255 void IAHWC2::DisableHDCPSessionForAllDisplays() {
1256   device_.DisableHDCPSessionForAllDisplays();
1257 }
1258
1259 void IAHWC2::SetPAVPSessionStatus(bool enabled, uint32_t papv_session_id,
1260                                   uint32_t pavp_instance_id) {
1261   device_.SetPAVPSessionStatus(enabled, papv_session_id, pavp_instance_id);
1262 }
1263
1264 void IAHWC2::SetHDCPSRMForAllDisplays(const int8_t *SRM, uint32_t SRMLength) {
1265   if (SRM == NULL) {
1266     ETRACE("Error:HDCP Set NULL SRM");
1267     return;
1268   }
1269   device_.SetHDCPSRMForAllDisplays(SRM, SRMLength);
1270 }
1271
1272 uint32_t IAHWC2::GetDisplayIDFromConnectorID(const uint32_t connector_id) {
1273   return device_.GetDisplayIDFromConnectorID(connector_id);
1274 }
1275
1276 void IAHWC2::SetHDCPSRMForDisplay(uint32_t connector, const int8_t *SRM,
1277                                   uint32_t SRMLength) {
1278   if (SRM == NULL) {
1279     ETRACE("Error:HDCP Set NULL SRM");
1280     return;
1281   }
1282   device_.SetHDCPSRMForDisplay(connector, SRM, SRMLength);
1283 }
1284
1285 }  // namespace android
1286
1287 static struct hw_module_methods_t hwc2_module_methods = {
1288     .open = android::IAHWC2::HookDevOpen,
1289 };
1290
1291 hw_module_t HAL_MODULE_INFO_SYM = {
1292     .tag = HARDWARE_MODULE_TAG,
1293     .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1294     .id = HWC_HARDWARE_MODULE_ID,
1295     .name = "IA-Hardware-Composer",
1296     .author = "The Android Open Source Project",
1297     .methods = &hwc2_module_methods,
1298     .dso = NULL,
1299     .reserved = {0},
1300 };