1/*
2 * Copyright 2016 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#ifndef ANDROID_SF_COMPOSER_HAL_H
18#define ANDROID_SF_COMPOSER_HAL_H
19
20#include <memory>
21#include <string>
22#include <unordered_map>
23#include <utility>
24#include <vector>
25
26#include <android/frameworks/vr/composer/1.0/IVrComposerClient.h>
27#include <android/hardware/graphics/composer/2.1/IComposer.h>
28#include <utils/StrongPointer.h>
29#include <IComposerCommandBuffer.h>
30
31namespace android {
32
33namespace Hwc2 {
34
35using android::frameworks::vr::composer::V1_0::IVrComposerClient;
36
37using android::hardware::graphics::common::V1_0::ColorMode;
38using android::hardware::graphics::common::V1_0::ColorTransform;
39using android::hardware::graphics::common::V1_0::Dataspace;
40using android::hardware::graphics::common::V1_0::Hdr;
41using android::hardware::graphics::common::V1_0::PixelFormat;
42using android::hardware::graphics::common::V1_0::Transform;
43
44using android::hardware::graphics::composer::V2_1::IComposer;
45using android::hardware::graphics::composer::V2_1::IComposerCallback;
46using android::hardware::graphics::composer::V2_1::IComposerClient;
47using android::hardware::graphics::composer::V2_1::Error;
48using android::hardware::graphics::composer::V2_1::Display;
49using android::hardware::graphics::composer::V2_1::Layer;
50using android::hardware::graphics::composer::V2_1::Config;
51
52using android::hardware::graphics::composer::V2_1::CommandWriterBase;
53using android::hardware::graphics::composer::V2_1::CommandReaderBase;
54
55using android::hardware::kSynchronizedReadWrite;
56using android::hardware::MessageQueue;
57using android::hardware::MQDescriptorSync;
58using android::hardware::hidl_vec;
59using android::hardware::hidl_handle;
60
61class CommandReader : public CommandReaderBase {
62public:
63    ~CommandReader();
64
65    // Parse and execute commands from the command queue.  The commands are
66    // actually return values from the server and will be saved in ReturnData.
67    Error parse();
68
69    // Get and clear saved errors.
70    struct CommandError {
71        uint32_t location;
72        Error error;
73    };
74    std::vector<CommandError> takeErrors();
75
76    bool hasChanges(Display display, uint32_t* outNumChangedCompositionTypes,
77            uint32_t* outNumLayerRequestMasks) const;
78
79    // Get and clear saved changed composition types.
80    void takeChangedCompositionTypes(Display display,
81            std::vector<Layer>* outLayers,
82            std::vector<IComposerClient::Composition>* outTypes);
83
84    // Get and clear saved display requests.
85    void takeDisplayRequests(Display display,
86        uint32_t* outDisplayRequestMask, std::vector<Layer>* outLayers,
87        std::vector<uint32_t>* outLayerRequestMasks);
88
89    // Get and clear saved release fences.
90    void takeReleaseFences(Display display, std::vector<Layer>* outLayers,
91            std::vector<int>* outReleaseFences);
92
93    // Get and clear saved present fence.
94    void takePresentFence(Display display, int* outPresentFence);
95
96    // Get what stage succeeded during PresentOrValidate: Present or Validate
97    void takePresentOrValidateStage(Display display, uint32_t * state);
98
99private:
100    void resetData();
101
102    bool parseSelectDisplay(uint16_t length);
103    bool parseSetError(uint16_t length);
104    bool parseSetChangedCompositionTypes(uint16_t length);
105    bool parseSetDisplayRequests(uint16_t length);
106    bool parseSetPresentFence(uint16_t length);
107    bool parseSetReleaseFences(uint16_t length);
108    bool parseSetPresentOrValidateDisplayResult(uint16_t length);
109
110    struct ReturnData {
111        uint32_t displayRequests = 0;
112
113        std::vector<Layer> changedLayers;
114        std::vector<IComposerClient::Composition> compositionTypes;
115
116        std::vector<Layer> requestedLayers;
117        std::vector<uint32_t> requestMasks;
118
119        int presentFence = -1;
120
121        std::vector<Layer> releasedLayers;
122        std::vector<int> releaseFences;
123
124        uint32_t presentOrValidateState;
125    };
126
127    std::vector<CommandError> mErrors;
128    std::unordered_map<Display, ReturnData> mReturnData;
129
130    // When SELECT_DISPLAY is parsed, this is updated to point to the
131    // display's return data in mReturnData.  We use it to avoid repeated
132    // map lookups.
133    ReturnData* mCurrentReturnData;
134};
135
136// Composer is a wrapper to IComposer, a proxy to server-side composer.
137class Composer {
138public:
139    Composer(bool useVrComposer);
140
141    std::vector<IComposer::Capability> getCapabilities();
142    std::string dumpDebugInfo();
143
144    void registerCallback(const sp<IComposerCallback>& callback);
145
146    // Reset all pending commands in the command buffer. Useful if you want to
147    // skip a frame but have already queued some commands.
148    void resetCommands();
149
150    uint32_t getMaxVirtualDisplayCount();
151    bool isUsingVrComposer() const { return mIsUsingVrComposer; }
152    Error createVirtualDisplay(uint32_t width, uint32_t height,
153            PixelFormat* format, Display* outDisplay);
154    Error destroyVirtualDisplay(Display display);
155
156    Error acceptDisplayChanges(Display display);
157
158    Error createLayer(Display display, Layer* outLayer);
159    Error destroyLayer(Display display, Layer layer);
160
161    Error getActiveConfig(Display display, Config* outConfig);
162    Error getChangedCompositionTypes(Display display,
163            std::vector<Layer>* outLayers,
164            std::vector<IComposerClient::Composition>* outTypes);
165    Error getColorModes(Display display, std::vector<ColorMode>* outModes);
166    Error getDisplayAttribute(Display display, Config config,
167            IComposerClient::Attribute attribute, int32_t* outValue);
168    Error getDisplayConfigs(Display display, std::vector<Config>* outConfigs);
169    Error getDisplayName(Display display, std::string* outName);
170
171    Error getDisplayRequests(Display display, uint32_t* outDisplayRequestMask,
172            std::vector<Layer>* outLayers,
173            std::vector<uint32_t>* outLayerRequestMasks);
174
175    Error getDisplayType(Display display,
176            IComposerClient::DisplayType* outType);
177    Error getDozeSupport(Display display, bool* outSupport);
178    Error getHdrCapabilities(Display display, std::vector<Hdr>* outTypes,
179            float* outMaxLuminance, float* outMaxAverageLuminance,
180            float* outMinLuminance);
181
182    Error getReleaseFences(Display display, std::vector<Layer>* outLayers,
183            std::vector<int>* outReleaseFences);
184
185    Error presentDisplay(Display display, int* outPresentFence);
186
187    Error setActiveConfig(Display display, Config config);
188
189    /*
190     * The composer caches client targets internally.  When target is nullptr,
191     * the composer uses slot to look up the client target from its cache.
192     * When target is not nullptr, the cache is updated with the new target.
193     */
194    Error setClientTarget(Display display, uint32_t slot,
195            const sp<GraphicBuffer>& target,
196            int acquireFence, Dataspace dataspace,
197            const std::vector<IComposerClient::Rect>& damage);
198    Error setColorMode(Display display, ColorMode mode);
199    Error setColorTransform(Display display, const float* matrix,
200            ColorTransform hint);
201    Error setOutputBuffer(Display display, const native_handle_t* buffer,
202            int releaseFence);
203    Error setPowerMode(Display display, IComposerClient::PowerMode mode);
204    Error setVsyncEnabled(Display display, IComposerClient::Vsync enabled);
205
206    Error setClientTargetSlotCount(Display display);
207
208    Error validateDisplay(Display display, uint32_t* outNumTypes,
209            uint32_t* outNumRequests);
210
211    Error presentOrValidateDisplay(Display display, uint32_t* outNumTypes,
212                                   uint32_t* outNumRequests,
213                                   int* outPresentFence,
214                                   uint32_t* state);
215
216    Error setCursorPosition(Display display, Layer layer,
217            int32_t x, int32_t y);
218    /* see setClientTarget for the purpose of slot */
219    Error setLayerBuffer(Display display, Layer layer, uint32_t slot,
220            const sp<GraphicBuffer>& buffer, int acquireFence);
221    Error setLayerSurfaceDamage(Display display, Layer layer,
222            const std::vector<IComposerClient::Rect>& damage);
223    Error setLayerBlendMode(Display display, Layer layer,
224            IComposerClient::BlendMode mode);
225    Error setLayerColor(Display display, Layer layer,
226            const IComposerClient::Color& color);
227    Error setLayerCompositionType(Display display, Layer layer,
228            IComposerClient::Composition type);
229    Error setLayerDataspace(Display display, Layer layer,
230            Dataspace dataspace);
231    Error setLayerDisplayFrame(Display display, Layer layer,
232            const IComposerClient::Rect& frame);
233    Error setLayerPlaneAlpha(Display display, Layer layer,
234            float alpha);
235    Error setLayerSidebandStream(Display display, Layer layer,
236            const native_handle_t* stream);
237    Error setLayerSourceCrop(Display display, Layer layer,
238            const IComposerClient::FRect& crop);
239    Error setLayerTransform(Display display, Layer layer,
240            Transform transform);
241    Error setLayerVisibleRegion(Display display, Layer layer,
242            const std::vector<IComposerClient::Rect>& visible);
243    Error setLayerZOrder(Display display, Layer layer, uint32_t z);
244    Error setLayerInfo(Display display, Layer layer, uint32_t type,
245                       uint32_t appId);
246private:
247    class CommandWriter : public CommandWriterBase {
248    public:
249        CommandWriter(uint32_t initialMaxSize);
250        ~CommandWriter() override;
251
252        void setLayerInfo(uint32_t type, uint32_t appId);
253        void setClientTargetMetadata(
254                const IVrComposerClient::BufferMetadata& metadata);
255        void setLayerBufferMetadata(
256                const IVrComposerClient::BufferMetadata& metadata);
257
258    private:
259        void writeBufferMetadata(
260                const IVrComposerClient::BufferMetadata& metadata);
261    };
262
263    // Many public functions above simply write a command into the command
264    // queue to batch the calls.  validateDisplay and presentDisplay will call
265    // this function to execute the command queue.
266    Error execute();
267
268    sp<IComposer> mComposer;
269    sp<IComposerClient> mClient;
270
271    // 64KiB minus a small space for metadata such as read/write pointers
272    static constexpr size_t kWriterInitialSize =
273        64 * 1024 / sizeof(uint32_t) - 16;
274    CommandWriter mWriter;
275    CommandReader mReader;
276
277    // When true, the we attach to the vr_hwcomposer service instead of the
278    // hwcomposer. This allows us to redirect surfaces to 3d surfaces in vr.
279    const bool mIsUsingVrComposer;
280};
281
282} // namespace Hwc2
283
284} // namespace android
285
286#endif // ANDROID_SF_COMPOSER_HAL_H
287