blob: e40602fe6254575ac3f3da0a8acab37096141160 [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>
36#include <vector>
37
38namespace android {
39 class Fence;
40 class FloatRect;
41 class GraphicBuffer;
42 class Rect;
43 class Region;
44}
45
46namespace HWC2 {
47
48class Display;
49class Layer;
50
51typedef std::function<void(std::shared_ptr<Display>, Connection)>
52 HotplugCallback;
53typedef std::function<void(std::shared_ptr<Display>)> RefreshCallback;
54typedef std::function<void(std::shared_ptr<Display>, nsecs_t)> VsyncCallback;
55
56class Device
57{
58public:
59 Device(hwc2_device_t* device);
60 ~Device();
61
62 friend class HWC2::Display;
63 friend class HWC2::Layer;
64
65 // Required by HWC2
66
67 std::string dump() const;
68
69 const std::vector<Capability>& getCapabilities() const {
70 return mCapabilities;
71 };
72
73 uint32_t getMaxVirtualDisplayCount() const;
74 Error createVirtualDisplay(uint32_t width, uint32_t height,
75 std::shared_ptr<Display>* outDisplay);
76
77 void registerHotplugCallback(HotplugCallback hotplug);
78 void registerRefreshCallback(RefreshCallback refresh);
79 void registerVsyncCallback(VsyncCallback vsync);
80
81 // For use by callbacks
82
83 void callHotplug(std::shared_ptr<Display> display, Connection connected);
84 void callRefresh(std::shared_ptr<Display> display);
85 void callVsync(std::shared_ptr<Display> display, nsecs_t timestamp);
86
87 // Other Device methods
88
89 // This will create a Display if one is not found, but it will not be marked
90 // as connected
91 std::shared_ptr<Display> getDisplayById(hwc2_display_t id);
92
Dan Stoza09e7a272016-04-14 12:31:01 -070093 bool hasCapability(HWC2::Capability capability) const;
94
Dan Stoza651bf312015-10-23 17:03:17 -070095private:
96 // Initialization methods
97
98 template <typename PFN>
99 [[clang::warn_unused_result]] bool loadFunctionPointer(
100 FunctionDescriptor desc, PFN& outPFN) {
101 auto intDesc = static_cast<int32_t>(desc);
102 auto pfn = mHwcDevice->getFunction(mHwcDevice, intDesc);
103 if (pfn != nullptr) {
104 outPFN = reinterpret_cast<PFN>(pfn);
105 return true;
106 } else {
107 ALOGE("Failed to load function %s", to_string(desc).c_str());
108 return false;
109 }
110 }
111
112 template <typename PFN, typename HOOK>
113 void registerCallback(Callback callback, HOOK hook) {
114 static_assert(std::is_same<PFN, HOOK>::value,
115 "Incompatible function pointer");
116 auto intCallback = static_cast<int32_t>(callback);
117 auto callbackData = static_cast<hwc2_callback_data_t>(this);
118 auto pfn = reinterpret_cast<hwc2_function_pointer_t>(hook);
119 mRegisterCallback(mHwcDevice, intCallback, callbackData, pfn);
120 }
121
122 void loadCapabilities();
123 void loadFunctionPointers();
124 void registerCallbacks();
125
126 // For use by Display
127
128 void destroyVirtualDisplay(hwc2_display_t display);
129
130 // Member variables
131
132 hwc2_device_t* mHwcDevice;
133
134 // Device function pointers
135 HWC2_PFN_CREATE_VIRTUAL_DISPLAY mCreateVirtualDisplay;
136 HWC2_PFN_DESTROY_VIRTUAL_DISPLAY mDestroyVirtualDisplay;
137 HWC2_PFN_DUMP mDump;
138 HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT mGetMaxVirtualDisplayCount;
139 HWC2_PFN_REGISTER_CALLBACK mRegisterCallback;
140
141 // Display function pointers
142 HWC2_PFN_ACCEPT_DISPLAY_CHANGES mAcceptDisplayChanges;
143 HWC2_PFN_CREATE_LAYER mCreateLayer;
144 HWC2_PFN_DESTROY_LAYER mDestroyLayer;
145 HWC2_PFN_GET_ACTIVE_CONFIG mGetActiveConfig;
146 HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES mGetChangedCompositionTypes;
Dan Stoza076ac672016-03-14 10:47:53 -0700147 HWC2_PFN_GET_COLOR_MODES mGetColorModes;
Dan Stoza651bf312015-10-23 17:03:17 -0700148 HWC2_PFN_GET_DISPLAY_ATTRIBUTE mGetDisplayAttribute;
149 HWC2_PFN_GET_DISPLAY_CONFIGS mGetDisplayConfigs;
150 HWC2_PFN_GET_DISPLAY_NAME mGetDisplayName;
151 HWC2_PFN_GET_DISPLAY_REQUESTS mGetDisplayRequests;
152 HWC2_PFN_GET_DISPLAY_TYPE mGetDisplayType;
153 HWC2_PFN_GET_DOZE_SUPPORT mGetDozeSupport;
Dan Stoza7d7ae732016-03-16 12:23:40 -0700154 HWC2_PFN_GET_HDR_CAPABILITIES mGetHdrCapabilities;
Dan Stoza651bf312015-10-23 17:03:17 -0700155 HWC2_PFN_GET_RELEASE_FENCES mGetReleaseFences;
156 HWC2_PFN_PRESENT_DISPLAY mPresentDisplay;
157 HWC2_PFN_SET_ACTIVE_CONFIG mSetActiveConfig;
158 HWC2_PFN_SET_CLIENT_TARGET mSetClientTarget;
Dan Stoza076ac672016-03-14 10:47:53 -0700159 HWC2_PFN_SET_COLOR_MODE mSetColorMode;
Dan Stoza5df2a862016-03-24 16:19:37 -0700160 HWC2_PFN_SET_COLOR_TRANSFORM mSetColorTransform;
Dan Stoza651bf312015-10-23 17:03:17 -0700161 HWC2_PFN_SET_OUTPUT_BUFFER mSetOutputBuffer;
162 HWC2_PFN_SET_POWER_MODE mSetPowerMode;
163 HWC2_PFN_SET_VSYNC_ENABLED mSetVsyncEnabled;
164 HWC2_PFN_VALIDATE_DISPLAY mValidateDisplay;
165
166 // Layer function pointers
167 HWC2_PFN_SET_CURSOR_POSITION mSetCursorPosition;
168 HWC2_PFN_SET_LAYER_BUFFER mSetLayerBuffer;
169 HWC2_PFN_SET_LAYER_SURFACE_DAMAGE mSetLayerSurfaceDamage;
170 HWC2_PFN_SET_LAYER_BLEND_MODE mSetLayerBlendMode;
171 HWC2_PFN_SET_LAYER_COLOR mSetLayerColor;
172 HWC2_PFN_SET_LAYER_COMPOSITION_TYPE mSetLayerCompositionType;
Dan Stoza5df2a862016-03-24 16:19:37 -0700173 HWC2_PFN_SET_LAYER_DATASPACE mSetLayerDataspace;
Dan Stoza651bf312015-10-23 17:03:17 -0700174 HWC2_PFN_SET_LAYER_DISPLAY_FRAME mSetLayerDisplayFrame;
175 HWC2_PFN_SET_LAYER_PLANE_ALPHA mSetLayerPlaneAlpha;
176 HWC2_PFN_SET_LAYER_SIDEBAND_STREAM mSetLayerSidebandStream;
177 HWC2_PFN_SET_LAYER_SOURCE_CROP mSetLayerSourceCrop;
178 HWC2_PFN_SET_LAYER_TRANSFORM mSetLayerTransform;
179 HWC2_PFN_SET_LAYER_VISIBLE_REGION mSetLayerVisibleRegion;
180 HWC2_PFN_SET_LAYER_Z_ORDER mSetLayerZOrder;
181
182 std::vector<Capability> mCapabilities;
183 std::unordered_map<hwc2_display_t, std::shared_ptr<Display>> mDisplays;
184
185 HotplugCallback mHotplug;
186 std::vector<std::pair<std::shared_ptr<Display>, Connection>>
187 mPendingHotplugs;
188 RefreshCallback mRefresh;
189 std::vector<std::shared_ptr<Display>> mPendingRefreshes;
190 VsyncCallback mVsync;
191 std::vector<std::pair<std::shared_ptr<Display>, nsecs_t>> mPendingVsyncs;
192};
193
194class Display : public std::enable_shared_from_this<Display>
195{
196public:
197 Display(Device& device, hwc2_display_t id);
198 ~Display();
199
200 friend class HWC2::Device;
201 friend class HWC2::Layer;
202
203 class Config
204 {
205 public:
206 class Builder
207 {
208 public:
209 Builder(Display& display, hwc2_config_t id);
210
211 std::shared_ptr<const Config> build() {
212 return std::const_pointer_cast<const Config>(
213 std::move(mConfig));
214 }
215
216 Builder& setWidth(int32_t width) {
217 mConfig->mWidth = width;
218 return *this;
219 }
220 Builder& setHeight(int32_t height) {
221 mConfig->mHeight = height;
222 return *this;
223 }
224 Builder& setVsyncPeriod(int32_t vsyncPeriod) {
225 mConfig->mVsyncPeriod = vsyncPeriod;
226 return *this;
227 }
228 Builder& setDpiX(int32_t dpiX) {
229 if (dpiX == -1) {
230 mConfig->mDpiX = getDefaultDensity();
231 } else {
232 mConfig->mDpiX = dpiX / 1000.0f;
233 }
234 return *this;
235 }
236 Builder& setDpiY(int32_t dpiY) {
237 if (dpiY == -1) {
238 mConfig->mDpiY = getDefaultDensity();
239 } else {
240 mConfig->mDpiY = dpiY / 1000.0f;
241 }
242 return *this;
243 }
244
245 private:
246 float getDefaultDensity();
247 std::shared_ptr<Config> mConfig;
248 };
249
250 hwc2_display_t getDisplayId() const { return mDisplay.getId(); }
251 hwc2_config_t getId() const { return mId; }
252
253 int32_t getWidth() const { return mWidth; }
254 int32_t getHeight() const { return mHeight; }
255 nsecs_t getVsyncPeriod() const { return mVsyncPeriod; }
256 float getDpiX() const { return mDpiX; }
257 float getDpiY() const { return mDpiY; }
258
259 private:
260 Config(Display& display, hwc2_config_t id);
261
262 Display& mDisplay;
263 hwc2_config_t mId;
264
265 int32_t mWidth;
266 int32_t mHeight;
267 nsecs_t mVsyncPeriod;
268 float mDpiX;
269 float mDpiY;
270 };
271
272 // Required by HWC2
273
274 [[clang::warn_unused_result]] Error acceptChanges();
275 [[clang::warn_unused_result]] Error createLayer(
276 std::shared_ptr<Layer>* outLayer);
277 [[clang::warn_unused_result]] Error getActiveConfig(
278 std::shared_ptr<const Config>* outConfig) const;
279 [[clang::warn_unused_result]] Error getChangedCompositionTypes(
280 std::unordered_map<std::shared_ptr<Layer>, Composition>* outTypes);
Dan Stoza076ac672016-03-14 10:47:53 -0700281 [[clang::warn_unused_result]] Error getColorModes(
282 std::vector<int32_t>* outModes) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700283
284 // Doesn't call into the HWC2 device, so no errors are possible
285 std::vector<std::shared_ptr<const Config>> getConfigs() const;
286
287 [[clang::warn_unused_result]] Error getName(std::string* outName) const;
288 [[clang::warn_unused_result]] Error getRequests(
289 DisplayRequest* outDisplayRequests,
290 std::unordered_map<std::shared_ptr<Layer>, LayerRequest>*
291 outLayerRequests);
292 [[clang::warn_unused_result]] Error getType(DisplayType* outType) const;
293 [[clang::warn_unused_result]] Error supportsDoze(bool* outSupport) const;
Dan Stoza7d7ae732016-03-16 12:23:40 -0700294 [[clang::warn_unused_result]] Error getHdrCapabilities(
295 std::unique_ptr<android::HdrCapabilities>* outCapabilities) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700296 [[clang::warn_unused_result]] Error getReleaseFences(
297 std::unordered_map<std::shared_ptr<Layer>,
298 android::sp<android::Fence>>* outFences) const;
299 [[clang::warn_unused_result]] Error present(
300 android::sp<android::Fence>* outRetireFence);
301 [[clang::warn_unused_result]] Error setActiveConfig(
302 const std::shared_ptr<const Config>& config);
303 [[clang::warn_unused_result]] Error setClientTarget(
304 buffer_handle_t target,
305 const android::sp<android::Fence>& acquireFence,
306 android_dataspace_t dataspace);
Dan Stoza076ac672016-03-14 10:47:53 -0700307 [[clang::warn_unused_result]] Error setColorMode(int32_t mode);
Dan Stoza5df2a862016-03-24 16:19:37 -0700308 [[clang::warn_unused_result]] Error setColorTransform(
309 const android::mat4& matrix, android_color_transform_t hint);
Dan Stoza651bf312015-10-23 17:03:17 -0700310 [[clang::warn_unused_result]] Error setOutputBuffer(
311 const android::sp<android::GraphicBuffer>& buffer,
312 const android::sp<android::Fence>& releaseFence);
313 [[clang::warn_unused_result]] Error setPowerMode(PowerMode mode);
314 [[clang::warn_unused_result]] Error setVsyncEnabled(Vsync enabled);
315 [[clang::warn_unused_result]] Error validate(uint32_t* outNumTypes,
316 uint32_t* outNumRequests);
317
318 // Other Display methods
319
320 Device& getDevice() const { return mDevice; }
321 hwc2_display_t getId() const { return mId; }
322 bool isConnected() const { return mIsConnected; }
323
324private:
325 // For use by Device
326
327 // Virtual displays are always connected
328 void setVirtual() {
329 mIsVirtual = true;
330 mIsConnected = true;
331 }
332
333 void setConnected(bool connected) { mIsConnected = connected; }
334 int32_t getAttribute(hwc2_config_t configId, Attribute attribute);
335 void loadConfig(hwc2_config_t configId);
336 void loadConfigs();
337
338 // For use by Layer
339 void destroyLayer(hwc2_layer_t layerId);
340
341 // This may fail (and return a null pointer) if no layer with this ID exists
342 // on this display
343 std::shared_ptr<Layer> getLayerById(hwc2_layer_t id) const;
344
345 // Member variables
346
347 Device& mDevice;
348 hwc2_display_t mId;
349 bool mIsConnected;
350 bool mIsVirtual;
351 std::unordered_map<hwc2_layer_t, std::weak_ptr<Layer>> mLayers;
352 std::unordered_map<hwc2_config_t, std::shared_ptr<const Config>> mConfigs;
353};
354
355class Layer
356{
357public:
358 Layer(const std::shared_ptr<Display>& display, hwc2_layer_t id);
359 ~Layer();
360
361 bool isAbandoned() const { return mDisplay.expired(); }
362 hwc2_layer_t getId() const { return mId; }
363
364 [[clang::warn_unused_result]] Error setCursorPosition(int32_t x, int32_t y);
365 [[clang::warn_unused_result]] Error setBuffer(buffer_handle_t buffer,
366 const android::sp<android::Fence>& acquireFence);
367 [[clang::warn_unused_result]] Error setSurfaceDamage(
368 const android::Region& damage);
369
370 [[clang::warn_unused_result]] Error setBlendMode(BlendMode mode);
371 [[clang::warn_unused_result]] Error setColor(hwc_color_t color);
372 [[clang::warn_unused_result]] Error setCompositionType(Composition type);
Dan Stoza5df2a862016-03-24 16:19:37 -0700373 [[clang::warn_unused_result]] Error setDataspace(
374 android_dataspace_t dataspace);
Dan Stoza651bf312015-10-23 17:03:17 -0700375 [[clang::warn_unused_result]] Error setDisplayFrame(
376 const android::Rect& frame);
377 [[clang::warn_unused_result]] Error setPlaneAlpha(float alpha);
378 [[clang::warn_unused_result]] Error setSidebandStream(
379 const native_handle_t* stream);
380 [[clang::warn_unused_result]] Error setSourceCrop(
381 const android::FloatRect& crop);
382 [[clang::warn_unused_result]] Error setTransform(Transform transform);
383 [[clang::warn_unused_result]] Error setVisibleRegion(
384 const android::Region& region);
385 [[clang::warn_unused_result]] Error setZOrder(uint32_t z);
386
387private:
388 std::weak_ptr<Display> mDisplay;
389 hwc2_display_t mDisplayId;
390 Device& mDevice;
391 hwc2_layer_t mId;
392};
393
394} // namespace HWC2
395
396#endif // ANDROID_SF_HWC2_H