blob: 7b61e0e967486d4b5a8d3b5a117076f9872c160e [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
Fabien Sanglard9d96de42016-10-11 00:15:18 +000017#ifndef USE_HWC2
18#include "HWComposer_hwc1.h"
19#else
20
Mathias Agopiana350ff92010-08-10 17:14:02 -070021#ifndef ANDROID_SF_HWCOMPOSER_H
22#define ANDROID_SF_HWCOMPOSER_H
23
Dan Stoza9e56aa02015-11-02 13:00:03 -080024#include "HWC2.h"
25
Mathias Agopiana350ff92010-08-10 17:14:02 -070026#include <stdint.h>
27#include <sys/types.h>
28
Chia-I Wu06d63de2017-01-04 14:58:51 +080029#include <gui/BufferQueue.h>
30
Jamie Gennis2ec3e072012-11-11 16:24:33 -080031#include <ui/Fence.h>
32
33#include <utils/BitSet.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070034#include <utils/Condition.h>
35#include <utils/Mutex.h>
Mathias Agopianc7d14e22011-08-01 16:32:21 -070036#include <utils/StrongPointer.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070037#include <utils/Thread.h>
38#include <utils/Timers.h>
Mathias Agopian22da60c2011-09-09 00:49:11 -070039#include <utils/Vector.h>
Mathias Agopianc7d14e22011-08-01 16:32:21 -070040
Dan Stoza9e56aa02015-11-02 13:00:03 -080041#include <memory>
42#include <set>
43#include <vector>
44
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070045extern "C" int clock_nanosleep(clockid_t clock_id, int flags,
46 const struct timespec *request,
47 struct timespec *remain);
48
Mathias Agopian8b736f12012-08-13 17:54:26 -070049struct framebuffer_device_t;
Mathias Agopian3e8b8532012-05-13 20:42:01 -070050
Dan Stoza9e56aa02015-11-02 13:00:03 -080051namespace HWC2 {
52 class Device;
53 class Display;
54}
55
Mathias Agopiana350ff92010-08-10 17:14:02 -070056namespace android {
57// ---------------------------------------------------------------------------
58
Dan Stoza9e56aa02015-11-02 13:00:03 -080059class DisplayDevice;
Mathias Agopianda27af92012-09-13 18:17:13 -070060class Fence;
Mathias Agopian6b442672013-07-09 21:24:52 -070061class FloatRect;
Jesse Hall399184a2014-03-03 15:42:54 -080062class GraphicBuffer;
Dan Stoza9e56aa02015-11-02 13:00:03 -080063class HWC2On1Adapter;
Jesse Hall399184a2014-03-03 15:42:54 -080064class NativeHandle;
Jamie Gennis1a4d8832012-08-02 20:11:05 -070065class Region;
Mathias Agopian83727852010-09-23 18:13:21 -070066class String8;
Mathias Agopianc7d14e22011-08-01 16:32:21 -070067class SurfaceFlinger;
Mathias Agopian83727852010-09-23 18:13:21 -070068
Mathias Agopiana350ff92010-08-10 17:14:02 -070069class HWComposer
70{
71public:
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070072 class EventHandler {
73 friend class HWComposer;
Dan Stoza9e56aa02015-11-02 13:00:03 -080074 virtual void onVSyncReceived(int32_t disp, nsecs_t timestamp) = 0;
75 virtual void onHotplugReceived(int32_t disp, bool connected) = 0;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070076 protected:
77 virtual ~EventHandler() {}
78 };
Mathias Agopiana350ff92010-08-10 17:14:02 -070079
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -080080 // useVrComposer is passed to the composer HAL. When true, the composer HAL
81 // will use the vr composer service, otherwise it uses the real hardware
82 // composer.
83 HWComposer(const sp<SurfaceFlinger>& flinger, bool useVrComposer);
Mathias Agopian8b736f12012-08-13 17:54:26 -070084
Mathias Agopiana350ff92010-08-10 17:14:02 -070085 ~HWComposer();
86
Dan Stoza9e56aa02015-11-02 13:00:03 -080087 void setEventHandler(EventHandler* handler);
Mathias Agopiana350ff92010-08-10 17:14:02 -070088
Dan Stoza9f26a9c2016-06-22 14:51:09 -070089 bool hasCapability(HWC2::Capability capability) const;
90
Dan Stoza9e56aa02015-11-02 13:00:03 -080091 // Attempts to allocate a virtual display. If the virtual display is created
92 // on the HWC device, outId will contain its HWC ID.
93 status_t allocateVirtualDisplay(uint32_t width, uint32_t height,
Dan Stoza5cf424b2016-05-20 14:02:39 -070094 android_pixel_format_t* format, int32_t* outId);
Mathias Agopiane60b0682012-08-21 23:34:09 -070095
Dan Stoza9e56aa02015-11-02 13:00:03 -080096 // Attempts to create a new layer on this display
97 std::shared_ptr<HWC2::Layer> createLayer(int32_t displayId);
Mathias Agopiane60b0682012-08-21 23:34:09 -070098
Mathias Agopiana350ff92010-08-10 17:14:02 -070099 // Asks the HAL what it can do
Dan Stoza9e56aa02015-11-02 13:00:03 -0800100 status_t prepare(DisplayDevice& displayDevice);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700101
Chia-I Wu06d63de2017-01-04 14:58:51 +0800102 status_t setClientTarget(int32_t displayId, uint32_t slot,
103 const sp<Fence>& acquireFence,
Dan Stoza9e56aa02015-11-02 13:00:03 -0800104 const sp<GraphicBuffer>& target, android_dataspace_t dataspace);
105
Fabien Sanglarda87aa7b2016-11-30 16:27:22 -0800106 // Present layers to the display and read releaseFences.
107 status_t presentAndGetReleaseFences(int32_t displayId);
Mathias Agopiana350ff92010-08-10 17:14:02 -0700108
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700109 // set power mode
Dan Stoza9e56aa02015-11-02 13:00:03 -0800110 status_t setPowerMode(int32_t displayId, int mode);
Colin Cross10fbdb62012-07-12 17:56:34 -0700111
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700112 // set active config
Dan Stoza9e56aa02015-11-02 13:00:03 -0800113 status_t setActiveConfig(int32_t displayId, size_t configId);
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700114
Dan Stoza9f26a9c2016-06-22 14:51:09 -0700115 // Sets a color transform to be applied to the result of composition
116 status_t setColorTransform(int32_t displayId, const mat4& transform);
117
Andy McFadden27ec5732012-10-02 19:04:45 -0700118 // reset state when an external, non-virtual display is disconnected
Dan Stoza9e56aa02015-11-02 13:00:03 -0800119 void disconnectDisplay(int32_t displayId);
Mathias Agopianda27af92012-09-13 18:17:13 -0700120
Mathias Agopiane60b0682012-08-21 23:34:09 -0700121 // does this display have layers handled by HWC
Dan Stoza9e56aa02015-11-02 13:00:03 -0800122 bool hasDeviceComposition(int32_t displayId) const;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700123
124 // 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
Brian Anderson3d4039d2016-09-23 16:31:30 -0700130 // Returns true if the present fence represents the start of the display
Brian Anderson069b3652016-07-22 10:32:47 -0700131 // controller's scan out. This should be true for all HWC2 implementations,
132 // except for the wrapper around HWC1 implementations.
Brian Anderson3d4039d2016-09-23 16:31:30 -0700133 bool presentFenceRepresentsStartOfScanout() const;
Brian Anderson069b3652016-07-22 10:32:47 -0700134
Dan Stoza9e56aa02015-11-02 13:00:03 -0800135 // Get last release fence for the given layer
136 sp<Fence> getLayerReleaseFence(int32_t displayId,
137 const std::shared_ptr<HWC2::Layer>& layer) const;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700138
Jesse Hall851cfe82013-03-20 13:44:00 -0700139 // Set the output buffer and acquire fence for a virtual display.
Dan Stoza9e56aa02015-11-02 13:00:03 -0800140 // Returns INVALID_OPERATION if displayId is not a virtual display.
141 status_t setOutputBuffer(int32_t displayId, const sp<Fence>& acquireFence,
Jesse Hall851cfe82013-03-20 13:44:00 -0700142 const sp<GraphicBuffer>& buf);
143
Dan Stoza9e56aa02015-11-02 13:00:03 -0800144 // After SurfaceFlinger has retrieved the release fences for all the frames,
145 // it can call this to clear the shared pointers in the release fence map
146 void clearReleaseFences(int32_t displayId);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700147
Dan Stozac4f471e2016-03-24 09:31:08 -0700148 // Returns the HDR capabilities of the given display
149 std::unique_ptr<HdrCapabilities> getHdrCapabilities(int32_t displayId);
150
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700151 // Events handling ---------------------------------------------------------
152
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800153 void setVsyncEnabled(int32_t displayId, HWC2::Vsync enabled);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700154
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700155 // Query display parameters. Pass in a display index (e.g.
156 // HWC_DISPLAY_PRIMARY).
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800157 nsecs_t getRefreshTimestamp(int32_t displayId) const;
158 bool isConnected(int32_t displayId) const;
Dan Stoza7f7da322014-05-02 15:26:25 -0700159
Dan Stoza9e56aa02015-11-02 13:00:03 -0800160 // Non-const because it can update configMap inside of mDisplayData
161 std::vector<std::shared_ptr<const HWC2::Display::Config>>
162 getConfigs(int32_t displayId) const;
Dan Stoza7f7da322014-05-02 15:26:25 -0700163
Dan Stoza9e56aa02015-11-02 13:00:03 -0800164 std::shared_ptr<const HWC2::Display::Config>
165 getActiveConfig(int32_t displayId) const;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700166
Michael Wright28f24d02016-07-12 13:30:53 -0700167 std::vector<android_color_mode_t> getColorModes(int32_t displayId) const;
168
169 status_t setActiveColorMode(int32_t displayId, android_color_mode_t mode);
Courtney Goeltzenleuchterfad9d8c2016-06-23 11:49:50 -0600170
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800171 bool isUsingVrComposer() const;
172
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700173 // for debugging ----------------------------------------------------------
Mathias Agopian74d211a2013-04-22 16:55:35 +0200174 void dump(String8& out) const;
Mathias Agopian83727852010-09-23 18:13:21 -0700175
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800176 android::Hwc2::Composer* getComposer() const { return mHwcDevice->getComposer(); }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700177private:
Dan Stoza9e56aa02015-11-02 13:00:03 -0800178 static const int32_t VIRTUAL_DISPLAY_ID_BASE = 2;
Jesse Hallb685c542012-07-31 14:32:56 -0700179
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800180 void loadHwcModule(bool useVrComposer);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800181
182 bool isValidDisplay(int32_t displayId) const;
183 static void validateChange(HWC2::Composition from, HWC2::Composition to);
Mathias Agopian31d28432012-04-03 16:31:39 -0700184
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700185 struct cb_context;
Mathias Agopian31d28432012-04-03 16:31:39 -0700186
Dan Stoza9e56aa02015-11-02 13:00:03 -0800187 void invalidate(const std::shared_ptr<HWC2::Display>& display);
188 void vsync(const std::shared_ptr<HWC2::Display>& display,
Jesse Hallbbd164a2012-08-21 12:05:09 -0700189 int64_t timestamp);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800190 void hotplug(const std::shared_ptr<HWC2::Display>& display,
191 HWC2::Connection connected);
Mathias Agopianda27af92012-09-13 18:17:13 -0700192
Mathias Agopiane60b0682012-08-21 23:34:09 -0700193 struct DisplayData {
Jesse Halla9a1b002013-02-27 16:39:25 -0800194 DisplayData();
195 ~DisplayData();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800196 void reset();
197
198 bool hasClientComposition;
199 bool hasDeviceComposition;
200 std::shared_ptr<HWC2::Display> hwcDisplay;
201 HWC2::DisplayRequest displayRequests;
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800202 sp<Fence> lastPresentFence; // signals when the last set op retires
Dan Stoza9e56aa02015-11-02 13:00:03 -0800203 std::unordered_map<std::shared_ptr<HWC2::Layer>, sp<Fence>>
204 releaseFences;
Jesse Hall851cfe82013-03-20 13:44:00 -0700205 buffer_handle_t outbufHandle;
206 sp<Fence> outbufAcquireFence;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800207 mutable std::unordered_map<int32_t,
208 std::shared_ptr<const HWC2::Display::Config>> configMap;
Jamie Gennis2ec3e072012-11-11 16:24:33 -0800209
Dan Stoza9e56aa02015-11-02 13:00:03 -0800210 // protected by mVsyncLock
211 HWC2::Vsync vsyncEnabled;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700212 };
213
Jesse Hall5880cc52012-06-05 23:40:32 -0700214 sp<SurfaceFlinger> mFlinger;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800215 std::unique_ptr<HWC2On1Adapter> mAdapter;
216 std::unique_ptr<HWC2::Device> mHwcDevice;
217 std::vector<DisplayData> mDisplayData;
218 std::set<size_t> mFreeDisplaySlots;
219 std::unordered_map<hwc2_display_t, int32_t> mHwcDisplaySlots;
Manoj Kumar AVMe04e4ed2015-06-11 14:18:14 -0700220 // protect mDisplayData from races between prepare and dump
221 mutable Mutex mDisplayLock;
Mathias Agopian1e260872012-08-08 18:35:12 -0700222
Jesse Hall5880cc52012-06-05 23:40:32 -0700223 cb_context* mCBContext;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800224 EventHandler* mEventHandler;
Mathias Agopianbef42c52013-08-21 17:45:46 -0700225 size_t mVSyncCounts[HWC_NUM_PHYSICAL_DISPLAY_TYPES];
Dan Stoza9e56aa02015-11-02 13:00:03 -0800226 uint32_t mRemainingHwcVirtualDisplays;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700227
228 // protected by mLock
229 mutable Mutex mLock;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800230 mutable std::unordered_map<int32_t, nsecs_t> mLastHwVSync;
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700231
232 // thread-safe
Dan Stoza9e56aa02015-11-02 13:00:03 -0800233 mutable Mutex mVsyncLock;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700234};
235
Chia-I Wu06d63de2017-01-04 14:58:51 +0800236class HWComposerBufferCache {
237public:
238 void clear();
239
240 void getHwcBuffer(int slot, const sp<GraphicBuffer>& buffer,
241 uint32_t* outSlot, sp<GraphicBuffer>* outBuffer);
242
243private:
244 // a vector as we expect "slot" to be in the range of [0, 63] (that is,
245 // less than BufferQueue::NUM_BUFFER_SLOTS).
246 std::vector<sp<GraphicBuffer>> mBuffers;
247};
248
Mathias Agopiana350ff92010-08-10 17:14:02 -0700249// ---------------------------------------------------------------------------
250}; // namespace android
251
252#endif // ANDROID_SF_HWCOMPOSER_H
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000253
254#endif // #ifdef USE_HWC2