1 /*
2  * Copyright 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 #ifndef ANDROID_HARDWARE_GRAPHICS_COMPOSER_V2_1_HWC_H
18 #define ANDROID_HARDWARE_GRAPHICS_COMPOSER_V2_1_HWC_H
19 
20 #include <mutex>
21 #include <memory>
22 #include <unordered_set>
23 #include <vector>
24 
25 #include <android/hardware/graphics/composer/2.1/IComposer.h>
26 #define HWC2_INCLUDE_STRINGIFICATION
27 #define HWC2_USE_CPP11
28 #include <hardware/hwcomposer2.h>
29 #undef HWC2_INCLUDE_STRINGIFICATION
30 #undef HWC2_USE_CPP11
31 #include "ComposerBase.h"
32 
33 namespace android {
34     class HWC2On1Adapter;
35 }
36 
37 namespace android {
38 namespace hardware {
39 namespace graphics {
40 namespace composer {
41 namespace V2_1 {
42 namespace implementation {
43 
44 using android::hardware::graphics::common::V1_0::PixelFormat;
45 using android::hardware::graphics::common::V1_0::Transform;
46 using android::hardware::graphics::common::V1_0::Dataspace;
47 using android::hardware::graphics::common::V1_0::ColorMode;
48 using android::hardware::graphics::common::V1_0::ColorTransform;
49 using android::hardware::graphics::common::V1_0::Hdr;
50 
51 class ComposerClient;
52 
53 class HwcHal : public IComposer, public ComposerBase {
54 public:
55     HwcHal(const hw_module_t* module);
56     virtual ~HwcHal();
57 
58     bool hasCapability(Capability capability) const;
59 
60     // IComposer interface
61     Return<void> getCapabilities(getCapabilities_cb hidl_cb) override;
62     Return<void> dumpDebugInfo(dumpDebugInfo_cb hidl_cb) override;
63     Return<void> createClient(createClient_cb hidl_cb) override;
64 
65     // ComposerBase interface
66     void removeClient() override;
67     void enableCallback(bool enable) override;
68     uint32_t getMaxVirtualDisplayCount() override;
69     Error createVirtualDisplay(uint32_t width, uint32_t height,
70         PixelFormat* format, Display* outDisplay) override;
71     Error destroyVirtualDisplay(Display display) override;
72 
73     Error createLayer(Display display, Layer* outLayer) override;
74     Error destroyLayer(Display display, Layer layer) override;
75 
76     Error getActiveConfig(Display display, Config* outConfig) override;
77     Error getClientTargetSupport(Display display,
78             uint32_t width, uint32_t height,
79             PixelFormat format, Dataspace dataspace) override;
80     Error getColorModes(Display display,
81             hidl_vec<ColorMode>* outModes) override;
82     Error getDisplayAttribute(Display display, Config config,
83             IComposerClient::Attribute attribute, int32_t* outValue) override;
84     Error getDisplayConfigs(Display display,
85             hidl_vec<Config>* outConfigs) override;
86     Error getDisplayName(Display display, hidl_string* outName) override;
87     Error getDisplayType(Display display,
88             IComposerClient::DisplayType* outType) override;
89     Error getDozeSupport(Display display, bool* outSupport) override;
90     Error getHdrCapabilities(Display display, hidl_vec<Hdr>* outTypes,
91             float* outMaxLuminance, float* outMaxAverageLuminance,
92             float* outMinLuminance) override;
93 
94     Error setActiveConfig(Display display, Config config) override;
95     Error setColorMode(Display display, ColorMode mode) override;
96     Error setPowerMode(Display display,
97             IComposerClient::PowerMode mode) override;
98     Error setVsyncEnabled(Display display,
99             IComposerClient::Vsync enabled) override;
100 
101     Error setColorTransform(Display display, const float* matrix,
102             int32_t hint) override;
103     Error setClientTarget(Display display, buffer_handle_t target,
104             int32_t acquireFence, int32_t dataspace,
105             const std::vector<hwc_rect_t>& damage) override;
106     Error setOutputBuffer(Display display, buffer_handle_t buffer,
107             int32_t releaseFence) override;
108     Error validateDisplay(Display display,
109             std::vector<Layer>* outChangedLayers,
110             std::vector<IComposerClient::Composition>* outCompositionTypes,
111             uint32_t* outDisplayRequestMask,
112             std::vector<Layer>* outRequestedLayers,
113             std::vector<uint32_t>* outRequestMasks) override;
114     Error acceptDisplayChanges(Display display) override;
115     Error presentDisplay(Display display, int32_t* outPresentFence,
116             std::vector<Layer>* outLayers,
117             std::vector<int32_t>* outReleaseFences) override;
118 
119     Error setLayerCursorPosition(Display display, Layer layer,
120             int32_t x, int32_t y) override;
121     Error setLayerBuffer(Display display, Layer layer,
122             buffer_handle_t buffer, int32_t acquireFence) override;
123     Error setLayerSurfaceDamage(Display display, Layer layer,
124             const std::vector<hwc_rect_t>& damage) override;
125     Error setLayerBlendMode(Display display, Layer layer,
126             int32_t mode) override;
127     Error setLayerColor(Display display, Layer layer,
128             IComposerClient::Color color) override;
129     Error setLayerCompositionType(Display display, Layer layer,
130             int32_t type) override;
131     Error setLayerDataspace(Display display, Layer layer,
132             int32_t dataspace) override;
133     Error setLayerDisplayFrame(Display display, Layer layer,
134             const hwc_rect_t& frame) override;
135     Error setLayerPlaneAlpha(Display display, Layer layer,
136             float alpha) override;
137     Error setLayerSidebandStream(Display display, Layer layer,
138             buffer_handle_t stream) override;
139     Error setLayerSourceCrop(Display display, Layer layer,
140             const hwc_frect_t& crop) override;
141     Error setLayerTransform(Display display, Layer layer,
142             int32_t transform) override;
143     Error setLayerVisibleRegion(Display display, Layer layer,
144             const std::vector<hwc_rect_t>& visible) override;
145     Error setLayerZOrder(Display display, Layer layer, uint32_t z) override;
146 
147 private:
148     void initCapabilities();
149 
150     template<typename T>
151     void initDispatch(hwc2_function_descriptor_t desc, T* outPfn);
152     void initDispatch();
153 
154     sp<ComposerClient> getClient();
155 
156     static void hotplugHook(hwc2_callback_data_t callbackData,
157         hwc2_display_t display, int32_t connected);
158     static void refreshHook(hwc2_callback_data_t callbackData,
159         hwc2_display_t display);
160     static void vsyncHook(hwc2_callback_data_t callbackData,
161         hwc2_display_t display, int64_t timestamp);
162 
163     hwc2_device_t* mDevice;
164 
165     std::unordered_set<Capability> mCapabilities;
166 
167     struct {
168         HWC2_PFN_ACCEPT_DISPLAY_CHANGES acceptDisplayChanges;
169         HWC2_PFN_CREATE_LAYER createLayer;
170         HWC2_PFN_CREATE_VIRTUAL_DISPLAY createVirtualDisplay;
171         HWC2_PFN_DESTROY_LAYER destroyLayer;
172         HWC2_PFN_DESTROY_VIRTUAL_DISPLAY destroyVirtualDisplay;
173         HWC2_PFN_DUMP dump;
174         HWC2_PFN_GET_ACTIVE_CONFIG getActiveConfig;
175         HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES getChangedCompositionTypes;
176         HWC2_PFN_GET_CLIENT_TARGET_SUPPORT getClientTargetSupport;
177         HWC2_PFN_GET_COLOR_MODES getColorModes;
178         HWC2_PFN_GET_DISPLAY_ATTRIBUTE getDisplayAttribute;
179         HWC2_PFN_GET_DISPLAY_CONFIGS getDisplayConfigs;
180         HWC2_PFN_GET_DISPLAY_NAME getDisplayName;
181         HWC2_PFN_GET_DISPLAY_REQUESTS getDisplayRequests;
182         HWC2_PFN_GET_DISPLAY_TYPE getDisplayType;
183         HWC2_PFN_GET_DOZE_SUPPORT getDozeSupport;
184         HWC2_PFN_GET_HDR_CAPABILITIES getHdrCapabilities;
185         HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT getMaxVirtualDisplayCount;
186         HWC2_PFN_GET_RELEASE_FENCES getReleaseFences;
187         HWC2_PFN_PRESENT_DISPLAY presentDisplay;
188         HWC2_PFN_REGISTER_CALLBACK registerCallback;
189         HWC2_PFN_SET_ACTIVE_CONFIG setActiveConfig;
190         HWC2_PFN_SET_CLIENT_TARGET setClientTarget;
191         HWC2_PFN_SET_COLOR_MODE setColorMode;
192         HWC2_PFN_SET_COLOR_TRANSFORM setColorTransform;
193         HWC2_PFN_SET_CURSOR_POSITION setCursorPosition;
194         HWC2_PFN_SET_LAYER_BLEND_MODE setLayerBlendMode;
195         HWC2_PFN_SET_LAYER_BUFFER setLayerBuffer;
196         HWC2_PFN_SET_LAYER_COLOR setLayerColor;
197         HWC2_PFN_SET_LAYER_COMPOSITION_TYPE setLayerCompositionType;
198         HWC2_PFN_SET_LAYER_DATASPACE setLayerDataspace;
199         HWC2_PFN_SET_LAYER_DISPLAY_FRAME setLayerDisplayFrame;
200         HWC2_PFN_SET_LAYER_PLANE_ALPHA setLayerPlaneAlpha;
201         HWC2_PFN_SET_LAYER_SIDEBAND_STREAM setLayerSidebandStream;
202         HWC2_PFN_SET_LAYER_SOURCE_CROP setLayerSourceCrop;
203         HWC2_PFN_SET_LAYER_SURFACE_DAMAGE setLayerSurfaceDamage;
204         HWC2_PFN_SET_LAYER_TRANSFORM setLayerTransform;
205         HWC2_PFN_SET_LAYER_VISIBLE_REGION setLayerVisibleRegion;
206         HWC2_PFN_SET_LAYER_Z_ORDER setLayerZOrder;
207         HWC2_PFN_SET_OUTPUT_BUFFER setOutputBuffer;
208         HWC2_PFN_SET_POWER_MODE setPowerMode;
209         HWC2_PFN_SET_VSYNC_ENABLED setVsyncEnabled;
210         HWC2_PFN_VALIDATE_DISPLAY validateDisplay;
211     } mDispatch;
212 
213     std::mutex mClientMutex;
214     wp<ComposerClient> mClient;
215 
216     // If the HWC implementation version is < 2.0, use an adapter to interface
217     // between HWC 2.0 <-> HWC 1.X.
218     std::unique_ptr<HWC2On1Adapter> mAdapter;
219 };
220 
221 extern "C" IComposer* HIDL_FETCH_IComposer(const char* name);
222 
223 } // namespace implementation
224 } // namespace V2_1
225 } // namespace composer
226 } // namespace graphics
227 } // namespace hardware
228 } // namespace android
229 
230 #endif  // ANDROID_HARDWARE_GRAPHICS_COMPOSER_V2_1_HWC_H
231