1 /*
2  * Copyright (C) 2021 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 #include <android/hardware/graphics/composer3/ComposerServiceWriter.h>
20 #include <utils/Mutex.h>
21 
22 #include <memory>
23 
24 #include "include/IComposerHal.h"
25 #include "include/IResourceManager.h"
26 
27 namespace aidl::android::hardware::graphics::composer3::impl {
28 
29 class ComposerCommandEngine {
30   public:
ComposerCommandEngine(IComposerHal * hal,IResourceManager * resources)31       ComposerCommandEngine(IComposerHal* hal, IResourceManager* resources)
32             : mHal(hal), mResources(resources) {}
33       int32_t init();
34 
35       int32_t execute(const std::vector<DisplayCommand>& commands,
36                       std::vector<CommandResultPayload>* result);
37 
38       template <typename InputType, typename Functor>
39       void dispatchLayerCommand(int64_t display, int64_t layer, const std::string& funcName,
40                                 const InputType input, const Functor func);
41       void dispatchBatchCreateDestroyLayerCommand(int64_t display, const LayerCommand& cmd);
reset()42       void reset() {
43           mWriter->reset();
44       }
45 
46   private:
47       void dispatchDisplayCommand(const DisplayCommand& displayCommand);
48       void dispatchLayerCommand(int64_t display, const LayerCommand& displayCommand);
49 
50       void executeSetColorTransform(int64_t display, const std::vector<float>& matrix);
51       void executeSetClientTarget(int64_t display, const ClientTarget& command);
52       void executeSetDisplayBrightness(uint64_t display, const DisplayBrightness& command);
53       void executeSetOutputBuffer(uint64_t display, const Buffer& buffer);
54       void executeValidateDisplay(int64_t display,
55                                   const std::optional<ClockMonotonicTimestamp> expectedPresentTime,
56                                   int frameIntervalNs);
57       void executePresentOrValidateDisplay(
58               int64_t display, const std::optional<ClockMonotonicTimestamp> expectedPresentTime,
59               int frameIntervalNs);
60       void executeAcceptDisplayChanges(int64_t display);
61       int executePresentDisplay(int64_t display);
62 
63       void executeSetLayerCursorPosition(int64_t display, int64_t layer,
64                                          const common::Point& cursorPosition);
65       void executeSetLayerBuffer(int64_t display, int64_t layer, const Buffer& buffer);
66       void executeSetLayerSurfaceDamage(int64_t display, int64_t layer,
67                                         const std::vector<std::optional<common::Rect>>& damage);
68       void executeSetLayerBlendMode(int64_t display, int64_t layer,
69                                     const ParcelableBlendMode& blendMode);
70       void executeSetLayerColor(int64_t display, int64_t layer, const Color& color);
71       void executeSetLayerComposition(int64_t display, int64_t layer,
72                                       const ParcelableComposition& composition);
73       void executeSetLayerDataspace(int64_t display, int64_t layer,
74                                     const ParcelableDataspace& dataspace);
75       void executeSetLayerDisplayFrame(int64_t display, int64_t layer, const common::Rect& rect);
76       void executeSetLayerPlaneAlpha(int64_t display, int64_t layer, const PlaneAlpha& planeAlpha);
77       void executeSetLayerSidebandStream(int64_t display, int64_t layer,
78                                          const AidlNativeHandle& sidebandStream);
79       void executeSetLayerSourceCrop(int64_t display, int64_t layer,
80                                      const common::FRect& sourceCrop);
81       void executeSetLayerTransform(int64_t display, int64_t layer,
82                                     const ParcelableTransform& transform);
83       void executeSetLayerVisibleRegion(
84               int64_t display, int64_t layer,
85               const std::vector<std::optional<common::Rect>>& visibleRegion);
86       void executeSetLayerZOrder(int64_t display, int64_t layer, const ZOrder& zOrder);
87       void executeSetLayerPerFrameMetadata(
88               int64_t display, int64_t layer,
89               const std::vector<std::optional<PerFrameMetadata>>& perFrameMetadata);
90       void executeSetLayerColorTransform(int64_t display, int64_t layer,
91                                          const std::vector<float>& colorTransform);
92       void executeSetLayerPerFrameMetadataBlobs(int64_t display, int64_t layer,
93               const std::vector<std::optional<PerFrameMetadataBlob>>& perFrameMetadataBlob);
94       void executeSetLayerBrightness(int64_t display, int64_t layer,
95                                      const LayerBrightness& brightness);
96       void executeSetLayerBufferSlotsToClear(int64_t display, int64_t layer,
97                                              const std::vector<int32_t>& bufferSlotsToClear);
98 
99       int32_t executeValidateDisplayInternal(int64_t display);
100       void executeSetExpectedPresentTimeInternal(
101               int64_t display, const std::optional<ClockMonotonicTimestamp> expectedPresentTime,
102               int frameIntervalNs);
103 
104       IComposerHal* mHal;
105       IResourceManager* mResources;
106       std::unique_ptr<ComposerServiceWriter> mWriter;
107       int32_t mCommandIndex;
108 };
109 
110 template <typename InputType, typename Functor>
dispatchLayerCommand(int64_t display,int64_t layer,const std::string & funcName,const InputType input,const Functor func)111 void ComposerCommandEngine::dispatchLayerCommand(int64_t display, int64_t layer,
112                                                  const std::string& funcName, const InputType input,
113                                                  const Functor func) {
114     if (input) {
115         auto err = (mHal->*func)(display, layer, *input);
116         if (err) {
117             LOG(ERROR) << funcName << ": err " << err;
118             mWriter->setError(mCommandIndex, err);
119         }
120     }
121 };
122 
123 } // namespace aidl::android::hardware::graphics::composer3::impl
124 
125