blob: a8f24d61be037c429596632337ac538b04e2c183 [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
David Sodman6d46c1e2018-07-13 12:59:48 -070026#include <cutils/properties.h>
Courtney Goeltzenleuchterf9c98e52018-02-12 07:23:17 -070027#include <gui/HdrMetadata.h>
Mathias Agopian1d77b712017-02-17 15:46:13 -080028#include <math/mat4.h>
Peiyong Linfd997e02018-03-28 15:29:00 -070029#include <ui/GraphicTypes.h>
Courtney Goeltzenleuchterf9c98e52018-02-12 07:23:17 -070030#include <ui/HdrCapabilities.h>
Dan Stoza651bf312015-10-23 17:03:17 -070031#include <utils/Log.h>
32#include <utils/StrongPointer.h>
33#include <utils/Timers.h>
34
35#include <functional>
36#include <string>
37#include <unordered_map>
Dan Stoza9f26a9c2016-06-22 14:51:09 -070038#include <unordered_set>
Dan Stoza651bf312015-10-23 17:03:17 -070039#include <vector>
40
Michael Wright5d22d4f2018-06-21 02:50:34 +010041#include "PowerAdvisor.h"
42
Dan Stoza651bf312015-10-23 17:03:17 -070043namespace android {
44 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
David Sodman44b5de02018-08-21 16:28:53 -070088 struct FrequencyScaler {
89 int32_t multiplier = 1;
90 int32_t divisor = 1;
91 };
92
Steven Thomas94e35b92017-07-26 18:48:28 -070093 void registerCallback(ComposerCallback* callback, int32_t sequenceId);
Dan Stoza651bf312015-10-23 17:03:17 -070094
95 // Required by HWC2
96
97 std::string dump() const;
98
Dan Stoza9f26a9c2016-06-22 14:51:09 -070099 const std::unordered_set<Capability>& getCapabilities() const {
Dan Stoza651bf312015-10-23 17:03:17 -0700100 return mCapabilities;
101 };
102
103 uint32_t getMaxVirtualDisplayCount() const;
Dominik Laskowski0954b1d2018-06-13 14:58:49 -0700104 Error getDisplayIdentificationData(hwc2_display_t hwcDisplayId, uint8_t* outPort,
105 std::vector<uint8_t>* outData) const;
106
Dan Stoza651bf312015-10-23 17:03:17 -0700107 Error createVirtualDisplay(uint32_t width, uint32_t height,
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700108 android::ui::PixelFormat* format, Display** outDisplay);
Steven Thomas94e35b92017-07-26 18:48:28 -0700109 void destroyDisplay(hwc2_display_t displayId);
Dan Stoza651bf312015-10-23 17:03:17 -0700110
Steven Thomas94e35b92017-07-26 18:48:28 -0700111 void onHotplug(hwc2_display_t displayId, Connection connection);
Dan Stoza651bf312015-10-23 17:03:17 -0700112
113 // Other Device methods
114
Steven Thomas94e35b92017-07-26 18:48:28 -0700115 Display* getDisplayById(hwc2_display_t id);
Dan Stoza09e7a272016-04-14 12:31:01 -0700116
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800117 android::Hwc2::Composer* getComposer() { return mComposer.get(); }
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800118
Chia-I Wuae5a6b82017-10-10 09:09:22 -0700119 // We buffer most state changes and flush them implicitly with
120 // Display::validate, Display::present, and Display::presentOrValidate.
121 // This method provides an explicit way to flush state changes to HWC.
122 Error flushCommands();
123
David Sodman44b5de02018-08-21 16:28:53 -0700124 void setDisplayFrequencyScaleParameters(FrequencyScaler frequecyScaler);
125 FrequencyScaler getDisplayFrequencyScaleParameters();
126
Dan Stoza651bf312015-10-23 17:03:17 -0700127private:
128 // Initialization methods
129
Dan Stoza651bf312015-10-23 17:03:17 -0700130 void loadCapabilities();
Dan Stoza651bf312015-10-23 17:03:17 -0700131
132 // Member variables
Chia-I Wuaab99f52016-10-05 12:59:58 +0800133 std::unique_ptr<android::Hwc2::Composer> mComposer;
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700134 std::unordered_set<Capability> mCapabilities;
Steven Thomas94e35b92017-07-26 18:48:28 -0700135 std::unordered_map<hwc2_display_t, std::unique_ptr<Display>> mDisplays;
Michael Wright5d22d4f2018-06-21 02:50:34 +0100136 android::Hwc2::impl::PowerAdvisor mPowerAdvisor;
David Sodman44b5de02018-08-21 16:28:53 -0700137 FrequencyScaler mFrequencyScaler;
Lloyd Piquea822d522017-12-20 16:42:57 -0800138 bool mRegisteredCallback = false;
Dan Stoza651bf312015-10-23 17:03:17 -0700139};
140
Fabien Sanglard33960702016-11-29 17:58:21 -0800141// Convenience C++ class to access hwc2_device_t Display functions directly.
Steven Thomas94e35b92017-07-26 18:48:28 -0700142class Display
Dan Stoza651bf312015-10-23 17:03:17 -0700143{
144public:
Michael Wright5d22d4f2018-06-21 02:50:34 +0100145 Display(android::Hwc2::Composer& composer, android::Hwc2::PowerAdvisor& advisor,
146 const std::unordered_set<Capability>& capabilities,
Steven Thomas94e35b92017-07-26 18:48:28 -0700147 hwc2_display_t id, DisplayType type);
Dan Stoza651bf312015-10-23 17:03:17 -0700148 ~Display();
149
Dan Stoza651bf312015-10-23 17:03:17 -0700150 class Config
151 {
152 public:
153 class Builder
154 {
155 public:
156 Builder(Display& display, hwc2_config_t id);
157
158 std::shared_ptr<const Config> build() {
159 return std::const_pointer_cast<const Config>(
160 std::move(mConfig));
161 }
162
163 Builder& setWidth(int32_t width) {
164 mConfig->mWidth = width;
165 return *this;
166 }
167 Builder& setHeight(int32_t height) {
168 mConfig->mHeight = height;
169 return *this;
170 }
171 Builder& setVsyncPeriod(int32_t vsyncPeriod) {
172 mConfig->mVsyncPeriod = vsyncPeriod;
173 return *this;
174 }
175 Builder& setDpiX(int32_t dpiX) {
176 if (dpiX == -1) {
177 mConfig->mDpiX = getDefaultDensity();
178 } else {
179 mConfig->mDpiX = dpiX / 1000.0f;
180 }
181 return *this;
182 }
183 Builder& setDpiY(int32_t dpiY) {
184 if (dpiY == -1) {
185 mConfig->mDpiY = getDefaultDensity();
186 } else {
187 mConfig->mDpiY = dpiY / 1000.0f;
188 }
189 return *this;
190 }
191
192 private:
193 float getDefaultDensity();
194 std::shared_ptr<Config> mConfig;
195 };
196
197 hwc2_display_t getDisplayId() const { return mDisplay.getId(); }
198 hwc2_config_t getId() const { return mId; }
199
200 int32_t getWidth() const { return mWidth; }
201 int32_t getHeight() const { return mHeight; }
David Sodman6d46c1e2018-07-13 12:59:48 -0700202 nsecs_t getVsyncPeriod() const {
David Sodman44b5de02018-08-21 16:28:53 -0700203 return mVsyncPeriod * mFrequencyScaler.multiplier / mFrequencyScaler.divisor; }
Dan Stoza651bf312015-10-23 17:03:17 -0700204 float getDpiX() const { return mDpiX; }
205 float getDpiY() const { return mDpiY; }
206
207 private:
208 Config(Display& display, hwc2_config_t id);
209
210 Display& mDisplay;
211 hwc2_config_t mId;
212
213 int32_t mWidth;
214 int32_t mHeight;
215 nsecs_t mVsyncPeriod;
David Sodman44b5de02018-08-21 16:28:53 -0700216 Device::FrequencyScaler mFrequencyScaler;
Dan Stoza651bf312015-10-23 17:03:17 -0700217 float mDpiX;
218 float mDpiY;
219 };
220
221 // Required by HWC2
222
223 [[clang::warn_unused_result]] Error acceptChanges();
Steven Thomas94e35b92017-07-26 18:48:28 -0700224 [[clang::warn_unused_result]] Error createLayer(Layer** outLayer);
225 [[clang::warn_unused_result]] Error destroyLayer(Layer* layer);
Dan Stoza651bf312015-10-23 17:03:17 -0700226 [[clang::warn_unused_result]] Error getActiveConfig(
227 std::shared_ptr<const Config>* outConfig) const;
Lloyd Pique3c085a02018-05-09 19:38:32 -0700228 [[clang::warn_unused_result]] Error getActiveConfigIndex(int* outIndex) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700229 [[clang::warn_unused_result]] Error getChangedCompositionTypes(
Steven Thomas94e35b92017-07-26 18:48:28 -0700230 std::unordered_map<Layer*, Composition>* outTypes);
Dan Stoza076ac672016-03-14 10:47:53 -0700231 [[clang::warn_unused_result]] Error getColorModes(
Peiyong Linfd997e02018-03-28 15:29:00 -0700232 std::vector<android::ui::ColorMode>* outModes) const;
Chia-I Wud7e01d72018-06-21 13:39:09 +0800233 // Returns a bitmask which contains HdrMetadata::Type::*.
234 [[clang::warn_unused_result]] int32_t getSupportedPerFrameMetadata() const;
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700235 [[clang::warn_unused_result]] Error getRenderIntents(
236 android::ui::ColorMode colorMode,
237 std::vector<android::ui::RenderIntent>* outRenderIntents) const;
238 [[clang::warn_unused_result]] Error getDataspaceSaturationMatrix(
239 android::ui::Dataspace dataspace, android::mat4* outMatrix);
Dan Stoza651bf312015-10-23 17:03:17 -0700240
241 // Doesn't call into the HWC2 device, so no errors are possible
242 std::vector<std::shared_ptr<const Config>> getConfigs() const;
243
244 [[clang::warn_unused_result]] Error getName(std::string* outName) const;
245 [[clang::warn_unused_result]] Error getRequests(
246 DisplayRequest* outDisplayRequests,
Steven Thomas94e35b92017-07-26 18:48:28 -0700247 std::unordered_map<Layer*, LayerRequest>* outLayerRequests);
Dan Stoza651bf312015-10-23 17:03:17 -0700248 [[clang::warn_unused_result]] Error getType(DisplayType* outType) const;
249 [[clang::warn_unused_result]] Error supportsDoze(bool* outSupport) const;
Dan Stoza7d7ae732016-03-16 12:23:40 -0700250 [[clang::warn_unused_result]] Error getHdrCapabilities(
Peiyong Lin62665892018-04-16 11:07:44 -0700251 android::HdrCapabilities* outCapabilities) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700252 [[clang::warn_unused_result]] Error getReleaseFences(
Steven Thomas94e35b92017-07-26 18:48:28 -0700253 std::unordered_map<Layer*,
Dan Stoza651bf312015-10-23 17:03:17 -0700254 android::sp<android::Fence>>* outFences) const;
255 [[clang::warn_unused_result]] Error present(
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800256 android::sp<android::Fence>* outPresentFence);
Dan Stoza651bf312015-10-23 17:03:17 -0700257 [[clang::warn_unused_result]] Error setActiveConfig(
258 const std::shared_ptr<const Config>& config);
259 [[clang::warn_unused_result]] Error setClientTarget(
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400260 uint32_t slot, const android::sp<android::GraphicBuffer>& target,
Dan Stoza651bf312015-10-23 17:03:17 -0700261 const android::sp<android::Fence>& acquireFence,
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700262 android::ui::Dataspace dataspace);
Peiyong Linfd997e02018-03-28 15:29:00 -0700263 [[clang::warn_unused_result]] Error setColorMode(
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700264 android::ui::ColorMode mode,
265 android::ui::RenderIntent renderIntent);
Dan Stoza5df2a862016-03-24 16:19:37 -0700266 [[clang::warn_unused_result]] Error setColorTransform(
267 const android::mat4& matrix, android_color_transform_t hint);
Dan Stoza651bf312015-10-23 17:03:17 -0700268 [[clang::warn_unused_result]] Error setOutputBuffer(
269 const android::sp<android::GraphicBuffer>& buffer,
270 const android::sp<android::Fence>& releaseFence);
271 [[clang::warn_unused_result]] Error setPowerMode(PowerMode mode);
272 [[clang::warn_unused_result]] Error setVsyncEnabled(Vsync enabled);
273 [[clang::warn_unused_result]] Error validate(uint32_t* outNumTypes,
274 uint32_t* outNumRequests);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700275 [[clang::warn_unused_result]] Error presentOrValidate(uint32_t* outNumTypes,
Peiyong Linfd997e02018-03-28 15:29:00 -0700276 uint32_t* outNumRequests,
277 android::sp<android::Fence>* outPresentFence, uint32_t* state);
Dan Stoza651bf312015-10-23 17:03:17 -0700278
279 // Other Display methods
280
Dan Stoza651bf312015-10-23 17:03:17 -0700281 hwc2_display_t getId() const { return mId; }
282 bool isConnected() const { return mIsConnected; }
Steven Thomas94e35b92017-07-26 18:48:28 -0700283 void setConnected(bool connected); // For use by Device only
David Sodman44b5de02018-08-21 16:28:53 -0700284 void setFrequencyScaleParameters(Device::FrequencyScaler frequencyScaler);
Dan Stoza651bf312015-10-23 17:03:17 -0700285
286private:
Dan Stoza651bf312015-10-23 17:03:17 -0700287 int32_t getAttribute(hwc2_config_t configId, Attribute attribute);
288 void loadConfig(hwc2_config_t configId);
289 void loadConfigs();
290
Dan Stoza651bf312015-10-23 17:03:17 -0700291 // This may fail (and return a null pointer) if no layer with this ID exists
292 // on this display
Steven Thomas94e35b92017-07-26 18:48:28 -0700293 Layer* getLayerById(hwc2_layer_t id) const;
Dan Stoza651bf312015-10-23 17:03:17 -0700294
Lloyd Piquebc792092018-01-17 11:52:30 -0800295 friend android::TestableSurfaceFlinger;
296
Dan Stoza651bf312015-10-23 17:03:17 -0700297 // Member variables
298
Steven Thomas94e35b92017-07-26 18:48:28 -0700299 // These are references to data owned by HWC2::Device, which will outlive
300 // this HWC2::Display, so these references are guaranteed to be valid for
301 // the lifetime of this object.
302 android::Hwc2::Composer& mComposer;
Michael Wright5d22d4f2018-06-21 02:50:34 +0100303 android::Hwc2::PowerAdvisor& mPowerAdvisor;
Steven Thomas94e35b92017-07-26 18:48:28 -0700304 const std::unordered_set<Capability>& mCapabilities;
305
Dan Stoza651bf312015-10-23 17:03:17 -0700306 hwc2_display_t mId;
307 bool mIsConnected;
Chris Forbes016d73c2017-04-11 10:04:31 -0700308 DisplayType mType;
David Sodman44b5de02018-08-21 16:28:53 -0700309 Device::FrequencyScaler mFrequencyScaler;
Steven Thomas94e35b92017-07-26 18:48:28 -0700310 std::unordered_map<hwc2_layer_t, std::unique_ptr<Layer>> mLayers;
Lloyd Pique3c085a02018-05-09 19:38:32 -0700311 std::unordered_map<hwc2_config_t, std::shared_ptr<const Config>> mConfigs;
Dan Stoza651bf312015-10-23 17:03:17 -0700312};
313
Fabien Sanglard33960702016-11-29 17:58:21 -0800314// Convenience C++ class to access hwc2_device_t Layer functions directly.
Dan Stoza651bf312015-10-23 17:03:17 -0700315class Layer
316{
317public:
Steven Thomas94e35b92017-07-26 18:48:28 -0700318 Layer(android::Hwc2::Composer& composer,
319 const std::unordered_set<Capability>& capabilities,
320 hwc2_display_t displayId, hwc2_layer_t layerId);
Dan Stoza651bf312015-10-23 17:03:17 -0700321 ~Layer();
322
Dan Stoza651bf312015-10-23 17:03:17 -0700323 hwc2_layer_t getId() const { return mId; }
324
Steven Thomas94e35b92017-07-26 18:48:28 -0700325 // Register a listener to be notified when the layer is destroyed. When the
326 // listener function is called, the Layer will be in the process of being
327 // destroyed, so it's not safe to call methods on it.
328 void setLayerDestroyedListener(std::function<void(Layer*)> listener);
329
Dan Stoza651bf312015-10-23 17:03:17 -0700330 [[clang::warn_unused_result]] Error setCursorPosition(int32_t x, int32_t y);
Chia-I Wu06d63de2017-01-04 14:58:51 +0800331 [[clang::warn_unused_result]] Error setBuffer(uint32_t slot,
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400332 const android::sp<android::GraphicBuffer>& buffer,
Dan Stoza651bf312015-10-23 17:03:17 -0700333 const android::sp<android::Fence>& acquireFence);
334 [[clang::warn_unused_result]] Error setSurfaceDamage(
335 const android::Region& damage);
336
337 [[clang::warn_unused_result]] Error setBlendMode(BlendMode mode);
338 [[clang::warn_unused_result]] Error setColor(hwc_color_t color);
339 [[clang::warn_unused_result]] Error setCompositionType(Composition type);
Dan Stoza5df2a862016-03-24 16:19:37 -0700340 [[clang::warn_unused_result]] Error setDataspace(
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700341 android::ui::Dataspace dataspace);
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700342 [[clang::warn_unused_result]] Error setPerFrameMetadata(
343 const int32_t supportedPerFrameMetadata,
344 const android::HdrMetadata& metadata);
Dan Stoza651bf312015-10-23 17:03:17 -0700345 [[clang::warn_unused_result]] Error setDisplayFrame(
346 const android::Rect& frame);
347 [[clang::warn_unused_result]] Error setPlaneAlpha(float alpha);
348 [[clang::warn_unused_result]] Error setSidebandStream(
349 const native_handle_t* stream);
350 [[clang::warn_unused_result]] Error setSourceCrop(
Dan Stoza5a423ea2017-02-16 14:10:39 -0800351 const android::FloatRect& crop);
Dan Stoza651bf312015-10-23 17:03:17 -0700352 [[clang::warn_unused_result]] Error setTransform(Transform transform);
353 [[clang::warn_unused_result]] Error setVisibleRegion(
354 const android::Region& region);
355 [[clang::warn_unused_result]] Error setZOrder(uint32_t z);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500356 [[clang::warn_unused_result]] Error setInfo(uint32_t type, uint32_t appId);
Dan Stoza651bf312015-10-23 17:03:17 -0700357
358private:
Steven Thomas94e35b92017-07-26 18:48:28 -0700359 // These are references to data owned by HWC2::Device, which will outlive
360 // this HWC2::Layer, so these references are guaranteed to be valid for
361 // the lifetime of this object.
362 android::Hwc2::Composer& mComposer;
363 const std::unordered_set<Capability>& mCapabilities;
364
Dan Stoza651bf312015-10-23 17:03:17 -0700365 hwc2_display_t mDisplayId;
Dan Stoza651bf312015-10-23 17:03:17 -0700366 hwc2_layer_t mId;
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700367 android::ui::Dataspace mDataSpace = android::ui::Dataspace::UNKNOWN;
Courtney Goeltzenleuchterf9c98e52018-02-12 07:23:17 -0700368 android::HdrMetadata mHdrMetadata;
Steven Thomas94e35b92017-07-26 18:48:28 -0700369 std::function<void(Layer*)> mLayerDestroyedListener;
Dan Stoza651bf312015-10-23 17:03:17 -0700370};
371
372} // namespace HWC2
373
374#endif // ANDROID_SF_HWC2_H