blob: 68d6e6fa13a06430e9c8f350ecfe5394cd4edc81 [file] [log] [blame]
Chia-I Wuaab99f52016-10-05 12:59:58 +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
17#ifndef ANDROID_SF_COMPOSER_HAL_H
18#define ANDROID_SF_COMPOSER_HAL_H
19
Chia-I Wucd8d7f02016-11-16 11:02:31 +080020#include <memory>
Chia-I Wuaab99f52016-10-05 12:59:58 +080021#include <string>
Chia-I Wucd8d7f02016-11-16 11:02:31 +080022#include <unordered_map>
23#include <utility>
Chia-I Wuaab99f52016-10-05 12:59:58 +080024#include <vector>
25
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -040026#include <android/frameworks/vr/composer/1.0/IVrComposerClient.h>
Chia-I Wuaab99f52016-10-05 12:59:58 +080027#include <android/hardware/graphics/composer/2.1/IComposer.h>
28#include <utils/StrongPointer.h>
Chia-I Wucd8d7f02016-11-16 11:02:31 +080029#include <IComposerCommandBuffer.h>
Chia-I Wuaab99f52016-10-05 12:59:58 +080030
31namespace android {
32
33namespace Hwc2 {
34
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -040035using android::frameworks::vr::composer::V1_0::IVrComposerClient;
36
Chia-I Wu5901fda2016-11-17 10:26:37 +080037using android::hardware::graphics::common::V1_0::ColorMode;
38using android::hardware::graphics::common::V1_0::ColorTransform;
39using android::hardware::graphics::common::V1_0::Dataspace;
40using android::hardware::graphics::common::V1_0::Hdr;
41using android::hardware::graphics::common::V1_0::PixelFormat;
42using android::hardware::graphics::common::V1_0::Transform;
Chia-I Wuaab99f52016-10-05 12:59:58 +080043
44using android::hardware::graphics::composer::V2_1::IComposer;
45using android::hardware::graphics::composer::V2_1::IComposerCallback;
Chia-I Wucd8d7f02016-11-16 11:02:31 +080046using android::hardware::graphics::composer::V2_1::IComposerClient;
Chia-I Wuaab99f52016-10-05 12:59:58 +080047using android::hardware::graphics::composer::V2_1::Error;
48using android::hardware::graphics::composer::V2_1::Display;
49using android::hardware::graphics::composer::V2_1::Layer;
50using android::hardware::graphics::composer::V2_1::Config;
51
Daniel Nicoara3c9cbd42017-01-17 12:04:06 -050052using android::hardware::graphics::composer::V2_1::CommandWriterBase;
Chia-I Wucd8d7f02016-11-16 11:02:31 +080053using android::hardware::graphics::composer::V2_1::CommandReaderBase;
54
55using android::hardware::kSynchronizedReadWrite;
56using android::hardware::MessageQueue;
57using android::hardware::MQDescriptorSync;
58using android::hardware::hidl_vec;
59using android::hardware::hidl_handle;
60
61class CommandReader : public CommandReaderBase {
62public:
63 ~CommandReader();
64
65 // Parse and execute commands from the command queue. The commands are
66 // actually return values from the server and will be saved in ReturnData.
67 Error parse();
68
69 // Get and clear saved errors.
70 struct CommandError {
71 uint32_t location;
72 Error error;
73 };
74 std::vector<CommandError> takeErrors();
75
Chia-I Wu67e376d2016-12-19 11:36:22 +080076 bool hasChanges(Display display, uint32_t* outNumChangedCompositionTypes,
77 uint32_t* outNumLayerRequestMasks) const;
Chia-I Wucd8d7f02016-11-16 11:02:31 +080078
79 // Get and clear saved changed composition types.
80 void takeChangedCompositionTypes(Display display,
Chia-I Wu67e376d2016-12-19 11:36:22 +080081 std::vector<Layer>* outLayers,
82 std::vector<IComposerClient::Composition>* outTypes);
Chia-I Wucd8d7f02016-11-16 11:02:31 +080083
84 // Get and clear saved display requests.
85 void takeDisplayRequests(Display display,
Chia-I Wu67e376d2016-12-19 11:36:22 +080086 uint32_t* outDisplayRequestMask, std::vector<Layer>* outLayers,
87 std::vector<uint32_t>* outLayerRequestMasks);
Chia-I Wucd8d7f02016-11-16 11:02:31 +080088
89 // Get and clear saved release fences.
Chia-I Wu67e376d2016-12-19 11:36:22 +080090 void takeReleaseFences(Display display, std::vector<Layer>* outLayers,
91 std::vector<int>* outReleaseFences);
Chia-I Wucd8d7f02016-11-16 11:02:31 +080092
93 // Get and clear saved present fence.
Chia-I Wu67e376d2016-12-19 11:36:22 +080094 void takePresentFence(Display display, int* outPresentFence);
Chia-I Wucd8d7f02016-11-16 11:02:31 +080095
96private:
97 void resetData();
98
99 bool parseSelectDisplay(uint16_t length);
100 bool parseSetError(uint16_t length);
101 bool parseSetChangedCompositionTypes(uint16_t length);
102 bool parseSetDisplayRequests(uint16_t length);
103 bool parseSetPresentFence(uint16_t length);
104 bool parseSetReleaseFences(uint16_t length);
105
106 struct ReturnData {
107 uint32_t displayRequests = 0;
108
109 std::vector<Layer> changedLayers;
110 std::vector<IComposerClient::Composition> compositionTypes;
111
112 std::vector<Layer> requestedLayers;
113 std::vector<uint32_t> requestMasks;
114
115 int presentFence = -1;
116
117 std::vector<Layer> releasedLayers;
118 std::vector<int> releaseFences;
119 };
120
121 std::vector<CommandError> mErrors;
122 std::unordered_map<Display, ReturnData> mReturnData;
123
124 // When SELECT_DISPLAY is parsed, this is updated to point to the
125 // display's return data in mReturnData. We use it to avoid repeated
126 // map lookups.
127 ReturnData* mCurrentReturnData;
128};
129
Chia-I Wuaab99f52016-10-05 12:59:58 +0800130// Composer is a wrapper to IComposer, a proxy to server-side composer.
131class Composer {
132public:
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800133 Composer(bool useVrComposer);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800134
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800135 std::vector<IComposer::Capability> getCapabilities();
136 std::string dumpDebugInfo();
Chia-I Wuaab99f52016-10-05 12:59:58 +0800137
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800138 void registerCallback(const sp<IComposerCallback>& callback);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800139
Steven Thomas0af4b9f2017-04-26 14:34:01 -0700140 // Reset all pending commands in the command buffer. Useful if you want to
141 // skip a frame but have already queued some commands.
142 void resetCommands();
143
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800144 uint32_t getMaxVirtualDisplayCount();
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800145 bool isUsingVrComposer() const { return mIsUsingVrComposer; }
Chia-I Wuaab99f52016-10-05 12:59:58 +0800146 Error createVirtualDisplay(uint32_t width, uint32_t height,
Chia-I Wu67e376d2016-12-19 11:36:22 +0800147 PixelFormat* format, Display* outDisplay);
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800148 Error destroyVirtualDisplay(Display display);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800149
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800150 Error acceptDisplayChanges(Display display);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800151
Chia-I Wu67e376d2016-12-19 11:36:22 +0800152 Error createLayer(Display display, Layer* outLayer);
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800153 Error destroyLayer(Display display, Layer layer);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800154
Chia-I Wu67e376d2016-12-19 11:36:22 +0800155 Error getActiveConfig(Display display, Config* outConfig);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800156 Error getChangedCompositionTypes(Display display,
Chia-I Wu67e376d2016-12-19 11:36:22 +0800157 std::vector<Layer>* outLayers,
158 std::vector<IComposerClient::Composition>* outTypes);
159 Error getColorModes(Display display, std::vector<ColorMode>* outModes);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800160 Error getDisplayAttribute(Display display, Config config,
Chia-I Wu67e376d2016-12-19 11:36:22 +0800161 IComposerClient::Attribute attribute, int32_t* outValue);
162 Error getDisplayConfigs(Display display, std::vector<Config>* outConfigs);
163 Error getDisplayName(Display display, std::string* outName);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800164
Chia-I Wu67e376d2016-12-19 11:36:22 +0800165 Error getDisplayRequests(Display display, uint32_t* outDisplayRequestMask,
166 std::vector<Layer>* outLayers,
167 std::vector<uint32_t>* outLayerRequestMasks);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800168
Chia-I Wu67e376d2016-12-19 11:36:22 +0800169 Error getDisplayType(Display display,
170 IComposerClient::DisplayType* outType);
171 Error getDozeSupport(Display display, bool* outSupport);
172 Error getHdrCapabilities(Display display, std::vector<Hdr>* outTypes,
173 float* outMaxLuminance, float* outMaxAverageLuminance,
174 float* outMinLuminance);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800175
Chia-I Wu67e376d2016-12-19 11:36:22 +0800176 Error getReleaseFences(Display display, std::vector<Layer>* outLayers,
177 std::vector<int>* outReleaseFences);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800178
Chia-I Wu67e376d2016-12-19 11:36:22 +0800179 Error presentDisplay(Display display, int* outPresentFence);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800180
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800181 Error setActiveConfig(Display display, Config config);
Chia-I Wu06d63de2017-01-04 14:58:51 +0800182
183 /*
184 * The composer caches client targets internally. When target is nullptr,
185 * the composer uses slot to look up the client target from its cache.
186 * When target is not nullptr, the cache is updated with the new target.
187 */
188 Error setClientTarget(Display display, uint32_t slot,
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400189 const sp<GraphicBuffer>& target,
Chia-I Wuaab99f52016-10-05 12:59:58 +0800190 int acquireFence, Dataspace dataspace,
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800191 const std::vector<IComposerClient::Rect>& damage);
192 Error setColorMode(Display display, ColorMode mode);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800193 Error setColorTransform(Display display, const float* matrix,
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800194 ColorTransform hint);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800195 Error setOutputBuffer(Display display, const native_handle_t* buffer,
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800196 int releaseFence);
197 Error setPowerMode(Display display, IComposerClient::PowerMode mode);
198 Error setVsyncEnabled(Display display, IComposerClient::Vsync enabled);
199
200 Error setClientTargetSlotCount(Display display);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800201
Chia-I Wu67e376d2016-12-19 11:36:22 +0800202 Error validateDisplay(Display display, uint32_t* outNumTypes,
203 uint32_t* outNumRequests);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800204
205 Error setCursorPosition(Display display, Layer layer,
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800206 int32_t x, int32_t y);
Chia-I Wu06d63de2017-01-04 14:58:51 +0800207 /* see setClientTarget for the purpose of slot */
208 Error setLayerBuffer(Display display, Layer layer, uint32_t slot,
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400209 const sp<GraphicBuffer>& buffer, int acquireFence);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800210 Error setLayerSurfaceDamage(Display display, Layer layer,
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800211 const std::vector<IComposerClient::Rect>& damage);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800212 Error setLayerBlendMode(Display display, Layer layer,
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800213 IComposerClient::BlendMode mode);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800214 Error setLayerColor(Display display, Layer layer,
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800215 const IComposerClient::Color& color);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800216 Error setLayerCompositionType(Display display, Layer layer,
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800217 IComposerClient::Composition type);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800218 Error setLayerDataspace(Display display, Layer layer,
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800219 Dataspace dataspace);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800220 Error setLayerDisplayFrame(Display display, Layer layer,
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800221 const IComposerClient::Rect& frame);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800222 Error setLayerPlaneAlpha(Display display, Layer layer,
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800223 float alpha);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800224 Error setLayerSidebandStream(Display display, Layer layer,
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800225 const native_handle_t* stream);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800226 Error setLayerSourceCrop(Display display, Layer layer,
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800227 const IComposerClient::FRect& crop);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800228 Error setLayerTransform(Display display, Layer layer,
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800229 Transform transform);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800230 Error setLayerVisibleRegion(Display display, Layer layer,
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800231 const std::vector<IComposerClient::Rect>& visible);
232 Error setLayerZOrder(Display display, Layer layer, uint32_t z);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500233 Error setLayerInfo(Display display, Layer layer, uint32_t type,
234 uint32_t appId);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800235private:
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500236 class CommandWriter : public CommandWriterBase {
237 public:
238 CommandWriter(uint32_t initialMaxSize);
239 ~CommandWriter() override;
240
241 void setLayerInfo(uint32_t type, uint32_t appId);
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400242 void setClientTargetMetadata(
243 const IVrComposerClient::BufferMetadata& metadata);
244 void setLayerBufferMetadata(
245 const IVrComposerClient::BufferMetadata& metadata);
246
247 private:
248 void writeBufferMetadata(
249 const IVrComposerClient::BufferMetadata& metadata);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500250 };
251
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800252 // Many public functions above simply write a command into the command
253 // queue to batch the calls. validateDisplay and presentDisplay will call
254 // this function to execute the command queue.
255 Error execute();
256
257 sp<IComposer> mComposer;
258 sp<IComposerClient> mClient;
259
260 // 64KiB minus a small space for metadata such as read/write pointers
261 static constexpr size_t kWriterInitialSize =
262 64 * 1024 / sizeof(uint32_t) - 16;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500263 CommandWriter mWriter;
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800264 CommandReader mReader;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500265
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -0800266 // When true, the we attach to the vr_hwcomposer service instead of the
267 // hwcomposer. This allows us to redirect surfaces to 3d surfaces in vr.
268 const bool mIsUsingVrComposer;
Chia-I Wuaab99f52016-10-05 12:59:58 +0800269};
270
271} // namespace Hwc2
272
273} // namespace android
274
275#endif // ANDROID_SF_COMPOSER_HAL_H