blob: 3dfb65b1e3a01674b1e30e9a53c3f7491578af9c [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
Jamie Gennis2ec3e072012-11-11 16:24:33 -080029#include <ui/Fence.h>
30
31#include <utils/BitSet.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070032#include <utils/Condition.h>
33#include <utils/Mutex.h>
Mathias Agopianc7d14e22011-08-01 16:32:21 -070034#include <utils/StrongPointer.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070035#include <utils/Thread.h>
36#include <utils/Timers.h>
Mathias Agopian22da60c2011-09-09 00:49:11 -070037#include <utils/Vector.h>
Mathias Agopianc7d14e22011-08-01 16:32:21 -070038
Dan Stoza9e56aa02015-11-02 13:00:03 -080039#include <memory>
40#include <set>
41#include <vector>
42
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070043extern "C" int clock_nanosleep(clockid_t clock_id, int flags,
44 const struct timespec *request,
45 struct timespec *remain);
46
Mathias Agopian8b736f12012-08-13 17:54:26 -070047struct framebuffer_device_t;
Mathias Agopian3e8b8532012-05-13 20:42:01 -070048
Dan Stoza9e56aa02015-11-02 13:00:03 -080049namespace HWC2 {
50 class Device;
51 class Display;
52}
53
Mathias Agopiana350ff92010-08-10 17:14:02 -070054namespace android {
55// ---------------------------------------------------------------------------
56
Dan Stoza9e56aa02015-11-02 13:00:03 -080057class DisplayDevice;
Mathias Agopianda27af92012-09-13 18:17:13 -070058class Fence;
Mathias Agopian6b442672013-07-09 21:24:52 -070059class FloatRect;
Jesse Hall399184a2014-03-03 15:42:54 -080060class GraphicBuffer;
61class NativeHandle;
Jamie Gennis1a4d8832012-08-02 20:11:05 -070062class Region;
Mathias Agopian83727852010-09-23 18:13:21 -070063class String8;
64
Mathias Agopiana350ff92010-08-10 17:14:02 -070065class HWComposer
66{
67public:
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070068 class EventHandler {
69 friend class HWComposer;
Steven Thomas3cfac282017-02-06 12:29:30 -080070 virtual void onVSyncReceived(
71 HWComposer* composer, int32_t disp, nsecs_t timestamp) = 0;
Stephen Kiazyk7d3dcb92017-04-05 16:46:49 -070072 virtual void onHotplugReceived(HWComposer* composer, int32_t disp, bool connected) = 0;
Steven Thomas3cfac282017-02-06 12:29:30 -080073 virtual void onInvalidateReceived(HWComposer* composer) = 0;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -070074 protected:
75 virtual ~EventHandler() {}
76 };
Mathias Agopiana350ff92010-08-10 17:14:02 -070077
Kalle Raitaa099a242017-01-11 11:17:29 -080078 // Uses the named composer service. Valid choices for normal use
79 // are 'default' and 'vr'.
80 HWComposer(const std::string& serviceName);
Mathias Agopian8b736f12012-08-13 17:54:26 -070081
Mathias Agopiana350ff92010-08-10 17:14:02 -070082 ~HWComposer();
83
Dan Stoza9e56aa02015-11-02 13:00:03 -080084 void setEventHandler(EventHandler* handler);
Mathias Agopiana350ff92010-08-10 17:14:02 -070085
Dan Stoza9f26a9c2016-06-22 14:51:09 -070086 bool hasCapability(HWC2::Capability capability) const;
87
Dan Stoza9e56aa02015-11-02 13:00:03 -080088 // Attempts to allocate a virtual display. If the virtual display is created
89 // on the HWC device, outId will contain its HWC ID.
90 status_t allocateVirtualDisplay(uint32_t width, uint32_t height,
Dan Stoza5cf424b2016-05-20 14:02:39 -070091 android_pixel_format_t* format, int32_t* outId);
Mathias Agopiane60b0682012-08-21 23:34:09 -070092
Dan Stoza9e56aa02015-11-02 13:00:03 -080093 // Attempts to create a new layer on this display
94 std::shared_ptr<HWC2::Layer> createLayer(int32_t displayId);
Mathias Agopiane60b0682012-08-21 23:34:09 -070095
Mathias Agopiana350ff92010-08-10 17:14:02 -070096 // Asks the HAL what it can do
Dan Stoza9e56aa02015-11-02 13:00:03 -080097 status_t prepare(DisplayDevice& displayDevice);
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,
Dan Stoza9e56aa02015-11-02 13:00:03 -0800101 const sp<GraphicBuffer>& target, android_dataspace_t dataspace);
102
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
121 // does this display have layers handled by GLES
Dan Stoza9e56aa02015-11-02 13:00:03 -0800122 bool hasClientComposition(int32_t displayId) const;
Mathias Agopian9c6e2972011-09-20 17:21:56 -0700123
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800124 // get the present fence received from the last call to present.
125 sp<Fence> getPresentFence(int32_t displayId) const;
Mathias Agopianda27af92012-09-13 18:17:13 -0700126
Dan Stoza9e56aa02015-11-02 13:00:03 -0800127 // Get last release fence for the given layer
128 sp<Fence> getLayerReleaseFence(int32_t displayId,
129 const std::shared_ptr<HWC2::Layer>& layer) const;
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700130
Jesse Hall851cfe82013-03-20 13:44:00 -0700131 // Set the output buffer and acquire fence for a virtual display.
Dan Stoza9e56aa02015-11-02 13:00:03 -0800132 // Returns INVALID_OPERATION if displayId is not a virtual display.
133 status_t setOutputBuffer(int32_t displayId, const sp<Fence>& acquireFence,
Jesse Hall851cfe82013-03-20 13:44:00 -0700134 const sp<GraphicBuffer>& buf);
135
Dan Stoza9e56aa02015-11-02 13:00:03 -0800136 // After SurfaceFlinger has retrieved the release fences for all the frames,
137 // it can call this to clear the shared pointers in the release fence map
138 void clearReleaseFences(int32_t displayId);
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700139
Dan Stozac4f471e2016-03-24 09:31:08 -0700140 // Returns the HDR capabilities of the given display
141 std::unique_ptr<HdrCapabilities> getHdrCapabilities(int32_t displayId);
142
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700143 // Events handling ---------------------------------------------------------
144
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800145 void setVsyncEnabled(int32_t displayId, HWC2::Vsync enabled);
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700146
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700147 // Query display parameters. Pass in a display index (e.g.
148 // HWC_DISPLAY_PRIMARY).
Fabien Sanglarddf0b7052016-11-30 15:51:53 -0800149 nsecs_t getRefreshTimestamp(int32_t displayId) const;
150 bool isConnected(int32_t displayId) const;
Dan Stoza7f7da322014-05-02 15:26:25 -0700151
Dan Stoza9e56aa02015-11-02 13:00:03 -0800152 // Non-const because it can update configMap inside of mDisplayData
153 std::vector<std::shared_ptr<const HWC2::Display::Config>>
154 getConfigs(int32_t displayId) const;
Dan Stoza7f7da322014-05-02 15:26:25 -0700155
Dan Stoza9e56aa02015-11-02 13:00:03 -0800156 std::shared_ptr<const HWC2::Display::Config>
157 getActiveConfig(int32_t displayId) const;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700158
Michael Wright28f24d02016-07-12 13:30:53 -0700159 std::vector<android_color_mode_t> getColorModes(int32_t displayId) const;
160
161 status_t setActiveColorMode(int32_t displayId, android_color_mode_t mode);
Courtney Goeltzenleuchterfad9d8c2016-06-23 11:49:50 -0600162
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800163 bool isUsingVrComposer() const;
164
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700165 // for debugging ----------------------------------------------------------
Mathias Agopian74d211a2013-04-22 16:55:35 +0200166 void dump(String8& out) const;
Mathias Agopian83727852010-09-23 18:13:21 -0700167
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800168 android::Hwc2::Composer* getComposer() const { return mHwcDevice->getComposer(); }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700169private:
Dan Stoza9e56aa02015-11-02 13:00:03 -0800170 static const int32_t VIRTUAL_DISPLAY_ID_BASE = 2;
Jesse Hallb685c542012-07-31 14:32:56 -0700171
Kalle Raitaa099a242017-01-11 11:17:29 -0800172 void loadHwcModule(const std::string& serviceName);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800173
174 bool isValidDisplay(int32_t displayId) const;
175 static void validateChange(HWC2::Composition from, HWC2::Composition to);
Mathias Agopian31d28432012-04-03 16:31:39 -0700176
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700177 struct cb_context;
Mathias Agopian31d28432012-04-03 16:31:39 -0700178
Dan Stoza9e56aa02015-11-02 13:00:03 -0800179 void invalidate(const std::shared_ptr<HWC2::Display>& display);
180 void vsync(const std::shared_ptr<HWC2::Display>& display,
Jesse Hallbbd164a2012-08-21 12:05:09 -0700181 int64_t timestamp);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800182 void hotplug(const std::shared_ptr<HWC2::Display>& display,
183 HWC2::Connection connected);
Mathias Agopianda27af92012-09-13 18:17:13 -0700184
Mathias Agopiane60b0682012-08-21 23:34:09 -0700185 struct DisplayData {
Jesse Halla9a1b002013-02-27 16:39:25 -0800186 DisplayData();
187 ~DisplayData();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800188 void reset();
189
190 bool hasClientComposition;
191 bool hasDeviceComposition;
192 std::shared_ptr<HWC2::Display> hwcDisplay;
193 HWC2::DisplayRequest displayRequests;
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800194 sp<Fence> lastPresentFence; // signals when the last set op retires
Dan Stoza9e56aa02015-11-02 13:00:03 -0800195 std::unordered_map<std::shared_ptr<HWC2::Layer>, sp<Fence>>
196 releaseFences;
Jesse Hall851cfe82013-03-20 13:44:00 -0700197 buffer_handle_t outbufHandle;
198 sp<Fence> outbufAcquireFence;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800199 mutable std::unordered_map<int32_t,
200 std::shared_ptr<const HWC2::Display::Config>> configMap;
Jamie Gennis2ec3e072012-11-11 16:24:33 -0800201
Dan Stoza9e56aa02015-11-02 13:00:03 -0800202 // protected by mVsyncLock
203 HWC2::Vsync vsyncEnabled;
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700204
205 bool validateWasSkipped;
206 HWC2::Error presentError;
Mathias Agopiane60b0682012-08-21 23:34:09 -0700207 };
208
Dan Stoza9e56aa02015-11-02 13:00:03 -0800209 std::unique_ptr<HWC2::Device> mHwcDevice;
210 std::vector<DisplayData> mDisplayData;
211 std::set<size_t> mFreeDisplaySlots;
212 std::unordered_map<hwc2_display_t, int32_t> mHwcDisplaySlots;
Manoj Kumar AVMe04e4ed2015-06-11 14:18:14 -0700213 // protect mDisplayData from races between prepare and dump
214 mutable Mutex mDisplayLock;
Mathias Agopian1e260872012-08-08 18:35:12 -0700215
Jesse Hall5880cc52012-06-05 23:40:32 -0700216 cb_context* mCBContext;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800217 EventHandler* mEventHandler;
Mathias Agopianbef42c52013-08-21 17:45:46 -0700218 size_t mVSyncCounts[HWC_NUM_PHYSICAL_DISPLAY_TYPES];
Dan Stoza9e56aa02015-11-02 13:00:03 -0800219 uint32_t mRemainingHwcVirtualDisplays;
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700220
221 // protected by mLock
222 mutable Mutex mLock;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800223 mutable std::unordered_map<int32_t, nsecs_t> mLastHwVSync;
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700224
225 // thread-safe
Dan Stoza9e56aa02015-11-02 13:00:03 -0800226 mutable Mutex mVsyncLock;
Mathias Agopiana350ff92010-08-10 17:14:02 -0700227};
228
Mathias Agopiana350ff92010-08-10 17:14:02 -0700229// ---------------------------------------------------------------------------
230}; // namespace android
231
232#endif // ANDROID_SF_HWCOMPOSER_H
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000233
234#endif // #ifdef USE_HWC2