1 /*
2  * Copyright 2022 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 <aidl/android/hardware/graphics/composer3/CommandResultPayload.h>
20 #include <aidl/android/hardware/graphics/composer3/IComposerClient.h>
21 #include <android-base/logging.h>
22 #include <inttypes.h>
23 #include <string.h>
24 
25 #include <algorithm>
26 #include <limits>
27 #include <memory>
28 #include <vector>
29 
30 namespace aidl::android::hardware::graphics::composer3::impl {
31 
32 class ComposerServiceWriter {
33   public:
ComposerServiceWriter()34     ComposerServiceWriter() { reset(); }
35 
~ComposerServiceWriter()36     virtual ~ComposerServiceWriter() { reset(); }
37 
reset()38     void reset() { mCommandsResults.clear(); }
39 
setError(int32_t index,int32_t errorCode)40     void setError(int32_t index, int32_t errorCode) {
41         CommandError error;
42         error.commandIndex = index;
43         error.errorCode = errorCode;
44         mCommandsResults.emplace_back(std::move(error));
45     }
46 
setPresentOrValidateResult(int64_t display,PresentOrValidate::Result result)47     void setPresentOrValidateResult(int64_t display, PresentOrValidate::Result result) {
48         PresentOrValidate presentOrValidate;
49         presentOrValidate.display = display;
50         presentOrValidate.result = result;
51         mCommandsResults.emplace_back(std::move(presentOrValidate));
52     }
53 
setChangedCompositionTypes(int64_t display,const std::vector<int64_t> & layers,const std::vector<Composition> & types)54     void setChangedCompositionTypes(int64_t display, const std::vector<int64_t>& layers,
55                                     const std::vector<Composition>& types) {
56         ChangedCompositionTypes changedCompositionTypes;
57         changedCompositionTypes.display = display;
58         changedCompositionTypes.layers.reserve(layers.size());
59         for (int i = 0; i < layers.size(); i++) {
60             auto layer = ChangedCompositionLayer{.layer = layers[i], .composition = types[i]};
61             changedCompositionTypes.layers.emplace_back(std::move(layer));
62         }
63         mCommandsResults.emplace_back(std::move(changedCompositionTypes));
64     }
65 
setDisplayRequests(int64_t display,int32_t displayRequestMask,const std::vector<int64_t> & layers,const std::vector<int32_t> & layerRequestMasks)66     void setDisplayRequests(int64_t display, int32_t displayRequestMask,
67                             const std::vector<int64_t>& layers,
68                             const std::vector<int32_t>& layerRequestMasks) {
69         DisplayRequest displayRequest;
70         displayRequest.display = display;
71         displayRequest.mask = displayRequestMask;
72         displayRequest.layerRequests.reserve(layers.size());
73         for (int i = 0; i < layers.size(); i++) {
74             auto layerRequest =
75                     DisplayRequest::LayerRequest{.layer = layers[i], .mask = layerRequestMasks[i]};
76             displayRequest.layerRequests.emplace_back(std::move(layerRequest));
77         }
78         mCommandsResults.emplace_back(std::move(displayRequest));
79     }
80 
setPresentFence(int64_t display,::ndk::ScopedFileDescriptor presentFence)81     void setPresentFence(int64_t display, ::ndk::ScopedFileDescriptor presentFence) {
82         if (presentFence.get() >= 0) {
83             PresentFence presentFenceCommand;
84             presentFenceCommand.fence = std::move(presentFence);
85             presentFenceCommand.display = display;
86             mCommandsResults.emplace_back(std::move(presentFenceCommand));
87         } else {
88             LOG(WARNING) << __func__ << ": invalid present fence " << presentFence.get();
89         }
90     }
91 
setReleaseFences(int64_t display,const std::vector<int64_t> & layers,std::vector<::ndk::ScopedFileDescriptor> releaseFences)92     void setReleaseFences(int64_t display, const std::vector<int64_t>& layers,
93                           std::vector<::ndk::ScopedFileDescriptor> releaseFences) {
94         ReleaseFences releaseFencesCommand;
95         releaseFencesCommand.display = display;
96         for (int i = 0; i < layers.size(); i++) {
97             if (releaseFences[i].get() >= 0) {
98                 ReleaseFences::Layer layer;
99                 layer.layer = layers[i];
100                 layer.fence = std::move(releaseFences[i]);
101                 releaseFencesCommand.layers.emplace_back(std::move(layer));
102             } else {
103                 LOG(WARNING) << __func__ << ": invalid release fence " << releaseFences[i].get();
104             }
105         }
106         mCommandsResults.emplace_back(std::move(releaseFencesCommand));
107     }
108 
setClientTargetProperty(int64_t display,const ClientTargetProperty & clientTargetProperty,float brightness,const DimmingStage & dimmingStage)109     void setClientTargetProperty(int64_t display, const ClientTargetProperty& clientTargetProperty,
110                                  float brightness, const DimmingStage& dimmingStage) {
111         ClientTargetPropertyWithBrightness clientTargetPropertyWithBrightness;
112         clientTargetPropertyWithBrightness.display = display;
113         clientTargetPropertyWithBrightness.clientTargetProperty = clientTargetProperty;
114         clientTargetPropertyWithBrightness.brightness = brightness;
115         clientTargetPropertyWithBrightness.dimmingStage = dimmingStage;
116         mCommandsResults.emplace_back(std::move(clientTargetPropertyWithBrightness));
117     }
118 
getPendingCommandResults()119     std::vector<CommandResultPayload> getPendingCommandResults() {
120         return std::move(mCommandsResults);
121     }
122 
123   private:
124     std::vector<CommandResultPayload> mCommandsResults;
125 };
126 
127 }  // namespace aidl::android::hardware::graphics::composer3::impl