Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 1 | /* |
| 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 Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 17 | #ifndef USE_HWC2 |
| 18 | #include "HWComposer_hwc1.h" |
| 19 | #else |
| 20 | |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 21 | #ifndef ANDROID_SF_HWCOMPOSER_H |
| 22 | #define ANDROID_SF_HWCOMPOSER_H |
| 23 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 24 | #include "HWC2.h" |
| 25 | |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 26 | #include <stdint.h> |
| 27 | #include <sys/types.h> |
| 28 | |
Jamie Gennis | 2ec3e07 | 2012-11-11 16:24:33 -0800 | [diff] [blame] | 29 | #include <ui/Fence.h> |
| 30 | |
| 31 | #include <utils/BitSet.h> |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 32 | #include <utils/Condition.h> |
| 33 | #include <utils/Mutex.h> |
Mathias Agopian | c7d14e2 | 2011-08-01 16:32:21 -0700 | [diff] [blame] | 34 | #include <utils/StrongPointer.h> |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 35 | #include <utils/Thread.h> |
| 36 | #include <utils/Timers.h> |
Mathias Agopian | 22da60c | 2011-09-09 00:49:11 -0700 | [diff] [blame] | 37 | #include <utils/Vector.h> |
Mathias Agopian | c7d14e2 | 2011-08-01 16:32:21 -0700 | [diff] [blame] | 38 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 39 | #include <memory> |
| 40 | #include <set> |
| 41 | #include <vector> |
| 42 | |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 43 | extern "C" int clock_nanosleep(clockid_t clock_id, int flags, |
| 44 | const struct timespec *request, |
| 45 | struct timespec *remain); |
| 46 | |
Mathias Agopian | 8b736f1 | 2012-08-13 17:54:26 -0700 | [diff] [blame] | 47 | struct framebuffer_device_t; |
Mathias Agopian | 3e8b853 | 2012-05-13 20:42:01 -0700 | [diff] [blame] | 48 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 49 | namespace HWC2 { |
| 50 | class Device; |
| 51 | class Display; |
| 52 | } |
| 53 | |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 54 | namespace android { |
| 55 | // --------------------------------------------------------------------------- |
| 56 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 57 | class DisplayDevice; |
Mathias Agopian | da27af9 | 2012-09-13 18:17:13 -0700 | [diff] [blame] | 58 | class Fence; |
Mathias Agopian | 6b44267 | 2013-07-09 21:24:52 -0700 | [diff] [blame] | 59 | class FloatRect; |
Jesse Hall | 399184a | 2014-03-03 15:42:54 -0800 | [diff] [blame] | 60 | class GraphicBuffer; |
| 61 | class NativeHandle; |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 62 | class Region; |
Mathias Agopian | 8372785 | 2010-09-23 18:13:21 -0700 | [diff] [blame] | 63 | class String8; |
| 64 | |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 65 | class HWComposer |
| 66 | { |
| 67 | public: |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 68 | class EventHandler { |
| 69 | friend class HWComposer; |
Steven Thomas | 3cfac28 | 2017-02-06 12:29:30 -0800 | [diff] [blame] | 70 | virtual void onVSyncReceived( |
| 71 | HWComposer* composer, int32_t disp, nsecs_t timestamp) = 0; |
Stephen Kiazyk | 7d3dcb9 | 2017-04-05 16:46:49 -0700 | [diff] [blame] | 72 | virtual void onHotplugReceived(HWComposer* composer, int32_t disp, bool connected) = 0; |
Steven Thomas | 3cfac28 | 2017-02-06 12:29:30 -0800 | [diff] [blame] | 73 | virtual void onInvalidateReceived(HWComposer* composer) = 0; |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 74 | protected: |
| 75 | virtual ~EventHandler() {} |
| 76 | }; |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 77 | |
Hendrik Wagenaar | 87670ff | 2017-02-01 12:10:46 -0800 | [diff] [blame] | 78 | // useVrComposer is passed to the composer HAL. When true, the composer HAL |
| 79 | // will use the vr composer service, otherwise it uses the real hardware |
| 80 | // composer. |
Steven Thomas | 3cfac28 | 2017-02-06 12:29:30 -0800 | [diff] [blame] | 81 | HWComposer(bool useVrComposer); |
Mathias Agopian | 8b736f1 | 2012-08-13 17:54:26 -0700 | [diff] [blame] | 82 | |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 83 | ~HWComposer(); |
| 84 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 85 | void setEventHandler(EventHandler* handler); |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 86 | |
Dan Stoza | 9f26a9c | 2016-06-22 14:51:09 -0700 | [diff] [blame] | 87 | bool hasCapability(HWC2::Capability capability) const; |
| 88 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 89 | // Attempts to allocate a virtual display. If the virtual display is created |
| 90 | // on the HWC device, outId will contain its HWC ID. |
| 91 | status_t allocateVirtualDisplay(uint32_t width, uint32_t height, |
Dan Stoza | 5cf424b | 2016-05-20 14:02:39 -0700 | [diff] [blame] | 92 | android_pixel_format_t* format, int32_t* outId); |
Mathias Agopian | e60b068 | 2012-08-21 23:34:09 -0700 | [diff] [blame] | 93 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 94 | // Attempts to create a new layer on this display |
| 95 | std::shared_ptr<HWC2::Layer> createLayer(int32_t displayId); |
Mathias Agopian | e60b068 | 2012-08-21 23:34:09 -0700 | [diff] [blame] | 96 | |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 97 | // Asks the HAL what it can do |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 98 | status_t prepare(DisplayDevice& displayDevice); |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 99 | |
Chia-I Wu | 06d63de | 2017-01-04 14:58:51 +0800 | [diff] [blame] | 100 | status_t setClientTarget(int32_t displayId, uint32_t slot, |
| 101 | const sp<Fence>& acquireFence, |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 102 | const sp<GraphicBuffer>& target, android_dataspace_t dataspace); |
| 103 | |
Fabien Sanglard | a87aa7b | 2016-11-30 16:27:22 -0800 | [diff] [blame] | 104 | // Present layers to the display and read releaseFences. |
| 105 | status_t presentAndGetReleaseFences(int32_t displayId); |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 106 | |
Prashant Malani | 2c9b11f | 2014-05-25 01:36:31 -0700 | [diff] [blame] | 107 | // set power mode |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 108 | status_t setPowerMode(int32_t displayId, int mode); |
Colin Cross | 10fbdb6 | 2012-07-12 17:56:34 -0700 | [diff] [blame] | 109 | |
Michael Lentine | 6c9e34a | 2014-07-14 13:48:55 -0700 | [diff] [blame] | 110 | // set active config |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 111 | status_t setActiveConfig(int32_t displayId, size_t configId); |
Michael Lentine | 6c9e34a | 2014-07-14 13:48:55 -0700 | [diff] [blame] | 112 | |
Dan Stoza | 9f26a9c | 2016-06-22 14:51:09 -0700 | [diff] [blame] | 113 | // Sets a color transform to be applied to the result of composition |
| 114 | status_t setColorTransform(int32_t displayId, const mat4& transform); |
| 115 | |
Andy McFadden | 27ec573 | 2012-10-02 19:04:45 -0700 | [diff] [blame] | 116 | // reset state when an external, non-virtual display is disconnected |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 117 | void disconnectDisplay(int32_t displayId); |
Mathias Agopian | da27af9 | 2012-09-13 18:17:13 -0700 | [diff] [blame] | 118 | |
Mathias Agopian | e60b068 | 2012-08-21 23:34:09 -0700 | [diff] [blame] | 119 | // does this display have layers handled by HWC |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 120 | bool hasDeviceComposition(int32_t displayId) const; |
Mathias Agopian | e60b068 | 2012-08-21 23:34:09 -0700 | [diff] [blame] | 121 | |
| 122 | // does this display have layers handled by GLES |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 123 | bool hasClientComposition(int32_t displayId) const; |
Mathias Agopian | 9c6e297 | 2011-09-20 17:21:56 -0700 | [diff] [blame] | 124 | |
Fabien Sanglard | 11d0fc3 | 2016-12-01 15:43:01 -0800 | [diff] [blame] | 125 | // get the present fence received from the last call to present. |
| 126 | sp<Fence> getPresentFence(int32_t displayId) const; |
Mathias Agopian | da27af9 | 2012-09-13 18:17:13 -0700 | [diff] [blame] | 127 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 128 | // Get last release fence for the given layer |
| 129 | sp<Fence> getLayerReleaseFence(int32_t displayId, |
| 130 | const std::shared_ptr<HWC2::Layer>& layer) const; |
Andy McFadden | b0d1dd3 | 2012-09-10 14:08:09 -0700 | [diff] [blame] | 131 | |
Jesse Hall | 851cfe8 | 2013-03-20 13:44:00 -0700 | [diff] [blame] | 132 | // Set the output buffer and acquire fence for a virtual display. |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 133 | // Returns INVALID_OPERATION if displayId is not a virtual display. |
| 134 | status_t setOutputBuffer(int32_t displayId, const sp<Fence>& acquireFence, |
Jesse Hall | 851cfe8 | 2013-03-20 13:44:00 -0700 | [diff] [blame] | 135 | const sp<GraphicBuffer>& buf); |
| 136 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 137 | // After SurfaceFlinger has retrieved the release fences for all the frames, |
| 138 | // it can call this to clear the shared pointers in the release fence map |
| 139 | void clearReleaseFences(int32_t displayId); |
Mathias Agopian | 3e8b853 | 2012-05-13 20:42:01 -0700 | [diff] [blame] | 140 | |
Dan Stoza | c4f471e | 2016-03-24 09:31:08 -0700 | [diff] [blame] | 141 | // Returns the HDR capabilities of the given display |
| 142 | std::unique_ptr<HdrCapabilities> getHdrCapabilities(int32_t displayId); |
| 143 | |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 144 | // Events handling --------------------------------------------------------- |
| 145 | |
Fabien Sanglard | df0b705 | 2016-11-30 15:51:53 -0800 | [diff] [blame] | 146 | void setVsyncEnabled(int32_t displayId, HWC2::Vsync enabled); |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 147 | |
Andy McFadden | b0d1dd3 | 2012-09-10 14:08:09 -0700 | [diff] [blame] | 148 | // Query display parameters. Pass in a display index (e.g. |
| 149 | // HWC_DISPLAY_PRIMARY). |
Fabien Sanglard | df0b705 | 2016-11-30 15:51:53 -0800 | [diff] [blame] | 150 | nsecs_t getRefreshTimestamp(int32_t displayId) const; |
| 151 | bool isConnected(int32_t displayId) const; |
Dan Stoza | 7f7da32 | 2014-05-02 15:26:25 -0700 | [diff] [blame] | 152 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 153 | // Non-const because it can update configMap inside of mDisplayData |
| 154 | std::vector<std::shared_ptr<const HWC2::Display::Config>> |
| 155 | getConfigs(int32_t displayId) const; |
Dan Stoza | 7f7da32 | 2014-05-02 15:26:25 -0700 | [diff] [blame] | 156 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 157 | std::shared_ptr<const HWC2::Display::Config> |
| 158 | getActiveConfig(int32_t displayId) const; |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 159 | |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 160 | std::vector<android_color_mode_t> getColorModes(int32_t displayId) const; |
| 161 | |
| 162 | status_t setActiveColorMode(int32_t displayId, android_color_mode_t mode); |
Courtney Goeltzenleuchter | fad9d8c | 2016-06-23 11:49:50 -0600 | [diff] [blame] | 163 | |
Hendrik Wagenaar | 87670ff | 2017-02-01 12:10:46 -0800 | [diff] [blame] | 164 | bool isUsingVrComposer() const; |
| 165 | |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 166 | // for debugging ---------------------------------------------------------- |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 167 | void dump(String8& out) const; |
Mathias Agopian | 8372785 | 2010-09-23 18:13:21 -0700 | [diff] [blame] | 168 | |
Hendrik Wagenaar | 87670ff | 2017-02-01 12:10:46 -0800 | [diff] [blame] | 169 | android::Hwc2::Composer* getComposer() const { return mHwcDevice->getComposer(); } |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 170 | private: |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 171 | static const int32_t VIRTUAL_DISPLAY_ID_BASE = 2; |
Jesse Hall | b685c54 | 2012-07-31 14:32:56 -0700 | [diff] [blame] | 172 | |
Hendrik Wagenaar | 87670ff | 2017-02-01 12:10:46 -0800 | [diff] [blame] | 173 | void loadHwcModule(bool useVrComposer); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 174 | |
| 175 | bool isValidDisplay(int32_t displayId) const; |
| 176 | static void validateChange(HWC2::Composition from, HWC2::Composition to); |
Mathias Agopian | 31d2843 | 2012-04-03 16:31:39 -0700 | [diff] [blame] | 177 | |
Mathias Agopian | 3e8b853 | 2012-05-13 20:42:01 -0700 | [diff] [blame] | 178 | struct cb_context; |
Mathias Agopian | 31d2843 | 2012-04-03 16:31:39 -0700 | [diff] [blame] | 179 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 180 | void invalidate(const std::shared_ptr<HWC2::Display>& display); |
| 181 | void vsync(const std::shared_ptr<HWC2::Display>& display, |
Jesse Hall | bbd164a | 2012-08-21 12:05:09 -0700 | [diff] [blame] | 182 | int64_t timestamp); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 183 | void hotplug(const std::shared_ptr<HWC2::Display>& display, |
| 184 | HWC2::Connection connected); |
Mathias Agopian | da27af9 | 2012-09-13 18:17:13 -0700 | [diff] [blame] | 185 | |
Mathias Agopian | e60b068 | 2012-08-21 23:34:09 -0700 | [diff] [blame] | 186 | struct DisplayData { |
Jesse Hall | a9a1b00 | 2013-02-27 16:39:25 -0800 | [diff] [blame] | 187 | DisplayData(); |
| 188 | ~DisplayData(); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 189 | void reset(); |
| 190 | |
| 191 | bool hasClientComposition; |
| 192 | bool hasDeviceComposition; |
| 193 | std::shared_ptr<HWC2::Display> hwcDisplay; |
| 194 | HWC2::DisplayRequest displayRequests; |
Fabien Sanglard | 11d0fc3 | 2016-12-01 15:43:01 -0800 | [diff] [blame] | 195 | sp<Fence> lastPresentFence; // signals when the last set op retires |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 196 | std::unordered_map<std::shared_ptr<HWC2::Layer>, sp<Fence>> |
| 197 | releaseFences; |
Jesse Hall | 851cfe8 | 2013-03-20 13:44:00 -0700 | [diff] [blame] | 198 | buffer_handle_t outbufHandle; |
| 199 | sp<Fence> outbufAcquireFence; |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 200 | mutable std::unordered_map<int32_t, |
| 201 | std::shared_ptr<const HWC2::Display::Config>> configMap; |
Jamie Gennis | 2ec3e07 | 2012-11-11 16:24:33 -0800 | [diff] [blame] | 202 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 203 | // protected by mVsyncLock |
| 204 | HWC2::Vsync vsyncEnabled; |
Mathias Agopian | e60b068 | 2012-08-21 23:34:09 -0700 | [diff] [blame] | 205 | }; |
| 206 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 207 | std::unique_ptr<HWC2::Device> mHwcDevice; |
| 208 | std::vector<DisplayData> mDisplayData; |
| 209 | std::set<size_t> mFreeDisplaySlots; |
| 210 | std::unordered_map<hwc2_display_t, int32_t> mHwcDisplaySlots; |
Manoj Kumar AVM | e04e4ed | 2015-06-11 14:18:14 -0700 | [diff] [blame] | 211 | // protect mDisplayData from races between prepare and dump |
| 212 | mutable Mutex mDisplayLock; |
Mathias Agopian | 1e26087 | 2012-08-08 18:35:12 -0700 | [diff] [blame] | 213 | |
Jesse Hall | 5880cc5 | 2012-06-05 23:40:32 -0700 | [diff] [blame] | 214 | cb_context* mCBContext; |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 215 | EventHandler* mEventHandler; |
Mathias Agopian | bef42c5 | 2013-08-21 17:45:46 -0700 | [diff] [blame] | 216 | size_t mVSyncCounts[HWC_NUM_PHYSICAL_DISPLAY_TYPES]; |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 217 | uint32_t mRemainingHwcVirtualDisplays; |
Mathias Agopian | d3ee231 | 2012-08-02 14:01:42 -0700 | [diff] [blame] | 218 | |
| 219 | // protected by mLock |
| 220 | mutable Mutex mLock; |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 221 | mutable std::unordered_map<int32_t, nsecs_t> mLastHwVSync; |
Mathias Agopian | 81cd5d3 | 2012-10-04 02:34:38 -0700 | [diff] [blame] | 222 | |
| 223 | // thread-safe |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 224 | mutable Mutex mVsyncLock; |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 225 | }; |
| 226 | |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 227 | // --------------------------------------------------------------------------- |
| 228 | }; // namespace android |
| 229 | |
| 230 | #endif // ANDROID_SF_HWCOMPOSER_H |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 231 | |
| 232 | #endif // #ifdef USE_HWC2 |