1 /*
2  * Copyright 2016 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_HARDWARE_GRAPHICS_COMPOSER_V2_1_COMPOSER_CLIENT_H
18 #define ANDROID_HARDWARE_GRAPHICS_COMPOSER_V2_1_COMPOSER_CLIENT_H
19 
20 #include <mutex>
21 #include <unordered_map>
22 #include <vector>
23 
24 #include <hardware/hwcomposer2.h>
25 #include "IComposerCommandBuffer.h"
26 #include "ComposerBase.h"
27 
28 namespace android {
29 namespace hardware {
30 namespace graphics {
31 namespace composer {
32 namespace V2_1 {
33 namespace implementation {
34 
35 class BufferCacheEntry {
36 public:
37     BufferCacheEntry();
38     BufferCacheEntry(BufferCacheEntry&& other);
39 
40     BufferCacheEntry(const BufferCacheEntry& other) = delete;
41     BufferCacheEntry& operator=(const BufferCacheEntry& other) = delete;
42 
43     BufferCacheEntry& operator=(buffer_handle_t handle);
44     ~BufferCacheEntry();
45 
getHandle()46     buffer_handle_t getHandle() const { return mHandle; }
47 
48 private:
49     void clear();
50 
51     buffer_handle_t mHandle;
52 };
53 
54 class ComposerClient : public IComposerClient {
55 public:
56     ComposerClient(ComposerBase& hal);
57     virtual ~ComposerClient();
58 
59     void initialize();
60 
61     void onHotplug(Display display, IComposerCallback::Connection connected);
62     void onRefresh(Display display);
63     void onVsync(Display display, int64_t timestamp);
64 
65     // IComposerClient interface
66     Return<void> registerCallback(
67             const sp<IComposerCallback>& callback) override;
68     Return<uint32_t> getMaxVirtualDisplayCount() override;
69     Return<void> createVirtualDisplay(uint32_t width, uint32_t height,
70             PixelFormat formatHint, uint32_t outputBufferSlotCount,
71             createVirtualDisplay_cb hidl_cb) override;
72     Return<Error> destroyVirtualDisplay(Display display) override;
73     Return<void> createLayer(Display display, uint32_t bufferSlotCount,
74             createLayer_cb hidl_cb) override;
75     Return<Error> destroyLayer(Display display, Layer layer) override;
76     Return<void> getActiveConfig(Display display,
77             getActiveConfig_cb hidl_cb) override;
78     Return<Error> getClientTargetSupport(Display display,
79             uint32_t width, uint32_t height,
80             PixelFormat format, Dataspace dataspace) override;
81     Return<void> getColorModes(Display display,
82             getColorModes_cb hidl_cb) override;
83     Return<void> getDisplayAttribute(Display display,
84             Config config, Attribute attribute,
85             getDisplayAttribute_cb hidl_cb) override;
86     Return<void> getDisplayConfigs(Display display,
87             getDisplayConfigs_cb hidl_cb) override;
88     Return<void> getDisplayName(Display display,
89             getDisplayName_cb hidl_cb) override;
90     Return<void> getDisplayType(Display display,
91             getDisplayType_cb hidl_cb) override;
92     Return<void> getDozeSupport(Display display,
93             getDozeSupport_cb hidl_cb) override;
94     Return<void> getHdrCapabilities(Display display,
95             getHdrCapabilities_cb hidl_cb) override;
96     Return<Error> setActiveConfig(Display display, Config config) override;
97     Return<Error> setColorMode(Display display, ColorMode mode) override;
98     Return<Error> setPowerMode(Display display, PowerMode mode) override;
99     Return<Error> setVsyncEnabled(Display display, Vsync enabled) override;
100     Return<Error> setClientTargetSlotCount(Display display,
101             uint32_t clientTargetSlotCount) override;
102     Return<Error> setInputCommandQueue(
103             const MQDescriptorSync<uint32_t>& descriptor) override;
104     Return<void> getOutputCommandQueue(
105             getOutputCommandQueue_cb hidl_cb) override;
106     Return<void> executeCommands(uint32_t inLength,
107             const hidl_vec<hidl_handle>& inHandles,
108             executeCommands_cb hidl_cb) override;
109 
110 protected:
111     struct LayerBuffers {
112         std::vector<BufferCacheEntry> Buffers;
113         BufferCacheEntry SidebandStream;
114     };
115 
116     struct DisplayData {
117         bool IsVirtual;
118 
119         std::vector<BufferCacheEntry> ClientTargets;
120         std::vector<BufferCacheEntry> OutputBuffers;
121 
122         std::unordered_map<Layer, LayerBuffers> Layers;
123 
DisplayDataDisplayData124         DisplayData(bool isVirtual) : IsVirtual(isVirtual) {}
125     };
126 
127     class CommandReader : public CommandReaderBase {
128     public:
129         CommandReader(ComposerClient& client);
130         virtual ~CommandReader();
131 
132         Error parse();
133 
134     protected:
135         virtual bool parseCommand(IComposerClient::Command command,
136                 uint16_t length);
137 
138         bool parseSelectDisplay(uint16_t length);
139         bool parseSelectLayer(uint16_t length);
140         bool parseSetColorTransform(uint16_t length);
141         bool parseSetClientTarget(uint16_t length);
142         bool parseSetOutputBuffer(uint16_t length);
143         bool parseValidateDisplay(uint16_t length);
144         bool parsePresentOrValidateDisplay(uint16_t length);
145         bool parseAcceptDisplayChanges(uint16_t length);
146         bool parsePresentDisplay(uint16_t length);
147         bool parseSetLayerCursorPosition(uint16_t length);
148         bool parseSetLayerBuffer(uint16_t length);
149         bool parseSetLayerSurfaceDamage(uint16_t length);
150         bool parseSetLayerBlendMode(uint16_t length);
151         bool parseSetLayerColor(uint16_t length);
152         bool parseSetLayerCompositionType(uint16_t length);
153         bool parseSetLayerDataspace(uint16_t length);
154         bool parseSetLayerDisplayFrame(uint16_t length);
155         bool parseSetLayerPlaneAlpha(uint16_t length);
156         bool parseSetLayerSidebandStream(uint16_t length);
157         bool parseSetLayerSourceCrop(uint16_t length);
158         bool parseSetLayerTransform(uint16_t length);
159         bool parseSetLayerVisibleRegion(uint16_t length);
160         bool parseSetLayerZOrder(uint16_t length);
161 
162         hwc_rect_t readRect();
163         std::vector<hwc_rect_t> readRegion(size_t count);
164         hwc_frect_t readFRect();
165 
166         enum class BufferCache {
167             CLIENT_TARGETS,
168             OUTPUT_BUFFERS,
169             LAYER_BUFFERS,
170             LAYER_SIDEBAND_STREAMS,
171         };
172         Error lookupBufferCacheEntryLocked(BufferCache cache, uint32_t slot,
173                 BufferCacheEntry** outEntry);
174         Error lookupBuffer(BufferCache cache, uint32_t slot,
175                 bool useCache, buffer_handle_t handle,
176                 buffer_handle_t* outHandle);
177         Error updateBuffer(BufferCache cache, uint32_t slot,
178                 bool useCache, buffer_handle_t handle);
179 
lookupLayerSidebandStream(buffer_handle_t handle,buffer_handle_t * outHandle)180         Error lookupLayerSidebandStream(buffer_handle_t handle,
181                 buffer_handle_t* outHandle)
182         {
183             return lookupBuffer(BufferCache::LAYER_SIDEBAND_STREAMS,
184                     0, false, handle, outHandle);
185         }
updateLayerSidebandStream(buffer_handle_t handle)186         Error updateLayerSidebandStream(buffer_handle_t handle)
187         {
188             return updateBuffer(BufferCache::LAYER_SIDEBAND_STREAMS,
189                     0, false, handle);
190         }
191 
192         ComposerClient& mClient;
193         ComposerBase& mHal;
194         CommandWriterBase& mWriter;
195 
196         Display mDisplay;
197         Layer mLayer;
198     };
199 
200     virtual std::unique_ptr<CommandReader> createCommandReader();
201 
202     ComposerBase& mHal;
203 
204     // 64KiB minus a small space for metadata such as read/write pointers
205     static constexpr size_t kWriterInitialSize =
206         64 * 1024 / sizeof(uint32_t) - 16;
207     std::mutex mCommandMutex;
208     std::unique_ptr<CommandReader> mReader;
209     CommandWriterBase mWriter;
210 
211     sp<IComposerCallback> mCallback;
212 
213     std::mutex mDisplayDataMutex;
214     std::unordered_map<Display, DisplayData> mDisplayData;
215 };
216 
217 } // namespace implementation
218 } // namespace V2_1
219 } // namespace composer
220 } // namespace graphics
221 } // namespace hardware
222 } // namespace android
223 
224 #endif  // ANDROID_HARDWARE_GRAPHICS_COMPOSER_V2_1_COMPOSER_CLIENT_H
225