blob: fbe4c7ebedaebce30defca5e4b0d5df8c024f547 [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
Steven Thomas94e35b92017-07-26 18:48:28 -070056// Implement this interface to receive hardware composer events.
57//
58// These callback functions will generally be called on a hwbinder thread, but
59// when first registering the callback the onHotplugReceived() function will
60// immediately be called on the thread calling registerCallback().
61//
62// All calls receive a sequenceId, which will be the value that was supplied to
63// HWC2::Device::registerCallback(). It's used to help differentiate callbacks
64// from different hardware composer instances.
65class ComposerCallback {
66 public:
67 virtual void onHotplugReceived(int32_t sequenceId, hwc2_display_t display,
68 Connection connection,
69 bool primaryDisplay) = 0;
70 virtual void onRefreshReceived(int32_t sequenceId,
71 hwc2_display_t display) = 0;
72 virtual void onVsyncReceived(int32_t sequenceId, hwc2_display_t display,
73 int64_t timestamp) = 0;
74 virtual ~ComposerCallback() = default;
75};
Dan Stoza651bf312015-10-23 17:03:17 -070076
Fabien Sanglard33960702016-11-29 17:58:21 -080077// C++ Wrapper around hwc2_device_t. Load all functions pointers
78// and handle callback registration.
Dan Stoza651bf312015-10-23 17:03:17 -070079class Device
80{
81public:
Kalle Raitaa099a242017-01-11 11:17:29 -080082 // Service name is expected to be 'default' or 'vr' for normal use.
83 // 'vr' will slightly modify the behavior of the mComposer.
84 Device(const std::string& serviceName);
Dan Stoza651bf312015-10-23 17:03:17 -070085
Steven Thomas94e35b92017-07-26 18:48:28 -070086 void registerCallback(ComposerCallback* callback, int32_t sequenceId);
Dan Stoza651bf312015-10-23 17:03:17 -070087
88 // Required by HWC2
89
90 std::string dump() const;
91
Dan Stoza9f26a9c2016-06-22 14:51:09 -070092 const std::unordered_set<Capability>& getCapabilities() const {
Dan Stoza651bf312015-10-23 17:03:17 -070093 return mCapabilities;
94 };
95
96 uint32_t getMaxVirtualDisplayCount() const;
97 Error createVirtualDisplay(uint32_t width, uint32_t height,
Steven Thomas94e35b92017-07-26 18:48:28 -070098 android_pixel_format_t* format, Display** outDisplay);
99 void destroyDisplay(hwc2_display_t displayId);
Dan Stoza651bf312015-10-23 17:03:17 -0700100
Steven Thomas94e35b92017-07-26 18:48:28 -0700101 void onHotplug(hwc2_display_t displayId, Connection connection);
Dan Stoza651bf312015-10-23 17:03:17 -0700102
103 // Other Device methods
104
Steven Thomas94e35b92017-07-26 18:48:28 -0700105 Display* getDisplayById(hwc2_display_t id);
Dan Stoza09e7a272016-04-14 12:31:01 -0700106
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
114 // Member variables
Chia-I Wuaab99f52016-10-05 12:59:58 +0800115 std::unique_ptr<android::Hwc2::Composer> mComposer;
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700116 std::unordered_set<Capability> mCapabilities;
Steven Thomas94e35b92017-07-26 18:48:28 -0700117 std::unordered_map<hwc2_display_t, std::unique_ptr<Display>> mDisplays;
118 bool mRegisteredCallback;
Dan Stoza651bf312015-10-23 17:03:17 -0700119};
120
Fabien Sanglard33960702016-11-29 17:58:21 -0800121// Convenience C++ class to access hwc2_device_t Display functions directly.
Steven Thomas94e35b92017-07-26 18:48:28 -0700122class Display
Dan Stoza651bf312015-10-23 17:03:17 -0700123{
124public:
Steven Thomas94e35b92017-07-26 18:48:28 -0700125 Display(android::Hwc2::Composer& composer,
126 const std::unordered_set<Capability>& capabilities,
127 hwc2_display_t id, DisplayType type);
Dan Stoza651bf312015-10-23 17:03:17 -0700128 ~Display();
129
Dan Stoza651bf312015-10-23 17:03:17 -0700130 class Config
131 {
132 public:
133 class Builder
134 {
135 public:
136 Builder(Display& display, hwc2_config_t id);
137
138 std::shared_ptr<const Config> build() {
139 return std::const_pointer_cast<const Config>(
140 std::move(mConfig));
141 }
142
143 Builder& setWidth(int32_t width) {
144 mConfig->mWidth = width;
145 return *this;
146 }
147 Builder& setHeight(int32_t height) {
148 mConfig->mHeight = height;
149 return *this;
150 }
151 Builder& setVsyncPeriod(int32_t vsyncPeriod) {
152 mConfig->mVsyncPeriod = vsyncPeriod;
153 return *this;
154 }
155 Builder& setDpiX(int32_t dpiX) {
156 if (dpiX == -1) {
157 mConfig->mDpiX = getDefaultDensity();
158 } else {
159 mConfig->mDpiX = dpiX / 1000.0f;
160 }
161 return *this;
162 }
163 Builder& setDpiY(int32_t dpiY) {
164 if (dpiY == -1) {
165 mConfig->mDpiY = getDefaultDensity();
166 } else {
167 mConfig->mDpiY = dpiY / 1000.0f;
168 }
169 return *this;
170 }
171
172 private:
173 float getDefaultDensity();
174 std::shared_ptr<Config> mConfig;
175 };
176
177 hwc2_display_t getDisplayId() const { return mDisplay.getId(); }
178 hwc2_config_t getId() const { return mId; }
179
180 int32_t getWidth() const { return mWidth; }
181 int32_t getHeight() const { return mHeight; }
182 nsecs_t getVsyncPeriod() const { return mVsyncPeriod; }
183 float getDpiX() const { return mDpiX; }
184 float getDpiY() const { return mDpiY; }
185
186 private:
187 Config(Display& display, hwc2_config_t id);
188
189 Display& mDisplay;
190 hwc2_config_t mId;
191
192 int32_t mWidth;
193 int32_t mHeight;
194 nsecs_t mVsyncPeriod;
195 float mDpiX;
196 float mDpiY;
197 };
198
199 // Required by HWC2
200
201 [[clang::warn_unused_result]] Error acceptChanges();
Steven Thomas94e35b92017-07-26 18:48:28 -0700202 [[clang::warn_unused_result]] Error createLayer(Layer** outLayer);
203 [[clang::warn_unused_result]] Error destroyLayer(Layer* layer);
Dan Stoza651bf312015-10-23 17:03:17 -0700204 [[clang::warn_unused_result]] Error getActiveConfig(
205 std::shared_ptr<const Config>* outConfig) const;
206 [[clang::warn_unused_result]] Error getChangedCompositionTypes(
Steven Thomas94e35b92017-07-26 18:48:28 -0700207 std::unordered_map<Layer*, Composition>* outTypes);
Dan Stoza076ac672016-03-14 10:47:53 -0700208 [[clang::warn_unused_result]] Error getColorModes(
Michael Wright28f24d02016-07-12 13:30:53 -0700209 std::vector<android_color_mode_t>* outModes) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700210
211 // Doesn't call into the HWC2 device, so no errors are possible
212 std::vector<std::shared_ptr<const Config>> getConfigs() const;
213
214 [[clang::warn_unused_result]] Error getName(std::string* outName) const;
215 [[clang::warn_unused_result]] Error getRequests(
216 DisplayRequest* outDisplayRequests,
Steven Thomas94e35b92017-07-26 18:48:28 -0700217 std::unordered_map<Layer*, LayerRequest>* outLayerRequests);
Dan Stoza651bf312015-10-23 17:03:17 -0700218 [[clang::warn_unused_result]] Error getType(DisplayType* outType) const;
219 [[clang::warn_unused_result]] Error supportsDoze(bool* outSupport) const;
Dan Stoza7d7ae732016-03-16 12:23:40 -0700220 [[clang::warn_unused_result]] Error getHdrCapabilities(
221 std::unique_ptr<android::HdrCapabilities>* outCapabilities) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700222 [[clang::warn_unused_result]] Error getReleaseFences(
Steven Thomas94e35b92017-07-26 18:48:28 -0700223 std::unordered_map<Layer*,
Dan Stoza651bf312015-10-23 17:03:17 -0700224 android::sp<android::Fence>>* outFences) const;
225 [[clang::warn_unused_result]] Error present(
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800226 android::sp<android::Fence>* outPresentFence);
Dan Stoza651bf312015-10-23 17:03:17 -0700227 [[clang::warn_unused_result]] Error setActiveConfig(
228 const std::shared_ptr<const Config>& config);
229 [[clang::warn_unused_result]] Error setClientTarget(
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400230 uint32_t slot, const android::sp<android::GraphicBuffer>& target,
Dan Stoza651bf312015-10-23 17:03:17 -0700231 const android::sp<android::Fence>& acquireFence,
232 android_dataspace_t dataspace);
Michael Wright28f24d02016-07-12 13:30:53 -0700233 [[clang::warn_unused_result]] Error setColorMode(android_color_mode_t mode);
Dan Stoza5df2a862016-03-24 16:19:37 -0700234 [[clang::warn_unused_result]] Error setColorTransform(
235 const android::mat4& matrix, android_color_transform_t hint);
Dan Stoza651bf312015-10-23 17:03:17 -0700236 [[clang::warn_unused_result]] Error setOutputBuffer(
237 const android::sp<android::GraphicBuffer>& buffer,
238 const android::sp<android::Fence>& releaseFence);
239 [[clang::warn_unused_result]] Error setPowerMode(PowerMode mode);
240 [[clang::warn_unused_result]] Error setVsyncEnabled(Vsync enabled);
241 [[clang::warn_unused_result]] Error validate(uint32_t* outNumTypes,
242 uint32_t* outNumRequests);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700243 [[clang::warn_unused_result]] Error presentOrValidate(uint32_t* outNumTypes,
244 uint32_t* outNumRequests,
245 android::sp<android::Fence>* outPresentFence, uint32_t* state);
Dan Stoza651bf312015-10-23 17:03:17 -0700246
Chia-I Wu0c6ce462017-06-22 10:48:28 -0700247 // Most methods in this class write a command to a command buffer. The
248 // command buffer is implicitly submitted in validate, present, and
249 // presentOrValidate. This method provides a way to discard the commands,
250 // which can be used to discard stale commands.
251 void discardCommands();
252
Dan Stoza651bf312015-10-23 17:03:17 -0700253 // Other Display methods
254
Dan Stoza651bf312015-10-23 17:03:17 -0700255 hwc2_display_t getId() const { return mId; }
256 bool isConnected() const { return mIsConnected; }
Steven Thomas94e35b92017-07-26 18:48:28 -0700257 void setConnected(bool connected); // For use by Device only
Dan Stoza651bf312015-10-23 17:03:17 -0700258
259private:
Dan Stoza651bf312015-10-23 17:03:17 -0700260 int32_t getAttribute(hwc2_config_t configId, Attribute attribute);
261 void loadConfig(hwc2_config_t configId);
262 void loadConfigs();
263
Dan Stoza651bf312015-10-23 17:03:17 -0700264 // This may fail (and return a null pointer) if no layer with this ID exists
265 // on this display
Steven Thomas94e35b92017-07-26 18:48:28 -0700266 Layer* getLayerById(hwc2_layer_t id) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700267
268 // Member variables
269
Steven Thomas94e35b92017-07-26 18:48:28 -0700270 // These are references to data owned by HWC2::Device, which will outlive
271 // this HWC2::Display, so these references are guaranteed to be valid for
272 // the lifetime of this object.
273 android::Hwc2::Composer& mComposer;
274 const std::unordered_set<Capability>& mCapabilities;
275
Dan Stoza651bf312015-10-23 17:03:17 -0700276 hwc2_display_t mId;
277 bool mIsConnected;
Chris Forbes016d73c2017-04-11 10:04:31 -0700278 DisplayType mType;
Steven Thomas94e35b92017-07-26 18:48:28 -0700279 std::unordered_map<hwc2_layer_t, std::unique_ptr<Layer>> mLayers;
Alistair Strachanc1752532017-06-07 16:34:44 -0700280 // The ordering in this map matters, for getConfigs(), when it is
281 // converted to a vector
282 std::map<hwc2_config_t, std::shared_ptr<const Config>> mConfigs;
Dan Stoza651bf312015-10-23 17:03:17 -0700283};
284
Fabien Sanglard33960702016-11-29 17:58:21 -0800285// Convenience C++ class to access hwc2_device_t Layer functions directly.
Dan Stoza651bf312015-10-23 17:03:17 -0700286class Layer
287{
288public:
Steven Thomas94e35b92017-07-26 18:48:28 -0700289 Layer(android::Hwc2::Composer& composer,
290 const std::unordered_set<Capability>& capabilities,
291 hwc2_display_t displayId, hwc2_layer_t layerId);
Dan Stoza651bf312015-10-23 17:03:17 -0700292 ~Layer();
293
Dan Stoza651bf312015-10-23 17:03:17 -0700294 hwc2_layer_t getId() const { return mId; }
295
Steven Thomas94e35b92017-07-26 18:48:28 -0700296 // Register a listener to be notified when the layer is destroyed. When the
297 // listener function is called, the Layer will be in the process of being
298 // destroyed, so it's not safe to call methods on it.
299 void setLayerDestroyedListener(std::function<void(Layer*)> listener);
300
Dan Stoza651bf312015-10-23 17:03:17 -0700301 [[clang::warn_unused_result]] Error setCursorPosition(int32_t x, int32_t y);
Chia-I Wu06d63de2017-01-04 14:58:51 +0800302 [[clang::warn_unused_result]] Error setBuffer(uint32_t slot,
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400303 const android::sp<android::GraphicBuffer>& buffer,
Dan Stoza651bf312015-10-23 17:03:17 -0700304 const android::sp<android::Fence>& acquireFence);
305 [[clang::warn_unused_result]] Error setSurfaceDamage(
306 const android::Region& damage);
307
308 [[clang::warn_unused_result]] Error setBlendMode(BlendMode mode);
309 [[clang::warn_unused_result]] Error setColor(hwc_color_t color);
310 [[clang::warn_unused_result]] Error setCompositionType(Composition type);
Dan Stoza5df2a862016-03-24 16:19:37 -0700311 [[clang::warn_unused_result]] Error setDataspace(
312 android_dataspace_t dataspace);
Dan Stoza651bf312015-10-23 17:03:17 -0700313 [[clang::warn_unused_result]] Error setDisplayFrame(
314 const android::Rect& frame);
315 [[clang::warn_unused_result]] Error setPlaneAlpha(float alpha);
316 [[clang::warn_unused_result]] Error setSidebandStream(
317 const native_handle_t* stream);
318 [[clang::warn_unused_result]] Error setSourceCrop(
Dan Stoza5a423ea2017-02-16 14:10:39 -0800319 const android::FloatRect& crop);
Dan Stoza651bf312015-10-23 17:03:17 -0700320 [[clang::warn_unused_result]] Error setTransform(Transform transform);
321 [[clang::warn_unused_result]] Error setVisibleRegion(
322 const android::Region& region);
323 [[clang::warn_unused_result]] Error setZOrder(uint32_t z);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500324 [[clang::warn_unused_result]] Error setInfo(uint32_t type, uint32_t appId);
Dan Stoza651bf312015-10-23 17:03:17 -0700325
326private:
Steven Thomas94e35b92017-07-26 18:48:28 -0700327 // These are references to data owned by HWC2::Device, which will outlive
328 // this HWC2::Layer, so these references are guaranteed to be valid for
329 // the lifetime of this object.
330 android::Hwc2::Composer& mComposer;
331 const std::unordered_set<Capability>& mCapabilities;
332
Dan Stoza651bf312015-10-23 17:03:17 -0700333 hwc2_display_t mDisplayId;
Dan Stoza651bf312015-10-23 17:03:17 -0700334 hwc2_layer_t mId;
Courtney Goeltzenleuchterc988ee42017-05-31 17:56:46 -0600335 android_dataspace mDataSpace = HAL_DATASPACE_UNKNOWN;
Steven Thomas94e35b92017-07-26 18:48:28 -0700336 std::function<void(Layer*)> mLayerDestroyedListener;
Dan Stoza651bf312015-10-23 17:03:17 -0700337};
338
339} // namespace HWC2
340
341#endif // ANDROID_SF_HWC2_H