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.2/ComposerCommandBuffer.h>
24 #include <composer-hal/2.1/ComposerCommandEngine.h>
25 #include <composer-hal/2.2/ComposerHal.h>
26 #include <composer-resources/2.2/ComposerResources.h>
27 
28 namespace android {
29 namespace hardware {
30 namespace graphics {
31 namespace composer {
32 namespace V2_2 {
33 namespace hal {
34 
35 class ComposerCommandEngine : public V2_1::hal::ComposerCommandEngine {
36    public:
ComposerCommandEngine(ComposerHal * hal,ComposerResources * resources)37     ComposerCommandEngine(ComposerHal* hal, ComposerResources* resources)
38         : BaseType2_1(hal, resources), mHal(hal) {}
39 
40    protected:
executeCommand(V2_1::IComposerClient::Command command,uint16_t length)41     bool executeCommand(V2_1::IComposerClient::Command command, uint16_t length) override {
42         switch (static_cast<IComposerClient::Command>(command)) {
43             case IComposerClient::Command::SET_LAYER_PER_FRAME_METADATA:
44                 return executeSetLayerPerFrameMetadata(length);
45             case IComposerClient::Command::SET_LAYER_FLOAT_COLOR:
46                 return executeSetLayerFloatColor(length);
47             default:
48                 return BaseType2_1::executeCommand(command, length);
49         }
50     }
51 
createCommandWriter(size_t writerInitialSize)52     std::unique_ptr<V2_1::CommandWriterBase> createCommandWriter(
53             size_t writerInitialSize) override {
54         return std::make_unique<CommandWriterBase>(writerInitialSize);
55     }
56 
executeSetLayerPerFrameMetadata(uint16_t length)57     bool executeSetLayerPerFrameMetadata(uint16_t length) {
58         // (key, value) pairs
59         if (length % 2 != 0) {
60             return false;
61         }
62 
63         std::vector<IComposerClient::PerFrameMetadata> metadata;
64         metadata.reserve(length / 2);
65         while (length > 0) {
66             metadata.emplace_back(IComposerClient::PerFrameMetadata{
67                 static_cast<IComposerClient::PerFrameMetadataKey>(readSigned()), readFloat()});
68             length -= 2;
69         }
70 
71         auto err = mHal->setLayerPerFrameMetadata(mCurrentDisplay, mCurrentLayer, metadata);
72         if (err != Error::NONE) {
73             mWriter->setError(getCommandLoc(), err);
74         }
75 
76         return true;
77     }
78 
executeSetLayerFloatColor(uint16_t length)79     bool executeSetLayerFloatColor(uint16_t length) {
80         if (length != CommandWriterBase::kSetLayerFloatColorLength) {
81             return false;
82         }
83 
84         auto err = mHal->setLayerFloatColor(mCurrentDisplay, mCurrentLayer, readFloatColor());
85         if (err != Error::NONE) {
86             mWriter->setError(getCommandLoc(), err);
87         }
88 
89         return true;
90     }
91 
readFloatColor()92     IComposerClient::FloatColor readFloatColor() {
93         return IComposerClient::FloatColor{readFloat(), readFloat(), readFloat(), readFloat()};
94     }
95 
96    private:
97     using BaseType2_1 = V2_1::hal::ComposerCommandEngine;
98     using BaseType2_1::mWriter;
99 
100     ComposerHal* mHal;
101 };
102 
103 }  // namespace hal
104 }  // namespace V2_2
105 }  // namespace composer
106 }  // namespace graphics
107 }  // namespace hardware
108 }  // namespace android
109