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 <algorithm>
27#include <limits>
28#include <memory>
29#include <vector>
30
31#include <inttypes.h>
32#include <string.h>
33
34#include <android/hardware/graphics/composer/2.2/IComposer.h>
35#include <android/hardware/graphics/composer/2.2/IComposerClient.h>
36#include <fmq/MessageQueue.h>
37#include <log/log.h>
38#include <sync/sync.h>
39
40#include <composer-command-buffer/2.1/ComposerCommandBuffer.h>
41
42namespace android {
43namespace hardware {
44namespace graphics {
45namespace composer {
46namespace V2_2 {
47
48using android::hardware::MessageQueue;
49using android::hardware::graphics::common::V1_0::ColorTransform;
50using android::hardware::graphics::common::V1_0::Transform;
51using android::hardware::graphics::common::V1_1::Dataspace;
52using android::hardware::graphics::composer::V2_1::Config;
53using android::hardware::graphics::composer::V2_1::Display;
54using android::hardware::graphics::composer::V2_1::Error;
55using android::hardware::graphics::composer::V2_1::IComposerCallback;
56using android::hardware::graphics::composer::V2_1::Layer;
57using android::hardware::graphics::composer::V2_2::IComposerClient;
58
59using CommandQueueType = MessageQueue<uint32_t, kSynchronizedReadWrite>;
60
61// This class helps build a command queue.  Note that all sizes/lengths are in
62// units of uint32_t's.
63class CommandWriterBase : public V2_1::CommandWriterBase {
64   public:
65    CommandWriterBase(uint32_t initialMaxSize) : V2_1::CommandWriterBase(initialMaxSize) {}
66
67    void setClientTarget(uint32_t slot, const native_handle_t* target, int acquireFence,
68                         Dataspace dataspace, const std::vector<IComposerClient::Rect>& damage) {
69        setClientTargetInternal(slot, target, acquireFence, static_cast<int32_t>(dataspace),
70                                damage);
71    }
72
73    void setLayerDataspace(Dataspace dataspace) {
74        setLayerDataspaceInternal(static_cast<int32_t>(dataspace));
75    }
76
77    static constexpr uint16_t kSetLayerFloatColorLength = 4;
78    void setLayerFloatColor(IComposerClient::FloatColor color) {
79        beginCommand_2_2(IComposerClient::Command::SET_LAYER_FLOAT_COLOR,
80                         kSetLayerFloatColorLength);
81        writeFloatColor(color);
82        endCommand();
83    }
84
85    void setLayerPerFrameMetadata(const hidl_vec<IComposerClient::PerFrameMetadata>& metadataVec) {
86        beginCommand_2_2(IComposerClient::Command::SET_LAYER_PER_FRAME_METADATA,
87                         metadataVec.size() * 2);
88        for (const auto& metadata : metadataVec) {
89            writeSigned(static_cast<int32_t>(metadata.key));
90            writeFloat(metadata.value);
91        }
92        endCommand();
93    }
94
95   protected:
96    void beginCommand_2_2(IComposerClient::Command command, uint16_t length) {
97        V2_1::CommandWriterBase::beginCommand(
98            static_cast<V2_1::IComposerClient::Command>(static_cast<int32_t>(command)), length);
99    }
100
101    void writeFloatColor(const IComposerClient::FloatColor& color) {
102        writeFloat(color.r);
103        writeFloat(color.g);
104        writeFloat(color.b);
105        writeFloat(color.a);
106    }
107};
108
109// This class helps parse a command queue.  Note that all sizes/lengths are in
110// units of uint32_t's.
111class CommandReaderBase : public V2_1::CommandReaderBase {
112   public:
113    CommandReaderBase() : V2_1::CommandReaderBase(){};
114
115   protected:
116    IComposerClient::FloatColor readFloatColor() {
117        float r = readFloat();
118        float g = readFloat();
119        float b = readFloat();
120        float a = readFloat();
121        return IComposerClient::FloatColor{r, g, b, a};
122    }
123};
124
125}  // namespace V2_2
126}  // namespace composer
127}  // namespace graphics
128}  // namespace hardware
129}  // namespace android
130