blob: 3b6d931b5ca13cc5a12fe2feed55572192ead95b [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>
26
27#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>
36#include <set>
37#include <vector>
38
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070039extern "C" int clock_nanosleep(clockid_t clock_id, int flags,
40 const struct timespec *request,
41 struct timespec *remain);
42
Mathias Agopian8b736f12012-08-13 17:54:26 -070043struct framebuffer_device_t;
Mathias Agopian3e8b8532012-05-13 20:42:01 -070044
Dan Stoza9e56aa02015-11-02 13:00:03 -080045namespace HWC2 {
46 class Device;
47 class Display;
48}
49
Mathias Agopiana350ff92010-08-10 17:14:02 -070050namespace android {
51// ---------------------------------------------------------------------------
52
Dan Stoza9e56aa02015-11-02 13:00:03 -080053class DisplayDevice;
Mathias Agopianda27af92012-09-13 18:17:13 -070054class Fence;
Mathias Agopian6b442672013-07-09 21:24:52 -070055class FloatRect;
Jesse Hall399184a2014-03-03 15:42:54 -080056class GraphicBuffer;
57class NativeHandle;
Jamie Gennis1a4d8832012-08-02 20:11:05 -070058class Region;
Mathias Agopian83727852010-09-23 18:13:21 -070059class String8;
60
Mathias Agopiana350ff92010-08-10 17:14:02 -070061class HWComposer
62{
63public:
Kalle Raitaa099a242017-01-11 11:17:29 -080064 // Uses the named composer service. Valid choices for normal use
65 // are 'default' and 'vr'.
66 HWComposer(const std::string& serviceName);
Mathias Agopian8b736f12012-08-13 17:54:26 -070067
Mathias Agopiana350ff92010-08-10 17:14:02 -070068 ~HWComposer();
69
Steven Thomas94e35b92017-07-26 18:48:28 -070070 void registerCallback(HWC2::ComposerCallback* callback,
71 int32_t sequenceId);
Mathias Agopiana350ff92010-08-10 17:14:02 -070072
Dan Stoza9f26a9c2016-06-22 14:51:09 -070073 bool hasCapability(HWC2::Capability capability) const;
74
Dan Stoza9e56aa02015-11-02 13:00:03 -080075 // Attempts to allocate a virtual display. If the virtual display is created
76 // on the HWC device, outId will contain its HWC ID.
77 status_t allocateVirtualDisplay(uint32_t width, uint32_t height,
Dan Stoza5cf424b2016-05-20 14:02:39 -070078 android_pixel_format_t* format, int32_t* outId);
Mathias Agopiane60b0682012-08-21 23:34:09 -070079
Dan Stoza9e56aa02015-11-02 13:00:03 -080080 // Attempts to create a new layer on this display
Steven Thomas94e35b92017-07-26 18:48:28 -070081 HWC2::Layer* createLayer(int32_t displayId);
82 // Destroy a previously created layer
83 void destroyLayer(int32_t displayId, HWC2::Layer* layer);
Mathias Agopiane60b0682012-08-21 23:34:09 -070084
Mathias Agopiana350ff92010-08-10 17:14:02 -070085 // Asks the HAL what it can do
Dan Stoza9e56aa02015-11-02 13:00:03 -080086 status_t prepare(DisplayDevice& displayDevice);
Mathias Agopiana350ff92010-08-10 17:14:02 -070087
Chia-I Wu06d63de2017-01-04 14:58:51 +080088 status_t setClientTarget(int32_t displayId, uint32_t slot,
89 const sp<Fence>& acquireFence,
Dan Stoza9e56aa02015-11-02 13:00:03 -080090 const sp<GraphicBuffer>& target, android_dataspace_t dataspace);
91
Fabien Sanglarda87aa7b2016-11-30 16:27:22 -080092 // Present layers to the display and read releaseFences.
93 status_t presentAndGetReleaseFences(int32_t displayId);
Mathias Agopiana350ff92010-08-10 17:14:02 -070094
Prashant Malani2c9b11f2014-05-25 01:36:31 -070095 // set power mode
Dan Stoza9e56aa02015-11-02 13:00:03 -080096 status_t setPowerMode(int32_t displayId, int mode);
Colin Cross10fbdb62012-07-12 17:56:34 -070097
Michael Lentine6c9e34a2014-07-14 13:48:55 -070098 // set active config
Dan Stoza9e56aa02015-11-02 13:00:03 -080099 status_t setActiveConfig(int32_t displayId, size_t configId);
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700100
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700101 // Sets a color transform to be applied to the result of composition
102 status_t setColorTransform(int32_t displayId, const mat4& transform);
103
Andy McFadden27ec5732012-10-02 19:04:45 -0700104 // reset state when an external, non-virtual display is disconnected
Dan Stoza9e56aa02015-11-02 13:00:03 -0800105 void disconnectDisplay(int32_t displayId);
Mathias Agopianda27af92012-09-13 18:17:13 -0700106
Mathias Agopiane60b0682012-08-21 23:34:09 -0700107 // does this display have layers handled by HWC
Dan Stoza9e56aa02015-11-02 13:00:03 -0800108 bool hasDeviceComposition(int32_t displayId) const;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700109
110 // does this display have layers handled by GLES
Dan Stoza9e56aa02015-11-02 13:00:03 -0800111 bool hasClientComposition(int32_t displayId) const;
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700112
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800113 // get the present fence received from the last call to present.
114 sp<Fence> getPresentFence(int32_t displayId) const;
Mathias Agopianda27af92012-09-13 18:17:13 -0700115
Dan Stoza9e56aa02015-11-02 13:00:03 -0800116 // Get last release fence for the given layer
117 sp<Fence> getLayerReleaseFence(int32_t displayId,
Steven Thomas94e35b92017-07-26 18:48:28 -0700118 HWC2::Layer* layer) const;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700119
Jesse Hall851cfe82013-03-20 13:44:00 -0700120 // Set the output buffer and acquire fence for a virtual display.
Dan Stoza9e56aa02015-11-02 13:00:03 -0800121 // Returns INVALID_OPERATION if displayId is not a virtual display.
122 status_t setOutputBuffer(int32_t displayId, const sp<Fence>& acquireFence,
Jesse Hall851cfe82013-03-20 13:44:00 -0700123 const sp<GraphicBuffer>& buf);
124
Dan Stoza9e56aa02015-11-02 13:00:03 -0800125 // After SurfaceFlinger has retrieved the release fences for all the frames,
126 // it can call this to clear the shared pointers in the release fence map
127 void clearReleaseFences(int32_t displayId);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700128
Dan Stozac4f471e2016-03-24 09:31:08 -0700129 // Returns the HDR capabilities of the given display
130 std::unique_ptr<HdrCapabilities> getHdrCapabilities(int32_t displayId);
131
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700132 // Events handling ---------------------------------------------------------
133
Steven Thomas94e35b92017-07-26 18:48:28 -0700134 // Returns true if successful, false otherwise. The
135 // DisplayDevice::DisplayType of the display is returned as an output param.
136 bool onVsync(hwc2_display_t displayId, int64_t timestamp,
137 int32_t* outDisplay);
138 void onHotplug(hwc2_display_t displayId, HWC2::Connection connection);
139
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800140 void setVsyncEnabled(int32_t displayId, HWC2::Vsync enabled);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700141
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700142 // Query display parameters. Pass in a display index (e.g.
143 // HWC_DISPLAY_PRIMARY).
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800144 nsecs_t getRefreshTimestamp(int32_t displayId) const;
145 bool isConnected(int32_t displayId) const;
Dan Stoza7f7da322014-05-02 15:26:25 -0700146
Dan Stoza9e56aa02015-11-02 13:00:03 -0800147 // Non-const because it can update configMap inside of mDisplayData
148 std::vector<std::shared_ptr<const HWC2::Display::Config>>
149 getConfigs(int32_t displayId) const;
Dan Stoza7f7da322014-05-02 15:26:25 -0700150
Dan Stoza9e56aa02015-11-02 13:00:03 -0800151 std::shared_ptr<const HWC2::Display::Config>
152 getActiveConfig(int32_t displayId) const;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700153
Michael Wright28f24d02016-07-12 13:30:53 -0700154 std::vector<android_color_mode_t> getColorModes(int32_t displayId) const;
155
156 status_t setActiveColorMode(int32_t displayId, android_color_mode_t mode);
Courtney Goeltzenleuchterfad9d8c2016-06-23 11:49:50 -0600157
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800158 bool isUsingVrComposer() const;
159
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700160 // for debugging ----------------------------------------------------------
Mathias Agopian74d211a2013-04-22 16:55:35 +0200161 void dump(String8& out) const;
Mathias Agopian83727852010-09-23 18:13:21 -0700162
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800163 android::Hwc2::Composer* getComposer() const { return mHwcDevice->getComposer(); }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700164private:
Dan Stoza9e56aa02015-11-02 13:00:03 -0800165 static const int32_t VIRTUAL_DISPLAY_ID_BASE = 2;
Jesse Hallb685c542012-07-31 14:32:56 -0700166
Dan Stoza9e56aa02015-11-02 13:00:03 -0800167 bool isValidDisplay(int32_t displayId) const;
168 static void validateChange(HWC2::Composition from, HWC2::Composition to);
Mathias Agopian31d28432012-04-03 16:31:39 -0700169
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700170 struct cb_context;
Mathias Agopian31d28432012-04-03 16:31:39 -0700171
Mathias Agopiane60b0682012-08-21 23:34:09 -0700172 struct DisplayData {
Jesse Halla9a1b002013-02-27 16:39:25 -0800173 DisplayData();
174 ~DisplayData();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800175 void reset();
176
177 bool hasClientComposition;
178 bool hasDeviceComposition;
Steven Thomas94e35b92017-07-26 18:48:28 -0700179 HWC2::Display* hwcDisplay;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800180 HWC2::DisplayRequest displayRequests;
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800181 sp<Fence> lastPresentFence; // signals when the last set op retires
Steven Thomas94e35b92017-07-26 18:48:28 -0700182 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
Jesse Hall851cfe82013-03-20 13:44:00 -0700183 buffer_handle_t outbufHandle;
184 sp<Fence> outbufAcquireFence;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800185 mutable std::unordered_map<int32_t,
186 std::shared_ptr<const HWC2::Display::Config>> configMap;
Jamie Gennis2ec3e072012-11-11 16:24:33 -0800187
Dan Stoza9e56aa02015-11-02 13:00:03 -0800188 // protected by mVsyncLock
189 HWC2::Vsync vsyncEnabled;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700190
191 bool validateWasSkipped;
192 HWC2::Error presentError;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700193 };
194
Dan Stoza9e56aa02015-11-02 13:00:03 -0800195 std::unique_ptr<HWC2::Device> mHwcDevice;
196 std::vector<DisplayData> mDisplayData;
197 std::set<size_t> mFreeDisplaySlots;
198 std::unordered_map<hwc2_display_t, int32_t> mHwcDisplaySlots;
Manoj Kumar AVMe04e4ed2015-06-11 14:18:14 -0700199 // protect mDisplayData from races between prepare and dump
200 mutable Mutex mDisplayLock;
Mathias Agopian1e260872012-08-08 18:35:12 -0700201
Jesse Hall5880cc52012-06-05 23:40:32 -0700202 cb_context* mCBContext;
Mathias Agopianbef42c52013-08-21 17:45:46 -0700203 size_t mVSyncCounts[HWC_NUM_PHYSICAL_DISPLAY_TYPES];
Dan Stoza9e56aa02015-11-02 13:00:03 -0800204 uint32_t mRemainingHwcVirtualDisplays;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700205
206 // protected by mLock
207 mutable Mutex mLock;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800208 mutable std::unordered_map<int32_t, nsecs_t> mLastHwVSync;
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700209
210 // thread-safe
Dan Stoza9e56aa02015-11-02 13:00:03 -0800211 mutable Mutex mVsyncLock;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700212};
213
Mathias Agopiana350ff92010-08-10 17:14:02 -0700214// ---------------------------------------------------------------------------
215}; // namespace android
216
217#endif // ANDROID_SF_HWCOMPOSER_H