1 /*
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 "include/core/SkTypes.h"
11
12 #include "include/core/SkSurface.h"
13 #include "include/gpu/GrDirectContext.h"
14 #include "src/gpu/GrDirectContextPriv.h"
15 #include "src/gpu/GrGpu.h"
16 #include "src/gpu/GrImageInfo.h"
17 #include "src/gpu/GrResourceProvider.h"
18 #include "src/gpu/GrSurfaceProxy.h"
19 #include "src/gpu/GrTexture.h"
20 #include "src/gpu/SkGr.h"
21 #include "tests/Test.h"
22 #include "tests/TestUtils.h"
23 #include "tools/gpu/GrContextFactory.h"
24
25 using sk_gpu_test::GrContextFactory;
26
fill_transfer_data(int left,int top,int width,int height,int rowBytes,GrColorType dstType,char * dst)27 void fill_transfer_data(int left, int top, int width, int height, int rowBytes,
28 GrColorType dstType, char* dst) {
29 size_t dstBpp = GrColorTypeBytesPerPixel(dstType);
30 auto dstLocation = [dst, dstBpp, rowBytes](int x, int y) {
31 return dst + y * rowBytes + x * dstBpp;
32 };
33 // build red-green gradient
34 for (int j = top; j < top + height; ++j) {
35 for (int i = left; i < left + width; ++i) {
36 auto r = (unsigned int)(256.f*((i - left) / (float)width));
37 auto g = (unsigned int)(256.f*((j - top) / (float)height));
38 r -= (r >> 8);
39 g -= (g >> 8);
40 // set b and a channels to be inverse of r and g just to have interesting values to
41 // test.
42 uint32_t srcPixel = GrColorPackRGBA(r, g, 0xff - r, 0xff - g);
43 GrImageInfo srcInfo(GrColorType::kRGBA_8888, kUnpremul_SkAlphaType, nullptr, 1, 1);
44 GrImageInfo dstInfo(dstType, kUnpremul_SkAlphaType, nullptr, 1, 1);
45 GrConvertPixels(GrPixmap(dstInfo, dstLocation(i, j), dstBpp),
46 GrPixmap(srcInfo, &srcPixel, 4));
47 }
48 }
49 }
50
determine_tolerances(GrColorType a,GrColorType b,float tolerances[4])51 void determine_tolerances(GrColorType a, GrColorType b, float tolerances[4]) {
52 std::fill_n(tolerances, 4, 0);
53
54 auto descA = GrGetColorTypeDesc(a);
55 auto descB = GrGetColorTypeDesc(b);
56 // For each channel x set the tolerance to 1 / (2^min(bits_in_a, bits_in_b) - 1) unless
57 // one color type is missing the channel. In that case leave it at 0. If the other color
58 // has the channel then it better be exactly 1 for alpha or 0 for rgb.
59 for (int i = 0; i < 4; ++i) {
60 if (descA[i] != descB[i]) {
61 auto m = std::min(descA[i], descB[i]);
62 if (m) {
63 tolerances[i] = 1.f / (m - 1);
64 }
65 }
66 }
67 }
68
read_pixels_from_texture(GrTexture * texture,GrColorType colorType,char * dst,float tolerances[4])69 bool read_pixels_from_texture(GrTexture* texture, GrColorType colorType, char* dst,
70 float tolerances[4]) {
71 auto* context = texture->getContext();
72 auto* gpu = context->priv().getGpu();
73 auto* caps = context->priv().caps();
74
75 int w = texture->width();
76 int h = texture->height();
77 size_t rowBytes = GrColorTypeBytesPerPixel(colorType) * w;
78
79 GrCaps::SupportedRead supportedRead =
80 caps->supportedReadPixelsColorType(colorType, texture->backendFormat(), colorType);
81 std::fill_n(tolerances, 4, 0);
82 if (supportedRead.fColorType != colorType) {
83 size_t tmpRowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * w;
84 std::unique_ptr<char[]> tmpPixels(new char[tmpRowBytes * h]);
85 if (!gpu->readPixels(texture,
86 SkIRect::MakeWH(w, h),
87 colorType,
88 supportedRead.fColorType,
89 tmpPixels.get(),
90 tmpRowBytes)) {
91 return false;
92 }
93 GrImageInfo tmpInfo(supportedRead.fColorType, kUnpremul_SkAlphaType, nullptr, w, h);
94 GrImageInfo dstInfo(colorType, kUnpremul_SkAlphaType, nullptr, w, h);
95 determine_tolerances(tmpInfo.colorType(), dstInfo.colorType(), tolerances);
96 return GrConvertPixels(GrPixmap(dstInfo, dst, rowBytes),
97 GrPixmap(tmpInfo, tmpPixels.get(), tmpRowBytes));
98 }
99 return gpu->readPixels(texture,
100 SkIRect::MakeWH(w, h),
101 colorType,
102 supportedRead.fColorType,
103 dst,
104 rowBytes);
105 }
106
basic_transfer_to_test(skiatest::Reporter * reporter,GrDirectContext * dContext,GrColorType colorType,GrRenderable renderable)107 void basic_transfer_to_test(skiatest::Reporter* reporter,
108 GrDirectContext* dContext,
109 GrColorType colorType,
110 GrRenderable renderable) {
111 if (GrCaps::kNone_MapFlags == dContext->priv().caps()->mapBufferFlags()) {
112 return;
113 }
114
115 auto* caps = dContext->priv().caps();
116
117 auto backendFormat = caps->getDefaultBackendFormat(colorType, renderable);
118 if (!backendFormat.isValid()) {
119 return;
120 }
121
122 auto resourceProvider = dContext->priv().resourceProvider();
123 GrGpu* gpu = dContext->priv().getGpu();
124
125 static constexpr SkISize kTexDims = {16, 16};
126 int srcBufferWidth = caps->transferPixelsToRowBytesSupport() ? 20 : 16;
127 const int kBufferHeight = 16;
128
129 sk_sp<GrTexture> tex =
130 resourceProvider->createTexture(kTexDims, backendFormat, GrTextureType::k2D, renderable,
131 1, GrMipmapped::kNo, SkBudgeted::kNo, GrProtected::kNo);
132 if (!tex) {
133 ERRORF(reporter, "Could not create texture");
134 return;
135 }
136
137 // We validate the results using GrGpu::readPixels, so exit if this is not supported.
138 // TODO: Do this through SurfaceContext once it works for all color types or support
139 // kCopyToTexture2D here.
140 if (GrCaps::SurfaceReadPixelsSupport::kSupported !=
141 caps->surfaceSupportsReadPixels(tex.get())) {
142 return;
143 }
144 // GL requires a texture to be framebuffer bindable to call glReadPixels. However, we have not
145 // incorporated that test into surfaceSupportsReadPixels(). TODO: Remove this once we handle
146 // drawing to a bindable format.
147 if (!caps->isFormatAsColorTypeRenderable(colorType, tex->backendFormat())) {
148 return;
149 }
150
151 // The caps tell us what color type we are allowed to upload and read back from this texture,
152 // either of which may differ from 'colorType'.
153 GrCaps::SupportedWrite allowedSrc =
154 caps->supportedWritePixelsColorType(colorType, tex->backendFormat(), colorType);
155 if (!allowedSrc.fOffsetAlignmentForTransferBuffer) {
156 return;
157 }
158 size_t srcRowBytes = SkAlignTo(GrColorTypeBytesPerPixel(allowedSrc.fColorType) * srcBufferWidth,
159 caps->transferBufferAlignment());
160
161 std::unique_ptr<char[]> srcData(new char[kTexDims.fHeight * srcRowBytes]);
162
163 fill_transfer_data(0, 0, kTexDims.fWidth, kTexDims.fHeight, srcRowBytes,
164 allowedSrc.fColorType, srcData.get());
165
166 // create and fill transfer buffer
167 size_t size = srcRowBytes * kBufferHeight;
168 sk_sp<GrGpuBuffer> buffer(resourceProvider->createBuffer(size, GrGpuBufferType::kXferCpuToGpu,
169 kDynamic_GrAccessPattern));
170 if (!buffer) {
171 return;
172 }
173 void* data = buffer->map();
174 if (!buffer) {
175 ERRORF(reporter, "Could not map buffer");
176 return;
177 }
178 memcpy(data, srcData.get(), size);
179 buffer->unmap();
180
181 //////////////////////////
182 // transfer full data
183
184 bool result;
185 result = gpu->transferPixelsTo(tex.get(),
186 SkIRect::MakeSize(kTexDims),
187 colorType,
188 allowedSrc.fColorType,
189 buffer,
190 0,
191 srcRowBytes);
192 REPORTER_ASSERT(reporter, result);
193
194 size_t dstRowBytes = GrColorTypeBytesPerPixel(colorType) * kTexDims.fWidth;
195 std::unique_ptr<char[]> dstBuffer(new char[dstRowBytes * kTexDims.fHeight]());
196
197 float compareTolerances[4] = {};
198 result = read_pixels_from_texture(tex.get(), colorType, dstBuffer.get(), compareTolerances);
199 if (!result) {
200 ERRORF(reporter, "Could not read pixels from texture, color type: %d",
201 static_cast<int>(colorType));
202 return;
203 }
204
205 auto error = std::function<ComparePixmapsErrorReporter>(
206 [reporter, colorType](int x, int y, const float diffs[4]) {
207 ERRORF(reporter,
208 "Error at (%d %d) in transfer, color type: %s, diffs: (%f, %f, %f, %f)",
209 x, y, GrColorTypeToStr(colorType),
210 diffs[0], diffs[1], diffs[2], diffs[3]);
211 });
212 GrImageInfo srcInfo(allowedSrc.fColorType, kUnpremul_SkAlphaType, nullptr, tex->dimensions());
213 GrImageInfo dstInfo( colorType, kUnpremul_SkAlphaType, nullptr, tex->dimensions());
214 ComparePixels(GrCPixmap(srcInfo, srcData.get(), srcRowBytes),
215 GrCPixmap(dstInfo, dstBuffer.get(), dstRowBytes),
216 compareTolerances,
217 error);
218
219 //////////////////////////
220 // transfer partial data
221
222 // We're relying on this cap to write partial texture data
223 if (!caps->transferPixelsToRowBytesSupport()) {
224 return;
225 }
226 // We keep a 1 to 1 correspondence between pixels in the buffer and the entire texture. We
227 // update the contents of a sub-rect of the buffer and push that rect to the texture. We start
228 // with a left sub-rect inset of 2 but may adjust that so we can fulfill the transfer buffer
229 // offset alignment requirement.
230 int left = 2;
231 int top = 10;
232 const int width = 10;
233 const int height = 2;
234 size_t offset = top * srcRowBytes + left * GrColorTypeBytesPerPixel(allowedSrc.fColorType);
235 while (offset % allowedSrc.fOffsetAlignmentForTransferBuffer) {
236 offset += GrColorTypeBytesPerPixel(allowedSrc.fColorType);
237 ++left;
238 // In most cases we assume that the required alignment is 1 or a small multiple of the bpp,
239 // which it is for color types across all current backends except Direct3D. To correct for
240 // Direct3D's large alignment requirement we may adjust the top location as well.
241 if (left + width > tex->width()) {
242 left = 0;
243 ++top;
244 offset = top * srcRowBytes;
245 }
246 SkASSERT(left + width <= tex->width());
247 SkASSERT(top + height <= tex->height());
248 }
249
250 // change color of subrectangle
251 fill_transfer_data(left, top, width, height, srcRowBytes, allowedSrc.fColorType,
252 srcData.get());
253 data = buffer->map();
254 memcpy(data, srcData.get(), size);
255 buffer->unmap();
256
257 result = gpu->transferPixelsTo(tex.get(),
258 SkIRect::MakeXYWH(left, top, width, height),
259 colorType,
260 allowedSrc.fColorType,
261 buffer,
262 offset,
263 srcRowBytes);
264 if (!result) {
265 ERRORF(reporter, "Could not transfer pixels to texture, color type: %d",
266 static_cast<int>(colorType));
267 return;
268 }
269
270 result = read_pixels_from_texture(tex.get(), colorType, dstBuffer.get(), compareTolerances);
271 if (!result) {
272 ERRORF(reporter, "Could not read pixels from texture, color type: %d",
273 static_cast<int>(colorType));
274 return;
275 }
276 ComparePixels(GrCPixmap(srcInfo, srcData.get(), srcRowBytes),
277 GrCPixmap(dstInfo, dstBuffer.get(), dstRowBytes),
278 compareTolerances,
279 error);
280 }
281
basic_transfer_from_test(skiatest::Reporter * reporter,const sk_gpu_test::ContextInfo & ctxInfo,GrColorType colorType,GrRenderable renderable)282 void basic_transfer_from_test(skiatest::Reporter* reporter, const sk_gpu_test::ContextInfo& ctxInfo,
283 GrColorType colorType, GrRenderable renderable) {
284 auto context = ctxInfo.directContext();
285 auto caps = context->priv().caps();
286 if (GrCaps::kNone_MapFlags == caps->mapBufferFlags()) {
287 return;
288 }
289
290 auto resourceProvider = context->priv().resourceProvider();
291 GrGpu* gpu = context->priv().getGpu();
292
293 static constexpr SkISize kTexDims = {16, 16};
294
295 // We'll do a full texture read into the buffer followed by a partial read. These values
296 // describe the partial read subrect.
297 const int kPartialLeft = 2;
298 const int kPartialTop = 10;
299 const int kPartialWidth = 10;
300 const int kPartialHeight = 2;
301
302 // create texture
303 auto format = context->priv().caps()->getDefaultBackendFormat(colorType, renderable);
304 if (!format.isValid()) {
305 return;
306 }
307
308 size_t textureDataBpp = GrColorTypeBytesPerPixel(colorType);
309 size_t textureDataRowBytes = kTexDims.fWidth * textureDataBpp;
310 std::unique_ptr<char[]> textureData(new char[kTexDims.fHeight * textureDataRowBytes]);
311 fill_transfer_data(0, 0, kTexDims.fWidth, kTexDims.fHeight, textureDataRowBytes, colorType,
312 textureData.get());
313 GrMipLevel data;
314 data.fPixels = textureData.get();
315 data.fRowBytes = textureDataRowBytes;
316 sk_sp<GrTexture> tex = resourceProvider->createTexture(kTexDims, format, GrTextureType::k2D,
317 colorType, renderable, 1,
318 SkBudgeted::kNo, GrMipMapped::kNo,
319 GrProtected::kNo, &data);
320 if (!tex) {
321 return;
322 }
323
324 if (GrCaps::SurfaceReadPixelsSupport::kSupported !=
325 caps->surfaceSupportsReadPixels(tex.get())) {
326 return;
327 }
328 // GL requires a texture to be framebuffer bindable to call glReadPixels. However, we have not
329 // incorporated that test into surfaceSupportsReadPixels(). TODO: Remove this once we handle
330 // drawing to a bindable format.
331 if (!caps->isFormatAsColorTypeRenderable(colorType, tex->backendFormat())) {
332 return;
333 }
334
335 // Create the transfer buffer.
336 auto allowedRead =
337 caps->supportedReadPixelsColorType(colorType, tex->backendFormat(), colorType);
338 if (!allowedRead.fOffsetAlignmentForTransferBuffer) {
339 return;
340 }
341 GrImageInfo readInfo(allowedRead.fColorType, kUnpremul_SkAlphaType, nullptr, kTexDims);
342
343 size_t bpp = GrColorTypeBytesPerPixel(allowedRead.fColorType);
344 size_t fullBufferRowBytes = SkAlignTo(kTexDims.fWidth * bpp, caps->transferBufferAlignment());
345 size_t partialBufferRowBytes = SkAlignTo(kPartialWidth * bpp, caps->transferBufferAlignment());
346 size_t offsetAlignment = allowedRead.fOffsetAlignmentForTransferBuffer;
347 SkASSERT(offsetAlignment);
348
349 size_t bufferSize = fullBufferRowBytes * kTexDims.fHeight;
350 // Arbitrary starting offset for the partial read.
351 static constexpr size_t kStartingOffset = 11;
352 size_t partialReadOffset = kStartingOffset +
353 (offsetAlignment - kStartingOffset%offsetAlignment)%offsetAlignment;
354 bufferSize = std::max(bufferSize,
355 partialReadOffset + partialBufferRowBytes * kPartialHeight);
356
357 sk_sp<GrGpuBuffer> buffer(resourceProvider->createBuffer(
358 bufferSize, GrGpuBufferType::kXferGpuToCpu, kDynamic_GrAccessPattern));
359 REPORTER_ASSERT(reporter, buffer);
360 if (!buffer) {
361 return;
362 }
363
364 int expectedTransferCnt = 0;
365 gpu->stats()->reset();
366
367 //////////////////////////
368 // transfer full data
369 bool result = gpu->transferPixelsFrom(tex.get(),
370 SkIRect::MakeSize(kTexDims),
371 colorType,
372 allowedRead.fColorType,
373 buffer,
374 0);
375 if (!result) {
376 ERRORF(reporter, "transferPixelsFrom failed.");
377 return;
378 }
379 ++expectedTransferCnt;
380
381 if (context->priv().caps()->mapBufferFlags() & GrCaps::kAsyncRead_MapFlag) {
382 gpu->submitToGpu(true);
383 }
384
385 // Copy the transfer buffer contents to a temporary so we can manipulate it.
386 const auto* map = reinterpret_cast<const char*>(buffer->map());
387 REPORTER_ASSERT(reporter, map);
388 if (!map) {
389 ERRORF(reporter, "Failed to map transfer buffer.");
390 return;
391 }
392 std::unique_ptr<char[]> transferData(new char[kTexDims.fHeight * fullBufferRowBytes]);
393 memcpy(transferData.get(), map, fullBufferRowBytes * kTexDims.fHeight);
394 buffer->unmap();
395
396 GrImageInfo transferInfo(allowedRead.fColorType, kUnpremul_SkAlphaType, nullptr, kTexDims);
397
398 float tol[4];
399 determine_tolerances(allowedRead.fColorType, colorType, tol);
400 auto error = std::function<ComparePixmapsErrorReporter>(
401 [reporter, colorType](int x, int y, const float diffs[4]) {
402 ERRORF(reporter,
403 "Error at (%d %d) in transfer, color type: %s, diffs: (%f, %f, %f, %f)",
404 x, y, GrColorTypeToStr(colorType),
405 diffs[0], diffs[1], diffs[2], diffs[3]);
406 });
407 GrImageInfo textureDataInfo(colorType, kUnpremul_SkAlphaType, nullptr, kTexDims);
408 ComparePixels(GrCPixmap(textureDataInfo, textureData.get(), textureDataRowBytes),
409 GrCPixmap( transferInfo, transferData.get(), fullBufferRowBytes),
410 tol,
411 error);
412
413 ///////////////////////
414 // Now test a partial read at an offset into the buffer.
415 result = gpu->transferPixelsFrom(
416 tex.get(),
417 SkIRect::MakeXYWH(kPartialLeft, kPartialTop, kPartialWidth, kPartialHeight),
418 colorType,
419 allowedRead.fColorType,
420 buffer,
421 partialReadOffset);
422 if (!result) {
423 ERRORF(reporter, "transferPixelsFrom failed.");
424 return;
425 }
426 ++expectedTransferCnt;
427
428 if (context->priv().caps()->mapBufferFlags() & GrCaps::kAsyncRead_MapFlag) {
429 gpu->submitToGpu(true);
430 }
431
432 map = reinterpret_cast<const char*>(buffer->map());
433 REPORTER_ASSERT(reporter, map);
434 if (!map) {
435 ERRORF(reporter, "Failed to map transfer buffer.");
436 return;
437 }
438 const char* bufferStart = reinterpret_cast<const char*>(map) + partialReadOffset;
439 memcpy(transferData.get(), bufferStart, partialBufferRowBytes * kTexDims.fHeight);
440 buffer->unmap();
441
442 transferInfo = transferInfo.makeWH(kPartialWidth, kPartialHeight);
443 const char* textureDataStart =
444 textureData.get() + textureDataRowBytes * kPartialTop + textureDataBpp * kPartialLeft;
445 textureDataInfo = textureDataInfo.makeWH(kPartialWidth, kPartialHeight);
446 ComparePixels(GrCPixmap(textureDataInfo, textureDataStart, textureDataRowBytes),
447 GrCPixmap(transferInfo , transferData.get(), partialBufferRowBytes),
448 tol,
449 error);
450 #if GR_GPU_STATS
451 REPORTER_ASSERT(reporter, gpu->stats()->transfersFromSurface() == expectedTransferCnt);
452 #else
453 (void)expectedTransferCnt;
454 #endif
455 }
456
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(TransferPixelsToTextureTest,reporter,ctxInfo)457 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(TransferPixelsToTextureTest, reporter, ctxInfo) {
458 if (!ctxInfo.directContext()->priv().caps()->transferFromBufferToTextureSupport()) {
459 return;
460 }
461 for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) {
462 for (auto colorType : {
463 GrColorType::kAlpha_8,
464 GrColorType::kBGR_565,
465 GrColorType::kABGR_4444,
466 GrColorType::kRGBA_8888,
467 GrColorType::kRGBA_8888_SRGB,
468 GrColorType::kRGB_888x,
469 GrColorType::kRG_88,
470 GrColorType::kBGRA_8888,
471 GrColorType::kRGBA_1010102,
472 GrColorType::kBGRA_1010102,
473 GrColorType::kGray_8,
474 GrColorType::kAlpha_F16,
475 GrColorType::kRGBA_F16,
476 GrColorType::kRGBA_F16_Clamped,
477 GrColorType::kRGBA_F32,
478 GrColorType::kAlpha_16,
479 GrColorType::kRG_1616,
480 GrColorType::kRGBA_16161616,
481 GrColorType::kRG_F16,
482 }) {
483 basic_transfer_to_test(reporter, ctxInfo.directContext(), colorType, renderable);
484 }
485 }
486 }
487
488 // TODO(bsalomon): Metal
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(TransferPixelsFromTextureTest,reporter,ctxInfo)489 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(TransferPixelsFromTextureTest, reporter, ctxInfo) {
490 if (!ctxInfo.directContext()->priv().caps()->transferFromSurfaceToBufferSupport()) {
491 return;
492 }
493 for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) {
494 for (auto colorType : {
495 GrColorType::kAlpha_8,
496 GrColorType::kAlpha_16,
497 GrColorType::kBGR_565,
498 GrColorType::kABGR_4444,
499 GrColorType::kRGBA_8888,
500 GrColorType::kRGBA_8888_SRGB,
501 GrColorType::kRGB_888x,
502 GrColorType::kRG_88,
503 GrColorType::kBGRA_8888,
504 GrColorType::kRGBA_1010102,
505 GrColorType::kBGRA_1010102,
506 GrColorType::kGray_8,
507 GrColorType::kAlpha_F16,
508 GrColorType::kRGBA_F16,
509 GrColorType::kRGBA_F16_Clamped,
510 GrColorType::kRGBA_F32,
511 GrColorType::kRG_1616,
512 GrColorType::kRGBA_16161616,
513 GrColorType::kRG_F16,
514 }) {
515 basic_transfer_from_test(reporter, ctxInfo, colorType, renderable);
516 }
517 }
518 }
519