1 /* 2 * Copyright (c) 2019-2021, The Linux Foundation. All rights reserved. 3 * Not a Contribution. 4 * 5 * Copyright (C) 2017 The Android Open Source Project 6 * 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 */ 19 20 #ifndef __QTICOMPOSERCLIENT_H__ 21 #define __QTICOMPOSERCLIENT_H__ 22 23 #include <vendor/qti/hardware/display/composer/3.0/IQtiComposerClient.h> 24 #include <hidl/MQDescriptor.h> 25 #include <hidl/Status.h> 26 #include <log/log.h> 27 #include <unordered_set> 28 #include <vector> 29 #include <string> 30 31 #include "hwc_session.h" 32 #include "QtiComposerCommandBuffer.h" 33 #include "QtiComposerHandleImporter.h" 34 35 namespace vendor { 36 namespace qti { 37 namespace hardware { 38 namespace display { 39 namespace composer { 40 namespace V3_0 { 41 namespace implementation { 42 43 namespace common_V1_0 = ::android::hardware::graphics::common::V1_0; 44 namespace common_V1_1 = ::android::hardware::graphics::common::V1_1; 45 namespace common_V1_2 = ::android::hardware::graphics::common::V1_2; 46 47 namespace composer_V2_1 = ::android::hardware::graphics::composer::V2_1; 48 namespace composer_V2_2 = ::android::hardware::graphics::composer::V2_2; 49 namespace composer_V2_3 = ::android::hardware::graphics::composer::V2_3; 50 namespace composer_V2_4 = ::android::hardware::graphics::composer::V2_4; 51 52 using PerFrameMetadataKey_V2 = composer_V2_2::IComposerClient::PerFrameMetadataKey; 53 using PerFrameMetadataKey = composer_V2_3::IComposerClient::PerFrameMetadataKey; 54 55 using ::android::hardware::MQDescriptorSync; 56 using ::android::hardware::hidl_string; 57 using ::android::hardware::hidl_vec; 58 using ::android::hardware::Return; 59 using ::android::hardware::Void; 60 using ::android::sp; 61 using ::android::hardware::hidl_handle; 62 using ::android::hardware::hidl_bitfield; 63 using ::android::hardware::graphics::composer::V2_1::Error; 64 65 using sdm::HWCSession; 66 using sdm::Fence; 67 68 class BufferCacheEntry { 69 public: 70 BufferCacheEntry(); 71 BufferCacheEntry(BufferCacheEntry&& other); 72 73 BufferCacheEntry(const BufferCacheEntry& other) = delete; 74 BufferCacheEntry& operator=(const BufferCacheEntry& other) = delete; 75 76 BufferCacheEntry& operator=(buffer_handle_t handle); 77 ~BufferCacheEntry(); 78 getHandle()79 buffer_handle_t getHandle() const { return mHandle; } 80 81 private: 82 void clear(); 83 84 buffer_handle_t mHandle; 85 }; 86 87 class QtiComposerClient : public IQtiComposerClient { 88 QtiComposerClient(); 89 virtual ~QtiComposerClient(); 90 static QtiComposerClient* qti_composerclient_instance_; 91 92 public: 93 // Methods from ::android::hardware::graphics::composer::V2_1::IComposerClient follow. 94 Return<void> registerCallback(const sp<composer_V2_1::IComposerCallback>& callback) override; 95 Return<uint32_t> getMaxVirtualDisplayCount() override; 96 Return<void> createVirtualDisplay(uint32_t width, uint32_t height, 97 common_V1_0::PixelFormat formatHint, 98 uint32_t outputBufferSlotCount, 99 createVirtualDisplay_cb _hidl_cb) override; 100 Return<Error> destroyVirtualDisplay(uint64_t display) override; 101 Return<void> createLayer(uint64_t display, uint32_t bufferSlotCount, 102 createLayer_cb _hidl_cb) override; 103 Return<Error> destroyLayer(uint64_t display, uint64_t layer) override; 104 Return<void> getActiveConfig(uint64_t display, getActiveConfig_cb _hidl_cb) override; 105 Return<Error> getClientTargetSupport(uint64_t display, uint32_t width, uint32_t height, 106 common_V1_0::PixelFormat format, 107 common_V1_0::Dataspace dataspace) override; 108 Return<void> getColorModes(uint64_t display, getColorModes_cb _hidl_cb) override; 109 Return<void> getDisplayAttribute(uint64_t display, uint32_t config, 110 composer_V2_1::IComposerClient::Attribute attribute, 111 getDisplayAttribute_cb _hidl_cb) override; 112 Return<void> getDisplayConfigs(uint64_t display, getDisplayConfigs_cb _hidl_cb) override; 113 Return<void> getDisplayName(uint64_t display, getDisplayName_cb _hidl_cb) override; 114 Return<void> getDisplayType(uint64_t display, getDisplayType_cb _hidl_cb) override; 115 Return<void> getDozeSupport(uint64_t display, getDozeSupport_cb _hidl_cb) override; 116 Return<void> getHdrCapabilities(uint64_t display, getHdrCapabilities_cb _hidl_cb) override; 117 Return<Error> setClientTargetSlotCount(uint64_t display, 118 uint32_t clientTargetSlotCount) override; 119 Return<Error> setActiveConfig(uint64_t display, uint32_t config) override; 120 Return<Error> setColorMode(uint64_t display, common_V1_0::ColorMode mode) override; 121 Return<Error> setPowerMode(uint64_t display, 122 composer_V2_1::IComposerClient::PowerMode mode) override; 123 Return<Error> setVsyncEnabled(uint64_t display, 124 composer_V2_1::IComposerClient::Vsync enabled) override; 125 Return<Error> setInputCommandQueue(const MQDescriptorSync<uint32_t>& descriptor) override; 126 Return<void> getOutputCommandQueue(getOutputCommandQueue_cb _hidl_cb) override; 127 Return<void> executeCommands(uint32_t inLength, const hidl_vec<hidl_handle>& inHandles, 128 executeCommands_cb _hidl_cb) override; 129 130 // Methods from ::android::hardware::graphics::composer::V2_2::IComposerClient follow. 131 Return<void> getPerFrameMetadataKeys(uint64_t display, 132 getPerFrameMetadataKeys_cb _hidl_cb) override; 133 Return<void> getReadbackBufferAttributes(uint64_t display, 134 getReadbackBufferAttributes_cb _hidl_cb) override; 135 Return<void> getReadbackBufferFence(uint64_t display, 136 getReadbackBufferFence_cb _hidl_cb) override; 137 Return<Error> setReadbackBuffer(uint64_t display, const hidl_handle& buffer, 138 const hidl_handle& releaseFence) override; 139 Return<void> createVirtualDisplay_2_2(uint32_t width, uint32_t height, 140 common_V1_1::PixelFormat formatHint, 141 uint32_t outputBufferSlotCount, 142 createVirtualDisplay_2_2_cb _hidl_cb) override; 143 Return<Error> getClientTargetSupport_2_2(uint64_t display, uint32_t width, uint32_t height, 144 common_V1_1::PixelFormat format, 145 common_V1_1::Dataspace dataspace) override; 146 Return<Error> setPowerMode_2_2(uint64_t display, 147 composer_V2_2::IComposerClient::PowerMode mode) override; 148 Return<void> getColorModes_2_2(uint64_t display, getColorModes_2_2_cb _hidl_cb) override; 149 Return<void> getRenderIntents(uint64_t display, common_V1_1::ColorMode mode, 150 getRenderIntents_cb _hidl_cb) override; 151 Return<Error> setColorMode_2_2(uint64_t display, common_V1_1::ColorMode mode, 152 common_V1_1::RenderIntent intent) override; 153 Return<void> getDataspaceSaturationMatrix(common_V1_1::Dataspace dataspace, 154 getDataspaceSaturationMatrix_cb _hidl_cb) override; 155 Return<void> executeCommands_2_2(uint32_t inLength, const hidl_vec<hidl_handle>& inHandles, 156 executeCommands_2_2_cb _hidl_cb) override; 157 158 // Methods from ::android::hardware::graphics::composer::V2_3::IComposerClient follow. 159 Return<void> getDisplayIdentificationData(uint64_t display, 160 getDisplayIdentificationData_cb _hidl_cb) override; 161 Return<void> getReadbackBufferAttributes_2_3(uint64_t display, 162 getReadbackBufferAttributes_2_3_cb _hidl_cb) override; 163 Return<Error> getClientTargetSupport_2_3(uint64_t display, uint32_t width, uint32_t height, 164 common_V1_2::PixelFormat format, 165 common_V1_2::Dataspace dataspace) override; 166 Return<void> getDisplayedContentSamplingAttributes(uint64_t display, 167 getDisplayedContentSamplingAttributes_cb _hidl_cb) override; 168 Return<Error> setDisplayedContentSamplingEnabled(uint64_t display, 169 composer_V2_3::IComposerClient::DisplayedContentSampling enable, 170 hidl_bitfield<FormatColorComponent> componentMask, 171 uint64_t maxFrames) override; 172 Return<void> getDisplayedContentSample(uint64_t display, uint64_t maxFrames, 173 uint64_t timestamp, 174 getDisplayedContentSample_cb _hidl_cb) override; 175 Return<void> executeCommands_2_3(uint32_t inLength, const hidl_vec<hidl_handle>& inHandles, 176 executeCommands_2_3_cb _hidl_cb) override; 177 Return<void> getRenderIntents_2_3(uint64_t display, common_V1_2::ColorMode mode, 178 getRenderIntents_2_3_cb _hidl_cb) override; 179 Return<void> getColorModes_2_3(uint64_t display, getColorModes_2_3_cb _hidl_cb) override; 180 Return<Error> setColorMode_2_3(uint64_t display, common_V1_2::ColorMode mode, 181 common_V1_1::RenderIntent intent) override; 182 Return<void> getDisplayCapabilities(uint64_t display, 183 getDisplayCapabilities_cb _hidl_cb) override; 184 Return<void> getPerFrameMetadataKeys_2_3(uint64_t display, 185 getPerFrameMetadataKeys_2_3_cb _hidl_cb) override; 186 Return<void> getHdrCapabilities_2_3(uint64_t display, 187 getHdrCapabilities_2_3_cb _hidl_cb) override; 188 Return<void> getDisplayBrightnessSupport(uint64_t display, 189 getDisplayBrightnessSupport_cb _hidl_cb) override; 190 Return<Error> setDisplayBrightness(uint64_t display, float brightness) override; 191 192 // Methods from ::android::hardware::graphics::composer::V2_4::IComposerClient follow. 193 Return<void> registerCallback_2_4(const sp<composer_V2_4::IComposerCallback> &callback) override; 194 Return<void> getDisplayCapabilities_2_4(uint64_t display, 195 getDisplayCapabilities_2_4_cb _hidl_cb) override; 196 Return<void> getDisplayConnectionType(uint64_t display, 197 getDisplayConnectionType_cb _hidl_cb) override; 198 Return<void> getDisplayAttribute_2_4(uint64_t display, uint32_t config, 199 composer_V2_4::IComposerClient::Attribute attribute, 200 getDisplayAttribute_2_4_cb _hidl_cb) override; 201 202 Return<void> getDisplayVsyncPeriod(uint64_t display, getDisplayVsyncPeriod_cb _hidl_cb) override; 203 Return<void> setActiveConfigWithConstraints( 204 uint64_t display, uint32_t config, 205 const VsyncPeriodChangeConstraints &vsyncPeriodChangeConstraints, 206 setActiveConfigWithConstraints_cb _hidl_cb) override; 207 208 Return<composer_V2_4::Error> setAutoLowLatencyMode(uint64_t display, bool on) override; 209 210 Return<void> getSupportedContentTypes(uint64_t display, 211 getSupportedContentTypes_cb _hidl_cb) override; 212 Return<composer_V2_4::Error> setContentType( 213 uint64_t display, composer_V2_4::IComposerClient::ContentType type) override; 214 Return<void> getLayerGenericMetadataKeys(getLayerGenericMetadataKeys_cb _hidl_cb) override; 215 216 // Methods for RegisterCallback 217 void enableCallback(bool enable); 218 static void onHotplug(hwc2_callback_data_t callbackData, hwc2_display_t display, 219 int32_t connected); 220 static void onRefresh(hwc2_callback_data_t callbackData, hwc2_display_t display); 221 static void onVsync(hwc2_callback_data_t callbackData, hwc2_display_t display, 222 int64_t timestamp); 223 static void onVsync_2_4(hwc2_callback_data_t callbackData, hwc2_display_t display, 224 int64_t timestamp, VsyncPeriodNanos vsyncPeriodNanos); 225 static void onVsyncPeriodTimingChanged(hwc2_callback_data_t callbackData, 226 hwc2_display_t display, 227 hwc_vsync_period_change_timeline_t* updatedTimeline); 228 static void onSeamlessPossible(hwc2_callback_data_t callbackData, hwc2_display_t display); 229 230 // Methods for ConcurrentWriteBack 231 hidl_handle getFenceHandle(const shared_ptr<Fence>& fence, char* handleStorage); 232 Error getFence(const hidl_handle& fenceHandle, shared_ptr<sdm::Fence>* outFence, 233 const string& name); 234 Error getDisplayReadbackBuffer(Display display, const native_handle_t* rawHandle, 235 const native_handle_t** outHandle); 236 237 // Methods to check support of specific features like skip_validate 238 std::unordered_set<hwc2_capability_t> mCapabilities; 239 void getCapabilities(); hasCapability(hwc2_capability_t capability)240 bool hasCapability(hwc2_capability_t capability) { 241 return (mCapabilities.count(capability) > 0); 242 } 243 CreateQtiComposerClientInstance()244 static QtiComposerClient* CreateQtiComposerClientInstance() { 245 if (!qti_composerclient_instance_) { 246 qti_composerclient_instance_ = new QtiComposerClient(); 247 return qti_composerclient_instance_; 248 } 249 return nullptr; 250 } 251 onLastStrongRef(const void * id)252 void onLastStrongRef(const void* id) { 253 if (qti_composerclient_instance_) { 254 qti_composerclient_instance_ = nullptr; 255 } 256 } 257 258 private: 259 struct LayerBuffers { 260 std::vector<BufferCacheEntry> Buffers; 261 // the handle is a sideband stream handle, not a buffer handle 262 BufferCacheEntry SidebandStream; 263 }; 264 265 struct DisplayData { 266 bool IsVirtual; 267 268 std::vector<BufferCacheEntry> ClientTargets; 269 std::vector<BufferCacheEntry> OutputBuffers; 270 271 std::unordered_map<Layer, LayerBuffers> Layers; 272 DisplayDataDisplayData273 explicit DisplayData(bool isVirtual) : IsVirtual(isVirtual) {} 274 }; 275 276 class CommandReader : public CommandReaderBase { 277 public: 278 explicit CommandReader(QtiComposerClient& client); 279 Error parse(); 280 Error validateDisplay(Display display, std::vector<Layer>& changedLayers, 281 std::vector<IComposerClient::Composition>& compositionTypes, 282 uint32_t& displayRequestMask, std::vector<Layer>& requestedLayers, 283 std::vector<uint32_t>& requestMasks, 284 IComposerClient::ClientTargetProperty& clienttargetproperty); 285 Error presentDisplay(Display display, shared_ptr<Fence>* presentFence, 286 std::vector<Layer>& layers, 287 std::vector<shared_ptr<Fence>>& releaseFences); 288 289 private: 290 // Commands from ::android::hardware::graphics::composer::V2_1::IComposerClient follow. 291 bool parseSelectDisplay(uint16_t length); 292 bool parseSelectLayer(uint16_t length); 293 bool parseSetColorTransform(uint16_t length); 294 bool parseSetClientTarget(uint16_t length); 295 bool parseSetOutputBuffer(uint16_t length); 296 bool parseValidateDisplay(uint16_t length); 297 bool parseAcceptDisplayChanges(uint16_t length); 298 bool parsePresentDisplay(uint16_t length); 299 bool parsePresentOrValidateDisplay(uint16_t length); 300 bool parseSetLayerCursorPosition(uint16_t length); 301 bool parseSetLayerBuffer(uint16_t length); 302 bool parseSetLayerSurfaceDamage(uint16_t length); 303 bool parseSetLayerBlendMode(uint16_t length); 304 bool parseSetLayerColor(uint16_t length); 305 bool parseSetLayerCompositionType(uint16_t length); 306 bool parseSetLayerDataspace(uint16_t length); 307 bool parseSetLayerDisplayFrame(uint16_t length); 308 bool parseSetLayerPlaneAlpha(uint16_t length); 309 bool parseSetLayerSidebandStream(uint16_t length); 310 bool parseSetLayerSourceCrop(uint16_t length); 311 bool parseSetLayerTransform(uint16_t length); 312 bool parseSetLayerVisibleRegion(uint16_t length); 313 bool parseSetLayerZOrder(uint16_t length); 314 bool parseSetLayerType(uint16_t length); 315 316 // Commands from ::android::hardware::graphics::composer::V2_2::IComposerClient follow. 317 bool parseSetLayerPerFrameMetadata(uint16_t length); 318 bool parseSetLayerFloatColor(uint16_t length); 319 320 // Commands from ::android::hardware::graphics::composer::V2_3::IComposerClient follow. 321 bool parseSetLayerColorTransform(uint16_t length); 322 bool parseSetLayerPerFrameMetadataBlobs(uint16_t length); 323 bool parseSetDisplayElapseTime(uint16_t length); 324 325 bool parseCommonCmd(IComposerClient::Command command, uint16_t length); 326 327 hwc_rect_t readRect(); 328 std::vector<hwc_rect_t> readRegion(size_t count); 329 hwc_frect_t readFRect(); 330 QtiComposerClient& mClient; 331 CommandWriter& mWriter; 332 Display mDisplay; 333 Layer mLayer; 334 335 // Buffer cache impl 336 enum class BufferCache { 337 CLIENT_TARGETS, 338 OUTPUT_BUFFERS, 339 LAYER_BUFFERS, 340 LAYER_SIDEBAND_STREAMS, 341 }; 342 343 Error lookupBufferCacheEntryLocked(BufferCache cache, uint32_t slot, 344 BufferCacheEntry** outEntry); 345 Error lookupBuffer(BufferCache cache, uint32_t slot, bool useCache, buffer_handle_t handle, 346 buffer_handle_t* outHandle); 347 Error updateBuffer(BufferCache cache, uint32_t slot, bool useCache, buffer_handle_t handle); 348 lookupLayerSidebandStream(buffer_handle_t handle,buffer_handle_t * outHandle)349 Error lookupLayerSidebandStream(buffer_handle_t handle, buffer_handle_t* outHandle) { 350 return lookupBuffer(BufferCache::LAYER_SIDEBAND_STREAMS, 0, false, handle, outHandle); 351 } updateLayerSidebandStream(buffer_handle_t handle)352 Error updateLayerSidebandStream(buffer_handle_t handle) { 353 return updateBuffer(BufferCache::LAYER_SIDEBAND_STREAMS, 0, false, handle); 354 } 355 }; 356 357 HWCSession *hwc_session_ = nullptr; 358 sp<composer_V2_1::IComposerCallback> callback_ = nullptr; 359 sp<composer_V2_4::IComposerCallback> callback24_ = nullptr; 360 bool mUseCallback24_ = false; 361 std::mutex mCommandMutex; 362 // 64KiB minus a small space for metadata such as read/write pointers */ 363 static constexpr size_t kWriterInitialSize = 64 * 1024 / sizeof(uint32_t) - 16; 364 CommandWriter mWriter; 365 CommandReader mReader; 366 std::mutex mDisplayDataMutex; 367 std::unordered_map<Display, DisplayData> mDisplayData; 368 }; 369 370 extern "C" IQtiComposerClient* HIDL_FETCH_IQtiComposerClient(const char* name); 371 372 } // namespace implementation 373 } // namespace V3_0 374 } // namespace composer 375 } // namespace display 376 } // namespace hardware 377 } // namespace qti 378 } // namespace vendor 379 380 #endif // __QTICOMPOSERCLIENT_H__ 381