2011-07-28 14:26:00 +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.
|
|
|
|
*/
|
2013-12-12 21:11:12 +00:00
|
|
|
|
2010-03-08 20:23:57 +00:00
|
|
|
#include "SkBitmap.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkColorPriv.h"
|
2010-03-15 20:48:02 +00:00
|
|
|
#include "SkGradientShader.h"
|
2010-03-08 20:23:57 +00:00
|
|
|
#include "SkRect.h"
|
2017-04-03 15:11:09 +00:00
|
|
|
#include "SkVertices.h"
|
2014-01-24 20:56:26 +00:00
|
|
|
#include "Test.h"
|
2010-03-08 20:23:57 +00:00
|
|
|
|
2016-11-23 15:55:18 +00:00
|
|
|
#include "sk_tool_utils.h"
|
|
|
|
|
2014-02-13 22:00:04 +00:00
|
|
|
// these are in the same order as the SkColorType enum
|
2014-03-05 13:43:15 +00:00
|
|
|
static const char* gColorTypeName[] = {
|
|
|
|
"None", "A8", "565", "4444", "RGBA", "BGRA", "Index8"
|
2010-03-08 20:23:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Returns -1 on success, else the x coord of the first bad pixel, return its
|
|
|
|
value in bad
|
|
|
|
*/
|
|
|
|
typedef int (*Proc)(const void*, int width, uint32_t expected, uint32_t* bad);
|
|
|
|
|
|
|
|
static int proc_32(const void* ptr, int w, uint32_t expected, uint32_t* bad) {
|
|
|
|
const SkPMColor* addr = static_cast<const SkPMColor*>(ptr);
|
|
|
|
for (int x = 0; x < w; x++) {
|
|
|
|
if (addr[x] != expected) {
|
|
|
|
*bad = addr[x];
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int proc_16(const void* ptr, int w, uint32_t expected, uint32_t* bad) {
|
|
|
|
const uint16_t* addr = static_cast<const uint16_t*>(ptr);
|
|
|
|
for (int x = 0; x < w; x++) {
|
|
|
|
if (addr[x] != expected) {
|
|
|
|
*bad = addr[x];
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int proc_8(const void* ptr, int w, uint32_t expected, uint32_t* bad) {
|
|
|
|
const SkPMColor* addr = static_cast<const SkPMColor*>(ptr);
|
|
|
|
for (int x = 0; x < w; x++) {
|
|
|
|
if (SkGetPackedA32(addr[x]) != expected) {
|
|
|
|
*bad = SkGetPackedA32(addr[x]);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-02-27 19:17:41 +00:00
|
|
|
static int proc_bad(const void*, int, uint32_t, uint32_t* bad) {
|
2010-03-08 20:23:57 +00:00
|
|
|
*bad = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Proc find_proc(const SkBitmap& bm, SkPMColor expect32, uint16_t expect16,
|
|
|
|
uint8_t expect8, uint32_t* expect) {
|
2014-03-05 13:43:15 +00:00
|
|
|
switch (bm.colorType()) {
|
2014-04-11 17:15:40 +00:00
|
|
|
case kN32_SkColorType:
|
2010-03-08 20:23:57 +00:00
|
|
|
*expect = expect32;
|
|
|
|
return proc_32;
|
2014-03-05 13:43:15 +00:00
|
|
|
case kARGB_4444_SkColorType:
|
|
|
|
case kRGB_565_SkColorType:
|
2010-03-08 20:23:57 +00:00
|
|
|
*expect = expect16;
|
|
|
|
return proc_16;
|
2014-03-05 13:43:15 +00:00
|
|
|
case kAlpha_8_SkColorType:
|
2010-03-08 20:23:57 +00:00
|
|
|
*expect = expect8;
|
|
|
|
return proc_8;
|
|
|
|
default:
|
|
|
|
*expect = 0;
|
|
|
|
return proc_bad;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool check_color(const SkBitmap& bm, SkPMColor expect32,
|
|
|
|
uint16_t expect16, uint8_t expect8,
|
|
|
|
skiatest::Reporter* reporter) {
|
|
|
|
uint32_t expect;
|
|
|
|
Proc proc = find_proc(bm, expect32, expect16, expect8, &expect);
|
|
|
|
for (int y = 0; y < bm.height(); y++) {
|
|
|
|
uint32_t bad;
|
|
|
|
int x = proc(bm.getAddr(0, y), bm.width(), expect, &bad);
|
|
|
|
if (x >= 0) {
|
2014-03-05 13:43:15 +00:00
|
|
|
ERRORF(reporter, "BlitRow colortype=%s [%d %d] expected %x got %x",
|
|
|
|
gColorTypeName[bm.colorType()], x, y, expect, bad);
|
2010-03-08 20:23:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-03-15 20:48:02 +00:00
|
|
|
// Make sure our blits always map src==0 to a noop, and src==FF to full opaque
|
|
|
|
static void test_00_FF(skiatest::Reporter* reporter) {
|
2010-03-08 20:23:57 +00:00
|
|
|
static const int W = 256;
|
|
|
|
|
2014-02-13 22:00:04 +00:00
|
|
|
static const SkColorType gDstColorType[] = {
|
2014-04-11 17:15:40 +00:00
|
|
|
kN32_SkColorType,
|
2014-02-13 22:00:04 +00:00
|
|
|
kRGB_565_SkColorType,
|
2010-03-08 20:23:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct {
|
|
|
|
SkColor fSrc;
|
|
|
|
SkColor fDst;
|
|
|
|
SkPMColor fResult32;
|
|
|
|
uint16_t fResult16;
|
|
|
|
uint8_t fResult8;
|
|
|
|
} gSrcRec[] = {
|
|
|
|
{ 0, 0, 0, 0, 0 },
|
|
|
|
{ 0, 0xFFFFFFFF, SkPackARGB32(0xFF, 0xFF, 0xFF, 0xFF), 0xFFFF, 0xFF },
|
|
|
|
{ 0xFFFFFFFF, 0, SkPackARGB32(0xFF, 0xFF, 0xFF, 0xFF), 0xFFFF, 0xFF },
|
|
|
|
{ 0xFFFFFFFF, 0xFFFFFFFF, SkPackARGB32(0xFF, 0xFF, 0xFF, 0xFF), 0xFFFF, 0xFF },
|
|
|
|
};
|
2010-03-08 22:00:55 +00:00
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
|
2010-03-08 20:23:57 +00:00
|
|
|
SkBitmap srcBM;
|
2014-02-13 22:00:04 +00:00
|
|
|
srcBM.allocN32Pixels(W, 1);
|
2010-03-15 20:48:02 +00:00
|
|
|
|
2014-02-13 22:00:04 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gDstColorType); i++) {
|
|
|
|
SkImageInfo info = SkImageInfo::Make(W, 1, gDstColorType[i],
|
|
|
|
kPremul_SkAlphaType);
|
2010-03-08 20:23:57 +00:00
|
|
|
SkBitmap dstBM;
|
2014-02-13 22:00:04 +00:00
|
|
|
dstBM.allocPixels(info);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2010-03-08 20:23:57 +00:00
|
|
|
SkCanvas canvas(dstBM);
|
|
|
|
for (size_t j = 0; j < SK_ARRAY_COUNT(gSrcRec); j++) {
|
|
|
|
srcBM.eraseColor(gSrcRec[j].fSrc);
|
|
|
|
dstBM.eraseColor(gSrcRec[j].fDst);
|
|
|
|
|
2010-03-08 22:00:55 +00:00
|
|
|
for (int k = 0; k < 4; k++) {
|
|
|
|
bool dither = (k & 1) != 0;
|
|
|
|
bool blend = (k & 2) != 0;
|
|
|
|
if (gSrcRec[j].fSrc != 0 && blend) {
|
|
|
|
// can't make a numerical promise about blending anything
|
|
|
|
// but 0
|
2010-03-09 15:21:28 +00:00
|
|
|
// continue;
|
2010-03-08 22:00:55 +00:00
|
|
|
}
|
|
|
|
paint.setDither(dither);
|
|
|
|
paint.setAlpha(blend ? 0x80 : 0xFF);
|
|
|
|
canvas.drawBitmap(srcBM, 0, 0, &paint);
|
|
|
|
if (!check_color(dstBM, gSrcRec[j].fResult32, gSrcRec[j].fResult16,
|
|
|
|
gSrcRec[j].fResult8, reporter)) {
|
|
|
|
SkDebugf("--- src index %d dither %d blend %d\n", j, dither, blend);
|
|
|
|
}
|
2010-03-08 20:23:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-15 20:48:02 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
struct Mesh {
|
|
|
|
SkPoint fPts[4];
|
|
|
|
|
|
|
|
Mesh(const SkBitmap& bm, SkPaint* paint) {
|
|
|
|
const SkScalar w = SkIntToScalar(bm.width());
|
|
|
|
const SkScalar h = SkIntToScalar(bm.height());
|
|
|
|
fPts[0].set(0, 0);
|
|
|
|
fPts[1].set(w, 0);
|
|
|
|
fPts[2].set(w, h);
|
|
|
|
fPts[3].set(0, h);
|
2016-03-13 21:13:58 +00:00
|
|
|
paint->setShader(SkShader::MakeBitmapShader(bm, SkShader::kClamp_TileMode,
|
|
|
|
SkShader::kClamp_TileMode));
|
2010-03-15 20:48:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void draw(SkCanvas* canvas, SkPaint* paint) {
|
2017-04-03 15:11:09 +00:00
|
|
|
canvas->drawVertices(SkVertices::MakeCopy(SkVertices::kTriangleFan_VertexMode, 4, fPts,
|
|
|
|
fPts, nullptr),
|
|
|
|
SkBlendMode::kModulate, *paint);
|
2010-03-15 20:48:02 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#include "SkImageEncoder.h"
|
|
|
|
static void save_bm(const SkBitmap& bm, const char name[]) {
|
2016-11-23 15:55:18 +00:00
|
|
|
sk_tool_utils::EncodeImageToFile(name, bm, SkEncodedImageFormat::kPNG, 100);
|
2010-03-15 20:48:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool gOnce;
|
|
|
|
|
|
|
|
// Make sure our blits are invariant with the width of the blit (i.e. that
|
|
|
|
// special case for 8 at a time have the same results as narrower blits)
|
|
|
|
static void test_diagonal(skiatest::Reporter* reporter) {
|
|
|
|
static const int W = 64;
|
|
|
|
static const int H = W;
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2014-02-13 22:00:04 +00:00
|
|
|
static const SkColorType gDstColorType[] = {
|
2014-04-11 17:15:40 +00:00
|
|
|
kN32_SkColorType,
|
2014-02-13 22:00:04 +00:00
|
|
|
kRGB_565_SkColorType,
|
2010-03-15 20:48:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const SkColor gDstBG[] = { 0, 0xFFFFFFFF };
|
2016-08-02 13:12:06 +00:00
|
|
|
const SkRect srcR = SkRect::MakeIWH(W, H);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2010-03-15 20:48:02 +00:00
|
|
|
SkBitmap srcBM;
|
2014-02-13 22:00:04 +00:00
|
|
|
srcBM.allocN32Pixels(W, H);
|
2016-08-02 13:12:06 +00:00
|
|
|
SkImageInfo info = SkImageInfo::Make(W, H, kUnknown_SkColorType, kPremul_SkAlphaType);
|
2014-02-13 22:00:04 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gDstColorType); i++) {
|
2014-09-03 18:54:58 +00:00
|
|
|
info = info.makeColorType(gDstColorType[i]);
|
2014-02-13 22:00:04 +00:00
|
|
|
|
2010-03-15 20:48:02 +00:00
|
|
|
SkBitmap dstBM0, dstBM1;
|
2014-02-13 22:00:04 +00:00
|
|
|
dstBM0.allocPixels(info);
|
|
|
|
dstBM1.allocPixels(info);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2010-03-15 20:48:02 +00:00
|
|
|
SkCanvas canvas0(dstBM0);
|
|
|
|
SkCanvas canvas1(dstBM1);
|
|
|
|
SkColor bgColor;
|
|
|
|
|
|
|
|
for (size_t j = 0; j < SK_ARRAY_COUNT(gDstBG); j++) {
|
|
|
|
bgColor = gDstBG[j];
|
|
|
|
|
|
|
|
for (int c = 0; c <= 0xFF; c++) {
|
2016-08-02 13:12:06 +00:00
|
|
|
// cons up a mesh to draw the bitmap with
|
|
|
|
SkPaint paint;
|
2010-03-15 20:48:02 +00:00
|
|
|
srcBM.eraseARGB(0xFF, c, c, c);
|
2016-08-02 13:12:06 +00:00
|
|
|
Mesh mesh(srcBM, &paint);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2010-03-15 20:48:02 +00:00
|
|
|
for (int k = 0; k < 4; k++) {
|
|
|
|
bool dither = (k & 1) != 0;
|
|
|
|
uint8_t alpha = (k & 2) ? 0x80 : 0xFF;
|
|
|
|
paint.setDither(dither);
|
|
|
|
paint.setAlpha(alpha);
|
|
|
|
|
|
|
|
dstBM0.eraseColor(bgColor);
|
|
|
|
dstBM1.eraseColor(bgColor);
|
|
|
|
|
|
|
|
canvas0.drawRect(srcR, paint);
|
|
|
|
mesh.draw(&canvas1, &paint);
|
|
|
|
|
|
|
|
if (!gOnce && false) {
|
|
|
|
save_bm(dstBM0, "drawBitmap.png");
|
|
|
|
save_bm(dstBM1, "drawMesh.png");
|
|
|
|
gOnce = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (memcmp(dstBM0.getPixels(), dstBM1.getPixels(), dstBM0.getSize())) {
|
2014-02-13 22:00:04 +00:00
|
|
|
ERRORF(reporter, "Diagonal colortype=%s bg=0x%x dither=%d"
|
2014-01-10 14:58:10 +00:00
|
|
|
" alpha=0x%x src=0x%x",
|
2014-03-05 13:43:15 +00:00
|
|
|
gColorTypeName[gDstColorType[i]], bgColor, dither,
|
2014-01-10 14:58:10 +00:00
|
|
|
alpha, c);
|
2010-03-15 20:48:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-12 21:11:12 +00:00
|
|
|
DEF_TEST(BlitRow, reporter) {
|
2010-03-15 20:48:02 +00:00
|
|
|
test_00_FF(reporter);
|
|
|
|
test_diagonal(reporter);
|
|
|
|
}
|