blob: 68781da2c3f20d097ca40f5e20ecc274cbf1fee4 [file] [log] [blame]
Dan Stoza651bf312015-10-23 17:03:17 -07001/*
2 * Copyright 2015 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_SF_HWC2_H
18#define ANDROID_SF_HWC2_H
19
20#define HWC2_INCLUDE_STRINGIFICATION
21#define HWC2_USE_CPP11
22#include <hardware/hwcomposer2.h>
23#undef HWC2_INCLUDE_STRINGIFICATION
24#undef HWC2_USE_CPP11
25
Dan Stoza7d7ae732016-03-16 12:23:40 -070026#include <ui/HdrCapabilities.h>
Dan Stoza5df2a862016-03-24 16:19:37 -070027#include <ui/mat4.h>
Dan Stoza7d7ae732016-03-16 12:23:40 -070028
Dan Stoza651bf312015-10-23 17:03:17 -070029#include <utils/Log.h>
30#include <utils/StrongPointer.h>
31#include <utils/Timers.h>
32
33#include <functional>
34#include <string>
35#include <unordered_map>
36#include <vector>
37
38namespace android {
39 class Fence;
40 class FloatRect;
41 class GraphicBuffer;
42 class Rect;
43 class Region;
44}
45
46namespace HWC2 {
47
48class Display;
49class Layer;
50
51typedef std::function<void(std::shared_ptr<Display>, Connection)>
52 HotplugCallback;
53typedef std::function<void(std::shared_ptr<Display>)> RefreshCallback;
54typedef std::function<void(std::shared_ptr<Display>, nsecs_t)> VsyncCallback;
55
56class Device
57{
58public:
59 Device(hwc2_device_t* device);
60 ~Device();
61
62 friend class HWC2::Display;
63 friend class HWC2::Layer;
64
65 // Required by HWC2
66
67 std::string dump() const;
68
69 const std::vector<Capability>& getCapabilities() const {
70 return mCapabilities;
71 };
72
73 uint32_t getMaxVirtualDisplayCount() const;
74 Error createVirtualDisplay(uint32_t width, uint32_t height,
Dan Stoza5cf424b2016-05-20 14:02:39 -070075 android_pixel_format_t* format,
Dan Stoza651bf312015-10-23 17:03:17 -070076 std::shared_ptr<Display>* outDisplay);
77
78 void registerHotplugCallback(HotplugCallback hotplug);
79 void registerRefreshCallback(RefreshCallback refresh);
80 void registerVsyncCallback(VsyncCallback vsync);
81
82 // For use by callbacks
83
84 void callHotplug(std::shared_ptr<Display> display, Connection connected);
85 void callRefresh(std::shared_ptr<Display> display);
86 void callVsync(std::shared_ptr<Display> display, nsecs_t timestamp);
87
88 // Other Device methods
89
90 // This will create a Display if one is not found, but it will not be marked
Dan Stoza38628982016-07-13 15:48:58 -070091 // as connected. This Display may be null if the display has been torn down
92 // but has not been removed from the map yet.
Dan Stoza651bf312015-10-23 17:03:17 -070093 std::shared_ptr<Display> getDisplayById(hwc2_display_t id);
94
Dan Stoza09e7a272016-04-14 12:31:01 -070095 bool hasCapability(HWC2::Capability capability) const;
96
Dan Stoza651bf312015-10-23 17:03:17 -070097private:
98 // Initialization methods
99
100 template <typename PFN>
101 [[clang::warn_unused_result]] bool loadFunctionPointer(
102 FunctionDescriptor desc, PFN& outPFN) {
103 auto intDesc = static_cast<int32_t>(desc);
104 auto pfn = mHwcDevice->getFunction(mHwcDevice, intDesc);
105 if (pfn != nullptr) {
106 outPFN = reinterpret_cast<PFN>(pfn);
107 return true;
108 } else {
109 ALOGE("Failed to load function %s", to_string(desc).c_str());
110 return false;
111 }
112 }
113
114 template <typename PFN, typename HOOK>
115 void registerCallback(Callback callback, HOOK hook) {
116 static_assert(std::is_same<PFN, HOOK>::value,
117 "Incompatible function pointer");
118 auto intCallback = static_cast<int32_t>(callback);
119 auto callbackData = static_cast<hwc2_callback_data_t>(this);
120 auto pfn = reinterpret_cast<hwc2_function_pointer_t>(hook);
121 mRegisterCallback(mHwcDevice, intCallback, callbackData, pfn);
122 }
123
124 void loadCapabilities();
125 void loadFunctionPointers();
126 void registerCallbacks();
127
128 // For use by Display
129
130 void destroyVirtualDisplay(hwc2_display_t display);
131
132 // Member variables
133
134 hwc2_device_t* mHwcDevice;
135
136 // Device function pointers
137 HWC2_PFN_CREATE_VIRTUAL_DISPLAY mCreateVirtualDisplay;
138 HWC2_PFN_DESTROY_VIRTUAL_DISPLAY mDestroyVirtualDisplay;
139 HWC2_PFN_DUMP mDump;
140 HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT mGetMaxVirtualDisplayCount;
141 HWC2_PFN_REGISTER_CALLBACK mRegisterCallback;
142
143 // Display function pointers
144 HWC2_PFN_ACCEPT_DISPLAY_CHANGES mAcceptDisplayChanges;
145 HWC2_PFN_CREATE_LAYER mCreateLayer;
146 HWC2_PFN_DESTROY_LAYER mDestroyLayer;
147 HWC2_PFN_GET_ACTIVE_CONFIG mGetActiveConfig;
148 HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES mGetChangedCompositionTypes;
Dan Stoza076ac672016-03-14 10:47:53 -0700149 HWC2_PFN_GET_COLOR_MODES mGetColorModes;
Dan Stoza651bf312015-10-23 17:03:17 -0700150 HWC2_PFN_GET_DISPLAY_ATTRIBUTE mGetDisplayAttribute;
151 HWC2_PFN_GET_DISPLAY_CONFIGS mGetDisplayConfigs;
152 HWC2_PFN_GET_DISPLAY_NAME mGetDisplayName;
153 HWC2_PFN_GET_DISPLAY_REQUESTS mGetDisplayRequests;
154 HWC2_PFN_GET_DISPLAY_TYPE mGetDisplayType;
155 HWC2_PFN_GET_DOZE_SUPPORT mGetDozeSupport;
Dan Stoza7d7ae732016-03-16 12:23:40 -0700156 HWC2_PFN_GET_HDR_CAPABILITIES mGetHdrCapabilities;
Dan Stoza651bf312015-10-23 17:03:17 -0700157 HWC2_PFN_GET_RELEASE_FENCES mGetReleaseFences;
158 HWC2_PFN_PRESENT_DISPLAY mPresentDisplay;
159 HWC2_PFN_SET_ACTIVE_CONFIG mSetActiveConfig;
160 HWC2_PFN_SET_CLIENT_TARGET mSetClientTarget;
Dan Stoza076ac672016-03-14 10:47:53 -0700161 HWC2_PFN_SET_COLOR_MODE mSetColorMode;
Dan Stoza5df2a862016-03-24 16:19:37 -0700162 HWC2_PFN_SET_COLOR_TRANSFORM mSetColorTransform;
Dan Stoza651bf312015-10-23 17:03:17 -0700163 HWC2_PFN_SET_OUTPUT_BUFFER mSetOutputBuffer;
164 HWC2_PFN_SET_POWER_MODE mSetPowerMode;
165 HWC2_PFN_SET_VSYNC_ENABLED mSetVsyncEnabled;
166 HWC2_PFN_VALIDATE_DISPLAY mValidateDisplay;
167
168 // Layer function pointers
169 HWC2_PFN_SET_CURSOR_POSITION mSetCursorPosition;
170 HWC2_PFN_SET_LAYER_BUFFER mSetLayerBuffer;
171 HWC2_PFN_SET_LAYER_SURFACE_DAMAGE mSetLayerSurfaceDamage;
172 HWC2_PFN_SET_LAYER_BLEND_MODE mSetLayerBlendMode;
173 HWC2_PFN_SET_LAYER_COLOR mSetLayerColor;
174 HWC2_PFN_SET_LAYER_COMPOSITION_TYPE mSetLayerCompositionType;
Dan Stoza5df2a862016-03-24 16:19:37 -0700175 HWC2_PFN_SET_LAYER_DATASPACE mSetLayerDataspace;
Dan Stoza651bf312015-10-23 17:03:17 -0700176 HWC2_PFN_SET_LAYER_DISPLAY_FRAME mSetLayerDisplayFrame;
177 HWC2_PFN_SET_LAYER_PLANE_ALPHA mSetLayerPlaneAlpha;
178 HWC2_PFN_SET_LAYER_SIDEBAND_STREAM mSetLayerSidebandStream;
179 HWC2_PFN_SET_LAYER_SOURCE_CROP mSetLayerSourceCrop;
180 HWC2_PFN_SET_LAYER_TRANSFORM mSetLayerTransform;
181 HWC2_PFN_SET_LAYER_VISIBLE_REGION mSetLayerVisibleRegion;
182 HWC2_PFN_SET_LAYER_Z_ORDER mSetLayerZOrder;
183
184 std::vector<Capability> mCapabilities;
Dan Stoza38628982016-07-13 15:48:58 -0700185 std::unordered_map<hwc2_display_t, std::weak_ptr<Display>> mDisplays;
Dan Stoza651bf312015-10-23 17:03:17 -0700186
187 HotplugCallback mHotplug;
188 std::vector<std::pair<std::shared_ptr<Display>, Connection>>
189 mPendingHotplugs;
190 RefreshCallback mRefresh;
191 std::vector<std::shared_ptr<Display>> mPendingRefreshes;
192 VsyncCallback mVsync;
193 std::vector<std::pair<std::shared_ptr<Display>, nsecs_t>> mPendingVsyncs;
194};
195
196class Display : public std::enable_shared_from_this<Display>
197{
198public:
199 Display(Device& device, hwc2_display_t id);
200 ~Display();
201
202 friend class HWC2::Device;
203 friend class HWC2::Layer;
204
205 class Config
206 {
207 public:
208 class Builder
209 {
210 public:
211 Builder(Display& display, hwc2_config_t id);
212
213 std::shared_ptr<const Config> build() {
214 return std::const_pointer_cast<const Config>(
215 std::move(mConfig));
216 }
217
218 Builder& setWidth(int32_t width) {
219 mConfig->mWidth = width;
220 return *this;
221 }
222 Builder& setHeight(int32_t height) {
223 mConfig->mHeight = height;
224 return *this;
225 }
226 Builder& setVsyncPeriod(int32_t vsyncPeriod) {
227 mConfig->mVsyncPeriod = vsyncPeriod;
228 return *this;
229 }
230 Builder& setDpiX(int32_t dpiX) {
231 if (dpiX == -1) {
232 mConfig->mDpiX = getDefaultDensity();
233 } else {
234 mConfig->mDpiX = dpiX / 1000.0f;
235 }
236 return *this;
237 }
238 Builder& setDpiY(int32_t dpiY) {
239 if (dpiY == -1) {
240 mConfig->mDpiY = getDefaultDensity();
241 } else {
242 mConfig->mDpiY = dpiY / 1000.0f;
243 }
244 return *this;
245 }
246
247 private:
248 float getDefaultDensity();
249 std::shared_ptr<Config> mConfig;
250 };
251
252 hwc2_display_t getDisplayId() const { return mDisplay.getId(); }
253 hwc2_config_t getId() const { return mId; }
254
255 int32_t getWidth() const { return mWidth; }
256 int32_t getHeight() const { return mHeight; }
257 nsecs_t getVsyncPeriod() const { return mVsyncPeriod; }
258 float getDpiX() const { return mDpiX; }
259 float getDpiY() const { return mDpiY; }
260
261 private:
262 Config(Display& display, hwc2_config_t id);
263
264 Display& mDisplay;
265 hwc2_config_t mId;
266
267 int32_t mWidth;
268 int32_t mHeight;
269 nsecs_t mVsyncPeriod;
270 float mDpiX;
271 float mDpiY;
272 };
273
274 // Required by HWC2
275
276 [[clang::warn_unused_result]] Error acceptChanges();
277 [[clang::warn_unused_result]] Error createLayer(
278 std::shared_ptr<Layer>* outLayer);
279 [[clang::warn_unused_result]] Error getActiveConfig(
280 std::shared_ptr<const Config>* outConfig) const;
281 [[clang::warn_unused_result]] Error getChangedCompositionTypes(
282 std::unordered_map<std::shared_ptr<Layer>, Composition>* outTypes);
Dan Stoza076ac672016-03-14 10:47:53 -0700283 [[clang::warn_unused_result]] Error getColorModes(
284 std::vector<int32_t>* outModes) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700285
286 // Doesn't call into the HWC2 device, so no errors are possible
287 std::vector<std::shared_ptr<const Config>> getConfigs() const;
288
289 [[clang::warn_unused_result]] Error getName(std::string* outName) const;
290 [[clang::warn_unused_result]] Error getRequests(
291 DisplayRequest* outDisplayRequests,
292 std::unordered_map<std::shared_ptr<Layer>, LayerRequest>*
293 outLayerRequests);
294 [[clang::warn_unused_result]] Error getType(DisplayType* outType) const;
295 [[clang::warn_unused_result]] Error supportsDoze(bool* outSupport) const;
Dan Stoza7d7ae732016-03-16 12:23:40 -0700296 [[clang::warn_unused_result]] Error getHdrCapabilities(
297 std::unique_ptr<android::HdrCapabilities>* outCapabilities) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700298 [[clang::warn_unused_result]] Error getReleaseFences(
299 std::unordered_map<std::shared_ptr<Layer>,
300 android::sp<android::Fence>>* outFences) const;
301 [[clang::warn_unused_result]] Error present(
302 android::sp<android::Fence>* outRetireFence);
303 [[clang::warn_unused_result]] Error setActiveConfig(
304 const std::shared_ptr<const Config>& config);
305 [[clang::warn_unused_result]] Error setClientTarget(
306 buffer_handle_t target,
307 const android::sp<android::Fence>& acquireFence,
308 android_dataspace_t dataspace);
Dan Stoza076ac672016-03-14 10:47:53 -0700309 [[clang::warn_unused_result]] Error setColorMode(int32_t mode);
Dan Stoza5df2a862016-03-24 16:19:37 -0700310 [[clang::warn_unused_result]] Error setColorTransform(
311 const android::mat4& matrix, android_color_transform_t hint);
Dan Stoza651bf312015-10-23 17:03:17 -0700312 [[clang::warn_unused_result]] Error setOutputBuffer(
313 const android::sp<android::GraphicBuffer>& buffer,
314 const android::sp<android::Fence>& releaseFence);
315 [[clang::warn_unused_result]] Error setPowerMode(PowerMode mode);
316 [[clang::warn_unused_result]] Error setVsyncEnabled(Vsync enabled);
317 [[clang::warn_unused_result]] Error validate(uint32_t* outNumTypes,
318 uint32_t* outNumRequests);
319
320 // Other Display methods
321
322 Device& getDevice() const { return mDevice; }
323 hwc2_display_t getId() const { return mId; }
324 bool isConnected() const { return mIsConnected; }
325
326private:
327 // For use by Device
328
329 // Virtual displays are always connected
330 void setVirtual() {
331 mIsVirtual = true;
332 mIsConnected = true;
333 }
334
335 void setConnected(bool connected) { mIsConnected = connected; }
336 int32_t getAttribute(hwc2_config_t configId, Attribute attribute);
337 void loadConfig(hwc2_config_t configId);
338 void loadConfigs();
339
340 // For use by Layer
341 void destroyLayer(hwc2_layer_t layerId);
342
343 // This may fail (and return a null pointer) if no layer with this ID exists
344 // on this display
345 std::shared_ptr<Layer> getLayerById(hwc2_layer_t id) const;
346
347 // Member variables
348
349 Device& mDevice;
350 hwc2_display_t mId;
351 bool mIsConnected;
352 bool mIsVirtual;
353 std::unordered_map<hwc2_layer_t, std::weak_ptr<Layer>> mLayers;
354 std::unordered_map<hwc2_config_t, std::shared_ptr<const Config>> mConfigs;
355};
356
357class Layer
358{
359public:
360 Layer(const std::shared_ptr<Display>& display, hwc2_layer_t id);
361 ~Layer();
362
363 bool isAbandoned() const { return mDisplay.expired(); }
364 hwc2_layer_t getId() const { return mId; }
365
366 [[clang::warn_unused_result]] Error setCursorPosition(int32_t x, int32_t y);
367 [[clang::warn_unused_result]] Error setBuffer(buffer_handle_t buffer,
368 const android::sp<android::Fence>& acquireFence);
369 [[clang::warn_unused_result]] Error setSurfaceDamage(
370 const android::Region& damage);
371
372 [[clang::warn_unused_result]] Error setBlendMode(BlendMode mode);
373 [[clang::warn_unused_result]] Error setColor(hwc_color_t color);
374 [[clang::warn_unused_result]] Error setCompositionType(Composition type);
Dan Stoza5df2a862016-03-24 16:19:37 -0700375 [[clang::warn_unused_result]] Error setDataspace(
376 android_dataspace_t dataspace);
Dan Stoza651bf312015-10-23 17:03:17 -0700377 [[clang::warn_unused_result]] Error setDisplayFrame(
378 const android::Rect& frame);
379 [[clang::warn_unused_result]] Error setPlaneAlpha(float alpha);
380 [[clang::warn_unused_result]] Error setSidebandStream(
381 const native_handle_t* stream);
382 [[clang::warn_unused_result]] Error setSourceCrop(
383 const android::FloatRect& crop);
384 [[clang::warn_unused_result]] Error setTransform(Transform transform);
385 [[clang::warn_unused_result]] Error setVisibleRegion(
386 const android::Region& region);
387 [[clang::warn_unused_result]] Error setZOrder(uint32_t z);
388
389private:
390 std::weak_ptr<Display> mDisplay;
391 hwc2_display_t mDisplayId;
392 Device& mDevice;
393 hwc2_layer_t mId;
394};
395
396} // namespace HWC2
397
398#endif // ANDROID_SF_HWC2_H