blob: 5aa7185507658c44e5e73e884347543a50da9af6 [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
Dan Stoza9e56aa02015-11-02 13:00:03 -080020#include "HWC2.h"
21
Mathias Agopiana350ff92010-08-10 17:14:02 -070022#include <stdint.h>
23#include <sys/types.h>
24
Jamie Gennis2ec3e072012-11-11 16:24:33 -080025#include <ui/Fence.h>
Peiyong Linfd997e02018-03-28 15:29:00 -070026#include <ui/GraphicTypes.h>
Jamie Gennis2ec3e072012-11-11 16:24:33 -080027#include <utils/BitSet.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070028#include <utils/Condition.h>
29#include <utils/Mutex.h>
Mathias Agopianc7d14e22011-08-01 16:32:21 -070030#include <utils/StrongPointer.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070031#include <utils/Thread.h>
32#include <utils/Timers.h>
Mathias Agopian22da60c2011-09-09 00:49:11 -070033#include <utils/Vector.h>
Mathias Agopianc7d14e22011-08-01 16:32:21 -070034
Dan Stoza9e56aa02015-11-02 13:00:03 -080035#include <memory>
Steven Thomas6e8f7062017-11-22 14:15:29 -080036#include <optional>
Dan Stoza9e56aa02015-11-02 13:00:03 -080037#include <set>
38#include <vector>
39
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070040#include "DisplayIdentification.h"
41
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070042extern "C" int clock_nanosleep(clockid_t clock_id, int flags,
43 const struct timespec *request,
44 struct timespec *remain);
45
Mathias Agopian8b736f12012-08-13 17:54:26 -070046struct framebuffer_device_t;
Mathias Agopian3e8b8532012-05-13 20:42:01 -070047
Dan Stoza9e56aa02015-11-02 13:00:03 -080048namespace HWC2 {
49 class Device;
50 class Display;
51}
52
Mathias Agopiana350ff92010-08-10 17:14:02 -070053namespace android {
54// ---------------------------------------------------------------------------
55
Dan Stoza9e56aa02015-11-02 13:00:03 -080056class DisplayDevice;
Mathias Agopianda27af92012-09-13 18:17:13 -070057class Fence;
Mathias Agopian6b442672013-07-09 21:24:52 -070058class FloatRect;
Jesse Hall399184a2014-03-03 15:42:54 -080059class GraphicBuffer;
60class NativeHandle;
Jamie Gennis1a4d8832012-08-02 20:11:05 -070061class Region;
Mathias Agopian83727852010-09-23 18:13:21 -070062class String8;
Lloyd Piquee39cad22017-12-20 17:01:29 -080063class TestableSurfaceFlinger;
David Sodmanfb95bcc2017-12-22 15:45:30 -080064struct CompositionInfo;
Mathias Agopian83727852010-09-23 18:13:21 -070065
Lloyd Piquea822d522017-12-20 16:42:57 -080066namespace Hwc2 {
67class Composer;
68} // namespace Hwc2
69
Mathias Agopiana350ff92010-08-10 17:14:02 -070070class HWComposer
71{
72public:
Lloyd Piquea822d522017-12-20 16:42:57 -080073 explicit HWComposer(std::unique_ptr<android::Hwc2::Composer> composer);
Mathias Agopian8b736f12012-08-13 17:54:26 -070074
Mathias Agopiana350ff92010-08-10 17:14:02 -070075 ~HWComposer();
76
Steven Thomas94e35b92017-07-26 18:48:28 -070077 void registerCallback(HWC2::ComposerCallback* callback,
78 int32_t sequenceId);
Mathias Agopiana350ff92010-08-10 17:14:02 -070079
Dominik Laskowskia2edf612018-06-01 13:15:16 -070080 bool getDisplayIdentificationData(hwc2_display_t hwcDisplayId, uint8_t* outPort,
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070081 DisplayIdentificationData* outData) const;
82
Dan Stoza9f26a9c2016-06-22 14:51:09 -070083 bool hasCapability(HWC2::Capability capability) const;
84
Dan Stoza9e56aa02015-11-02 13:00:03 -080085 // Attempts to allocate a virtual display. If the virtual display is created
86 // on the HWC device, outId will contain its HWC ID.
87 status_t allocateVirtualDisplay(uint32_t width, uint32_t height,
Peiyong Lin34beb7a2018-03-28 11:57:12 -070088 ui::PixelFormat* format, int32_t* outId);
Mathias Agopiane60b0682012-08-21 23:34:09 -070089
Dan Stoza9e56aa02015-11-02 13:00:03 -080090 // Attempts to create a new layer on this display
Steven Thomas94e35b92017-07-26 18:48:28 -070091 HWC2::Layer* createLayer(int32_t displayId);
92 // Destroy a previously created layer
93 void destroyLayer(int32_t displayId, HWC2::Layer* layer);
Mathias Agopiane60b0682012-08-21 23:34:09 -070094
Mathias Agopiana350ff92010-08-10 17:14:02 -070095 // Asks the HAL what it can do
David Sodmanfb95bcc2017-12-22 15:45:30 -080096 status_t prepare(DisplayDevice& display,
97 std::vector<CompositionInfo>& compositionData);
Mathias Agopiana350ff92010-08-10 17:14:02 -070098
Chia-I Wu06d63de2017-01-04 14:58:51 +080099 status_t setClientTarget(int32_t displayId, uint32_t slot,
100 const sp<Fence>& acquireFence,
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700101 const sp<GraphicBuffer>& target, ui::Dataspace dataspace);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800102
Fabien Sanglarda87aa7b2016-11-30 16:27:22 -0800103 // Present layers to the display and read releaseFences.
104 status_t presentAndGetReleaseFences(int32_t displayId);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700105
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700106 // set power mode
Dan Stoza9e56aa02015-11-02 13:00:03 -0800107 status_t setPowerMode(int32_t displayId, int mode);
Colin Cross10fbdb62012-07-12 17:56:34 -0700108
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700109 // set active config
Dan Stoza9e56aa02015-11-02 13:00:03 -0800110 status_t setActiveConfig(int32_t displayId, size_t configId);
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700111
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700112 // Sets a color transform to be applied to the result of composition
113 status_t setColorTransform(int32_t displayId, const mat4& transform);
114
Andy McFadden27ec5732012-10-02 19:04:45 -0700115 // reset state when an external, non-virtual display is disconnected
Dan Stoza9e56aa02015-11-02 13:00:03 -0800116 void disconnectDisplay(int32_t displayId);
Mathias Agopianda27af92012-09-13 18:17:13 -0700117
Mathias Agopiane60b0682012-08-21 23:34:09 -0700118 // does this display have layers handled by HWC
Dan Stoza9e56aa02015-11-02 13:00:03 -0800119 bool hasDeviceComposition(int32_t displayId) const;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700120
Madhuri Athota88a905b2017-05-04 16:58:15 +0530121 // does this display have pending request to flip client target
122 bool hasFlipClientTargetRequest(int32_t displayId) const;
123
Mathias Agopiane60b0682012-08-21 23:34:09 -0700124 // does this display have layers handled by GLES
Dan Stoza9e56aa02015-11-02 13:00:03 -0800125 bool hasClientComposition(int32_t displayId) const;
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700126
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800127 // get the present fence received from the last call to present.
128 sp<Fence> getPresentFence(int32_t displayId) const;
Mathias Agopianda27af92012-09-13 18:17:13 -0700129
Dan Stoza9e56aa02015-11-02 13:00:03 -0800130 // Get last release fence for the given layer
131 sp<Fence> getLayerReleaseFence(int32_t displayId,
Steven Thomas94e35b92017-07-26 18:48:28 -0700132 HWC2::Layer* layer) const;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700133
Jesse Hall851cfe82013-03-20 13:44:00 -0700134 // Set the output buffer and acquire fence for a virtual display.
Dan Stoza9e56aa02015-11-02 13:00:03 -0800135 // Returns INVALID_OPERATION if displayId is not a virtual display.
136 status_t setOutputBuffer(int32_t displayId, const sp<Fence>& acquireFence,
Jesse Hall851cfe82013-03-20 13:44:00 -0700137 const sp<GraphicBuffer>& buf);
138
Dan Stoza9e56aa02015-11-02 13:00:03 -0800139 // After SurfaceFlinger has retrieved the release fences for all the frames,
140 // it can call this to clear the shared pointers in the release fence map
141 void clearReleaseFences(int32_t displayId);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700142
Peiyong Lin62665892018-04-16 11:07:44 -0700143 // Fetches the HDR capabilities of the given display
144 status_t getHdrCapabilities(int32_t displayId, HdrCapabilities* outCapabilities);
Dan Stozac4f471e2016-03-24 09:31:08 -0700145
Peiyong Lin0ac5f4e2018-04-19 22:06:34 -0700146 int32_t getSupportedPerFrameMetadata(int32_t displayId) const;
147
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700148 // Returns the available RenderIntent of the given display.
149 std::vector<ui::RenderIntent> getRenderIntents(int32_t displayId, ui::ColorMode colorMode) const;
150
151 mat4 getDataspaceSaturationMatrix(int32_t displayId, ui::Dataspace dataspace);
152
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700153 // Events handling ---------------------------------------------------------
154
Steven Thomas94e35b92017-07-26 18:48:28 -0700155 // Returns true if successful, false otherwise. The
156 // DisplayDevice::DisplayType of the display is returned as an output param.
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700157 bool onVsync(hwc2_display_t hwcDisplayId, int64_t timestamp, int32_t* outDisplay);
158 std::optional<DisplayId> onHotplug(hwc2_display_t hwcDisplayId, int32_t displayType,
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700159 HWC2::Connection connection);
Steven Thomas94e35b92017-07-26 18:48:28 -0700160
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800161 void setVsyncEnabled(int32_t displayId, HWC2::Vsync enabled);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700162
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700163 // Query display parameters. Pass in a display index (e.g.
164 // HWC_DISPLAY_PRIMARY).
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800165 nsecs_t getRefreshTimestamp(int32_t displayId) const;
166 bool isConnected(int32_t displayId) const;
Dan Stoza7f7da322014-05-02 15:26:25 -0700167
Dan Stoza9e56aa02015-11-02 13:00:03 -0800168 // Non-const because it can update configMap inside of mDisplayData
169 std::vector<std::shared_ptr<const HWC2::Display::Config>>
170 getConfigs(int32_t displayId) const;
Dan Stoza7f7da322014-05-02 15:26:25 -0700171
Dan Stoza9e56aa02015-11-02 13:00:03 -0800172 std::shared_ptr<const HWC2::Display::Config>
173 getActiveConfig(int32_t displayId) const;
Lloyd Pique3c085a02018-05-09 19:38:32 -0700174 int getActiveConfigIndex(int32_t displayId) const;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700175
Peiyong Linfd997e02018-03-28 15:29:00 -0700176 std::vector<ui::ColorMode> getColorModes(int32_t displayId) const;
Michael Wright28f24d02016-07-12 13:30:53 -0700177
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700178 status_t setActiveColorMode(int32_t displayId, ui::ColorMode mode,
179 ui::RenderIntent renderIntent);
Courtney Goeltzenleuchterfad9d8c2016-06-23 11:49:50 -0600180
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800181 bool isUsingVrComposer() const;
182
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700183 // for debugging ----------------------------------------------------------
Mathias Agopian74d211a2013-04-22 16:55:35 +0200184 void dump(String8& out) const;
Mathias Agopian83727852010-09-23 18:13:21 -0700185
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800186 android::Hwc2::Composer* getComposer() const { return mHwcDevice->getComposer(); }
Steven Thomas6e8f7062017-11-22 14:15:29 -0800187
188 std::optional<hwc2_display_t> getHwcDisplayId(int32_t displayId) const;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700189private:
Lloyd Piquee39cad22017-12-20 17:01:29 -0800190 // For unit tests
191 friend TestableSurfaceFlinger;
192
Dan Stoza9e56aa02015-11-02 13:00:03 -0800193 bool isValidDisplay(int32_t displayId) const;
194 static void validateChange(HWC2::Composition from, HWC2::Composition to);
Mathias Agopian31d28432012-04-03 16:31:39 -0700195
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700196 struct cb_context;
Mathias Agopian31d28432012-04-03 16:31:39 -0700197
Mathias Agopiane60b0682012-08-21 23:34:09 -0700198 struct DisplayData {
Dominik Laskowskic1f18f62018-06-13 15:17:55 -0700199 bool isVirtual = false;
Dominik Laskowskif9750f22018-06-06 12:24:53 -0700200 bool hasClientComposition = false;
201 bool hasDeviceComposition = false;
202 HWC2::Display* hwcDisplay = nullptr;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800203 HWC2::DisplayRequest displayRequests;
Dominik Laskowskif9750f22018-06-06 12:24:53 -0700204 sp<Fence> lastPresentFence = Fence::NO_FENCE; // signals when the last set op retires
Steven Thomas94e35b92017-07-26 18:48:28 -0700205 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Dominik Laskowskif9750f22018-06-06 12:24:53 -0700206 buffer_handle_t outbufHandle = nullptr;
207 sp<Fence> outbufAcquireFence = Fence::NO_FENCE;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800208 mutable std::unordered_map<int32_t,
209 std::shared_ptr<const HWC2::Display::Config>> configMap;
Jamie Gennis2ec3e072012-11-11 16:24:33 -0800210
Dan Stoza9e56aa02015-11-02 13:00:03 -0800211 // protected by mVsyncLock
Dominik Laskowskif9750f22018-06-06 12:24:53 -0700212 HWC2::Vsync vsyncEnabled = HWC2::Vsync::Disable;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700213
214 bool validateWasSkipped;
215 HWC2::Error presentError;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700216 };
217
Lloyd Piquea822d522017-12-20 16:42:57 -0800218 std::vector<DisplayData> mDisplayData{HWC_NUM_PHYSICAL_DISPLAY_TYPES};
Dominik Laskowskib04f98a2018-11-07 21:07:16 -0800219
220 // This must be destroyed before mDisplayData, because destructor may call back into HWComposer
221 // and look up DisplayData.
222 std::unique_ptr<HWC2::Device> mHwcDevice;
223
224 std::set<size_t> mFreeDisplaySlots;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800225 std::unordered_map<hwc2_display_t, int32_t> mHwcDisplaySlots;
Manoj Kumar AVMe04e4ed2015-06-11 14:18:14 -0700226 // protect mDisplayData from races between prepare and dump
227 mutable Mutex mDisplayLock;
Mathias Agopian1e260872012-08-08 18:35:12 -0700228
Lloyd Piquea822d522017-12-20 16:42:57 -0800229 cb_context* mCBContext = nullptr;
230 size_t mVSyncCounts[HWC_NUM_PHYSICAL_DISPLAY_TYPES]{0, 0};
231 uint32_t mRemainingHwcVirtualDisplays{mHwcDevice->getMaxVirtualDisplayCount()};
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700232
233 // protected by mLock
234 mutable Mutex mLock;
Lloyd Piquea822d522017-12-20 16:42:57 -0800235 mutable std::unordered_map<int32_t, nsecs_t> mLastHwVSync{
236 {{HWC_DISPLAY_PRIMARY, 0}, {HWC_DISPLAY_EXTERNAL, 0}}};
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700237
238 // thread-safe
Dan Stoza9e56aa02015-11-02 13:00:03 -0800239 mutable Mutex mVsyncLock;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700240};
241
Mathias Agopiana350ff92010-08-10 17:14:02 -0700242// ---------------------------------------------------------------------------
243}; // namespace android
244
245#endif // ANDROID_SF_HWCOMPOSER_H