blob: eefab981557b107eac5a2635095ffa8734d917cd [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>
39
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 }
Lloyd Piquebc792092018-01-17 11:52:30 -080049
50 class TestableSurfaceFlinger;
Dan Stoza651bf312015-10-23 17:03:17 -070051}
52
53namespace HWC2 {
54
55class Display;
56class Layer;
57
Steven Thomas94e35b92017-07-26 18:48:28 -070058// Implement this interface to receive hardware composer events.
59//
60// These callback functions will generally be called on a hwbinder thread, but
61// when first registering the callback the onHotplugReceived() function will
62// immediately be called on the thread calling registerCallback().
63//
64// All calls receive a sequenceId, which will be the value that was supplied to
65// HWC2::Device::registerCallback(). It's used to help differentiate callbacks
66// from different hardware composer instances.
67class ComposerCallback {
68 public:
69 virtual void onHotplugReceived(int32_t sequenceId, hwc2_display_t display,
Lloyd Pique715a2c12017-12-14 17:18:08 -080070 Connection connection) = 0;
Steven Thomas94e35b92017-07-26 18:48:28 -070071 virtual void onRefreshReceived(int32_t sequenceId,
72 hwc2_display_t display) = 0;
73 virtual void onVsyncReceived(int32_t sequenceId, hwc2_display_t display,
74 int64_t timestamp) = 0;
75 virtual ~ComposerCallback() = default;
76};
Dan Stoza651bf312015-10-23 17:03:17 -070077
Fabien Sanglard33960702016-11-29 17:58:21 -080078// C++ Wrapper around hwc2_device_t. Load all functions pointers
79// and handle callback registration.
Dan Stoza651bf312015-10-23 17:03:17 -070080class Device
81{
82public:
Lloyd Piquea822d522017-12-20 16:42:57 -080083 explicit Device(std::unique_ptr<android::Hwc2::Composer> composer);
Dan Stoza651bf312015-10-23 17:03:17 -070084
Steven Thomas94e35b92017-07-26 18:48:28 -070085 void registerCallback(ComposerCallback* callback, int32_t sequenceId);
Dan Stoza651bf312015-10-23 17:03:17 -070086
87 // Required by HWC2
88
89 std::string dump() const;
90
Dan Stoza9f26a9c2016-06-22 14:51:09 -070091 const std::unordered_set<Capability>& getCapabilities() const {
Dan Stoza651bf312015-10-23 17:03:17 -070092 return mCapabilities;
93 };
94
95 uint32_t getMaxVirtualDisplayCount() const;
Dominik Laskowski0954b1d2018-06-13 14:58:49 -070096 Error getDisplayIdentificationData(hwc2_display_t hwcDisplayId, uint8_t* outPort,
97 std::vector<uint8_t>* outData) const;
98
Dan Stoza651bf312015-10-23 17:03:17 -070099 Error createVirtualDisplay(uint32_t width, uint32_t height,
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700100 android::ui::PixelFormat* format, Display** outDisplay);
Steven Thomas94e35b92017-07-26 18:48:28 -0700101 void destroyDisplay(hwc2_display_t displayId);
Dan Stoza651bf312015-10-23 17:03:17 -0700102
Steven Thomas94e35b92017-07-26 18:48:28 -0700103 void onHotplug(hwc2_display_t displayId, Connection connection);
Dan Stoza651bf312015-10-23 17:03:17 -0700104
105 // Other Device methods
106
Steven Thomas94e35b92017-07-26 18:48:28 -0700107 Display* getDisplayById(hwc2_display_t id);
Dan Stoza09e7a272016-04-14 12:31:01 -0700108
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800109 android::Hwc2::Composer* getComposer() { return mComposer.get(); }
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800110
Chia-I Wuae5a6b82017-10-10 09:09:22 -0700111 // We buffer most state changes and flush them implicitly with
112 // Display::validate, Display::present, and Display::presentOrValidate.
113 // This method provides an explicit way to flush state changes to HWC.
114 Error flushCommands();
115
Dan Stoza651bf312015-10-23 17:03:17 -0700116private:
117 // Initialization methods
118
Dan Stoza651bf312015-10-23 17:03:17 -0700119 void loadCapabilities();
Dan Stoza651bf312015-10-23 17:03:17 -0700120
121 // Member variables
Chia-I Wuaab99f52016-10-05 12:59:58 +0800122 std::unique_ptr<android::Hwc2::Composer> mComposer;
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700123 std::unordered_set<Capability> mCapabilities;
Steven Thomas94e35b92017-07-26 18:48:28 -0700124 std::unordered_map<hwc2_display_t, std::unique_ptr<Display>> mDisplays;
Lloyd Piquea822d522017-12-20 16:42:57 -0800125 bool mRegisteredCallback = false;
Dan Stoza651bf312015-10-23 17:03:17 -0700126};
127
Fabien Sanglard33960702016-11-29 17:58:21 -0800128// Convenience C++ class to access hwc2_device_t Display functions directly.
Steven Thomas94e35b92017-07-26 18:48:28 -0700129class Display
Dan Stoza651bf312015-10-23 17:03:17 -0700130{
131public:
Lloyd Piquebc792092018-01-17 11:52:30 -0800132 Display(android::Hwc2::Composer& composer, const std::unordered_set<Capability>& capabilities,
Steven Thomas94e35b92017-07-26 18:48:28 -0700133 hwc2_display_t id, DisplayType type);
Dan Stoza651bf312015-10-23 17:03:17 -0700134 ~Display();
135
Dan Stoza651bf312015-10-23 17:03:17 -0700136 class Config
137 {
138 public:
139 class Builder
140 {
141 public:
142 Builder(Display& display, hwc2_config_t id);
143
144 std::shared_ptr<const Config> build() {
145 return std::const_pointer_cast<const Config>(
146 std::move(mConfig));
147 }
148
149 Builder& setWidth(int32_t width) {
150 mConfig->mWidth = width;
151 return *this;
152 }
153 Builder& setHeight(int32_t height) {
154 mConfig->mHeight = height;
155 return *this;
156 }
157 Builder& setVsyncPeriod(int32_t vsyncPeriod) {
158 mConfig->mVsyncPeriod = vsyncPeriod;
159 return *this;
160 }
161 Builder& setDpiX(int32_t dpiX) {
162 if (dpiX == -1) {
163 mConfig->mDpiX = getDefaultDensity();
164 } else {
165 mConfig->mDpiX = dpiX / 1000.0f;
166 }
167 return *this;
168 }
169 Builder& setDpiY(int32_t dpiY) {
170 if (dpiY == -1) {
171 mConfig->mDpiY = getDefaultDensity();
172 } else {
173 mConfig->mDpiY = dpiY / 1000.0f;
174 }
175 return *this;
176 }
177
178 private:
179 float getDefaultDensity();
180 std::shared_ptr<Config> mConfig;
181 };
182
183 hwc2_display_t getDisplayId() const { return mDisplay.getId(); }
184 hwc2_config_t getId() const { return mId; }
185
186 int32_t getWidth() const { return mWidth; }
187 int32_t getHeight() const { return mHeight; }
188 nsecs_t getVsyncPeriod() const { return mVsyncPeriod; }
189 float getDpiX() const { return mDpiX; }
190 float getDpiY() const { return mDpiY; }
191
192 private:
193 Config(Display& display, hwc2_config_t id);
194
195 Display& mDisplay;
196 hwc2_config_t mId;
197
198 int32_t mWidth;
199 int32_t mHeight;
200 nsecs_t mVsyncPeriod;
201 float mDpiX;
202 float mDpiY;
203 };
204
205 // Required by HWC2
206
207 [[clang::warn_unused_result]] Error acceptChanges();
Steven Thomas94e35b92017-07-26 18:48:28 -0700208 [[clang::warn_unused_result]] Error createLayer(Layer** outLayer);
209 [[clang::warn_unused_result]] Error destroyLayer(Layer* layer);
Dan Stoza651bf312015-10-23 17:03:17 -0700210 [[clang::warn_unused_result]] Error getActiveConfig(
211 std::shared_ptr<const Config>* outConfig) const;
Lloyd Pique3c085a02018-05-09 19:38:32 -0700212 [[clang::warn_unused_result]] Error getActiveConfigIndex(int* outIndex) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700213 [[clang::warn_unused_result]] Error getChangedCompositionTypes(
Steven Thomas94e35b92017-07-26 18:48:28 -0700214 std::unordered_map<Layer*, Composition>* outTypes);
Dan Stoza076ac672016-03-14 10:47:53 -0700215 [[clang::warn_unused_result]] Error getColorModes(
Peiyong Linfd997e02018-03-28 15:29:00 -0700216 std::vector<android::ui::ColorMode>* outModes) const;
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700217 // outSupportedPerFrameMetadata is an opaque bitmask to the callers
218 // but contains HdrMetadata::Type::*.
219 [[clang::warn_unused_result]] Error getSupportedPerFrameMetadata(
220 int32_t* outSupportedPerFrameMetadata) const;
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700221 [[clang::warn_unused_result]] Error getRenderIntents(
222 android::ui::ColorMode colorMode,
223 std::vector<android::ui::RenderIntent>* outRenderIntents) const;
224 [[clang::warn_unused_result]] Error getDataspaceSaturationMatrix(
225 android::ui::Dataspace dataspace, android::mat4* outMatrix);
Dan Stoza651bf312015-10-23 17:03:17 -0700226
227 // Doesn't call into the HWC2 device, so no errors are possible
228 std::vector<std::shared_ptr<const Config>> getConfigs() const;
229
230 [[clang::warn_unused_result]] Error getName(std::string* outName) const;
231 [[clang::warn_unused_result]] Error getRequests(
232 DisplayRequest* outDisplayRequests,
Steven Thomas94e35b92017-07-26 18:48:28 -0700233 std::unordered_map<Layer*, LayerRequest>* outLayerRequests);
Dan Stoza651bf312015-10-23 17:03:17 -0700234 [[clang::warn_unused_result]] Error getType(DisplayType* outType) const;
235 [[clang::warn_unused_result]] Error supportsDoze(bool* outSupport) const;
Dan Stoza7d7ae732016-03-16 12:23:40 -0700236 [[clang::warn_unused_result]] Error getHdrCapabilities(
Peiyong Lin62665892018-04-16 11:07:44 -0700237 android::HdrCapabilities* outCapabilities) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700238 [[clang::warn_unused_result]] Error getReleaseFences(
Steven Thomas94e35b92017-07-26 18:48:28 -0700239 std::unordered_map<Layer*,
Dan Stoza651bf312015-10-23 17:03:17 -0700240 android::sp<android::Fence>>* outFences) const;
241 [[clang::warn_unused_result]] Error present(
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800242 android::sp<android::Fence>* outPresentFence);
Dan Stoza651bf312015-10-23 17:03:17 -0700243 [[clang::warn_unused_result]] Error setActiveConfig(
244 const std::shared_ptr<const Config>& config);
245 [[clang::warn_unused_result]] Error setClientTarget(
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400246 uint32_t slot, const android::sp<android::GraphicBuffer>& target,
Dan Stoza651bf312015-10-23 17:03:17 -0700247 const android::sp<android::Fence>& acquireFence,
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700248 android::ui::Dataspace dataspace);
Peiyong Linfd997e02018-03-28 15:29:00 -0700249 [[clang::warn_unused_result]] Error setColorMode(
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700250 android::ui::ColorMode mode,
251 android::ui::RenderIntent renderIntent);
Dan Stoza5df2a862016-03-24 16:19:37 -0700252 [[clang::warn_unused_result]] Error setColorTransform(
253 const android::mat4& matrix, android_color_transform_t hint);
Dan Stoza651bf312015-10-23 17:03:17 -0700254 [[clang::warn_unused_result]] Error setOutputBuffer(
255 const android::sp<android::GraphicBuffer>& buffer,
256 const android::sp<android::Fence>& releaseFence);
257 [[clang::warn_unused_result]] Error setPowerMode(PowerMode mode);
258 [[clang::warn_unused_result]] Error setVsyncEnabled(Vsync enabled);
259 [[clang::warn_unused_result]] Error validate(uint32_t* outNumTypes,
260 uint32_t* outNumRequests);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700261 [[clang::warn_unused_result]] Error presentOrValidate(uint32_t* outNumTypes,
Peiyong Linfd997e02018-03-28 15:29:00 -0700262 uint32_t* outNumRequests,
263 android::sp<android::Fence>* outPresentFence, uint32_t* state);
Dan Stoza651bf312015-10-23 17:03:17 -0700264
265 // Other Display methods
266
Dan Stoza651bf312015-10-23 17:03:17 -0700267 hwc2_display_t getId() const { return mId; }
268 bool isConnected() const { return mIsConnected; }
Steven Thomas94e35b92017-07-26 18:48:28 -0700269 void setConnected(bool connected); // For use by Device only
Dan Stoza651bf312015-10-23 17:03:17 -0700270
271private:
Dan Stoza651bf312015-10-23 17:03:17 -0700272 int32_t getAttribute(hwc2_config_t configId, Attribute attribute);
273 void loadConfig(hwc2_config_t configId);
274 void loadConfigs();
275
Dan Stoza651bf312015-10-23 17:03:17 -0700276 // This may fail (and return a null pointer) if no layer with this ID exists
277 // on this display
Steven Thomas94e35b92017-07-26 18:48:28 -0700278 Layer* getLayerById(hwc2_layer_t id) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700279
Lloyd Piquebc792092018-01-17 11:52:30 -0800280 friend android::TestableSurfaceFlinger;
281
Dan Stoza651bf312015-10-23 17:03:17 -0700282 // Member variables
283
Steven Thomas94e35b92017-07-26 18:48:28 -0700284 // These are references to data owned by HWC2::Device, which will outlive
285 // this HWC2::Display, so these references are guaranteed to be valid for
286 // the lifetime of this object.
287 android::Hwc2::Composer& mComposer;
288 const std::unordered_set<Capability>& mCapabilities;
289
Dan Stoza651bf312015-10-23 17:03:17 -0700290 hwc2_display_t mId;
291 bool mIsConnected;
Chris Forbes016d73c2017-04-11 10:04:31 -0700292 DisplayType mType;
Steven Thomas94e35b92017-07-26 18:48:28 -0700293 std::unordered_map<hwc2_layer_t, std::unique_ptr<Layer>> mLayers;
Lloyd Pique3c085a02018-05-09 19:38:32 -0700294 std::unordered_map<hwc2_config_t, std::shared_ptr<const Config>> mConfigs;
Dan Stoza651bf312015-10-23 17:03:17 -0700295};
296
Fabien Sanglard33960702016-11-29 17:58:21 -0800297// Convenience C++ class to access hwc2_device_t Layer functions directly.
Dan Stoza651bf312015-10-23 17:03:17 -0700298class Layer
299{
300public:
Steven Thomas94e35b92017-07-26 18:48:28 -0700301 Layer(android::Hwc2::Composer& composer,
302 const std::unordered_set<Capability>& capabilities,
303 hwc2_display_t displayId, hwc2_layer_t layerId);
Dan Stoza651bf312015-10-23 17:03:17 -0700304 ~Layer();
305
Dan Stoza651bf312015-10-23 17:03:17 -0700306 hwc2_layer_t getId() const { return mId; }
307
Steven Thomas94e35b92017-07-26 18:48:28 -0700308 // Register a listener to be notified when the layer is destroyed. When the
309 // listener function is called, the Layer will be in the process of being
310 // destroyed, so it's not safe to call methods on it.
311 void setLayerDestroyedListener(std::function<void(Layer*)> listener);
312
Dan Stoza651bf312015-10-23 17:03:17 -0700313 [[clang::warn_unused_result]] Error setCursorPosition(int32_t x, int32_t y);
Chia-I Wu06d63de2017-01-04 14:58:51 +0800314 [[clang::warn_unused_result]] Error setBuffer(uint32_t slot,
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400315 const android::sp<android::GraphicBuffer>& buffer,
Dan Stoza651bf312015-10-23 17:03:17 -0700316 const android::sp<android::Fence>& acquireFence);
317 [[clang::warn_unused_result]] Error setSurfaceDamage(
318 const android::Region& damage);
319
320 [[clang::warn_unused_result]] Error setBlendMode(BlendMode mode);
321 [[clang::warn_unused_result]] Error setColor(hwc_color_t color);
322 [[clang::warn_unused_result]] Error setCompositionType(Composition type);
Dan Stoza5df2a862016-03-24 16:19:37 -0700323 [[clang::warn_unused_result]] Error setDataspace(
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700324 android::ui::Dataspace dataspace);
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700325 [[clang::warn_unused_result]] Error setPerFrameMetadata(
326 const int32_t supportedPerFrameMetadata,
327 const android::HdrMetadata& metadata);
Dan Stoza651bf312015-10-23 17:03:17 -0700328 [[clang::warn_unused_result]] Error setDisplayFrame(
329 const android::Rect& frame);
330 [[clang::warn_unused_result]] Error setPlaneAlpha(float alpha);
331 [[clang::warn_unused_result]] Error setSidebandStream(
332 const native_handle_t* stream);
333 [[clang::warn_unused_result]] Error setSourceCrop(
Dan Stoza5a423ea2017-02-16 14:10:39 -0800334 const android::FloatRect& crop);
Dan Stoza651bf312015-10-23 17:03:17 -0700335 [[clang::warn_unused_result]] Error setTransform(Transform transform);
336 [[clang::warn_unused_result]] Error setVisibleRegion(
337 const android::Region& region);
338 [[clang::warn_unused_result]] Error setZOrder(uint32_t z);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500339 [[clang::warn_unused_result]] Error setInfo(uint32_t type, uint32_t appId);
Dan Stoza651bf312015-10-23 17:03:17 -0700340
341private:
Steven Thomas94e35b92017-07-26 18:48:28 -0700342 // These are references to data owned by HWC2::Device, which will outlive
343 // this HWC2::Layer, so these references are guaranteed to be valid for
344 // the lifetime of this object.
345 android::Hwc2::Composer& mComposer;
346 const std::unordered_set<Capability>& mCapabilities;
347
Dan Stoza651bf312015-10-23 17:03:17 -0700348 hwc2_display_t mDisplayId;
Dan Stoza651bf312015-10-23 17:03:17 -0700349 hwc2_layer_t mId;
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700350 android::ui::Dataspace mDataSpace = android::ui::Dataspace::UNKNOWN;
Courtney Goeltzenleuchterf9c98e52018-02-12 07:23:17 -0700351 android::HdrMetadata mHdrMetadata;
Steven Thomas94e35b92017-07-26 18:48:28 -0700352 std::function<void(Layer*)> mLayerDestroyedListener;
Dan Stoza651bf312015-10-23 17:03:17 -0700353};
354
355} // namespace HWC2
356
357#endif // ANDROID_SF_HWC2_H