blob: 8c0f50c068905465b1cfcddff4a67053a0e82627 [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
Michael Wright5d22d4f2018-06-21 02:50:34 +010040#include "PowerAdvisor.h"
41
Dan Stoza651bf312015-10-23 17:03:17 -070042namespace android {
43 class Fence;
Dan Stoza5a423ea2017-02-16 14:10:39 -080044 class FloatRect;
Dan Stoza651bf312015-10-23 17:03:17 -070045 class GraphicBuffer;
46 class Rect;
47 class Region;
Chia-I Wuaab99f52016-10-05 12:59:58 +080048 namespace Hwc2 {
49 class Composer;
Dan Stoza71bded52016-10-19 11:10:33 -070050 }
Lloyd Piquebc792092018-01-17 11:52:30 -080051
52 class TestableSurfaceFlinger;
Dan Stoza651bf312015-10-23 17:03:17 -070053}
54
55namespace HWC2 {
56
57class Display;
58class Layer;
59
Steven Thomas94e35b92017-07-26 18:48:28 -070060// Implement this interface to receive hardware composer events.
61//
62// These callback functions will generally be called on a hwbinder thread, but
63// when first registering the callback the onHotplugReceived() function will
64// immediately be called on the thread calling registerCallback().
65//
66// All calls receive a sequenceId, which will be the value that was supplied to
67// HWC2::Device::registerCallback(). It's used to help differentiate callbacks
68// from different hardware composer instances.
69class ComposerCallback {
70 public:
71 virtual void onHotplugReceived(int32_t sequenceId, hwc2_display_t display,
Lloyd Pique715a2c12017-12-14 17:18:08 -080072 Connection connection) = 0;
Steven Thomas94e35b92017-07-26 18:48:28 -070073 virtual void onRefreshReceived(int32_t sequenceId,
74 hwc2_display_t display) = 0;
75 virtual void onVsyncReceived(int32_t sequenceId, hwc2_display_t display,
76 int64_t timestamp) = 0;
77 virtual ~ComposerCallback() = default;
78};
Dan Stoza651bf312015-10-23 17:03:17 -070079
Fabien Sanglard33960702016-11-29 17:58:21 -080080// C++ Wrapper around hwc2_device_t. Load all functions pointers
81// and handle callback registration.
Dan Stoza651bf312015-10-23 17:03:17 -070082class Device
83{
84public:
Lloyd Piquea822d522017-12-20 16:42:57 -080085 explicit Device(std::unique_ptr<android::Hwc2::Composer> composer);
Dan Stoza651bf312015-10-23 17:03:17 -070086
Steven Thomas94e35b92017-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;
Dominik Laskowski0954b1d2018-06-13 14:58:49 -070098 Error getDisplayIdentificationData(hwc2_display_t hwcDisplayId, uint8_t* outPort,
99 std::vector<uint8_t>* outData) const;
100
Dan Stoza651bf312015-10-23 17:03:17 -0700101 Error createVirtualDisplay(uint32_t width, uint32_t height,
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700102 android::ui::PixelFormat* format, Display** outDisplay);
Steven Thomas94e35b92017-07-26 18:48:28 -0700103 void destroyDisplay(hwc2_display_t displayId);
Dan Stoza651bf312015-10-23 17:03:17 -0700104
Steven Thomas94e35b92017-07-26 18:48:28 -0700105 void onHotplug(hwc2_display_t displayId, Connection connection);
Dan Stoza651bf312015-10-23 17:03:17 -0700106
107 // Other Device methods
108
Steven Thomas94e35b92017-07-26 18:48:28 -0700109 Display* getDisplayById(hwc2_display_t id);
Dan Stoza09e7a272016-04-14 12:31:01 -0700110
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800111 android::Hwc2::Composer* getComposer() { return mComposer.get(); }
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800112
Chia-I Wuae5a6b82017-10-10 09:09:22 -0700113 // We buffer most state changes and flush them implicitly with
114 // Display::validate, Display::present, and Display::presentOrValidate.
115 // This method provides an explicit way to flush state changes to HWC.
116 Error flushCommands();
117
Dan Stoza651bf312015-10-23 17:03:17 -0700118private:
119 // Initialization methods
120
Dan Stoza651bf312015-10-23 17:03:17 -0700121 void loadCapabilities();
Dan Stoza651bf312015-10-23 17:03:17 -0700122
123 // Member variables
Chia-I Wuaab99f52016-10-05 12:59:58 +0800124 std::unique_ptr<android::Hwc2::Composer> mComposer;
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700125 std::unordered_set<Capability> mCapabilities;
Steven Thomas94e35b92017-07-26 18:48:28 -0700126 std::unordered_map<hwc2_display_t, std::unique_ptr<Display>> mDisplays;
Michael Wright5d22d4f2018-06-21 02:50:34 +0100127 android::Hwc2::impl::PowerAdvisor mPowerAdvisor;
Lloyd Piquea822d522017-12-20 16:42:57 -0800128 bool mRegisteredCallback = false;
Dan Stoza651bf312015-10-23 17:03:17 -0700129};
130
Fabien Sanglard33960702016-11-29 17:58:21 -0800131// Convenience C++ class to access hwc2_device_t Display functions directly.
Steven Thomas94e35b92017-07-26 18:48:28 -0700132class Display
Dan Stoza651bf312015-10-23 17:03:17 -0700133{
134public:
Michael Wright5d22d4f2018-06-21 02:50:34 +0100135 Display(android::Hwc2::Composer& composer, android::Hwc2::PowerAdvisor& advisor,
136 const std::unordered_set<Capability>& capabilities,
Steven Thomas94e35b92017-07-26 18:48:28 -0700137 hwc2_display_t id, DisplayType type);
Dan Stoza651bf312015-10-23 17:03:17 -0700138 ~Display();
139
Dan Stoza651bf312015-10-23 17:03:17 -0700140 class Config
141 {
142 public:
143 class Builder
144 {
145 public:
146 Builder(Display& display, hwc2_config_t id);
147
148 std::shared_ptr<const Config> build() {
149 return std::const_pointer_cast<const Config>(
150 std::move(mConfig));
151 }
152
153 Builder& setWidth(int32_t width) {
154 mConfig->mWidth = width;
155 return *this;
156 }
157 Builder& setHeight(int32_t height) {
158 mConfig->mHeight = height;
159 return *this;
160 }
161 Builder& setVsyncPeriod(int32_t vsyncPeriod) {
162 mConfig->mVsyncPeriod = vsyncPeriod;
163 return *this;
164 }
165 Builder& setDpiX(int32_t dpiX) {
166 if (dpiX == -1) {
167 mConfig->mDpiX = getDefaultDensity();
168 } else {
169 mConfig->mDpiX = dpiX / 1000.0f;
170 }
171 return *this;
172 }
173 Builder& setDpiY(int32_t dpiY) {
174 if (dpiY == -1) {
175 mConfig->mDpiY = getDefaultDensity();
176 } else {
177 mConfig->mDpiY = dpiY / 1000.0f;
178 }
179 return *this;
180 }
181
182 private:
183 float getDefaultDensity();
184 std::shared_ptr<Config> mConfig;
185 };
186
187 hwc2_display_t getDisplayId() const { return mDisplay.getId(); }
188 hwc2_config_t getId() const { return mId; }
189
190 int32_t getWidth() const { return mWidth; }
191 int32_t getHeight() const { return mHeight; }
David Sodman1afa8b02018-10-15 11:20:48 -0700192 nsecs_t getVsyncPeriod() const { return mVsyncPeriod; }
Dan Stoza651bf312015-10-23 17:03:17 -0700193 float getDpiX() const { return mDpiX; }
194 float getDpiY() const { return mDpiY; }
195
196 private:
197 Config(Display& display, hwc2_config_t id);
198
199 Display& mDisplay;
200 hwc2_config_t mId;
201
202 int32_t mWidth;
203 int32_t mHeight;
204 nsecs_t mVsyncPeriod;
205 float mDpiX;
206 float mDpiY;
207 };
208
209 // Required by HWC2
210
211 [[clang::warn_unused_result]] Error acceptChanges();
Steven Thomas94e35b92017-07-26 18:48:28 -0700212 [[clang::warn_unused_result]] Error createLayer(Layer** outLayer);
213 [[clang::warn_unused_result]] Error destroyLayer(Layer* layer);
Dan Stoza651bf312015-10-23 17:03:17 -0700214 [[clang::warn_unused_result]] Error getActiveConfig(
215 std::shared_ptr<const Config>* outConfig) const;
Lloyd Pique3c085a02018-05-09 19:38:32 -0700216 [[clang::warn_unused_result]] Error getActiveConfigIndex(int* outIndex) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700217 [[clang::warn_unused_result]] Error getChangedCompositionTypes(
Steven Thomas94e35b92017-07-26 18:48:28 -0700218 std::unordered_map<Layer*, Composition>* outTypes);
Dan Stoza076ac672016-03-14 10:47:53 -0700219 [[clang::warn_unused_result]] Error getColorModes(
Peiyong Linfd997e02018-03-28 15:29:00 -0700220 std::vector<android::ui::ColorMode>* outModes) const;
Chia-I Wud7e01d72018-06-21 13:39:09 +0800221 // Returns a bitmask which contains HdrMetadata::Type::*.
222 [[clang::warn_unused_result]] int32_t getSupportedPerFrameMetadata() const;
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700223 [[clang::warn_unused_result]] Error getRenderIntents(
224 android::ui::ColorMode colorMode,
225 std::vector<android::ui::RenderIntent>* outRenderIntents) const;
226 [[clang::warn_unused_result]] Error getDataspaceSaturationMatrix(
227 android::ui::Dataspace dataspace, android::mat4* outMatrix);
Dan Stoza651bf312015-10-23 17:03:17 -0700228
229 // Doesn't call into the HWC2 device, so no errors are possible
230 std::vector<std::shared_ptr<const Config>> getConfigs() const;
231
232 [[clang::warn_unused_result]] Error getName(std::string* outName) const;
233 [[clang::warn_unused_result]] Error getRequests(
234 DisplayRequest* outDisplayRequests,
Steven Thomas94e35b92017-07-26 18:48:28 -0700235 std::unordered_map<Layer*, LayerRequest>* outLayerRequests);
Dan Stoza651bf312015-10-23 17:03:17 -0700236 [[clang::warn_unused_result]] Error getType(DisplayType* outType) const;
237 [[clang::warn_unused_result]] Error supportsDoze(bool* outSupport) const;
Dan Stoza7d7ae732016-03-16 12:23:40 -0700238 [[clang::warn_unused_result]] Error getHdrCapabilities(
Peiyong Lin62665892018-04-16 11:07:44 -0700239 android::HdrCapabilities* outCapabilities) const;
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700240 [[clang::warn_unused_result]] Error getDisplayedContentSamplingAttributes(
241 android::ui::PixelFormat* outFormat, android::ui::Dataspace* outDataspace,
242 uint8_t* outComponentMask) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700243 [[clang::warn_unused_result]] Error getReleaseFences(
Steven Thomas94e35b92017-07-26 18:48:28 -0700244 std::unordered_map<Layer*,
Dan Stoza651bf312015-10-23 17:03:17 -0700245 android::sp<android::Fence>>* outFences) const;
246 [[clang::warn_unused_result]] Error present(
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800247 android::sp<android::Fence>* outPresentFence);
Dan Stoza651bf312015-10-23 17:03:17 -0700248 [[clang::warn_unused_result]] Error setActiveConfig(
249 const std::shared_ptr<const Config>& config);
250 [[clang::warn_unused_result]] Error setClientTarget(
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400251 uint32_t slot, const android::sp<android::GraphicBuffer>& target,
Dan Stoza651bf312015-10-23 17:03:17 -0700252 const android::sp<android::Fence>& acquireFence,
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700253 android::ui::Dataspace dataspace);
Peiyong Linfd997e02018-03-28 15:29:00 -0700254 [[clang::warn_unused_result]] Error setColorMode(
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700255 android::ui::ColorMode mode,
256 android::ui::RenderIntent renderIntent);
Dan Stoza5df2a862016-03-24 16:19:37 -0700257 [[clang::warn_unused_result]] Error setColorTransform(
258 const android::mat4& matrix, android_color_transform_t hint);
Dan Stoza651bf312015-10-23 17:03:17 -0700259 [[clang::warn_unused_result]] Error setOutputBuffer(
260 const android::sp<android::GraphicBuffer>& buffer,
261 const android::sp<android::Fence>& releaseFence);
262 [[clang::warn_unused_result]] Error setPowerMode(PowerMode mode);
263 [[clang::warn_unused_result]] Error setVsyncEnabled(Vsync enabled);
264 [[clang::warn_unused_result]] Error validate(uint32_t* outNumTypes,
265 uint32_t* outNumRequests);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700266 [[clang::warn_unused_result]] Error presentOrValidate(uint32_t* outNumTypes,
Peiyong Linfd997e02018-03-28 15:29:00 -0700267 uint32_t* outNumRequests,
268 android::sp<android::Fence>* outPresentFence, uint32_t* state);
Dan Stoza651bf312015-10-23 17:03:17 -0700269
270 // Other Display methods
271
Dan Stoza651bf312015-10-23 17:03:17 -0700272 hwc2_display_t getId() const { return mId; }
273 bool isConnected() const { return mIsConnected; }
Steven Thomas94e35b92017-07-26 18:48:28 -0700274 void setConnected(bool connected); // For use by Device only
Peiyong Lined531a32018-10-26 18:27:56 -0700275 const std::unordered_set<DisplayCapability>& getCapabilities() const {
276 return mDisplayCapabilities;
277 };
Dan Stoza651bf312015-10-23 17:03:17 -0700278
279private:
Dan Stoza651bf312015-10-23 17:03:17 -0700280 int32_t getAttribute(hwc2_config_t configId, Attribute attribute);
281 void loadConfig(hwc2_config_t configId);
282 void loadConfigs();
283
Dan Stoza651bf312015-10-23 17:03:17 -0700284 // This may fail (and return a null pointer) if no layer with this ID exists
285 // on this display
Steven Thomas94e35b92017-07-26 18:48:28 -0700286 Layer* getLayerById(hwc2_layer_t id) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700287
Lloyd Piquebc792092018-01-17 11:52:30 -0800288 friend android::TestableSurfaceFlinger;
289
Dan Stoza651bf312015-10-23 17:03:17 -0700290 // Member variables
291
Steven Thomas94e35b92017-07-26 18:48:28 -0700292 // These are references to data owned by HWC2::Device, which will outlive
293 // this HWC2::Display, so these references are guaranteed to be valid for
294 // the lifetime of this object.
295 android::Hwc2::Composer& mComposer;
Michael Wright5d22d4f2018-06-21 02:50:34 +0100296 android::Hwc2::PowerAdvisor& mPowerAdvisor;
Steven Thomas94e35b92017-07-26 18:48:28 -0700297 const std::unordered_set<Capability>& mCapabilities;
298
Dan Stoza651bf312015-10-23 17:03:17 -0700299 hwc2_display_t mId;
300 bool mIsConnected;
Chris Forbes016d73c2017-04-11 10:04:31 -0700301 DisplayType mType;
Steven Thomas94e35b92017-07-26 18:48:28 -0700302 std::unordered_map<hwc2_layer_t, std::unique_ptr<Layer>> mLayers;
Lloyd Pique3c085a02018-05-09 19:38:32 -0700303 std::unordered_map<hwc2_config_t, std::shared_ptr<const Config>> mConfigs;
Peiyong Lined531a32018-10-26 18:27:56 -0700304 std::unordered_set<DisplayCapability> mDisplayCapabilities;
Dan Stoza651bf312015-10-23 17:03:17 -0700305};
306
Fabien Sanglard33960702016-11-29 17:58:21 -0800307// Convenience C++ class to access hwc2_device_t Layer functions directly.
Dan Stoza651bf312015-10-23 17:03:17 -0700308class Layer
309{
310public:
Steven Thomas94e35b92017-07-26 18:48:28 -0700311 Layer(android::Hwc2::Composer& composer,
312 const std::unordered_set<Capability>& capabilities,
313 hwc2_display_t displayId, hwc2_layer_t layerId);
Dan Stoza651bf312015-10-23 17:03:17 -0700314 ~Layer();
315
Dan Stoza651bf312015-10-23 17:03:17 -0700316 hwc2_layer_t getId() const { return mId; }
317
Steven Thomas94e35b92017-07-26 18:48:28 -0700318 // Register a listener to be notified when the layer is destroyed. When the
319 // listener function is called, the Layer will be in the process of being
320 // destroyed, so it's not safe to call methods on it.
321 void setLayerDestroyedListener(std::function<void(Layer*)> listener);
322
Dan Stoza651bf312015-10-23 17:03:17 -0700323 [[clang::warn_unused_result]] Error setCursorPosition(int32_t x, int32_t y);
Chia-I Wu06d63de2017-01-04 14:58:51 +0800324 [[clang::warn_unused_result]] Error setBuffer(uint32_t slot,
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400325 const android::sp<android::GraphicBuffer>& buffer,
Dan Stoza651bf312015-10-23 17:03:17 -0700326 const android::sp<android::Fence>& acquireFence);
327 [[clang::warn_unused_result]] Error setSurfaceDamage(
328 const android::Region& damage);
329
330 [[clang::warn_unused_result]] Error setBlendMode(BlendMode mode);
331 [[clang::warn_unused_result]] Error setColor(hwc_color_t color);
332 [[clang::warn_unused_result]] Error setCompositionType(Composition type);
Dan Stoza5df2a862016-03-24 16:19:37 -0700333 [[clang::warn_unused_result]] Error setDataspace(
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700334 android::ui::Dataspace dataspace);
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700335 [[clang::warn_unused_result]] Error setPerFrameMetadata(
336 const int32_t supportedPerFrameMetadata,
337 const android::HdrMetadata& metadata);
Dan Stoza651bf312015-10-23 17:03:17 -0700338 [[clang::warn_unused_result]] Error setDisplayFrame(
339 const android::Rect& frame);
340 [[clang::warn_unused_result]] Error setPlaneAlpha(float alpha);
341 [[clang::warn_unused_result]] Error setSidebandStream(
342 const native_handle_t* stream);
343 [[clang::warn_unused_result]] Error setSourceCrop(
Dan Stoza5a423ea2017-02-16 14:10:39 -0800344 const android::FloatRect& crop);
Dan Stoza651bf312015-10-23 17:03:17 -0700345 [[clang::warn_unused_result]] Error setTransform(Transform transform);
346 [[clang::warn_unused_result]] Error setVisibleRegion(
347 const android::Region& region);
348 [[clang::warn_unused_result]] Error setZOrder(uint32_t z);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500349 [[clang::warn_unused_result]] Error setInfo(uint32_t type, uint32_t appId);
Dan Stoza651bf312015-10-23 17:03:17 -0700350
Peiyong Lin698147a2018-09-14 13:27:18 -0700351 // Composer HAL 2.3
352 [[clang::warn_unused_result]] Error setColorTransform(const android::mat4& matrix);
353
Dan Stoza651bf312015-10-23 17:03:17 -0700354private:
Steven Thomas94e35b92017-07-26 18:48:28 -0700355 // These are references to data owned by HWC2::Device, which will outlive
356 // this HWC2::Layer, so these references are guaranteed to be valid for
357 // the lifetime of this object.
358 android::Hwc2::Composer& mComposer;
359 const std::unordered_set<Capability>& mCapabilities;
360
Dan Stoza651bf312015-10-23 17:03:17 -0700361 hwc2_display_t mDisplayId;
Dan Stoza651bf312015-10-23 17:03:17 -0700362 hwc2_layer_t mId;
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700363 android::ui::Dataspace mDataSpace = android::ui::Dataspace::UNKNOWN;
Courtney Goeltzenleuchterf9c98e52018-02-12 07:23:17 -0700364 android::HdrMetadata mHdrMetadata;
Steven Thomas94e35b92017-07-26 18:48:28 -0700365 std::function<void(Layer*)> mLayerDestroyedListener;
Peiyong Lin698147a2018-09-14 13:27:18 -0700366 android::mat4 mColorMatrix;
Dan Stoza651bf312015-10-23 17:03:17 -0700367};
368
369} // namespace HWC2
370
371#endif // ANDROID_SF_HWC2_H