blob: 76963056d81a9e67847eac5ae580f57cd609fd90 [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;
147 HWC2_PFN_GET_DISPLAY_ATTRIBUTE mGetDisplayAttribute;
148 HWC2_PFN_GET_DISPLAY_CONFIGS mGetDisplayConfigs;
149 HWC2_PFN_GET_DISPLAY_NAME mGetDisplayName;
150 HWC2_PFN_GET_DISPLAY_REQUESTS mGetDisplayRequests;
151 HWC2_PFN_GET_DISPLAY_TYPE mGetDisplayType;
152 HWC2_PFN_GET_DOZE_SUPPORT mGetDozeSupport;
Dan Stoza7d7ae732016-03-16 12:23:40 -0700153 HWC2_PFN_GET_HDR_CAPABILITIES mGetHdrCapabilities;
Dan Stoza651bf312015-10-23 17:03:17 -0700154 HWC2_PFN_GET_RELEASE_FENCES mGetReleaseFences;
155 HWC2_PFN_PRESENT_DISPLAY mPresentDisplay;
156 HWC2_PFN_SET_ACTIVE_CONFIG mSetActiveConfig;
157 HWC2_PFN_SET_CLIENT_TARGET mSetClientTarget;
Dan Stoza5df2a862016-03-24 16:19:37 -0700158 HWC2_PFN_SET_COLOR_TRANSFORM mSetColorTransform;
Dan Stoza651bf312015-10-23 17:03:17 -0700159 HWC2_PFN_SET_OUTPUT_BUFFER mSetOutputBuffer;
160 HWC2_PFN_SET_POWER_MODE mSetPowerMode;
161 HWC2_PFN_SET_VSYNC_ENABLED mSetVsyncEnabled;
162 HWC2_PFN_VALIDATE_DISPLAY mValidateDisplay;
163
164 // Layer function pointers
165 HWC2_PFN_SET_CURSOR_POSITION mSetCursorPosition;
166 HWC2_PFN_SET_LAYER_BUFFER mSetLayerBuffer;
167 HWC2_PFN_SET_LAYER_SURFACE_DAMAGE mSetLayerSurfaceDamage;
168 HWC2_PFN_SET_LAYER_BLEND_MODE mSetLayerBlendMode;
169 HWC2_PFN_SET_LAYER_COLOR mSetLayerColor;
170 HWC2_PFN_SET_LAYER_COMPOSITION_TYPE mSetLayerCompositionType;
Dan Stoza5df2a862016-03-24 16:19:37 -0700171 HWC2_PFN_SET_LAYER_DATASPACE mSetLayerDataspace;
Dan Stoza651bf312015-10-23 17:03:17 -0700172 HWC2_PFN_SET_LAYER_DISPLAY_FRAME mSetLayerDisplayFrame;
173 HWC2_PFN_SET_LAYER_PLANE_ALPHA mSetLayerPlaneAlpha;
174 HWC2_PFN_SET_LAYER_SIDEBAND_STREAM mSetLayerSidebandStream;
175 HWC2_PFN_SET_LAYER_SOURCE_CROP mSetLayerSourceCrop;
176 HWC2_PFN_SET_LAYER_TRANSFORM mSetLayerTransform;
177 HWC2_PFN_SET_LAYER_VISIBLE_REGION mSetLayerVisibleRegion;
178 HWC2_PFN_SET_LAYER_Z_ORDER mSetLayerZOrder;
179
180 std::vector<Capability> mCapabilities;
181 std::unordered_map<hwc2_display_t, std::shared_ptr<Display>> mDisplays;
182
183 HotplugCallback mHotplug;
184 std::vector<std::pair<std::shared_ptr<Display>, Connection>>
185 mPendingHotplugs;
186 RefreshCallback mRefresh;
187 std::vector<std::shared_ptr<Display>> mPendingRefreshes;
188 VsyncCallback mVsync;
189 std::vector<std::pair<std::shared_ptr<Display>, nsecs_t>> mPendingVsyncs;
190};
191
192class Display : public std::enable_shared_from_this<Display>
193{
194public:
195 Display(Device& device, hwc2_display_t id);
196 ~Display();
197
198 friend class HWC2::Device;
199 friend class HWC2::Layer;
200
201 class Config
202 {
203 public:
204 class Builder
205 {
206 public:
207 Builder(Display& display, hwc2_config_t id);
208
209 std::shared_ptr<const Config> build() {
210 return std::const_pointer_cast<const Config>(
211 std::move(mConfig));
212 }
213
214 Builder& setWidth(int32_t width) {
215 mConfig->mWidth = width;
216 return *this;
217 }
218 Builder& setHeight(int32_t height) {
219 mConfig->mHeight = height;
220 return *this;
221 }
222 Builder& setVsyncPeriod(int32_t vsyncPeriod) {
223 mConfig->mVsyncPeriod = vsyncPeriod;
224 return *this;
225 }
226 Builder& setDpiX(int32_t dpiX) {
227 if (dpiX == -1) {
228 mConfig->mDpiX = getDefaultDensity();
229 } else {
230 mConfig->mDpiX = dpiX / 1000.0f;
231 }
232 return *this;
233 }
234 Builder& setDpiY(int32_t dpiY) {
235 if (dpiY == -1) {
236 mConfig->mDpiY = getDefaultDensity();
237 } else {
238 mConfig->mDpiY = dpiY / 1000.0f;
239 }
240 return *this;
241 }
242
243 private:
244 float getDefaultDensity();
245 std::shared_ptr<Config> mConfig;
246 };
247
248 hwc2_display_t getDisplayId() const { return mDisplay.getId(); }
249 hwc2_config_t getId() const { return mId; }
250
251 int32_t getWidth() const { return mWidth; }
252 int32_t getHeight() const { return mHeight; }
253 nsecs_t getVsyncPeriod() const { return mVsyncPeriod; }
254 float getDpiX() const { return mDpiX; }
255 float getDpiY() const { return mDpiY; }
256
257 private:
258 Config(Display& display, hwc2_config_t id);
259
260 Display& mDisplay;
261 hwc2_config_t mId;
262
263 int32_t mWidth;
264 int32_t mHeight;
265 nsecs_t mVsyncPeriod;
266 float mDpiX;
267 float mDpiY;
268 };
269
270 // Required by HWC2
271
272 [[clang::warn_unused_result]] Error acceptChanges();
273 [[clang::warn_unused_result]] Error createLayer(
274 std::shared_ptr<Layer>* outLayer);
275 [[clang::warn_unused_result]] Error getActiveConfig(
276 std::shared_ptr<const Config>* outConfig) const;
277 [[clang::warn_unused_result]] Error getChangedCompositionTypes(
278 std::unordered_map<std::shared_ptr<Layer>, Composition>* outTypes);
279
280 // Doesn't call into the HWC2 device, so no errors are possible
281 std::vector<std::shared_ptr<const Config>> getConfigs() const;
282
283 [[clang::warn_unused_result]] Error getName(std::string* outName) const;
284 [[clang::warn_unused_result]] Error getRequests(
285 DisplayRequest* outDisplayRequests,
286 std::unordered_map<std::shared_ptr<Layer>, LayerRequest>*
287 outLayerRequests);
288 [[clang::warn_unused_result]] Error getType(DisplayType* outType) const;
289 [[clang::warn_unused_result]] Error supportsDoze(bool* outSupport) const;
Dan Stoza7d7ae732016-03-16 12:23:40 -0700290 [[clang::warn_unused_result]] Error getHdrCapabilities(
291 std::unique_ptr<android::HdrCapabilities>* outCapabilities) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700292 [[clang::warn_unused_result]] Error getReleaseFences(
293 std::unordered_map<std::shared_ptr<Layer>,
294 android::sp<android::Fence>>* outFences) const;
295 [[clang::warn_unused_result]] Error present(
296 android::sp<android::Fence>* outRetireFence);
297 [[clang::warn_unused_result]] Error setActiveConfig(
298 const std::shared_ptr<const Config>& config);
299 [[clang::warn_unused_result]] Error setClientTarget(
300 buffer_handle_t target,
301 const android::sp<android::Fence>& acquireFence,
302 android_dataspace_t dataspace);
Dan Stoza5df2a862016-03-24 16:19:37 -0700303 [[clang::warn_unused_result]] Error setColorTransform(
304 const android::mat4& matrix, android_color_transform_t hint);
Dan Stoza651bf312015-10-23 17:03:17 -0700305 [[clang::warn_unused_result]] Error setOutputBuffer(
306 const android::sp<android::GraphicBuffer>& buffer,
307 const android::sp<android::Fence>& releaseFence);
308 [[clang::warn_unused_result]] Error setPowerMode(PowerMode mode);
309 [[clang::warn_unused_result]] Error setVsyncEnabled(Vsync enabled);
310 [[clang::warn_unused_result]] Error validate(uint32_t* outNumTypes,
311 uint32_t* outNumRequests);
312
313 // Other Display methods
314
315 Device& getDevice() const { return mDevice; }
316 hwc2_display_t getId() const { return mId; }
317 bool isConnected() const { return mIsConnected; }
318
319private:
320 // For use by Device
321
322 // Virtual displays are always connected
323 void setVirtual() {
324 mIsVirtual = true;
325 mIsConnected = true;
326 }
327
328 void setConnected(bool connected) { mIsConnected = connected; }
329 int32_t getAttribute(hwc2_config_t configId, Attribute attribute);
330 void loadConfig(hwc2_config_t configId);
331 void loadConfigs();
332
333 // For use by Layer
334 void destroyLayer(hwc2_layer_t layerId);
335
336 // This may fail (and return a null pointer) if no layer with this ID exists
337 // on this display
338 std::shared_ptr<Layer> getLayerById(hwc2_layer_t id) const;
339
340 // Member variables
341
342 Device& mDevice;
343 hwc2_display_t mId;
344 bool mIsConnected;
345 bool mIsVirtual;
346 std::unordered_map<hwc2_layer_t, std::weak_ptr<Layer>> mLayers;
347 std::unordered_map<hwc2_config_t, std::shared_ptr<const Config>> mConfigs;
348};
349
350class Layer
351{
352public:
353 Layer(const std::shared_ptr<Display>& display, hwc2_layer_t id);
354 ~Layer();
355
356 bool isAbandoned() const { return mDisplay.expired(); }
357 hwc2_layer_t getId() const { return mId; }
358
359 [[clang::warn_unused_result]] Error setCursorPosition(int32_t x, int32_t y);
360 [[clang::warn_unused_result]] Error setBuffer(buffer_handle_t buffer,
361 const android::sp<android::Fence>& acquireFence);
362 [[clang::warn_unused_result]] Error setSurfaceDamage(
363 const android::Region& damage);
364
365 [[clang::warn_unused_result]] Error setBlendMode(BlendMode mode);
366 [[clang::warn_unused_result]] Error setColor(hwc_color_t color);
367 [[clang::warn_unused_result]] Error setCompositionType(Composition type);
Dan Stoza5df2a862016-03-24 16:19:37 -0700368 [[clang::warn_unused_result]] Error setDataspace(
369 android_dataspace_t dataspace);
Dan Stoza651bf312015-10-23 17:03:17 -0700370 [[clang::warn_unused_result]] Error setDisplayFrame(
371 const android::Rect& frame);
372 [[clang::warn_unused_result]] Error setPlaneAlpha(float alpha);
373 [[clang::warn_unused_result]] Error setSidebandStream(
374 const native_handle_t* stream);
375 [[clang::warn_unused_result]] Error setSourceCrop(
376 const android::FloatRect& crop);
377 [[clang::warn_unused_result]] Error setTransform(Transform transform);
378 [[clang::warn_unused_result]] Error setVisibleRegion(
379 const android::Region& region);
380 [[clang::warn_unused_result]] Error setZOrder(uint32_t z);
381
382private:
383 std::weak_ptr<Display> mDisplay;
384 hwc2_display_t mDisplayId;
385 Device& mDevice;
386 hwc2_layer_t mId;
387};
388
389} // namespace HWC2
390
391#endif // ANDROID_SF_HWC2_H