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 "AlternatingImageStorage.h"
21 #include "Common.h"
22 #include "Display.h"
23 #include "DrmClient.h"
24 #include "DrmSwapchain.h"
25 #include "FrameComposer.h"
26 #include "Gralloc.h"
27 #include "Layer.h"
28 
29 namespace aidl::android::hardware::graphics::composer3::impl {
30 
31 class GuestFrameComposer : public FrameComposer {
32    public:
33     GuestFrameComposer() = default;
34 
35     GuestFrameComposer(const GuestFrameComposer&) = delete;
36     GuestFrameComposer& operator=(const GuestFrameComposer&) = delete;
37 
38     GuestFrameComposer(GuestFrameComposer&&) = delete;
39     GuestFrameComposer& operator=(GuestFrameComposer&&) = delete;
40 
41     HWC3::Error init() override;
42 
43     HWC3::Error registerOnHotplugCallback(const HotplugCallback& cb) override;
44 
45     HWC3::Error unregisterOnHotplugCallback() override;
46 
47     HWC3::Error onDisplayCreate(Display*) override;
48 
49     HWC3::Error onDisplayDestroy(Display*) override;
50 
51     HWC3::Error onDisplayClientTargetSet(Display*) override;
52 
53     // Determines if this composer can compose the given layers on the given
54     // display and requests changes for layers that can't not be composed.
55     HWC3::Error validateDisplay(Display* display, DisplayChanges* outChanges) override;
56 
57     // Performs the actual composition of layers and presents the composed result
58     // to the display.
59     HWC3::Error presentDisplay(
60         Display* display, ::android::base::unique_fd* outDisplayFence,
61         std::unordered_map<int64_t, ::android::base::unique_fd>* outLayerFences) override;
62 
63     HWC3::Error onActiveConfigChange(Display* /*display*/) override;
64 
getDrmPresenter()65     const DrmClient* getDrmPresenter() const override { return &mDrmClient; }
66 
67    private:
68     struct DisplayConfig {
69         int width;
70         int height;
71         int dpiX;
72         int dpiY;
73         int refreshRateHz;
74     };
75 
76     HWC3::Error getDisplayConfigsFromSystemProp(std::vector<DisplayConfig>* configs);
77 
78     // Returns true if the given layer's buffer has supported format.
79     bool canComposeLayer(Layer* layer);
80 
81     // Composes the given layer into the given destination buffer.
82     HWC3::Error composeLayerInto(AlternatingImageStorage& storage, Layer* layer,
83                                  std::uint8_t* dstBuffer, std::uint32_t dstBufferWidth,
84                                  std::uint32_t dstBufferHeight, std::uint32_t dstBufferStrideBytes,
85                                  std::uint32_t dstBufferBytesPerPixel);
86 
87     struct DisplayInfo {
88         // Additional per display buffers for the composition result.
89         std::unique_ptr<DrmSwapchain> swapchain = {};
90 
91         // Scratch storage space for intermediate images during composition.
92         AlternatingImageStorage compositionIntermediateStorage;
93     };
94 
95 
96     std::unordered_map<int64_t, DisplayInfo> mDisplayInfos;
97 
98     Gralloc mGralloc;
99 
100     DrmClient mDrmClient;
101 
102     // Cuttlefish on QEMU does not have a display. Disable presenting to avoid
103     // spamming logcat with DRM commit failures.
104     bool mPresentDisabled = false;
105 
106     HWC3::Error applyColorTransformToRGBA(const std::array<float, 16>& colorTransform,  //
107                                           std::uint8_t* buffer,                         //
108                                           std::uint32_t bufferWidth,                    //
109                                           std::uint32_t bufferHeight,                   //
110                                           std::uint32_t bufferStrideBytes);
111 };
112 
113 }  // namespace aidl::android::hardware::graphics::composer3::impl
114 
115 #endif
116