1 /*
2  * Copyright 2018 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 #pragma once
18 
19 #ifndef LOG_TAG
20 #warn "ComposerCommandBuffer.h included without LOG_TAG"
21 #endif
22 
23 #undef LOG_NDEBUG
24 #define LOG_NDEBUG 0
25 
26 #include <android/hardware/graphics/composer/2.3/IComposer.h>
27 #include <android/hardware/graphics/composer/2.3/IComposerClient.h>
28 #include <composer-command-buffer/2.2/ComposerCommandBuffer.h>
29 #include <limits>
30 
31 namespace android {
32 namespace hardware {
33 namespace graphics {
34 namespace composer {
35 namespace V2_3 {
36 
37 using android::hardware::MessageQueue;
38 using android::hardware::graphics::common::V1_2::Dataspace;
39 using android::hardware::graphics::composer::V2_1::Error;
40 using android::hardware::graphics::composer::V2_1::IComposerCallback;
41 using android::hardware::graphics::composer::V2_1::Layer;
42 using android::hardware::graphics::composer::V2_3::IComposerClient;
43 
44 // This class helps build a command queue.  Note that all sizes/lengths are in
45 // units of uint32_t's.
46 class CommandWriterBase : public V2_2::CommandWriterBase {
47    public:
setLayerPerFrameMetadata(const hidl_vec<IComposerClient::PerFrameMetadata> & metadataVec)48     void setLayerPerFrameMetadata(const hidl_vec<IComposerClient::PerFrameMetadata>& metadataVec) {
49         beginCommand(IComposerClient::Command::SET_LAYER_PER_FRAME_METADATA,
50                      metadataVec.size() * 2);
51         for (const auto& metadata : metadataVec) {
52             writeSigned(static_cast<int32_t>(metadata.key));
53             writeFloat(metadata.value);
54         }
55         endCommand();
56     }
57 
setLayerDataspace(Dataspace dataspace)58     void setLayerDataspace(Dataspace dataspace) {
59         setLayerDataspaceInternal(static_cast<int32_t>(dataspace));
60     }
61 
setClientTarget(uint32_t slot,const native_handle_t * target,int acquireFence,Dataspace dataspace,const std::vector<IComposerClient::Rect> & damage)62     void setClientTarget(uint32_t slot, const native_handle_t* target, int acquireFence,
63                          Dataspace dataspace, const std::vector<IComposerClient::Rect>& damage) {
64         setClientTargetInternal(slot, target, acquireFence, static_cast<int32_t>(dataspace),
65                                 damage);
66     }
67 
CommandWriterBase(uint32_t initialMaxSize)68     CommandWriterBase(uint32_t initialMaxSize) : V2_2::CommandWriterBase(initialMaxSize) {}
69 
70     static constexpr uint16_t kSetLayerColorTransformLength = 16;
setLayerColorTransform(const float * matrix)71     void setLayerColorTransform(const float* matrix) {
72         beginCommand(IComposerClient::Command::SET_LAYER_COLOR_TRANSFORM,
73                      kSetLayerColorTransformLength);
74         for (int i = 0; i < 16; i++) {
75             writeFloat(matrix[i]);
76         }
77         endCommand();
78     }
79 
setLayerPerFrameMetadataBlobs(const hidl_vec<IComposerClient::PerFrameMetadataBlob> & metadata)80     void setLayerPerFrameMetadataBlobs(
81         const hidl_vec<IComposerClient::PerFrameMetadataBlob>& metadata) {
82         // in units of uint32_t's
83         size_t commandLength = 0;
84 
85         if (metadata.size() > std::numeric_limits<uint32_t>::max()) {
86             LOG_FATAL("too many metadata blobs - dynamic metadata size is too large");
87             return;
88         }
89 
90         // space for numElements
91         commandLength += 1;
92 
93         for (auto metadataBlob : metadata) {
94             commandLength += 1;  // key of metadata blob
95             commandLength += 1;  // size information of metadata blob
96 
97             // metadata content size
98             size_t metadataSize = metadataBlob.blob.size() / sizeof(uint32_t);
99             commandLength += metadataSize;
100             commandLength +=
101                 (metadataBlob.blob.size() - (metadataSize * sizeof(uint32_t)) > 0) ? 1 : 0;
102         }
103 
104         if (commandLength > std::numeric_limits<uint16_t>::max()) {
105             LOG_FATAL("dynamic metadata size is too large");
106             return;
107         }
108 
109         // Blobs are written as:
110         // {numElements, key1, size1, blob1, key2, size2, blob2, key3, size3...}
111         uint16_t length = static_cast<uint16_t>(commandLength);
112         beginCommand(IComposerClient::Command::SET_LAYER_PER_FRAME_METADATA_BLOBS, length);
113         write(static_cast<uint32_t>(metadata.size()));
114         for (auto metadataBlob : metadata) {
115             writeSigned(static_cast<int32_t>(metadataBlob.key));
116             write(static_cast<uint32_t>(metadataBlob.blob.size()));
117             writeBlob(static_cast<uint32_t>(metadataBlob.blob.size()), metadataBlob.blob.data());
118         }
119         endCommand();
120     }
121 
122    protected:
writeBlob(uint32_t length,const unsigned char * blob)123     void writeBlob(uint32_t length, const unsigned char* blob) {
124         memcpy(&mData[mDataWritten], blob, length);
125         uint32_t numElements = length / 4;
126         mDataWritten += numElements;
127         mDataWritten += (length - (numElements * 4) > 0) ? 1 : 0;
128     }
129 };
130 
131 // This class helps parse a command queue.  Note that all sizes/lengths are in
132 // units of uint32_t's.
133 class CommandReaderBase : public V2_2::CommandReaderBase {
134    public:
CommandReaderBase()135     CommandReaderBase() : V2_2::CommandReaderBase(){};
136 };
137 
138 }  // namespace V2_3
139 }  // namespace composer
140 }  // namespace graphics
141 }  // namespace hardware
142 }  // namespace android
143