• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         // the handle is a sideband stream handle, not a buffer handle
114         BufferCacheEntry SidebandStream;
115     };
116 
117     struct DisplayData {
118         bool IsVirtual;
119 
120         std::vector<BufferCacheEntry> ClientTargets;
121         std::vector<BufferCacheEntry> OutputBuffers;
122 
123         std::unordered_map<Layer, LayerBuffers> Layers;
124 
DisplayDataDisplayData125         DisplayData(bool isVirtual) : IsVirtual(isVirtual) {}
126     };
127 
128     class CommandReader : public CommandReaderBase {
129     public:
130         CommandReader(ComposerClient& client);
131         virtual ~CommandReader();
132 
133         Error parse();
134 
135     protected:
136         virtual bool parseCommand(IComposerClient::Command command,
137                 uint16_t length);
138 
139         bool parseSelectDisplay(uint16_t length);
140         bool parseSelectLayer(uint16_t length);
141         bool parseSetColorTransform(uint16_t length);
142         bool parseSetClientTarget(uint16_t length);
143         bool parseSetOutputBuffer(uint16_t length);
144         bool parseValidateDisplay(uint16_t length);
145         bool parsePresentOrValidateDisplay(uint16_t length);
146         bool parseAcceptDisplayChanges(uint16_t length);
147         bool parsePresentDisplay(uint16_t length);
148         bool parseSetLayerCursorPosition(uint16_t length);
149         bool parseSetLayerBuffer(uint16_t length);
150         bool parseSetLayerSurfaceDamage(uint16_t length);
151         bool parseSetLayerBlendMode(uint16_t length);
152         bool parseSetLayerColor(uint16_t length);
153         bool parseSetLayerCompositionType(uint16_t length);
154         bool parseSetLayerDataspace(uint16_t length);
155         bool parseSetLayerDisplayFrame(uint16_t length);
156         bool parseSetLayerPlaneAlpha(uint16_t length);
157         bool parseSetLayerSidebandStream(uint16_t length);
158         bool parseSetLayerSourceCrop(uint16_t length);
159         bool parseSetLayerTransform(uint16_t length);
160         bool parseSetLayerVisibleRegion(uint16_t length);
161         bool parseSetLayerZOrder(uint16_t length);
162 
163         hwc_rect_t readRect();
164         std::vector<hwc_rect_t> readRegion(size_t count);
165         hwc_frect_t readFRect();
166 
167         enum class BufferCache {
168             CLIENT_TARGETS,
169             OUTPUT_BUFFERS,
170             LAYER_BUFFERS,
171             LAYER_SIDEBAND_STREAMS,
172         };
173         Error lookupBufferCacheEntryLocked(BufferCache cache, uint32_t slot,
174                 BufferCacheEntry** outEntry);
175         Error lookupBuffer(BufferCache cache, uint32_t slot,
176                 bool useCache, buffer_handle_t handle,
177                 buffer_handle_t* outHandle);
178         Error updateBuffer(BufferCache cache, uint32_t slot,
179                 bool useCache, buffer_handle_t handle);
180 
lookupLayerSidebandStream(buffer_handle_t handle,buffer_handle_t * outHandle)181         Error lookupLayerSidebandStream(buffer_handle_t handle,
182                 buffer_handle_t* outHandle)
183         {
184             return lookupBuffer(BufferCache::LAYER_SIDEBAND_STREAMS,
185                     0, false, handle, outHandle);
186         }
updateLayerSidebandStream(buffer_handle_t handle)187         Error updateLayerSidebandStream(buffer_handle_t handle)
188         {
189             return updateBuffer(BufferCache::LAYER_SIDEBAND_STREAMS,
190                     0, false, handle);
191         }
192 
193         ComposerClient& mClient;
194         ComposerBase& mHal;
195         CommandWriterBase& mWriter;
196 
197         Display mDisplay;
198         Layer mLayer;
199     };
200 
201     virtual std::unique_ptr<CommandReader> createCommandReader();
202 
203     ComposerBase& mHal;
204 
205     // 64KiB minus a small space for metadata such as read/write pointers
206     static constexpr size_t kWriterInitialSize =
207         64 * 1024 / sizeof(uint32_t) - 16;
208     std::mutex mCommandMutex;
209     std::unique_ptr<CommandReader> mReader;
210     CommandWriterBase mWriter;
211 
212     sp<IComposerCallback> mCallback;
213 
214     std::mutex mDisplayDataMutex;
215     std::unordered_map<Display, DisplayData> mDisplayData;
216 };
217 
218 } // namespace implementation
219 } // namespace V2_1
220 } // namespace composer
221 } // namespace graphics
222 } // namespace hardware
223 } // namespace android
224 
225 #endif  // ANDROID_HARDWARE_GRAPHICS_COMPOSER_V2_1_COMPOSER_CLIENT_H
226