blob: 949f0e3f825cf28fe5026eeb391c7831782960c7 [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 Thomasd7f49c52017-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:
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -080082 // useVrComposer is passed to the composer HAL. When true, the composer HAL
83 // will use the vr composer service, otherwise it uses the real hardware
84 // composer.
85 Device(bool useVrComposer);
Dan Stoza651bf312015-10-23 17:03:17 -070086
Steven Thomasd7f49c52017-07-26 18:48:28 -070087 void registerCallback(ComposerCallback* callback, int32_t sequenceId);
Dan Stoza651bf312015-10-23 17:03:17 -070088
89 // Required by HWC2
90
91 std::string dump() const;
92
Dan Stoza9f26a9c2016-06-22 14:51:09 -070093 const std::unordered_set<Capability>& getCapabilities() const {
Dan Stoza651bf312015-10-23 17:03:17 -070094 return mCapabilities;
95 };
96
97 uint32_t getMaxVirtualDisplayCount() const;
98 Error createVirtualDisplay(uint32_t width, uint32_t height,
Steven Thomasd7f49c52017-07-26 18:48:28 -070099 android_pixel_format_t* format, Display** outDisplay);
100 void destroyDisplay(hwc2_display_t displayId);
Dan Stoza651bf312015-10-23 17:03:17 -0700101
Steven Thomasd7f49c52017-07-26 18:48:28 -0700102 void onHotplug(hwc2_display_t displayId, Connection connection);
Dan Stoza651bf312015-10-23 17:03:17 -0700103
104 // Other Device methods
105
Steven Thomasd7f49c52017-07-26 18:48:28 -0700106 Display* getDisplayById(hwc2_display_t id);
Dan Stoza09e7a272016-04-14 12:31:01 -0700107
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800108 android::Hwc2::Composer* getComposer() { return mComposer.get(); }
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800109
Dan Stoza651bf312015-10-23 17:03:17 -0700110private:
111 // Initialization methods
112
Dan Stoza651bf312015-10-23 17:03:17 -0700113 void loadCapabilities();
Dan Stoza651bf312015-10-23 17:03:17 -0700114
115 // Member variables
Chia-I Wuaab99f52016-10-05 12:59:58 +0800116 std::unique_ptr<android::Hwc2::Composer> mComposer;
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700117 std::unordered_set<Capability> mCapabilities;
Steven Thomasd7f49c52017-07-26 18:48:28 -0700118 std::unordered_map<hwc2_display_t, std::unique_ptr<Display>> mDisplays;
119 bool mRegisteredCallback;
Dan Stoza651bf312015-10-23 17:03:17 -0700120};
121
Fabien Sanglard33960702016-11-29 17:58:21 -0800122// Convenience C++ class to access hwc2_device_t Display functions directly.
Steven Thomasd7f49c52017-07-26 18:48:28 -0700123class Display
Dan Stoza651bf312015-10-23 17:03:17 -0700124{
125public:
Steven Thomasd7f49c52017-07-26 18:48:28 -0700126 Display(android::Hwc2::Composer& composer,
127 const std::unordered_set<Capability>& capabilities,
128 hwc2_display_t id, DisplayType type);
Dan Stoza651bf312015-10-23 17:03:17 -0700129 ~Display();
130
Dan Stoza651bf312015-10-23 17:03:17 -0700131 class Config
132 {
133 public:
134 class Builder
135 {
136 public:
137 Builder(Display& display, hwc2_config_t id);
138
139 std::shared_ptr<const Config> build() {
140 return std::const_pointer_cast<const Config>(
141 std::move(mConfig));
142 }
143
144 Builder& setWidth(int32_t width) {
145 mConfig->mWidth = width;
146 return *this;
147 }
148 Builder& setHeight(int32_t height) {
149 mConfig->mHeight = height;
150 return *this;
151 }
152 Builder& setVsyncPeriod(int32_t vsyncPeriod) {
153 mConfig->mVsyncPeriod = vsyncPeriod;
154 return *this;
155 }
156 Builder& setDpiX(int32_t dpiX) {
157 if (dpiX == -1) {
158 mConfig->mDpiX = getDefaultDensity();
159 } else {
160 mConfig->mDpiX = dpiX / 1000.0f;
161 }
162 return *this;
163 }
164 Builder& setDpiY(int32_t dpiY) {
165 if (dpiY == -1) {
166 mConfig->mDpiY = getDefaultDensity();
167 } else {
168 mConfig->mDpiY = dpiY / 1000.0f;
169 }
170 return *this;
171 }
172
173 private:
174 float getDefaultDensity();
175 std::shared_ptr<Config> mConfig;
176 };
177
178 hwc2_display_t getDisplayId() const { return mDisplay.getId(); }
179 hwc2_config_t getId() const { return mId; }
180
181 int32_t getWidth() const { return mWidth; }
182 int32_t getHeight() const { return mHeight; }
183 nsecs_t getVsyncPeriod() const { return mVsyncPeriod; }
184 float getDpiX() const { return mDpiX; }
185 float getDpiY() const { return mDpiY; }
186
187 private:
188 Config(Display& display, hwc2_config_t id);
189
190 Display& mDisplay;
191 hwc2_config_t mId;
192
193 int32_t mWidth;
194 int32_t mHeight;
195 nsecs_t mVsyncPeriod;
196 float mDpiX;
197 float mDpiY;
198 };
199
200 // Required by HWC2
201
202 [[clang::warn_unused_result]] Error acceptChanges();
Steven Thomasd7f49c52017-07-26 18:48:28 -0700203 [[clang::warn_unused_result]] Error createLayer(Layer** outLayer);
204 [[clang::warn_unused_result]] Error destroyLayer(Layer* layer);
Dan Stoza651bf312015-10-23 17:03:17 -0700205 [[clang::warn_unused_result]] Error getActiveConfig(
206 std::shared_ptr<const Config>* outConfig) const;
207 [[clang::warn_unused_result]] Error getChangedCompositionTypes(
Steven Thomasd7f49c52017-07-26 18:48:28 -0700208 std::unordered_map<Layer*, Composition>* outTypes);
Dan Stoza076ac672016-03-14 10:47:53 -0700209 [[clang::warn_unused_result]] Error getColorModes(
Michael Wright28f24d02016-07-12 13:30:53 -0700210 std::vector<android_color_mode_t>* outModes) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700211
212 // Doesn't call into the HWC2 device, so no errors are possible
213 std::vector<std::shared_ptr<const Config>> getConfigs() const;
214
215 [[clang::warn_unused_result]] Error getName(std::string* outName) const;
216 [[clang::warn_unused_result]] Error getRequests(
217 DisplayRequest* outDisplayRequests,
Steven Thomasd7f49c52017-07-26 18:48:28 -0700218 std::unordered_map<Layer*, LayerRequest>* outLayerRequests);
Dan Stoza651bf312015-10-23 17:03:17 -0700219 [[clang::warn_unused_result]] Error getType(DisplayType* outType) const;
220 [[clang::warn_unused_result]] Error supportsDoze(bool* outSupport) const;
Dan Stoza7d7ae732016-03-16 12:23:40 -0700221 [[clang::warn_unused_result]] Error getHdrCapabilities(
222 std::unique_ptr<android::HdrCapabilities>* outCapabilities) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700223 [[clang::warn_unused_result]] Error getReleaseFences(
Steven Thomasd7f49c52017-07-26 18:48:28 -0700224 std::unordered_map<Layer*,
Dan Stoza651bf312015-10-23 17:03:17 -0700225 android::sp<android::Fence>>* outFences) const;
226 [[clang::warn_unused_result]] Error present(
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800227 android::sp<android::Fence>* outPresentFence);
Dan Stoza651bf312015-10-23 17:03:17 -0700228 [[clang::warn_unused_result]] Error setActiveConfig(
229 const std::shared_ptr<const Config>& config);
230 [[clang::warn_unused_result]] Error setClientTarget(
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400231 uint32_t slot, const android::sp<android::GraphicBuffer>& target,
Dan Stoza651bf312015-10-23 17:03:17 -0700232 const android::sp<android::Fence>& acquireFence,
233 android_dataspace_t dataspace);
Michael Wright28f24d02016-07-12 13:30:53 -0700234 [[clang::warn_unused_result]] Error setColorMode(android_color_mode_t mode);
Dan Stoza5df2a862016-03-24 16:19:37 -0700235 [[clang::warn_unused_result]] Error setColorTransform(
236 const android::mat4& matrix, android_color_transform_t hint);
Dan Stoza651bf312015-10-23 17:03:17 -0700237 [[clang::warn_unused_result]] Error setOutputBuffer(
238 const android::sp<android::GraphicBuffer>& buffer,
239 const android::sp<android::Fence>& releaseFence);
240 [[clang::warn_unused_result]] Error setPowerMode(PowerMode mode);
241 [[clang::warn_unused_result]] Error setVsyncEnabled(Vsync enabled);
242 [[clang::warn_unused_result]] Error validate(uint32_t* outNumTypes,
243 uint32_t* outNumRequests);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700244 [[clang::warn_unused_result]] Error presentOrValidate(uint32_t* outNumTypes,
245 uint32_t* outNumRequests,
246 android::sp<android::Fence>* outPresentFence, uint32_t* state);
Dan Stoza651bf312015-10-23 17:03:17 -0700247
Chia-I Wu0c6ce462017-06-22 10:48:28 -0700248 // Most methods in this class write a command to a command buffer. The
249 // command buffer is implicitly submitted in validate, present, and
250 // presentOrValidate. This method provides a way to discard the commands,
251 // which can be used to discard stale commands.
252 void discardCommands();
253
Dan Stoza651bf312015-10-23 17:03:17 -0700254 // Other Display methods
255
Dan Stoza651bf312015-10-23 17:03:17 -0700256 hwc2_display_t getId() const { return mId; }
257 bool isConnected() const { return mIsConnected; }
Steven Thomasd7f49c52017-07-26 18:48:28 -0700258 void setConnected(bool connected); // For use by Device only
Dan Stoza651bf312015-10-23 17:03:17 -0700259
260private:
Dan Stoza651bf312015-10-23 17:03:17 -0700261 int32_t getAttribute(hwc2_config_t configId, Attribute attribute);
262 void loadConfig(hwc2_config_t configId);
263 void loadConfigs();
264
Dan Stoza651bf312015-10-23 17:03:17 -0700265 // This may fail (and return a null pointer) if no layer with this ID exists
266 // on this display
Steven Thomasd7f49c52017-07-26 18:48:28 -0700267 Layer* getLayerById(hwc2_layer_t id) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700268
269 // Member variables
270
Steven Thomasd7f49c52017-07-26 18:48:28 -0700271 // These are references to data owned by HWC2::Device, which will outlive
272 // this HWC2::Display, so these references are guaranteed to be valid for
273 // the lifetime of this object.
274 android::Hwc2::Composer& mComposer;
275 const std::unordered_set<Capability>& mCapabilities;
276
Dan Stoza651bf312015-10-23 17:03:17 -0700277 hwc2_display_t mId;
278 bool mIsConnected;
Chris Forbes016d73c2017-04-11 10:04:31 -0700279 DisplayType mType;
Steven Thomasd7f49c52017-07-26 18:48:28 -0700280 std::unordered_map<hwc2_layer_t, std::unique_ptr<Layer>> mLayers;
Alistair Strachanc1752532017-06-07 16:34:44 -0700281 // The ordering in this map matters, for getConfigs(), when it is
282 // converted to a vector
283 std::map<hwc2_config_t, std::shared_ptr<const Config>> mConfigs;
Dan Stoza651bf312015-10-23 17:03:17 -0700284};
285
Fabien Sanglard33960702016-11-29 17:58:21 -0800286// Convenience C++ class to access hwc2_device_t Layer functions directly.
Dan Stoza651bf312015-10-23 17:03:17 -0700287class Layer
288{
289public:
Steven Thomasd7f49c52017-07-26 18:48:28 -0700290 Layer(android::Hwc2::Composer& composer,
291 const std::unordered_set<Capability>& capabilities,
292 hwc2_display_t displayId, hwc2_layer_t layerId);
Dan Stoza651bf312015-10-23 17:03:17 -0700293 ~Layer();
294
Dan Stoza651bf312015-10-23 17:03:17 -0700295 hwc2_layer_t getId() const { return mId; }
296
Steven Thomasd7f49c52017-07-26 18:48:28 -0700297 // Register a listener to be notified when the layer is destroyed. When the
298 // listener function is called, the Layer will be in the process of being
299 // destroyed, so it's not safe to call methods on it.
300 void setLayerDestroyedListener(std::function<void(Layer*)> listener);
301
Dan Stoza651bf312015-10-23 17:03:17 -0700302 [[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:
Steven Thomasd7f49c52017-07-26 18:48:28 -0700328 // These are references to data owned by HWC2::Device, which will outlive
329 // this HWC2::Layer, so these references are guaranteed to be valid for
330 // the lifetime of this object.
331 android::Hwc2::Composer& mComposer;
332 const std::unordered_set<Capability>& mCapabilities;
333
Dan Stoza651bf312015-10-23 17:03:17 -0700334 hwc2_display_t mDisplayId;
Dan Stoza651bf312015-10-23 17:03:17 -0700335 hwc2_layer_t mId;
Courtney Goeltzenleuchterc988ee42017-05-31 17:56:46 -0600336 android_dataspace mDataSpace = HAL_DATASPACE_UNKNOWN;
Steven Thomasd7f49c52017-07-26 18:48:28 -0700337 std::function<void(Layer*)> mLayerDestroyedListener;
Dan Stoza651bf312015-10-23 17:03:17 -0700338};
339
340} // namespace HWC2
341
342#endif // ANDROID_SF_HWC2_H