blob: c9733511b36f5c3e67df8da79547e85c580e550e [file] [log] [blame]
Chia-I Wubb61a722016-10-24 15:40:20 +08001/*
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
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -050017#ifndef ANDROID_HARDWARE_GRAPHICS_COMPOSER_V2_1_COMPOSER_CLIENT_H
18#define ANDROID_HARDWARE_GRAPHICS_COMPOSER_V2_1_COMPOSER_CLIENT_H
Chia-I Wubb61a722016-10-24 15:40:20 +080019
20#include <mutex>
21#include <unordered_map>
22#include <vector>
23
Fabien Sanglard9b117a42017-03-21 09:39:35 -070024#include <hardware/hwcomposer2.h>
Chia-I Wubb61a722016-10-24 15:40:20 +080025#include "IComposerCommandBuffer.h"
Fabien Sanglard9b117a42017-03-21 09:39:35 -070026#include "ComposerBase.h"
Chia-I Wubb61a722016-10-24 15:40:20 +080027
28namespace android {
29namespace hardware {
30namespace graphics {
31namespace composer {
32namespace V2_1 {
33namespace implementation {
34
Chia-I Wu41a1c152017-03-09 15:41:59 -080035class BufferCacheEntry {
Chia-I Wubb61a722016-10-24 15:40:20 +080036public:
Chia-I Wu41a1c152017-03-09 15:41:59 -080037 BufferCacheEntry();
38 BufferCacheEntry(BufferCacheEntry&& other);
Chia-I Wubb61a722016-10-24 15:40:20 +080039
Chia-I Wu41a1c152017-03-09 15:41:59 -080040 BufferCacheEntry(const BufferCacheEntry& other) = delete;
41 BufferCacheEntry& operator=(const BufferCacheEntry& other) = delete;
Chia-I Wubb61a722016-10-24 15:40:20 +080042
Chia-I Wu41a1c152017-03-09 15:41:59 -080043 BufferCacheEntry& operator=(buffer_handle_t handle);
44 ~BufferCacheEntry();
Chia-I Wubb61a722016-10-24 15:40:20 +080045
Chia-I Wu41a1c152017-03-09 15:41:59 -080046 buffer_handle_t getHandle() const { return mHandle; }
Chia-I Wubb61a722016-10-24 15:40:20 +080047
48private:
49 void clear();
50
51 buffer_handle_t mHandle;
52};
53
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -050054class ComposerClient : public IComposerClient {
Chia-I Wubb61a722016-10-24 15:40:20 +080055public:
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -050056 ComposerClient(ComposerBase& hal);
57 virtual ~ComposerClient();
58
59 void initialize();
Chia-I Wubb61a722016-10-24 15:40:20 +080060
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(
Hridya Valsaraju33351da2016-12-27 12:40:01 -0800103 const MQDescriptorSync<uint32_t>& descriptor) override;
Chia-I Wubb61a722016-10-24 15:40:20 +0800104 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
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500110protected:
Chia-I Wubb61a722016-10-24 15:40:20 +0800111 struct LayerBuffers {
Chia-I Wu41a1c152017-03-09 15:41:59 -0800112 std::vector<BufferCacheEntry> Buffers;
113 BufferCacheEntry SidebandStream;
Chia-I Wubb61a722016-10-24 15:40:20 +0800114 };
115
116 struct DisplayData {
117 bool IsVirtual;
118
Chia-I Wu41a1c152017-03-09 15:41:59 -0800119 std::vector<BufferCacheEntry> ClientTargets;
120 std::vector<BufferCacheEntry> OutputBuffers;
Chia-I Wubb61a722016-10-24 15:40:20 +0800121
122 std::unordered_map<Layer, LayerBuffers> Layers;
123
124 DisplayData(bool isVirtual) : IsVirtual(isVirtual) {}
125 };
126
127 class CommandReader : public CommandReaderBase {
128 public:
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500129 CommandReader(ComposerClient& client);
130 virtual ~CommandReader();
131
Chia-I Wubb61a722016-10-24 15:40:20 +0800132 Error parse();
133
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500134 protected:
135 virtual bool parseCommand(IComposerClient::Command command,
136 uint16_t length);
137
Chia-I Wubb61a722016-10-24 15:40:20 +0800138 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 parseAcceptDisplayChanges(uint16_t length);
145 bool parsePresentDisplay(uint16_t length);
146 bool parseSetLayerCursorPosition(uint16_t length);
147 bool parseSetLayerBuffer(uint16_t length);
148 bool parseSetLayerSurfaceDamage(uint16_t length);
149 bool parseSetLayerBlendMode(uint16_t length);
150 bool parseSetLayerColor(uint16_t length);
151 bool parseSetLayerCompositionType(uint16_t length);
152 bool parseSetLayerDataspace(uint16_t length);
153 bool parseSetLayerDisplayFrame(uint16_t length);
154 bool parseSetLayerPlaneAlpha(uint16_t length);
155 bool parseSetLayerSidebandStream(uint16_t length);
156 bool parseSetLayerSourceCrop(uint16_t length);
157 bool parseSetLayerTransform(uint16_t length);
158 bool parseSetLayerVisibleRegion(uint16_t length);
159 bool parseSetLayerZOrder(uint16_t length);
160
161 hwc_rect_t readRect();
162 std::vector<hwc_rect_t> readRegion(size_t count);
163 hwc_frect_t readFRect();
164
165 enum class BufferCache {
166 CLIENT_TARGETS,
167 OUTPUT_BUFFERS,
168 LAYER_BUFFERS,
169 LAYER_SIDEBAND_STREAMS,
170 };
Chia-I Wu41a1c152017-03-09 15:41:59 -0800171 Error lookupBufferCacheEntryLocked(BufferCache cache, uint32_t slot,
172 BufferCacheEntry** outEntry);
Chia-I Wubb61a722016-10-24 15:40:20 +0800173 Error lookupBuffer(BufferCache cache, uint32_t slot,
Chia-I Wu41a1c152017-03-09 15:41:59 -0800174 bool useCache, buffer_handle_t handle,
175 buffer_handle_t* outHandle);
176 void updateBuffer(BufferCache cache, uint32_t slot,
177 bool useCache, buffer_handle_t handle);
Chia-I Wubb61a722016-10-24 15:40:20 +0800178
Chia-I Wu41a1c152017-03-09 15:41:59 -0800179 Error lookupLayerSidebandStream(buffer_handle_t handle,
180 buffer_handle_t* outHandle)
Chia-I Wubb61a722016-10-24 15:40:20 +0800181 {
182 return lookupBuffer(BufferCache::LAYER_SIDEBAND_STREAMS,
Chia-I Wu41a1c152017-03-09 15:41:59 -0800183 0, false, handle, outHandle);
184 }
185 void updateLayerSidebandStream(buffer_handle_t handle)
186 {
187 updateBuffer(BufferCache::LAYER_SIDEBAND_STREAMS,
Chia-I Wubb61a722016-10-24 15:40:20 +0800188 0, false, handle);
189 }
190
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500191 ComposerClient& mClient;
192 ComposerBase& mHal;
Daniel Nicoarabd82b812017-01-17 11:15:26 -0500193 CommandWriterBase& mWriter;
Chia-I Wubb61a722016-10-24 15:40:20 +0800194
195 Display mDisplay;
196 Layer mLayer;
197 };
198
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500199 virtual std::unique_ptr<CommandReader> createCommandReader();
200
201 ComposerBase& mHal;
Chia-I Wubb61a722016-10-24 15:40:20 +0800202
203 // 64KiB minus a small space for metadata such as read/write pointers
204 static constexpr size_t kWriterInitialSize =
205 64 * 1024 / sizeof(uint32_t) - 16;
206 std::mutex mCommandMutex;
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500207 std::unique_ptr<CommandReader> mReader;
Daniel Nicoarabd82b812017-01-17 11:15:26 -0500208 CommandWriterBase mWriter;
Chia-I Wubb61a722016-10-24 15:40:20 +0800209
210 sp<IComposerCallback> mCallback;
211
212 std::mutex mDisplayDataMutex;
213 std::unordered_map<Display, DisplayData> mDisplayData;
214};
215
216} // namespace implementation
217} // namespace V2_1
218} // namespace composer
219} // namespace graphics
220} // namespace hardware
221} // namespace android
222
Daniel Nicoara0a60e4b2017-01-09 12:51:06 -0500223#endif // ANDROID_HARDWARE_GRAPHICS_COMPOSER_V2_1_COMPOSER_CLIENT_H