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