blob: 7f26e56a63af5d986181809adbaa2c4d0678616b [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>
Alistair Strachanc1752532017-06-07 16:34:44 -070038#include <map>
Dan Stoza651bf312015-10-23 17:03:17 -070039
40namespace android {
41 class Fence;
Dan Stoza5a423ea2017-02-16 14:10:39 -080042 class FloatRect;
Dan Stoza651bf312015-10-23 17:03:17 -070043 class GraphicBuffer;
44 class Rect;
45 class Region;
Chia-I Wuaab99f52016-10-05 12:59:58 +080046 namespace Hwc2 {
47 class Composer;
Dan Stoza71bded52016-10-19 11:10:33 -070048 }
Dan Stoza651bf312015-10-23 17:03:17 -070049}
50
51namespace HWC2 {
52
53class Display;
54class Layer;
55
56typedef std::function<void(std::shared_ptr<Display>, Connection)>
57 HotplugCallback;
58typedef std::function<void(std::shared_ptr<Display>)> RefreshCallback;
59typedef std::function<void(std::shared_ptr<Display>, nsecs_t)> VsyncCallback;
60
Fabien Sanglard33960702016-11-29 17:58:21 -080061// C++ Wrapper around hwc2_device_t. Load all functions pointers
62// and handle callback registration.
Dan Stoza651bf312015-10-23 17:03:17 -070063class Device
64{
65public:
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -080066 // useVrComposer is passed to the composer HAL. When true, the composer HAL
67 // will use the vr composer service, otherwise it uses the real hardware
68 // composer.
69 Device(bool useVrComposer);
Dan Stoza651bf312015-10-23 17:03:17 -070070 ~Device();
71
72 friend class HWC2::Display;
73 friend class HWC2::Layer;
74
75 // Required by HWC2
76
77 std::string dump() const;
78
Dan Stoza9f26a9c2016-06-22 14:51:09 -070079 const std::unordered_set<Capability>& getCapabilities() const {
Dan Stoza651bf312015-10-23 17:03:17 -070080 return mCapabilities;
81 };
82
83 uint32_t getMaxVirtualDisplayCount() const;
84 Error createVirtualDisplay(uint32_t width, uint32_t height,
Dan Stoza5cf424b2016-05-20 14:02:39 -070085 android_pixel_format_t* format,
Dan Stoza651bf312015-10-23 17:03:17 -070086 std::shared_ptr<Display>* outDisplay);
87
88 void registerHotplugCallback(HotplugCallback hotplug);
89 void registerRefreshCallback(RefreshCallback refresh);
90 void registerVsyncCallback(VsyncCallback vsync);
91
92 // For use by callbacks
93
94 void callHotplug(std::shared_ptr<Display> display, Connection connected);
95 void callRefresh(std::shared_ptr<Display> display);
96 void callVsync(std::shared_ptr<Display> display, nsecs_t timestamp);
97
98 // Other Device methods
99
100 // This will create a Display if one is not found, but it will not be marked
Dan Stoza38628982016-07-13 15:48:58 -0700101 // as connected. This Display may be null if the display has been torn down
102 // but has not been removed from the map yet.
Dan Stoza651bf312015-10-23 17:03:17 -0700103 std::shared_ptr<Display> getDisplayById(hwc2_display_t id);
104
Dan Stoza09e7a272016-04-14 12:31:01 -0700105 bool hasCapability(HWC2::Capability capability) const;
106
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800107 android::Hwc2::Composer* getComposer() { return mComposer.get(); }
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800108
Dan Stoza651bf312015-10-23 17:03:17 -0700109private:
110 // Initialization methods
111
Dan Stoza651bf312015-10-23 17:03:17 -0700112 void loadCapabilities();
Dan Stoza651bf312015-10-23 17:03:17 -0700113 void registerCallbacks();
114
115 // For use by Display
116
117 void destroyVirtualDisplay(hwc2_display_t display);
118
119 // Member variables
Chia-I Wuaab99f52016-10-05 12:59:58 +0800120 std::unique_ptr<android::Hwc2::Composer> mComposer;
Dan Stoza651bf312015-10-23 17:03:17 -0700121
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700122 std::unordered_set<Capability> mCapabilities;
Dan Stoza38628982016-07-13 15:48:58 -0700123 std::unordered_map<hwc2_display_t, std::weak_ptr<Display>> mDisplays;
Dan Stoza651bf312015-10-23 17:03:17 -0700124
125 HotplugCallback mHotplug;
126 std::vector<std::pair<std::shared_ptr<Display>, Connection>>
127 mPendingHotplugs;
128 RefreshCallback mRefresh;
129 std::vector<std::shared_ptr<Display>> mPendingRefreshes;
130 VsyncCallback mVsync;
131 std::vector<std::pair<std::shared_ptr<Display>, nsecs_t>> mPendingVsyncs;
132};
133
Fabien Sanglard33960702016-11-29 17:58:21 -0800134// Convenience C++ class to access hwc2_device_t Display functions directly.
Dan Stoza651bf312015-10-23 17:03:17 -0700135class Display : public std::enable_shared_from_this<Display>
136{
137public:
138 Display(Device& device, hwc2_display_t id);
139 ~Display();
140
141 friend class HWC2::Device;
142 friend class HWC2::Layer;
143
144 class Config
145 {
146 public:
147 class Builder
148 {
149 public:
150 Builder(Display& display, hwc2_config_t id);
151
152 std::shared_ptr<const Config> build() {
153 return std::const_pointer_cast<const Config>(
154 std::move(mConfig));
155 }
156
157 Builder& setWidth(int32_t width) {
158 mConfig->mWidth = width;
159 return *this;
160 }
161 Builder& setHeight(int32_t height) {
162 mConfig->mHeight = height;
163 return *this;
164 }
165 Builder& setVsyncPeriod(int32_t vsyncPeriod) {
166 mConfig->mVsyncPeriod = vsyncPeriod;
167 return *this;
168 }
169 Builder& setDpiX(int32_t dpiX) {
170 if (dpiX == -1) {
171 mConfig->mDpiX = getDefaultDensity();
172 } else {
173 mConfig->mDpiX = dpiX / 1000.0f;
174 }
175 return *this;
176 }
177 Builder& setDpiY(int32_t dpiY) {
178 if (dpiY == -1) {
179 mConfig->mDpiY = getDefaultDensity();
180 } else {
181 mConfig->mDpiY = dpiY / 1000.0f;
182 }
183 return *this;
184 }
185
186 private:
187 float getDefaultDensity();
188 std::shared_ptr<Config> mConfig;
189 };
190
191 hwc2_display_t getDisplayId() const { return mDisplay.getId(); }
192 hwc2_config_t getId() const { return mId; }
193
194 int32_t getWidth() const { return mWidth; }
195 int32_t getHeight() const { return mHeight; }
196 nsecs_t getVsyncPeriod() const { return mVsyncPeriod; }
197 float getDpiX() const { return mDpiX; }
198 float getDpiY() const { return mDpiY; }
199
200 private:
201 Config(Display& display, hwc2_config_t id);
202
203 Display& mDisplay;
204 hwc2_config_t mId;
205
206 int32_t mWidth;
207 int32_t mHeight;
208 nsecs_t mVsyncPeriod;
209 float mDpiX;
210 float mDpiY;
211 };
212
213 // Required by HWC2
214
215 [[clang::warn_unused_result]] Error acceptChanges();
216 [[clang::warn_unused_result]] Error createLayer(
217 std::shared_ptr<Layer>* outLayer);
218 [[clang::warn_unused_result]] Error getActiveConfig(
219 std::shared_ptr<const Config>* outConfig) const;
220 [[clang::warn_unused_result]] Error getChangedCompositionTypes(
221 std::unordered_map<std::shared_ptr<Layer>, Composition>* outTypes);
Dan Stoza076ac672016-03-14 10:47:53 -0700222 [[clang::warn_unused_result]] Error getColorModes(
Michael Wright28f24d02016-07-12 13:30:53 -0700223 std::vector<android_color_mode_t>* outModes) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700224
225 // Doesn't call into the HWC2 device, so no errors are possible
226 std::vector<std::shared_ptr<const Config>> getConfigs() const;
227
228 [[clang::warn_unused_result]] Error getName(std::string* outName) const;
229 [[clang::warn_unused_result]] Error getRequests(
230 DisplayRequest* outDisplayRequests,
231 std::unordered_map<std::shared_ptr<Layer>, LayerRequest>*
232 outLayerRequests);
233 [[clang::warn_unused_result]] Error getType(DisplayType* outType) const;
234 [[clang::warn_unused_result]] Error supportsDoze(bool* outSupport) const;
Dan Stoza7d7ae732016-03-16 12:23:40 -0700235 [[clang::warn_unused_result]] Error getHdrCapabilities(
236 std::unique_ptr<android::HdrCapabilities>* outCapabilities) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700237 [[clang::warn_unused_result]] Error getReleaseFences(
238 std::unordered_map<std::shared_ptr<Layer>,
239 android::sp<android::Fence>>* outFences) const;
240 [[clang::warn_unused_result]] Error present(
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800241 android::sp<android::Fence>* outPresentFence);
Dan Stoza651bf312015-10-23 17:03:17 -0700242 [[clang::warn_unused_result]] Error setActiveConfig(
243 const std::shared_ptr<const Config>& config);
244 [[clang::warn_unused_result]] Error setClientTarget(
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400245 uint32_t slot, const android::sp<android::GraphicBuffer>& target,
Dan Stoza651bf312015-10-23 17:03:17 -0700246 const android::sp<android::Fence>& acquireFence,
247 android_dataspace_t dataspace);
Michael Wright28f24d02016-07-12 13:30:53 -0700248 [[clang::warn_unused_result]] Error setColorMode(android_color_mode_t mode);
Dan Stoza5df2a862016-03-24 16:19:37 -0700249 [[clang::warn_unused_result]] Error setColorTransform(
250 const android::mat4& matrix, android_color_transform_t hint);
Dan Stoza651bf312015-10-23 17:03:17 -0700251 [[clang::warn_unused_result]] Error setOutputBuffer(
252 const android::sp<android::GraphicBuffer>& buffer,
253 const android::sp<android::Fence>& releaseFence);
254 [[clang::warn_unused_result]] Error setPowerMode(PowerMode mode);
255 [[clang::warn_unused_result]] Error setVsyncEnabled(Vsync enabled);
256 [[clang::warn_unused_result]] Error validate(uint32_t* outNumTypes,
257 uint32_t* outNumRequests);
258
259 // Other Display methods
260
261 Device& getDevice() const { return mDevice; }
262 hwc2_display_t getId() const { return mId; }
263 bool isConnected() const { return mIsConnected; }
264
265private:
266 // For use by Device
267
Dan Stoza651bf312015-10-23 17:03:17 -0700268 void setConnected(bool connected) { mIsConnected = connected; }
269 int32_t getAttribute(hwc2_config_t configId, Attribute attribute);
270 void loadConfig(hwc2_config_t configId);
271 void loadConfigs();
272
273 // For use by Layer
274 void destroyLayer(hwc2_layer_t layerId);
275
276 // This may fail (and return a null pointer) if no layer with this ID exists
277 // on this display
278 std::shared_ptr<Layer> getLayerById(hwc2_layer_t id) const;
279
280 // Member variables
281
282 Device& mDevice;
283 hwc2_display_t mId;
284 bool mIsConnected;
Chris Forbes016d73c2017-04-11 10:04:31 -0700285 DisplayType mType;
Dan Stoza651bf312015-10-23 17:03:17 -0700286 std::unordered_map<hwc2_layer_t, std::weak_ptr<Layer>> mLayers;
Alistair Strachanc1752532017-06-07 16:34:44 -0700287 // The ordering in this map matters, for getConfigs(), when it is
288 // converted to a vector
289 std::map<hwc2_config_t, std::shared_ptr<const Config>> mConfigs;
Dan Stoza651bf312015-10-23 17:03:17 -0700290};
291
Fabien Sanglard33960702016-11-29 17:58:21 -0800292// Convenience C++ class to access hwc2_device_t Layer functions directly.
Dan Stoza651bf312015-10-23 17:03:17 -0700293class Layer
294{
295public:
296 Layer(const std::shared_ptr<Display>& display, hwc2_layer_t id);
297 ~Layer();
298
299 bool isAbandoned() const { return mDisplay.expired(); }
300 hwc2_layer_t getId() const { return mId; }
301
302 [[clang::warn_unused_result]] Error setCursorPosition(int32_t x, int32_t y);
Chia-I Wu06d63de2017-01-04 14:58:51 +0800303 [[clang::warn_unused_result]] Error setBuffer(uint32_t slot,
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400304 const android::sp<android::GraphicBuffer>& buffer,
Dan Stoza651bf312015-10-23 17:03:17 -0700305 const android::sp<android::Fence>& acquireFence);
306 [[clang::warn_unused_result]] Error setSurfaceDamage(
307 const android::Region& damage);
308
309 [[clang::warn_unused_result]] Error setBlendMode(BlendMode mode);
310 [[clang::warn_unused_result]] Error setColor(hwc_color_t color);
311 [[clang::warn_unused_result]] Error setCompositionType(Composition type);
Dan Stoza5df2a862016-03-24 16:19:37 -0700312 [[clang::warn_unused_result]] Error setDataspace(
313 android_dataspace_t dataspace);
Dan Stoza651bf312015-10-23 17:03:17 -0700314 [[clang::warn_unused_result]] Error setDisplayFrame(
315 const android::Rect& frame);
316 [[clang::warn_unused_result]] Error setPlaneAlpha(float alpha);
317 [[clang::warn_unused_result]] Error setSidebandStream(
318 const native_handle_t* stream);
319 [[clang::warn_unused_result]] Error setSourceCrop(
Dan Stoza5a423ea2017-02-16 14:10:39 -0800320 const android::FloatRect& crop);
Dan Stoza651bf312015-10-23 17:03:17 -0700321 [[clang::warn_unused_result]] Error setTransform(Transform transform);
322 [[clang::warn_unused_result]] Error setVisibleRegion(
323 const android::Region& region);
324 [[clang::warn_unused_result]] Error setZOrder(uint32_t z);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500325 [[clang::warn_unused_result]] Error setInfo(uint32_t type, uint32_t appId);
Dan Stoza651bf312015-10-23 17:03:17 -0700326
327private:
328 std::weak_ptr<Display> mDisplay;
329 hwc2_display_t mDisplayId;
330 Device& mDevice;
331 hwc2_layer_t mId;
332};
333
334} // namespace HWC2
335
336#endif // ANDROID_SF_HWC2_H