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 17 #ifndef USE_HWC2 18 #include "HWComposer_hwc1.h" 19 #else 20 21 #ifndef ANDROID_SF_HWCOMPOSER_H 22 #define ANDROID_SF_HWCOMPOSER_H 23 24 #include "HWC2.h" 25 26 #include <stdint.h> 27 #include <sys/types.h> 28 29 #include <ui/Fence.h> 30 31 #include <utils/BitSet.h> 32 #include <utils/Condition.h> 33 #include <utils/Mutex.h> 34 #include <utils/StrongPointer.h> 35 #include <utils/Thread.h> 36 #include <utils/Timers.h> 37 #include <utils/Vector.h> 38 39 #include <memory> 40 #include <set> 41 #include <vector> 42 43 extern "C" int clock_nanosleep(clockid_t clock_id, int flags, 44 const struct timespec *request, 45 struct timespec *remain); 46 47 struct framebuffer_device_t; 48 49 namespace HWC2 { 50 class Device; 51 class Display; 52 } 53 54 namespace android { 55 // --------------------------------------------------------------------------- 56 57 class DisplayDevice; 58 class Fence; 59 class FloatRect; 60 class GraphicBuffer; 61 class NativeHandle; 62 class Region; 63 class String8; 64 65 class HWComposer 66 { 67 public: 68 // useVrComposer is passed to the composer HAL. When true, the composer HAL 69 // will use the vr composer service, otherwise it uses the real hardware 70 // composer. 71 HWComposer(bool useVrComposer); 72 73 ~HWComposer(); 74 75 void registerCallback(HWC2::ComposerCallback* callback, 76 int32_t sequenceId); 77 78 bool hasCapability(HWC2::Capability capability) const; 79 80 // Attempts to allocate a virtual display. If the virtual display is created 81 // on the HWC device, outId will contain its HWC ID. 82 status_t allocateVirtualDisplay(uint32_t width, uint32_t height, 83 android_pixel_format_t* format, int32_t* outId); 84 85 // Attempts to create a new layer on this display 86 HWC2::Layer* createLayer(int32_t displayId); 87 // Destroy a previously created layer 88 void destroyLayer(int32_t displayId, HWC2::Layer* layer); 89 90 // Asks the HAL what it can do 91 status_t prepare(DisplayDevice& displayDevice); 92 93 status_t setClientTarget(int32_t displayId, uint32_t slot, 94 const sp<Fence>& acquireFence, 95 const sp<GraphicBuffer>& target, android_dataspace_t dataspace); 96 97 // Present layers to the display and read releaseFences. 98 status_t presentAndGetReleaseFences(int32_t displayId); 99 100 // set power mode 101 status_t setPowerMode(int32_t displayId, int mode); 102 103 // set active config 104 status_t setActiveConfig(int32_t displayId, size_t configId); 105 106 // Sets a color transform to be applied to the result of composition 107 status_t setColorTransform(int32_t displayId, const mat4& transform); 108 109 // reset state when an external, non-virtual display is disconnected 110 void disconnectDisplay(int32_t displayId); 111 112 // does this display have layers handled by HWC 113 bool hasDeviceComposition(int32_t displayId) const; 114 115 // does this display have layers handled by GLES 116 bool hasClientComposition(int32_t displayId) const; 117 118 // get the present fence received from the last call to present. 119 sp<Fence> getPresentFence(int32_t displayId) const; 120 121 // Get last release fence for the given layer 122 sp<Fence> getLayerReleaseFence(int32_t displayId, 123 HWC2::Layer* layer) const; 124 125 // Set the output buffer and acquire fence for a virtual display. 126 // Returns INVALID_OPERATION if displayId is not a virtual display. 127 status_t setOutputBuffer(int32_t displayId, const sp<Fence>& acquireFence, 128 const sp<GraphicBuffer>& buf); 129 130 // After SurfaceFlinger has retrieved the release fences for all the frames, 131 // it can call this to clear the shared pointers in the release fence map 132 void clearReleaseFences(int32_t displayId); 133 134 // Returns the HDR capabilities of the given display 135 std::unique_ptr<HdrCapabilities> getHdrCapabilities(int32_t displayId); 136 137 // Events handling --------------------------------------------------------- 138 139 // Returns true if successful, false otherwise. The 140 // DisplayDevice::DisplayType of the display is returned as an output param. 141 bool onVsync(hwc2_display_t displayId, int64_t timestamp, 142 int32_t* outDisplay); 143 void onHotplug(hwc2_display_t displayId, HWC2::Connection connection); 144 145 void setVsyncEnabled(int32_t displayId, HWC2::Vsync enabled); 146 147 // Query display parameters. Pass in a display index (e.g. 148 // HWC_DISPLAY_PRIMARY). 149 nsecs_t getRefreshTimestamp(int32_t displayId) const; 150 bool isConnected(int32_t displayId) const; 151 152 // 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; 155 156 std::shared_ptr<const HWC2::Display::Config> 157 getActiveConfig(int32_t displayId) const; 158 159 std::vector<android_color_mode_t> getColorModes(int32_t displayId) const; 160 161 status_t setActiveColorMode(int32_t displayId, android_color_mode_t mode); 162 163 bool isUsingVrComposer() const; 164 165 // for debugging ---------------------------------------------------------- 166 void dump(String8& out) const; 167 getComposer()168 android::Hwc2::Composer* getComposer() const { return mHwcDevice->getComposer(); } 169 private: 170 static const int32_t VIRTUAL_DISPLAY_ID_BASE = 2; 171 172 bool isValidDisplay(int32_t displayId) const; 173 static void validateChange(HWC2::Composition from, HWC2::Composition to); 174 175 struct cb_context; 176 177 struct DisplayData { 178 DisplayData(); 179 ~DisplayData(); 180 void reset(); 181 182 bool hasClientComposition; 183 bool hasDeviceComposition; 184 HWC2::Display* hwcDisplay; 185 HWC2::DisplayRequest displayRequests; 186 sp<Fence> lastPresentFence; // signals when the last set op retires 187 std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences; 188 buffer_handle_t outbufHandle; 189 sp<Fence> outbufAcquireFence; 190 mutable std::unordered_map<int32_t, 191 std::shared_ptr<const HWC2::Display::Config>> configMap; 192 193 // protected by mVsyncLock 194 HWC2::Vsync vsyncEnabled; 195 196 bool validateWasSkipped; 197 HWC2::Error presentError; 198 }; 199 200 std::unique_ptr<HWC2::Device> mHwcDevice; 201 std::vector<DisplayData> mDisplayData; 202 std::set<size_t> mFreeDisplaySlots; 203 std::unordered_map<hwc2_display_t, int32_t> mHwcDisplaySlots; 204 // protect mDisplayData from races between prepare and dump 205 mutable Mutex mDisplayLock; 206 207 cb_context* mCBContext; 208 size_t mVSyncCounts[HWC_NUM_PHYSICAL_DISPLAY_TYPES]; 209 uint32_t mRemainingHwcVirtualDisplays; 210 211 // protected by mLock 212 mutable Mutex mLock; 213 mutable std::unordered_map<int32_t, nsecs_t> mLastHwVSync; 214 215 // thread-safe 216 mutable Mutex mVsyncLock; 217 }; 218 219 // --------------------------------------------------------------------------- 220 }; // namespace android 221 222 #endif // ANDROID_SF_HWCOMPOSER_H 223 224 #endif // #ifdef USE_HWC2 225