• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_2_3(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_2_3(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         size_t commandLength = 0;
83 
84         if (metadata.size() > std::numeric_limits<uint32_t>::max()) {
85             LOG_FATAL("too many metadata blobs - dynamic metadata size is too large");
86             return;
87         }
88 
89         // number of blobs
90         commandLength += metadata.size();
91 
92         for (auto metadataBlob : metadata) {
93             commandLength += sizeof(int32_t);  // key of metadata blob
94             commandLength += 1;                // size information of metadata blob
95 
96             // metadata content size
97             size_t metadataSize = metadataBlob.blob.size() / sizeof(uint32_t);
98             commandLength += metadataSize;
99             commandLength +=
100                 (metadataBlob.blob.size() - (metadataSize * sizeof(uint32_t)) > 0) ? 1 : 0;
101         }
102 
103         if (commandLength > std::numeric_limits<uint16_t>::max()) {
104             LOG_FATAL("dynamic metadata size is too large");
105             return;
106         }
107 
108         // Blobs are written as:
109         // {numElements, key1, size1, blob1, key2, size2, blob2, key3, size3...}
110         uint16_t length = static_cast<uint16_t>(commandLength);
111         beginCommand_2_3(IComposerClient::Command::SET_LAYER_PER_FRAME_METADATA_BLOBS, length);
112         write(static_cast<uint32_t>(metadata.size()));
113         for (auto metadataBlob : metadata) {
114             writeSigned(static_cast<int32_t>(metadataBlob.key));
115             write(static_cast<uint32_t>(metadataBlob.blob.size()));
116             writeBlob(static_cast<uint32_t>(metadataBlob.blob.size()), metadataBlob.blob.data());
117         }
118         endCommand();
119     }
120 
121    protected:
beginCommand_2_3(IComposerClient::Command command,uint16_t length)122     void beginCommand_2_3(IComposerClient::Command command, uint16_t length) {
123         V2_2::CommandWriterBase::beginCommand_2_2(
124             static_cast<V2_2::IComposerClient::Command>(static_cast<int32_t>(command)), length);
125     }
126 
writeBlob(uint32_t length,const unsigned char * blob)127     void writeBlob(uint32_t length, const unsigned char* blob) {
128         memcpy(&mData[mDataWritten], blob, length);
129         uint32_t numElements = length / 4;
130         mDataWritten += numElements;
131         mDataWritten += (length - (numElements * 4) > 0) ? 1 : 0;
132     }
133 };
134 
135 // This class helps parse a command queue.  Note that all sizes/lengths are in
136 // units of uint32_t's.
137 class CommandReaderBase : public V2_2::CommandReaderBase {
138    public:
CommandReaderBase()139     CommandReaderBase() : V2_2::CommandReaderBase(){};
140 };
141 
142 }  // namespace V2_3
143 }  // namespace composer
144 }  // namespace graphics
145 }  // namespace hardware
146 }  // namespace android
147