1 /* 2 * Copyright 2018 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 #pragma once 18 19 #ifndef LOG_TAG 20 #warning "ComposerResources.h included without LOG_TAG" 21 #endif 22 23 #include <memory> 24 #include <mutex> 25 #include <unordered_map> 26 #include <vector> 27 28 #include <android/hardware/graphics/composer/2.1/types.h> 29 30 #include <log/log.h> 31 32 namespace android { 33 class GraphicBufferMapper; 34 namespace hardware { 35 namespace graphics { 36 namespace composer { 37 namespace V2_1 { 38 namespace hal { 39 40 // wrapper for IMapper to import buffers and sideband streams 41 class ComposerHandleImporter { 42 public: 43 ComposerHandleImporter(); 44 bool init(); 45 46 Error importBuffer(const native_handle_t* rawHandle, const native_handle_t** outBufferHandle); 47 void freeBuffer(const native_handle_t* bufferHandle); 48 Error importStream(const native_handle_t* rawHandle, const native_handle_t** outStreamHandle); 49 void freeStream(const native_handle_t* streamHandle); 50 51 private: 52 GraphicBufferMapper& mMapper; 53 }; 54 55 class ComposerHandleCache { 56 public: 57 enum class HandleType { 58 INVALID, 59 BUFFER, 60 STREAM, 61 }; 62 63 ComposerHandleCache(ComposerHandleImporter& importer, HandleType type, uint32_t cacheSize); 64 65 // must be initialized later with initCache 66 ComposerHandleCache(ComposerHandleImporter& importer); 67 68 ~ComposerHandleCache(); 69 70 ComposerHandleCache(const ComposerHandleCache&) = delete; 71 ComposerHandleCache& operator=(const ComposerHandleCache&) = delete; 72 73 bool initCache(HandleType type, uint32_t cacheSize); 74 size_t getCacheSize() const; 75 Error lookupCache(uint32_t slot, const native_handle_t** outHandle); 76 Error updateCache(uint32_t slot, const native_handle_t* handle, 77 const native_handle** outReplacedHandle); 78 79 // when fromCache is true, look up in the cache; otherwise, update the cache 80 Error getHandle(uint32_t slot, bool fromCache, const native_handle_t* inHandle, 81 const native_handle_t** outHandle, const native_handle** outReplacedHandle); 82 83 private: 84 ComposerHandleImporter& mImporter; 85 HandleType mHandleType = HandleType::INVALID; 86 std::vector<const native_handle_t*> mHandles; 87 }; 88 89 // layer resource 90 class ComposerLayerResource { 91 public: 92 ComposerLayerResource(ComposerHandleImporter& importer, uint32_t bufferCacheSize); 93 94 virtual ~ComposerLayerResource() = default; 95 96 Error getBuffer(uint32_t slot, bool fromCache, const native_handle_t* inHandle, 97 const native_handle_t** outHandle, const native_handle** outReplacedHandle); 98 Error getSidebandStream(uint32_t slot, bool fromCache, const native_handle_t* inHandle, 99 const native_handle_t** outHandle, 100 const native_handle** outReplacedHandle); 101 102 protected: 103 ComposerHandleCache mBufferCache; 104 ComposerHandleCache mSidebandStreamCache; 105 }; 106 107 // display resource 108 class ComposerDisplayResource { 109 public: 110 enum class DisplayType { 111 PHYSICAL, 112 VIRTUAL, 113 }; 114 115 virtual ~ComposerDisplayResource() = default; 116 117 ComposerDisplayResource(DisplayType type, ComposerHandleImporter& importer, 118 uint32_t outputBufferCacheSize); 119 120 bool initClientTargetCache(uint32_t cacheSize); 121 size_t getClientTargetCacheSize() const; 122 size_t getOutputBufferCacheSize() const; 123 bool isVirtual() const; 124 125 Error getClientTarget(uint32_t slot, bool fromCache, const native_handle_t* inHandle, 126 const native_handle_t** outHandle, 127 const native_handle** outReplacedHandle); 128 129 Error getOutputBuffer(uint32_t slot, bool fromCache, const native_handle_t* inHandle, 130 const native_handle_t** outHandle, 131 const native_handle** outReplacedHandle); 132 133 bool addLayer(Layer layer, std::unique_ptr<ComposerLayerResource> layerResource); 134 bool removeLayer(Layer layer); 135 ComposerLayerResource* findLayerResource(Layer layer); 136 std::vector<Layer> getLayers() const; 137 138 void setMustValidateState(bool mustValidate); 139 140 bool mustValidate() const; 141 142 protected: 143 const DisplayType mType; 144 ComposerHandleCache mClientTargetCache; 145 ComposerHandleCache mOutputBufferCache; 146 bool mMustValidate; 147 148 std::unordered_map<Layer, std::unique_ptr<ComposerLayerResource>> mLayerResources; 149 }; 150 151 class ComposerResources { 152 public: 153 static std::unique_ptr<ComposerResources> create(); 154 155 ComposerResources() = default; 156 virtual ~ComposerResources() = default; 157 158 bool init(); 159 160 using RemoveDisplay = 161 std::function<void(Display display, bool isVirtual, const std::vector<Layer>& layers)>; 162 void clear(RemoveDisplay removeDisplay); 163 164 bool hasDisplay(Display display); 165 Error addPhysicalDisplay(Display display); 166 Error addVirtualDisplay(Display display, uint32_t outputBufferCacheSize); 167 168 Error removeDisplay(Display display); 169 170 Error setDisplayClientTargetCacheSize(Display display, uint32_t clientTargetCacheSize); 171 Error getDisplayClientTargetCacheSize(Display display, size_t* outCacheSize); 172 Error getDisplayOutputBufferCacheSize(Display display, size_t* outCacheSize); 173 174 Error addLayer(Display display, Layer layer, uint32_t bufferCacheSize); 175 Error removeLayer(Display display, Layer layer); 176 177 void setDisplayMustValidateState(Display display, bool mustValidate); 178 179 bool mustValidateDisplay(Display display); 180 181 // When a buffer in the cache is replaced by a new one, we must keep it 182 // alive until it has been replaced in ComposerHal because it is still using 183 // the old buffer. 184 class ReplacedHandle { 185 public: ReplacedHandle(bool isBuffer)186 explicit ReplacedHandle(bool isBuffer) : mIsBuffer(isBuffer) {} 187 ReplacedHandle(const ReplacedHandle&) = delete; 188 ReplacedHandle& operator=(const ReplacedHandle&) = delete; 189 ~ReplacedHandle()190 ~ReplacedHandle() { reset(); } 191 isBuffer()192 bool isBuffer() { return mIsBuffer; } 193 194 void reset(ComposerHandleImporter* importer = nullptr, 195 const native_handle_t* handle = nullptr) { 196 if (mHandle) { 197 if (mIsBuffer) { 198 mImporter->freeBuffer(mHandle); 199 } else { 200 mImporter->freeStream(mHandle); 201 } 202 } 203 204 mImporter = importer; 205 mHandle = handle; 206 } 207 208 private: 209 bool mIsBuffer; 210 ComposerHandleImporter* mImporter = nullptr; 211 const native_handle_t* mHandle = nullptr; 212 }; 213 214 Error getDisplayClientTarget(Display display, uint32_t slot, bool fromCache, 215 const native_handle_t* rawHandle, 216 const native_handle_t** outBufferHandle, 217 ReplacedHandle* outReplacedBuffer); 218 219 Error getDisplayOutputBuffer(Display display, uint32_t slot, bool fromCache, 220 const native_handle_t* rawHandle, 221 const native_handle_t** outBufferHandle, 222 ReplacedHandle* outReplacedBuffer); 223 224 Error getLayerBuffer(Display display, Layer layer, uint32_t slot, bool fromCache, 225 const native_handle_t* rawHandle, const native_handle_t** outBufferHandle, 226 ReplacedHandle* outReplacedBuffer); 227 228 Error getLayerSidebandStream(Display display, Layer layer, const native_handle_t* rawHandle, 229 const native_handle_t** outStreamHandle, 230 ReplacedHandle* outReplacedStream); 231 232 protected: 233 virtual std::unique_ptr<ComposerDisplayResource> createDisplayResource( 234 ComposerDisplayResource::DisplayType type, uint32_t outputBufferCacheSize); 235 236 virtual std::unique_ptr<ComposerLayerResource> createLayerResource(uint32_t bufferCacheSize); 237 238 ComposerDisplayResource* findDisplayResourceLocked(Display display); 239 240 ComposerHandleImporter mImporter; 241 242 std::mutex mDisplayResourcesMutex; 243 std::unordered_map<Display, std::unique_ptr<ComposerDisplayResource>> mDisplayResources; 244 245 private: 246 enum class Cache { 247 CLIENT_TARGET, 248 OUTPUT_BUFFER, 249 LAYER_BUFFER, 250 LAYER_SIDEBAND_STREAM, 251 }; 252 253 Error getHandle(Display display, Layer layer, uint32_t slot, Cache cache, bool fromCache, 254 const native_handle_t* rawHandle, const native_handle_t** outHandle, 255 ReplacedHandle* outReplacedHandle); 256 }; 257 258 } // namespace hal 259 } // namespace V2_1 260 } // namespace composer 261 } // namespace graphics 262 } // namespace hardware 263 } // namespace android 264