1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* vim:set ts=4 sw=4 sts=4 et: */
3 /*
4  * Copyright (c) 2015 The Linux Foundation. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #ifndef mozilla_HwcHALBase
20 #define mozilla_HwcHALBase
21 
22 #include "mozilla/UniquePtr.h"
23 #include "nsRect.h"
24 
25 #include <hardware/hwcomposer.h>
26 
27 #ifndef HWC_BLIT
28 #if ANDROID_VERSION >= 21
29 #define HWC_BLIT 0xFF
30 #elif ANDROID_VERSION >= 17
31 #define HWC_BLIT (HWC_FRAMEBUFFER_TARGET + 1)
32 #else
33 // ICS didn't support this. However, we define this
34 // for passing compilation
35 #define HWC_BLIT 0xFF
36 #endif // #if ANDROID_VERSION
37 #endif // #ifndef HWC_BLIT
38 
39 namespace mozilla {
40 
41 #if ANDROID_VERSION >= 17
42 using HwcDevice = hwc_composer_device_1_t;
43 using HwcList   = hwc_display_contents_1_t;
44 using HwcLayer  = hwc_layer_1_t;
45 #else
46 using HwcDevice = hwc_composer_device_t;
47 using HwcList   = hwc_layer_list_t;
48 using HwcLayer  = hwc_layer_t;
49 #endif
50 
51 // HwcHAL definition for HwcEvent callback types
52 // Note: hwc_procs is different between ICS and later,
53 //       and the signature of invalidate is also different.
54 //       Use this wrap struct to hide the detail. BTW,
55 //       we don't have to register callback functions on ICS, so
56 //       there is no callbacks for ICS in HwcHALProcs.
57 typedef struct HwcHALProcs {
58     void (*invalidate)(const struct hwc_procs* procs);
59     void (*vsync)(const struct hwc_procs* procs, int disp, int64_t timestamp);
60     void (*hotplug)(const struct hwc_procs* procs, int disp, int connected);
61 } HwcHALProcs_t;
62 
63 // HwcHAL class
64 // This class handle all the HAL related work
65 // The purpose of HwcHAL is to make HwcComposer2D simpler.
66 class HwcHALBase {
67 
68 public:
69     // Query Types. We can add more types easily in the future
70     enum class QueryType {
71         COLOR_FILL = 0x8,
72         RB_SWAP = 0x40
73     };
74 
75 public:
76     explicit HwcHALBase() = default;
77 
~HwcHALBase()78     virtual ~HwcHALBase() {}
79 
80     // Create HwcHAL module, Only HwcComposer2D calls this.
81     // If other modules want to use HwcHAL, please use APIs in
82     // HwcComposer2D
83     static UniquePtr<HwcHALBase> CreateHwcHAL();
84 
85     // Check if mHwc exists
86     virtual bool HasHwc() const = 0;
87 
88     // Set EGL info (only ICS need this info)
89     virtual void SetEGLInfo(hwc_display_t aEGLDisplay,
90                             hwc_surface_t aEGLSurface) = 0;
91 
92     // HwcDevice query properties
93     virtual bool Query(QueryType aType) = 0;
94 
95     // HwcDevice set
96     virtual int Set(HwcList *aList,
97                     uint32_t aDisp) = 0;
98 
99     // Reset HwcDevice
100     virtual int ResetHwc() = 0;
101 
102     // HwcDevice prepare
103     virtual int Prepare(HwcList *aList,
104                         uint32_t aDisp,
105                         hwc_rect_t aDispRect,
106                         buffer_handle_t aHandle,
107                         int aFenceFd) = 0;
108 
109     // Check transparency support
110     virtual bool SupportTransparency() const = 0;
111 
112     // Get a geometry change flag
113     virtual uint32_t GetGeometryChangedFlag(bool aGeometryChanged) const = 0;
114 
115     // Set crop help
116     virtual void SetCrop(HwcLayer &aLayer,
117                          const hwc_rect_t &aSrcCrop) const = 0;
118 
119     // Enable HW Vsync
120     virtual bool EnableVsync(bool aEnable) = 0;
121 
122     // Register HW event callback functions
123     virtual bool RegisterHwcEventCallback(const HwcHALProcs_t &aProcs) = 0;
124 
125 protected:
HwcAPIVersion(uint32_t aMaj,uint32_t aMin)126     constexpr static uint32_t HwcAPIVersion(uint32_t aMaj, uint32_t aMin) {
127         // HARDWARE_MAKE_API_VERSION_2, from Android hardware.h
128         return (((aMaj & 0xff) << 24) | ((aMin & 0xff) << 16) | (1 & 0xffff));
129     }
130 };
131 
132 } // namespace mozilla
133 
134 #endif // mozilla_HwcHALBase
135