blob: 4419dc1172749261f4545cbe821d6f36652fb0bf [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>
Mathias Agopian1d77b712017-02-17 15:46:13 -080027#include <math/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 Stoza5a423ea2017-02-16 14:10:39 -080041 class FloatRect;
Dan Stoza651bf312015-10-23 17:03:17 -070042 class GraphicBuffer;
43 class Rect;
44 class Region;
Chia-I Wuaab99f52016-10-05 12:59:58 +080045 namespace Hwc2 {
46 class Composer;
Dan Stoza71bded52016-10-19 11:10:33 -070047 }
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
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -080068 // useVrComposer is passed to the composer HAL. When true, the composer HAL
69 // will use the vr composer service, otherwise it uses the real hardware
70 // composer.
71 Device(bool useVrComposer);
Chia-I Wuaab99f52016-10-05 12:59:58 +080072#endif
Dan Stoza651bf312015-10-23 17:03:17 -070073 ~Device();
74
75 friend class HWC2::Display;
76 friend class HWC2::Layer;
77
78 // Required by HWC2
79
80 std::string dump() const;
81
Dan Stoza9f26a9c2016-06-22 14:51:09 -070082 const std::unordered_set<Capability>& getCapabilities() const {
Dan Stoza651bf312015-10-23 17:03:17 -070083 return mCapabilities;
84 };
85
86 uint32_t getMaxVirtualDisplayCount() const;
87 Error createVirtualDisplay(uint32_t width, uint32_t height,
Dan Stoza5cf424b2016-05-20 14:02:39 -070088 android_pixel_format_t* format,
Dan Stoza651bf312015-10-23 17:03:17 -070089 std::shared_ptr<Display>* outDisplay);
90
91 void registerHotplugCallback(HotplugCallback hotplug);
92 void registerRefreshCallback(RefreshCallback refresh);
93 void registerVsyncCallback(VsyncCallback vsync);
94
95 // For use by callbacks
96
97 void callHotplug(std::shared_ptr<Display> display, Connection connected);
98 void callRefresh(std::shared_ptr<Display> display);
99 void callVsync(std::shared_ptr<Display> display, nsecs_t timestamp);
100
101 // Other Device methods
102
103 // This will create a Display if one is not found, but it will not be marked
Dan Stoza38628982016-07-13 15:48:58 -0700104 // as connected. This Display may be null if the display has been torn down
105 // but has not been removed from the map yet.
Dan Stoza651bf312015-10-23 17:03:17 -0700106 std::shared_ptr<Display> getDisplayById(hwc2_display_t id);
107
Dan Stoza09e7a272016-04-14 12:31:01 -0700108 bool hasCapability(HWC2::Capability capability) const;
109
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800110#ifdef BYPASS_IHWC
111 android::Hwc2::Composer* getComposer() { return nullptr; }
112#else
113 android::Hwc2::Composer* getComposer() { return mComposer.get(); }
114#endif
115
Dan Stoza651bf312015-10-23 17:03:17 -0700116private:
117 // Initialization methods
118
Chia-I Wuaab99f52016-10-05 12:59:58 +0800119#ifdef BYPASS_IHWC
Dan Stoza651bf312015-10-23 17:03:17 -0700120 template <typename PFN>
121 [[clang::warn_unused_result]] bool loadFunctionPointer(
122 FunctionDescriptor desc, PFN& outPFN) {
123 auto intDesc = static_cast<int32_t>(desc);
124 auto pfn = mHwcDevice->getFunction(mHwcDevice, intDesc);
125 if (pfn != nullptr) {
126 outPFN = reinterpret_cast<PFN>(pfn);
127 return true;
128 } else {
129 ALOGE("Failed to load function %s", to_string(desc).c_str());
130 return false;
131 }
132 }
133
134 template <typename PFN, typename HOOK>
135 void registerCallback(Callback callback, HOOK hook) {
136 static_assert(std::is_same<PFN, HOOK>::value,
137 "Incompatible function pointer");
138 auto intCallback = static_cast<int32_t>(callback);
139 auto callbackData = static_cast<hwc2_callback_data_t>(this);
140 auto pfn = reinterpret_cast<hwc2_function_pointer_t>(hook);
141 mRegisterCallback(mHwcDevice, intCallback, callbackData, pfn);
142 }
Chia-I Wuaab99f52016-10-05 12:59:58 +0800143#endif
Dan Stoza651bf312015-10-23 17:03:17 -0700144
145 void loadCapabilities();
146 void loadFunctionPointers();
147 void registerCallbacks();
148
149 // For use by Display
150
151 void destroyVirtualDisplay(hwc2_display_t display);
152
153 // Member variables
154
Chia-I Wuaab99f52016-10-05 12:59:58 +0800155#ifdef BYPASS_IHWC
Dan Stoza651bf312015-10-23 17:03:17 -0700156 hwc2_device_t* mHwcDevice;
157
158 // Device function pointers
159 HWC2_PFN_CREATE_VIRTUAL_DISPLAY mCreateVirtualDisplay;
160 HWC2_PFN_DESTROY_VIRTUAL_DISPLAY mDestroyVirtualDisplay;
161 HWC2_PFN_DUMP mDump;
162 HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT mGetMaxVirtualDisplayCount;
163 HWC2_PFN_REGISTER_CALLBACK mRegisterCallback;
164
165 // Display function pointers
166 HWC2_PFN_ACCEPT_DISPLAY_CHANGES mAcceptDisplayChanges;
167 HWC2_PFN_CREATE_LAYER mCreateLayer;
168 HWC2_PFN_DESTROY_LAYER mDestroyLayer;
169 HWC2_PFN_GET_ACTIVE_CONFIG mGetActiveConfig;
170 HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES mGetChangedCompositionTypes;
Dan Stoza076ac672016-03-14 10:47:53 -0700171 HWC2_PFN_GET_COLOR_MODES mGetColorModes;
Dan Stoza651bf312015-10-23 17:03:17 -0700172 HWC2_PFN_GET_DISPLAY_ATTRIBUTE mGetDisplayAttribute;
173 HWC2_PFN_GET_DISPLAY_CONFIGS mGetDisplayConfigs;
174 HWC2_PFN_GET_DISPLAY_NAME mGetDisplayName;
175 HWC2_PFN_GET_DISPLAY_REQUESTS mGetDisplayRequests;
176 HWC2_PFN_GET_DISPLAY_TYPE mGetDisplayType;
177 HWC2_PFN_GET_DOZE_SUPPORT mGetDozeSupport;
Dan Stoza7d7ae732016-03-16 12:23:40 -0700178 HWC2_PFN_GET_HDR_CAPABILITIES mGetHdrCapabilities;
Dan Stoza651bf312015-10-23 17:03:17 -0700179 HWC2_PFN_GET_RELEASE_FENCES mGetReleaseFences;
180 HWC2_PFN_PRESENT_DISPLAY mPresentDisplay;
181 HWC2_PFN_SET_ACTIVE_CONFIG mSetActiveConfig;
182 HWC2_PFN_SET_CLIENT_TARGET mSetClientTarget;
Dan Stoza076ac672016-03-14 10:47:53 -0700183 HWC2_PFN_SET_COLOR_MODE mSetColorMode;
Dan Stoza5df2a862016-03-24 16:19:37 -0700184 HWC2_PFN_SET_COLOR_TRANSFORM mSetColorTransform;
Dan Stoza651bf312015-10-23 17:03:17 -0700185 HWC2_PFN_SET_OUTPUT_BUFFER mSetOutputBuffer;
186 HWC2_PFN_SET_POWER_MODE mSetPowerMode;
187 HWC2_PFN_SET_VSYNC_ENABLED mSetVsyncEnabled;
188 HWC2_PFN_VALIDATE_DISPLAY mValidateDisplay;
189
190 // Layer function pointers
191 HWC2_PFN_SET_CURSOR_POSITION mSetCursorPosition;
192 HWC2_PFN_SET_LAYER_BUFFER mSetLayerBuffer;
193 HWC2_PFN_SET_LAYER_SURFACE_DAMAGE mSetLayerSurfaceDamage;
194 HWC2_PFN_SET_LAYER_BLEND_MODE mSetLayerBlendMode;
195 HWC2_PFN_SET_LAYER_COLOR mSetLayerColor;
196 HWC2_PFN_SET_LAYER_COMPOSITION_TYPE mSetLayerCompositionType;
Dan Stoza5df2a862016-03-24 16:19:37 -0700197 HWC2_PFN_SET_LAYER_DATASPACE mSetLayerDataspace;
Dan Stoza651bf312015-10-23 17:03:17 -0700198 HWC2_PFN_SET_LAYER_DISPLAY_FRAME mSetLayerDisplayFrame;
199 HWC2_PFN_SET_LAYER_PLANE_ALPHA mSetLayerPlaneAlpha;
200 HWC2_PFN_SET_LAYER_SIDEBAND_STREAM mSetLayerSidebandStream;
201 HWC2_PFN_SET_LAYER_SOURCE_CROP mSetLayerSourceCrop;
202 HWC2_PFN_SET_LAYER_TRANSFORM mSetLayerTransform;
203 HWC2_PFN_SET_LAYER_VISIBLE_REGION mSetLayerVisibleRegion;
204 HWC2_PFN_SET_LAYER_Z_ORDER mSetLayerZOrder;
Chia-I Wuaab99f52016-10-05 12:59:58 +0800205#else
206 std::unique_ptr<android::Hwc2::Composer> mComposer;
207#endif // BYPASS_IHWC
Dan Stoza651bf312015-10-23 17:03:17 -0700208
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700209 std::unordered_set<Capability> mCapabilities;
Dan Stoza38628982016-07-13 15:48:58 -0700210 std::unordered_map<hwc2_display_t, std::weak_ptr<Display>> mDisplays;
Dan Stoza651bf312015-10-23 17:03:17 -0700211
212 HotplugCallback mHotplug;
213 std::vector<std::pair<std::shared_ptr<Display>, Connection>>
214 mPendingHotplugs;
215 RefreshCallback mRefresh;
216 std::vector<std::shared_ptr<Display>> mPendingRefreshes;
217 VsyncCallback mVsync;
218 std::vector<std::pair<std::shared_ptr<Display>, nsecs_t>> mPendingVsyncs;
219};
220
Fabien Sanglard33960702016-11-29 17:58:21 -0800221// Convenience C++ class to access hwc2_device_t Display functions directly.
Dan Stoza651bf312015-10-23 17:03:17 -0700222class Display : public std::enable_shared_from_this<Display>
223{
224public:
225 Display(Device& device, hwc2_display_t id);
226 ~Display();
227
228 friend class HWC2::Device;
229 friend class HWC2::Layer;
230
231 class Config
232 {
233 public:
234 class Builder
235 {
236 public:
237 Builder(Display& display, hwc2_config_t id);
238
239 std::shared_ptr<const Config> build() {
240 return std::const_pointer_cast<const Config>(
241 std::move(mConfig));
242 }
243
244 Builder& setWidth(int32_t width) {
245 mConfig->mWidth = width;
246 return *this;
247 }
248 Builder& setHeight(int32_t height) {
249 mConfig->mHeight = height;
250 return *this;
251 }
252 Builder& setVsyncPeriod(int32_t vsyncPeriod) {
253 mConfig->mVsyncPeriod = vsyncPeriod;
254 return *this;
255 }
256 Builder& setDpiX(int32_t dpiX) {
257 if (dpiX == -1) {
258 mConfig->mDpiX = getDefaultDensity();
259 } else {
260 mConfig->mDpiX = dpiX / 1000.0f;
261 }
262 return *this;
263 }
264 Builder& setDpiY(int32_t dpiY) {
265 if (dpiY == -1) {
266 mConfig->mDpiY = getDefaultDensity();
267 } else {
268 mConfig->mDpiY = dpiY / 1000.0f;
269 }
270 return *this;
271 }
272
273 private:
274 float getDefaultDensity();
275 std::shared_ptr<Config> mConfig;
276 };
277
278 hwc2_display_t getDisplayId() const { return mDisplay.getId(); }
279 hwc2_config_t getId() const { return mId; }
280
281 int32_t getWidth() const { return mWidth; }
282 int32_t getHeight() const { return mHeight; }
283 nsecs_t getVsyncPeriod() const { return mVsyncPeriod; }
284 float getDpiX() const { return mDpiX; }
285 float getDpiY() const { return mDpiY; }
286
287 private:
288 Config(Display& display, hwc2_config_t id);
289
290 Display& mDisplay;
291 hwc2_config_t mId;
292
293 int32_t mWidth;
294 int32_t mHeight;
295 nsecs_t mVsyncPeriod;
296 float mDpiX;
297 float mDpiY;
298 };
299
300 // Required by HWC2
301
302 [[clang::warn_unused_result]] Error acceptChanges();
303 [[clang::warn_unused_result]] Error createLayer(
304 std::shared_ptr<Layer>* outLayer);
305 [[clang::warn_unused_result]] Error getActiveConfig(
306 std::shared_ptr<const Config>* outConfig) const;
307 [[clang::warn_unused_result]] Error getChangedCompositionTypes(
308 std::unordered_map<std::shared_ptr<Layer>, Composition>* outTypes);
Dan Stoza076ac672016-03-14 10:47:53 -0700309 [[clang::warn_unused_result]] Error getColorModes(
Michael Wright28f24d02016-07-12 13:30:53 -0700310 std::vector<android_color_mode_t>* outModes) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700311
312 // Doesn't call into the HWC2 device, so no errors are possible
313 std::vector<std::shared_ptr<const Config>> getConfigs() const;
314
315 [[clang::warn_unused_result]] Error getName(std::string* outName) const;
316 [[clang::warn_unused_result]] Error getRequests(
317 DisplayRequest* outDisplayRequests,
318 std::unordered_map<std::shared_ptr<Layer>, LayerRequest>*
319 outLayerRequests);
320 [[clang::warn_unused_result]] Error getType(DisplayType* outType) const;
321 [[clang::warn_unused_result]] Error supportsDoze(bool* outSupport) const;
Dan Stoza7d7ae732016-03-16 12:23:40 -0700322 [[clang::warn_unused_result]] Error getHdrCapabilities(
323 std::unique_ptr<android::HdrCapabilities>* outCapabilities) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700324 [[clang::warn_unused_result]] Error getReleaseFences(
325 std::unordered_map<std::shared_ptr<Layer>,
326 android::sp<android::Fence>>* outFences) const;
327 [[clang::warn_unused_result]] Error present(
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800328 android::sp<android::Fence>* outPresentFence);
Dan Stoza651bf312015-10-23 17:03:17 -0700329 [[clang::warn_unused_result]] Error setActiveConfig(
330 const std::shared_ptr<const Config>& config);
331 [[clang::warn_unused_result]] Error setClientTarget(
Chia-I Wu06d63de2017-01-04 14:58:51 +0800332 uint32_t slot, buffer_handle_t target,
Dan Stoza651bf312015-10-23 17:03:17 -0700333 const android::sp<android::Fence>& acquireFence,
334 android_dataspace_t dataspace);
Michael Wright28f24d02016-07-12 13:30:53 -0700335 [[clang::warn_unused_result]] Error setColorMode(android_color_mode_t mode);
Dan Stoza5df2a862016-03-24 16:19:37 -0700336 [[clang::warn_unused_result]] Error setColorTransform(
337 const android::mat4& matrix, android_color_transform_t hint);
Dan Stoza651bf312015-10-23 17:03:17 -0700338 [[clang::warn_unused_result]] Error setOutputBuffer(
339 const android::sp<android::GraphicBuffer>& buffer,
340 const android::sp<android::Fence>& releaseFence);
341 [[clang::warn_unused_result]] Error setPowerMode(PowerMode mode);
342 [[clang::warn_unused_result]] Error setVsyncEnabled(Vsync enabled);
343 [[clang::warn_unused_result]] Error validate(uint32_t* outNumTypes,
344 uint32_t* outNumRequests);
345
346 // Other Display methods
347
348 Device& getDevice() const { return mDevice; }
349 hwc2_display_t getId() const { return mId; }
350 bool isConnected() const { return mIsConnected; }
351
352private:
353 // For use by Device
354
355 // Virtual displays are always connected
356 void setVirtual() {
357 mIsVirtual = true;
358 mIsConnected = true;
359 }
360
361 void setConnected(bool connected) { mIsConnected = connected; }
362 int32_t getAttribute(hwc2_config_t configId, Attribute attribute);
363 void loadConfig(hwc2_config_t configId);
364 void loadConfigs();
365
366 // For use by Layer
367 void destroyLayer(hwc2_layer_t layerId);
368
369 // This may fail (and return a null pointer) if no layer with this ID exists
370 // on this display
371 std::shared_ptr<Layer> getLayerById(hwc2_layer_t id) const;
372
373 // Member variables
374
375 Device& mDevice;
376 hwc2_display_t mId;
377 bool mIsConnected;
378 bool mIsVirtual;
379 std::unordered_map<hwc2_layer_t, std::weak_ptr<Layer>> mLayers;
380 std::unordered_map<hwc2_config_t, std::shared_ptr<const Config>> mConfigs;
381};
382
Fabien Sanglard33960702016-11-29 17:58:21 -0800383// Convenience C++ class to access hwc2_device_t Layer functions directly.
Dan Stoza651bf312015-10-23 17:03:17 -0700384class Layer
385{
386public:
387 Layer(const std::shared_ptr<Display>& display, hwc2_layer_t id);
388 ~Layer();
389
390 bool isAbandoned() const { return mDisplay.expired(); }
391 hwc2_layer_t getId() const { return mId; }
392
393 [[clang::warn_unused_result]] Error setCursorPosition(int32_t x, int32_t y);
Chia-I Wu06d63de2017-01-04 14:58:51 +0800394 [[clang::warn_unused_result]] Error setBuffer(uint32_t slot,
395 buffer_handle_t buffer,
Dan Stoza651bf312015-10-23 17:03:17 -0700396 const android::sp<android::Fence>& acquireFence);
397 [[clang::warn_unused_result]] Error setSurfaceDamage(
398 const android::Region& damage);
399
400 [[clang::warn_unused_result]] Error setBlendMode(BlendMode mode);
401 [[clang::warn_unused_result]] Error setColor(hwc_color_t color);
402 [[clang::warn_unused_result]] Error setCompositionType(Composition type);
Dan Stoza5df2a862016-03-24 16:19:37 -0700403 [[clang::warn_unused_result]] Error setDataspace(
404 android_dataspace_t dataspace);
Dan Stoza651bf312015-10-23 17:03:17 -0700405 [[clang::warn_unused_result]] Error setDisplayFrame(
406 const android::Rect& frame);
407 [[clang::warn_unused_result]] Error setPlaneAlpha(float alpha);
408 [[clang::warn_unused_result]] Error setSidebandStream(
409 const native_handle_t* stream);
410 [[clang::warn_unused_result]] Error setSourceCrop(
Dan Stoza5a423ea2017-02-16 14:10:39 -0800411 const android::FloatRect& crop);
Dan Stoza651bf312015-10-23 17:03:17 -0700412 [[clang::warn_unused_result]] Error setTransform(Transform transform);
413 [[clang::warn_unused_result]] Error setVisibleRegion(
414 const android::Region& region);
415 [[clang::warn_unused_result]] Error setZOrder(uint32_t z);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500416 [[clang::warn_unused_result]] Error setInfo(uint32_t type, uint32_t appId);
Dan Stoza651bf312015-10-23 17:03:17 -0700417
418private:
419 std::weak_ptr<Display> mDisplay;
420 hwc2_display_t mDisplayId;
421 Device& mDevice;
422 hwc2_layer_t mId;
423};
424
425} // namespace HWC2
426
427#endif // ANDROID_SF_HWC2_H