blob: 8eabaef5e3b0981eb756e6f7235b3431a6f42357 [file] [log] [blame]
Valerie Haub019fd72019-05-23 12:50:12 -07001/*
2 * Copyright 2019 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#include <composer-vts/2.2/ReadbackVts.h>
18
19namespace android {
20namespace hardware {
21namespace graphics {
22namespace composer {
23namespace V2_2 {
24namespace vts {
25
26void TestLayer::write(const std::shared_ptr<CommandWriterBase>& writer) {
27 writer->selectLayer(mLayer);
28 writer->setLayerDisplayFrame(mDisplayFrame);
29 writer->setLayerSourceCrop(mSourceCrop);
30 writer->setLayerZOrder(mZOrder);
31 writer->setLayerSurfaceDamage(mSurfaceDamage);
32 writer->setLayerTransform(mTransform);
33 writer->setLayerPlaneAlpha(mAlpha);
34 writer->setLayerBlendMode(mBlendMode);
35}
36
37int32_t ReadbackHelper::GetBytesPerPixel(PixelFormat pixelFormat) {
38 switch (pixelFormat) {
39 case PixelFormat::RGBA_8888:
40 return 4;
41 case PixelFormat::RGB_888:
42 return 3;
43 default:
44 return -1;
45 }
46}
47
48void ReadbackHelper::fillBuffer(int32_t width, int32_t height, uint32_t stride, void* bufferData,
49 PixelFormat pixelFormat,
50 std::vector<IComposerClient::Color> desiredPixelColors) {
51 ASSERT_TRUE(pixelFormat == PixelFormat::RGB_888 || pixelFormat == PixelFormat::RGBA_8888);
52 int32_t bytesPerPixel = GetBytesPerPixel(pixelFormat);
53 ASSERT_NE(-1, bytesPerPixel);
54 for (int row = 0; row < height; row++) {
55 for (int col = 0; col < width; col++) {
56 int pixel = row * width + col;
57 IComposerClient::Color srcColor = desiredPixelColors[pixel];
58
59 int offset = (row * stride + col) * bytesPerPixel;
60 uint8_t* pixelColor = (uint8_t*)bufferData + offset;
61 pixelColor[0] = srcColor.r;
62 pixelColor[1] = srcColor.g;
63 pixelColor[2] = srcColor.b;
64
65 if (bytesPerPixel == 4) {
66 pixelColor[3] = srcColor.a;
67 }
68 }
69 }
70}
71
72void ReadbackHelper::clearColors(std::vector<IComposerClient::Color>& expectedColors, int32_t width,
73 int32_t height, int32_t displayWidth) {
74 for (int row = 0; row < height; row++) {
75 for (int col = 0; col < width; col++) {
76 int pixel = row * displayWidth + col;
77 expectedColors[pixel] = BLACK;
78 }
79 }
80}
81
82void ReadbackHelper::fillColorsArea(std::vector<IComposerClient::Color>& expectedColors,
83 int32_t stride, IComposerClient::Rect area,
84 IComposerClient::Color color) {
85 for (int row = area.top; row < area.bottom; row++) {
86 for (int col = area.left; col < area.right; col++) {
87 int pixel = row * stride + col;
88 expectedColors[pixel] = color;
89 }
90 }
91}
92
93bool ReadbackHelper::readbackSupported(const PixelFormat& pixelFormat, const Dataspace& dataspace,
94 const Error error) {
95 if (error != Error::NONE) {
96 return false;
97 }
98 // TODO: add support for RGBA_1010102
99 if (pixelFormat != PixelFormat::RGB_888 && pixelFormat != PixelFormat::RGBA_8888) {
100 return false;
101 }
102 if (dataspace != Dataspace::V0_SRGB) {
103 return false;
104 }
105 return true;
106}
107
108ReadbackBuffer::ReadbackBuffer(Display display, const std::shared_ptr<ComposerClient>& client,
109 const std::shared_ptr<Gralloc>& gralloc, uint32_t width,
110 uint32_t height, PixelFormat pixelFormat, Dataspace dataspace) {
111 mDisplay = display;
112
113 mComposerClient = client;
114 mGralloc = gralloc;
115
116 mPixelFormat = pixelFormat;
117 mDataspace = dataspace;
118
119 mInfo.width = width;
120 mInfo.height = height;
121 mInfo.layerCount = 1;
122 mInfo.format = mPixelFormat;
123 mInfo.usage = static_cast<uint64_t>(BufferUsage::CPU_READ_OFTEN | BufferUsage::GPU_TEXTURE);
124
125 mAccessRegion.top = 0;
126 mAccessRegion.left = 0;
127 mAccessRegion.width = width;
128 mAccessRegion.height = height;
129}
130
131ReadbackBuffer::~ReadbackBuffer() {
132 if (mBufferHandle != nullptr) {
133 mGralloc->freeBuffer(mBufferHandle);
134 }
135}
136
137void ReadbackBuffer::setReadbackBuffer() {
138 if (mBufferHandle != nullptr) {
139 mGralloc->freeBuffer(mBufferHandle);
140 mBufferHandle = nullptr;
141 }
142 mBufferHandle = mGralloc->allocate(mInfo, /*import*/ true, &mStride);
143 ASSERT_NE(false, mGralloc->validateBufferSize(mBufferHandle, mInfo, mStride));
144 ASSERT_NO_FATAL_FAILURE(mComposerClient->setReadbackBuffer(mDisplay, mBufferHandle, -1));
145}
146
147void ReadbackBuffer::checkReadbackBuffer(std::vector<IComposerClient::Color> expectedColors) {
148 // lock buffer for reading
149 int32_t fenceHandle;
150 ASSERT_NO_FATAL_FAILURE(mComposerClient->getReadbackBufferFence(mDisplay, &fenceHandle));
151
152 void* bufData = mGralloc->lock(mBufferHandle, mInfo.usage, mAccessRegion, fenceHandle);
153 ASSERT_TRUE(mPixelFormat == PixelFormat::RGB_888 || mPixelFormat == PixelFormat::RGBA_8888);
154 int32_t bytesPerPixel = ReadbackHelper::GetBytesPerPixel(mPixelFormat);
155 ASSERT_NE(-1, bytesPerPixel);
156 for (int row = 0; row < mInfo.height; row++) {
157 for (int col = 0; col < mInfo.width; col++) {
158 int pixel = row * mInfo.width + col;
159 int offset = (row * mStride + col) * bytesPerPixel;
160 uint8_t* pixelColor = (uint8_t*)bufData + offset;
161
162 ASSERT_EQ(expectedColors[pixel].r, pixelColor[0]);
163 ASSERT_EQ(expectedColors[pixel].g, pixelColor[1]);
164 ASSERT_EQ(expectedColors[pixel].b, pixelColor[2]);
165 }
166 }
167 int32_t unlockFence = mGralloc->unlock(mBufferHandle);
168 if (unlockFence != -1) {
169 sync_wait(unlockFence, -1);
170 close(unlockFence);
171 }
172}
173
174void TestColorLayer::write(const std::shared_ptr<CommandWriterBase>& writer) {
175 TestLayer::write(writer);
176 writer->setLayerCompositionType(IComposerClient::Composition::SOLID_COLOR);
177 writer->setLayerColor(mColor);
178}
179
180TestBufferLayer::TestBufferLayer(const std::shared_ptr<ComposerClient>& client,
181 const std::shared_ptr<Gralloc>& gralloc, Display display,
182 int32_t width, int32_t height, PixelFormat format,
183 IComposerClient::Composition composition)
184 : TestLayer{client, display} {
185 mGralloc = gralloc;
186 mComposition = composition;
187 mInfo.width = width;
188 mInfo.height = height;
189 mInfo.layerCount = 1;
190 mInfo.format = format;
191 mInfo.usage = static_cast<uint64_t>(BufferUsage::CPU_READ_OFTEN | BufferUsage::CPU_WRITE_OFTEN |
192 BufferUsage::COMPOSER_OVERLAY);
193
194 mAccessRegion.top = 0;
195 mAccessRegion.left = 0;
196 mAccessRegion.width = width;
197 mAccessRegion.height = height;
198
199 setSourceCrop({0, 0, (float)width, (float)height});
200}
201
202TestBufferLayer::~TestBufferLayer() {
203 if (mBufferHandle != nullptr) {
204 mGralloc->freeBuffer(mBufferHandle);
205 }
206}
207
208void TestBufferLayer::write(const std::shared_ptr<CommandWriterBase>& writer) {
209 TestLayer::write(writer);
210 writer->setLayerCompositionType(mComposition);
211 writer->setLayerDataspace(Dataspace::UNKNOWN);
212 writer->setLayerVisibleRegion(std::vector<IComposerClient::Rect>(1, mDisplayFrame));
213 if (mBufferHandle != nullptr) writer->setLayerBuffer(0, mBufferHandle, mFillFence);
214}
215
216void TestBufferLayer::fillBuffer(std::vector<IComposerClient::Color> expectedColors) {
217 void* bufData = mGralloc->lock(mBufferHandle, mInfo.usage, mAccessRegion, -1);
218 ASSERT_NO_FATAL_FAILURE(ReadbackHelper::fillBuffer(mInfo.width, mInfo.height, mStride, bufData,
219 mInfo.format, expectedColors));
220 mFillFence = mGralloc->unlock(mBufferHandle);
221 if (mFillFence != -1) {
222 sync_wait(mFillFence, -1);
223 close(mFillFence);
224 }
225}
226void TestBufferLayer::setBuffer(std::vector<IComposerClient::Color> colors) {
227 if (mBufferHandle != nullptr) {
228 mGralloc->freeBuffer(mBufferHandle);
229 mBufferHandle = nullptr;
230 }
231 mBufferHandle = mGralloc->allocate(mInfo, /*import*/ true, &mStride);
232 ASSERT_NE(nullptr, mBufferHandle);
233 ASSERT_NO_FATAL_FAILURE(fillBuffer(colors));
234 ASSERT_NE(false, mGralloc->validateBufferSize(mBufferHandle, mInfo, mStride));
235}
236
237void TestBufferLayer::setToClientComposition(const std::shared_ptr<CommandWriterBase>& writer) {
238 writer->selectLayer(mLayer);
239 writer->setLayerCompositionType(IComposerClient::Composition::CLIENT);
240}
241
242} // namespace vts
243} // namespace V2_2
244} // namespace composer
245} // namespace graphics
246} // namespace hardware
247} // namespace android