blob: 97582a7a99440a5c8c4783074a2cc202459e5c17 [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:
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -080065 // useVrComposer is passed to the composer HAL. When true, the composer HAL
66 // will use the vr composer service, otherwise it uses the real hardware
67 // composer.
68 Device(bool useVrComposer);
Dan Stoza651bf312015-10-23 17:03:17 -070069 ~Device();
70
71 friend class HWC2::Display;
72 friend class HWC2::Layer;
73
74 // Required by HWC2
75
76 std::string dump() const;
77
Dan Stoza9f26a9c2016-06-22 14:51:09 -070078 const std::unordered_set<Capability>& getCapabilities() const {
Dan Stoza651bf312015-10-23 17:03:17 -070079 return mCapabilities;
80 };
81
82 uint32_t getMaxVirtualDisplayCount() const;
83 Error createVirtualDisplay(uint32_t width, uint32_t height,
Dan Stoza5cf424b2016-05-20 14:02:39 -070084 android_pixel_format_t* format,
Dan Stoza651bf312015-10-23 17:03:17 -070085 std::shared_ptr<Display>* outDisplay);
86
87 void registerHotplugCallback(HotplugCallback hotplug);
88 void registerRefreshCallback(RefreshCallback refresh);
89 void registerVsyncCallback(VsyncCallback vsync);
90
91 // For use by callbacks
92
93 void callHotplug(std::shared_ptr<Display> display, Connection connected);
94 void callRefresh(std::shared_ptr<Display> display);
95 void callVsync(std::shared_ptr<Display> display, nsecs_t timestamp);
96
97 // Other Device methods
98
99 // This will create a Display if one is not found, but it will not be marked
Dan Stoza38628982016-07-13 15:48:58 -0700100 // as connected. This Display may be null if the display has been torn down
101 // but has not been removed from the map yet.
Dan Stoza651bf312015-10-23 17:03:17 -0700102 std::shared_ptr<Display> getDisplayById(hwc2_display_t id);
103
Dan Stoza09e7a272016-04-14 12:31:01 -0700104 bool hasCapability(HWC2::Capability capability) const;
105
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800106 android::Hwc2::Composer* getComposer() { return mComposer.get(); }
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800107
Dan Stoza651bf312015-10-23 17:03:17 -0700108private:
109 // Initialization methods
110
Dan Stoza651bf312015-10-23 17:03:17 -0700111 void loadCapabilities();
Dan Stoza651bf312015-10-23 17:03:17 -0700112 void registerCallbacks();
113
114 // For use by Display
115
116 void destroyVirtualDisplay(hwc2_display_t display);
117
118 // Member variables
Chia-I Wuaab99f52016-10-05 12:59:58 +0800119 std::unique_ptr<android::Hwc2::Composer> mComposer;
Dan Stoza651bf312015-10-23 17:03:17 -0700120
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700121 std::unordered_set<Capability> mCapabilities;
Dan Stoza38628982016-07-13 15:48:58 -0700122 std::unordered_map<hwc2_display_t, std::weak_ptr<Display>> mDisplays;
Dan Stoza651bf312015-10-23 17:03:17 -0700123
124 HotplugCallback mHotplug;
125 std::vector<std::pair<std::shared_ptr<Display>, Connection>>
126 mPendingHotplugs;
127 RefreshCallback mRefresh;
128 std::vector<std::shared_ptr<Display>> mPendingRefreshes;
129 VsyncCallback mVsync;
130 std::vector<std::pair<std::shared_ptr<Display>, nsecs_t>> mPendingVsyncs;
131};
132
Fabien Sanglard33960702016-11-29 17:58:21 -0800133// Convenience C++ class to access hwc2_device_t Display functions directly.
Dan Stoza651bf312015-10-23 17:03:17 -0700134class Display : public std::enable_shared_from_this<Display>
135{
136public:
137 Display(Device& device, hwc2_display_t id);
138 ~Display();
139
140 friend class HWC2::Device;
141 friend class HWC2::Layer;
142
143 class Config
144 {
145 public:
146 class Builder
147 {
148 public:
149 Builder(Display& display, hwc2_config_t id);
150
151 std::shared_ptr<const Config> build() {
152 return std::const_pointer_cast<const Config>(
153 std::move(mConfig));
154 }
155
156 Builder& setWidth(int32_t width) {
157 mConfig->mWidth = width;
158 return *this;
159 }
160 Builder& setHeight(int32_t height) {
161 mConfig->mHeight = height;
162 return *this;
163 }
164 Builder& setVsyncPeriod(int32_t vsyncPeriod) {
165 mConfig->mVsyncPeriod = vsyncPeriod;
166 return *this;
167 }
168 Builder& setDpiX(int32_t dpiX) {
169 if (dpiX == -1) {
170 mConfig->mDpiX = getDefaultDensity();
171 } else {
172 mConfig->mDpiX = dpiX / 1000.0f;
173 }
174 return *this;
175 }
176 Builder& setDpiY(int32_t dpiY) {
177 if (dpiY == -1) {
178 mConfig->mDpiY = getDefaultDensity();
179 } else {
180 mConfig->mDpiY = dpiY / 1000.0f;
181 }
182 return *this;
183 }
184
185 private:
186 float getDefaultDensity();
187 std::shared_ptr<Config> mConfig;
188 };
189
190 hwc2_display_t getDisplayId() const { return mDisplay.getId(); }
191 hwc2_config_t getId() const { return mId; }
192
193 int32_t getWidth() const { return mWidth; }
194 int32_t getHeight() const { return mHeight; }
195 nsecs_t getVsyncPeriod() const { return mVsyncPeriod; }
196 float getDpiX() const { return mDpiX; }
197 float getDpiY() const { return mDpiY; }
198
199 private:
200 Config(Display& display, hwc2_config_t id);
201
202 Display& mDisplay;
203 hwc2_config_t mId;
204
205 int32_t mWidth;
206 int32_t mHeight;
207 nsecs_t mVsyncPeriod;
208 float mDpiX;
209 float mDpiY;
210 };
211
212 // Required by HWC2
213
214 [[clang::warn_unused_result]] Error acceptChanges();
215 [[clang::warn_unused_result]] Error createLayer(
216 std::shared_ptr<Layer>* outLayer);
217 [[clang::warn_unused_result]] Error getActiveConfig(
218 std::shared_ptr<const Config>* outConfig) const;
219 [[clang::warn_unused_result]] Error getChangedCompositionTypes(
220 std::unordered_map<std::shared_ptr<Layer>, Composition>* outTypes);
Dan Stoza076ac672016-03-14 10:47:53 -0700221 [[clang::warn_unused_result]] Error getColorModes(
Michael Wright28f24d02016-07-12 13:30:53 -0700222 std::vector<android_color_mode_t>* outModes) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700223
224 // Doesn't call into the HWC2 device, so no errors are possible
225 std::vector<std::shared_ptr<const Config>> getConfigs() const;
226
227 [[clang::warn_unused_result]] Error getName(std::string* outName) const;
228 [[clang::warn_unused_result]] Error getRequests(
229 DisplayRequest* outDisplayRequests,
230 std::unordered_map<std::shared_ptr<Layer>, LayerRequest>*
231 outLayerRequests);
232 [[clang::warn_unused_result]] Error getType(DisplayType* outType) const;
233 [[clang::warn_unused_result]] Error supportsDoze(bool* outSupport) const;
Dan Stoza7d7ae732016-03-16 12:23:40 -0700234 [[clang::warn_unused_result]] Error getHdrCapabilities(
235 std::unique_ptr<android::HdrCapabilities>* outCapabilities) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700236 [[clang::warn_unused_result]] Error getReleaseFences(
237 std::unordered_map<std::shared_ptr<Layer>,
238 android::sp<android::Fence>>* outFences) const;
239 [[clang::warn_unused_result]] Error present(
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800240 android::sp<android::Fence>* outPresentFence);
Dan Stoza651bf312015-10-23 17:03:17 -0700241 [[clang::warn_unused_result]] Error setActiveConfig(
242 const std::shared_ptr<const Config>& config);
243 [[clang::warn_unused_result]] Error setClientTarget(
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400244 uint32_t slot, const android::sp<android::GraphicBuffer>& target,
Dan Stoza651bf312015-10-23 17:03:17 -0700245 const android::sp<android::Fence>& acquireFence,
246 android_dataspace_t dataspace);
Michael Wright28f24d02016-07-12 13:30:53 -0700247 [[clang::warn_unused_result]] Error setColorMode(android_color_mode_t mode);
Dan Stoza5df2a862016-03-24 16:19:37 -0700248 [[clang::warn_unused_result]] Error setColorTransform(
249 const android::mat4& matrix, android_color_transform_t hint);
Dan Stoza651bf312015-10-23 17:03:17 -0700250 [[clang::warn_unused_result]] Error setOutputBuffer(
251 const android::sp<android::GraphicBuffer>& buffer,
252 const android::sp<android::Fence>& releaseFence);
253 [[clang::warn_unused_result]] Error setPowerMode(PowerMode mode);
254 [[clang::warn_unused_result]] Error setVsyncEnabled(Vsync enabled);
255 [[clang::warn_unused_result]] Error validate(uint32_t* outNumTypes,
256 uint32_t* outNumRequests);
257
258 // Other Display methods
259
260 Device& getDevice() const { return mDevice; }
261 hwc2_display_t getId() const { return mId; }
262 bool isConnected() const { return mIsConnected; }
263
264private:
265 // For use by Device
266
Dan Stoza651bf312015-10-23 17:03:17 -0700267 void setConnected(bool connected) { mIsConnected = connected; }
268 int32_t getAttribute(hwc2_config_t configId, Attribute attribute);
269 void loadConfig(hwc2_config_t configId);
270 void loadConfigs();
271
272 // For use by Layer
273 void destroyLayer(hwc2_layer_t layerId);
274
275 // This may fail (and return a null pointer) if no layer with this ID exists
276 // on this display
277 std::shared_ptr<Layer> getLayerById(hwc2_layer_t id) const;
278
279 // Member variables
280
281 Device& mDevice;
282 hwc2_display_t mId;
283 bool mIsConnected;
Chris Forbes016d73c2017-04-11 10:04:31 -0700284 DisplayType mType;
Dan Stoza651bf312015-10-23 17:03:17 -0700285 std::unordered_map<hwc2_layer_t, std::weak_ptr<Layer>> mLayers;
286 std::unordered_map<hwc2_config_t, std::shared_ptr<const Config>> mConfigs;
287};
288
Fabien Sanglard33960702016-11-29 17:58:21 -0800289// Convenience C++ class to access hwc2_device_t Layer functions directly.
Dan Stoza651bf312015-10-23 17:03:17 -0700290class Layer
291{
292public:
293 Layer(const std::shared_ptr<Display>& display, hwc2_layer_t id);
294 ~Layer();
295
296 bool isAbandoned() const { return mDisplay.expired(); }
297 hwc2_layer_t getId() const { return mId; }
298
299 [[clang::warn_unused_result]] Error setCursorPosition(int32_t x, int32_t y);
Chia-I Wu06d63de2017-01-04 14:58:51 +0800300 [[clang::warn_unused_result]] Error setBuffer(uint32_t slot,
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400301 const android::sp<android::GraphicBuffer>& buffer,
Dan Stoza651bf312015-10-23 17:03:17 -0700302 const android::sp<android::Fence>& acquireFence);
303 [[clang::warn_unused_result]] Error setSurfaceDamage(
304 const android::Region& damage);
305
306 [[clang::warn_unused_result]] Error setBlendMode(BlendMode mode);
307 [[clang::warn_unused_result]] Error setColor(hwc_color_t color);
308 [[clang::warn_unused_result]] Error setCompositionType(Composition type);
Dan Stoza5df2a862016-03-24 16:19:37 -0700309 [[clang::warn_unused_result]] Error setDataspace(
310 android_dataspace_t dataspace);
Dan Stoza651bf312015-10-23 17:03:17 -0700311 [[clang::warn_unused_result]] Error setDisplayFrame(
312 const android::Rect& frame);
313 [[clang::warn_unused_result]] Error setPlaneAlpha(float alpha);
314 [[clang::warn_unused_result]] Error setSidebandStream(
315 const native_handle_t* stream);
316 [[clang::warn_unused_result]] Error setSourceCrop(
Dan Stoza5a423ea2017-02-16 14:10:39 -0800317 const android::FloatRect& crop);
Dan Stoza651bf312015-10-23 17:03:17 -0700318 [[clang::warn_unused_result]] Error setTransform(Transform transform);
319 [[clang::warn_unused_result]] Error setVisibleRegion(
320 const android::Region& region);
321 [[clang::warn_unused_result]] Error setZOrder(uint32_t z);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500322 [[clang::warn_unused_result]] Error setInfo(uint32_t type, uint32_t appId);
Dan Stoza651bf312015-10-23 17:03:17 -0700323
324private:
325 std::weak_ptr<Display> mDisplay;
326 hwc2_display_t mDisplayId;
327 Device& mDevice;
328 hwc2_layer_t mId;
329};
330
331} // namespace HWC2
332
333#endif // ANDROID_SF_HWC2_H