OSDN Git Service

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