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.
|
|
|
|
*/
|
|
|
|
|
2018-03-07 18:01:25 +00:00
|
|
|
#include <initializer_list>
|
2011-11-02 19:57:21 +00:00
|
|
|
#include "SkCanvas.h"
|
2017-09-15 15:59:23 +00:00
|
|
|
#include "SkColorData.h"
|
2017-02-14 16:21:02 +00:00
|
|
|
#include "SkHalf.h"
|
|
|
|
#include "SkImageInfoPriv.h"
|
2012-08-07 21:35:13 +00:00
|
|
|
#include "SkMathPriv.h"
|
2014-06-30 16:05:34 +00:00
|
|
|
#include "SkSurface.h"
|
2014-01-10 22:08:27 +00:00
|
|
|
#include "Test.h"
|
|
|
|
|
2012-08-02 14:03:32 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
2015-12-01 12:35:26 +00:00
|
|
|
#include "GrContext.h"
|
2017-06-08 20:38:25 +00:00
|
|
|
#include "GrContextFactory.h"
|
2017-04-06 11:59:41 +00:00
|
|
|
#include "GrContextPriv.h"
|
2018-01-16 13:06:32 +00:00
|
|
|
#include "GrProxyProvider.h"
|
2018-03-07 18:01:25 +00:00
|
|
|
#include "ProxyUtils.h"
|
2015-07-16 15:23:13 +00:00
|
|
|
#include "SkGr.h"
|
2012-08-02 14:03:32 +00:00
|
|
|
#endif
|
2011-11-02 19:57:21 +00:00
|
|
|
|
2015-12-01 12:35:26 +00:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
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]);
|
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.
|
2015-07-16 15:23:13 +00:00
|
|
|
static bool check_read(skiatest::Reporter* reporter,
|
|
|
|
const SkBitmap& bitmap,
|
|
|
|
int x, int y,
|
|
|
|
bool checkCanvasPixels,
|
2016-10-11 21:29:12 +00:00
|
|
|
bool checkBitmapPixels,
|
|
|
|
SkColorType ct,
|
|
|
|
SkAlphaType at) {
|
|
|
|
SkASSERT(ct == bitmap.colorType() && at == bitmap.alphaType());
|
2011-11-02 19:57:21 +00:00
|
|
|
SkASSERT(!bitmap.isNull());
|
2011-11-03 20:29:47 +00:00
|
|
|
SkASSERT(checkCanvasPixels || 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();
|
|
|
|
}
|
2016-10-11 21:29:12 +00:00
|
|
|
if (kAlpha_8_SkColorType == ct) {
|
|
|
|
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)) {
|
|
|
|
if (checkCanvasPixels) {
|
|
|
|
uint8_t canvasAlpha = SkGetPackedA32(get_src_color(devx, devy));
|
|
|
|
if (canvasAlpha != *alpha) {
|
|
|
|
ERRORF(reporter, "Expected readback alpha (%d, %d) value 0x%02x, got 0x%02x. ",
|
|
|
|
bx, by, canvasAlpha, *alpha);
|
|
|
|
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)) {
|
2011-11-03 20:29:47 +00:00
|
|
|
if (checkCanvasPixels) {
|
2015-07-16 15:23:13 +00:00
|
|
|
SkPMColor canvasPixel = get_src_color(devx, devy);
|
2011-11-07 15:54:49 +00:00
|
|
|
bool didPremul;
|
2015-07-16 15:23:13 +00:00
|
|
|
SkPMColor pmPixel = convert_to_pmcolor(ct, at, pixel, &didPremul);
|
2015-07-23 15:07:21 +00:00
|
|
|
if (!check_read_pixel(pmPixel, canvasPixel, didPremul)) {
|
2016-06-27 21:34:55 +00:00
|
|
|
ERRORF(reporter, "Expected readback pixel (%d, %d) value 0x%08x, got 0x%08x. "
|
|
|
|
"Readback was unpremul: %d", bx, by, canvasPixel, 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
|
|
|
}
|
|
|
|
|
|
|
|
enum BitmapInit {
|
|
|
|
kFirstBitmapInit = 0,
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2017-04-17 14:53:29 +00:00
|
|
|
kTight_BitmapInit = kFirstBitmapInit,
|
2011-11-02 19:57:21 +00:00
|
|
|
kRowBytes_BitmapInit,
|
2016-02-01 20:49:30 +00:00
|
|
|
kRowBytesOdd_BitmapInit,
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2016-02-01 20:49:30 +00:00
|
|
|
kLastAligned_BitmapInit = kRowBytes_BitmapInit,
|
2016-02-01 21:44:22 +00:00
|
|
|
|
|
|
|
#if 0 // THIS CAUSES ERRORS ON WINDOWS AND SOME ANDROID DEVICES
|
2016-02-01 20:49:30 +00:00
|
|
|
kLast_BitmapInit = kRowBytesOdd_BitmapInit
|
2016-02-01 21:44:22 +00:00
|
|
|
#else
|
|
|
|
kLast_BitmapInit = kLastAligned_BitmapInit
|
|
|
|
#endif
|
2011-11-02 19:57:21 +00:00
|
|
|
};
|
|
|
|
|
2013-10-12 17:25:17 +00:00
|
|
|
static BitmapInit nextBMI(BitmapInit bmi) {
|
2011-11-02 19:57:21 +00:00
|
|
|
int x = bmi;
|
|
|
|
return static_cast<BitmapInit>(++x);
|
|
|
|
}
|
|
|
|
|
2014-03-17 21:31:26 +00:00
|
|
|
static void init_bitmap(SkBitmap* bitmap, const SkIRect& rect, BitmapInit init, SkColorType ct,
|
|
|
|
SkAlphaType at) {
|
|
|
|
SkImageInfo info = SkImageInfo::Make(rect.width(), rect.height(), ct, at);
|
2014-02-13 22:00:04 +00:00
|
|
|
size_t rowBytes = 0;
|
2011-11-02 19:57:21 +00:00
|
|
|
switch (init) {
|
|
|
|
case kTight_BitmapInit:
|
|
|
|
break;
|
|
|
|
case kRowBytes_BitmapInit:
|
2016-10-11 21:29:12 +00:00
|
|
|
rowBytes = SkAlign4((info.width() + 16) * info.bytesPerPixel());
|
2011-11-02 19:57:21 +00:00
|
|
|
break;
|
2016-02-01 20:49:30 +00:00
|
|
|
case kRowBytesOdd_BitmapInit:
|
2016-10-11 21:29:12 +00:00
|
|
|
rowBytes = SkAlign4(info.width() * info.bytesPerPixel()) + 3;
|
2016-02-01 20:49:30 +00:00
|
|
|
break;
|
2011-11-02 19:57:21 +00:00
|
|
|
default:
|
|
|
|
SkASSERT(0);
|
|
|
|
break;
|
|
|
|
}
|
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[] = {
|
|
|
|
{ kRGBA_8888_SkColorType, kPremul_SkAlphaType },
|
|
|
|
{ kRGBA_8888_SkColorType, kUnpremul_SkAlphaType },
|
|
|
|
{ kBGRA_8888_SkColorType, kPremul_SkAlphaType },
|
|
|
|
{ kBGRA_8888_SkColorType, kUnpremul_SkAlphaType },
|
2016-10-11 21:29:12 +00:00
|
|
|
{ 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
|
|
|
|
2016-03-24 01:59:25 +00:00
|
|
|
static void test_readpixels(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
|
2016-02-01 20:49:30 +00:00
|
|
|
BitmapInit lastBitmapInit) {
|
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];
|
2016-02-01 20:49:30 +00:00
|
|
|
for (BitmapInit bmi = kFirstBitmapInit; bmi <= lastBitmapInit; bmi = nextBMI(bmi)) {
|
2015-12-01 12:35:26 +00:00
|
|
|
for (size_t c = 0; c < SK_ARRAY_COUNT(gReadPixelsConfigs); ++c) {
|
|
|
|
SkBitmap bmp;
|
|
|
|
init_bitmap(&bmp, srcRect, bmi,
|
|
|
|
gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType);
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
|
|
|
// we expect to succeed when the read isn't fully clipped
|
|
|
|
// out.
|
|
|
|
bool expectSuccess = SkIRect::Intersects(srcRect, DEV_RECT);
|
|
|
|
// determine whether we expected the read to succeed.
|
|
|
|
REPORTER_ASSERT(reporter, success == expectSuccess);
|
|
|
|
// read pixels should never change the gen id
|
|
|
|
REPORTER_ASSERT(reporter, idBefore == idAfter);
|
|
|
|
|
|
|
|
if (success || startsWithPixels) {
|
|
|
|
check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop,
|
2016-10-11 21:29:12 +00:00
|
|
|
success, startsWithPixels,
|
|
|
|
gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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));
|
2016-02-01 20:49:30 +00:00
|
|
|
// SW readback fails a premul check when reading back to an unaligned rowbytes.
|
|
|
|
test_readpixels(reporter, surface, kLastAligned_BitmapInit);
|
2015-12-01 12:35:26 +00:00
|
|
|
}
|
|
|
|
#if SK_SUPPORT_GPU
|
2016-06-27 21:34:55 +00:00
|
|
|
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadPixels_Gpu, reporter, ctxInfo) {
|
2017-06-08 20:38:25 +00:00
|
|
|
if (ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_D3D9_ES2_ContextType ||
|
|
|
|
ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_GL_ES2_ContextType ||
|
|
|
|
ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_D3D11_ES2_ContextType) {
|
|
|
|
// skbug.com/6742 ReadPixels_Texture & _Gpu don't work with ANGLE ES2 configs
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-26 18:38:17 +00:00
|
|
|
const SkImageInfo ii = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
|
2015-12-01 12:35:26 +00:00
|
|
|
for (auto& origin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
|
2016-07-26 18:38:17 +00:00
|
|
|
sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo,
|
|
|
|
ii, 0, origin, nullptr));
|
2016-02-01 20:49:30 +00:00
|
|
|
test_readpixels(reporter, surface, kLast_BitmapInit);
|
2015-12-01 12:35:26 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-16 15:23:13 +00:00
|
|
|
#endif
|
2011-11-02 19:57:21 +00:00
|
|
|
|
2015-07-16 15:23:13 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
2017-04-06 11:59:41 +00:00
|
|
|
static void test_readpixels_texture(skiatest::Reporter* reporter,
|
2017-04-17 11:43:27 +00:00
|
|
|
sk_sp<GrSurfaceContext> sContext) {
|
2015-12-01 12:35:26 +00:00
|
|
|
for (size_t rect = 0; rect < SK_ARRAY_COUNT(gReadPixelsTestRects); ++rect) {
|
|
|
|
const SkIRect& srcRect = gReadPixelsTestRects[rect];
|
2016-02-01 20:49:30 +00:00
|
|
|
for (BitmapInit bmi = kFirstBitmapInit; bmi <= kLast_BitmapInit; bmi = nextBMI(bmi)) {
|
2015-12-01 12:35:26 +00:00
|
|
|
for (size_t c = 0; c < SK_ARRAY_COUNT(gReadPixelsConfigs); ++c) {
|
|
|
|
SkBitmap bmp;
|
|
|
|
init_bitmap(&bmp, srcRect, bmi,
|
|
|
|
gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType);
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
uint32_t flags = 0;
|
|
|
|
if (gReadPixelsConfigs[c].fAlphaType == kUnpremul_SkAlphaType) {
|
2017-04-06 11:59:41 +00:00
|
|
|
flags = GrContextPriv::kUnpremul_PixelOpsFlag;
|
2011-11-03 20:29:47 +00:00
|
|
|
}
|
2017-04-17 11:43:27 +00:00
|
|
|
bool success = sContext->readPixels(bmp.info(), bmp.getPixels(),
|
|
|
|
bmp.rowBytes(),
|
|
|
|
srcRect.fLeft, srcRect.fTop, flags);
|
2015-12-01 12:35:26 +00:00
|
|
|
check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop,
|
2016-10-11 21:29:12 +00:00
|
|
|
success, true,
|
|
|
|
gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType);
|
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) {
|
2017-06-08 20:38:25 +00:00
|
|
|
if (ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_D3D9_ES2_ContextType ||
|
|
|
|
ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_GL_ES2_ContextType ||
|
|
|
|
ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_D3D11_ES2_ContextType) {
|
|
|
|
// skbug.com/6742 ReadPixels_Texture & _Gpu don't work with ANGLE ES2 configs
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-17 11:43:27 +00:00
|
|
|
GrContext* context = ctxInfo.grContext();
|
|
|
|
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}) {
|
2018-03-07 18:01:25 +00:00
|
|
|
for (auto isRT : {false, true}) {
|
|
|
|
sk_sp<GrTextureProxy> proxy = sk_gpu_test::MakeTextureProxyFromData(
|
|
|
|
context, isRT, DEV_W, DEV_H, bmp.colorType(), origin, bmp.getPixels(),
|
|
|
|
bmp.rowBytes());
|
2017-04-17 11:43:27 +00:00
|
|
|
sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeWrappedSurfaceContext(
|
2018-01-31 14:29:48 +00:00
|
|
|
std::move(proxy));
|
2017-04-17 11:43:27 +00:00
|
|
|
test_readpixels_texture(reporter, std::move(sContext));
|
2016-11-03 19:21:06 +00:00
|
|
|
}
|
2015-12-01 12:35:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
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:
|
|
|
|
return nullptr; // remove me when kIndex_8 is removed from the enum
|
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) {
|
2017-05-02 20:04:56 +00:00
|
|
|
if (!SkImageInfoIsValidRenderingCS(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() &&
|
|
|
|
kGray_8_SkColorType != dstInfo.colorType())
|
|
|
|
{
|
|
|
|
// This conversion is legal, but we won't get the "reference" pixels since we cannot
|
|
|
|
// represent colors in kGray8.
|
|
|
|
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) {
|
|
|
|
for (SkAlphaType dstAT: kAlphaTypes) {
|
|
|
|
for (sk_sp<SkColorSpace> dstCS : kColorSpaces) {
|
|
|
|
for (SkColorType srcCT : kColorTypes) {
|
|
|
|
for (SkAlphaType srcAT: kAlphaTypes) {
|
|
|
|
for (sk_sp<SkColorSpace> srcCS : kColorSpaces) {
|
|
|
|
if (kRGBA_F16_SkColorType == dstCT && dstCS) {
|
2017-12-12 19:09:31 +00:00
|
|
|
dstCS = dstCS->makeLinearGamma();
|
2017-02-14 16:21:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (kRGBA_F16_SkColorType == srcCT && srcCS) {
|
2017-12-12 19:09:31 +00:00
|
|
|
srcCS = srcCS->makeLinearGamma();
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|