blob: 9ce9f30c7f33ab5dc9724c229fd70759e3bfa259 [file] [log] [blame]
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001/*
2 * Copyright 2017 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8// This is a GPU-backend specific test. It relies on static intializers to work
9
10#include "SkTypes.h"
11
Jim Van Verth2e5eaf02017-06-21 15:55:46 -040012#include "GrContextFactory.h"
13#include "GrContextPriv.h"
14#include "GrGpu.h"
15#include "GrResourceProvider.h"
16#include "GrSurfaceProxy.h"
17#include "GrTexture.h"
Jim Van Verth2e5eaf02017-06-21 15:55:46 -040018#include "SkGr.h"
19#include "SkSurface.h"
20#include "Test.h"
21
22using sk_gpu_test::GrContextFactory;
23
24void fill_transfer_data(int left, int top, int width, int height, int bufferWidth,
25 GrColor* data) {
26
27 // build red-green gradient
28 for (int j = top; j < top + height; ++j) {
29 for (int i = left; i < left + width; ++i) {
30 unsigned int red = (unsigned int)(256.f*((i - left) / (float)width));
31 unsigned int green = (unsigned int)(256.f*((j - top) / (float)height));
32 data[i + j*bufferWidth] = GrColorPackRGBA(red - (red>>8),
33 green - (green>>8), 0xff, 0xff);
34 }
35 }
36}
37
Brian Salomone05ba5a2019-04-08 11:59:07 -040038bool do_buffers_contain_same_values(const GrColor* bufferA,
39 const GrColor* bufferB,
40 int width,
41 int height,
42 size_t rowBytesA,
Brian Salomon42cbedd2019-04-09 15:00:05 -040043 size_t rowBytesB,
44 bool swiz) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -040045 for (int j = 0; j < height; ++j) {
46 for (int i = 0; i < width; ++i) {
Brian Salomon42cbedd2019-04-09 15:00:05 -040047 auto colorA = bufferA[i];
48 if (swiz) {
49 colorA = GrColorPackRGBA(GrColorUnpackB(colorA), GrColorUnpackG(colorA),
50 GrColorUnpackR(colorA), GrColorUnpackA(colorA));
51 }
52 if (colorA != bufferB[i]) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -040053 return false;
54 }
55 }
Brian Salomone05ba5a2019-04-08 11:59:07 -040056 bufferA = reinterpret_cast<const GrColor*>(reinterpret_cast<const char*>(bufferA) +
57 rowBytesA);
58 bufferB = reinterpret_cast<const GrColor*>(reinterpret_cast<const char*>(bufferB) +
59 rowBytesB);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -040060 }
61 return true;
62}
63
Brian Salomone05ba5a2019-04-08 11:59:07 -040064void basic_transfer_to_test(skiatest::Reporter* reporter, GrContext* context, GrColorType colorType,
65 bool renderTarget) {
Robert Phillips9da87e02019-02-04 13:26:26 -050066 if (GrCaps::kNone_MapFlags == context->priv().caps()->mapBufferFlags()) {
Brian Salomon7f56d3d2017-10-09 13:02:49 -040067 return;
68 }
69
Robert Phillips9da87e02019-02-04 13:26:26 -050070 auto resourceProvider = context->priv().resourceProvider();
71 GrGpu* gpu = context->priv().getGpu();
Robert Phillips6be756b2018-01-16 15:07:54 -050072
Jim Van Verth2e5eaf02017-06-21 15:55:46 -040073 // set up the data
74 const int kTextureWidth = 16;
75 const int kTextureHeight = 16;
Jim Van Verth3a749252018-12-07 10:57:26 -050076#ifdef SK_BUILD_FOR_IOS
77 // UNPACK_ROW_LENGTH is broken on iOS so rowBytes needs to match data width
Robert Phillips4217ea72019-01-30 13:08:28 -050078 const int kBufferWidth = GrBackendApi::kOpenGL == context->backend() ? 16 : 20;
Jim Van Verth3a749252018-12-07 10:57:26 -050079#else
Jim Van Verth2e5eaf02017-06-21 15:55:46 -040080 const int kBufferWidth = 20;
Jim Van Verth3a749252018-12-07 10:57:26 -050081#endif
Jim Van Verth2e5eaf02017-06-21 15:55:46 -040082 const int kBufferHeight = 16;
83 size_t rowBytes = kBufferWidth * sizeof(GrColor);
84 SkAutoTMalloc<GrColor> srcBuffer(kBufferWidth*kBufferHeight);
85 SkAutoTMalloc<GrColor> dstBuffer(kBufferWidth*kBufferHeight);
86
87 fill_transfer_data(0, 0, kTextureWidth, kTextureHeight, kBufferWidth, srcBuffer.get());
88
89 // create and fill transfer buffer
90 size_t size = rowBytes*kBufferHeight;
Brian Salomondbf70722019-02-07 11:31:24 -050091 sk_sp<GrGpuBuffer> buffer(resourceProvider->createBuffer(size, GrGpuBufferType::kXferCpuToGpu,
92 kDynamic_GrAccessPattern));
Jim Van Verth2e5eaf02017-06-21 15:55:46 -040093 if (!buffer) {
94 return;
95 }
96
97 void* data = buffer->map();
98 memcpy(data, srcBuffer.get(), size);
99 buffer->unmap();
100
Brian Salomonc320b152018-02-20 14:05:36 -0500101 for (auto srgbEncoding : {GrSRGBEncoded::kNo, GrSRGBEncoded::kYes}) {
102 // create texture
103 GrSurfaceDesc desc;
104 desc.fFlags = renderTarget ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
Brian Salomonc320b152018-02-20 14:05:36 -0500105 desc.fWidth = kTextureWidth;
106 desc.fHeight = kTextureHeight;
107 desc.fConfig = GrColorTypeToPixelConfig(colorType, srgbEncoding);
108 desc.fSampleCnt = 1;
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400109
Brian Salomonc320b152018-02-20 14:05:36 -0500110 if (kUnknown_GrPixelConfig == desc.fConfig) {
111 SkASSERT(GrSRGBEncoded::kYes == srgbEncoding);
112 continue;
113 }
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400114
Robert Phillips9da87e02019-02-04 13:26:26 -0500115 if (!context->priv().caps()->isConfigTexturable(desc.fConfig) ||
116 (renderTarget && !context->priv().caps()->isConfigRenderable(desc.fConfig))) {
Brian Salomonc320b152018-02-20 14:05:36 -0500117 continue;
118 }
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400119
Robert Phillips9313aa72019-04-09 18:41:27 -0400120 sk_sp<GrTexture> tex = resourceProvider->createTexture(
121 desc, SkBudgeted::kNo, GrResourceProvider::Flags::kNoPendingIO);
Robert Phillips6fc30922018-07-27 12:21:37 -0400122 if (!tex) {
123 continue;
124 }
Robert Phillips16d8ec62017-07-27 16:16:25 -0400125
Brian Salomonc320b152018-02-20 14:05:36 -0500126 //////////////////////////
127 // transfer full data
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400128
Brian Salomonc320b152018-02-20 14:05:36 -0500129 bool result;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400130 result = gpu->transferPixelsTo(tex.get(), 0, 0, kTextureWidth, kTextureHeight, colorType,
131 buffer.get(), 0, rowBytes);
Brian Salomonc320b152018-02-20 14:05:36 -0500132 REPORTER_ASSERT(reporter, result);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400133
Brian Salomonc320b152018-02-20 14:05:36 -0500134 memset(dstBuffer.get(), 0xCDCD, size);
Brian Salomona6948702018-06-01 15:33:20 -0400135 result = gpu->readPixels(tex.get(), 0, 0, kTextureWidth, kTextureHeight, colorType,
Brian Salomonc320b152018-02-20 14:05:36 -0500136 dstBuffer.get(), rowBytes);
137 if (result) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400138 REPORTER_ASSERT(reporter, do_buffers_contain_same_values(srcBuffer,
139 dstBuffer,
140 kTextureWidth,
141 kTextureHeight,
142 rowBytes,
Brian Salomon42cbedd2019-04-09 15:00:05 -0400143 rowBytes,
144 false));
Brian Salomonc320b152018-02-20 14:05:36 -0500145 }
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400146
Brian Salomonc320b152018-02-20 14:05:36 -0500147 //////////////////////////
148 // transfer partial data
Jim Van Verth3a749252018-12-07 10:57:26 -0500149#ifdef SK_BUILD_FOR_IOS
150 // UNPACK_ROW_LENGTH is broken on iOS so we can't do partial transfers
Robert Phillips4217ea72019-01-30 13:08:28 -0500151 if (GrBackendApi::kOpenGL == context->backend()) {
Jim Van Verth3a749252018-12-07 10:57:26 -0500152 continue;
153 }
154#endif
Brian Salomonc320b152018-02-20 14:05:36 -0500155 const int kLeft = 2;
156 const int kTop = 10;
157 const int kWidth = 10;
158 const int kHeight = 2;
159
160 // change color of subrectangle
161 fill_transfer_data(kLeft, kTop, kWidth, kHeight, kBufferWidth, srcBuffer.get());
162 data = buffer->map();
163 memcpy(data, srcBuffer.get(), size);
164 buffer->unmap();
165
166 size_t offset = sizeof(GrColor) * (kTop * kBufferWidth + kLeft);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400167 result = gpu->transferPixelsTo(tex.get(), kLeft, kTop, kWidth, kHeight, colorType,
168 buffer.get(), offset, rowBytes);
Brian Salomonc320b152018-02-20 14:05:36 -0500169 REPORTER_ASSERT(reporter, result);
170
171 memset(dstBuffer.get(), 0xCDCD, size);
Brian Salomona6948702018-06-01 15:33:20 -0400172 result = gpu->readPixels(tex.get(), 0, 0, kTextureWidth, kTextureHeight, colorType,
Brian Salomonc320b152018-02-20 14:05:36 -0500173 dstBuffer.get(), rowBytes);
174 if (result) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400175 REPORTER_ASSERT(reporter, do_buffers_contain_same_values(srcBuffer,
176 dstBuffer,
177 kTextureWidth,
178 kTextureHeight,
179 rowBytes,
Brian Salomon42cbedd2019-04-09 15:00:05 -0400180 rowBytes,
181 false));
Brian Salomonc320b152018-02-20 14:05:36 -0500182 }
Jim Van Verth52fb02e2017-06-27 10:36:56 -0400183 }
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400184}
185
Brian Salomon42cbedd2019-04-09 15:00:05 -0400186void basic_transfer_from_test(skiatest::Reporter* reporter, const sk_gpu_test::ContextInfo& ctxInfo,
Brian Salomone05ba5a2019-04-08 11:59:07 -0400187 GrColorType colorType, bool renderTarget) {
Brian Salomon42cbedd2019-04-09 15:00:05 -0400188 auto context = ctxInfo.grContext();
Brian Salomone05ba5a2019-04-08 11:59:07 -0400189 if (GrCaps::kNone_MapFlags == context->priv().caps()->mapBufferFlags()) {
190 return;
191 }
Jim Van Verth686046b2019-03-18 15:39:22 -0400192
Brian Salomon42cbedd2019-04-09 15:00:05 -0400193 // On OpenGL ES it may not be possible to read back in to BGRA becagse GL_RGBA/GL_UNSIGNED_BYTE
194 // may be the only allowed format/type params to glReadPixels. So read back into GL_RGBA.
195 // TODO(bsalomon): Make this work in GrGLGpu.
196 auto readColorType = colorType;
197 if (GrColorType::kBGRA_8888 == colorType &&
198 ctxInfo.type() == sk_gpu_test::GrContextFactory::kGLES_ContextType) {
199 readColorType = GrColorType::kRGBA_8888;
200 }
201
Brian Salomone05ba5a2019-04-08 11:59:07 -0400202 auto resourceProvider = context->priv().resourceProvider();
203 GrGpu* gpu = context->priv().getGpu();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400204
Brian Salomone05ba5a2019-04-08 11:59:07 -0400205 const int kTextureWidth = 16;
206 const int kTextureHeight = 16;
207
208 // We'll do a full texture read into the buffer followed by a partial read. These values
209 // describe the partial read subrect.
210 const int kPartialLeft = 2;
211 const int kPartialTop = 10;
212 const int kPartialWidth = 10;
213 const int kPartialHeight = 2;
214
215 size_t fullBufferRowBytes;
216 size_t partialBufferRowBytes;
217 size_t fullBufferOffsetAlignment;
218 size_t partialBufferOffsetAlignment;
219
220 SkAssertResult(context->priv().caps()->transferFromBufferRequirements(
221 colorType, kTextureWidth, &fullBufferRowBytes, &fullBufferOffsetAlignment));
222 SkAssertResult(context->priv().caps()->transferFromBufferRequirements(
223 colorType, kPartialWidth, &partialBufferRowBytes, &partialBufferOffsetAlignment));
224
225 size_t bufferSize = fullBufferRowBytes * kTextureHeight;
226 // Arbitrary starting offset for the partial read.
227 size_t partialReadOffset = GrSizeAlignUp(11, partialBufferOffsetAlignment);
228 bufferSize =
229 SkTMax(bufferSize, partialReadOffset + partialBufferRowBytes * partialBufferRowBytes);
230
231 sk_sp<GrGpuBuffer> buffer(resourceProvider->createBuffer(
232 bufferSize, GrGpuBufferType::kXferGpuToCpu, kDynamic_GrAccessPattern));
233 REPORTER_ASSERT(reporter, buffer);
234 if (!buffer) {
235 return;
236 }
237
238 int expectedTransferCnt = 0;
239 gpu->stats()->reset();
240 for (auto srgbEncoding : {GrSRGBEncoded::kNo, GrSRGBEncoded::kYes}) {
241 // create texture
242 GrSurfaceDesc desc;
243 desc.fFlags = renderTarget ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
244 desc.fWidth = kTextureWidth;
245 desc.fHeight = kTextureHeight;
246 desc.fConfig = GrColorTypeToPixelConfig(colorType, srgbEncoding);
247 desc.fSampleCnt = 1;
248
249 if (kUnknown_GrPixelConfig == desc.fConfig) {
250 SkASSERT(GrSRGBEncoded::kYes == srgbEncoding);
251 continue;
252 }
253
254 if (!context->priv().caps()->isConfigTexturable(desc.fConfig) ||
255 (renderTarget && !context->priv().caps()->isConfigRenderable(desc.fConfig))) {
256 continue;
257 }
258
259 SkAutoTMalloc<GrColor> textureData(kTextureWidth * kTextureHeight);
260 size_t textureDataRowBytes = kTextureWidth * sizeof(GrColor);
261 fill_transfer_data(0, 0, kTextureWidth, kTextureHeight, kTextureWidth, textureData.get());
262 GrMipLevel data;
263 data.fPixels = textureData.get();
264 data.fRowBytes = kTextureWidth * sizeof(GrColor);
265 sk_sp<GrTexture> tex = resourceProvider->createTexture(desc, SkBudgeted::kNo, &data, 1);
266 if (!tex) {
267 continue;
268 }
269
270 //////////////////////////
271 // transfer full data
Brian Salomon42cbedd2019-04-09 15:00:05 -0400272 auto bufferRowBytes = gpu->transferPixelsFrom(
273 tex.get(), 0, 0, kTextureWidth, kTextureHeight, readColorType, buffer.get(), 0);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400274 REPORTER_ASSERT(reporter, bufferRowBytes = fullBufferRowBytes);
275 if (!bufferRowBytes) {
276 continue;
277 }
278 ++expectedTransferCnt;
279
280 // TODO(bsalomon): caps to know if the map() is synchronous and skip the flush if so.
281 gpu->finishFlush(nullptr, SkSurface::BackendSurfaceAccess::kNoAccess,
282 SkSurface::kSyncCpu_FlushFlag, 0, nullptr);
283
284 const auto* map = reinterpret_cast<const GrColor*>(buffer->map());
285 REPORTER_ASSERT(reporter, map);
286 if (!map) {
287 continue;
288 }
289 REPORTER_ASSERT(reporter, do_buffers_contain_same_values(textureData.get(),
290 map,
291 kTextureWidth,
292 kTextureHeight,
293 textureDataRowBytes,
Brian Salomon42cbedd2019-04-09 15:00:05 -0400294 bufferRowBytes,
295 readColorType != colorType));
Brian Salomone05ba5a2019-04-08 11:59:07 -0400296 buffer->unmap();
297
Brian Salomon42cbedd2019-04-09 15:00:05 -0400298 ///////////////////////
Brian Salomone05ba5a2019-04-08 11:59:07 -0400299 // Now test a partial read at an offset into the buffer.
Brian Salomon42cbedd2019-04-09 15:00:05 -0400300 bufferRowBytes = gpu->transferPixelsFrom(tex.get(), kPartialLeft, kPartialTop,
301 kPartialWidth, kPartialHeight, readColorType,
302 buffer.get(), partialReadOffset);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400303 REPORTER_ASSERT(reporter, bufferRowBytes = partialBufferRowBytes);
304 if (!bufferRowBytes) {
305 continue;
306 }
307 ++expectedTransferCnt;
308
309 // TODO(bsalomon): caps to know if the map() is synchronous and skip the flush if so.
310 gpu->finishFlush(nullptr, SkSurface::BackendSurfaceAccess::kNoAccess,
311 SkSurface::kSyncCpu_FlushFlag, 0, nullptr);
312
313 map = reinterpret_cast<const GrColor*>(buffer->map());
314 REPORTER_ASSERT(reporter, map);
315 if (!map) {
316 continue;
317 }
318 const GrColor* textureDataStart = reinterpret_cast<const GrColor*>(
319 reinterpret_cast<const char*>(textureData.get()) +
320 textureDataRowBytes * kPartialTop + sizeof(GrColor) * kPartialLeft);
321 const GrColor* bufferStart = reinterpret_cast<const GrColor*>(
322 reinterpret_cast<const char*>(map) + partialReadOffset);
323 REPORTER_ASSERT(reporter, do_buffers_contain_same_values(textureDataStart,
324 bufferStart,
325 kPartialWidth,
326 kPartialHeight,
327 textureDataRowBytes,
Brian Salomon42cbedd2019-04-09 15:00:05 -0400328 bufferRowBytes,
329 readColorType != colorType));
Brian Salomone05ba5a2019-04-08 11:59:07 -0400330 buffer->unmap();
331 }
332#if GR_GPU_STATS
333 REPORTER_ASSERT(reporter, gpu->stats()->transfersFromSurface() == expectedTransferCnt);
334#else
335 (void)expectedTransferCnt;
336#endif
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400337}
Jim Van Verth686046b2019-03-18 15:39:22 -0400338
Brian Salomone05ba5a2019-04-08 11:59:07 -0400339DEF_GPUTEST_FOR_RENDERING_CONTEXTS(TransferPixelsToTest, reporter, ctxInfo) {
340 if (!ctxInfo.grContext()->priv().caps()->transferBufferSupport()) {
341 return;
342 }
343 // RGBA
344 basic_transfer_to_test(reporter, ctxInfo.grContext(), GrColorType::kRGBA_8888, false);
345 basic_transfer_to_test(reporter, ctxInfo.grContext(), GrColorType::kRGBA_8888, true);
346
347 // BGRA
348 basic_transfer_to_test(reporter, ctxInfo.grContext(), GrColorType::kBGRA_8888, false);
349 basic_transfer_to_test(reporter, ctxInfo.grContext(), GrColorType::kBGRA_8888, true);
350}
351
352// TODO(bsalomon): Vulkan
353DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(TransferPixelsFromTest, reporter, ctxInfo) {
354 if (!ctxInfo.grContext()->priv().caps()->transferBufferSupport()) {
355 return;
356 }
357 // RGBA
Brian Salomon42cbedd2019-04-09 15:00:05 -0400358 basic_transfer_from_test(reporter, ctxInfo, GrColorType::kRGBA_8888, false);
359 basic_transfer_from_test(reporter, ctxInfo, GrColorType::kRGBA_8888, true);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400360
361 // BGRA
Brian Salomon42cbedd2019-04-09 15:00:05 -0400362 basic_transfer_from_test(reporter, ctxInfo, GrColorType::kBGRA_8888, false);
363 basic_transfer_from_test(reporter, ctxInfo, GrColorType::kBGRA_8888, true);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400364}