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