1 /*
2  * Copyright (C) 2017 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 <memory>
20 #include <string>
21 #include <unordered_map>
22 #include <unordered_set>
23 #include <vector>
24 
25 #include <android/hardware/graphics/composer/2.1/IComposer.h>
26 #include <composer-command-buffer/2.1/ComposerCommandBuffer.h>
27 #include <composer-vts/2.1/TestCommandReader.h>
28 #include <mapper-vts/2.0/MapperVts.h>
29 #include <mapper-vts/3.0/MapperVts.h>
30 #include <utils/StrongPointer.h>
31 
32 #include "gtest/gtest.h"
33 
34 namespace android {
35 namespace hardware {
36 namespace graphics {
37 namespace composer {
38 namespace V2_1 {
39 namespace vts {
40 
41 using android::hardware::graphics::common::V1_0::ColorMode;
42 using android::hardware::graphics::common::V1_0::Dataspace;
43 using android::hardware::graphics::common::V1_0::Hdr;
44 using android::hardware::graphics::common::V1_0::PixelFormat;
45 using IMapper2 = android::hardware::graphics::mapper::V2_0::IMapper;
46 using IMapper3 = android::hardware::graphics::mapper::V3_0::IMapper;
47 using Gralloc2 = android::hardware::graphics::mapper::V2_0::vts::Gralloc;
48 using Gralloc3 = android::hardware::graphics::mapper::V3_0::vts::Gralloc;
49 
50 class ComposerClient;
51 
52 // A wrapper to IComposer.
53 class Composer {
54    public:
55     Composer();
56     explicit Composer(const std::string& name);
57 
58     sp<IComposer> getRaw() const;
59 
60     // Returns true when the composer supports the specified capability.
61     bool hasCapability(IComposer::Capability capability) const;
62 
63     std::vector<IComposer::Capability> getCapabilities();
64     std::string dumpDebugInfo();
65     std::unique_ptr<ComposerClient> createClient();
66 
67    protected:
68     explicit Composer(const sp<IComposer>& composer);
69 
70    private:
71     const sp<IComposer> mComposer;
72 
73     std::unordered_set<IComposer::Capability> mCapabilities;
74 };
75 
76 // A wrapper to IComposerClient.
77 class ComposerClient {
78    public:
79     explicit ComposerClient(const sp<IComposerClient>& client);
80     ~ComposerClient();
81 
82     sp<IComposerClient> getRaw() const;
83 
84     void registerCallback(const sp<IComposerCallback>& callback);
85     uint32_t getMaxVirtualDisplayCount();
86 
87     Display createVirtualDisplay(uint32_t width, uint32_t height, PixelFormat formatHint,
88                                  uint32_t outputBufferSlotCount, PixelFormat* outFormat);
89     void destroyVirtualDisplay(Display display);
90 
91     Layer createLayer(Display display, uint32_t bufferSlotCount);
92     void destroyLayer(Display display, Layer layer);
93 
94     Config getActiveConfig(Display display);
95     bool getClientTargetSupport(Display display, uint32_t width, uint32_t height,
96                                 PixelFormat format, Dataspace dataspace);
97     std::vector<ColorMode> getColorModes(Display display);
98     int32_t getDisplayAttribute(Display display, Config config,
99                                 IComposerClient::Attribute attribute);
100     std::vector<Config> getDisplayConfigs(Display display);
101     std::string getDisplayName(Display display);
102     IComposerClient::DisplayType getDisplayType(Display display);
103     bool getDozeSupport(Display display);
104     std::vector<Hdr> getHdrCapabilities(Display display, float* outMaxLuminance,
105                                         float* outMaxAverageLuminance, float* outMinLuminance);
106 
107     void setClientTargetSlotCount(Display display, uint32_t clientTargetSlotCount);
108     void setActiveConfig(Display display, Config config);
109     void setColorMode(Display display, ColorMode mode);
110     void setPowerMode(Display display, IComposerClient::PowerMode mode);
111     void setVsyncEnabled(Display display, bool enabled);
112 
113     void execute(TestCommandReader* reader, CommandWriterBase* writer);
114 
115    protected:
116     // Keep track of all virtual displays and layers.  When a test fails with
117     // ASSERT_*, the destructor will clean up the resources for the test.
118     struct DisplayResource {
DisplayResourceDisplayResource119         DisplayResource(bool isVirtual_) : isVirtual(isVirtual_) {}
120 
121         bool isVirtual;
122         std::unordered_set<Layer> layers;
123     };
124     std::unordered_map<Display, DisplayResource> mDisplayResources;
125 
126    private:
127     const sp<IComposerClient> mClient;
128 };
129 
130 class AccessRegion {
131   public:
132     int32_t left;
133     int32_t top;
134     int32_t width;
135     int32_t height;
136 };
137 
138 class Gralloc {
139   public:
140     explicit Gralloc();
141 
142     const native_handle_t* allocate(uint32_t width, uint32_t height, uint32_t layerCount,
143                                     PixelFormat format, uint64_t usage, bool import = true,
144                                     uint32_t* outStride = nullptr);
145 
146     void* lock(const native_handle_t* bufferHandle, uint64_t cpuUsage,
147                const AccessRegion& accessRegionRect, int acquireFence);
148 
149     int unlock(const native_handle_t* bufferHandle);
150 
151     void freeBuffer(const native_handle_t* bufferHandle);
152 
153   protected:
154     std::shared_ptr<Gralloc2> mGralloc2 = nullptr;
155     std::shared_ptr<Gralloc3> mGralloc3 = nullptr;
156 };
157 
158 }  // namespace vts
159 }  // namespace V2_1
160 }  // namespace composer
161 }  // namespace graphics
162 }  // namespace hardware
163 }  // namespace android
164