2011-11-02 19:57:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
2020-06-26 17:32:09 +00:00
|
|
|
#include "include/core/SkImage.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkSurface.h"
|
2019-10-23 17:34:36 +00:00
|
|
|
#include "include/effects/SkGradientShader.h"
|
2020-07-06 14:56:46 +00:00
|
|
|
#include "include/gpu/GrDirectContext.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/private/SkColorData.h"
|
|
|
|
#include "include/private/SkHalf.h"
|
|
|
|
#include "include/private/SkImageInfoPriv.h"
|
2019-09-26 20:57:09 +00:00
|
|
|
#include "include/utils/SkNWayCanvas.h"
|
2019-05-10 18:24:50 +00:00
|
|
|
#include "src/core/SkAutoPixmapStorage.h"
|
2019-10-23 17:34:36 +00:00
|
|
|
#include "src/core/SkConvertPixels.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/core/SkMathPriv.h"
|
|
|
|
#include "src/gpu/GrContextPriv.h"
|
2019-09-30 16:15:30 +00:00
|
|
|
#include "src/gpu/GrImageInfo.h"
|
2020-04-06 17:57:30 +00:00
|
|
|
#include "src/gpu/GrSurfaceContext.h"
|
2019-05-10 18:24:50 +00:00
|
|
|
#include "tests/Test.h"
|
2019-07-15 16:30:44 +00:00
|
|
|
#include "tests/TestUtils.h"
|
2019-09-26 20:57:09 +00:00
|
|
|
#include "tools/ToolUtils.h"
|
2020-06-26 17:32:09 +00:00
|
|
|
#include "tools/gpu/BackendTextureImageFactory.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/gpu/GrContextFactory.h"
|
|
|
|
#include "tools/gpu/ProxyUtils.h"
|
2011-11-02 19:57:21 +00:00
|
|
|
|
2020-06-26 17:32:09 +00:00
|
|
|
#include <initializer_list>
|
|
|
|
|
2011-11-02 19:57:21 +00:00
|
|
|
static const int DEV_W = 100, DEV_H = 100;
|
|
|
|
static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H);
|
2012-08-23 18:14:13 +00:00
|
|
|
static const SkRect DEV_RECT_S = SkRect::MakeWH(DEV_W * SK_Scalar1,
|
2011-11-02 19:57:21 +00:00
|
|
|
DEV_H * SK_Scalar1);
|
|
|
|
|
2015-07-16 15:23:13 +00:00
|
|
|
static SkPMColor get_src_color(int x, int y) {
|
2011-11-02 19:57:21 +00:00
|
|
|
SkASSERT(x >= 0 && x < DEV_W);
|
|
|
|
SkASSERT(y >= 0 && y < DEV_H);
|
2011-11-03 20:29:47 +00:00
|
|
|
|
|
|
|
U8CPU r = x;
|
|
|
|
U8CPU g = y;
|
|
|
|
U8CPU b = 0xc;
|
|
|
|
|
|
|
|
U8CPU a = 0xff;
|
2011-11-07 15:54:49 +00:00
|
|
|
switch ((x+y) % 5) {
|
2011-11-03 20:29:47 +00:00
|
|
|
case 0:
|
|
|
|
a = 0xff;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
a = 0x80;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
a = 0xCC;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
a = 0x01;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
a = 0x00;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return SkPremultiplyARGBInline(a, r, g, b);
|
2011-11-02 19:57:21 +00:00
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-07-16 15:23:13 +00:00
|
|
|
static SkPMColor get_dst_bmp_init_color(int x, int y, int w) {
|
2011-11-02 19:57:21 +00:00
|
|
|
int n = y * w + x;
|
2011-11-03 20:29:47 +00:00
|
|
|
|
2011-11-02 19:57:21 +00:00
|
|
|
U8CPU b = n & 0xff;
|
|
|
|
U8CPU g = (n >> 8) & 0xff;
|
|
|
|
U8CPU r = (n >> 16) & 0xff;
|
|
|
|
return SkPackARGB32(0xff, r, g , b);
|
|
|
|
}
|
|
|
|
|
2018-03-22 14:01:16 +00:00
|
|
|
// TODO: Make this consider both ATs
|
2015-07-16 15:23:13 +00:00
|
|
|
static SkPMColor convert_to_pmcolor(SkColorType ct, SkAlphaType at, const uint32_t* addr,
|
|
|
|
bool* doUnpremul) {
|
2014-03-17 21:31:26 +00:00
|
|
|
*doUnpremul = (kUnpremul_SkAlphaType == at);
|
|
|
|
|
|
|
|
const uint8_t* c = reinterpret_cast<const uint8_t*>(addr);
|
2011-11-03 20:29:47 +00:00
|
|
|
U8CPU a,r,g,b;
|
2014-03-17 21:31:26 +00:00
|
|
|
switch (ct) {
|
|
|
|
case kBGRA_8888_SkColorType:
|
2011-11-03 20:29:47 +00:00
|
|
|
b = static_cast<U8CPU>(c[0]);
|
2014-03-17 21:31:26 +00:00
|
|
|
g = static_cast<U8CPU>(c[1]);
|
|
|
|
r = static_cast<U8CPU>(c[2]);
|
2011-11-03 20:29:47 +00:00
|
|
|
a = static_cast<U8CPU>(c[3]);
|
2014-03-17 21:31:26 +00:00
|
|
|
break;
|
2018-03-22 14:01:16 +00:00
|
|
|
case kRGB_888x_SkColorType: // fallthrough
|
2014-03-17 21:31:26 +00:00
|
|
|
case kRGBA_8888_SkColorType:
|
2011-11-03 20:29:47 +00:00
|
|
|
r = static_cast<U8CPU>(c[0]);
|
|
|
|
g = static_cast<U8CPU>(c[1]);
|
|
|
|
b = static_cast<U8CPU>(c[2]);
|
2018-03-22 14:01:16 +00:00
|
|
|
// We set this even when for kRGB_888x because our caller will validate that it is 0xff.
|
2014-03-17 21:31:26 +00:00
|
|
|
a = static_cast<U8CPU>(c[3]);
|
2011-11-03 20:29:47 +00:00
|
|
|
break;
|
2012-09-25 19:55:07 +00:00
|
|
|
default:
|
2014-03-17 21:31:26 +00:00
|
|
|
SkDEBUGFAIL("Unexpected colortype");
|
2012-09-25 19:55:07 +00:00
|
|
|
return 0;
|
2011-11-03 20:29:47 +00:00
|
|
|
}
|
2014-03-17 21:31:26 +00:00
|
|
|
|
|
|
|
if (*doUnpremul) {
|
2011-11-03 20:29:47 +00:00
|
|
|
r = SkMulDiv255Ceiling(r, a);
|
|
|
|
g = SkMulDiv255Ceiling(g, a);
|
|
|
|
b = SkMulDiv255Ceiling(b, a);
|
|
|
|
}
|
|
|
|
return SkPackARGB32(a, r, g, b);
|
|
|
|
}
|
|
|
|
|
2015-07-16 15:23:13 +00:00
|
|
|
static SkBitmap make_src_bitmap() {
|
2011-11-02 19:57:21 +00:00
|
|
|
static SkBitmap bmp;
|
|
|
|
if (bmp.isNull()) {
|
2014-09-02 19:50:45 +00:00
|
|
|
bmp.allocN32Pixels(DEV_W, DEV_H);
|
2011-11-02 19:57:21 +00:00
|
|
|
intptr_t pixels = reinterpret_cast<intptr_t>(bmp.getPixels());
|
|
|
|
for (int y = 0; y < DEV_H; ++y) {
|
|
|
|
for (int x = 0; x < DEV_W; ++x) {
|
|
|
|
SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bmp.rowBytes() + x * bmp.bytesPerPixel());
|
2015-07-16 15:23:13 +00:00
|
|
|
*pixel = get_src_color(x, y);
|
2011-11-02 19:57:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-16 15:23:13 +00:00
|
|
|
return bmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fill_src_canvas(SkCanvas* canvas) {
|
2011-11-02 19:57:21 +00:00
|
|
|
canvas->save();
|
|
|
|
canvas->setMatrix(SkMatrix::I());
|
2016-12-09 14:00:50 +00:00
|
|
|
canvas->clipRect(DEV_RECT_S, kReplace_SkClipOp);
|
2011-11-02 19:57:21 +00:00
|
|
|
SkPaint paint;
|
2016-10-06 00:33:02 +00:00
|
|
|
paint.setBlendMode(SkBlendMode::kSrc);
|
2015-07-16 15:23:13 +00:00
|
|
|
canvas->drawBitmap(make_src_bitmap(), 0, 0, &paint);
|
2011-11-02 19:57:21 +00:00
|
|
|
canvas->restore();
|
|
|
|
}
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2015-07-16 15:23:13 +00:00
|
|
|
static void fill_dst_bmp_with_init_data(SkBitmap* bitmap) {
|
2011-11-02 19:57:21 +00:00
|
|
|
int w = bitmap->width();
|
|
|
|
int h = bitmap->height();
|
|
|
|
intptr_t pixels = reinterpret_cast<intptr_t>(bitmap->getPixels());
|
|
|
|
for (int y = 0; y < h; ++y) {
|
|
|
|
for (int x = 0; x < w; ++x) {
|
2016-10-11 21:29:12 +00:00
|
|
|
SkPMColor initColor = get_dst_bmp_init_color(x, y, w);
|
|
|
|
if (kAlpha_8_SkColorType == bitmap->colorType()) {
|
|
|
|
uint8_t* alpha = reinterpret_cast<uint8_t*>(pixels + y * bitmap->rowBytes() + x);
|
|
|
|
*alpha = SkGetPackedA32(initColor);
|
|
|
|
} else {
|
|
|
|
SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bitmap->rowBytes() + x * bitmap->bytesPerPixel());
|
|
|
|
*pixel = initColor;
|
|
|
|
}
|
2011-11-02 19:57:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-16 15:23:13 +00:00
|
|
|
static bool check_read_pixel(SkPMColor a, SkPMColor b, bool didPremulConversion) {
|
2011-11-07 15:54:49 +00:00
|
|
|
if (!didPremulConversion) {
|
|
|
|
return a == b;
|
|
|
|
}
|
|
|
|
int32_t aA = static_cast<int32_t>(SkGetPackedA32(a));
|
|
|
|
int32_t aR = static_cast<int32_t>(SkGetPackedR32(a));
|
|
|
|
int32_t aG = static_cast<int32_t>(SkGetPackedG32(a));
|
|
|
|
int32_t aB = SkGetPackedB32(a);
|
|
|
|
|
|
|
|
int32_t bA = static_cast<int32_t>(SkGetPackedA32(b));
|
|
|
|
int32_t bR = static_cast<int32_t>(SkGetPackedR32(b));
|
|
|
|
int32_t bG = static_cast<int32_t>(SkGetPackedG32(b));
|
|
|
|
int32_t bB = static_cast<int32_t>(SkGetPackedB32(b));
|
|
|
|
|
|
|
|
return aA == bA &&
|
|
|
|
SkAbs32(aR - bR) <= 1 &&
|
|
|
|
SkAbs32(aG - bG) <= 1 &&
|
|
|
|
SkAbs32(aB - bB) <= 1;
|
|
|
|
}
|
|
|
|
|
2011-11-02 19:57:21 +00:00
|
|
|
// checks the bitmap contains correct pixels after the readPixels
|
|
|
|
// if the bitmap was prefilled with pixels it checks that these weren't
|
|
|
|
// overwritten in the area outside the readPixels.
|
2018-03-22 14:01:16 +00:00
|
|
|
static bool check_read(skiatest::Reporter* reporter, const SkBitmap& bitmap, int x, int y,
|
|
|
|
bool checkSurfacePixels, bool checkBitmapPixels,
|
let's like, chill out about all these rules, man
There's really no reason to prevent any of these conversions;
they all have somewhat reasonable behavior:
- converting between grey in different color spaces
should probably work just fine
- we'll convert color to gray using a fixed set of
luminance coefficients, but that's better than failing
- we'll invent {r,g,b} = {0,0,0} if we convert alpha
to something with color
- converting to opaque formats without thinking about
premul/unpremul is probably fine, better than just
not working at all
- a missing src color space can always be assumed to be sRGB
Updates to ReadPixelsTest:
- skip more supported test cases in test_conversion(),
each with a TODO
- conversions from non-opaque to opaque should now work
- conversion from A8 to non-A8 should sometimes now
work on GPUs, and the test needed a little bit of
a tweak to not expect A8 to carry around color somehow.
Updates to SRGBReadWritePixelsTest:
- writing untagged pixels shouldn't fail anymore;
instead, it should behave like it was tagged sRGB
Change-Id: I19e78f3a6c89ef74fbcbc985d3fbd77fa984b1c2
Reviewed-on: https://skia-review.googlesource.com/147815
Commit-Queue: Mike Klein <mtklein@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
2018-08-17 18:09:55 +00:00
|
|
|
SkImageInfo surfaceInfo) {
|
2018-03-22 14:01:16 +00:00
|
|
|
SkAlphaType bmpAT = bitmap.alphaType();
|
|
|
|
SkColorType bmpCT = bitmap.colorType();
|
2011-11-02 19:57:21 +00:00
|
|
|
SkASSERT(!bitmap.isNull());
|
2018-03-22 14:01:16 +00:00
|
|
|
SkASSERT(checkSurfacePixels || checkBitmapPixels);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-11-02 19:57:21 +00:00
|
|
|
int bw = bitmap.width();
|
|
|
|
int bh = bitmap.height();
|
|
|
|
|
|
|
|
SkIRect srcRect = SkIRect::MakeXYWH(x, y, bw, bh);
|
|
|
|
SkIRect clippedSrcRect = DEV_RECT;
|
|
|
|
if (!clippedSrcRect.intersect(srcRect)) {
|
|
|
|
clippedSrcRect.setEmpty();
|
|
|
|
}
|
2018-03-22 14:01:16 +00:00
|
|
|
if (kAlpha_8_SkColorType == bmpCT) {
|
2016-10-11 21:29:12 +00:00
|
|
|
for (int by = 0; by < bh; ++by) {
|
|
|
|
for (int bx = 0; bx < bw; ++bx) {
|
|
|
|
int devx = bx + srcRect.fLeft;
|
|
|
|
int devy = by + srcRect.fTop;
|
|
|
|
const uint8_t* alpha = bitmap.getAddr8(bx, by);
|
|
|
|
|
|
|
|
if (clippedSrcRect.contains(devx, devy)) {
|
2018-03-22 14:01:16 +00:00
|
|
|
if (checkSurfacePixels) {
|
let's like, chill out about all these rules, man
There's really no reason to prevent any of these conversions;
they all have somewhat reasonable behavior:
- converting between grey in different color spaces
should probably work just fine
- we'll convert color to gray using a fixed set of
luminance coefficients, but that's better than failing
- we'll invent {r,g,b} = {0,0,0} if we convert alpha
to something with color
- converting to opaque formats without thinking about
premul/unpremul is probably fine, better than just
not working at all
- a missing src color space can always be assumed to be sRGB
Updates to ReadPixelsTest:
- skip more supported test cases in test_conversion(),
each with a TODO
- conversions from non-opaque to opaque should now work
- conversion from A8 to non-A8 should sometimes now
work on GPUs, and the test needed a little bit of
a tweak to not expect A8 to carry around color somehow.
Updates to SRGBReadWritePixelsTest:
- writing untagged pixels shouldn't fail anymore;
instead, it should behave like it was tagged sRGB
Change-Id: I19e78f3a6c89ef74fbcbc985d3fbd77fa984b1c2
Reviewed-on: https://skia-review.googlesource.com/147815
Commit-Queue: Mike Klein <mtklein@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
2018-08-17 18:09:55 +00:00
|
|
|
uint8_t surfaceAlpha = (surfaceInfo.alphaType() == kOpaque_SkAlphaType)
|
2018-03-22 14:01:16 +00:00
|
|
|
? 0xFF
|
|
|
|
: SkGetPackedA32(get_src_color(devx, devy));
|
|
|
|
if (surfaceAlpha != *alpha) {
|
|
|
|
ERRORF(reporter,
|
|
|
|
"Expected readback alpha (%d, %d) value 0x%02x, got 0x%02x. ",
|
|
|
|
bx, by, surfaceAlpha, *alpha);
|
2016-10-11 21:29:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (checkBitmapPixels) {
|
|
|
|
uint32_t origDstAlpha = SkGetPackedA32(get_dst_bmp_init_color(bx, by, bw));
|
|
|
|
if (origDstAlpha != *alpha) {
|
|
|
|
ERRORF(reporter, "Expected clipped out area of readback to be unchanged. "
|
|
|
|
"Expected 0x%02x, got 0x%02x", origDstAlpha, *alpha);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2011-11-02 19:57:21 +00:00
|
|
|
for (int by = 0; by < bh; ++by) {
|
|
|
|
for (int bx = 0; bx < bw; ++bx) {
|
|
|
|
int devx = bx + srcRect.fLeft;
|
|
|
|
int devy = by + srcRect.fTop;
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2014-03-17 21:31:26 +00:00
|
|
|
const uint32_t* pixel = bitmap.getAddr32(bx, by);
|
2011-11-02 19:57:21 +00:00
|
|
|
|
|
|
|
if (clippedSrcRect.contains(devx, devy)) {
|
2018-03-22 14:01:16 +00:00
|
|
|
if (checkSurfacePixels) {
|
|
|
|
SkPMColor surfacePMColor = get_src_color(devx, devy);
|
let's like, chill out about all these rules, man
There's really no reason to prevent any of these conversions;
they all have somewhat reasonable behavior:
- converting between grey in different color spaces
should probably work just fine
- we'll convert color to gray using a fixed set of
luminance coefficients, but that's better than failing
- we'll invent {r,g,b} = {0,0,0} if we convert alpha
to something with color
- converting to opaque formats without thinking about
premul/unpremul is probably fine, better than just
not working at all
- a missing src color space can always be assumed to be sRGB
Updates to ReadPixelsTest:
- skip more supported test cases in test_conversion(),
each with a TODO
- conversions from non-opaque to opaque should now work
- conversion from A8 to non-A8 should sometimes now
work on GPUs, and the test needed a little bit of
a tweak to not expect A8 to carry around color somehow.
Updates to SRGBReadWritePixelsTest:
- writing untagged pixels shouldn't fail anymore;
instead, it should behave like it was tagged sRGB
Change-Id: I19e78f3a6c89ef74fbcbc985d3fbd77fa984b1c2
Reviewed-on: https://skia-review.googlesource.com/147815
Commit-Queue: Mike Klein <mtklein@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
2018-08-17 18:09:55 +00:00
|
|
|
if (SkColorTypeIsAlphaOnly(surfaceInfo.colorType())) {
|
|
|
|
surfacePMColor &= 0xFF000000;
|
|
|
|
}
|
|
|
|
if (kOpaque_SkAlphaType == surfaceInfo.alphaType() || kOpaque_SkAlphaType == bmpAT) {
|
2018-03-22 14:01:16 +00:00
|
|
|
surfacePMColor |= 0xFF000000;
|
|
|
|
}
|
2011-11-07 15:54:49 +00:00
|
|
|
bool didPremul;
|
2018-03-22 14:01:16 +00:00
|
|
|
SkPMColor pmPixel = convert_to_pmcolor(bmpCT, bmpAT, pixel, &didPremul);
|
|
|
|
if (!check_read_pixel(pmPixel, surfacePMColor, didPremul)) {
|
|
|
|
ERRORF(reporter,
|
|
|
|
"Expected readback pixel (%d, %d) value 0x%08x, got 0x%08x. "
|
|
|
|
"Readback was unpremul: %d",
|
|
|
|
bx, by, surfacePMColor, pmPixel, didPremul);
|
2012-08-17 13:32:06 +00:00
|
|
|
return false;
|
2011-11-03 20:29:47 +00:00
|
|
|
}
|
2011-11-02 19:57:21 +00:00
|
|
|
}
|
2011-11-03 20:29:47 +00:00
|
|
|
} else if (checkBitmapPixels) {
|
2015-07-23 15:07:21 +00:00
|
|
|
uint32_t origDstPixel = get_dst_bmp_init_color(bx, by, bw);
|
|
|
|
if (origDstPixel != *pixel) {
|
|
|
|
ERRORF(reporter, "Expected clipped out area of readback to be unchanged. "
|
|
|
|
"Expected 0x%08x, got 0x%08x", origDstPixel, *pixel);
|
2012-08-17 13:32:06 +00:00
|
|
|
return false;
|
2011-11-02 19:57:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-17 13:32:06 +00:00
|
|
|
return true;
|
2011-11-02 19:57:21 +00:00
|
|
|
}
|
|
|
|
|
2019-10-23 17:34:36 +00:00
|
|
|
enum class TightRowBytes : bool { kNo, kYes };
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2019-10-23 17:34:36 +00:00
|
|
|
static void init_bitmap(SkBitmap* bitmap, const SkIRect& rect, TightRowBytes tightRB,
|
|
|
|
SkColorType ct, SkAlphaType at) {
|
2019-09-30 19:12:27 +00:00
|
|
|
SkImageInfo info = SkImageInfo::Make(rect.size(), ct, at);
|
2014-02-13 22:00:04 +00:00
|
|
|
size_t rowBytes = 0;
|
2019-10-23 17:34:36 +00:00
|
|
|
if (tightRB == TightRowBytes::kNo) {
|
|
|
|
rowBytes = SkAlign4((info.width() + 16) * info.bytesPerPixel());
|
2011-11-02 19:57:21 +00:00
|
|
|
}
|
2017-04-17 14:53:29 +00:00
|
|
|
bitmap->allocPixels(info, rowBytes);
|
2011-11-02 19:57:21 +00:00
|
|
|
}
|
|
|
|
|
2015-12-01 12:35:26 +00:00
|
|
|
static const struct {
|
|
|
|
SkColorType fColorType;
|
|
|
|
SkAlphaType fAlphaType;
|
|
|
|
} gReadPixelsConfigs[] = {
|
2018-03-22 14:01:16 +00:00
|
|
|
{kRGBA_8888_SkColorType, kPremul_SkAlphaType},
|
|
|
|
{kRGBA_8888_SkColorType, kUnpremul_SkAlphaType},
|
|
|
|
{kRGB_888x_SkColorType, kOpaque_SkAlphaType},
|
|
|
|
{kBGRA_8888_SkColorType, kPremul_SkAlphaType},
|
|
|
|
{kBGRA_8888_SkColorType, kUnpremul_SkAlphaType},
|
|
|
|
{kAlpha_8_SkColorType, kPremul_SkAlphaType},
|
2015-12-01 12:35:26 +00:00
|
|
|
};
|
|
|
|
const SkIRect gReadPixelsTestRects[] = {
|
|
|
|
// entire thing
|
|
|
|
DEV_RECT,
|
|
|
|
// larger on all sides
|
|
|
|
SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10),
|
|
|
|
// fully contained
|
|
|
|
SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4),
|
|
|
|
// outside top left
|
|
|
|
SkIRect::MakeLTRB(-10, -10, -1, -1),
|
|
|
|
// touching top left corner
|
|
|
|
SkIRect::MakeLTRB(-10, -10, 0, 0),
|
|
|
|
// overlapping top left corner
|
|
|
|
SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4),
|
|
|
|
// overlapping top left and top right corners
|
|
|
|
SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4),
|
|
|
|
// touching entire top edge
|
|
|
|
SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0),
|
|
|
|
// overlapping top right corner
|
|
|
|
SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4),
|
|
|
|
// contained in x, overlapping top edge
|
|
|
|
SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4),
|
|
|
|
// outside top right corner
|
|
|
|
SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1),
|
|
|
|
// touching top right corner
|
|
|
|
SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0),
|
|
|
|
// overlapping top left and bottom left corners
|
|
|
|
SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10),
|
|
|
|
// touching entire left edge
|
|
|
|
SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10),
|
|
|
|
// overlapping bottom left corner
|
|
|
|
SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10),
|
|
|
|
// contained in y, overlapping left edge
|
|
|
|
SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4),
|
|
|
|
// outside bottom left corner
|
|
|
|
SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10),
|
|
|
|
// touching bottom left corner
|
|
|
|
SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10),
|
|
|
|
// overlapping bottom left and bottom right corners
|
|
|
|
SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
|
|
|
|
// touching entire left edge
|
|
|
|
SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10),
|
|
|
|
// overlapping bottom right corner
|
|
|
|
SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
|
|
|
|
// overlapping top right and bottom right corners
|
|
|
|
SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10),
|
|
|
|
};
|
2015-07-16 15:23:13 +00:00
|
|
|
|
2019-07-01 17:05:28 +00:00
|
|
|
bool read_should_succeed(const SkIRect& srcRect, const SkImageInfo& dstInfo,
|
|
|
|
const SkImageInfo& srcInfo) {
|
|
|
|
return SkIRect::Intersects(srcRect, DEV_RECT) && SkImageInfoValidConversion(dstInfo, srcInfo);
|
2018-03-22 14:01:16 +00:00
|
|
|
}
|
|
|
|
|
2016-03-24 01:59:25 +00:00
|
|
|
static void test_readpixels(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
|
2019-10-23 17:34:36 +00:00
|
|
|
const SkImageInfo& surfaceInfo) {
|
2015-12-01 12:35:26 +00:00
|
|
|
SkCanvas* canvas = surface->getCanvas();
|
|
|
|
fill_src_canvas(canvas);
|
|
|
|
for (size_t rect = 0; rect < SK_ARRAY_COUNT(gReadPixelsTestRects); ++rect) {
|
|
|
|
const SkIRect& srcRect = gReadPixelsTestRects[rect];
|
2019-10-23 17:34:36 +00:00
|
|
|
for (auto tightRB : {TightRowBytes::kYes, TightRowBytes::kNo}) {
|
2015-12-01 12:35:26 +00:00
|
|
|
for (size_t c = 0; c < SK_ARRAY_COUNT(gReadPixelsConfigs); ++c) {
|
|
|
|
SkBitmap bmp;
|
2019-10-23 17:34:36 +00:00
|
|
|
init_bitmap(&bmp, srcRect, tightRB, gReadPixelsConfigs[c].fColorType,
|
|
|
|
gReadPixelsConfigs[c].fAlphaType);
|
2015-12-01 12:35:26 +00:00
|
|
|
|
|
|
|
// if the bitmap has pixels allocated before the readPixels,
|
|
|
|
// note that and fill them with pattern
|
|
|
|
bool startsWithPixels = !bmp.isNull();
|
|
|
|
if (startsWithPixels) {
|
|
|
|
fill_dst_bmp_with_init_data(&bmp);
|
2013-02-04 16:13:32 +00:00
|
|
|
}
|
2015-12-01 12:35:26 +00:00
|
|
|
uint32_t idBefore = surface->generationID();
|
2017-07-21 18:24:29 +00:00
|
|
|
bool success = surface->readPixels(bmp, srcRect.fLeft, srcRect.fTop);
|
2015-12-01 12:35:26 +00:00
|
|
|
uint32_t idAfter = surface->generationID();
|
|
|
|
|
2018-03-22 14:01:16 +00:00
|
|
|
// we expect to succeed when the read isn't fully clipped out and the infos are
|
|
|
|
// compatible.
|
2019-07-01 17:05:28 +00:00
|
|
|
bool expectSuccess = read_should_succeed(srcRect, bmp.info(), surfaceInfo);
|
2015-12-01 12:35:26 +00:00
|
|
|
// determine whether we expected the read to succeed.
|
2019-07-01 17:05:28 +00:00
|
|
|
REPORTER_ASSERT(reporter, expectSuccess == success,
|
2018-03-22 14:01:16 +00:00
|
|
|
"Read succeed=%d unexpectedly, src ct/at: %d/%d, dst ct/at: %d/%d",
|
|
|
|
success, surfaceInfo.colorType(), surfaceInfo.alphaType(),
|
|
|
|
bmp.info().colorType(), bmp.info().alphaType());
|
2015-12-01 12:35:26 +00:00
|
|
|
// read pixels should never change the gen id
|
|
|
|
REPORTER_ASSERT(reporter, idBefore == idAfter);
|
|
|
|
|
|
|
|
if (success || startsWithPixels) {
|
2018-03-22 14:01:16 +00:00
|
|
|
check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop, success,
|
let's like, chill out about all these rules, man
There's really no reason to prevent any of these conversions;
they all have somewhat reasonable behavior:
- converting between grey in different color spaces
should probably work just fine
- we'll convert color to gray using a fixed set of
luminance coefficients, but that's better than failing
- we'll invent {r,g,b} = {0,0,0} if we convert alpha
to something with color
- converting to opaque formats without thinking about
premul/unpremul is probably fine, better than just
not working at all
- a missing src color space can always be assumed to be sRGB
Updates to ReadPixelsTest:
- skip more supported test cases in test_conversion(),
each with a TODO
- conversions from non-opaque to opaque should now work
- conversion from A8 to non-A8 should sometimes now
work on GPUs, and the test needed a little bit of
a tweak to not expect A8 to carry around color somehow.
Updates to SRGBReadWritePixelsTest:
- writing untagged pixels shouldn't fail anymore;
instead, it should behave like it was tagged sRGB
Change-Id: I19e78f3a6c89ef74fbcbc985d3fbd77fa984b1c2
Reviewed-on: https://skia-review.googlesource.com/147815
Commit-Queue: Mike Klein <mtklein@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
2018-08-17 18:09:55 +00:00
|
|
|
startsWithPixels, surfaceInfo);
|
2015-12-01 12:35:26 +00:00
|
|
|
} else {
|
|
|
|
// if we had no pixels beforehand and the readPixels
|
|
|
|
// failed then our bitmap should still not have pixels
|
|
|
|
REPORTER_ASSERT(reporter, bmp.isNull());
|
2013-02-04 16:13:32 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-01 12:35:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-22 14:01:16 +00:00
|
|
|
|
2015-12-01 12:35:26 +00:00
|
|
|
DEF_TEST(ReadPixels, reporter) {
|
|
|
|
const SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
|
2016-03-24 01:59:25 +00:00
|
|
|
auto surface(SkSurface::MakeRaster(info));
|
2019-10-23 17:34:36 +00:00
|
|
|
test_readpixels(reporter, surface, info);
|
2015-12-01 12:35:26 +00:00
|
|
|
}
|
2011-11-02 19:57:21 +00:00
|
|
|
|
2017-04-06 11:59:41 +00:00
|
|
|
static void test_readpixels_texture(skiatest::Reporter* reporter,
|
2020-08-11 16:02:22 +00:00
|
|
|
GrDirectContext* dContext,
|
2019-08-21 13:38:10 +00:00
|
|
|
std::unique_ptr<GrSurfaceContext> sContext,
|
2018-03-22 14:01:16 +00:00
|
|
|
const SkImageInfo& surfaceInfo) {
|
2015-12-01 12:35:26 +00:00
|
|
|
for (size_t rect = 0; rect < SK_ARRAY_COUNT(gReadPixelsTestRects); ++rect) {
|
|
|
|
const SkIRect& srcRect = gReadPixelsTestRects[rect];
|
2019-10-23 17:34:36 +00:00
|
|
|
for (auto tightRB : {TightRowBytes::kYes, TightRowBytes::kNo}) {
|
2015-12-01 12:35:26 +00:00
|
|
|
for (size_t c = 0; c < SK_ARRAY_COUNT(gReadPixelsConfigs); ++c) {
|
|
|
|
SkBitmap bmp;
|
2019-10-23 17:34:36 +00:00
|
|
|
init_bitmap(&bmp, srcRect, tightRB, gReadPixelsConfigs[c].fColorType,
|
|
|
|
gReadPixelsConfigs[c].fAlphaType);
|
2015-12-01 12:35:26 +00:00
|
|
|
|
|
|
|
// if the bitmap has pixels allocated before the readPixels,
|
|
|
|
// note that and fill them with pattern
|
|
|
|
bool startsWithPixels = !bmp.isNull();
|
|
|
|
// Try doing the read directly from a non-renderable texture
|
|
|
|
if (startsWithPixels) {
|
|
|
|
fill_dst_bmp_with_init_data(&bmp);
|
2020-08-11 16:02:22 +00:00
|
|
|
bool success = sContext->readPixels(dContext, bmp.info(), bmp.getPixels(),
|
|
|
|
bmp.rowBytes(),
|
2019-10-23 17:34:36 +00:00
|
|
|
{srcRect.fLeft, srcRect.fTop});
|
2019-07-01 17:05:28 +00:00
|
|
|
auto expectSuccess = read_should_succeed(srcRect, bmp.info(), surfaceInfo);
|
2018-03-22 14:01:16 +00:00
|
|
|
REPORTER_ASSERT(
|
2019-07-01 17:05:28 +00:00
|
|
|
reporter, expectSuccess == success,
|
2018-03-22 14:01:16 +00:00
|
|
|
"Read succeed=%d unexpectedly, src ct/at: %d/%d, dst ct/at: %d/%d",
|
|
|
|
success, surfaceInfo.colorType(), surfaceInfo.alphaType(),
|
|
|
|
bmp.info().colorType(), bmp.info().alphaType());
|
|
|
|
if (success) {
|
|
|
|
check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop, success, true,
|
let's like, chill out about all these rules, man
There's really no reason to prevent any of these conversions;
they all have somewhat reasonable behavior:
- converting between grey in different color spaces
should probably work just fine
- we'll convert color to gray using a fixed set of
luminance coefficients, but that's better than failing
- we'll invent {r,g,b} = {0,0,0} if we convert alpha
to something with color
- converting to opaque formats without thinking about
premul/unpremul is probably fine, better than just
not working at all
- a missing src color space can always be assumed to be sRGB
Updates to ReadPixelsTest:
- skip more supported test cases in test_conversion(),
each with a TODO
- conversions from non-opaque to opaque should now work
- conversion from A8 to non-A8 should sometimes now
work on GPUs, and the test needed a little bit of
a tweak to not expect A8 to carry around color somehow.
Updates to SRGBReadWritePixelsTest:
- writing untagged pixels shouldn't fail anymore;
instead, it should behave like it was tagged sRGB
Change-Id: I19e78f3a6c89ef74fbcbc985d3fbd77fa984b1c2
Reviewed-on: https://skia-review.googlesource.com/147815
Commit-Queue: Mike Klein <mtklein@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
2018-08-17 18:09:55 +00:00
|
|
|
surfaceInfo);
|
2018-03-22 14:01:16 +00:00
|
|
|
}
|
2011-11-03 20:29:47 +00:00
|
|
|
}
|
2011-11-02 19:57:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-17 11:43:27 +00:00
|
|
|
|
2016-06-27 21:34:55 +00:00
|
|
|
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadPixels_Texture, reporter, ctxInfo) {
|
2020-08-11 16:02:22 +00:00
|
|
|
auto dContext = ctxInfo.directContext();
|
2020-07-06 14:56:46 +00:00
|
|
|
|
2017-04-17 11:43:27 +00:00
|
|
|
SkBitmap bmp = make_src_bitmap();
|
|
|
|
|
2015-12-01 12:35:26 +00:00
|
|
|
// On the GPU we will also try reading back from a non-renderable texture.
|
2016-11-03 19:21:06 +00:00
|
|
|
for (auto origin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
|
2019-05-13 14:40:06 +00:00
|
|
|
for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) {
|
2020-08-12 18:06:50 +00:00
|
|
|
auto view = sk_gpu_test::MakeTextureProxyViewFromData(
|
2020-08-11 16:02:22 +00:00
|
|
|
dContext, renderable, origin, bmp.info(), bmp.getPixels(), bmp.rowBytes());
|
2020-01-14 14:56:04 +00:00
|
|
|
GrColorType grColorType = SkColorTypeToGrColorType(bmp.colorType());
|
2020-08-11 16:02:22 +00:00
|
|
|
auto sContext = GrSurfaceContext::Make(dContext, std::move(view),
|
2020-01-14 14:56:04 +00:00
|
|
|
grColorType, kPremul_SkAlphaType, nullptr);
|
2018-03-22 14:01:16 +00:00
|
|
|
auto info = SkImageInfo::Make(DEV_W, DEV_H, kN32_SkColorType, kPremul_SkAlphaType);
|
2020-08-11 16:02:22 +00:00
|
|
|
test_readpixels_texture(reporter, dContext, std::move(sContext), info);
|
2016-11-03 19:21:06 +00:00
|
|
|
}
|
2015-12-01 12:35:26 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-14 16:21:02 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static const uint32_t kNumPixels = 5;
|
|
|
|
|
|
|
|
// The five reference pixels are: red, green, blue, white, black.
|
|
|
|
// Five is an interesting number to test because we'll exercise a full 4-wide SIMD vector
|
|
|
|
// plus a tail pixel.
|
|
|
|
static const uint32_t rgba[kNumPixels] = {
|
|
|
|
0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF, 0xFF000000
|
|
|
|
};
|
|
|
|
static const uint32_t bgra[kNumPixels] = {
|
|
|
|
0xFFFF0000, 0xFF00FF00, 0xFF0000FF, 0xFFFFFFFF, 0xFF000000
|
|
|
|
};
|
|
|
|
static const uint16_t rgb565[kNumPixels] = {
|
|
|
|
SK_R16_MASK_IN_PLACE, SK_G16_MASK_IN_PLACE, SK_B16_MASK_IN_PLACE, 0xFFFF, 0x0
|
|
|
|
};
|
|
|
|
|
|
|
|
static const uint16_t rgba4444[kNumPixels] = { 0xF00F, 0x0F0F, 0x00FF, 0xFFFF, 0x000F };
|
|
|
|
|
|
|
|
static const uint64_t kRed = (uint64_t) SK_Half1 << 0;
|
|
|
|
static const uint64_t kGreen = (uint64_t) SK_Half1 << 16;
|
|
|
|
static const uint64_t kBlue = (uint64_t) SK_Half1 << 32;
|
|
|
|
static const uint64_t kAlpha = (uint64_t) SK_Half1 << 48;
|
|
|
|
static const uint64_t f16[kNumPixels] = {
|
|
|
|
kAlpha | kRed, kAlpha | kGreen, kAlpha | kBlue, kAlpha | kBlue | kGreen | kRed, kAlpha
|
|
|
|
};
|
|
|
|
|
|
|
|
static const uint8_t alpha8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
|
|
|
|
static const uint8_t gray8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
|
|
|
|
|
|
|
|
static const void* five_reference_pixels(SkColorType colorType) {
|
|
|
|
switch (colorType) {
|
|
|
|
case kUnknown_SkColorType:
|
|
|
|
return nullptr;
|
|
|
|
case kAlpha_8_SkColorType:
|
|
|
|
return alpha8;
|
|
|
|
case kRGB_565_SkColorType:
|
|
|
|
return rgb565;
|
|
|
|
case kARGB_4444_SkColorType:
|
|
|
|
return rgba4444;
|
|
|
|
case kRGBA_8888_SkColorType:
|
|
|
|
return rgba;
|
|
|
|
case kBGRA_8888_SkColorType:
|
|
|
|
return bgra;
|
|
|
|
case kGray_8_SkColorType:
|
|
|
|
return gray8;
|
|
|
|
case kRGBA_F16_SkColorType:
|
|
|
|
return f16;
|
2017-07-12 19:10:28 +00:00
|
|
|
default:
|
2018-09-04 15:10:04 +00:00
|
|
|
return nullptr;
|
2017-02-14 16:21:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkASSERT(false);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_conversion(skiatest::Reporter* r, const SkImageInfo& dstInfo,
|
|
|
|
const SkImageInfo& srcInfo) {
|
2018-06-04 13:21:17 +00:00
|
|
|
if (!SkImageInfoIsValid(srcInfo)) {
|
2017-02-14 16:21:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const void* srcPixels = five_reference_pixels(srcInfo.colorType());
|
2017-07-12 19:10:28 +00:00
|
|
|
SkPixmap srcPixmap(srcInfo, srcPixels, srcInfo.minRowBytes());
|
2017-02-14 16:21:02 +00:00
|
|
|
sk_sp<SkImage> src = SkImage::MakeFromRaster(srcPixmap, nullptr, nullptr);
|
|
|
|
REPORTER_ASSERT(r, src);
|
|
|
|
|
|
|
|
// Enough space for 5 pixels when color type is F16, more than enough space in other cases.
|
|
|
|
uint64_t dstPixels[kNumPixels];
|
2017-07-12 19:10:28 +00:00
|
|
|
SkPixmap dstPixmap(dstInfo, dstPixels, dstInfo.minRowBytes());
|
2017-02-14 16:21:02 +00:00
|
|
|
bool success = src->readPixels(dstPixmap, 0, 0);
|
|
|
|
REPORTER_ASSERT(r, success == SkImageInfoValidConversion(dstInfo, srcInfo));
|
|
|
|
|
|
|
|
if (success) {
|
|
|
|
if (kGray_8_SkColorType == srcInfo.colorType() &&
|
let's like, chill out about all these rules, man
There's really no reason to prevent any of these conversions;
they all have somewhat reasonable behavior:
- converting between grey in different color spaces
should probably work just fine
- we'll convert color to gray using a fixed set of
luminance coefficients, but that's better than failing
- we'll invent {r,g,b} = {0,0,0} if we convert alpha
to something with color
- converting to opaque formats without thinking about
premul/unpremul is probably fine, better than just
not working at all
- a missing src color space can always be assumed to be sRGB
Updates to ReadPixelsTest:
- skip more supported test cases in test_conversion(),
each with a TODO
- conversions from non-opaque to opaque should now work
- conversion from A8 to non-A8 should sometimes now
work on GPUs, and the test needed a little bit of
a tweak to not expect A8 to carry around color somehow.
Updates to SRGBReadWritePixelsTest:
- writing untagged pixels shouldn't fail anymore;
instead, it should behave like it was tagged sRGB
Change-Id: I19e78f3a6c89ef74fbcbc985d3fbd77fa984b1c2
Reviewed-on: https://skia-review.googlesource.com/147815
Commit-Queue: Mike Klein <mtklein@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
2018-08-17 18:09:55 +00:00
|
|
|
kGray_8_SkColorType != dstInfo.colorType()) {
|
|
|
|
// TODO: test (r,g,b) == (gray,gray,gray)?
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kGray_8_SkColorType == dstInfo.colorType() &&
|
|
|
|
kGray_8_SkColorType != srcInfo.colorType()) {
|
|
|
|
// TODO: test gray = luminance?
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kAlpha_8_SkColorType == srcInfo.colorType() &&
|
|
|
|
kAlpha_8_SkColorType != dstInfo.colorType()) {
|
|
|
|
// TODO: test output = black with this alpha?
|
2017-02-14 16:21:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
REPORTER_ASSERT(r, 0 == memcmp(dstPixels, five_reference_pixels(dstInfo.colorType()),
|
|
|
|
kNumPixels * SkColorTypeBytesPerPixel(dstInfo.colorType())));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(ReadPixels_ValidConversion, reporter) {
|
|
|
|
const SkColorType kColorTypes[] = {
|
|
|
|
kUnknown_SkColorType,
|
|
|
|
kAlpha_8_SkColorType,
|
|
|
|
kRGB_565_SkColorType,
|
|
|
|
kARGB_4444_SkColorType,
|
|
|
|
kRGBA_8888_SkColorType,
|
|
|
|
kBGRA_8888_SkColorType,
|
|
|
|
kGray_8_SkColorType,
|
|
|
|
kRGBA_F16_SkColorType,
|
|
|
|
};
|
|
|
|
|
|
|
|
const SkAlphaType kAlphaTypes[] = {
|
|
|
|
kUnknown_SkAlphaType,
|
|
|
|
kOpaque_SkAlphaType,
|
|
|
|
kPremul_SkAlphaType,
|
|
|
|
kUnpremul_SkAlphaType,
|
|
|
|
};
|
|
|
|
|
|
|
|
const sk_sp<SkColorSpace> kColorSpaces[] = {
|
|
|
|
nullptr,
|
|
|
|
SkColorSpace::MakeSRGB(),
|
|
|
|
};
|
|
|
|
|
|
|
|
for (SkColorType dstCT : kColorTypes) {
|
2020-07-31 00:24:57 +00:00
|
|
|
for (SkAlphaType dstAT : kAlphaTypes) {
|
|
|
|
for (const sk_sp<SkColorSpace>& dstCS : kColorSpaces) {
|
2017-02-14 16:21:02 +00:00
|
|
|
for (SkColorType srcCT : kColorTypes) {
|
2020-07-31 00:24:57 +00:00
|
|
|
for (SkAlphaType srcAT : kAlphaTypes) {
|
|
|
|
for (const sk_sp<SkColorSpace>& srcCS : kColorSpaces) {
|
2017-02-14 16:21:02 +00:00
|
|
|
test_conversion(reporter,
|
|
|
|
SkImageInfo::Make(kNumPixels, 1, dstCT, dstAT, dstCS),
|
|
|
|
SkImageInfo::Make(kNumPixels, 1, srcCT, srcAT, srcCS));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-10 18:24:50 +00:00
|
|
|
|
2019-10-23 17:34:36 +00:00
|
|
|
static constexpr int min_rgb_channel_bits(SkColorType ct) {
|
2019-05-10 20:32:54 +00:00
|
|
|
switch (ct) {
|
2019-09-19 20:05:48 +00:00
|
|
|
case kUnknown_SkColorType: return 0;
|
2019-10-23 17:34:36 +00:00
|
|
|
case kAlpha_8_SkColorType: return 0;
|
|
|
|
case kA16_unorm_SkColorType: return 0;
|
|
|
|
case kA16_float_SkColorType: return 0;
|
2019-09-19 20:05:48 +00:00
|
|
|
case kRGB_565_SkColorType: return 5;
|
|
|
|
case kARGB_4444_SkColorType: return 4;
|
|
|
|
case kR8G8_unorm_SkColorType: return 8;
|
|
|
|
case kR16G16_unorm_SkColorType: return 16;
|
|
|
|
case kR16G16_float_SkColorType: return 16;
|
|
|
|
case kRGBA_8888_SkColorType: return 8;
|
|
|
|
case kRGB_888x_SkColorType: return 8;
|
|
|
|
case kBGRA_8888_SkColorType: return 8;
|
|
|
|
case kRGBA_1010102_SkColorType: return 10;
|
|
|
|
case kRGB_101010x_SkColorType: return 10;
|
2020-02-11 18:19:08 +00:00
|
|
|
case kBGRA_1010102_SkColorType: return 10;
|
|
|
|
case kBGR_101010x_SkColorType: return 10;
|
2019-09-19 20:05:48 +00:00
|
|
|
case kGray_8_SkColorType: return 8; // counting gray as "rgb"
|
|
|
|
case kRGBA_F16Norm_SkColorType: return 10; // just counting the mantissa
|
|
|
|
case kRGBA_F16_SkColorType: return 10; // just counting the mantissa
|
|
|
|
case kRGBA_F32_SkColorType: return 23; // just counting the mantissa
|
|
|
|
case kR16G16B16A16_unorm_SkColorType: return 16;
|
2019-05-10 20:32:54 +00:00
|
|
|
}
|
2019-10-23 17:34:36 +00:00
|
|
|
SkUNREACHABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr int alpha_channel_bits(SkColorType ct) {
|
|
|
|
switch (ct) {
|
|
|
|
case kUnknown_SkColorType: return 0;
|
|
|
|
case kAlpha_8_SkColorType: return 8;
|
|
|
|
case kA16_unorm_SkColorType: return 16;
|
|
|
|
case kA16_float_SkColorType: return 16;
|
|
|
|
case kRGB_565_SkColorType: return 0;
|
|
|
|
case kARGB_4444_SkColorType: return 4;
|
|
|
|
case kR8G8_unorm_SkColorType: return 0;
|
|
|
|
case kR16G16_unorm_SkColorType: return 0;
|
|
|
|
case kR16G16_float_SkColorType: return 0;
|
|
|
|
case kRGBA_8888_SkColorType: return 8;
|
|
|
|
case kRGB_888x_SkColorType: return 0;
|
|
|
|
case kBGRA_8888_SkColorType: return 8;
|
|
|
|
case kRGBA_1010102_SkColorType: return 2;
|
|
|
|
case kRGB_101010x_SkColorType: return 0;
|
2020-02-11 18:19:08 +00:00
|
|
|
case kBGRA_1010102_SkColorType: return 2;
|
|
|
|
case kBGR_101010x_SkColorType: return 0;
|
2019-10-23 17:34:36 +00:00
|
|
|
case kGray_8_SkColorType: return 0;
|
|
|
|
case kRGBA_F16Norm_SkColorType: return 10; // just counting the mantissa
|
|
|
|
case kRGBA_F16_SkColorType: return 10; // just counting the mantissa
|
|
|
|
case kRGBA_F32_SkColorType: return 23; // just counting the mantissa
|
|
|
|
case kR16G16B16A16_unorm_SkColorType: return 16;
|
|
|
|
}
|
|
|
|
SkUNREACHABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct GpuReadPixelTestRules {
|
|
|
|
// Test unpremul sources? We could omit this and detect that creating the source of the read
|
|
|
|
// failed but having it lets us skip generating reference color data.
|
|
|
|
bool fAllowUnpremulSrc = true;
|
|
|
|
// Expect read function to succeed for kUnpremul?
|
|
|
|
bool fAllowUnpremulRead = true;
|
|
|
|
// Are reads that are overlapping but not contained by the src bounds expected to succeed?
|
|
|
|
bool fUncontainedRectSucceeds = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Makes a src populated with the pixmap. The src should get its image info (or equivalent) from
|
|
|
|
// the pixmap.
|
|
|
|
template <typename T> using GpuSrcFactory = T(SkPixmap&);
|
|
|
|
|
2020-06-26 17:32:09 +00:00
|
|
|
enum class GpuReadResult {
|
|
|
|
kFail,
|
|
|
|
kSuccess,
|
|
|
|
kExcusedFailure,
|
|
|
|
};
|
|
|
|
|
2019-10-23 17:34:36 +00:00
|
|
|
// Does a read from the T into the pixmap.
|
2020-06-26 17:32:09 +00:00
|
|
|
template <typename T>
|
|
|
|
using GpuReadSrcFn = GpuReadResult(const T&, const SkIVector& offset, const SkPixmap&);
|
2019-10-23 17:34:36 +00:00
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static void gpu_read_pixels_test_driver(skiatest::Reporter* reporter,
|
|
|
|
const GpuReadPixelTestRules& rules,
|
|
|
|
const std::function<GpuSrcFactory<T>>& srcFactory,
|
|
|
|
const std::function<GpuReadSrcFn<T>>& read) {
|
|
|
|
// Separate this out just to give it some line width to breathe. Note 'srcPixels' should have
|
|
|
|
// the same image info as src. We will do a converting readPixels() on it to get the data
|
|
|
|
// to compare with the results of 'read'.
|
|
|
|
auto runTest = [&](const T& src, const SkPixmap& srcPixels, const SkImageInfo& readInfo,
|
|
|
|
const SkIVector& offset) {
|
|
|
|
const bool csConversion =
|
|
|
|
!SkColorSpace::Equals(readInfo.colorSpace(), srcPixels.info().colorSpace());
|
|
|
|
const auto readCT = readInfo.colorType();
|
|
|
|
const auto readAT = readInfo.alphaType();
|
|
|
|
const auto srcCT = srcPixels.info().colorType();
|
|
|
|
const auto srcAT = srcPixels.info().alphaType();
|
|
|
|
const auto rect = SkIRect::MakeWH(readInfo.width(), readInfo.height()).makeOffset(offset);
|
|
|
|
const auto surfBounds = SkIRect::MakeWH(srcPixels.width(), srcPixels.height());
|
|
|
|
const size_t readBpp = SkColorTypeBytesPerPixel(readCT);
|
|
|
|
|
|
|
|
// Make the row bytes in the dst be loose for extra stress.
|
|
|
|
const size_t dstRB = readBpp * readInfo.width() + 10 * readBpp;
|
|
|
|
// This will make the last row tight.
|
|
|
|
const size_t dstSize = readInfo.computeByteSize(dstRB);
|
|
|
|
std::unique_ptr<char[]> dstData(new char[dstSize]);
|
|
|
|
SkPixmap dstPixels(readInfo, dstData.get(), dstRB);
|
|
|
|
// Initialize with an arbitrary value for each byte. Later we will check that only the
|
|
|
|
// correct part of the destination gets overwritten by 'read'.
|
|
|
|
static constexpr auto kInitialByte = static_cast<char>(0x1B);
|
|
|
|
std::fill_n(static_cast<char*>(dstPixels.writable_addr()),
|
|
|
|
dstPixels.computeByteSize(),
|
|
|
|
kInitialByte);
|
|
|
|
|
2020-06-26 17:32:09 +00:00
|
|
|
const GpuReadResult result = read(src, offset, dstPixels);
|
2019-10-23 17:34:36 +00:00
|
|
|
|
|
|
|
if (!SkIRect::Intersects(rect, surfBounds)) {
|
2020-06-26 17:32:09 +00:00
|
|
|
REPORTER_ASSERT(reporter, result != GpuReadResult::kSuccess);
|
2019-10-23 17:34:36 +00:00
|
|
|
} else if (readCT == kUnknown_SkColorType) {
|
2020-06-26 17:32:09 +00:00
|
|
|
REPORTER_ASSERT(reporter, result != GpuReadResult::kSuccess);
|
2019-10-23 17:34:36 +00:00
|
|
|
} else if (readAT == kUnknown_SkAlphaType) {
|
2020-06-26 17:32:09 +00:00
|
|
|
REPORTER_ASSERT(reporter, result != GpuReadResult::kSuccess);
|
2019-10-23 17:34:36 +00:00
|
|
|
} else if (!rules.fUncontainedRectSucceeds && !surfBounds.contains(rect)) {
|
2020-06-26 17:32:09 +00:00
|
|
|
REPORTER_ASSERT(reporter, result != GpuReadResult::kSuccess);
|
2019-10-23 17:34:36 +00:00
|
|
|
} else if (!rules.fAllowUnpremulRead && readAT == kUnpremul_SkAlphaType) {
|
2020-06-26 17:32:09 +00:00
|
|
|
REPORTER_ASSERT(reporter, result != GpuReadResult::kSuccess);
|
|
|
|
} else if (result == GpuReadResult::kFail) {
|
2020-02-11 18:19:08 +00:00
|
|
|
// TODO: Support RGB/BGR 101010x, BGRA 1010102 on the GPU.
|
|
|
|
if (SkColorTypeToGrColorType(readCT) != GrColorType::kUnknown) {
|
2019-10-23 17:34:36 +00:00
|
|
|
ERRORF(reporter,
|
|
|
|
"Read failed. Src CT: %s, Src AT: %s Read CT: %s, Read AT: %s, "
|
|
|
|
"Rect [%d, %d, %d, %d], CS conversion: %d\n",
|
|
|
|
ToolUtils::colortype_name(srcCT), ToolUtils::alphatype_name(srcAT),
|
|
|
|
ToolUtils::colortype_name(readCT), ToolUtils::alphatype_name(readAT),
|
|
|
|
rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, csConversion);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool guardOk = true;
|
|
|
|
auto guardCheck = [](char x) { return x == kInitialByte; };
|
|
|
|
|
|
|
|
// Considering the rect we tried to read and the surface bounds figure out which pixels in
|
|
|
|
// both src and dst space should actually have been read and written.
|
|
|
|
SkIRect srcReadRect;
|
2020-06-26 17:32:09 +00:00
|
|
|
if (result == GpuReadResult::kSuccess && srcReadRect.intersect(surfBounds, rect)) {
|
2019-10-23 17:34:36 +00:00
|
|
|
SkIRect dstWriteRect = srcReadRect.makeOffset(-rect.fLeft, -rect.fTop);
|
|
|
|
|
2019-11-04 20:04:54 +00:00
|
|
|
const bool lumConversion =
|
2020-03-26 20:17:56 +00:00
|
|
|
!(SkColorTypeChannelFlags(srcCT) & kGray_SkColorChannelFlag) &&
|
|
|
|
(SkColorTypeChannelFlags(readCT) & kGray_SkColorChannelFlag);
|
2019-11-04 20:04:54 +00:00
|
|
|
// A CS or luminance conversion allows a 3 value difference and otherwise a 2 value
|
|
|
|
// difference. Note that sometimes read back on GPU can be lossy even when there no
|
2020-06-26 17:32:09 +00:00
|
|
|
// conversion at all because GPU->CPU read may go to a lower bit depth format and then
|
|
|
|
// be promoted back to the original type. For example, GL ES cannot read to 1010102, so
|
|
|
|
// we go through 8888.
|
2019-11-06 15:56:00 +00:00
|
|
|
const float numer = (lumConversion || csConversion) ? 3.f : 2.f;
|
2020-06-26 17:32:09 +00:00
|
|
|
int rgbBits = std::min(
|
|
|
|
{min_rgb_channel_bits(readCT), min_rgb_channel_bits(srcCT), 8});
|
2019-10-23 17:34:36 +00:00
|
|
|
float tol = numer / (1 << rgbBits);
|
|
|
|
float alphaTol = 0;
|
|
|
|
if (readAT != kOpaque_SkAlphaType && srcAT != kOpaque_SkAlphaType) {
|
2020-06-26 17:32:09 +00:00
|
|
|
// Alpha can also get squashed down to 8 bits going through an intermediate
|
|
|
|
// color format.
|
|
|
|
const int alphaBits =
|
|
|
|
std::min({alpha_channel_bits(readCT), alpha_channel_bits(srcCT), 8});
|
2019-10-23 17:34:36 +00:00
|
|
|
alphaTol = 2.f / (1 << alphaBits);
|
|
|
|
}
|
|
|
|
|
|
|
|
const float tols[4] = {tol, tol, tol, alphaTol};
|
|
|
|
auto error = std::function<ComparePixmapsErrorReporter>([&](int x, int y,
|
|
|
|
const float diffs[4]) {
|
|
|
|
SkASSERT(x >= 0 && y >= 0);
|
|
|
|
ERRORF(reporter,
|
|
|
|
"Src CT: %s, Src AT: %s, Read CT: %s, Read AT: %s, Rect [%d, %d, %d, %d]"
|
|
|
|
", CS conversion: %d\n"
|
|
|
|
"Error at %d, %d. Diff in floats: (%f, %f, %f %f)",
|
|
|
|
ToolUtils::colortype_name(srcCT), ToolUtils::alphatype_name(srcAT),
|
|
|
|
ToolUtils::colortype_name(readCT), ToolUtils::alphatype_name(readAT),
|
|
|
|
rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, csConversion, x, y,
|
|
|
|
diffs[0], diffs[1], diffs[2], diffs[3]);
|
|
|
|
});
|
|
|
|
SkAutoPixmapStorage ref;
|
|
|
|
ref.alloc(readInfo.makeWH(dstWriteRect.width(), dstWriteRect.height()));
|
|
|
|
srcPixels.readPixels(ref, srcReadRect.x(), srcReadRect.y());
|
|
|
|
// This is the part of dstPixels that should have been updated.
|
|
|
|
SkPixmap actual;
|
|
|
|
SkAssertResult(dstPixels.extractSubset(&actual, dstWriteRect));
|
2019-10-25 00:07:39 +00:00
|
|
|
ComparePixels(ref, actual, tols, error);
|
2019-10-23 17:34:36 +00:00
|
|
|
|
|
|
|
const auto* v = dstData.get();
|
|
|
|
const auto* end = dstData.get() + dstSize;
|
|
|
|
guardOk = std::all_of(v, v + dstWriteRect.top() * dstPixels.rowBytes(), guardCheck);
|
|
|
|
v += dstWriteRect.top() * dstPixels.rowBytes();
|
|
|
|
for (int y = dstWriteRect.top(); y < dstWriteRect.bottom(); ++y) {
|
|
|
|
guardOk |= std::all_of(v, v + dstWriteRect.left() * readBpp, guardCheck);
|
|
|
|
auto pad = v + dstWriteRect.right() * readBpp;
|
|
|
|
auto rowEnd = std::min(end, v + dstPixels.rowBytes());
|
|
|
|
// min protects against reading past the end of the tight last row.
|
|
|
|
guardOk |= std::all_of(pad, rowEnd, guardCheck);
|
|
|
|
v = rowEnd;
|
|
|
|
}
|
|
|
|
guardOk |= std::all_of(v, end, guardCheck);
|
|
|
|
} else {
|
|
|
|
guardOk = std::all_of(dstData.get(), dstData.get() + dstSize, guardCheck);
|
|
|
|
}
|
|
|
|
if (!guardOk) {
|
|
|
|
ERRORF(reporter,
|
|
|
|
"Result pixels modified result outside read rect [%d, %d, %d, %d]. "
|
|
|
|
"Src CT: %s, Read CT: %s, CS conversion: %d",
|
|
|
|
rect.fLeft, rect.fTop, rect.fRight, rect.fBottom,
|
|
|
|
ToolUtils::colortype_name(srcCT), ToolUtils::colortype_name(readCT),
|
|
|
|
csConversion);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static constexpr int kW = 16;
|
|
|
|
static constexpr int kH = 16;
|
|
|
|
|
|
|
|
// Makes the reference data that is used to populate the src. Always F32 regardless of srcCT.
|
2020-03-27 18:29:38 +00:00
|
|
|
auto make_ref_f32_data = [](SkAlphaType srcAT, SkColorType srcCT) -> SkAutoPixmapStorage {
|
2019-10-23 17:34:36 +00:00
|
|
|
// Make src data in F32 with srcAT. We will convert it to each color type we test to
|
|
|
|
// initialize the src.
|
|
|
|
const auto refInfo =
|
|
|
|
SkImageInfo::Make(kW, kH, kRGBA_F32_SkColorType, srcAT, SkColorSpace::MakeSRGB());
|
|
|
|
auto refSurf = SkSurface::MakeRaster(refInfo);
|
|
|
|
static constexpr SkPoint kPts1[] = {{0, 0}, {kW, kH}};
|
|
|
|
static constexpr SkColor kColors1[] = {SK_ColorGREEN, SK_ColorRED};
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setShader(
|
|
|
|
SkGradientShader::MakeLinear(kPts1, kColors1, nullptr, 2, SkTileMode::kClamp));
|
|
|
|
refSurf->getCanvas()->drawPaint(paint);
|
|
|
|
static constexpr SkPoint kPts2[] = {{kW, 0}, {0, kH}};
|
|
|
|
static constexpr SkColor kColors2[] = {SK_ColorBLUE, SK_ColorBLACK};
|
|
|
|
paint.setShader(
|
|
|
|
SkGradientShader::MakeLinear(kPts2, kColors2, nullptr, 2, SkTileMode::kClamp));
|
|
|
|
paint.setBlendMode(SkBlendMode::kPlus);
|
|
|
|
refSurf->getCanvas()->drawPaint(paint);
|
|
|
|
// Keep everything opaque if the src alpha type is opaque. Also, there is an issue with
|
|
|
|
// 1010102 (the only color type where the number of alpha bits is non-zero and not the
|
|
|
|
// same as r, g, and b). Because of the different precisions the draw below can create
|
|
|
|
// data that isn't strictly premul (e.g. alpha is 1/3 but green is .4). SW will clamp
|
|
|
|
// r, g, b to a if the dst is premul and a different color type. GPU doesn't do this.
|
|
|
|
// We could but 1010102 premul is kind of dubious anyway. So for now just keep the data
|
|
|
|
// opaque.
|
|
|
|
if (srcAT != kOpaque_SkAlphaType &&
|
2020-02-11 18:19:08 +00:00
|
|
|
(srcAT == kPremul_SkAlphaType && srcCT != kRGBA_1010102_SkColorType
|
|
|
|
&& srcCT != kBGRA_1010102_SkColorType)) {
|
2019-10-23 17:34:36 +00:00
|
|
|
static constexpr SkColor kColors3[] = {SK_ColorWHITE,
|
|
|
|
SK_ColorWHITE,
|
|
|
|
0x60FFFFFF,
|
|
|
|
SK_ColorWHITE,
|
|
|
|
SK_ColorWHITE};
|
|
|
|
static constexpr SkScalar kPos3[] = {0.f, 0.15f, 0.5f, 0.85f, 1.f};
|
|
|
|
paint.setShader(SkGradientShader::MakeRadial({kW / 2.f, kH / 2.f}, (kW + kH) / 10.f,
|
|
|
|
kColors3, kPos3, 5, SkTileMode::kMirror));
|
|
|
|
paint.setBlendMode(SkBlendMode::kDstIn);
|
|
|
|
refSurf->getCanvas()->drawPaint(paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto srcInfo = SkImageInfo::Make(kW, kH, srcCT, srcAT, SkColorSpace::MakeSRGB());
|
|
|
|
SkAutoPixmapStorage srcPixels;
|
|
|
|
srcPixels.alloc(srcInfo);
|
|
|
|
refSurf->readPixels(srcPixels, 0, 0);
|
2020-03-27 18:29:38 +00:00
|
|
|
return srcPixels;
|
2019-10-23 17:34:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
for (int sat = 0; sat < kLastEnum_SkAlphaType; ++sat) {
|
|
|
|
const auto srcAT = static_cast<SkAlphaType>(sat);
|
|
|
|
if (srcAT == kUnknown_SkAlphaType ||
|
|
|
|
(srcAT == kUnpremul_SkAlphaType && !rules.fAllowUnpremulSrc)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (int sct = 0; sct <= kLastEnum_SkColorType; ++sct) {
|
|
|
|
const auto srcCT = static_cast<SkColorType>(sct);
|
|
|
|
// Note that we only currently use srcCT for a 1010102 workaround. If we remove this we
|
|
|
|
// can also but the ref data setup above the srcCT loop.
|
|
|
|
SkAutoPixmapStorage srcPixels = make_ref_f32_data(srcAT, srcCT);
|
|
|
|
auto src = srcFactory(srcPixels);
|
|
|
|
if (!src) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (int rct = 0; rct <= kLastEnum_SkColorType; ++rct) {
|
|
|
|
const auto readCT = static_cast<SkColorType>(rct);
|
|
|
|
for (const sk_sp<SkColorSpace>& readCS :
|
|
|
|
{SkColorSpace::MakeSRGB(), SkColorSpace::MakeSRGBLinear()}) {
|
|
|
|
for (int at = 0; at <= kLastEnum_SkAlphaType; ++at) {
|
|
|
|
const auto readAT = static_cast<SkAlphaType>(at);
|
|
|
|
if (srcAT != kOpaque_SkAlphaType && readAT == kOpaque_SkAlphaType) {
|
|
|
|
// This doesn't make sense.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Test full size, partial, empty, and too wide rects.
|
|
|
|
for (const auto& rect : {
|
|
|
|
// entire thing
|
|
|
|
SkIRect::MakeWH(kW, kH),
|
|
|
|
// larger on all sides
|
|
|
|
SkIRect::MakeLTRB(-10, -10, kW + 10, kH + 10),
|
|
|
|
// fully contained
|
|
|
|
SkIRect::MakeLTRB(kW / 4, kH / 4, 3 * kW / 4, 3 * kH / 4),
|
|
|
|
// outside top left
|
|
|
|
SkIRect::MakeLTRB(-10, -10, -1, -1),
|
|
|
|
// touching top left corner
|
|
|
|
SkIRect::MakeLTRB(-10, -10, 0, 0),
|
|
|
|
// overlapping top left corner
|
|
|
|
SkIRect::MakeLTRB(-10, -10, kW / 4, kH / 4),
|
|
|
|
// overlapping top left and top right corners
|
|
|
|
SkIRect::MakeLTRB(-10, -10, kW + 10, kH / 4),
|
|
|
|
// touching entire top edge
|
|
|
|
SkIRect::MakeLTRB(-10, -10, kW + 10, 0),
|
|
|
|
// overlapping top right corner
|
|
|
|
SkIRect::MakeLTRB(3 * kW / 4, -10, kW + 10, kH / 4),
|
|
|
|
// contained in x, overlapping top edge
|
|
|
|
SkIRect::MakeLTRB(kW / 4, -10, 3 * kW / 4, kH / 4),
|
|
|
|
// outside top right corner
|
|
|
|
SkIRect::MakeLTRB(kW + 1, -10, kW + 10, -1),
|
|
|
|
// touching top right corner
|
|
|
|
SkIRect::MakeLTRB(kW, -10, kW + 10, 0),
|
|
|
|
// overlapping top left and bottom left corners
|
|
|
|
SkIRect::MakeLTRB(-10, -10, kW / 4, kH + 10),
|
|
|
|
// touching entire left edge
|
|
|
|
SkIRect::MakeLTRB(-10, -10, 0, kH + 10),
|
|
|
|
// overlapping bottom left corner
|
|
|
|
SkIRect::MakeLTRB(-10, 3 * kH / 4, kW / 4, kH + 10),
|
|
|
|
// contained in y, overlapping left edge
|
|
|
|
SkIRect::MakeLTRB(-10, kH / 4, kW / 4, 3 * kH / 4),
|
|
|
|
// outside bottom left corner
|
|
|
|
SkIRect::MakeLTRB(-10, kH + 1, -1, kH + 10),
|
|
|
|
// touching bottom left corner
|
|
|
|
SkIRect::MakeLTRB(-10, kH, 0, kH + 10),
|
|
|
|
// overlapping bottom left and bottom right corners
|
|
|
|
SkIRect::MakeLTRB(-10, 3 * kH / 4, kW + 10, kH + 10),
|
|
|
|
// touching entire left edge
|
|
|
|
SkIRect::MakeLTRB(0, kH, kW, kH + 10),
|
|
|
|
// overlapping bottom right corner
|
|
|
|
SkIRect::MakeLTRB(3 * kW / 4, 3 * kH / 4, kW + 10, kH + 10),
|
|
|
|
// overlapping top right and bottom right corners
|
|
|
|
SkIRect::MakeLTRB(3 * kW / 4, -10, kW + 10, kH + 10),
|
|
|
|
}) {
|
|
|
|
const auto readInfo = SkImageInfo::Make(rect.width(), rect.height(),
|
|
|
|
readCT, readAT, readCS);
|
|
|
|
const SkIVector offset = rect.topLeft();
|
|
|
|
runTest(src, srcPixels, readInfo, offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-10 20:32:54 +00:00
|
|
|
}
|
|
|
|
|
2019-10-03 17:26:54 +00:00
|
|
|
namespace {
|
|
|
|
struct AsyncContext {
|
|
|
|
bool fCalled = false;
|
2020-06-26 17:32:09 +00:00
|
|
|
std::unique_ptr<const SkImage::AsyncReadResult> fResult;
|
2019-10-03 17:26:54 +00:00
|
|
|
};
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
// Making this a lambda in the test functions caused:
|
|
|
|
// "error: cannot compile this forwarded non-trivially copyable parameter yet"
|
|
|
|
// on x86/Win/Clang bot, referring to 'result'.
|
2020-06-26 17:32:09 +00:00
|
|
|
static void async_callback(void* c, std::unique_ptr<const SkImage::AsyncReadResult> result) {
|
2019-10-03 17:26:54 +00:00
|
|
|
auto context = static_cast<AsyncContext*>(c);
|
|
|
|
context->fResult = std::move(result);
|
|
|
|
context->fCalled = true;
|
|
|
|
};
|
|
|
|
|
2020-06-26 17:32:09 +00:00
|
|
|
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceAsyncReadPixels, reporter, ctxInfo) {
|
2019-10-23 17:34:36 +00:00
|
|
|
using Surface = sk_sp<SkSurface>;
|
|
|
|
auto reader = std::function<GpuReadSrcFn<Surface>>([](const Surface& surface,
|
|
|
|
const SkIVector& offset,
|
|
|
|
const SkPixmap& pixels) {
|
2020-07-06 19:50:15 +00:00
|
|
|
auto direct = surface->recordingContext()->asDirectContext();
|
|
|
|
SkASSERT(direct);
|
|
|
|
|
2019-10-23 17:34:36 +00:00
|
|
|
AsyncContext context;
|
|
|
|
auto rect = SkIRect::MakeSize(pixels.dimensions()).makeOffset(offset);
|
|
|
|
|
|
|
|
// Rescale quality and linearity don't matter since we're doing a non-scaling readback.
|
2020-06-26 17:32:09 +00:00
|
|
|
surface->asyncRescaleAndReadPixels(pixels.info(), rect, SkImage::RescaleGamma::kSrc,
|
2019-10-23 17:34:36 +00:00
|
|
|
kNone_SkFilterQuality, async_callback, &context);
|
2020-07-06 19:50:15 +00:00
|
|
|
direct->submit();
|
2019-10-23 17:34:36 +00:00
|
|
|
while (!context.fCalled) {
|
2020-07-06 19:50:15 +00:00
|
|
|
direct->checkAsyncWorkCompletion();
|
2019-05-10 18:24:50 +00:00
|
|
|
}
|
2019-10-23 17:34:36 +00:00
|
|
|
if (!context.fResult) {
|
2020-06-26 17:32:09 +00:00
|
|
|
return GpuReadResult::kFail;
|
2019-10-23 17:34:36 +00:00
|
|
|
}
|
|
|
|
SkRectMemcpy(pixels.writable_addr(), pixels.rowBytes(), context.fResult->data(0),
|
|
|
|
context.fResult->rowBytes(0), pixels.info().minRowBytes(), pixels.height());
|
2020-06-26 17:32:09 +00:00
|
|
|
return GpuReadResult::kSuccess;
|
2019-10-23 17:34:36 +00:00
|
|
|
});
|
|
|
|
GpuReadPixelTestRules rules;
|
|
|
|
rules.fAllowUnpremulSrc = false;
|
|
|
|
rules.fAllowUnpremulRead = false;
|
|
|
|
rules.fUncontainedRectSucceeds = false;
|
|
|
|
|
|
|
|
for (GrSurfaceOrigin origin : {kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) {
|
|
|
|
auto factory = std::function<GpuSrcFactory<Surface>>(
|
2020-07-06 14:56:46 +00:00
|
|
|
[context = ctxInfo.directContext(), origin](const SkPixmap& src) {
|
2019-10-23 17:34:36 +00:00
|
|
|
if (src.colorType() == kRGB_888x_SkColorType) {
|
|
|
|
return Surface();
|
|
|
|
}
|
|
|
|
auto surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, src.info(),
|
|
|
|
0, origin, nullptr);
|
|
|
|
if (surf) {
|
|
|
|
surf->writePixels(src, 0, 0);
|
|
|
|
}
|
|
|
|
return surf;
|
|
|
|
});
|
|
|
|
gpu_read_pixels_test_driver(reporter, rules, factory, reader);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-26 17:32:09 +00:00
|
|
|
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageAsyncReadPixels, reporter, ctxInfo) {
|
|
|
|
using Image = sk_sp<SkImage>;
|
2020-07-06 14:56:46 +00:00
|
|
|
auto context = ctxInfo.directContext();
|
2020-06-26 17:32:09 +00:00
|
|
|
auto reader = std::function<GpuReadSrcFn<Image>>([context](const Image& image,
|
|
|
|
const SkIVector& offset,
|
|
|
|
const SkPixmap& pixels) {
|
|
|
|
AsyncContext asyncContext;
|
|
|
|
auto rect = SkIRect::MakeSize(pixels.dimensions()).makeOffset(offset);
|
|
|
|
// The GPU implementation is based on rendering and will fail for non-renderable color
|
|
|
|
// types.
|
|
|
|
auto ct = SkColorTypeToGrColorType(image->colorType());
|
|
|
|
auto format = context->priv().caps()->getDefaultBackendFormat(ct, GrRenderable::kYes);
|
|
|
|
if (!context->priv().caps()->isFormatAsColorTypeRenderable(ct, format)) {
|
|
|
|
return GpuReadResult::kExcusedFailure;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rescale quality and linearity don't matter since we're doing a non-scaling readback.
|
|
|
|
image->asyncRescaleAndReadPixels(pixels.info(), rect, SkImage::RescaleGamma::kSrc,
|
|
|
|
kNone_SkFilterQuality, async_callback, &asyncContext);
|
|
|
|
context->submit();
|
|
|
|
while (!asyncContext.fCalled) {
|
|
|
|
context->checkAsyncWorkCompletion();
|
|
|
|
}
|
|
|
|
if (!asyncContext.fResult) {
|
|
|
|
return GpuReadResult::kFail;
|
|
|
|
}
|
|
|
|
SkRectMemcpy(pixels.writable_addr(), pixels.rowBytes(), asyncContext.fResult->data(0),
|
|
|
|
asyncContext.fResult->rowBytes(0), pixels.info().minRowBytes(),
|
|
|
|
pixels.height());
|
|
|
|
return GpuReadResult::kSuccess;
|
|
|
|
});
|
|
|
|
|
|
|
|
GpuReadPixelTestRules rules;
|
|
|
|
rules.fAllowUnpremulSrc = true;
|
|
|
|
// GPU doesn't support reading to kUnpremul because the rescaling works by rendering and now
|
|
|
|
// we only support premul rendering.
|
|
|
|
rules.fAllowUnpremulRead = false;
|
|
|
|
rules.fUncontainedRectSucceeds = false;
|
|
|
|
|
|
|
|
for (auto origin : {kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) {
|
|
|
|
for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) {
|
|
|
|
auto factory = std::function<GpuSrcFactory<Image>>([&](const SkPixmap& src) {
|
|
|
|
if (src.colorType() == kRGB_888x_SkColorType) {
|
|
|
|
return Image();
|
|
|
|
}
|
2020-07-06 14:56:46 +00:00
|
|
|
return sk_gpu_test::MakeBackendTextureImage(ctxInfo.directContext(), src,
|
|
|
|
renderable, origin);
|
2020-06-26 17:32:09 +00:00
|
|
|
});
|
|
|
|
gpu_read_pixels_test_driver(reporter, rules, factory, reader);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-23 17:34:36 +00:00
|
|
|
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadPixels_Gpu, reporter, ctxInfo) {
|
|
|
|
using Surface = sk_sp<SkSurface>;
|
|
|
|
auto reader = std::function<GpuReadSrcFn<Surface>>(
|
|
|
|
[](const Surface& surface, const SkIVector& offset, const SkPixmap& pixels) {
|
2020-06-26 17:32:09 +00:00
|
|
|
return surface->readPixels(pixels, offset.fX, offset.fY) ? GpuReadResult::kSuccess
|
|
|
|
: GpuReadResult::kFail;
|
2019-10-23 17:34:36 +00:00
|
|
|
});
|
|
|
|
GpuReadPixelTestRules rules;
|
|
|
|
rules.fAllowUnpremulSrc = false;
|
|
|
|
rules.fAllowUnpremulRead = true;
|
|
|
|
rules.fUncontainedRectSucceeds = true;
|
|
|
|
|
|
|
|
for (GrSurfaceOrigin origin : {kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) {
|
|
|
|
auto factory = std::function<GpuSrcFactory<Surface>>(
|
2020-07-06 14:56:46 +00:00
|
|
|
[context = ctxInfo.directContext(), origin](const SkPixmap& src) {
|
2019-10-23 17:34:36 +00:00
|
|
|
if (src.colorType() == kRGB_888x_SkColorType) {
|
|
|
|
return Surface();
|
|
|
|
}
|
|
|
|
auto surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, src.info(),
|
|
|
|
0, origin, nullptr);
|
|
|
|
if (surf) {
|
|
|
|
surf->writePixels(src, 0, 0);
|
|
|
|
}
|
|
|
|
return surf;
|
|
|
|
});
|
|
|
|
gpu_read_pixels_test_driver(reporter, rules, factory, reader);
|
2019-05-10 18:24:50 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-03 17:26:54 +00:00
|
|
|
|
|
|
|
DEF_GPUTEST(AsyncReadPixelsContextShutdown, reporter, options) {
|
|
|
|
const auto ii = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
|
|
|
|
SkColorSpace::MakeSRGB());
|
|
|
|
enum class ShutdownSequence {
|
|
|
|
kFreeResult_DestroyContext,
|
|
|
|
kDestroyContext_FreeResult,
|
|
|
|
kFreeResult_ReleaseAndAbandon_DestroyContext,
|
|
|
|
kFreeResult_Abandon_DestroyContext,
|
|
|
|
kReleaseAndAbandon_FreeResult_DestroyContext,
|
|
|
|
kAbandon_FreeResult_DestroyContext,
|
|
|
|
kReleaseAndAbandon_DestroyContext_FreeResult,
|
|
|
|
kAbandon_DestroyContext_FreeResult,
|
|
|
|
};
|
|
|
|
for (int t = 0; t < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++t) {
|
|
|
|
auto type = static_cast<sk_gpu_test::GrContextFactory::ContextType>(t);
|
|
|
|
for (auto sequence : {ShutdownSequence::kFreeResult_DestroyContext,
|
|
|
|
ShutdownSequence::kDestroyContext_FreeResult,
|
|
|
|
ShutdownSequence::kFreeResult_ReleaseAndAbandon_DestroyContext,
|
|
|
|
ShutdownSequence::kFreeResult_Abandon_DestroyContext,
|
|
|
|
ShutdownSequence::kReleaseAndAbandon_FreeResult_DestroyContext,
|
|
|
|
ShutdownSequence::kAbandon_FreeResult_DestroyContext,
|
|
|
|
ShutdownSequence::kReleaseAndAbandon_DestroyContext_FreeResult,
|
|
|
|
ShutdownSequence::kAbandon_DestroyContext_FreeResult}) {
|
|
|
|
// Vulkan context abandoning without resource release has issues outside of the scope of
|
|
|
|
// this test.
|
|
|
|
if (type == sk_gpu_test::GrContextFactory::kVulkan_ContextType &&
|
2020-06-18 19:59:17 +00:00
|
|
|
(sequence == ShutdownSequence::kFreeResult_ReleaseAndAbandon_DestroyContext ||
|
|
|
|
sequence == ShutdownSequence::kFreeResult_Abandon_DestroyContext ||
|
|
|
|
sequence == ShutdownSequence::kReleaseAndAbandon_FreeResult_DestroyContext ||
|
|
|
|
sequence == ShutdownSequence::kReleaseAndAbandon_DestroyContext_FreeResult ||
|
|
|
|
sequence == ShutdownSequence::kAbandon_FreeResult_DestroyContext ||
|
|
|
|
sequence == ShutdownSequence::kAbandon_DestroyContext_FreeResult)) {
|
2019-10-03 17:26:54 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (bool yuv : {false, true}) {
|
|
|
|
sk_gpu_test::GrContextFactory factory(options);
|
2020-07-06 19:50:15 +00:00
|
|
|
auto direct = factory.get(type);
|
|
|
|
if (!direct) {
|
2019-10-03 17:26:54 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-01-10 18:04:45 +00:00
|
|
|
// This test is only meaningful for contexts that support transfer buffers for
|
|
|
|
// reads.
|
2020-07-06 19:50:15 +00:00
|
|
|
if (!direct->priv().caps()->transferFromSurfaceToBufferSupport()) {
|
2019-10-03 17:26:54 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-07-06 19:50:15 +00:00
|
|
|
auto surf = SkSurface::MakeRenderTarget(direct, SkBudgeted::kYes, ii, 1, nullptr);
|
2019-10-03 17:26:54 +00:00
|
|
|
if (!surf) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
AsyncContext cbContext;
|
|
|
|
if (yuv) {
|
|
|
|
surf->asyncRescaleAndReadPixelsYUV420(
|
|
|
|
kIdentity_SkYUVColorSpace, SkColorSpace::MakeSRGB(), ii.bounds(),
|
2020-06-26 17:32:09 +00:00
|
|
|
ii.dimensions(), SkImage::RescaleGamma::kSrc, kNone_SkFilterQuality,
|
2019-10-03 17:26:54 +00:00
|
|
|
&async_callback, &cbContext);
|
|
|
|
} else {
|
2020-06-26 17:32:09 +00:00
|
|
|
surf->asyncRescaleAndReadPixels(ii, ii.bounds(), SkImage::RescaleGamma::kSrc,
|
2019-10-03 17:26:54 +00:00
|
|
|
kNone_SkFilterQuality, &async_callback,
|
|
|
|
&cbContext);
|
|
|
|
}
|
2020-07-06 19:50:15 +00:00
|
|
|
direct->submit();
|
2019-10-03 17:26:54 +00:00
|
|
|
while (!cbContext.fCalled) {
|
2020-07-06 19:50:15 +00:00
|
|
|
direct->checkAsyncWorkCompletion();
|
2019-10-03 17:26:54 +00:00
|
|
|
}
|
|
|
|
if (!cbContext.fResult) {
|
|
|
|
ERRORF(reporter, "Callback failed on %s. is YUV: %d",
|
|
|
|
sk_gpu_test::GrContextFactory::ContextTypeName(type), yuv);
|
|
|
|
continue;
|
|
|
|
}
|
2020-07-23 17:54:35 +00:00
|
|
|
// For vulkan we need to release all refs to the GrDirectContext before trying to
|
|
|
|
// destroy the test context. The surface here is holding a ref.
|
2020-06-18 19:59:17 +00:00
|
|
|
surf.reset();
|
|
|
|
|
2019-10-03 17:26:54 +00:00
|
|
|
// The real test is that we don't crash, get Vulkan validation errors, etc, during
|
|
|
|
// this shutdown sequence.
|
|
|
|
switch (sequence) {
|
|
|
|
case ShutdownSequence::kFreeResult_DestroyContext:
|
|
|
|
case ShutdownSequence::kFreeResult_ReleaseAndAbandon_DestroyContext:
|
|
|
|
case ShutdownSequence::kFreeResult_Abandon_DestroyContext:
|
|
|
|
break;
|
|
|
|
case ShutdownSequence::kDestroyContext_FreeResult:
|
|
|
|
factory.destroyContexts();
|
|
|
|
break;
|
|
|
|
case ShutdownSequence::kReleaseAndAbandon_FreeResult_DestroyContext:
|
|
|
|
factory.releaseResourcesAndAbandonContexts();
|
|
|
|
break;
|
|
|
|
case ShutdownSequence::kAbandon_FreeResult_DestroyContext:
|
|
|
|
factory.abandonContexts();
|
|
|
|
break;
|
|
|
|
case ShutdownSequence::kReleaseAndAbandon_DestroyContext_FreeResult:
|
|
|
|
factory.releaseResourcesAndAbandonContexts();
|
|
|
|
factory.destroyContexts();
|
|
|
|
break;
|
|
|
|
case ShutdownSequence::kAbandon_DestroyContext_FreeResult:
|
|
|
|
factory.abandonContexts();
|
|
|
|
factory.destroyContexts();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
cbContext.fResult.reset();
|
|
|
|
switch (sequence) {
|
|
|
|
case ShutdownSequence::kFreeResult_ReleaseAndAbandon_DestroyContext:
|
|
|
|
factory.releaseResourcesAndAbandonContexts();
|
|
|
|
break;
|
|
|
|
case ShutdownSequence::kFreeResult_Abandon_DestroyContext:
|
|
|
|
factory.abandonContexts();
|
|
|
|
break;
|
|
|
|
case ShutdownSequence::kFreeResult_DestroyContext:
|
|
|
|
case ShutdownSequence::kDestroyContext_FreeResult:
|
|
|
|
case ShutdownSequence::kReleaseAndAbandon_FreeResult_DestroyContext:
|
|
|
|
case ShutdownSequence::kAbandon_FreeResult_DestroyContext:
|
|
|
|
case ShutdownSequence::kReleaseAndAbandon_DestroyContext_FreeResult:
|
|
|
|
case ShutdownSequence::kAbandon_DestroyContext_FreeResult:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|