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