blob: 577ebc67a564b1ef64277a18684428f7a456764a [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
Courtney Goeltzenleuchterf9c98e52018-02-12 07:23:17 -070026#include <gui/HdrMetadata.h>
Mathias Agopian1d77b712017-02-17 15:46:13 -080027#include <math/mat4.h>
Peiyong Linfd997e02018-03-28 15:29:00 -070028#include <ui/GraphicTypes.h>
Courtney Goeltzenleuchterf9c98e52018-02-12 07:23:17 -070029#include <ui/HdrCapabilities.h>
Dan Stoza651bf312015-10-23 17:03:17 -070030#include <utils/Log.h>
31#include <utils/StrongPointer.h>
32#include <utils/Timers.h>
33
34#include <functional>
35#include <string>
36#include <unordered_map>
Dan Stoza9f26a9c2016-06-22 14:51:09 -070037#include <unordered_set>
Dan Stoza651bf312015-10-23 17:03:17 -070038#include <vector>
Alistair Strachanc1752532017-06-07 16:34:44 -070039#include <map>
Dan Stoza651bf312015-10-23 17:03:17 -070040
41namespace android {
42 class Fence;
Dan Stoza5a423ea2017-02-16 14:10:39 -080043 class FloatRect;
Dan Stoza651bf312015-10-23 17:03:17 -070044 class GraphicBuffer;
45 class Rect;
46 class Region;
Chia-I Wuaab99f52016-10-05 12:59:58 +080047 namespace Hwc2 {
48 class Composer;
Dan Stoza71bded52016-10-19 11:10:33 -070049 }
Lloyd Piquebc792092018-01-17 11:52:30 -080050
51 class TestableSurfaceFlinger;
Dan Stoza651bf312015-10-23 17:03:17 -070052}
53
54namespace HWC2 {
55
56class Display;
57class Layer;
58
Steven Thomas94e35b92017-07-26 18:48:28 -070059// Implement this interface to receive hardware composer events.
60//
61// These callback functions will generally be called on a hwbinder thread, but
62// when first registering the callback the onHotplugReceived() function will
63// immediately be called on the thread calling registerCallback().
64//
65// All calls receive a sequenceId, which will be the value that was supplied to
66// HWC2::Device::registerCallback(). It's used to help differentiate callbacks
67// from different hardware composer instances.
68class ComposerCallback {
69 public:
70 virtual void onHotplugReceived(int32_t sequenceId, hwc2_display_t display,
Lloyd Pique715a2c12017-12-14 17:18:08 -080071 Connection connection) = 0;
Steven Thomas94e35b92017-07-26 18:48:28 -070072 virtual void onRefreshReceived(int32_t sequenceId,
73 hwc2_display_t display) = 0;
74 virtual void onVsyncReceived(int32_t sequenceId, hwc2_display_t display,
75 int64_t timestamp) = 0;
76 virtual ~ComposerCallback() = default;
77};
Dan Stoza651bf312015-10-23 17:03:17 -070078
Fabien Sanglard33960702016-11-29 17:58:21 -080079// C++ Wrapper around hwc2_device_t. Load all functions pointers
80// and handle callback registration.
Dan Stoza651bf312015-10-23 17:03:17 -070081class Device
82{
83public:
Lloyd Piquea822d522017-12-20 16:42:57 -080084 explicit Device(std::unique_ptr<android::Hwc2::Composer> composer);
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,
Peiyong Lin34beb7a2018-03-28 11:57:12 -070098 android::ui::PixelFormat* format, Display** outDisplay);
Steven Thomas94e35b92017-07-26 18:48:28 -070099 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
Chia-I Wuae5a6b82017-10-10 09:09:22 -0700109 // We buffer most state changes and flush them implicitly with
110 // Display::validate, Display::present, and Display::presentOrValidate.
111 // This method provides an explicit way to flush state changes to HWC.
112 Error flushCommands();
113
Dan Stoza651bf312015-10-23 17:03:17 -0700114private:
115 // Initialization methods
116
Dan Stoza651bf312015-10-23 17:03:17 -0700117 void loadCapabilities();
Dan Stoza651bf312015-10-23 17:03:17 -0700118
119 // Member variables
Chia-I Wuaab99f52016-10-05 12:59:58 +0800120 std::unique_ptr<android::Hwc2::Composer> mComposer;
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700121 std::unordered_set<Capability> mCapabilities;
Steven Thomas94e35b92017-07-26 18:48:28 -0700122 std::unordered_map<hwc2_display_t, std::unique_ptr<Display>> mDisplays;
Lloyd Piquea822d522017-12-20 16:42:57 -0800123 bool mRegisteredCallback = false;
Dan Stoza651bf312015-10-23 17:03:17 -0700124};
125
Fabien Sanglard33960702016-11-29 17:58:21 -0800126// Convenience C++ class to access hwc2_device_t Display functions directly.
Steven Thomas94e35b92017-07-26 18:48:28 -0700127class Display
Dan Stoza651bf312015-10-23 17:03:17 -0700128{
129public:
Lloyd Piquebc792092018-01-17 11:52:30 -0800130 Display(android::Hwc2::Composer& composer, const std::unordered_set<Capability>& capabilities,
Steven Thomas94e35b92017-07-26 18:48:28 -0700131 hwc2_display_t id, DisplayType type);
Dan Stoza651bf312015-10-23 17:03:17 -0700132 ~Display();
133
Dan Stoza651bf312015-10-23 17:03:17 -0700134 class Config
135 {
136 public:
137 class Builder
138 {
139 public:
140 Builder(Display& display, hwc2_config_t id);
141
142 std::shared_ptr<const Config> build() {
143 return std::const_pointer_cast<const Config>(
144 std::move(mConfig));
145 }
146
147 Builder& setWidth(int32_t width) {
148 mConfig->mWidth = width;
149 return *this;
150 }
151 Builder& setHeight(int32_t height) {
152 mConfig->mHeight = height;
153 return *this;
154 }
155 Builder& setVsyncPeriod(int32_t vsyncPeriod) {
156 mConfig->mVsyncPeriod = vsyncPeriod;
157 return *this;
158 }
159 Builder& setDpiX(int32_t dpiX) {
160 if (dpiX == -1) {
161 mConfig->mDpiX = getDefaultDensity();
162 } else {
163 mConfig->mDpiX = dpiX / 1000.0f;
164 }
165 return *this;
166 }
167 Builder& setDpiY(int32_t dpiY) {
168 if (dpiY == -1) {
169 mConfig->mDpiY = getDefaultDensity();
170 } else {
171 mConfig->mDpiY = dpiY / 1000.0f;
172 }
173 return *this;
174 }
175
176 private:
177 float getDefaultDensity();
178 std::shared_ptr<Config> mConfig;
179 };
180
181 hwc2_display_t getDisplayId() const { return mDisplay.getId(); }
182 hwc2_config_t getId() const { return mId; }
183
184 int32_t getWidth() const { return mWidth; }
185 int32_t getHeight() const { return mHeight; }
186 nsecs_t getVsyncPeriod() const { return mVsyncPeriod; }
187 float getDpiX() const { return mDpiX; }
188 float getDpiY() const { return mDpiY; }
189
190 private:
191 Config(Display& display, hwc2_config_t id);
192
193 Display& mDisplay;
194 hwc2_config_t mId;
195
196 int32_t mWidth;
197 int32_t mHeight;
198 nsecs_t mVsyncPeriod;
199 float mDpiX;
200 float mDpiY;
201 };
202
203 // Required by HWC2
204
205 [[clang::warn_unused_result]] Error acceptChanges();
Steven Thomas94e35b92017-07-26 18:48:28 -0700206 [[clang::warn_unused_result]] Error createLayer(Layer** outLayer);
207 [[clang::warn_unused_result]] Error destroyLayer(Layer* layer);
Dan Stoza651bf312015-10-23 17:03:17 -0700208 [[clang::warn_unused_result]] Error getActiveConfig(
209 std::shared_ptr<const Config>* outConfig) const;
210 [[clang::warn_unused_result]] Error getChangedCompositionTypes(
Steven Thomas94e35b92017-07-26 18:48:28 -0700211 std::unordered_map<Layer*, Composition>* outTypes);
Dan Stoza076ac672016-03-14 10:47:53 -0700212 [[clang::warn_unused_result]] Error getColorModes(
Peiyong Linfd997e02018-03-28 15:29:00 -0700213 std::vector<android::ui::ColorMode>* outModes) const;
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700214 // outSupportedPerFrameMetadata is an opaque bitmask to the callers
215 // but contains HdrMetadata::Type::*.
216 [[clang::warn_unused_result]] Error getSupportedPerFrameMetadata(
217 int32_t* outSupportedPerFrameMetadata) const;
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700218 [[clang::warn_unused_result]] Error getRenderIntents(
219 android::ui::ColorMode colorMode,
220 std::vector<android::ui::RenderIntent>* outRenderIntents) const;
221 [[clang::warn_unused_result]] Error getDataspaceSaturationMatrix(
222 android::ui::Dataspace dataspace, android::mat4* outMatrix);
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
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700227 [[clang::warn_unused_result]] Error getIdentificationData(uint8_t* outPort,
228 std::vector<uint8_t>* outData) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700229 [[clang::warn_unused_result]] Error getName(std::string* outName) const;
230 [[clang::warn_unused_result]] Error getRequests(
231 DisplayRequest* outDisplayRequests,
Steven Thomas94e35b92017-07-26 18:48:28 -0700232 std::unordered_map<Layer*, LayerRequest>* outLayerRequests);
Dan Stoza651bf312015-10-23 17:03:17 -0700233 [[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(
Peiyong Lin62665892018-04-16 11:07:44 -0700236 android::HdrCapabilities* outCapabilities) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700237 [[clang::warn_unused_result]] Error getReleaseFences(
Steven Thomas94e35b92017-07-26 18:48:28 -0700238 std::unordered_map<Layer*,
Dan Stoza651bf312015-10-23 17:03:17 -0700239 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,
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700247 android::ui::Dataspace dataspace);
Peiyong Linfd997e02018-03-28 15:29:00 -0700248 [[clang::warn_unused_result]] Error setColorMode(
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700249 android::ui::ColorMode mode,
250 android::ui::RenderIntent renderIntent);
Dan Stoza5df2a862016-03-24 16:19:37 -0700251 [[clang::warn_unused_result]] Error setColorTransform(
252 const android::mat4& matrix, android_color_transform_t hint);
Dan Stoza651bf312015-10-23 17:03:17 -0700253 [[clang::warn_unused_result]] Error setOutputBuffer(
254 const android::sp<android::GraphicBuffer>& buffer,
255 const android::sp<android::Fence>& releaseFence);
256 [[clang::warn_unused_result]] Error setPowerMode(PowerMode mode);
257 [[clang::warn_unused_result]] Error setVsyncEnabled(Vsync enabled);
258 [[clang::warn_unused_result]] Error validate(uint32_t* outNumTypes,
259 uint32_t* outNumRequests);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700260 [[clang::warn_unused_result]] Error presentOrValidate(uint32_t* outNumTypes,
Peiyong Linfd997e02018-03-28 15:29:00 -0700261 uint32_t* outNumRequests,
262 android::sp<android::Fence>* outPresentFence, uint32_t* state);
Dan Stoza651bf312015-10-23 17:03:17 -0700263
264 // Other Display methods
265
Dan Stoza651bf312015-10-23 17:03:17 -0700266 hwc2_display_t getId() const { return mId; }
267 bool isConnected() const { return mIsConnected; }
Steven Thomas94e35b92017-07-26 18:48:28 -0700268 void setConnected(bool connected); // For use by Device only
Dan Stoza651bf312015-10-23 17:03:17 -0700269
270private:
Dan Stoza651bf312015-10-23 17:03:17 -0700271 int32_t getAttribute(hwc2_config_t configId, Attribute attribute);
272 void loadConfig(hwc2_config_t configId);
273 void loadConfigs();
274
Dan Stoza651bf312015-10-23 17:03:17 -0700275 // This may fail (and return a null pointer) if no layer with this ID exists
276 // on this display
Steven Thomas94e35b92017-07-26 18:48:28 -0700277 Layer* getLayerById(hwc2_layer_t id) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700278
Lloyd Piquebc792092018-01-17 11:52:30 -0800279 friend android::TestableSurfaceFlinger;
280
Dan Stoza651bf312015-10-23 17:03:17 -0700281 // Member variables
282
Steven Thomas94e35b92017-07-26 18:48:28 -0700283 // These are references to data owned by HWC2::Device, which will outlive
284 // this HWC2::Display, so these references are guaranteed to be valid for
285 // the lifetime of this object.
286 android::Hwc2::Composer& mComposer;
287 const std::unordered_set<Capability>& mCapabilities;
288
Dan Stoza651bf312015-10-23 17:03:17 -0700289 hwc2_display_t mId;
290 bool mIsConnected;
Chris Forbes016d73c2017-04-11 10:04:31 -0700291 DisplayType mType;
Steven Thomas94e35b92017-07-26 18:48:28 -0700292 std::unordered_map<hwc2_layer_t, std::unique_ptr<Layer>> mLayers;
Alistair Strachanc1752532017-06-07 16:34:44 -0700293 // The ordering in this map matters, for getConfigs(), when it is
294 // converted to a vector
295 std::map<hwc2_config_t, std::shared_ptr<const Config>> mConfigs;
Dan Stoza651bf312015-10-23 17:03:17 -0700296};
297
Fabien Sanglard33960702016-11-29 17:58:21 -0800298// Convenience C++ class to access hwc2_device_t Layer functions directly.
Dan Stoza651bf312015-10-23 17:03:17 -0700299class Layer
300{
301public:
Steven Thomas94e35b92017-07-26 18:48:28 -0700302 Layer(android::Hwc2::Composer& composer,
303 const std::unordered_set<Capability>& capabilities,
304 hwc2_display_t displayId, hwc2_layer_t layerId);
Dan Stoza651bf312015-10-23 17:03:17 -0700305 ~Layer();
306
Dan Stoza651bf312015-10-23 17:03:17 -0700307 hwc2_layer_t getId() const { return mId; }
308
Steven Thomas94e35b92017-07-26 18:48:28 -0700309 // Register a listener to be notified when the layer is destroyed. When the
310 // listener function is called, the Layer will be in the process of being
311 // destroyed, so it's not safe to call methods on it.
312 void setLayerDestroyedListener(std::function<void(Layer*)> listener);
313
Dan Stoza651bf312015-10-23 17:03:17 -0700314 [[clang::warn_unused_result]] Error setCursorPosition(int32_t x, int32_t y);
Chia-I Wu06d63de2017-01-04 14:58:51 +0800315 [[clang::warn_unused_result]] Error setBuffer(uint32_t slot,
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400316 const android::sp<android::GraphicBuffer>& buffer,
Dan Stoza651bf312015-10-23 17:03:17 -0700317 const android::sp<android::Fence>& acquireFence);
318 [[clang::warn_unused_result]] Error setSurfaceDamage(
319 const android::Region& damage);
320
321 [[clang::warn_unused_result]] Error setBlendMode(BlendMode mode);
322 [[clang::warn_unused_result]] Error setColor(hwc_color_t color);
323 [[clang::warn_unused_result]] Error setCompositionType(Composition type);
Dan Stoza5df2a862016-03-24 16:19:37 -0700324 [[clang::warn_unused_result]] Error setDataspace(
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700325 android::ui::Dataspace dataspace);
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700326 [[clang::warn_unused_result]] Error setPerFrameMetadata(
327 const int32_t supportedPerFrameMetadata,
328 const android::HdrMetadata& metadata);
Dan Stoza651bf312015-10-23 17:03:17 -0700329 [[clang::warn_unused_result]] Error setDisplayFrame(
330 const android::Rect& frame);
331 [[clang::warn_unused_result]] Error setPlaneAlpha(float alpha);
332 [[clang::warn_unused_result]] Error setSidebandStream(
333 const native_handle_t* stream);
334 [[clang::warn_unused_result]] Error setSourceCrop(
Dan Stoza5a423ea2017-02-16 14:10:39 -0800335 const android::FloatRect& crop);
Dan Stoza651bf312015-10-23 17:03:17 -0700336 [[clang::warn_unused_result]] Error setTransform(Transform transform);
337 [[clang::warn_unused_result]] Error setVisibleRegion(
338 const android::Region& region);
339 [[clang::warn_unused_result]] Error setZOrder(uint32_t z);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500340 [[clang::warn_unused_result]] Error setInfo(uint32_t type, uint32_t appId);
Dan Stoza651bf312015-10-23 17:03:17 -0700341
342private:
Steven Thomas94e35b92017-07-26 18:48:28 -0700343 // These are references to data owned by HWC2::Device, which will outlive
344 // this HWC2::Layer, so these references are guaranteed to be valid for
345 // the lifetime of this object.
346 android::Hwc2::Composer& mComposer;
347 const std::unordered_set<Capability>& mCapabilities;
348
Dan Stoza651bf312015-10-23 17:03:17 -0700349 hwc2_display_t mDisplayId;
Dan Stoza651bf312015-10-23 17:03:17 -0700350 hwc2_layer_t mId;
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700351 android::ui::Dataspace mDataSpace = android::ui::Dataspace::UNKNOWN;
Courtney Goeltzenleuchterf9c98e52018-02-12 07:23:17 -0700352 android::HdrMetadata mHdrMetadata;
Steven Thomas94e35b92017-07-26 18:48:28 -0700353 std::function<void(Layer*)> mLayerDestroyedListener;
Dan Stoza651bf312015-10-23 17:03:17 -0700354};
355
356} // namespace HWC2
357
358#endif // ANDROID_SF_HWC2_H