blob: aaa6e8c2553637a765256c76db642b8ef2e65048 [file] [log] [blame]
Mathias Agopiana350ff92010-08-10 17:14:02 -07001/*
2 * Copyright (C) 2010 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_HWCOMPOSER_H
18#define ANDROID_SF_HWCOMPOSER_H
19
Dominik Laskowski1af47932018-11-12 10:20:46 -080020#include <cstdint>
Dan Stoza9e56aa02015-11-02 13:00:03 -080021#include <memory>
Dominik Laskowski1af47932018-11-12 10:20:46 -080022#include <mutex>
Steven Thomas6e8f7062017-11-22 14:15:29 -080023#include <optional>
Dominik Laskowski075d3172018-05-24 15:50:06 -070024#include <unordered_map>
25#include <unordered_set>
Dan Stoza9e56aa02015-11-02 13:00:03 -080026#include <vector>
27
Dominik Laskowski1af47932018-11-12 10:20:46 -080028#include <android-base/thread_annotations.h>
29#include <ui/Fence.h>
30#include <ui/GraphicTypes.h>
31#include <utils/StrongPointer.h>
32#include <utils/Timers.h>
33
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070034#include "DisplayIdentification.h"
Dominik Laskowski1af47932018-11-12 10:20:46 -080035#include "HWC2.h"
Dan Stoza9e56aa02015-11-02 13:00:03 -080036
Mathias Agopiana350ff92010-08-10 17:14:02 -070037namespace android {
Mathias Agopiana350ff92010-08-10 17:14:02 -070038
Jesse Hall399184a2014-03-03 15:42:54 -080039class GraphicBuffer;
Lloyd Piquee39cad22017-12-20 17:01:29 -080040class TestableSurfaceFlinger;
David Sodmanfb95bcc2017-12-22 15:45:30 -080041struct CompositionInfo;
Mathias Agopian83727852010-09-23 18:13:21 -070042
Lloyd Piquea822d522017-12-20 16:42:57 -080043namespace Hwc2 {
44class Composer;
45} // namespace Hwc2
46
Mathias Agopiana350ff92010-08-10 17:14:02 -070047class HWComposer
48{
49public:
Dominik Laskowski1af47932018-11-12 10:20:46 -080050 explicit HWComposer(std::unique_ptr<Hwc2::Composer> composer);
Mathias Agopian8b736f12012-08-13 17:54:26 -070051
Mathias Agopiana350ff92010-08-10 17:14:02 -070052 ~HWComposer();
53
Steven Thomas94e35b92017-07-26 18:48:28 -070054 void registerCallback(HWC2::ComposerCallback* callback,
55 int32_t sequenceId);
Mathias Agopiana350ff92010-08-10 17:14:02 -070056
Dominik Laskowskia2edf612018-06-01 13:15:16 -070057 bool getDisplayIdentificationData(hwc2_display_t hwcDisplayId, uint8_t* outPort,
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070058 DisplayIdentificationData* outData) const;
59
Dan Stoza9f26a9c2016-06-22 14:51:09 -070060 bool hasCapability(HWC2::Capability capability) const;
Peiyong Lined531a32018-10-26 18:27:56 -070061 bool hasDisplayCapability(const std::optional<DisplayId>& displayId,
62 HWC2::DisplayCapability capability) const;
Dan Stoza9f26a9c2016-06-22 14:51:09 -070063
Dominik Laskowski075d3172018-05-24 15:50:06 -070064 // Attempts to allocate a virtual display and returns its ID if created on the HWC device.
65 std::optional<DisplayId> allocateVirtualDisplay(uint32_t width, uint32_t height,
66 ui::PixelFormat* format);
Mathias Agopiane60b0682012-08-21 23:34:09 -070067
Dan Stoza9e56aa02015-11-02 13:00:03 -080068 // Attempts to create a new layer on this display
Dominik Laskowski075d3172018-05-24 15:50:06 -070069 HWC2::Layer* createLayer(DisplayId displayId);
Steven Thomas94e35b92017-07-26 18:48:28 -070070 // Destroy a previously created layer
Dominik Laskowski075d3172018-05-24 15:50:06 -070071 void destroyLayer(DisplayId displayId, HWC2::Layer* layer);
Mathias Agopiane60b0682012-08-21 23:34:09 -070072
Mathias Agopiana350ff92010-08-10 17:14:02 -070073 // Asks the HAL what it can do
Dominik Laskowski075d3172018-05-24 15:50:06 -070074 status_t prepare(DisplayId displayId, std::vector<CompositionInfo>& compositionData);
Mathias Agopiana350ff92010-08-10 17:14:02 -070075
Dominik Laskowski075d3172018-05-24 15:50:06 -070076 status_t setClientTarget(DisplayId displayId, uint32_t slot, const sp<Fence>& acquireFence,
77 const sp<GraphicBuffer>& target, ui::Dataspace dataspace);
Dan Stoza9e56aa02015-11-02 13:00:03 -080078
Fabien Sanglarda87aa7b2016-11-30 16:27:22 -080079 // Present layers to the display and read releaseFences.
Dominik Laskowski075d3172018-05-24 15:50:06 -070080 status_t presentAndGetReleaseFences(DisplayId displayId);
Mathias Agopiana350ff92010-08-10 17:14:02 -070081
Prashant Malani2c9b11f2014-05-25 01:36:31 -070082 // set power mode
Dominik Laskowski075d3172018-05-24 15:50:06 -070083 status_t setPowerMode(DisplayId displayId, int mode);
Colin Cross10fbdb62012-07-12 17:56:34 -070084
Michael Lentine6c9e34a2014-07-14 13:48:55 -070085 // set active config
Dominik Laskowski075d3172018-05-24 15:50:06 -070086 status_t setActiveConfig(DisplayId displayId, size_t configId);
Michael Lentine6c9e34a2014-07-14 13:48:55 -070087
Dan Stoza9f26a9c2016-06-22 14:51:09 -070088 // Sets a color transform to be applied to the result of composition
Dominik Laskowski075d3172018-05-24 15:50:06 -070089 status_t setColorTransform(DisplayId displayId, const mat4& transform);
Dan Stoza9f26a9c2016-06-22 14:51:09 -070090
Andy McFadden27ec5732012-10-02 19:04:45 -070091 // reset state when an external, non-virtual display is disconnected
Dominik Laskowski075d3172018-05-24 15:50:06 -070092 void disconnectDisplay(DisplayId displayId);
Mathias Agopianda27af92012-09-13 18:17:13 -070093
Mathias Agopiane60b0682012-08-21 23:34:09 -070094 // does this display have layers handled by HWC
Dominik Laskowski075d3172018-05-24 15:50:06 -070095 bool hasDeviceComposition(const std::optional<DisplayId>& displayId) const;
Mathias Agopiane60b0682012-08-21 23:34:09 -070096
Madhuri Athota88a905b2017-05-04 16:58:15 +053097 // does this display have pending request to flip client target
Dominik Laskowski075d3172018-05-24 15:50:06 -070098 bool hasFlipClientTargetRequest(const std::optional<DisplayId>& displayId) const;
Madhuri Athota88a905b2017-05-04 16:58:15 +053099
Mathias Agopiane60b0682012-08-21 23:34:09 -0700100 // does this display have layers handled by GLES
Dominik Laskowski075d3172018-05-24 15:50:06 -0700101 bool hasClientComposition(const std::optional<DisplayId>& displayId) const;
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700102
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800103 // get the present fence received from the last call to present.
Dominik Laskowski075d3172018-05-24 15:50:06 -0700104 sp<Fence> getPresentFence(DisplayId displayId) const;
Mathias Agopianda27af92012-09-13 18:17:13 -0700105
Dan Stoza9e56aa02015-11-02 13:00:03 -0800106 // Get last release fence for the given layer
Dominik Laskowski075d3172018-05-24 15:50:06 -0700107 sp<Fence> getLayerReleaseFence(DisplayId displayId, HWC2::Layer* layer) const;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700108
Jesse Hall851cfe82013-03-20 13:44:00 -0700109 // Set the output buffer and acquire fence for a virtual display.
Dan Stoza9e56aa02015-11-02 13:00:03 -0800110 // Returns INVALID_OPERATION if displayId is not a virtual display.
Dominik Laskowski075d3172018-05-24 15:50:06 -0700111 status_t setOutputBuffer(DisplayId displayId, const sp<Fence>& acquireFence,
112 const sp<GraphicBuffer>& buffer);
Jesse Hall851cfe82013-03-20 13:44:00 -0700113
Dan Stoza9e56aa02015-11-02 13:00:03 -0800114 // After SurfaceFlinger has retrieved the release fences for all the frames,
115 // it can call this to clear the shared pointers in the release fence map
Dominik Laskowski075d3172018-05-24 15:50:06 -0700116 void clearReleaseFences(DisplayId displayId);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700117
Peiyong Lin62665892018-04-16 11:07:44 -0700118 // Fetches the HDR capabilities of the given display
Dominik Laskowski075d3172018-05-24 15:50:06 -0700119 status_t getHdrCapabilities(DisplayId displayId, HdrCapabilities* outCapabilities);
Dan Stozac4f471e2016-03-24 09:31:08 -0700120
Dominik Laskowski075d3172018-05-24 15:50:06 -0700121 int32_t getSupportedPerFrameMetadata(DisplayId displayId) const;
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700122
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700123 // Returns the available RenderIntent of the given display.
Dominik Laskowski075d3172018-05-24 15:50:06 -0700124 std::vector<ui::RenderIntent> getRenderIntents(DisplayId displayId,
125 ui::ColorMode colorMode) const;
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700126
Dominik Laskowski075d3172018-05-24 15:50:06 -0700127 mat4 getDataspaceSaturationMatrix(DisplayId displayId, ui::Dataspace dataspace);
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700128
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700129 // Returns the attributes of the color sampling engine.
130 status_t getDisplayedContentSamplingAttributes(DisplayId displayId, ui::PixelFormat* outFormat,
131 ui::Dataspace* outDataspace,
132 uint8_t* outComponentMask);
Kevin DuBois74e53772018-11-19 10:52:38 -0800133 status_t setDisplayContentSamplingEnabled(DisplayId displayId, bool enabled,
134 uint8_t componentMask, uint64_t maxFrames);
Kevin DuBois9c0a1762018-10-16 13:32:31 -0700135
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700136 // Events handling ---------------------------------------------------------
137
Dominik Laskowski075d3172018-05-24 15:50:06 -0700138 // Returns stable display ID (and display name on connection of new or previously disconnected
139 // display), or std::nullopt if hotplug event was ignored.
140 std::optional<DisplayIdentificationInfo> onHotplug(hwc2_display_t hwcDisplayId,
141 HWC2::Connection connection);
Steven Thomas94e35b92017-07-26 18:48:28 -0700142
Dominik Laskowski075d3172018-05-24 15:50:06 -0700143 bool onVsync(hwc2_display_t hwcDisplayId, int64_t timestamp);
144 void setVsyncEnabled(DisplayId displayId, HWC2::Vsync enabled);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700145
Dominik Laskowski075d3172018-05-24 15:50:06 -0700146 nsecs_t getRefreshTimestamp(DisplayId displayId) const;
147 bool isConnected(DisplayId displayId) const;
Dan Stoza7f7da322014-05-02 15:26:25 -0700148
Dan Stoza9e56aa02015-11-02 13:00:03 -0800149 // Non-const because it can update configMap inside of mDisplayData
Dominik Laskowski075d3172018-05-24 15:50:06 -0700150 std::vector<std::shared_ptr<const HWC2::Display::Config>> getConfigs(DisplayId displayId) const;
Dan Stoza7f7da322014-05-02 15:26:25 -0700151
Dominik Laskowski075d3172018-05-24 15:50:06 -0700152 std::shared_ptr<const HWC2::Display::Config> getActiveConfig(DisplayId displayId) const;
153 int getActiveConfigIndex(DisplayId displayId) const;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700154
Dominik Laskowski075d3172018-05-24 15:50:06 -0700155 std::vector<ui::ColorMode> getColorModes(DisplayId displayId) const;
Michael Wright28f24d02016-07-12 13:30:53 -0700156
Dominik Laskowski075d3172018-05-24 15:50:06 -0700157 status_t setActiveColorMode(DisplayId displayId, ui::ColorMode mode,
158 ui::RenderIntent renderIntent);
Courtney Goeltzenleuchterfad9d8c2016-06-23 11:49:50 -0600159
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800160 bool isUsingVrComposer() const;
161
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700162 // for debugging ----------------------------------------------------------
Yiwei Zhang5434a782018-12-05 18:06:32 -0800163 void dump(std::string& out) const;
Mathias Agopian83727852010-09-23 18:13:21 -0700164
Dominik Laskowski1af47932018-11-12 10:20:46 -0800165 Hwc2::Composer* getComposer() const { return mHwcDevice->getComposer(); }
Steven Thomas6e8f7062017-11-22 14:15:29 -0800166
Dominik Laskowski075d3172018-05-24 15:50:06 -0700167 // TODO(b/74619554): Remove special cases for internal/external display.
168 std::optional<hwc2_display_t> getInternalHwcDisplayId() const { return mInternalHwcDisplayId; }
169 std::optional<hwc2_display_t> getExternalHwcDisplayId() const { return mExternalHwcDisplayId; }
170
171 std::optional<DisplayId> toPhysicalDisplayId(hwc2_display_t hwcDisplayId) const;
172 std::optional<hwc2_display_t> fromPhysicalDisplayId(DisplayId displayId) const;
173
Mathias Agopiana350ff92010-08-10 17:14:02 -0700174private:
Lloyd Piquee39cad22017-12-20 17:01:29 -0800175 // For unit tests
176 friend TestableSurfaceFlinger;
177
Dominik Laskowski075d3172018-05-24 15:50:06 -0700178 std::optional<DisplayIdentificationInfo> onHotplugConnect(hwc2_display_t hwcDisplayId);
179
Dan Stoza9e56aa02015-11-02 13:00:03 -0800180 static void validateChange(HWC2::Composition from, HWC2::Composition to);
Mathias Agopian31d28432012-04-03 16:31:39 -0700181
Mathias Agopiane60b0682012-08-21 23:34:09 -0700182 struct DisplayData {
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700183 bool isVirtual = false;
Dominik Laskowskif9750f22018-06-06 12:24:53 -0700184 bool hasClientComposition = false;
185 bool hasDeviceComposition = false;
186 HWC2::Display* hwcDisplay = nullptr;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800187 HWC2::DisplayRequest displayRequests;
Dominik Laskowskif9750f22018-06-06 12:24:53 -0700188 sp<Fence> lastPresentFence = Fence::NO_FENCE; // signals when the last set op retires
Steven Thomas94e35b92017-07-26 18:48:28 -0700189 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Dominik Laskowskif9750f22018-06-06 12:24:53 -0700190 buffer_handle_t outbufHandle = nullptr;
191 sp<Fence> outbufAcquireFence = Fence::NO_FENCE;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800192 mutable std::unordered_map<int32_t,
193 std::shared_ptr<const HWC2::Display::Config>> configMap;
Jamie Gennis2ec3e072012-11-11 16:24:33 -0800194
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700195 bool validateWasSkipped;
196 HWC2::Error presentError;
Dominik Laskowski1af47932018-11-12 10:20:46 -0800197
198 bool vsyncTraceToggle = false;
199
200 std::mutex vsyncEnabledLock;
201 HWC2::Vsync vsyncEnabled GUARDED_BY(vsyncEnabledLock) = HWC2::Vsync::Disable;
202
203 mutable std::mutex lastHwVsyncLock;
204 nsecs_t lastHwVsync GUARDED_BY(lastHwVsyncLock) = 0;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700205 };
206
Dominik Laskowski075d3172018-05-24 15:50:06 -0700207 std::unordered_map<DisplayId, DisplayData> mDisplayData;
Dominik Laskowskib04f98a2018-11-07 21:07:16 -0800208
209 // This must be destroyed before mDisplayData, because destructor may call back into HWComposer
210 // and look up DisplayData.
211 std::unique_ptr<HWC2::Device> mHwcDevice;
212
Dominik Laskowski075d3172018-05-24 15:50:06 -0700213 std::unordered_map<hwc2_display_t, DisplayId> mPhysicalDisplayIdMap;
214 std::optional<hwc2_display_t> mInternalHwcDisplayId;
215 std::optional<hwc2_display_t> mExternalHwcDisplayId;
216 bool mHasMultiDisplaySupport = false;
217
Dominik Laskowski34157762018-10-31 13:07:19 -0700218 std::unordered_set<DisplayId> mFreeVirtualDisplayIds;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700219 uint32_t mNextVirtualDisplayId = 0;
Lloyd Piquea822d522017-12-20 16:42:57 -0800220 uint32_t mRemainingHwcVirtualDisplays{mHwcDevice->getMaxVirtualDisplayCount()};
Mathias Agopiana350ff92010-08-10 17:14:02 -0700221};
222
Dominik Laskowski1af47932018-11-12 10:20:46 -0800223} // namespace android
Mathias Agopiana350ff92010-08-10 17:14:02 -0700224
225#endif // ANDROID_SF_HWCOMPOSER_H