blob: 1145ba1fdddec12c5637f330c7951d0df9d6c035 [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
Chia-I Wuaab99f52016-10-05 12:59:58 +080020#ifndef USE_HWC2
21#define BYPASS_IHWC
22#endif
23
Dan Stoza651bf312015-10-23 17:03:17 -070024#define HWC2_INCLUDE_STRINGIFICATION
25#define HWC2_USE_CPP11
26#include <hardware/hwcomposer2.h>
27#undef HWC2_INCLUDE_STRINGIFICATION
28#undef HWC2_USE_CPP11
29
Dan Stoza7d7ae732016-03-16 12:23:40 -070030#include <ui/HdrCapabilities.h>
Dan Stoza5df2a862016-03-24 16:19:37 -070031#include <ui/mat4.h>
Dan Stoza7d7ae732016-03-16 12:23:40 -070032
Dan Stoza651bf312015-10-23 17:03:17 -070033#include <utils/Log.h>
34#include <utils/StrongPointer.h>
35#include <utils/Timers.h>
36
37#include <functional>
38#include <string>
39#include <unordered_map>
Dan Stoza9f26a9c2016-06-22 14:51:09 -070040#include <unordered_set>
Dan Stoza651bf312015-10-23 17:03:17 -070041#include <vector>
42
43namespace android {
44 class Fence;
45 class FloatRect;
46 class GraphicBuffer;
47 class Rect;
48 class Region;
Chia-I Wuaab99f52016-10-05 12:59:58 +080049 namespace Hwc2 {
50 class Composer;
51 };
Dan Stoza651bf312015-10-23 17:03:17 -070052}
53
54namespace HWC2 {
55
56class Display;
57class Layer;
58
59typedef std::function<void(std::shared_ptr<Display>, Connection)>
60 HotplugCallback;
61typedef std::function<void(std::shared_ptr<Display>)> RefreshCallback;
62typedef std::function<void(std::shared_ptr<Display>, nsecs_t)> VsyncCallback;
63
64class 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
214class Display : public std::enable_shared_from_this<Display>
215{
216public:
217 Display(Device& device, hwc2_display_t id);
218 ~Display();
219
220 friend class HWC2::Device;
221 friend class HWC2::Layer;
222
223 class Config
224 {
225 public:
226 class Builder
227 {
228 public:
229 Builder(Display& display, hwc2_config_t id);
230
231 std::shared_ptr<const Config> build() {
232 return std::const_pointer_cast<const Config>(
233 std::move(mConfig));
234 }
235
236 Builder& setWidth(int32_t width) {
237 mConfig->mWidth = width;
238 return *this;
239 }
240 Builder& setHeight(int32_t height) {
241 mConfig->mHeight = height;
242 return *this;
243 }
244 Builder& setVsyncPeriod(int32_t vsyncPeriod) {
245 mConfig->mVsyncPeriod = vsyncPeriod;
246 return *this;
247 }
248 Builder& setDpiX(int32_t dpiX) {
249 if (dpiX == -1) {
250 mConfig->mDpiX = getDefaultDensity();
251 } else {
252 mConfig->mDpiX = dpiX / 1000.0f;
253 }
254 return *this;
255 }
256 Builder& setDpiY(int32_t dpiY) {
257 if (dpiY == -1) {
258 mConfig->mDpiY = getDefaultDensity();
259 } else {
260 mConfig->mDpiY = dpiY / 1000.0f;
261 }
262 return *this;
263 }
264
265 private:
266 float getDefaultDensity();
267 std::shared_ptr<Config> mConfig;
268 };
269
270 hwc2_display_t getDisplayId() const { return mDisplay.getId(); }
271 hwc2_config_t getId() const { return mId; }
272
273 int32_t getWidth() const { return mWidth; }
274 int32_t getHeight() const { return mHeight; }
275 nsecs_t getVsyncPeriod() const { return mVsyncPeriod; }
276 float getDpiX() const { return mDpiX; }
277 float getDpiY() const { return mDpiY; }
278
279 private:
280 Config(Display& display, hwc2_config_t id);
281
282 Display& mDisplay;
283 hwc2_config_t mId;
284
285 int32_t mWidth;
286 int32_t mHeight;
287 nsecs_t mVsyncPeriod;
288 float mDpiX;
289 float mDpiY;
290 };
291
292 // Required by HWC2
293
294 [[clang::warn_unused_result]] Error acceptChanges();
295 [[clang::warn_unused_result]] Error createLayer(
296 std::shared_ptr<Layer>* outLayer);
297 [[clang::warn_unused_result]] Error getActiveConfig(
298 std::shared_ptr<const Config>* outConfig) const;
299 [[clang::warn_unused_result]] Error getChangedCompositionTypes(
300 std::unordered_map<std::shared_ptr<Layer>, Composition>* outTypes);
Dan Stoza076ac672016-03-14 10:47:53 -0700301 [[clang::warn_unused_result]] Error getColorModes(
Michael Wright28f24d02016-07-12 13:30:53 -0700302 std::vector<android_color_mode_t>* outModes) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700303
304 // Doesn't call into the HWC2 device, so no errors are possible
305 std::vector<std::shared_ptr<const Config>> getConfigs() const;
306
307 [[clang::warn_unused_result]] Error getName(std::string* outName) const;
308 [[clang::warn_unused_result]] Error getRequests(
309 DisplayRequest* outDisplayRequests,
310 std::unordered_map<std::shared_ptr<Layer>, LayerRequest>*
311 outLayerRequests);
312 [[clang::warn_unused_result]] Error getType(DisplayType* outType) const;
313 [[clang::warn_unused_result]] Error supportsDoze(bool* outSupport) const;
Dan Stoza7d7ae732016-03-16 12:23:40 -0700314 [[clang::warn_unused_result]] Error getHdrCapabilities(
315 std::unique_ptr<android::HdrCapabilities>* outCapabilities) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700316 [[clang::warn_unused_result]] Error getReleaseFences(
317 std::unordered_map<std::shared_ptr<Layer>,
318 android::sp<android::Fence>>* outFences) const;
319 [[clang::warn_unused_result]] Error present(
320 android::sp<android::Fence>* outRetireFence);
321 [[clang::warn_unused_result]] Error setActiveConfig(
322 const std::shared_ptr<const Config>& config);
323 [[clang::warn_unused_result]] Error setClientTarget(
324 buffer_handle_t target,
325 const android::sp<android::Fence>& acquireFence,
326 android_dataspace_t dataspace);
Michael Wright28f24d02016-07-12 13:30:53 -0700327 [[clang::warn_unused_result]] Error setColorMode(android_color_mode_t mode);
Dan Stoza5df2a862016-03-24 16:19:37 -0700328 [[clang::warn_unused_result]] Error setColorTransform(
329 const android::mat4& matrix, android_color_transform_t hint);
Dan Stoza651bf312015-10-23 17:03:17 -0700330 [[clang::warn_unused_result]] Error setOutputBuffer(
331 const android::sp<android::GraphicBuffer>& buffer,
332 const android::sp<android::Fence>& releaseFence);
333 [[clang::warn_unused_result]] Error setPowerMode(PowerMode mode);
334 [[clang::warn_unused_result]] Error setVsyncEnabled(Vsync enabled);
335 [[clang::warn_unused_result]] Error validate(uint32_t* outNumTypes,
336 uint32_t* outNumRequests);
337
338 // Other Display methods
339
340 Device& getDevice() const { return mDevice; }
341 hwc2_display_t getId() const { return mId; }
342 bool isConnected() const { return mIsConnected; }
343
344private:
345 // For use by Device
346
347 // Virtual displays are always connected
348 void setVirtual() {
349 mIsVirtual = true;
350 mIsConnected = true;
351 }
352
353 void setConnected(bool connected) { mIsConnected = connected; }
354 int32_t getAttribute(hwc2_config_t configId, Attribute attribute);
355 void loadConfig(hwc2_config_t configId);
356 void loadConfigs();
357
358 // For use by Layer
359 void destroyLayer(hwc2_layer_t layerId);
360
361 // This may fail (and return a null pointer) if no layer with this ID exists
362 // on this display
363 std::shared_ptr<Layer> getLayerById(hwc2_layer_t id) const;
364
365 // Member variables
366
367 Device& mDevice;
368 hwc2_display_t mId;
369 bool mIsConnected;
370 bool mIsVirtual;
371 std::unordered_map<hwc2_layer_t, std::weak_ptr<Layer>> mLayers;
372 std::unordered_map<hwc2_config_t, std::shared_ptr<const Config>> mConfigs;
373};
374
375class Layer
376{
377public:
378 Layer(const std::shared_ptr<Display>& display, hwc2_layer_t id);
379 ~Layer();
380
381 bool isAbandoned() const { return mDisplay.expired(); }
382 hwc2_layer_t getId() const { return mId; }
383
384 [[clang::warn_unused_result]] Error setCursorPosition(int32_t x, int32_t y);
385 [[clang::warn_unused_result]] Error setBuffer(buffer_handle_t buffer,
386 const android::sp<android::Fence>& acquireFence);
387 [[clang::warn_unused_result]] Error setSurfaceDamage(
388 const android::Region& damage);
389
390 [[clang::warn_unused_result]] Error setBlendMode(BlendMode mode);
391 [[clang::warn_unused_result]] Error setColor(hwc_color_t color);
392 [[clang::warn_unused_result]] Error setCompositionType(Composition type);
Dan Stoza5df2a862016-03-24 16:19:37 -0700393 [[clang::warn_unused_result]] Error setDataspace(
394 android_dataspace_t dataspace);
Dan Stoza651bf312015-10-23 17:03:17 -0700395 [[clang::warn_unused_result]] Error setDisplayFrame(
396 const android::Rect& frame);
397 [[clang::warn_unused_result]] Error setPlaneAlpha(float alpha);
398 [[clang::warn_unused_result]] Error setSidebandStream(
399 const native_handle_t* stream);
400 [[clang::warn_unused_result]] Error setSourceCrop(
401 const android::FloatRect& crop);
402 [[clang::warn_unused_result]] Error setTransform(Transform transform);
403 [[clang::warn_unused_result]] Error setVisibleRegion(
404 const android::Region& region);
405 [[clang::warn_unused_result]] Error setZOrder(uint32_t z);
406
407private:
408 std::weak_ptr<Display> mDisplay;
409 hwc2_display_t mDisplayId;
410 Device& mDevice;
411 hwc2_layer_t mId;
412};
413
414} // namespace HWC2
415
416#endif // ANDROID_SF_HWC2_H