blob: 93eb99907b80d3f789b14dfcc7362a42ed0f4936 [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>
Dan Stoza9f26a9c2016-06-22 14:51:09 -070036#include <unordered_set>
Dan Stoza651bf312015-10-23 17:03:17 -070037#include <vector>
38
39namespace android {
40 class Fence;
Dan Stoza651bf312015-10-23 17:03:17 -070041 class GraphicBuffer;
42 class Rect;
43 class Region;
Dan Stoza71bded52016-10-19 11:10:33 -070044 namespace gfx {
45 class FloatRect;
46 }
Chia-I Wuaab99f52016-10-05 12:59:58 +080047 namespace Hwc2 {
48 class Composer;
Dan Stoza71bded52016-10-19 11:10:33 -070049 }
Dan Stoza651bf312015-10-23 17:03:17 -070050}
51
52namespace HWC2 {
53
54class Display;
55class Layer;
56
57typedef std::function<void(std::shared_ptr<Display>, Connection)>
58 HotplugCallback;
59typedef std::function<void(std::shared_ptr<Display>)> RefreshCallback;
60typedef std::function<void(std::shared_ptr<Display>, nsecs_t)> VsyncCallback;
61
Fabien Sanglard33960702016-11-29 17:58:21 -080062// C++ Wrapper around hwc2_device_t. Load all functions pointers
63// and handle callback registration.
Dan Stoza651bf312015-10-23 17:03:17 -070064class Device
65{
66public:
Chia-I Wuaab99f52016-10-05 12:59:58 +080067#ifdef BYPASS_IHWC
Chih-Hung Hsieh342b7602016-09-01 11:34:16 -070068 explicit Device(hwc2_device_t* device);
Chia-I Wuaab99f52016-10-05 12:59:58 +080069#else
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -080070 // useVrComposer is passed to the composer HAL. When true, the composer HAL
71 // will use the vr composer service, otherwise it uses the real hardware
72 // composer.
73 Device(bool useVrComposer);
Chia-I Wuaab99f52016-10-05 12:59:58 +080074#endif
Dan Stoza651bf312015-10-23 17:03:17 -070075 ~Device();
76
77 friend class HWC2::Display;
78 friend class HWC2::Layer;
79
80 // Required by HWC2
81
82 std::string dump() const;
83
Dan Stoza9f26a9c2016-06-22 14:51:09 -070084 const std::unordered_set<Capability>& getCapabilities() const {
Dan Stoza651bf312015-10-23 17:03:17 -070085 return mCapabilities;
86 };
87
88 uint32_t getMaxVirtualDisplayCount() const;
89 Error createVirtualDisplay(uint32_t width, uint32_t height,
Dan Stoza5cf424b2016-05-20 14:02:39 -070090 android_pixel_format_t* format,
Dan Stoza651bf312015-10-23 17:03:17 -070091 std::shared_ptr<Display>* outDisplay);
92
93 void registerHotplugCallback(HotplugCallback hotplug);
94 void registerRefreshCallback(RefreshCallback refresh);
95 void registerVsyncCallback(VsyncCallback vsync);
96
97 // For use by callbacks
98
99 void callHotplug(std::shared_ptr<Display> display, Connection connected);
100 void callRefresh(std::shared_ptr<Display> display);
101 void callVsync(std::shared_ptr<Display> display, nsecs_t timestamp);
102
103 // Other Device methods
104
105 // This will create a Display if one is not found, but it will not be marked
Dan Stoza38628982016-07-13 15:48:58 -0700106 // as connected. This Display may be null if the display has been torn down
107 // but has not been removed from the map yet.
Dan Stoza651bf312015-10-23 17:03:17 -0700108 std::shared_ptr<Display> getDisplayById(hwc2_display_t id);
109
Dan Stoza09e7a272016-04-14 12:31:01 -0700110 bool hasCapability(HWC2::Capability capability) const;
111
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800112#ifdef BYPASS_IHWC
113 android::Hwc2::Composer* getComposer() { return nullptr; }
114#else
115 android::Hwc2::Composer* getComposer() { return mComposer.get(); }
116#endif
117
Dan Stoza651bf312015-10-23 17:03:17 -0700118private:
119 // Initialization methods
120
Chia-I Wuaab99f52016-10-05 12:59:58 +0800121#ifdef BYPASS_IHWC
Dan Stoza651bf312015-10-23 17:03:17 -0700122 template <typename PFN>
123 [[clang::warn_unused_result]] bool loadFunctionPointer(
124 FunctionDescriptor desc, PFN& outPFN) {
125 auto intDesc = static_cast<int32_t>(desc);
126 auto pfn = mHwcDevice->getFunction(mHwcDevice, intDesc);
127 if (pfn != nullptr) {
128 outPFN = reinterpret_cast<PFN>(pfn);
129 return true;
130 } else {
131 ALOGE("Failed to load function %s", to_string(desc).c_str());
132 return false;
133 }
134 }
135
136 template <typename PFN, typename HOOK>
137 void registerCallback(Callback callback, HOOK hook) {
138 static_assert(std::is_same<PFN, HOOK>::value,
139 "Incompatible function pointer");
140 auto intCallback = static_cast<int32_t>(callback);
141 auto callbackData = static_cast<hwc2_callback_data_t>(this);
142 auto pfn = reinterpret_cast<hwc2_function_pointer_t>(hook);
143 mRegisterCallback(mHwcDevice, intCallback, callbackData, pfn);
144 }
Chia-I Wuaab99f52016-10-05 12:59:58 +0800145#endif
Dan Stoza651bf312015-10-23 17:03:17 -0700146
147 void loadCapabilities();
148 void loadFunctionPointers();
149 void registerCallbacks();
150
151 // For use by Display
152
153 void destroyVirtualDisplay(hwc2_display_t display);
154
155 // Member variables
156
Chia-I Wuaab99f52016-10-05 12:59:58 +0800157#ifdef BYPASS_IHWC
Dan Stoza651bf312015-10-23 17:03:17 -0700158 hwc2_device_t* mHwcDevice;
159
160 // Device function pointers
161 HWC2_PFN_CREATE_VIRTUAL_DISPLAY mCreateVirtualDisplay;
162 HWC2_PFN_DESTROY_VIRTUAL_DISPLAY mDestroyVirtualDisplay;
163 HWC2_PFN_DUMP mDump;
164 HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT mGetMaxVirtualDisplayCount;
165 HWC2_PFN_REGISTER_CALLBACK mRegisterCallback;
166
167 // Display function pointers
168 HWC2_PFN_ACCEPT_DISPLAY_CHANGES mAcceptDisplayChanges;
169 HWC2_PFN_CREATE_LAYER mCreateLayer;
170 HWC2_PFN_DESTROY_LAYER mDestroyLayer;
171 HWC2_PFN_GET_ACTIVE_CONFIG mGetActiveConfig;
172 HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES mGetChangedCompositionTypes;
Dan Stoza076ac672016-03-14 10:47:53 -0700173 HWC2_PFN_GET_COLOR_MODES mGetColorModes;
Dan Stoza651bf312015-10-23 17:03:17 -0700174 HWC2_PFN_GET_DISPLAY_ATTRIBUTE mGetDisplayAttribute;
175 HWC2_PFN_GET_DISPLAY_CONFIGS mGetDisplayConfigs;
176 HWC2_PFN_GET_DISPLAY_NAME mGetDisplayName;
177 HWC2_PFN_GET_DISPLAY_REQUESTS mGetDisplayRequests;
178 HWC2_PFN_GET_DISPLAY_TYPE mGetDisplayType;
179 HWC2_PFN_GET_DOZE_SUPPORT mGetDozeSupport;
Dan Stoza7d7ae732016-03-16 12:23:40 -0700180 HWC2_PFN_GET_HDR_CAPABILITIES mGetHdrCapabilities;
Dan Stoza651bf312015-10-23 17:03:17 -0700181 HWC2_PFN_GET_RELEASE_FENCES mGetReleaseFences;
182 HWC2_PFN_PRESENT_DISPLAY mPresentDisplay;
183 HWC2_PFN_SET_ACTIVE_CONFIG mSetActiveConfig;
184 HWC2_PFN_SET_CLIENT_TARGET mSetClientTarget;
Dan Stoza076ac672016-03-14 10:47:53 -0700185 HWC2_PFN_SET_COLOR_MODE mSetColorMode;
Dan Stoza5df2a862016-03-24 16:19:37 -0700186 HWC2_PFN_SET_COLOR_TRANSFORM mSetColorTransform;
Dan Stoza651bf312015-10-23 17:03:17 -0700187 HWC2_PFN_SET_OUTPUT_BUFFER mSetOutputBuffer;
188 HWC2_PFN_SET_POWER_MODE mSetPowerMode;
189 HWC2_PFN_SET_VSYNC_ENABLED mSetVsyncEnabled;
190 HWC2_PFN_VALIDATE_DISPLAY mValidateDisplay;
191
192 // Layer function pointers
193 HWC2_PFN_SET_CURSOR_POSITION mSetCursorPosition;
194 HWC2_PFN_SET_LAYER_BUFFER mSetLayerBuffer;
195 HWC2_PFN_SET_LAYER_SURFACE_DAMAGE mSetLayerSurfaceDamage;
196 HWC2_PFN_SET_LAYER_BLEND_MODE mSetLayerBlendMode;
197 HWC2_PFN_SET_LAYER_COLOR mSetLayerColor;
198 HWC2_PFN_SET_LAYER_COMPOSITION_TYPE mSetLayerCompositionType;
Dan Stoza5df2a862016-03-24 16:19:37 -0700199 HWC2_PFN_SET_LAYER_DATASPACE mSetLayerDataspace;
Dan Stoza651bf312015-10-23 17:03:17 -0700200 HWC2_PFN_SET_LAYER_DISPLAY_FRAME mSetLayerDisplayFrame;
201 HWC2_PFN_SET_LAYER_PLANE_ALPHA mSetLayerPlaneAlpha;
202 HWC2_PFN_SET_LAYER_SIDEBAND_STREAM mSetLayerSidebandStream;
203 HWC2_PFN_SET_LAYER_SOURCE_CROP mSetLayerSourceCrop;
204 HWC2_PFN_SET_LAYER_TRANSFORM mSetLayerTransform;
205 HWC2_PFN_SET_LAYER_VISIBLE_REGION mSetLayerVisibleRegion;
206 HWC2_PFN_SET_LAYER_Z_ORDER mSetLayerZOrder;
Chia-I Wuaab99f52016-10-05 12:59:58 +0800207#else
208 std::unique_ptr<android::Hwc2::Composer> mComposer;
209#endif // BYPASS_IHWC
Dan Stoza651bf312015-10-23 17:03:17 -0700210
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700211 std::unordered_set<Capability> mCapabilities;
Dan Stoza38628982016-07-13 15:48:58 -0700212 std::unordered_map<hwc2_display_t, std::weak_ptr<Display>> mDisplays;
Dan Stoza651bf312015-10-23 17:03:17 -0700213
214 HotplugCallback mHotplug;
215 std::vector<std::pair<std::shared_ptr<Display>, Connection>>
216 mPendingHotplugs;
217 RefreshCallback mRefresh;
218 std::vector<std::shared_ptr<Display>> mPendingRefreshes;
219 VsyncCallback mVsync;
220 std::vector<std::pair<std::shared_ptr<Display>, nsecs_t>> mPendingVsyncs;
221};
222
Fabien Sanglard33960702016-11-29 17:58:21 -0800223// Convenience C++ class to access hwc2_device_t Display functions directly.
Dan Stoza651bf312015-10-23 17:03:17 -0700224class Display : public std::enable_shared_from_this<Display>
225{
226public:
227 Display(Device& device, hwc2_display_t id);
228 ~Display();
229
230 friend class HWC2::Device;
231 friend class HWC2::Layer;
232
233 class Config
234 {
235 public:
236 class Builder
237 {
238 public:
239 Builder(Display& display, hwc2_config_t id);
240
241 std::shared_ptr<const Config> build() {
242 return std::const_pointer_cast<const Config>(
243 std::move(mConfig));
244 }
245
246 Builder& setWidth(int32_t width) {
247 mConfig->mWidth = width;
248 return *this;
249 }
250 Builder& setHeight(int32_t height) {
251 mConfig->mHeight = height;
252 return *this;
253 }
254 Builder& setVsyncPeriod(int32_t vsyncPeriod) {
255 mConfig->mVsyncPeriod = vsyncPeriod;
256 return *this;
257 }
258 Builder& setDpiX(int32_t dpiX) {
259 if (dpiX == -1) {
260 mConfig->mDpiX = getDefaultDensity();
261 } else {
262 mConfig->mDpiX = dpiX / 1000.0f;
263 }
264 return *this;
265 }
266 Builder& setDpiY(int32_t dpiY) {
267 if (dpiY == -1) {
268 mConfig->mDpiY = getDefaultDensity();
269 } else {
270 mConfig->mDpiY = dpiY / 1000.0f;
271 }
272 return *this;
273 }
274
275 private:
276 float getDefaultDensity();
277 std::shared_ptr<Config> mConfig;
278 };
279
280 hwc2_display_t getDisplayId() const { return mDisplay.getId(); }
281 hwc2_config_t getId() const { return mId; }
282
283 int32_t getWidth() const { return mWidth; }
284 int32_t getHeight() const { return mHeight; }
285 nsecs_t getVsyncPeriod() const { return mVsyncPeriod; }
286 float getDpiX() const { return mDpiX; }
287 float getDpiY() const { return mDpiY; }
288
289 private:
290 Config(Display& display, hwc2_config_t id);
291
292 Display& mDisplay;
293 hwc2_config_t mId;
294
295 int32_t mWidth;
296 int32_t mHeight;
297 nsecs_t mVsyncPeriod;
298 float mDpiX;
299 float mDpiY;
300 };
301
302 // Required by HWC2
303
304 [[clang::warn_unused_result]] Error acceptChanges();
305 [[clang::warn_unused_result]] Error createLayer(
306 std::shared_ptr<Layer>* outLayer);
307 [[clang::warn_unused_result]] Error getActiveConfig(
308 std::shared_ptr<const Config>* outConfig) const;
309 [[clang::warn_unused_result]] Error getChangedCompositionTypes(
310 std::unordered_map<std::shared_ptr<Layer>, Composition>* outTypes);
Dan Stoza076ac672016-03-14 10:47:53 -0700311 [[clang::warn_unused_result]] Error getColorModes(
Michael Wright28f24d02016-07-12 13:30:53 -0700312 std::vector<android_color_mode_t>* outModes) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700313
314 // Doesn't call into the HWC2 device, so no errors are possible
315 std::vector<std::shared_ptr<const Config>> getConfigs() const;
316
317 [[clang::warn_unused_result]] Error getName(std::string* outName) const;
318 [[clang::warn_unused_result]] Error getRequests(
319 DisplayRequest* outDisplayRequests,
320 std::unordered_map<std::shared_ptr<Layer>, LayerRequest>*
321 outLayerRequests);
322 [[clang::warn_unused_result]] Error getType(DisplayType* outType) const;
323 [[clang::warn_unused_result]] Error supportsDoze(bool* outSupport) const;
Dan Stoza7d7ae732016-03-16 12:23:40 -0700324 [[clang::warn_unused_result]] Error getHdrCapabilities(
325 std::unique_ptr<android::HdrCapabilities>* outCapabilities) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700326 [[clang::warn_unused_result]] Error getReleaseFences(
327 std::unordered_map<std::shared_ptr<Layer>,
328 android::sp<android::Fence>>* outFences) const;
329 [[clang::warn_unused_result]] Error present(
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800330 android::sp<android::Fence>* outPresentFence);
Dan Stoza651bf312015-10-23 17:03:17 -0700331 [[clang::warn_unused_result]] Error setActiveConfig(
332 const std::shared_ptr<const Config>& config);
333 [[clang::warn_unused_result]] Error setClientTarget(
Chia-I Wu06d63de2017-01-04 14:58:51 +0800334 uint32_t slot, buffer_handle_t target,
Dan Stoza651bf312015-10-23 17:03:17 -0700335 const android::sp<android::Fence>& acquireFence,
336 android_dataspace_t dataspace);
Michael Wright28f24d02016-07-12 13:30:53 -0700337 [[clang::warn_unused_result]] Error setColorMode(android_color_mode_t mode);
Dan Stoza5df2a862016-03-24 16:19:37 -0700338 [[clang::warn_unused_result]] Error setColorTransform(
339 const android::mat4& matrix, android_color_transform_t hint);
Dan Stoza651bf312015-10-23 17:03:17 -0700340 [[clang::warn_unused_result]] Error setOutputBuffer(
341 const android::sp<android::GraphicBuffer>& buffer,
342 const android::sp<android::Fence>& releaseFence);
343 [[clang::warn_unused_result]] Error setPowerMode(PowerMode mode);
344 [[clang::warn_unused_result]] Error setVsyncEnabled(Vsync enabled);
345 [[clang::warn_unused_result]] Error validate(uint32_t* outNumTypes,
346 uint32_t* outNumRequests);
347
348 // Other Display methods
349
350 Device& getDevice() const { return mDevice; }
351 hwc2_display_t getId() const { return mId; }
352 bool isConnected() const { return mIsConnected; }
353
354private:
355 // For use by Device
356
357 // Virtual displays are always connected
358 void setVirtual() {
359 mIsVirtual = true;
360 mIsConnected = true;
361 }
362
363 void setConnected(bool connected) { mIsConnected = connected; }
364 int32_t getAttribute(hwc2_config_t configId, Attribute attribute);
365 void loadConfig(hwc2_config_t configId);
366 void loadConfigs();
367
368 // For use by Layer
369 void destroyLayer(hwc2_layer_t layerId);
370
371 // This may fail (and return a null pointer) if no layer with this ID exists
372 // on this display
373 std::shared_ptr<Layer> getLayerById(hwc2_layer_t id) const;
374
375 // Member variables
376
377 Device& mDevice;
378 hwc2_display_t mId;
379 bool mIsConnected;
380 bool mIsVirtual;
381 std::unordered_map<hwc2_layer_t, std::weak_ptr<Layer>> mLayers;
382 std::unordered_map<hwc2_config_t, std::shared_ptr<const Config>> mConfigs;
383};
384
Fabien Sanglard33960702016-11-29 17:58:21 -0800385// Convenience C++ class to access hwc2_device_t Layer functions directly.
Dan Stoza651bf312015-10-23 17:03:17 -0700386class Layer
387{
388public:
389 Layer(const std::shared_ptr<Display>& display, hwc2_layer_t id);
390 ~Layer();
391
392 bool isAbandoned() const { return mDisplay.expired(); }
393 hwc2_layer_t getId() const { return mId; }
394
395 [[clang::warn_unused_result]] Error setCursorPosition(int32_t x, int32_t y);
Chia-I Wu06d63de2017-01-04 14:58:51 +0800396 [[clang::warn_unused_result]] Error setBuffer(uint32_t slot,
397 buffer_handle_t buffer,
Dan Stoza651bf312015-10-23 17:03:17 -0700398 const android::sp<android::Fence>& acquireFence);
399 [[clang::warn_unused_result]] Error setSurfaceDamage(
400 const android::Region& damage);
401
402 [[clang::warn_unused_result]] Error setBlendMode(BlendMode mode);
403 [[clang::warn_unused_result]] Error setColor(hwc_color_t color);
404 [[clang::warn_unused_result]] Error setCompositionType(Composition type);
Dan Stoza5df2a862016-03-24 16:19:37 -0700405 [[clang::warn_unused_result]] Error setDataspace(
406 android_dataspace_t dataspace);
Dan Stoza651bf312015-10-23 17:03:17 -0700407 [[clang::warn_unused_result]] Error setDisplayFrame(
408 const android::Rect& frame);
409 [[clang::warn_unused_result]] Error setPlaneAlpha(float alpha);
410 [[clang::warn_unused_result]] Error setSidebandStream(
411 const native_handle_t* stream);
412 [[clang::warn_unused_result]] Error setSourceCrop(
Dan Stoza71bded52016-10-19 11:10:33 -0700413 const android::gfx::FloatRect& crop);
Dan Stoza651bf312015-10-23 17:03:17 -0700414 [[clang::warn_unused_result]] Error setTransform(Transform transform);
415 [[clang::warn_unused_result]] Error setVisibleRegion(
416 const android::Region& region);
417 [[clang::warn_unused_result]] Error setZOrder(uint32_t z);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500418 [[clang::warn_unused_result]] Error setInfo(uint32_t type, uint32_t appId);
Dan Stoza651bf312015-10-23 17:03:17 -0700419
420private:
421 std::weak_ptr<Display> mDisplay;
422 hwc2_display_t mDisplayId;
423 Device& mDevice;
424 hwc2_layer_t mId;
425};
426
427} // namespace HWC2
428
429#endif // ANDROID_SF_HWC2_H