1 /* 2 * Copyright 2021 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 ANDROID_HWC_HOSTCOMPOSER_H 18 #define ANDROID_HWC_HOSTCOMPOSER_H 19 20 #include <android-base/unique_fd.h> 21 22 #include <tuple> 23 #include <vector> 24 25 #include "Common.h" 26 #include "Composer.h" 27 #include "DrmPresenter.h" 28 #include "FencedBuffer.h" 29 #include "HostConnection.h" 30 31 namespace android { 32 33 class HostComposer : public Composer { 34 public: 35 HostComposer(DrmPresenter* drmPresenter, bool isMinigbm); 36 37 HostComposer(const HostComposer&) = delete; 38 HostComposer& operator=(const HostComposer&) = delete; 39 40 HostComposer(HostComposer&&) = delete; 41 HostComposer& operator=(HostComposer&&) = delete; 42 43 HWC2::Error init() override; 44 45 HWC2::Error onDisplayCreate(Display* display) override; 46 47 HWC2::Error onDisplayDestroy(Display* display) override; 48 49 HWC2::Error onDisplayClientTargetSet(Display* display) override; 50 51 // Determines if this composer can compose the given layers on the given 52 // display and requests changes for layers that can't not be composed. 53 HWC2::Error validateDisplay( 54 Display* display, std::unordered_map<hwc2_layer_t, HWC2::Composition>* 55 outLayerCompositionChanges) override; 56 57 // Performs the actual composition of layers and presents the composed result 58 // to the display. 59 std::tuple<HWC2::Error, base::unique_fd> presentDisplay( 60 Display* display) override; 61 62 HWC2::Error onActiveConfigChange(Display* display) override; 63 64 private: 65 HWC2::Error createHostComposerDisplayInfo(Display* display, 66 uint32_t hostDisplayId); 67 68 void post(HostConnection* hostCon, ExtendedRCEncoderContext* rcEnc, 69 buffer_handle_t h); 70 71 bool mIsMinigbm = false; 72 73 int mSyncDeviceFd = -1; 74 75 class CompositionResultBuffer { 76 public: 77 static std::unique_ptr<CompositionResultBuffer> create(int32_t width, 78 int32_t height); 79 static std::unique_ptr<CompositionResultBuffer> createWithDrmBuffer( 80 int32_t width, int32_t height, DrmPresenter&); 81 ~CompositionResultBuffer(); 82 83 DrmBuffer& waitAndGetDrmBuffer(); 84 buffer_handle_t waitAndGetBufferHandle(); 85 bool isReady() const; 86 void setFence(base::unique_fd fence); 87 88 private: 89 CompositionResultBuffer() = default; 90 91 void waitForFence(); 92 93 std::unique_ptr<FencedBuffer> mFencedBuffer; 94 // Drm info for the additional composition result buffer. 95 std::unique_ptr<DrmBuffer> mDrmBuffer; 96 }; 97 class HostComposerDisplayInfo { 98 public: 99 HostComposerDisplayInfo() = default; 100 void resetCompositionResultBuffers( 101 std::vector<std::unique_ptr<CompositionResultBuffer>>); 102 CompositionResultBuffer& getNextCompositionResultBuffer(); 103 104 uint32_t hostDisplayId = 0; 105 // Drm info for the displays client target buffer. 106 std::unique_ptr<DrmBuffer> clientTargetDrmBuffer; 107 108 private: 109 // Additional per display buffer for the composition result. 110 std::vector<std::unique_ptr<CompositionResultBuffer>> 111 compositionResultBuffers; 112 }; 113 114 std::unordered_map<hwc2_display_t, HostComposerDisplayInfo> mDisplayInfos; 115 DrmPresenter* mDrmPresenter; 116 }; 117 118 } // namespace android 119 120 #endif 121