blob: 42b79bb0d7516cf6b28d2a8127fce9874e88ea44 [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;
41 class FloatRect;
42 class GraphicBuffer;
43 class Rect;
44 class Region;
Chia-I Wuaab99f52016-10-05 12:59:58 +080045 namespace Hwc2 {
46 class Composer;
47 };
Dan Stoza651bf312015-10-23 17:03:17 -070048}
49
50namespace HWC2 {
51
52class Display;
53class Layer;
54
55typedef std::function<void(std::shared_ptr<Display>, Connection)>
56 HotplugCallback;
57typedef std::function<void(std::shared_ptr<Display>)> RefreshCallback;
58typedef std::function<void(std::shared_ptr<Display>, nsecs_t)> VsyncCallback;
59
Fabien Sanglard33960702016-11-29 17:58:21 -080060// C++ Wrapper around hwc2_device_t. Load all functions pointers
61// and handle callback registration.
Dan Stoza651bf312015-10-23 17:03:17 -070062class Device
63{
64public:
Chia-I Wuaab99f52016-10-05 12:59:58 +080065#ifdef BYPASS_IHWC
Chih-Hung Hsieh342b7602016-09-01 11:34:16 -070066 explicit Device(hwc2_device_t* device);
Chia-I Wuaab99f52016-10-05 12:59:58 +080067#else
68 Device();
69#endif
Dan Stoza651bf312015-10-23 17:03:17 -070070 ~Device();
71
72 friend class HWC2::Display;
73 friend class HWC2::Layer;
74
75 // Required by HWC2
76
77 std::string dump() const;
78
Dan Stoza9f26a9c2016-06-22 14:51:09 -070079 const std::unordered_set<Capability>& getCapabilities() const {
Dan Stoza651bf312015-10-23 17:03:17 -070080 return mCapabilities;
81 };
82
83 uint32_t getMaxVirtualDisplayCount() const;
84 Error createVirtualDisplay(uint32_t width, uint32_t height,
Dan Stoza5cf424b2016-05-20 14:02:39 -070085 android_pixel_format_t* format,
Dan Stoza651bf312015-10-23 17:03:17 -070086 std::shared_ptr<Display>* outDisplay);
87
88 void registerHotplugCallback(HotplugCallback hotplug);
89 void registerRefreshCallback(RefreshCallback refresh);
90 void registerVsyncCallback(VsyncCallback vsync);
91
92 // For use by callbacks
93
94 void callHotplug(std::shared_ptr<Display> display, Connection connected);
95 void callRefresh(std::shared_ptr<Display> display);
96 void callVsync(std::shared_ptr<Display> display, nsecs_t timestamp);
97
98 // Other Device methods
99
100 // This will create a Display if one is not found, but it will not be marked
Dan Stoza38628982016-07-13 15:48:58 -0700101 // as connected. This Display may be null if the display has been torn down
102 // but has not been removed from the map yet.
Dan Stoza651bf312015-10-23 17:03:17 -0700103 std::shared_ptr<Display> getDisplayById(hwc2_display_t id);
104
Dan Stoza09e7a272016-04-14 12:31:01 -0700105 bool hasCapability(HWC2::Capability capability) const;
106
Dan Stoza651bf312015-10-23 17:03:17 -0700107private:
108 // Initialization methods
109
Chia-I Wuaab99f52016-10-05 12:59:58 +0800110#ifdef BYPASS_IHWC
Dan Stoza651bf312015-10-23 17:03:17 -0700111 template <typename PFN>
112 [[clang::warn_unused_result]] bool loadFunctionPointer(
113 FunctionDescriptor desc, PFN& outPFN) {
114 auto intDesc = static_cast<int32_t>(desc);
115 auto pfn = mHwcDevice->getFunction(mHwcDevice, intDesc);
116 if (pfn != nullptr) {
117 outPFN = reinterpret_cast<PFN>(pfn);
118 return true;
119 } else {
120 ALOGE("Failed to load function %s", to_string(desc).c_str());
121 return false;
122 }
123 }
124
125 template <typename PFN, typename HOOK>
126 void registerCallback(Callback callback, HOOK hook) {
127 static_assert(std::is_same<PFN, HOOK>::value,
128 "Incompatible function pointer");
129 auto intCallback = static_cast<int32_t>(callback);
130 auto callbackData = static_cast<hwc2_callback_data_t>(this);
131 auto pfn = reinterpret_cast<hwc2_function_pointer_t>(hook);
132 mRegisterCallback(mHwcDevice, intCallback, callbackData, pfn);
133 }
Chia-I Wuaab99f52016-10-05 12:59:58 +0800134#endif
Dan Stoza651bf312015-10-23 17:03:17 -0700135
136 void loadCapabilities();
137 void loadFunctionPointers();
138 void registerCallbacks();
139
140 // For use by Display
141
142 void destroyVirtualDisplay(hwc2_display_t display);
143
144 // Member variables
145
Chia-I Wuaab99f52016-10-05 12:59:58 +0800146#ifdef BYPASS_IHWC
Dan Stoza651bf312015-10-23 17:03:17 -0700147 hwc2_device_t* mHwcDevice;
148
149 // Device function pointers
150 HWC2_PFN_CREATE_VIRTUAL_DISPLAY mCreateVirtualDisplay;
151 HWC2_PFN_DESTROY_VIRTUAL_DISPLAY mDestroyVirtualDisplay;
152 HWC2_PFN_DUMP mDump;
153 HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT mGetMaxVirtualDisplayCount;
154 HWC2_PFN_REGISTER_CALLBACK mRegisterCallback;
155
156 // Display function pointers
157 HWC2_PFN_ACCEPT_DISPLAY_CHANGES mAcceptDisplayChanges;
158 HWC2_PFN_CREATE_LAYER mCreateLayer;
159 HWC2_PFN_DESTROY_LAYER mDestroyLayer;
160 HWC2_PFN_GET_ACTIVE_CONFIG mGetActiveConfig;
161 HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES mGetChangedCompositionTypes;
Dan Stoza076ac672016-03-14 10:47:53 -0700162 HWC2_PFN_GET_COLOR_MODES mGetColorModes;
Dan Stoza651bf312015-10-23 17:03:17 -0700163 HWC2_PFN_GET_DISPLAY_ATTRIBUTE mGetDisplayAttribute;
164 HWC2_PFN_GET_DISPLAY_CONFIGS mGetDisplayConfigs;
165 HWC2_PFN_GET_DISPLAY_NAME mGetDisplayName;
166 HWC2_PFN_GET_DISPLAY_REQUESTS mGetDisplayRequests;
167 HWC2_PFN_GET_DISPLAY_TYPE mGetDisplayType;
168 HWC2_PFN_GET_DOZE_SUPPORT mGetDozeSupport;
Dan Stoza7d7ae732016-03-16 12:23:40 -0700169 HWC2_PFN_GET_HDR_CAPABILITIES mGetHdrCapabilities;
Dan Stoza651bf312015-10-23 17:03:17 -0700170 HWC2_PFN_GET_RELEASE_FENCES mGetReleaseFences;
171 HWC2_PFN_PRESENT_DISPLAY mPresentDisplay;
172 HWC2_PFN_SET_ACTIVE_CONFIG mSetActiveConfig;
173 HWC2_PFN_SET_CLIENT_TARGET mSetClientTarget;
Dan Stoza076ac672016-03-14 10:47:53 -0700174 HWC2_PFN_SET_COLOR_MODE mSetColorMode;
Dan Stoza5df2a862016-03-24 16:19:37 -0700175 HWC2_PFN_SET_COLOR_TRANSFORM mSetColorTransform;
Dan Stoza651bf312015-10-23 17:03:17 -0700176 HWC2_PFN_SET_OUTPUT_BUFFER mSetOutputBuffer;
177 HWC2_PFN_SET_POWER_MODE mSetPowerMode;
178 HWC2_PFN_SET_VSYNC_ENABLED mSetVsyncEnabled;
179 HWC2_PFN_VALIDATE_DISPLAY mValidateDisplay;
180
181 // Layer function pointers
182 HWC2_PFN_SET_CURSOR_POSITION mSetCursorPosition;
183 HWC2_PFN_SET_LAYER_BUFFER mSetLayerBuffer;
184 HWC2_PFN_SET_LAYER_SURFACE_DAMAGE mSetLayerSurfaceDamage;
185 HWC2_PFN_SET_LAYER_BLEND_MODE mSetLayerBlendMode;
186 HWC2_PFN_SET_LAYER_COLOR mSetLayerColor;
187 HWC2_PFN_SET_LAYER_COMPOSITION_TYPE mSetLayerCompositionType;
Dan Stoza5df2a862016-03-24 16:19:37 -0700188 HWC2_PFN_SET_LAYER_DATASPACE mSetLayerDataspace;
Dan Stoza651bf312015-10-23 17:03:17 -0700189 HWC2_PFN_SET_LAYER_DISPLAY_FRAME mSetLayerDisplayFrame;
190 HWC2_PFN_SET_LAYER_PLANE_ALPHA mSetLayerPlaneAlpha;
191 HWC2_PFN_SET_LAYER_SIDEBAND_STREAM mSetLayerSidebandStream;
192 HWC2_PFN_SET_LAYER_SOURCE_CROP mSetLayerSourceCrop;
193 HWC2_PFN_SET_LAYER_TRANSFORM mSetLayerTransform;
194 HWC2_PFN_SET_LAYER_VISIBLE_REGION mSetLayerVisibleRegion;
195 HWC2_PFN_SET_LAYER_Z_ORDER mSetLayerZOrder;
Chia-I Wuaab99f52016-10-05 12:59:58 +0800196#else
197 std::unique_ptr<android::Hwc2::Composer> mComposer;
198#endif // BYPASS_IHWC
Dan Stoza651bf312015-10-23 17:03:17 -0700199
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700200 std::unordered_set<Capability> mCapabilities;
Dan Stoza38628982016-07-13 15:48:58 -0700201 std::unordered_map<hwc2_display_t, std::weak_ptr<Display>> mDisplays;
Dan Stoza651bf312015-10-23 17:03:17 -0700202
203 HotplugCallback mHotplug;
204 std::vector<std::pair<std::shared_ptr<Display>, Connection>>
205 mPendingHotplugs;
206 RefreshCallback mRefresh;
207 std::vector<std::shared_ptr<Display>> mPendingRefreshes;
208 VsyncCallback mVsync;
209 std::vector<std::pair<std::shared_ptr<Display>, nsecs_t>> mPendingVsyncs;
210};
211
Fabien Sanglard33960702016-11-29 17:58:21 -0800212// Convenience C++ class to access hwc2_device_t Display functions directly.
Dan Stoza651bf312015-10-23 17:03:17 -0700213class Display : public std::enable_shared_from_this<Display>
214{
215public:
216 Display(Device& device, hwc2_display_t id);
217 ~Display();
218
219 friend class HWC2::Device;
220 friend class HWC2::Layer;
221
222 class Config
223 {
224 public:
225 class Builder
226 {
227 public:
228 Builder(Display& display, hwc2_config_t id);
229
230 std::shared_ptr<const Config> build() {
231 return std::const_pointer_cast<const Config>(
232 std::move(mConfig));
233 }
234
235 Builder& setWidth(int32_t width) {
236 mConfig->mWidth = width;
237 return *this;
238 }
239 Builder& setHeight(int32_t height) {
240 mConfig->mHeight = height;
241 return *this;
242 }
243 Builder& setVsyncPeriod(int32_t vsyncPeriod) {
244 mConfig->mVsyncPeriod = vsyncPeriod;
245 return *this;
246 }
247 Builder& setDpiX(int32_t dpiX) {
248 if (dpiX == -1) {
249 mConfig->mDpiX = getDefaultDensity();
250 } else {
251 mConfig->mDpiX = dpiX / 1000.0f;
252 }
253 return *this;
254 }
255 Builder& setDpiY(int32_t dpiY) {
256 if (dpiY == -1) {
257 mConfig->mDpiY = getDefaultDensity();
258 } else {
259 mConfig->mDpiY = dpiY / 1000.0f;
260 }
261 return *this;
262 }
263
264 private:
265 float getDefaultDensity();
266 std::shared_ptr<Config> mConfig;
267 };
268
269 hwc2_display_t getDisplayId() const { return mDisplay.getId(); }
270 hwc2_config_t getId() const { return mId; }
271
272 int32_t getWidth() const { return mWidth; }
273 int32_t getHeight() const { return mHeight; }
274 nsecs_t getVsyncPeriod() const { return mVsyncPeriod; }
275 float getDpiX() const { return mDpiX; }
276 float getDpiY() const { return mDpiY; }
277
278 private:
279 Config(Display& display, hwc2_config_t id);
280
281 Display& mDisplay;
282 hwc2_config_t mId;
283
284 int32_t mWidth;
285 int32_t mHeight;
286 nsecs_t mVsyncPeriod;
287 float mDpiX;
288 float mDpiY;
289 };
290
291 // Required by HWC2
292
293 [[clang::warn_unused_result]] Error acceptChanges();
294 [[clang::warn_unused_result]] Error createLayer(
295 std::shared_ptr<Layer>* outLayer);
296 [[clang::warn_unused_result]] Error getActiveConfig(
297 std::shared_ptr<const Config>* outConfig) const;
298 [[clang::warn_unused_result]] Error getChangedCompositionTypes(
299 std::unordered_map<std::shared_ptr<Layer>, Composition>* outTypes);
Dan Stoza076ac672016-03-14 10:47:53 -0700300 [[clang::warn_unused_result]] Error getColorModes(
Michael Wright28f24d02016-07-12 13:30:53 -0700301 std::vector<android_color_mode_t>* outModes) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700302
303 // Doesn't call into the HWC2 device, so no errors are possible
304 std::vector<std::shared_ptr<const Config>> getConfigs() const;
305
306 [[clang::warn_unused_result]] Error getName(std::string* outName) const;
307 [[clang::warn_unused_result]] Error getRequests(
308 DisplayRequest* outDisplayRequests,
309 std::unordered_map<std::shared_ptr<Layer>, LayerRequest>*
310 outLayerRequests);
311 [[clang::warn_unused_result]] Error getType(DisplayType* outType) const;
312 [[clang::warn_unused_result]] Error supportsDoze(bool* outSupport) const;
Dan Stoza7d7ae732016-03-16 12:23:40 -0700313 [[clang::warn_unused_result]] Error getHdrCapabilities(
314 std::unique_ptr<android::HdrCapabilities>* outCapabilities) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700315 [[clang::warn_unused_result]] Error getReleaseFences(
316 std::unordered_map<std::shared_ptr<Layer>,
317 android::sp<android::Fence>>* outFences) const;
318 [[clang::warn_unused_result]] Error present(
319 android::sp<android::Fence>* outRetireFence);
320 [[clang::warn_unused_result]] Error setActiveConfig(
321 const std::shared_ptr<const Config>& config);
322 [[clang::warn_unused_result]] Error setClientTarget(
323 buffer_handle_t target,
324 const android::sp<android::Fence>& acquireFence,
325 android_dataspace_t dataspace);
Michael Wright28f24d02016-07-12 13:30:53 -0700326 [[clang::warn_unused_result]] Error setColorMode(android_color_mode_t mode);
Dan Stoza5df2a862016-03-24 16:19:37 -0700327 [[clang::warn_unused_result]] Error setColorTransform(
328 const android::mat4& matrix, android_color_transform_t hint);
Dan Stoza651bf312015-10-23 17:03:17 -0700329 [[clang::warn_unused_result]] Error setOutputBuffer(
330 const android::sp<android::GraphicBuffer>& buffer,
331 const android::sp<android::Fence>& releaseFence);
332 [[clang::warn_unused_result]] Error setPowerMode(PowerMode mode);
333 [[clang::warn_unused_result]] Error setVsyncEnabled(Vsync enabled);
334 [[clang::warn_unused_result]] Error validate(uint32_t* outNumTypes,
335 uint32_t* outNumRequests);
336
337 // Other Display methods
338
339 Device& getDevice() const { return mDevice; }
340 hwc2_display_t getId() const { return mId; }
341 bool isConnected() const { return mIsConnected; }
342
343private:
344 // For use by Device
345
346 // Virtual displays are always connected
347 void setVirtual() {
348 mIsVirtual = true;
349 mIsConnected = true;
350 }
351
352 void setConnected(bool connected) { mIsConnected = connected; }
353 int32_t getAttribute(hwc2_config_t configId, Attribute attribute);
354 void loadConfig(hwc2_config_t configId);
355 void loadConfigs();
356
357 // For use by Layer
358 void destroyLayer(hwc2_layer_t layerId);
359
360 // This may fail (and return a null pointer) if no layer with this ID exists
361 // on this display
362 std::shared_ptr<Layer> getLayerById(hwc2_layer_t id) const;
363
364 // Member variables
365
366 Device& mDevice;
367 hwc2_display_t mId;
368 bool mIsConnected;
369 bool mIsVirtual;
370 std::unordered_map<hwc2_layer_t, std::weak_ptr<Layer>> mLayers;
371 std::unordered_map<hwc2_config_t, std::shared_ptr<const Config>> mConfigs;
372};
373
Fabien Sanglard33960702016-11-29 17:58:21 -0800374// Convenience C++ class to access hwc2_device_t Layer functions directly.
Dan Stoza651bf312015-10-23 17:03:17 -0700375class Layer
376{
377public:
378 Layer(const std::shared_ptr<Display>& display, hwc2_layer_t id);
379 ~Layer();
380
381 bool isAbandoned() const { return mDisplay.expired(); }
382 hwc2_layer_t getId() const { return mId; }
383
384 [[clang::warn_unused_result]] Error setCursorPosition(int32_t x, int32_t y);
385 [[clang::warn_unused_result]] Error setBuffer(buffer_handle_t buffer,
386 const android::sp<android::Fence>& acquireFence);
387 [[clang::warn_unused_result]] Error setSurfaceDamage(
388 const android::Region& damage);
389
390 [[clang::warn_unused_result]] Error setBlendMode(BlendMode mode);
391 [[clang::warn_unused_result]] Error setColor(hwc_color_t color);
392 [[clang::warn_unused_result]] Error setCompositionType(Composition type);
Dan Stoza5df2a862016-03-24 16:19:37 -0700393 [[clang::warn_unused_result]] Error setDataspace(
394 android_dataspace_t dataspace);
Dan Stoza651bf312015-10-23 17:03:17 -0700395 [[clang::warn_unused_result]] Error setDisplayFrame(
396 const android::Rect& frame);
397 [[clang::warn_unused_result]] Error setPlaneAlpha(float alpha);
398 [[clang::warn_unused_result]] Error setSidebandStream(
399 const native_handle_t* stream);
400 [[clang::warn_unused_result]] Error setSourceCrop(
401 const android::FloatRect& crop);
402 [[clang::warn_unused_result]] Error setTransform(Transform transform);
403 [[clang::warn_unused_result]] Error setVisibleRegion(
404 const android::Region& region);
405 [[clang::warn_unused_result]] Error setZOrder(uint32_t z);
406
407private:
408 std::weak_ptr<Display> mDisplay;
409 hwc2_display_t mDisplayId;
410 Device& mDevice;
411 hwc2_layer_t mId;
412};
413
414} // namespace HWC2
415
416#endif // ANDROID_SF_HWC2_H