• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 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_GUESTFRAMECOMPOSER_H
18 #define ANDROID_HWC_GUESTFRAMECOMPOSER_H
19 
20 #include "Common.h"
21 #include "Display.h"
22 #include "DrmPresenter.h"
23 #include "FrameComposer.h"
24 #include "Gralloc.h"
25 #include "Layer.h"
26 
27 namespace aidl::android::hardware::graphics::composer3::impl {
28 
29 class GuestFrameComposer : public FrameComposer {
30  public:
31   GuestFrameComposer() = default;
32 
33   GuestFrameComposer(const GuestFrameComposer&) = delete;
34   GuestFrameComposer& operator=(const GuestFrameComposer&) = delete;
35 
36   GuestFrameComposer(GuestFrameComposer&&) = delete;
37   GuestFrameComposer& operator=(GuestFrameComposer&&) = delete;
38 
39   HWC3::Error init() override;
40 
41   HWC3::Error registerOnHotplugCallback(const HotplugCallback& cb) override;
42 
43   HWC3::Error unregisterOnHotplugCallback() override;
44 
45   HWC3::Error onDisplayCreate(Display*) override;
46 
47   HWC3::Error onDisplayDestroy(Display*) override;
48 
49   HWC3::Error onDisplayClientTargetSet(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   HWC3::Error validateDisplay(Display* display,
54                               DisplayChanges* outChanges) override;
55 
56   // Performs the actual composition of layers and presents the composed result
57   // to the display.
58   HWC3::Error presentDisplay(
59       Display* display, ::android::base::unique_fd* outDisplayFence,
60       std::unordered_map<int64_t, ::android::base::unique_fd>* outLayerFences)
61       override;
62 
63   HWC3::Error onActiveConfigChange(Display* /*display*/) override;
64 
getDrmPresenter()65   const DrmPresenter* getDrmPresenter() const override {
66     return &mDrmPresenter;
67   }
68 
69  private:
70   struct DisplayConfig {
71     int width;
72     int height;
73     int dpiX;
74     int dpiY;
75     int refreshRateHz;
76   };
77 
78   HWC3::Error getDisplayConfigsFromDeviceConfig(
79       std::vector<DisplayConfig>* configs);
80 
81   HWC3::Error getDisplayConfigsFromSystemProp(
82       std::vector<DisplayConfig>* configs);
83 
84   // Returns true if the given layer's buffer has supported format.
85   bool canComposeLayer(Layer* layer);
86 
87   // Composes the given layer into the given destination buffer.
88   HWC3::Error composeLayerInto(Layer* layer, std::uint8_t* dstBuffer,
89                                std::uint32_t dstBufferWidth,
90                                std::uint32_t dstBufferHeight,
91                                std::uint32_t dstBufferStrideBytes,
92                                std::uint32_t dstBufferBytesPerPixel);
93 
94   struct DisplayInfo {
95     // Additional per display buffer for the composition result.
96     buffer_handle_t compositionResultBuffer = nullptr;
97 
98     std::shared_ptr<DrmBuffer> compositionResultDrmBuffer;
99   };
100 
101   std::unordered_map<int64_t, DisplayInfo> mDisplayInfos;
102 
103   Gralloc mGralloc;
104 
105   DrmPresenter mDrmPresenter;
106 
107   // Cuttlefish on QEMU does not have a display. Disable presenting to avoid
108   // spamming logcat with DRM commit failures.
109   bool mPresentDisabled = false;
110 
111   uint8_t* getRotatingScratchBuffer(std::size_t neededSize,
112                                     std::uint32_t order);
113   uint8_t* getSpecialScratchBuffer(std::size_t neededSize);
114 
115   HWC3::Error applyColorTransformToRGBA(
116       const std::array<float, 16>& colorTransform,  //
117       std::uint8_t* buffer,                         //
118       std::uint32_t bufferWidth,                    //
119       std::uint32_t bufferHeight,                   //
120       std::uint32_t bufferStrideBytes);
121 
122   std::vector<uint8_t> mScratchBuffer;
123   std::vector<uint8_t> mSpecialScratchBuffer;
124 };
125 
126 }  // namespace aidl::android::hardware::graphics::composer3::impl
127 
128 #endif
129