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_SF_COMPOSER_HAL_H
18 #define ANDROID_SF_COMPOSER_HAL_H
19 
20 #include <memory>
21 #include <string>
22 #include <unordered_map>
23 #include <utility>
24 #include <vector>
25 
26 #include <android/frameworks/vr/composer/1.0/IVrComposerClient.h>
27 #include <android/hardware/graphics/common/1.1/types.h>
28 #include <android/hardware/graphics/composer/2.3/IComposer.h>
29 #include <android/hardware/graphics/composer/2.3/IComposerClient.h>
30 #include <composer-command-buffer/2.3/ComposerCommandBuffer.h>
31 #include <gui/HdrMetadata.h>
32 #include <math/mat4.h>
33 #include <ui/DisplayedFrameStats.h>
34 #include <ui/GraphicBuffer.h>
35 #include <utils/StrongPointer.h>
36 
37 namespace android {
38 
39 namespace Hwc2 {
40 
41 using frameworks::vr::composer::V1_0::IVrComposerClient;
42 
43 namespace types = hardware::graphics::common;
44 
45 namespace V2_1 = hardware::graphics::composer::V2_1;
46 namespace V2_2 = hardware::graphics::composer::V2_2;
47 namespace V2_3 = hardware::graphics::composer::V2_3;
48 
49 using types::V1_0::ColorTransform;
50 using types::V1_0::Transform;
51 using types::V1_1::RenderIntent;
52 using types::V1_2::ColorMode;
53 using types::V1_2::Dataspace;
54 using types::V1_2::Hdr;
55 using types::V1_2::PixelFormat;
56 
57 using V2_1::Config;
58 using V2_1::Display;
59 using V2_1::Error;
60 using V2_1::IComposerCallback;
61 using V2_1::Layer;
62 using V2_3::CommandReaderBase;
63 using V2_3::CommandWriterBase;
64 using V2_3::IComposer;
65 using V2_3::IComposerClient;
66 using DisplayCapability = IComposerClient::DisplayCapability;
67 using PerFrameMetadata = IComposerClient::PerFrameMetadata;
68 using PerFrameMetadataKey = IComposerClient::PerFrameMetadataKey;
69 using PerFrameMetadataBlob = IComposerClient::PerFrameMetadataBlob;
70 
71 class Composer {
72 public:
73     virtual ~Composer() = 0;
74 
75     virtual std::vector<IComposer::Capability> getCapabilities() = 0;
76     virtual std::string dumpDebugInfo() = 0;
77 
78     virtual void registerCallback(const sp<IComposerCallback>& callback) = 0;
79 
80     // Returns true if the connected composer service is running in a remote
81     // process, false otherwise. This will return false if the service is
82     // configured in passthrough mode, for example.
83     virtual bool isRemote() = 0;
84 
85     // Reset all pending commands in the command buffer. Useful if you want to
86     // skip a frame but have already queued some commands.
87     virtual void resetCommands() = 0;
88 
89     // Explicitly flush all pending commands in the command buffer.
90     virtual Error executeCommands() = 0;
91 
92     virtual uint32_t getMaxVirtualDisplayCount() = 0;
93     virtual bool isUsingVrComposer() const = 0;
94     virtual Error createVirtualDisplay(uint32_t width, uint32_t height, PixelFormat* format,
95                                        Display* outDisplay) = 0;
96     virtual Error destroyVirtualDisplay(Display display) = 0;
97 
98     virtual Error acceptDisplayChanges(Display display) = 0;
99 
100     virtual Error createLayer(Display display, Layer* outLayer) = 0;
101     virtual Error destroyLayer(Display display, Layer layer) = 0;
102 
103     virtual Error getActiveConfig(Display display, Config* outConfig) = 0;
104     virtual Error getChangedCompositionTypes(
105             Display display, std::vector<Layer>* outLayers,
106             std::vector<IComposerClient::Composition>* outTypes) = 0;
107     virtual Error getColorModes(Display display, std::vector<ColorMode>* outModes) = 0;
108     virtual Error getDisplayAttribute(Display display, Config config,
109                                       IComposerClient::Attribute attribute, int32_t* outValue) = 0;
110     virtual Error getDisplayConfigs(Display display, std::vector<Config>* outConfigs) = 0;
111     virtual Error getDisplayName(Display display, std::string* outName) = 0;
112 
113     virtual Error getDisplayRequests(Display display, uint32_t* outDisplayRequestMask,
114                                      std::vector<Layer>* outLayers,
115                                      std::vector<uint32_t>* outLayerRequestMasks) = 0;
116 
117     virtual Error getDisplayType(Display display, IComposerClient::DisplayType* outType) = 0;
118     virtual Error getDozeSupport(Display display, bool* outSupport) = 0;
119     virtual Error getHdrCapabilities(Display display, std::vector<Hdr>* outTypes,
120                                      float* outMaxLuminance, float* outMaxAverageLuminance,
121                                      float* outMinLuminance) = 0;
122 
123     virtual Error getReleaseFences(Display display, std::vector<Layer>* outLayers,
124                                    std::vector<int>* outReleaseFences) = 0;
125 
126     virtual Error presentDisplay(Display display, int* outPresentFence) = 0;
127 
128     virtual Error setActiveConfig(Display display, Config config) = 0;
129 
130     /*
131      * The composer caches client targets internally.  When target is nullptr,
132      * the composer uses slot to look up the client target from its cache.
133      * When target is not nullptr, the cache is updated with the new target.
134      */
135     virtual Error setClientTarget(Display display, uint32_t slot, const sp<GraphicBuffer>& target,
136                                   int acquireFence, Dataspace dataspace,
137                                   const std::vector<IComposerClient::Rect>& damage) = 0;
138     virtual Error setColorMode(Display display, ColorMode mode, RenderIntent renderIntent) = 0;
139     virtual Error setColorTransform(Display display, const float* matrix, ColorTransform hint) = 0;
140     virtual Error setOutputBuffer(Display display, const native_handle_t* buffer,
141                                   int releaseFence) = 0;
142     virtual Error setPowerMode(Display display, IComposerClient::PowerMode mode) = 0;
143     virtual Error setVsyncEnabled(Display display, IComposerClient::Vsync enabled) = 0;
144 
145     virtual Error setClientTargetSlotCount(Display display) = 0;
146 
147     virtual Error validateDisplay(Display display, uint32_t* outNumTypes,
148                                   uint32_t* outNumRequests) = 0;
149 
150     virtual Error presentOrValidateDisplay(Display display, uint32_t* outNumTypes,
151                                            uint32_t* outNumRequests, int* outPresentFence,
152                                            uint32_t* state) = 0;
153 
154     virtual Error setCursorPosition(Display display, Layer layer, int32_t x, int32_t y) = 0;
155     /* see setClientTarget for the purpose of slot */
156     virtual Error setLayerBuffer(Display display, Layer layer, uint32_t slot,
157                                  const sp<GraphicBuffer>& buffer, int acquireFence) = 0;
158     virtual Error setLayerSurfaceDamage(Display display, Layer layer,
159                                         const std::vector<IComposerClient::Rect>& damage) = 0;
160     virtual Error setLayerBlendMode(Display display, Layer layer,
161                                     IComposerClient::BlendMode mode) = 0;
162     virtual Error setLayerColor(Display display, Layer layer,
163                                 const IComposerClient::Color& color) = 0;
164     virtual Error setLayerCompositionType(Display display, Layer layer,
165                                           IComposerClient::Composition type) = 0;
166     virtual Error setLayerDataspace(Display display, Layer layer, Dataspace dataspace) = 0;
167     virtual Error setLayerDisplayFrame(Display display, Layer layer,
168                                        const IComposerClient::Rect& frame) = 0;
169     virtual Error setLayerPlaneAlpha(Display display, Layer layer, float alpha) = 0;
170     virtual Error setLayerSidebandStream(Display display, Layer layer,
171                                          const native_handle_t* stream) = 0;
172     virtual Error setLayerSourceCrop(Display display, Layer layer,
173                                      const IComposerClient::FRect& crop) = 0;
174     virtual Error setLayerTransform(Display display, Layer layer, Transform transform) = 0;
175     virtual Error setLayerVisibleRegion(Display display, Layer layer,
176                                         const std::vector<IComposerClient::Rect>& visible) = 0;
177     virtual Error setLayerZOrder(Display display, Layer layer, uint32_t z) = 0;
178     virtual Error setLayerInfo(Display display, Layer layer, uint32_t type, uint32_t appId) = 0;
179 
180     // Composer HAL 2.2
181     virtual Error setLayerPerFrameMetadata(
182             Display display, Layer layer,
183             const std::vector<IComposerClient::PerFrameMetadata>& perFrameMetadatas) = 0;
184     virtual std::vector<IComposerClient::PerFrameMetadataKey> getPerFrameMetadataKeys(
185             Display display) = 0;
186     virtual Error getRenderIntents(Display display, ColorMode colorMode,
187             std::vector<RenderIntent>* outRenderIntents) = 0;
188     virtual Error getDataspaceSaturationMatrix(Dataspace dataspace, mat4* outMatrix) = 0;
189 
190     // Composer HAL 2.3
191     virtual Error getDisplayIdentificationData(Display display, uint8_t* outPort,
192                                                std::vector<uint8_t>* outData) = 0;
193     virtual Error setLayerColorTransform(Display display, Layer layer,
194                                          const float* matrix) = 0;
195     virtual Error getDisplayedContentSamplingAttributes(Display display, PixelFormat* outFormat,
196                                                         Dataspace* outDataspace,
197                                                         uint8_t* outComponentMask) = 0;
198     virtual Error setDisplayContentSamplingEnabled(Display display, bool enabled,
199                                                    uint8_t componentMask, uint64_t maxFrames) = 0;
200     virtual Error getDisplayedContentSample(Display display, uint64_t maxFrames, uint64_t timestamp,
201                                             DisplayedFrameStats* outStats) = 0;
202     virtual Error getDisplayCapabilities(Display display,
203                                          std::vector<DisplayCapability>* outCapabilities) = 0;
204     virtual Error setLayerPerFrameMetadataBlobs(
205             Display display, Layer layer, const std::vector<PerFrameMetadataBlob>& metadata) = 0;
206     virtual Error setDisplayBrightness(Display display, float brightness) = 0;
207 };
208 
209 namespace impl {
210 
211 class CommandReader : public CommandReaderBase {
212 public:
213     ~CommandReader();
214 
215     // Parse and execute commands from the command queue.  The commands are
216     // actually return values from the server and will be saved in ReturnData.
217     Error parse();
218 
219     // Get and clear saved errors.
220     struct CommandError {
221         uint32_t location;
222         Error error;
223     };
224     std::vector<CommandError> takeErrors();
225 
226     bool hasChanges(Display display, uint32_t* outNumChangedCompositionTypes,
227             uint32_t* outNumLayerRequestMasks) const;
228 
229     // Get and clear saved changed composition types.
230     void takeChangedCompositionTypes(Display display,
231             std::vector<Layer>* outLayers,
232             std::vector<IComposerClient::Composition>* outTypes);
233 
234     // Get and clear saved display requests.
235     void takeDisplayRequests(Display display,
236         uint32_t* outDisplayRequestMask, std::vector<Layer>* outLayers,
237         std::vector<uint32_t>* outLayerRequestMasks);
238 
239     // Get and clear saved release fences.
240     void takeReleaseFences(Display display, std::vector<Layer>* outLayers,
241             std::vector<int>* outReleaseFences);
242 
243     // Get and clear saved present fence.
244     void takePresentFence(Display display, int* outPresentFence);
245 
246     // Get what stage succeeded during PresentOrValidate: Present or Validate
247     void takePresentOrValidateStage(Display display, uint32_t * state);
248 
249 private:
250     void resetData();
251 
252     bool parseSelectDisplay(uint16_t length);
253     bool parseSetError(uint16_t length);
254     bool parseSetChangedCompositionTypes(uint16_t length);
255     bool parseSetDisplayRequests(uint16_t length);
256     bool parseSetPresentFence(uint16_t length);
257     bool parseSetReleaseFences(uint16_t length);
258     bool parseSetPresentOrValidateDisplayResult(uint16_t length);
259 
260     struct ReturnData {
261         uint32_t displayRequests = 0;
262 
263         std::vector<Layer> changedLayers;
264         std::vector<IComposerClient::Composition> compositionTypes;
265 
266         std::vector<Layer> requestedLayers;
267         std::vector<uint32_t> requestMasks;
268 
269         int presentFence = -1;
270 
271         std::vector<Layer> releasedLayers;
272         std::vector<int> releaseFences;
273 
274         uint32_t presentOrValidateState;
275     };
276 
277     std::vector<CommandError> mErrors;
278     std::unordered_map<Display, ReturnData> mReturnData;
279 
280     // When SELECT_DISPLAY is parsed, this is updated to point to the
281     // display's return data in mReturnData.  We use it to avoid repeated
282     // map lookups.
283     ReturnData* mCurrentReturnData;
284 };
285 
286 // Composer is a wrapper to IComposer, a proxy to server-side composer.
287 class Composer final : public Hwc2::Composer {
288 public:
289     explicit Composer(const std::string& serviceName);
290     ~Composer() override;
291 
292     std::vector<IComposer::Capability> getCapabilities() override;
293     std::string dumpDebugInfo() override;
294 
295     void registerCallback(const sp<IComposerCallback>& callback) override;
296 
297     // Returns true if the connected composer service is running in a remote
298     // process, false otherwise. This will return false if the service is
299     // configured in passthrough mode, for example.
300     bool isRemote() override;
301 
302     // Reset all pending commands in the command buffer. Useful if you want to
303     // skip a frame but have already queued some commands.
304     void resetCommands() override;
305 
306     // Explicitly flush all pending commands in the command buffer.
307     Error executeCommands() override;
308 
309     uint32_t getMaxVirtualDisplayCount() override;
isUsingVrComposer()310     bool isUsingVrComposer() const override { return mIsUsingVrComposer; }
311     Error createVirtualDisplay(uint32_t width, uint32_t height, PixelFormat* format,
312                                Display* outDisplay) override;
313     Error destroyVirtualDisplay(Display display) override;
314 
315     Error acceptDisplayChanges(Display display) override;
316 
317     Error createLayer(Display display, Layer* outLayer) override;
318     Error destroyLayer(Display display, Layer layer) override;
319 
320     Error getActiveConfig(Display display, Config* outConfig) override;
321     Error getChangedCompositionTypes(Display display, std::vector<Layer>* outLayers,
322                                      std::vector<IComposerClient::Composition>* outTypes) override;
323     Error getColorModes(Display display, std::vector<ColorMode>* outModes) override;
324     Error getDisplayAttribute(Display display, Config config, IComposerClient::Attribute attribute,
325                               int32_t* outValue) override;
326     Error getDisplayConfigs(Display display, std::vector<Config>* outConfigs);
327     Error getDisplayName(Display display, std::string* outName) override;
328 
329     Error getDisplayRequests(Display display, uint32_t* outDisplayRequestMask,
330                              std::vector<Layer>* outLayers,
331                              std::vector<uint32_t>* outLayerRequestMasks) override;
332 
333     Error getDisplayType(Display display, IComposerClient::DisplayType* outType) override;
334     Error getDozeSupport(Display display, bool* outSupport) override;
335     Error getHdrCapabilities(Display display, std::vector<Hdr>* outTypes, float* outMaxLuminance,
336                              float* outMaxAverageLuminance, float* outMinLuminance) override;
337 
338     Error getReleaseFences(Display display, std::vector<Layer>* outLayers,
339                            std::vector<int>* outReleaseFences) override;
340 
341     Error presentDisplay(Display display, int* outPresentFence) override;
342 
343     Error setActiveConfig(Display display, Config config) override;
344 
345     /*
346      * The composer caches client targets internally.  When target is nullptr,
347      * the composer uses slot to look up the client target from its cache.
348      * When target is not nullptr, the cache is updated with the new target.
349      */
350     Error setClientTarget(Display display, uint32_t slot, const sp<GraphicBuffer>& target,
351                           int acquireFence, Dataspace dataspace,
352                           const std::vector<IComposerClient::Rect>& damage) override;
353     Error setColorMode(Display display, ColorMode mode, RenderIntent renderIntent) override;
354     Error setColorTransform(Display display, const float* matrix, ColorTransform hint) override;
355     Error setOutputBuffer(Display display, const native_handle_t* buffer,
356                           int releaseFence) override;
357     Error setPowerMode(Display display, IComposerClient::PowerMode mode) override;
358     Error setVsyncEnabled(Display display, IComposerClient::Vsync enabled) override;
359 
360     Error setClientTargetSlotCount(Display display) override;
361 
362     Error validateDisplay(Display display, uint32_t* outNumTypes,
363                           uint32_t* outNumRequests) override;
364 
365     Error presentOrValidateDisplay(Display display, uint32_t* outNumTypes, uint32_t* outNumRequests,
366                                    int* outPresentFence, uint32_t* state) override;
367 
368     Error setCursorPosition(Display display, Layer layer, int32_t x, int32_t y) override;
369     /* see setClientTarget for the purpose of slot */
370     Error setLayerBuffer(Display display, Layer layer, uint32_t slot,
371                          const sp<GraphicBuffer>& buffer, int acquireFence) override;
372     Error setLayerSurfaceDamage(Display display, Layer layer,
373                                 const std::vector<IComposerClient::Rect>& damage) override;
374     Error setLayerBlendMode(Display display, Layer layer, IComposerClient::BlendMode mode) override;
375     Error setLayerColor(Display display, Layer layer, const IComposerClient::Color& color) override;
376     Error setLayerCompositionType(Display display, Layer layer,
377                                   IComposerClient::Composition type) override;
378     Error setLayerDataspace(Display display, Layer layer, Dataspace dataspace) override;
379     Error setLayerDisplayFrame(Display display, Layer layer,
380                                const IComposerClient::Rect& frame) override;
381     Error setLayerPlaneAlpha(Display display, Layer layer, float alpha) override;
382     Error setLayerSidebandStream(Display display, Layer layer,
383                                  const native_handle_t* stream) override;
384     Error setLayerSourceCrop(Display display, Layer layer,
385                              const IComposerClient::FRect& crop) override;
386     Error setLayerTransform(Display display, Layer layer, Transform transform) override;
387     Error setLayerVisibleRegion(Display display, Layer layer,
388                                 const std::vector<IComposerClient::Rect>& visible) override;
389     Error setLayerZOrder(Display display, Layer layer, uint32_t z) override;
390     Error setLayerInfo(Display display, Layer layer, uint32_t type, uint32_t appId) override;
391 
392     // Composer HAL 2.2
393     Error setLayerPerFrameMetadata(
394             Display display, Layer layer,
395             const std::vector<IComposerClient::PerFrameMetadata>& perFrameMetadatas) override;
396     std::vector<IComposerClient::PerFrameMetadataKey> getPerFrameMetadataKeys(
397             Display display) override;
398     Error getRenderIntents(Display display, ColorMode colorMode,
399             std::vector<RenderIntent>* outRenderIntents) override;
400     Error getDataspaceSaturationMatrix(Dataspace dataspace, mat4* outMatrix) override;
401 
402     // Composer HAL 2.3
403     Error getDisplayIdentificationData(Display display, uint8_t* outPort,
404                                        std::vector<uint8_t>* outData) override;
405     Error setLayerColorTransform(Display display, Layer layer, const float* matrix) override;
406     Error getDisplayedContentSamplingAttributes(Display display, PixelFormat* outFormat,
407                                                 Dataspace* outDataspace,
408                                                 uint8_t* outComponentMask) override;
409     Error setDisplayContentSamplingEnabled(Display display, bool enabled, uint8_t componentMask,
410                                            uint64_t maxFrames) override;
411     Error getDisplayedContentSample(Display display, uint64_t maxFrames, uint64_t timestamp,
412                                     DisplayedFrameStats* outStats) override;
413     Error getDisplayCapabilities(Display display,
414                                  std::vector<DisplayCapability>* outCapabilities) override;
415     Error setLayerPerFrameMetadataBlobs(
416             Display display, Layer layer,
417             const std::vector<IComposerClient::PerFrameMetadataBlob>& metadata) override;
418     Error setDisplayBrightness(Display display, float brightness) override;
419 
420 private:
421     class CommandWriter : public CommandWriterBase {
422     public:
423         explicit CommandWriter(uint32_t initialMaxSize);
424         ~CommandWriter() override;
425 
426         void setLayerInfo(uint32_t type, uint32_t appId);
427         void setClientTargetMetadata(
428                 const IVrComposerClient::BufferMetadata& metadata);
429         void setLayerBufferMetadata(
430                 const IVrComposerClient::BufferMetadata& metadata);
431 
432     private:
433         void writeBufferMetadata(
434                 const IVrComposerClient::BufferMetadata& metadata);
435     };
436 
437     // Many public functions above simply write a command into the command
438     // queue to batch the calls.  validateDisplay and presentDisplay will call
439     // this function to execute the command queue.
440     Error execute();
441 
442     sp<V2_1::IComposer> mComposer;
443 
444     sp<V2_1::IComposerClient> mClient;
445     sp<V2_2::IComposerClient> mClient_2_2;
446     sp<IComposerClient> mClient_2_3;
447 
448     // 64KiB minus a small space for metadata such as read/write pointers
449     static constexpr size_t kWriterInitialSize =
450         64 * 1024 / sizeof(uint32_t) - 16;
451     CommandWriter mWriter;
452     CommandReader mReader;
453 
454     // When true, the we attach to the vr_hwcomposer service instead of the
455     // hwcomposer. This allows us to redirect surfaces to 3d surfaces in vr.
456     const bool mIsUsingVrComposer;
457 };
458 
459 } // namespace impl
460 
461 } // namespace Hwc2
462 
463 } // namespace android
464 
465 #endif // ANDROID_SF_COMPOSER_HAL_H
466