blob: d770f22ed331a9e6472aa6e055ed296ef62dea20 [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
70 Device();
71#endif
Dan Stoza651bf312015-10-23 17:03:17 -070072 ~Device();
73
74 friend class HWC2::Display;
75 friend class HWC2::Layer;
76
77 // Required by HWC2
78
79 std::string dump() const;
80
Dan Stoza9f26a9c2016-06-22 14:51:09 -070081 const std::unordered_set<Capability>& getCapabilities() const {
Dan Stoza651bf312015-10-23 17:03:17 -070082 return mCapabilities;
83 };
84
85 uint32_t getMaxVirtualDisplayCount() const;
86 Error createVirtualDisplay(uint32_t width, uint32_t height,
Dan Stoza5cf424b2016-05-20 14:02:39 -070087 android_pixel_format_t* format,
Dan Stoza651bf312015-10-23 17:03:17 -070088 std::shared_ptr<Display>* outDisplay);
89
90 void registerHotplugCallback(HotplugCallback hotplug);
91 void registerRefreshCallback(RefreshCallback refresh);
92 void registerVsyncCallback(VsyncCallback vsync);
93
94 // For use by callbacks
95
96 void callHotplug(std::shared_ptr<Display> display, Connection connected);
97 void callRefresh(std::shared_ptr<Display> display);
98 void callVsync(std::shared_ptr<Display> display, nsecs_t timestamp);
99
100 // Other Device methods
101
102 // This will create a Display if one is not found, but it will not be marked
Dan Stoza38628982016-07-13 15:48:58 -0700103 // as connected. This Display may be null if the display has been torn down
104 // but has not been removed from the map yet.
Dan Stoza651bf312015-10-23 17:03:17 -0700105 std::shared_ptr<Display> getDisplayById(hwc2_display_t id);
106
Dan Stoza09e7a272016-04-14 12:31:01 -0700107 bool hasCapability(HWC2::Capability capability) const;
108
Dan Stoza651bf312015-10-23 17:03:17 -0700109private:
110 // Initialization methods
111
Chia-I Wuaab99f52016-10-05 12:59:58 +0800112#ifdef BYPASS_IHWC
Dan Stoza651bf312015-10-23 17:03:17 -0700113 template <typename PFN>
114 [[clang::warn_unused_result]] bool loadFunctionPointer(
115 FunctionDescriptor desc, PFN& outPFN) {
116 auto intDesc = static_cast<int32_t>(desc);
117 auto pfn = mHwcDevice->getFunction(mHwcDevice, intDesc);
118 if (pfn != nullptr) {
119 outPFN = reinterpret_cast<PFN>(pfn);
120 return true;
121 } else {
122 ALOGE("Failed to load function %s", to_string(desc).c_str());
123 return false;
124 }
125 }
126
127 template <typename PFN, typename HOOK>
128 void registerCallback(Callback callback, HOOK hook) {
129 static_assert(std::is_same<PFN, HOOK>::value,
130 "Incompatible function pointer");
131 auto intCallback = static_cast<int32_t>(callback);
132 auto callbackData = static_cast<hwc2_callback_data_t>(this);
133 auto pfn = reinterpret_cast<hwc2_function_pointer_t>(hook);
134 mRegisterCallback(mHwcDevice, intCallback, callbackData, pfn);
135 }
Chia-I Wuaab99f52016-10-05 12:59:58 +0800136#endif
Dan Stoza651bf312015-10-23 17:03:17 -0700137
138 void loadCapabilities();
139 void loadFunctionPointers();
140 void registerCallbacks();
141
142 // For use by Display
143
144 void destroyVirtualDisplay(hwc2_display_t display);
145
146 // Member variables
147
Chia-I Wuaab99f52016-10-05 12:59:58 +0800148#ifdef BYPASS_IHWC
Dan Stoza651bf312015-10-23 17:03:17 -0700149 hwc2_device_t* mHwcDevice;
150
151 // Device function pointers
152 HWC2_PFN_CREATE_VIRTUAL_DISPLAY mCreateVirtualDisplay;
153 HWC2_PFN_DESTROY_VIRTUAL_DISPLAY mDestroyVirtualDisplay;
154 HWC2_PFN_DUMP mDump;
155 HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT mGetMaxVirtualDisplayCount;
156 HWC2_PFN_REGISTER_CALLBACK mRegisterCallback;
157
158 // Display function pointers
159 HWC2_PFN_ACCEPT_DISPLAY_CHANGES mAcceptDisplayChanges;
160 HWC2_PFN_CREATE_LAYER mCreateLayer;
161 HWC2_PFN_DESTROY_LAYER mDestroyLayer;
162 HWC2_PFN_GET_ACTIVE_CONFIG mGetActiveConfig;
163 HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES mGetChangedCompositionTypes;
Dan Stoza076ac672016-03-14 10:47:53 -0700164 HWC2_PFN_GET_COLOR_MODES mGetColorModes;
Dan Stoza651bf312015-10-23 17:03:17 -0700165 HWC2_PFN_GET_DISPLAY_ATTRIBUTE mGetDisplayAttribute;
166 HWC2_PFN_GET_DISPLAY_CONFIGS mGetDisplayConfigs;
167 HWC2_PFN_GET_DISPLAY_NAME mGetDisplayName;
168 HWC2_PFN_GET_DISPLAY_REQUESTS mGetDisplayRequests;
169 HWC2_PFN_GET_DISPLAY_TYPE mGetDisplayType;
170 HWC2_PFN_GET_DOZE_SUPPORT mGetDozeSupport;
Dan Stoza7d7ae732016-03-16 12:23:40 -0700171 HWC2_PFN_GET_HDR_CAPABILITIES mGetHdrCapabilities;
Dan Stoza651bf312015-10-23 17:03:17 -0700172 HWC2_PFN_GET_RELEASE_FENCES mGetReleaseFences;
173 HWC2_PFN_PRESENT_DISPLAY mPresentDisplay;
174 HWC2_PFN_SET_ACTIVE_CONFIG mSetActiveConfig;
175 HWC2_PFN_SET_CLIENT_TARGET mSetClientTarget;
Dan Stoza076ac672016-03-14 10:47:53 -0700176 HWC2_PFN_SET_COLOR_MODE mSetColorMode;
Dan Stoza5df2a862016-03-24 16:19:37 -0700177 HWC2_PFN_SET_COLOR_TRANSFORM mSetColorTransform;
Dan Stoza651bf312015-10-23 17:03:17 -0700178 HWC2_PFN_SET_OUTPUT_BUFFER mSetOutputBuffer;
179 HWC2_PFN_SET_POWER_MODE mSetPowerMode;
180 HWC2_PFN_SET_VSYNC_ENABLED mSetVsyncEnabled;
181 HWC2_PFN_VALIDATE_DISPLAY mValidateDisplay;
182
183 // Layer function pointers
184 HWC2_PFN_SET_CURSOR_POSITION mSetCursorPosition;
185 HWC2_PFN_SET_LAYER_BUFFER mSetLayerBuffer;
186 HWC2_PFN_SET_LAYER_SURFACE_DAMAGE mSetLayerSurfaceDamage;
187 HWC2_PFN_SET_LAYER_BLEND_MODE mSetLayerBlendMode;
188 HWC2_PFN_SET_LAYER_COLOR mSetLayerColor;
189 HWC2_PFN_SET_LAYER_COMPOSITION_TYPE mSetLayerCompositionType;
Dan Stoza5df2a862016-03-24 16:19:37 -0700190 HWC2_PFN_SET_LAYER_DATASPACE mSetLayerDataspace;
Dan Stoza651bf312015-10-23 17:03:17 -0700191 HWC2_PFN_SET_LAYER_DISPLAY_FRAME mSetLayerDisplayFrame;
192 HWC2_PFN_SET_LAYER_PLANE_ALPHA mSetLayerPlaneAlpha;
193 HWC2_PFN_SET_LAYER_SIDEBAND_STREAM mSetLayerSidebandStream;
194 HWC2_PFN_SET_LAYER_SOURCE_CROP mSetLayerSourceCrop;
195 HWC2_PFN_SET_LAYER_TRANSFORM mSetLayerTransform;
196 HWC2_PFN_SET_LAYER_VISIBLE_REGION mSetLayerVisibleRegion;
197 HWC2_PFN_SET_LAYER_Z_ORDER mSetLayerZOrder;
Chia-I Wuaab99f52016-10-05 12:59:58 +0800198#else
199 std::unique_ptr<android::Hwc2::Composer> mComposer;
200#endif // BYPASS_IHWC
Dan Stoza651bf312015-10-23 17:03:17 -0700201
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700202 std::unordered_set<Capability> mCapabilities;
Dan Stoza38628982016-07-13 15:48:58 -0700203 std::unordered_map<hwc2_display_t, std::weak_ptr<Display>> mDisplays;
Dan Stoza651bf312015-10-23 17:03:17 -0700204
205 HotplugCallback mHotplug;
206 std::vector<std::pair<std::shared_ptr<Display>, Connection>>
207 mPendingHotplugs;
208 RefreshCallback mRefresh;
209 std::vector<std::shared_ptr<Display>> mPendingRefreshes;
210 VsyncCallback mVsync;
211 std::vector<std::pair<std::shared_ptr<Display>, nsecs_t>> mPendingVsyncs;
212};
213
Fabien Sanglard33960702016-11-29 17:58:21 -0800214// Convenience C++ class to access hwc2_device_t Display functions directly.
Dan Stoza651bf312015-10-23 17:03:17 -0700215class Display : public std::enable_shared_from_this<Display>
216{
217public:
218 Display(Device& device, hwc2_display_t id);
219 ~Display();
220
221 friend class HWC2::Device;
222 friend class HWC2::Layer;
223
224 class Config
225 {
226 public:
227 class Builder
228 {
229 public:
230 Builder(Display& display, hwc2_config_t id);
231
232 std::shared_ptr<const Config> build() {
233 return std::const_pointer_cast<const Config>(
234 std::move(mConfig));
235 }
236
237 Builder& setWidth(int32_t width) {
238 mConfig->mWidth = width;
239 return *this;
240 }
241 Builder& setHeight(int32_t height) {
242 mConfig->mHeight = height;
243 return *this;
244 }
245 Builder& setVsyncPeriod(int32_t vsyncPeriod) {
246 mConfig->mVsyncPeriod = vsyncPeriod;
247 return *this;
248 }
249 Builder& setDpiX(int32_t dpiX) {
250 if (dpiX == -1) {
251 mConfig->mDpiX = getDefaultDensity();
252 } else {
253 mConfig->mDpiX = dpiX / 1000.0f;
254 }
255 return *this;
256 }
257 Builder& setDpiY(int32_t dpiY) {
258 if (dpiY == -1) {
259 mConfig->mDpiY = getDefaultDensity();
260 } else {
261 mConfig->mDpiY = dpiY / 1000.0f;
262 }
263 return *this;
264 }
265
266 private:
267 float getDefaultDensity();
268 std::shared_ptr<Config> mConfig;
269 };
270
271 hwc2_display_t getDisplayId() const { return mDisplay.getId(); }
272 hwc2_config_t getId() const { return mId; }
273
274 int32_t getWidth() const { return mWidth; }
275 int32_t getHeight() const { return mHeight; }
276 nsecs_t getVsyncPeriod() const { return mVsyncPeriod; }
277 float getDpiX() const { return mDpiX; }
278 float getDpiY() const { return mDpiY; }
279
280 private:
281 Config(Display& display, hwc2_config_t id);
282
283 Display& mDisplay;
284 hwc2_config_t mId;
285
286 int32_t mWidth;
287 int32_t mHeight;
288 nsecs_t mVsyncPeriod;
289 float mDpiX;
290 float mDpiY;
291 };
292
293 // Required by HWC2
294
295 [[clang::warn_unused_result]] Error acceptChanges();
296 [[clang::warn_unused_result]] Error createLayer(
297 std::shared_ptr<Layer>* outLayer);
298 [[clang::warn_unused_result]] Error getActiveConfig(
299 std::shared_ptr<const Config>* outConfig) const;
300 [[clang::warn_unused_result]] Error getChangedCompositionTypes(
301 std::unordered_map<std::shared_ptr<Layer>, Composition>* outTypes);
Dan Stoza076ac672016-03-14 10:47:53 -0700302 [[clang::warn_unused_result]] Error getColorModes(
Michael Wright28f24d02016-07-12 13:30:53 -0700303 std::vector<android_color_mode_t>* outModes) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700304
305 // Doesn't call into the HWC2 device, so no errors are possible
306 std::vector<std::shared_ptr<const Config>> getConfigs() const;
307
308 [[clang::warn_unused_result]] Error getName(std::string* outName) const;
309 [[clang::warn_unused_result]] Error getRequests(
310 DisplayRequest* outDisplayRequests,
311 std::unordered_map<std::shared_ptr<Layer>, LayerRequest>*
312 outLayerRequests);
313 [[clang::warn_unused_result]] Error getType(DisplayType* outType) const;
314 [[clang::warn_unused_result]] Error supportsDoze(bool* outSupport) const;
Dan Stoza7d7ae732016-03-16 12:23:40 -0700315 [[clang::warn_unused_result]] Error getHdrCapabilities(
316 std::unique_ptr<android::HdrCapabilities>* outCapabilities) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700317 [[clang::warn_unused_result]] Error getReleaseFences(
318 std::unordered_map<std::shared_ptr<Layer>,
319 android::sp<android::Fence>>* outFences) const;
320 [[clang::warn_unused_result]] Error present(
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800321 android::sp<android::Fence>* outPresentFence);
Dan Stoza651bf312015-10-23 17:03:17 -0700322 [[clang::warn_unused_result]] Error setActiveConfig(
323 const std::shared_ptr<const Config>& config);
324 [[clang::warn_unused_result]] Error setClientTarget(
Chia-I Wu06d63de2017-01-04 14:58:51 +0800325 uint32_t slot, buffer_handle_t target,
Dan Stoza651bf312015-10-23 17:03:17 -0700326 const android::sp<android::Fence>& acquireFence,
327 android_dataspace_t dataspace);
Michael Wright28f24d02016-07-12 13:30:53 -0700328 [[clang::warn_unused_result]] Error setColorMode(android_color_mode_t mode);
Dan Stoza5df2a862016-03-24 16:19:37 -0700329 [[clang::warn_unused_result]] Error setColorTransform(
330 const android::mat4& matrix, android_color_transform_t hint);
Dan Stoza651bf312015-10-23 17:03:17 -0700331 [[clang::warn_unused_result]] Error setOutputBuffer(
332 const android::sp<android::GraphicBuffer>& buffer,
333 const android::sp<android::Fence>& releaseFence);
334 [[clang::warn_unused_result]] Error setPowerMode(PowerMode mode);
335 [[clang::warn_unused_result]] Error setVsyncEnabled(Vsync enabled);
336 [[clang::warn_unused_result]] Error validate(uint32_t* outNumTypes,
337 uint32_t* outNumRequests);
338
339 // Other Display methods
340
341 Device& getDevice() const { return mDevice; }
342 hwc2_display_t getId() const { return mId; }
343 bool isConnected() const { return mIsConnected; }
344
345private:
346 // For use by Device
347
348 // Virtual displays are always connected
349 void setVirtual() {
350 mIsVirtual = true;
351 mIsConnected = true;
352 }
353
354 void setConnected(bool connected) { mIsConnected = connected; }
355 int32_t getAttribute(hwc2_config_t configId, Attribute attribute);
356 void loadConfig(hwc2_config_t configId);
357 void loadConfigs();
358
359 // For use by Layer
360 void destroyLayer(hwc2_layer_t layerId);
361
362 // This may fail (and return a null pointer) if no layer with this ID exists
363 // on this display
364 std::shared_ptr<Layer> getLayerById(hwc2_layer_t id) const;
365
366 // Member variables
367
368 Device& mDevice;
369 hwc2_display_t mId;
370 bool mIsConnected;
371 bool mIsVirtual;
372 std::unordered_map<hwc2_layer_t, std::weak_ptr<Layer>> mLayers;
373 std::unordered_map<hwc2_config_t, std::shared_ptr<const Config>> mConfigs;
374};
375
Fabien Sanglard33960702016-11-29 17:58:21 -0800376// Convenience C++ class to access hwc2_device_t Layer functions directly.
Dan Stoza651bf312015-10-23 17:03:17 -0700377class Layer
378{
379public:
380 Layer(const std::shared_ptr<Display>& display, hwc2_layer_t id);
381 ~Layer();
382
383 bool isAbandoned() const { return mDisplay.expired(); }
384 hwc2_layer_t getId() const { return mId; }
385
386 [[clang::warn_unused_result]] Error setCursorPosition(int32_t x, int32_t y);
Chia-I Wu06d63de2017-01-04 14:58:51 +0800387 [[clang::warn_unused_result]] Error setBuffer(uint32_t slot,
388 buffer_handle_t buffer,
Dan Stoza651bf312015-10-23 17:03:17 -0700389 const android::sp<android::Fence>& acquireFence);
390 [[clang::warn_unused_result]] Error setSurfaceDamage(
391 const android::Region& damage);
392
393 [[clang::warn_unused_result]] Error setBlendMode(BlendMode mode);
394 [[clang::warn_unused_result]] Error setColor(hwc_color_t color);
395 [[clang::warn_unused_result]] Error setCompositionType(Composition type);
Dan Stoza5df2a862016-03-24 16:19:37 -0700396 [[clang::warn_unused_result]] Error setDataspace(
397 android_dataspace_t dataspace);
Dan Stoza651bf312015-10-23 17:03:17 -0700398 [[clang::warn_unused_result]] Error setDisplayFrame(
399 const android::Rect& frame);
400 [[clang::warn_unused_result]] Error setPlaneAlpha(float alpha);
401 [[clang::warn_unused_result]] Error setSidebandStream(
402 const native_handle_t* stream);
403 [[clang::warn_unused_result]] Error setSourceCrop(
Dan Stoza71bded52016-10-19 11:10:33 -0700404 const android::gfx::FloatRect& crop);
Dan Stoza651bf312015-10-23 17:03:17 -0700405 [[clang::warn_unused_result]] Error setTransform(Transform transform);
406 [[clang::warn_unused_result]] Error setVisibleRegion(
407 const android::Region& region);
408 [[clang::warn_unused_result]] Error setZOrder(uint32_t z);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500409 [[clang::warn_unused_result]] Error setInfo(uint32_t type, uint32_t appId);
Dan Stoza651bf312015-10-23 17:03:17 -0700410
411private:
412 std::weak_ptr<Display> mDisplay;
413 hwc2_display_t mDisplayId;
414 Device& mDevice;
415 hwc2_layer_t mId;
416};
417
418} // namespace HWC2
419
420#endif // ANDROID_SF_HWC2_H