blob: f5cb97e061e1b9914c71bc2ee24edda9c6e72c5a [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 {
Kevin DuBois1d4249a2018-08-29 10:45:14 -070043 struct DisplayedFrameStats;
Dan Stoza651bf312015-10-23 17:03:17 -070044 class Fence;
Dan Stoza5a423ea2017-02-16 14:10:39 -080045 class FloatRect;
Dan Stoza651bf312015-10-23 17:03:17 -070046 class GraphicBuffer;
47 class Rect;
48 class Region;
Chia-I Wuaab99f52016-10-05 12:59:58 +080049 namespace Hwc2 {
50 class Composer;
Dan Stoza71bded52016-10-19 11:10:33 -070051 }
Lloyd Piquebc792092018-01-17 11:52:30 -080052
53 class TestableSurfaceFlinger;
Dan Stoza651bf312015-10-23 17:03:17 -070054}
55
56namespace HWC2 {
57
58class Display;
59class Layer;
60
Steven Thomas94e35b92017-07-26 18:48:28 -070061// Implement this interface to receive hardware composer events.
62//
63// These callback functions will generally be called on a hwbinder thread, but
64// when first registering the callback the onHotplugReceived() function will
65// immediately be called on the thread calling registerCallback().
66//
67// All calls receive a sequenceId, which will be the value that was supplied to
68// HWC2::Device::registerCallback(). It's used to help differentiate callbacks
69// from different hardware composer instances.
70class ComposerCallback {
71 public:
72 virtual void onHotplugReceived(int32_t sequenceId, hwc2_display_t display,
Lloyd Pique715a2c12017-12-14 17:18:08 -080073 Connection connection) = 0;
Steven Thomas94e35b92017-07-26 18:48:28 -070074 virtual void onRefreshReceived(int32_t sequenceId,
75 hwc2_display_t display) = 0;
76 virtual void onVsyncReceived(int32_t sequenceId, hwc2_display_t display,
77 int64_t timestamp) = 0;
78 virtual ~ComposerCallback() = default;
79};
Dan Stoza651bf312015-10-23 17:03:17 -070080
Fabien Sanglard33960702016-11-29 17:58:21 -080081// C++ Wrapper around hwc2_device_t. Load all functions pointers
82// and handle callback registration.
Dan Stoza651bf312015-10-23 17:03:17 -070083class Device
84{
85public:
Lloyd Piquea822d522017-12-20 16:42:57 -080086 explicit Device(std::unique_ptr<android::Hwc2::Composer> composer);
Dan Stoza651bf312015-10-23 17:03:17 -070087
Steven Thomas94e35b92017-07-26 18:48:28 -070088 void registerCallback(ComposerCallback* callback, int32_t sequenceId);
Dan Stoza651bf312015-10-23 17:03:17 -070089
90 // Required by HWC2
91
92 std::string dump() const;
93
Dan Stoza9f26a9c2016-06-22 14:51:09 -070094 const std::unordered_set<Capability>& getCapabilities() const {
Dan Stoza651bf312015-10-23 17:03:17 -070095 return mCapabilities;
96 };
97
98 uint32_t getMaxVirtualDisplayCount() const;
Dominik Laskowski0954b1d2018-06-13 14:58:49 -070099 Error getDisplayIdentificationData(hwc2_display_t hwcDisplayId, uint8_t* outPort,
100 std::vector<uint8_t>* outData) const;
101
Dan Stoza651bf312015-10-23 17:03:17 -0700102 Error createVirtualDisplay(uint32_t width, uint32_t height,
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700103 android::ui::PixelFormat* format, Display** outDisplay);
Steven Thomas94e35b92017-07-26 18:48:28 -0700104 void destroyDisplay(hwc2_display_t displayId);
Dan Stoza651bf312015-10-23 17:03:17 -0700105
Steven Thomas94e35b92017-07-26 18:48:28 -0700106 void onHotplug(hwc2_display_t displayId, Connection connection);
Dan Stoza651bf312015-10-23 17:03:17 -0700107
108 // Other Device methods
109
Steven Thomas94e35b92017-07-26 18:48:28 -0700110 Display* getDisplayById(hwc2_display_t id);
Dan Stoza09e7a272016-04-14 12:31:01 -0700111
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800112 android::Hwc2::Composer* getComposer() { return mComposer.get(); }
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800113
Chia-I Wuae5a6b82017-10-10 09:09:22 -0700114 // We buffer most state changes and flush them implicitly with
115 // Display::validate, Display::present, and Display::presentOrValidate.
116 // This method provides an explicit way to flush state changes to HWC.
117 Error flushCommands();
118
Dan Stoza651bf312015-10-23 17:03:17 -0700119private:
120 // Initialization methods
121
Dan Stoza651bf312015-10-23 17:03:17 -0700122 void loadCapabilities();
Dan Stoza651bf312015-10-23 17:03:17 -0700123
124 // Member variables
Chia-I Wuaab99f52016-10-05 12:59:58 +0800125 std::unique_ptr<android::Hwc2::Composer> mComposer;
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700126 std::unordered_set<Capability> mCapabilities;
Steven Thomas94e35b92017-07-26 18:48:28 -0700127 std::unordered_map<hwc2_display_t, std::unique_ptr<Display>> mDisplays;
Michael Wright5d22d4f2018-06-21 02:50:34 +0100128 android::Hwc2::impl::PowerAdvisor mPowerAdvisor;
Lloyd Piquea822d522017-12-20 16:42:57 -0800129 bool mRegisteredCallback = false;
Dan Stoza651bf312015-10-23 17:03:17 -0700130};
131
Fabien Sanglard33960702016-11-29 17:58:21 -0800132// Convenience C++ class to access hwc2_device_t Display functions directly.
Steven Thomas94e35b92017-07-26 18:48:28 -0700133class Display
Dan Stoza651bf312015-10-23 17:03:17 -0700134{
135public:
Michael Wright5d22d4f2018-06-21 02:50:34 +0100136 Display(android::Hwc2::Composer& composer, android::Hwc2::PowerAdvisor& advisor,
137 const std::unordered_set<Capability>& capabilities,
Steven Thomas94e35b92017-07-26 18:48:28 -0700138 hwc2_display_t id, DisplayType type);
Dan Stoza651bf312015-10-23 17:03:17 -0700139 ~Display();
140
Dan Stoza651bf312015-10-23 17:03:17 -0700141 class Config
142 {
143 public:
144 class Builder
145 {
146 public:
147 Builder(Display& display, hwc2_config_t id);
148
149 std::shared_ptr<const Config> build() {
150 return std::const_pointer_cast<const Config>(
151 std::move(mConfig));
152 }
153
154 Builder& setWidth(int32_t width) {
155 mConfig->mWidth = width;
156 return *this;
157 }
158 Builder& setHeight(int32_t height) {
159 mConfig->mHeight = height;
160 return *this;
161 }
162 Builder& setVsyncPeriod(int32_t vsyncPeriod) {
163 mConfig->mVsyncPeriod = vsyncPeriod;
164 return *this;
165 }
166 Builder& setDpiX(int32_t dpiX) {
167 if (dpiX == -1) {
168 mConfig->mDpiX = getDefaultDensity();
169 } else {
170 mConfig->mDpiX = dpiX / 1000.0f;
171 }
172 return *this;
173 }
174 Builder& setDpiY(int32_t dpiY) {
175 if (dpiY == -1) {
176 mConfig->mDpiY = getDefaultDensity();
177 } else {
178 mConfig->mDpiY = dpiY / 1000.0f;
179 }
180 return *this;
181 }
182
183 private:
184 float getDefaultDensity();
185 std::shared_ptr<Config> mConfig;
186 };
187
188 hwc2_display_t getDisplayId() const { return mDisplay.getId(); }
189 hwc2_config_t getId() const { return mId; }
190
191 int32_t getWidth() const { return mWidth; }
192 int32_t getHeight() const { return mHeight; }
David Sodman1afa8b02018-10-15 11:20:48 -0700193 nsecs_t getVsyncPeriod() const { return mVsyncPeriod; }
Dan Stoza651bf312015-10-23 17:03:17 -0700194 float getDpiX() const { return mDpiX; }
195 float getDpiY() const { return mDpiY; }
196
197 private:
198 Config(Display& display, hwc2_config_t id);
199
200 Display& mDisplay;
201 hwc2_config_t mId;
202
203 int32_t mWidth;
204 int32_t mHeight;
205 nsecs_t mVsyncPeriod;
206 float mDpiX;
207 float mDpiY;
208 };
209
210 // Required by HWC2
211
212 [[clang::warn_unused_result]] Error acceptChanges();
Steven Thomas94e35b92017-07-26 18:48:28 -0700213 [[clang::warn_unused_result]] Error createLayer(Layer** outLayer);
214 [[clang::warn_unused_result]] Error destroyLayer(Layer* layer);
Dan Stoza651bf312015-10-23 17:03:17 -0700215 [[clang::warn_unused_result]] Error getActiveConfig(
216 std::shared_ptr<const Config>* outConfig) const;
Lloyd Pique3c085a02018-05-09 19:38:32 -0700217 [[clang::warn_unused_result]] Error getActiveConfigIndex(int* outIndex) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700218 [[clang::warn_unused_result]] Error getChangedCompositionTypes(
Steven Thomas94e35b92017-07-26 18:48:28 -0700219 std::unordered_map<Layer*, Composition>* outTypes);
Dan Stoza076ac672016-03-14 10:47:53 -0700220 [[clang::warn_unused_result]] Error getColorModes(
Peiyong Linfd997e02018-03-28 15:29:00 -0700221 std::vector<android::ui::ColorMode>* outModes) const;
Chia-I Wud7e01d72018-06-21 13:39:09 +0800222 // Returns a bitmask which contains HdrMetadata::Type::*.
223 [[clang::warn_unused_result]] int32_t getSupportedPerFrameMetadata() const;
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700224 [[clang::warn_unused_result]] Error getRenderIntents(
225 android::ui::ColorMode colorMode,
226 std::vector<android::ui::RenderIntent>* outRenderIntents) const;
227 [[clang::warn_unused_result]] Error getDataspaceSaturationMatrix(
228 android::ui::Dataspace dataspace, android::mat4* outMatrix);
Dan Stoza651bf312015-10-23 17:03:17 -0700229
230 // Doesn't call into the HWC2 device, so no errors are possible
231 std::vector<std::shared_ptr<const Config>> getConfigs() const;
232
233 [[clang::warn_unused_result]] Error getName(std::string* outName) const;
234 [[clang::warn_unused_result]] Error getRequests(
235 DisplayRequest* outDisplayRequests,
Steven Thomas94e35b92017-07-26 18:48:28 -0700236 std::unordered_map<Layer*, LayerRequest>* outLayerRequests);
Dan Stoza651bf312015-10-23 17:03:17 -0700237 [[clang::warn_unused_result]] Error getType(DisplayType* outType) const;
238 [[clang::warn_unused_result]] Error supportsDoze(bool* outSupport) const;
Dan Stoza7d7ae732016-03-16 12:23:40 -0700239 [[clang::warn_unused_result]] Error getHdrCapabilities(
Peiyong Lin62665892018-04-16 11:07:44 -0700240 android::HdrCapabilities* outCapabilities) const;
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700241 [[clang::warn_unused_result]] Error getDisplayedContentSamplingAttributes(
242 android::ui::PixelFormat* outFormat, android::ui::Dataspace* outDataspace,
243 uint8_t* outComponentMask) const;
Kevin DuBois74e53772018-11-19 10:52:38 -0800244 [[clang::warn_unused_result]] Error setDisplayContentSamplingEnabled(bool enabled,
245 uint8_t componentMask,
246 uint64_t maxFrames) const;
Kevin DuBois1d4249a2018-08-29 10:45:14 -0700247 [[clang::warn_unused_result]] Error getDisplayedContentSample(
248 uint64_t maxFrames, uint64_t timestamp, android::DisplayedFrameStats* outStats) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700249 [[clang::warn_unused_result]] Error getReleaseFences(
Steven Thomas94e35b92017-07-26 18:48:28 -0700250 std::unordered_map<Layer*,
Dan Stoza651bf312015-10-23 17:03:17 -0700251 android::sp<android::Fence>>* outFences) const;
252 [[clang::warn_unused_result]] Error present(
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800253 android::sp<android::Fence>* outPresentFence);
Dan Stoza651bf312015-10-23 17:03:17 -0700254 [[clang::warn_unused_result]] Error setActiveConfig(
255 const std::shared_ptr<const Config>& config);
256 [[clang::warn_unused_result]] Error setClientTarget(
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400257 uint32_t slot, const android::sp<android::GraphicBuffer>& target,
Dan Stoza651bf312015-10-23 17:03:17 -0700258 const android::sp<android::Fence>& acquireFence,
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700259 android::ui::Dataspace dataspace);
Peiyong Linfd997e02018-03-28 15:29:00 -0700260 [[clang::warn_unused_result]] Error setColorMode(
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700261 android::ui::ColorMode mode,
262 android::ui::RenderIntent renderIntent);
Dan Stoza5df2a862016-03-24 16:19:37 -0700263 [[clang::warn_unused_result]] Error setColorTransform(
264 const android::mat4& matrix, android_color_transform_t hint);
Dan Stoza651bf312015-10-23 17:03:17 -0700265 [[clang::warn_unused_result]] Error setOutputBuffer(
266 const android::sp<android::GraphicBuffer>& buffer,
267 const android::sp<android::Fence>& releaseFence);
268 [[clang::warn_unused_result]] Error setPowerMode(PowerMode mode);
269 [[clang::warn_unused_result]] Error setVsyncEnabled(Vsync enabled);
270 [[clang::warn_unused_result]] Error validate(uint32_t* outNumTypes,
271 uint32_t* outNumRequests);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700272 [[clang::warn_unused_result]] Error presentOrValidate(uint32_t* outNumTypes,
Peiyong Linfd997e02018-03-28 15:29:00 -0700273 uint32_t* outNumRequests,
274 android::sp<android::Fence>* outPresentFence, uint32_t* state);
Dan Stoza651bf312015-10-23 17:03:17 -0700275
276 // Other Display methods
277
Dan Stoza651bf312015-10-23 17:03:17 -0700278 hwc2_display_t getId() const { return mId; }
279 bool isConnected() const { return mIsConnected; }
Steven Thomas94e35b92017-07-26 18:48:28 -0700280 void setConnected(bool connected); // For use by Device only
Peiyong Lined531a32018-10-26 18:27:56 -0700281 const std::unordered_set<DisplayCapability>& getCapabilities() const {
282 return mDisplayCapabilities;
283 };
Dan Stoza651bf312015-10-23 17:03:17 -0700284
285private:
Dan Stoza651bf312015-10-23 17:03:17 -0700286 int32_t getAttribute(hwc2_config_t configId, Attribute attribute);
287 void loadConfig(hwc2_config_t configId);
288 void loadConfigs();
289
Dan Stoza651bf312015-10-23 17:03:17 -0700290 // This may fail (and return a null pointer) if no layer with this ID exists
291 // on this display
Steven Thomas94e35b92017-07-26 18:48:28 -0700292 Layer* getLayerById(hwc2_layer_t id) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700293
Lloyd Piquebc792092018-01-17 11:52:30 -0800294 friend android::TestableSurfaceFlinger;
295
Dan Stoza651bf312015-10-23 17:03:17 -0700296 // Member variables
297
Steven Thomas94e35b92017-07-26 18:48:28 -0700298 // These are references to data owned by HWC2::Device, which will outlive
299 // this HWC2::Display, so these references are guaranteed to be valid for
300 // the lifetime of this object.
301 android::Hwc2::Composer& mComposer;
Michael Wright5d22d4f2018-06-21 02:50:34 +0100302 android::Hwc2::PowerAdvisor& mPowerAdvisor;
Steven Thomas94e35b92017-07-26 18:48:28 -0700303 const std::unordered_set<Capability>& mCapabilities;
304
Dan Stoza651bf312015-10-23 17:03:17 -0700305 hwc2_display_t mId;
306 bool mIsConnected;
Chris Forbes016d73c2017-04-11 10:04:31 -0700307 DisplayType mType;
Steven Thomas94e35b92017-07-26 18:48:28 -0700308 std::unordered_map<hwc2_layer_t, std::unique_ptr<Layer>> mLayers;
Lloyd Pique3c085a02018-05-09 19:38:32 -0700309 std::unordered_map<hwc2_config_t, std::shared_ptr<const Config>> mConfigs;
Peiyong Lined531a32018-10-26 18:27:56 -0700310 std::unordered_set<DisplayCapability> mDisplayCapabilities;
Dan Stoza651bf312015-10-23 17:03:17 -0700311};
312
Fabien Sanglard33960702016-11-29 17:58:21 -0800313// Convenience C++ class to access hwc2_device_t Layer functions directly.
Dan Stoza651bf312015-10-23 17:03:17 -0700314class Layer
315{
316public:
Steven Thomas94e35b92017-07-26 18:48:28 -0700317 Layer(android::Hwc2::Composer& composer,
318 const std::unordered_set<Capability>& capabilities,
319 hwc2_display_t displayId, hwc2_layer_t layerId);
Dan Stoza651bf312015-10-23 17:03:17 -0700320 ~Layer();
321
Dan Stoza651bf312015-10-23 17:03:17 -0700322 hwc2_layer_t getId() const { return mId; }
323
Steven Thomas94e35b92017-07-26 18:48:28 -0700324 // Register a listener to be notified when the layer is destroyed. When the
325 // listener function is called, the Layer will be in the process of being
326 // destroyed, so it's not safe to call methods on it.
327 void setLayerDestroyedListener(std::function<void(Layer*)> listener);
328
Dan Stoza651bf312015-10-23 17:03:17 -0700329 [[clang::warn_unused_result]] Error setCursorPosition(int32_t x, int32_t y);
Chia-I Wu06d63de2017-01-04 14:58:51 +0800330 [[clang::warn_unused_result]] Error setBuffer(uint32_t slot,
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400331 const android::sp<android::GraphicBuffer>& buffer,
Dan Stoza651bf312015-10-23 17:03:17 -0700332 const android::sp<android::Fence>& acquireFence);
333 [[clang::warn_unused_result]] Error setSurfaceDamage(
334 const android::Region& damage);
335
336 [[clang::warn_unused_result]] Error setBlendMode(BlendMode mode);
337 [[clang::warn_unused_result]] Error setColor(hwc_color_t color);
338 [[clang::warn_unused_result]] Error setCompositionType(Composition type);
Dan Stoza5df2a862016-03-24 16:19:37 -0700339 [[clang::warn_unused_result]] Error setDataspace(
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700340 android::ui::Dataspace dataspace);
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700341 [[clang::warn_unused_result]] Error setPerFrameMetadata(
342 const int32_t supportedPerFrameMetadata,
343 const android::HdrMetadata& metadata);
Dan Stoza651bf312015-10-23 17:03:17 -0700344 [[clang::warn_unused_result]] Error setDisplayFrame(
345 const android::Rect& frame);
346 [[clang::warn_unused_result]] Error setPlaneAlpha(float alpha);
347 [[clang::warn_unused_result]] Error setSidebandStream(
348 const native_handle_t* stream);
349 [[clang::warn_unused_result]] Error setSourceCrop(
Dan Stoza5a423ea2017-02-16 14:10:39 -0800350 const android::FloatRect& crop);
Dan Stoza651bf312015-10-23 17:03:17 -0700351 [[clang::warn_unused_result]] Error setTransform(Transform transform);
352 [[clang::warn_unused_result]] Error setVisibleRegion(
353 const android::Region& region);
354 [[clang::warn_unused_result]] Error setZOrder(uint32_t z);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500355 [[clang::warn_unused_result]] Error setInfo(uint32_t type, uint32_t appId);
Dan Stoza651bf312015-10-23 17:03:17 -0700356
Peiyong Lin698147a2018-09-14 13:27:18 -0700357 // Composer HAL 2.3
358 [[clang::warn_unused_result]] Error setColorTransform(const android::mat4& matrix);
359
Dan Stoza651bf312015-10-23 17:03:17 -0700360private:
Steven Thomas94e35b92017-07-26 18:48:28 -0700361 // These are references to data owned by HWC2::Device, which will outlive
362 // this HWC2::Layer, so these references are guaranteed to be valid for
363 // the lifetime of this object.
364 android::Hwc2::Composer& mComposer;
365 const std::unordered_set<Capability>& mCapabilities;
366
Dan Stoza651bf312015-10-23 17:03:17 -0700367 hwc2_display_t mDisplayId;
Dan Stoza651bf312015-10-23 17:03:17 -0700368 hwc2_layer_t mId;
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700369 android::ui::Dataspace mDataSpace = android::ui::Dataspace::UNKNOWN;
Courtney Goeltzenleuchterf9c98e52018-02-12 07:23:17 -0700370 android::HdrMetadata mHdrMetadata;
Steven Thomas94e35b92017-07-26 18:48:28 -0700371 std::function<void(Layer*)> mLayerDestroyedListener;
Peiyong Lin698147a2018-09-14 13:27:18 -0700372 android::mat4 mColorMatrix;
Dan Stoza651bf312015-10-23 17:03:17 -0700373};
374
375} // namespace HWC2
376
377#endif // ANDROID_SF_HWC2_H