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_GUESTCOMPOSER_H 18 #define ANDROID_HWC_GUESTCOMPOSER_H 19 20 #include "Common.h" 21 #include "Composer.h" 22 #include "DrmPresenter.h" 23 #include "Gralloc.h" 24 #include "Layer.h" 25 26 namespace android { 27 28 class GuestComposer : public Composer { 29 public: 30 GuestComposer() = default; 31 32 GuestComposer(const GuestComposer&) = delete; 33 GuestComposer& operator=(const GuestComposer&) = delete; 34 35 GuestComposer(GuestComposer&&) = delete; 36 GuestComposer& operator=(GuestComposer&&) = delete; 37 38 HWC2::Error init(const HotplugCallback& cb) override; 39 40 HWC2::Error createDisplays( 41 Device* device, 42 const AddDisplayToDeviceFunction& addDisplayToDeviceFn) override; 43 44 HWC2::Error createDisplay( 45 Device* device, uint32_t displayId, uint32_t width, uint32_t height, 46 uint32_t dpiX, uint32_t dpiY, uint32_t refreshRateHz, 47 const AddDisplayToDeviceFunction& addDisplayToDeviceFn) override; 48 49 HWC2::Error onDisplayDestroy(Display*) override; 50 onDisplayClientTargetSet(Display *)51 HWC2::Error onDisplayClientTargetSet(Display*) override { 52 return HWC2::Error::None; 53 } 54 55 // Determines if this composer can compose the given layers on the given 56 // display and requests changes for layers that can't not be composed. 57 HWC2::Error validateDisplay( 58 Display* display, std::unordered_map<hwc2_layer_t, HWC2::Composition>* 59 outLayerCompositionChanges) override; 60 61 // Performs the actual composition of layers and presents the composed result 62 // to the display. 63 HWC2::Error presentDisplay(Display* display, 64 int32_t* outPresentFence) override; 65 66 private: 67 struct DisplayConfig { 68 int width; 69 int height; 70 int dpiX; 71 int dpiY; 72 int refreshRateHz; 73 }; 74 75 HWC2::Error getDisplayConfigsFromDeviceConfig( 76 std::vector<DisplayConfig>* configs); 77 78 HWC2::Error getDisplayConfigsFromSystemProp( 79 std::vector<DisplayConfig>* configs); 80 81 // Returns true if the given layer's buffer has supported format. 82 bool canComposeLayer(Layer* layer); 83 84 // Composes the given layer into the given destination buffer. 85 HWC2::Error composeLayerInto(Layer* layer, std::uint8_t* dstBuffer, 86 std::uint32_t dstBufferWidth, 87 std::uint32_t dstBufferHeight, 88 std::uint32_t dstBufferStrideBytes, 89 std::uint32_t dstBufferBytesPerPixel); 90 91 struct GuestComposerDisplayInfo { 92 // Additional per display buffer for the composition result. 93 buffer_handle_t compositionResultBuffer = nullptr; 94 95 std::unique_ptr<DrmBuffer> compositionResultDrmBuffer; 96 }; 97 98 std::unordered_map<hwc2_display_t, GuestComposerDisplayInfo> mDisplayInfos; 99 100 Gralloc mGralloc; 101 102 DrmPresenter mDrmPresenter; 103 104 // Cuttlefish on QEMU does not have a display. Disable presenting to avoid 105 // spamming logcat with DRM commit failures. 106 bool mPresentDisabled = false; 107 108 uint8_t* getRotatingScratchBuffer(std::size_t neededSize, 109 std::uint32_t order); 110 uint8_t* getSpecialScratchBuffer(std::size_t neededSize); 111 112 std::vector<uint8_t> mScratchBuffer; 113 std::vector<uint8_t> mSpecialScratchBuffer; 114 }; 115 116 } // namespace android 117 118 #endif 119