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 #warning "ComposerCommandEngine.h included without LOG_TAG"
21 #endif
22 
23 #include <composer-command-buffer/2.3/ComposerCommandBuffer.h>
24 #include <composer-hal/2.1/ComposerCommandEngine.h>
25 #include <composer-hal/2.2/ComposerCommandEngine.h>
26 #include <composer-hal/2.3/ComposerHal.h>
27 #include <composer-resources/2.2/ComposerResources.h>
28 
29 namespace android {
30 namespace hardware {
31 namespace graphics {
32 namespace composer {
33 namespace V2_3 {
34 namespace hal {
35 
36 class ComposerCommandEngine : public V2_2::hal::ComposerCommandEngine {
37    public:
ComposerCommandEngine(ComposerHal * hal,V2_2::hal::ComposerResources * resources)38     ComposerCommandEngine(ComposerHal* hal, V2_2::hal::ComposerResources* resources)
39         : BaseType2_2(hal, resources), mHal(hal) {}
40 
41    protected:
executeCommand(V2_1::IComposerClient::Command command,uint16_t length)42     bool executeCommand(V2_1::IComposerClient::Command command, uint16_t length) override {
43         switch (static_cast<IComposerClient::Command>(command)) {
44             case IComposerClient::Command::SET_LAYER_COLOR_TRANSFORM:
45                 return executeSetLayerColorTransform(length);
46             case IComposerClient::Command::SET_LAYER_PER_FRAME_METADATA_BLOBS:
47                 return executeSetLayerPerFrameMetadataBlobs(length);
48             default:
49                 return BaseType2_2::executeCommand(command, length);
50         }
51     }
52 
createCommandWriter(size_t writerInitialSize)53     std::unique_ptr<V2_1::CommandWriterBase> createCommandWriter(
54             size_t writerInitialSize) override {
55         return std::make_unique<CommandWriterBase>(writerInitialSize);
56     }
57 
executeSetLayerColorTransform(uint16_t length)58     bool executeSetLayerColorTransform(uint16_t length) {
59         if (length != CommandWriterBase::kSetLayerColorTransformLength) {
60             return false;
61         }
62 
63         float matrix[16];
64         for (int i = 0; i < 16; i++) {
65             matrix[i] = readFloat();
66         }
67         auto err = mHal->setLayerColorTransform(mCurrentDisplay, mCurrentLayer, matrix);
68         if (err != Error::NONE) {
69             mWriter->setError(getCommandLoc(), err);
70         }
71 
72         return true;
73     }
74 
executeSetLayerPerFrameMetadataBlobs(uint16_t length)75     bool executeSetLayerPerFrameMetadataBlobs(uint16_t length) {
76         // must have at least one metadata blob
77         // of at least size 1 in queue (i.e {/*numBlobs=*/1, key, size, blob})
78         if (length < 4) {
79             return false;
80         }
81 
82         uint32_t numBlobs = read();
83         length--;
84 
85         std::vector<IComposerClient::PerFrameMetadataBlob> metadata;
86 
87         for (size_t i = 0; i < numBlobs; i++) {
88             IComposerClient::PerFrameMetadataKey key =
89                 static_cast<IComposerClient::PerFrameMetadataKey>(readSigned());
90             uint32_t blobSize = read();
91 
92             length -= 2;
93 
94             if (length * sizeof(uint32_t) < blobSize) {
95                 return false;
96             }
97 
98             metadata.push_back({key, std::vector<uint8_t>()});
99             IComposerClient::PerFrameMetadataBlob& metadataBlob = metadata.back();
100             metadataBlob.blob.resize(blobSize);
101             readBlob(blobSize, metadataBlob.blob.data());
102         }
103         auto err = mHal->setLayerPerFrameMetadataBlobs(mCurrentDisplay, mCurrentLayer, metadata);
104         if (err != Error::NONE) {
105             mWriter->setError(getCommandLoc(), err);
106         }
107         return true;
108     }
109 
readBlob(uint32_t size,void * blob)110     void readBlob(uint32_t size, void* blob) {
111         memcpy(blob, &mData[mDataRead], size);
112         uint32_t numElements = size / sizeof(uint32_t);
113         mDataRead += numElements;
114         mDataRead += (size - numElements * sizeof(uint32_t) != 0) ? 1 : 0;
115     }
116 
117    private:
118     using BaseType2_1 = V2_1::hal::ComposerCommandEngine;
119     using BaseType2_2 = V2_2::hal::ComposerCommandEngine;
120     using BaseType2_1::mWriter;
121 
122     ComposerHal* mHal;
123 };
124 
125 }  // namespace hal
126 }  // namespace V2_3
127 }  // namespace composer
128 }  // namespace graphics
129 }  // namespace hardware
130 }  // namespace android
131