1 /*
2  * Copyright 2019 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.4/IComposer.h>
27 #include <android/hardware/graphics/composer/2.4/IComposerClient.h>
28 #include <composer-command-buffer/2.3/ComposerCommandBuffer.h>
29 
30 namespace android {
31 namespace hardware {
32 namespace graphics {
33 namespace composer {
34 namespace V2_4 {
35 
36 using android::hardware::MessageQueue;
37 using android::hardware::graphics::composer::V2_4::Error;
38 using android::hardware::graphics::composer::V2_4::IComposerClient;
39 
40 // This class helps build a command queue.  Note that all sizes/lengths are in
41 // units of uint32_t's.
42 class CommandWriterBase : public V2_3::CommandWriterBase {
43   public:
44     static constexpr uint16_t kSetClientTargetPropertyLength = 2;
45 
CommandWriterBase(uint32_t initialMaxSize)46     CommandWriterBase(uint32_t initialMaxSize) : V2_3::CommandWriterBase(initialMaxSize) {}
47 
setClientTargetProperty(const IComposerClient::ClientTargetProperty & clientTargetProperty)48     void setClientTargetProperty(
49             const IComposerClient::ClientTargetProperty& clientTargetProperty) {
50         beginCommand(IComposerClient::Command::SET_CLIENT_TARGET_PROPERTY,
51                      kSetClientTargetPropertyLength);
52         writeSigned(static_cast<int32_t>(clientTargetProperty.pixelFormat));
53         writeSigned(static_cast<int32_t>(clientTargetProperty.dataspace));
54         endCommand();
55     }
56 
setLayerGenericMetadata(const hidl_string & key,const bool mandatory,const hidl_vec<uint8_t> & value)57     void setLayerGenericMetadata(const hidl_string& key, const bool mandatory,
58                                  const hidl_vec<uint8_t>& value) {
59         const size_t commandSize = 3 + sizeToElements(key.size()) + sizeToElements(value.size());
60         if (commandSize > std::numeric_limits<uint16_t>::max()) {
61             LOG_FATAL("Too much generic metadata (%zu elements)", commandSize);
62             return;
63         }
64 
65         beginCommand(IComposerClient::Command::SET_LAYER_GENERIC_METADATA,
66                      static_cast<uint16_t>(commandSize));
67         write(key.size());
68         writeBlob(key.size(), reinterpret_cast<const unsigned char*>(key.c_str()));
69         write(mandatory);
70         write(value.size());
71         writeBlob(value.size(), value.data());
72         endCommand();
73     }
74 
75   protected:
sizeToElements(uint32_t size)76     uint32_t sizeToElements(uint32_t size) { return (size + 3) / 4; }
77 };
78 
79 // This class helps parse a command queue.  Note that all sizes/lengths are in
80 // units of uint32_t's.
81 class CommandReaderBase : public V2_3::CommandReaderBase {
82   public:
CommandReaderBase()83     CommandReaderBase() : V2_3::CommandReaderBase() {}
84 };
85 
86 }  // namespace V2_4
87 }  // namespace composer
88 }  // namespace graphics
89 }  // namespace hardware
90 }  // namespace android
91