2013-12-13 18:29:51 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SkBitmap.h"
|
2018-03-09 19:41:36 +00:00
|
|
|
#include "SkColor.h"
|
|
|
|
#include "SkImageInfo.h"
|
2015-01-23 15:51:14 +00:00
|
|
|
#include "SkMallocPixelRef.h"
|
2018-03-09 19:41:36 +00:00
|
|
|
#include "SkPixelRef.h"
|
|
|
|
#include "SkPixmap.h"
|
2016-12-07 21:21:53 +00:00
|
|
|
#include "SkRandom.h"
|
2018-03-09 19:41:36 +00:00
|
|
|
#include "SkRefCnt.h"
|
|
|
|
#include "SkTypes.h"
|
2013-12-13 18:29:51 +00:00
|
|
|
#include "Test.h"
|
2017-04-28 15:15:22 +00:00
|
|
|
#include "sk_tool_utils.h"
|
2013-12-13 18:29:51 +00:00
|
|
|
|
2015-06-05 13:58:22 +00:00
|
|
|
static void test_peekpixels(skiatest::Reporter* reporter) {
|
|
|
|
const SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
|
|
|
|
|
|
|
|
SkPixmap pmap;
|
|
|
|
SkBitmap bm;
|
|
|
|
|
|
|
|
// empty should return false
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, !bm.peekPixels(nullptr));
|
2015-06-05 13:58:22 +00:00
|
|
|
REPORTER_ASSERT(reporter, !bm.peekPixels(&pmap));
|
|
|
|
|
|
|
|
// no pixels should return false
|
|
|
|
bm.setInfo(SkImageInfo::MakeN32Premul(10, 10));
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, !bm.peekPixels(nullptr));
|
2015-06-05 13:58:22 +00:00
|
|
|
REPORTER_ASSERT(reporter, !bm.peekPixels(&pmap));
|
|
|
|
|
|
|
|
// real pixels should return true
|
|
|
|
bm.allocPixels(info);
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, bm.peekPixels(nullptr));
|
2015-06-05 13:58:22 +00:00
|
|
|
REPORTER_ASSERT(reporter, bm.peekPixels(&pmap));
|
|
|
|
REPORTER_ASSERT(reporter, pmap.info() == bm.info());
|
|
|
|
REPORTER_ASSERT(reporter, pmap.addr() == bm.getPixels());
|
|
|
|
REPORTER_ASSERT(reporter, pmap.rowBytes() == bm.rowBytes());
|
|
|
|
}
|
|
|
|
|
2015-01-23 15:51:14 +00:00
|
|
|
// https://code.google.com/p/chromium/issues/detail?id=446164
|
|
|
|
static void test_bigalloc(skiatest::Reporter* reporter) {
|
|
|
|
const int width = 0x40000001;
|
|
|
|
const int height = 0x00000096;
|
|
|
|
const SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
|
|
|
|
|
|
|
|
SkBitmap bm;
|
|
|
|
REPORTER_ASSERT(reporter, !bm.tryAllocPixels(info));
|
|
|
|
|
2017-07-18 14:53:11 +00:00
|
|
|
sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeAllocate(info, info.minRowBytes());
|
2015-01-23 15:51:14 +00:00
|
|
|
REPORTER_ASSERT(reporter, !pr);
|
|
|
|
}
|
|
|
|
|
2014-07-01 19:48:11 +00:00
|
|
|
static void test_allocpixels(skiatest::Reporter* reporter) {
|
|
|
|
const int width = 10;
|
|
|
|
const int height = 10;
|
|
|
|
const SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
|
|
|
|
const size_t explicitRowBytes = info.minRowBytes() + 24;
|
|
|
|
|
|
|
|
SkBitmap bm;
|
|
|
|
bm.setInfo(info);
|
|
|
|
REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes());
|
|
|
|
bm.allocPixels();
|
|
|
|
REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes());
|
|
|
|
bm.reset();
|
|
|
|
bm.allocPixels(info);
|
|
|
|
REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes());
|
|
|
|
|
|
|
|
bm.setInfo(info, explicitRowBytes);
|
|
|
|
REPORTER_ASSERT(reporter, explicitRowBytes == bm.rowBytes());
|
|
|
|
bm.allocPixels();
|
|
|
|
REPORTER_ASSERT(reporter, explicitRowBytes == bm.rowBytes());
|
|
|
|
bm.reset();
|
|
|
|
bm.allocPixels(info, explicitRowBytes);
|
|
|
|
REPORTER_ASSERT(reporter, explicitRowBytes == bm.rowBytes());
|
|
|
|
|
|
|
|
bm.reset();
|
|
|
|
bm.setInfo(info, 0);
|
|
|
|
REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes());
|
|
|
|
bm.reset();
|
|
|
|
bm.allocPixels(info, 0);
|
|
|
|
REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes());
|
|
|
|
|
|
|
|
bm.reset();
|
|
|
|
bool success = bm.setInfo(info, info.minRowBytes() - 1); // invalid for 32bit
|
|
|
|
REPORTER_ASSERT(reporter, !success);
|
|
|
|
REPORTER_ASSERT(reporter, bm.isNull());
|
|
|
|
}
|
|
|
|
|
2013-12-30 19:21:22 +00:00
|
|
|
static void test_bigwidth(skiatest::Reporter* reporter) {
|
|
|
|
SkBitmap bm;
|
|
|
|
int width = 1 << 29; // *4 will be the high-bit of 32bit int
|
|
|
|
|
2014-05-30 13:26:10 +00:00
|
|
|
SkImageInfo info = SkImageInfo::MakeA8(width, 1);
|
|
|
|
REPORTER_ASSERT(reporter, bm.setInfo(info));
|
2014-09-03 18:54:58 +00:00
|
|
|
REPORTER_ASSERT(reporter, bm.setInfo(info.makeColorType(kRGB_565_SkColorType)));
|
2013-12-31 07:01:36 +00:00
|
|
|
|
2013-12-30 19:21:22 +00:00
|
|
|
// for a 4-byte config, this width will compute a rowbytes of 0x80000000,
|
|
|
|
// which does not fit in a int32_t. setConfig should detect this, and fail.
|
|
|
|
|
|
|
|
// TODO: perhaps skia can relax this, and only require that rowBytes fit
|
|
|
|
// in a uint32_t (or larger), but for now this is the constraint.
|
|
|
|
|
2014-09-03 18:54:58 +00:00
|
|
|
REPORTER_ASSERT(reporter, !bm.setInfo(info.makeColorType(kN32_SkColorType)));
|
2013-12-30 19:21:22 +00:00
|
|
|
}
|
|
|
|
|
2013-12-13 18:29:51 +00:00
|
|
|
/**
|
|
|
|
* This test contains basic sanity checks concerning bitmaps.
|
|
|
|
*/
|
|
|
|
DEF_TEST(Bitmap, reporter) {
|
|
|
|
// Zero-sized bitmaps are allowed
|
|
|
|
for (int width = 0; width < 2; ++width) {
|
|
|
|
for (int height = 0; height < 2; ++height) {
|
|
|
|
SkBitmap bm;
|
2014-05-30 13:26:10 +00:00
|
|
|
bool setConf = bm.setInfo(SkImageInfo::MakeN32Premul(width, height));
|
2013-12-13 18:29:51 +00:00
|
|
|
REPORTER_ASSERT(reporter, setConf);
|
|
|
|
if (setConf) {
|
2014-09-02 19:50:45 +00:00
|
|
|
bm.allocPixels();
|
2013-12-13 18:29:51 +00:00
|
|
|
}
|
2013-12-13 19:25:21 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkToBool(width & height) != bm.empty());
|
2013-12-13 18:29:51 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-31 07:01:36 +00:00
|
|
|
|
2013-12-30 19:21:22 +00:00
|
|
|
test_bigwidth(reporter);
|
2014-07-01 19:48:11 +00:00
|
|
|
test_allocpixels(reporter);
|
2015-01-23 15:51:14 +00:00
|
|
|
test_bigalloc(reporter);
|
2015-06-05 13:58:22 +00:00
|
|
|
test_peekpixels(reporter);
|
2013-12-13 18:29:51 +00:00
|
|
|
}
|
2015-12-09 18:21:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This test checks that getColor works for both swizzles.
|
|
|
|
*/
|
|
|
|
DEF_TEST(Bitmap_getColor_Swizzle, r) {
|
|
|
|
SkBitmap source;
|
|
|
|
source.allocN32Pixels(1,1);
|
|
|
|
source.eraseColor(SK_ColorRED);
|
|
|
|
SkColorType colorTypes[] = {
|
|
|
|
kRGBA_8888_SkColorType,
|
|
|
|
kBGRA_8888_SkColorType,
|
|
|
|
};
|
|
|
|
for (SkColorType ct : colorTypes) {
|
|
|
|
SkBitmap copy;
|
2017-04-28 15:15:22 +00:00
|
|
|
if (!sk_tool_utils::copy_to(©, ct, source)) {
|
2015-12-09 18:21:59 +00:00
|
|
|
ERRORF(r, "SkBitmap::copy failed %d", (int)ct);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(r, source.getColor(0, 0) == copy.getColor(0, 0));
|
|
|
|
}
|
|
|
|
}
|
2015-12-11 22:08:58 +00:00
|
|
|
|
|
|
|
static void test_erasecolor_premul(skiatest::Reporter* reporter, SkColorType ct, SkColor input,
|
|
|
|
SkColor expected) {
|
|
|
|
SkBitmap bm;
|
|
|
|
bm.allocPixels(SkImageInfo::Make(1, 1, ct, kPremul_SkAlphaType));
|
|
|
|
bm.eraseColor(input);
|
2016-02-25 01:59:16 +00:00
|
|
|
INFOF(reporter, "expected: %x actual: %x\n", expected, bm.getColor(0, 0));
|
2015-12-11 22:08:58 +00:00
|
|
|
REPORTER_ASSERT(reporter, bm.getColor(0, 0) == expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This test checks that eraseColor premultiplies the color correctly.
|
|
|
|
*/
|
|
|
|
DEF_TEST(Bitmap_eraseColor_Premul, r) {
|
|
|
|
SkColor color = 0x80FF0080;
|
|
|
|
test_erasecolor_premul(r, kAlpha_8_SkColorType, color, 0x80000000);
|
|
|
|
test_erasecolor_premul(r, kRGB_565_SkColorType, color, 0xFF840042);
|
|
|
|
test_erasecolor_premul(r, kARGB_4444_SkColorType, color, 0x88FF0080);
|
|
|
|
test_erasecolor_premul(r, kRGBA_8888_SkColorType, color, color);
|
|
|
|
test_erasecolor_premul(r, kBGRA_8888_SkColorType, color, color);
|
|
|
|
}
|
2016-12-07 19:59:27 +00:00
|
|
|
|
|
|
|
// Test that SkBitmap::ComputeOpaque() is correct for various colortypes.
|
|
|
|
DEF_TEST(Bitmap_compute_is_opaque, r) {
|
|
|
|
struct {
|
|
|
|
SkColorType fCT;
|
|
|
|
SkAlphaType fAT;
|
|
|
|
} types[] = {
|
|
|
|
{ kGray_8_SkColorType, kOpaque_SkAlphaType },
|
|
|
|
{ kAlpha_8_SkColorType, kPremul_SkAlphaType },
|
|
|
|
{ kARGB_4444_SkColorType, kPremul_SkAlphaType },
|
|
|
|
{ kRGB_565_SkColorType, kOpaque_SkAlphaType },
|
|
|
|
{ kBGRA_8888_SkColorType, kPremul_SkAlphaType },
|
|
|
|
{ kRGBA_8888_SkColorType, kPremul_SkAlphaType },
|
|
|
|
{ kRGBA_F16_SkColorType, kPremul_SkAlphaType },
|
|
|
|
};
|
|
|
|
for (auto type : types) {
|
|
|
|
SkBitmap bm;
|
|
|
|
REPORTER_ASSERT(r, !SkBitmap::ComputeIsOpaque(bm));
|
|
|
|
|
|
|
|
bm.allocPixels(SkImageInfo::Make(13, 17, type.fCT, type.fAT));
|
|
|
|
bm.eraseColor(SkColorSetARGB(255, 10, 20, 30));
|
|
|
|
REPORTER_ASSERT(r, SkBitmap::ComputeIsOpaque(bm));
|
|
|
|
|
|
|
|
bm.eraseColor(SkColorSetARGB(128, 255, 255, 255));
|
|
|
|
bool isOpaque = SkBitmap::ComputeIsOpaque(bm);
|
|
|
|
bool shouldBeOpaque = (type.fAT == kOpaque_SkAlphaType);
|
|
|
|
REPORTER_ASSERT(r, isOpaque == shouldBeOpaque);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that erase+getColor round trips with RGBA_F16 pixels.
|
|
|
|
DEF_TEST(Bitmap_erase_f16_erase_getColor, r) {
|
|
|
|
SkRandom random;
|
|
|
|
SkPixmap pm;
|
|
|
|
SkBitmap bm;
|
|
|
|
bm.allocPixels(SkImageInfo::Make(1, 1, kRGBA_F16_SkColorType, kPremul_SkAlphaType));
|
|
|
|
REPORTER_ASSERT(r, bm.peekPixels(&pm));
|
|
|
|
for (unsigned i = 0; i < 0x100; ++i) {
|
|
|
|
// Test all possible values of blue component.
|
|
|
|
SkColor color1 = (SkColor)((random.nextU() & 0xFFFFFF00) | i);
|
|
|
|
// Test all possible values of alpha component.
|
|
|
|
SkColor color2 = (SkColor)((random.nextU() & 0x00FFFFFF) | (i << 24));
|
|
|
|
for (SkColor color : {color1, color2}) {
|
|
|
|
pm.erase(color);
|
|
|
|
if (SkColorGetA(color) != 0) {
|
|
|
|
REPORTER_ASSERT(r, color == pm.getColor(0, 0));
|
|
|
|
} else {
|
|
|
|
REPORTER_ASSERT(r, 0 == SkColorGetA(pm.getColor(0, 0)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-12 16:09:54 +00:00
|
|
|
// Make sure that the bitmap remains valid when pixelref is removed.
|
|
|
|
DEF_TEST(Bitmap_clear_pixelref_keep_info, r) {
|
|
|
|
SkBitmap bm;
|
|
|
|
bm.allocPixels(SkImageInfo::MakeN32Premul(100,100));
|
|
|
|
bm.setPixelRef(nullptr, 0, 0);
|
|
|
|
SkDEBUGCODE(bm.validate();)
|
|
|
|
}
|
|
|
|
|
2018-03-06 13:43:22 +00:00
|
|
|
// At the time of writing, SkBitmap::erase() works when the color is zero for all formats,
|
|
|
|
// but some formats failed when the color is non-zero!
|
|
|
|
DEF_TEST(Bitmap_erase, r) {
|
|
|
|
SkColorType colorTypes[] = {
|
|
|
|
kRGB_565_SkColorType,
|
|
|
|
kARGB_4444_SkColorType,
|
|
|
|
kRGB_888x_SkColorType,
|
|
|
|
kRGBA_8888_SkColorType,
|
|
|
|
kBGRA_8888_SkColorType,
|
|
|
|
kRGB_101010x_SkColorType,
|
|
|
|
kRGBA_1010102_SkColorType,
|
|
|
|
};
|
|
|
|
|
|
|
|
for (SkColorType ct : colorTypes) {
|
|
|
|
SkImageInfo info = SkImageInfo::Make(1,1, (SkColorType)ct, kPremul_SkAlphaType);
|
|
|
|
|
|
|
|
SkBitmap bm;
|
|
|
|
bm.allocPixels(info);
|
|
|
|
|
|
|
|
bm.eraseColor(0x00000000);
|
|
|
|
if (SkColorTypeIsAlwaysOpaque(ct)) {
|
|
|
|
REPORTER_ASSERT(r, bm.getColor(0,0) == 0xff000000);
|
|
|
|
} else {
|
|
|
|
REPORTER_ASSERT(r, bm.getColor(0,0) == 0x00000000);
|
|
|
|
}
|
|
|
|
|
|
|
|
bm.eraseColor(0xaabbccdd);
|
|
|
|
REPORTER_ASSERT(r, bm.getColor(0,0) != 0xff000000);
|
|
|
|
REPORTER_ASSERT(r, bm.getColor(0,0) != 0x00000000);
|
|
|
|
}
|
|
|
|
}
|