2013-10-29 19:55:00 +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"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkData.h"
|
2013-12-05 18:31:42 +00:00
|
|
|
#include "SkDiscardableMemoryPool.h"
|
2016-03-01 20:12:27 +00:00
|
|
|
#include "SkImage.h"
|
|
|
|
#include "SkImageEncoder.h"
|
2014-05-27 14:14:22 +00:00
|
|
|
#include "SkImageGeneratorPriv.h"
|
2014-08-28 20:35:23 +00:00
|
|
|
#include "SkResourceCache.h"
|
2013-10-29 19:55:00 +00:00
|
|
|
#include "SkStream.h"
|
2013-12-05 18:31:42 +00:00
|
|
|
#include "SkUtils.h"
|
2013-11-06 10:08:30 +00:00
|
|
|
|
2013-10-29 19:55:00 +00:00
|
|
|
#include "Test.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fill this bitmap with some color.
|
|
|
|
*/
|
|
|
|
static void make_test_image(SkBitmap* bm) {
|
2014-06-10 02:52:07 +00:00
|
|
|
const int W = 50, H = 50;
|
|
|
|
bm->allocN32Pixels(W, H);
|
2013-10-29 19:55:00 +00:00
|
|
|
bm->eraseColor(SK_ColorBLACK);
|
|
|
|
SkCanvas canvas(*bm);
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorBLUE);
|
|
|
|
canvas.drawRectCoords(0, 0, SkIntToScalar(W/2),
|
|
|
|
SkIntToScalar(H/2), paint);
|
|
|
|
paint.setColor(SK_ColorWHITE);
|
|
|
|
canvas.drawRectCoords(SkIntToScalar(W/2), SkIntToScalar(H/2),
|
|
|
|
SkIntToScalar(W), SkIntToScalar(H), paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* encode this bitmap into some data via SkImageEncoder
|
|
|
|
*/
|
|
|
|
static SkData* create_data_from_bitmap(const SkBitmap& bm,
|
|
|
|
SkImageEncoder::Type type) {
|
|
|
|
SkDynamicMemoryWStream stream;
|
|
|
|
if (SkImageEncoder::EncodeStream(&stream, bm, type, 100)) {
|
|
|
|
return stream.copyToData();
|
|
|
|
}
|
2015-08-27 14:41:13 +00:00
|
|
|
return nullptr;
|
2013-10-29 19:55:00 +00:00
|
|
|
}
|
|
|
|
|
2013-12-05 18:31:42 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2013-10-29 19:55:00 +00:00
|
|
|
|
|
|
|
static void compare_bitmaps(skiatest::Reporter* reporter,
|
|
|
|
const SkBitmap& b1, const SkBitmap& b2,
|
|
|
|
bool pixelPerfect = true) {
|
|
|
|
REPORTER_ASSERT(reporter, b1.empty() == b2.empty());
|
|
|
|
REPORTER_ASSERT(reporter, b1.width() == b2.width());
|
|
|
|
REPORTER_ASSERT(reporter, b1.height() == b2.height());
|
|
|
|
REPORTER_ASSERT(reporter, b1.isNull() == b2.isNull());
|
|
|
|
SkAutoLockPixels autoLockPixels1(b1);
|
|
|
|
SkAutoLockPixels autoLockPixels2(b2);
|
|
|
|
REPORTER_ASSERT(reporter, b1.isNull() == b2.isNull());
|
|
|
|
if (b1.isNull() || b1.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
2014-09-05 20:34:00 +00:00
|
|
|
REPORTER_ASSERT(reporter, b1.getPixels());
|
|
|
|
REPORTER_ASSERT(reporter, b2.getPixels());
|
2013-10-29 19:55:00 +00:00
|
|
|
if ((!(b1.getPixels())) || (!(b2.getPixels()))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ((b1.width() != b2.width()) ||
|
|
|
|
(b1.height() != b2.height())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!pixelPerfect) {
|
|
|
|
return;
|
|
|
|
}
|
2013-11-06 10:08:30 +00:00
|
|
|
|
2013-10-29 19:55:00 +00:00
|
|
|
int pixelErrors = 0;
|
|
|
|
for (int y = 0; y < b2.height(); ++y) {
|
|
|
|
for (int x = 0; x < b2.width(); ++x) {
|
|
|
|
if (b1.getColor(x, y) != b2.getColor(x, y)) {
|
|
|
|
++pixelErrors;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(reporter, 0 == pixelErrors);
|
|
|
|
}
|
|
|
|
|
2013-12-05 14:00:03 +00:00
|
|
|
typedef bool (*InstallEncoded)(SkData* encoded, SkBitmap* dst);
|
|
|
|
|
2013-10-29 19:55:00 +00:00
|
|
|
/**
|
2013-12-05 14:00:03 +00:00
|
|
|
This function tests three differently encoded images against the
|
|
|
|
original bitmap */
|
2013-11-06 10:08:30 +00:00
|
|
|
static void test_three_encodings(skiatest::Reporter* reporter,
|
2013-12-05 14:00:03 +00:00
|
|
|
InstallEncoded install) {
|
2013-10-29 19:55:00 +00:00
|
|
|
SkBitmap original;
|
|
|
|
make_test_image(&original);
|
2013-11-06 10:08:30 +00:00
|
|
|
REPORTER_ASSERT(reporter, !original.empty());
|
|
|
|
REPORTER_ASSERT(reporter, !original.isNull());
|
|
|
|
if (original.empty() || original.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
2013-10-29 19:55:00 +00:00
|
|
|
static const SkImageEncoder::Type types[] = {
|
|
|
|
SkImageEncoder::kPNG_Type,
|
|
|
|
SkImageEncoder::kJPEG_Type,
|
|
|
|
SkImageEncoder::kWEBP_Type
|
|
|
|
};
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(types); i++) {
|
|
|
|
SkImageEncoder::Type type = types[i];
|
2016-08-02 21:40:46 +00:00
|
|
|
sk_sp<SkData> encoded(create_data_from_bitmap(original, type));
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, encoded.get() != nullptr);
|
|
|
|
if (nullptr == encoded.get()) {
|
2013-12-05 14:00:03 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
SkBitmap lazy;
|
|
|
|
bool installSuccess = install(encoded.get(), &lazy);
|
|
|
|
REPORTER_ASSERT(reporter, installSuccess);
|
|
|
|
if (!installSuccess) {
|
|
|
|
continue;
|
2013-10-29 19:55:00 +00:00
|
|
|
}
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, nullptr == lazy.getPixels());
|
2013-12-05 14:00:03 +00:00
|
|
|
{
|
|
|
|
SkAutoLockPixels autoLockPixels(lazy); // now pixels are good.
|
2014-09-05 20:34:00 +00:00
|
|
|
REPORTER_ASSERT(reporter, lazy.getPixels());
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == lazy.getPixels()) {
|
2013-12-05 14:00:03 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// pixels should be gone!
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, nullptr == lazy.getPixels());
|
2013-12-05 14:00:03 +00:00
|
|
|
{
|
|
|
|
SkAutoLockPixels autoLockPixels(lazy); // now pixels are good.
|
2014-09-05 20:34:00 +00:00
|
|
|
REPORTER_ASSERT(reporter, lazy.getPixels());
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == lazy.getPixels()) {
|
2013-12-05 14:00:03 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bool comparePixels = (SkImageEncoder::kPNG_Type == type);
|
|
|
|
compare_bitmaps(reporter, original, lazy, comparePixels);
|
2013-11-06 10:08:30 +00:00
|
|
|
}
|
|
|
|
}
|
2013-10-29 19:55:00 +00:00
|
|
|
|
2013-12-05 14:00:03 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2013-12-05 18:31:42 +00:00
|
|
|
static bool install_skDiscardablePixelRef(SkData* encoded, SkBitmap* dst) {
|
|
|
|
// Use system-default discardable memory.
|
2015-09-25 13:56:57 +00:00
|
|
|
return SkDEPRECATED_InstallDiscardablePixelRef(encoded, dst);
|
2013-11-06 10:08:30 +00:00
|
|
|
}
|
2013-10-29 19:55:00 +00:00
|
|
|
|
2013-12-05 14:00:03 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2013-11-06 10:08:30 +00:00
|
|
|
/**
|
2015-09-24 07:21:20 +00:00
|
|
|
* This checks to see that SkDiscardablePixelRef works as advertised with a
|
2013-12-05 18:31:42 +00:00
|
|
|
* SkDecodingImageGenerator.
|
2013-11-06 10:08:30 +00:00
|
|
|
*/
|
2013-12-05 18:31:42 +00:00
|
|
|
DEF_TEST(DecodingImageGenerator, reporter) {
|
|
|
|
test_three_encodings(reporter, install_skDiscardablePixelRef);
|
2013-10-29 19:55:00 +00:00
|
|
|
}
|
2013-11-21 15:32:08 +00:00
|
|
|
|
2013-12-05 18:31:42 +00:00
|
|
|
class TestImageGenerator : public SkImageGenerator {
|
|
|
|
public:
|
|
|
|
enum TestType {
|
|
|
|
kFailGetPixels_TestType,
|
|
|
|
kSucceedGetPixels_TestType,
|
|
|
|
kLast_TestType = kSucceedGetPixels_TestType
|
|
|
|
};
|
|
|
|
static int Width() { return 10; }
|
|
|
|
static int Height() { return 10; }
|
2015-11-10 12:55:15 +00:00
|
|
|
// value choosen so that there is no loss when converting to to RGB565 and back
|
|
|
|
static SkColor Color() { return 0xff10345a; }
|
|
|
|
static SkPMColor PMColor() { return SkPreMultiplyColor(Color()); }
|
|
|
|
|
|
|
|
TestImageGenerator(TestType type, skiatest::Reporter* reporter,
|
|
|
|
SkColorType colorType = kN32_SkColorType)
|
|
|
|
: INHERITED(GetMyInfo(colorType)), fType(type), fReporter(reporter) {
|
2013-12-05 18:31:42 +00:00
|
|
|
SkASSERT((fType <= kLast_TestType) && (fType >= 0));
|
|
|
|
}
|
2014-01-21 23:39:22 +00:00
|
|
|
virtual ~TestImageGenerator() { }
|
2014-05-29 15:57:20 +00:00
|
|
|
|
|
|
|
protected:
|
2015-11-10 12:55:15 +00:00
|
|
|
static SkImageInfo GetMyInfo(SkColorType colorType) {
|
|
|
|
return SkImageInfo::Make(TestImageGenerator::Width(), TestImageGenerator::Height(),
|
|
|
|
colorType, kOpaque_SkAlphaType);
|
2015-03-19 15:31:14 +00:00
|
|
|
}
|
|
|
|
|
2015-07-09 16:08:00 +00:00
|
|
|
bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
|
|
|
|
SkPMColor ctable[], int* ctableCount) override {
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(fReporter, pixels != nullptr);
|
2015-02-13 19:13:34 +00:00
|
|
|
REPORTER_ASSERT(fReporter, rowBytes >= info.minRowBytes());
|
|
|
|
if (fType != kSucceedGetPixels_TestType) {
|
2015-07-09 16:08:00 +00:00
|
|
|
return false;
|
2015-02-13 19:13:34 +00:00
|
|
|
}
|
2015-11-10 12:55:15 +00:00
|
|
|
if (info.colorType() != kN32_SkColorType && info.colorType() != getInfo().colorType()) {
|
2015-07-09 16:08:00 +00:00
|
|
|
return false;
|
2013-12-05 18:31:42 +00:00
|
|
|
}
|
|
|
|
char* bytePtr = static_cast<char*>(pixels);
|
2015-11-10 12:55:15 +00:00
|
|
|
switch (info.colorType()) {
|
|
|
|
case kN32_SkColorType:
|
|
|
|
for (int y = 0; y < info.height(); ++y) {
|
|
|
|
sk_memset32((uint32_t*)bytePtr,
|
|
|
|
TestImageGenerator::PMColor(), info.width());
|
|
|
|
bytePtr += rowBytes;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kIndex_8_SkColorType:
|
|
|
|
*ctableCount = 1;
|
|
|
|
ctable[0] = TestImageGenerator::PMColor();
|
|
|
|
for (int y = 0; y < info.height(); ++y) {
|
|
|
|
memset(bytePtr, 0, info.width());
|
|
|
|
bytePtr += rowBytes;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kRGB_565_SkColorType:
|
|
|
|
for (int y = 0; y < info.height(); ++y) {
|
|
|
|
sk_memset16((uint16_t*)bytePtr,
|
|
|
|
SkPixel32ToPixel16(TestImageGenerator::PMColor()), info.width());
|
|
|
|
bytePtr += rowBytes;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
2013-12-05 18:31:42 +00:00
|
|
|
}
|
2015-07-09 16:08:00 +00:00
|
|
|
return true;
|
2013-12-05 18:31:42 +00:00
|
|
|
}
|
Add Options to SkDecodingImageGenerator, simplify API.
Motivation: We want to remove redundant classes from Skia. To
that end we want to remove SkImageRef and its subclasses and
replace their uses with SkDiscardablePixelRef +
SkDecodingImageGenerator. Since Android uses SkImageRef, we need
to make sure that SkDecodingImageGenerator allows all of the
settings that Android exposes in BitmapFactory.Options.
To that end, we have created an Options struct for the
SkDecodingImageGenerator which lets the client of the generator set
sample size, dithering, and bitmap config.
We have made the SkDecodingImageGenerator constructor private
and replaced the SkDecodingImageGenerator::Install functions
with a SkDecodingImageGenerator::Create functions (one for
SkData and one for SkStream) which now take a
SkDecodingImageGenerator::Options struct.
Also added a ImageDecoderOptions test which loops through a list
of sets of options and tries them on a set of 5 small encoded
images.
Also updated several users of SkDecodingImageGenerator::Install to
follow new call signature - gm/factory.cpp, LazyDecodeBitmap.cpp,
and PictureTest.cpp, CachedDecodingPixelRefTest.cpp.
We also added a new ImprovedBitmapFactory Test which simulates the
exact function that Android will need to modify to use this,
installPixelRef() in BitmapFactory.
R=reed@google.com, scroggo@google.com
Committed: https://code.google.com/p/skia/source/detail?r=12744
Review URL: https://codereview.chromium.org/93703004
git-svn-id: http://skia.googlecode.com/svn/trunk@12855 2bbb7eff-a529-9590-31e7-b0007b416f81
2014-01-02 13:15:13 +00:00
|
|
|
|
2013-12-05 18:31:42 +00:00
|
|
|
private:
|
|
|
|
const TestType fType;
|
|
|
|
skiatest::Reporter* const fReporter;
|
2015-03-19 15:31:14 +00:00
|
|
|
|
|
|
|
typedef SkImageGenerator INHERITED;
|
2013-12-05 18:31:42 +00:00
|
|
|
};
|
Add Options to SkDecodingImageGenerator, simplify API.
Motivation: We want to remove redundant classes from Skia. To
that end we want to remove SkImageRef and its subclasses and
replace their uses with SkDiscardablePixelRef +
SkDecodingImageGenerator. Since Android uses SkImageRef, we need
to make sure that SkDecodingImageGenerator allows all of the
settings that Android exposes in BitmapFactory.Options.
To that end, we have created an Options struct for the
SkDecodingImageGenerator which lets the client of the generator set
sample size, dithering, and bitmap config.
We have made the SkDecodingImageGenerator constructor private
and replaced the SkDecodingImageGenerator::Install functions
with a SkDecodingImageGenerator::Create functions (one for
SkData and one for SkStream) which now take a
SkDecodingImageGenerator::Options struct.
Also added a ImageDecoderOptions test which loops through a list
of sets of options and tries them on a set of 5 small encoded
images.
Also updated several users of SkDecodingImageGenerator::Install to
follow new call signature - gm/factory.cpp, LazyDecodeBitmap.cpp,
and PictureTest.cpp, CachedDecodingPixelRefTest.cpp.
We also added a new ImprovedBitmapFactory Test which simulates the
exact function that Android will need to modify to use this,
installPixelRef() in BitmapFactory.
R=reed@google.com, scroggo@google.com
Committed: https://code.google.com/p/skia/source/detail?r=12744
Review URL: https://codereview.chromium.org/93703004
git-svn-id: http://skia.googlecode.com/svn/trunk@12855 2bbb7eff-a529-9590-31e7-b0007b416f81
2014-01-02 13:15:13 +00:00
|
|
|
|
2014-01-21 23:39:22 +00:00
|
|
|
static void check_test_image_generator_bitmap(skiatest::Reporter* reporter,
|
|
|
|
const SkBitmap& bm) {
|
2013-12-05 18:31:42 +00:00
|
|
|
REPORTER_ASSERT(reporter, TestImageGenerator::Width() == bm.width());
|
|
|
|
REPORTER_ASSERT(reporter, TestImageGenerator::Height() == bm.height());
|
|
|
|
SkAutoLockPixels autoLockPixels(bm);
|
2014-09-05 20:34:00 +00:00
|
|
|
REPORTER_ASSERT(reporter, bm.getPixels());
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == bm.getPixels()) {
|
2013-12-05 18:31:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
int errors = 0;
|
|
|
|
for (int y = 0; y < bm.height(); ++y) {
|
|
|
|
for (int x = 0; x < bm.width(); ++x) {
|
2015-11-10 12:55:15 +00:00
|
|
|
if (TestImageGenerator::Color() != bm.getColor(x, y)) {
|
2013-12-05 18:31:42 +00:00
|
|
|
++errors;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(reporter, 0 == errors);
|
|
|
|
}
|
|
|
|
|
2014-01-21 23:39:22 +00:00
|
|
|
static void check_pixelref(TestImageGenerator::TestType type,
|
|
|
|
skiatest::Reporter* reporter,
|
2015-11-10 12:55:15 +00:00
|
|
|
SkDiscardableMemory::Factory* factory,
|
|
|
|
SkColorType colorType) {
|
|
|
|
SkAutoTDelete<SkImageGenerator> gen(new TestImageGenerator(type, reporter, colorType));
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, gen.get() != nullptr);
|
2013-12-05 18:31:42 +00:00
|
|
|
SkBitmap lazy;
|
2016-03-16 20:53:35 +00:00
|
|
|
bool success = SkDEPRECATED_InstallDiscardablePixelRef(gen.release(), nullptr, &lazy, factory);
|
2015-09-24 07:21:20 +00:00
|
|
|
|
2015-03-19 15:31:14 +00:00
|
|
|
REPORTER_ASSERT(reporter, success);
|
2013-12-05 18:31:42 +00:00
|
|
|
if (TestImageGenerator::kSucceedGetPixels_TestType == type) {
|
2014-01-21 23:39:22 +00:00
|
|
|
check_test_image_generator_bitmap(reporter, lazy);
|
2013-12-05 18:31:42 +00:00
|
|
|
} else if (TestImageGenerator::kFailGetPixels_TestType == type) {
|
|
|
|
SkAutoLockPixels autoLockPixels(lazy);
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, nullptr == lazy.getPixels());
|
2013-12-05 18:31:42 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-12 22:37:32 +00:00
|
|
|
|
2013-12-05 14:00:03 +00:00
|
|
|
/**
|
2013-12-05 18:31:42 +00:00
|
|
|
* This tests the basic functionality of SkDiscardablePixelRef with a
|
|
|
|
* basic SkImageGenerator implementation and several
|
|
|
|
* SkDiscardableMemory::Factory choices.
|
2013-12-05 14:00:03 +00:00
|
|
|
*/
|
2013-12-05 18:31:42 +00:00
|
|
|
DEF_TEST(DiscardableAndCachingPixelRef, reporter) {
|
2015-11-10 12:55:15 +00:00
|
|
|
const SkColorType testColorTypes[] = {
|
|
|
|
kN32_SkColorType,
|
|
|
|
kIndex_8_SkColorType,
|
|
|
|
kRGB_565_SkColorType
|
|
|
|
};
|
|
|
|
for (const SkColorType testColorType : testColorTypes) {
|
|
|
|
check_pixelref(TestImageGenerator::kFailGetPixels_TestType, reporter, nullptr,
|
|
|
|
testColorType);
|
|
|
|
check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, reporter, nullptr,
|
|
|
|
testColorType);
|
2013-12-05 18:31:42 +00:00
|
|
|
|
2015-11-10 12:55:15 +00:00
|
|
|
SkAutoTUnref<SkDiscardableMemoryPool> pool(
|
|
|
|
SkDiscardableMemoryPool::Create(1, nullptr));
|
|
|
|
REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed());
|
|
|
|
check_pixelref(TestImageGenerator::kFailGetPixels_TestType, reporter, pool,
|
|
|
|
testColorType);
|
|
|
|
REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed());
|
|
|
|
check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, reporter, pool,
|
|
|
|
testColorType);
|
|
|
|
REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed());
|
2013-12-05 18:31:42 +00:00
|
|
|
|
2015-11-10 12:55:15 +00:00
|
|
|
SkDiscardableMemoryPool* globalPool = SkGetGlobalDiscardableMemoryPool();
|
|
|
|
// Only acts differently from nullptr on a platform that has a
|
|
|
|
// default discardable memory implementation that differs from the
|
|
|
|
// global DM pool.
|
|
|
|
check_pixelref(TestImageGenerator::kFailGetPixels_TestType, reporter, globalPool,
|
|
|
|
testColorType);
|
|
|
|
check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, reporter, globalPool,
|
|
|
|
testColorType);
|
|
|
|
}
|
2013-11-21 15:32:08 +00:00
|
|
|
}
|
2014-08-18 15:27:09 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
DEF_TEST(Image_NewFromGenerator, r) {
|
2015-11-10 12:55:15 +00:00
|
|
|
const TestImageGenerator::TestType testTypes[] = {
|
2014-08-18 15:27:09 +00:00
|
|
|
TestImageGenerator::kFailGetPixels_TestType,
|
|
|
|
TestImageGenerator::kSucceedGetPixels_TestType,
|
|
|
|
};
|
2015-11-10 12:55:15 +00:00
|
|
|
const SkColorType testColorTypes[] = {
|
|
|
|
kN32_SkColorType,
|
|
|
|
kIndex_8_SkColorType,
|
|
|
|
kRGB_565_SkColorType
|
|
|
|
};
|
2014-08-18 15:27:09 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(testTypes); ++i) {
|
|
|
|
TestImageGenerator::TestType test = testTypes[i];
|
2015-11-10 12:55:15 +00:00
|
|
|
for (const SkColorType testColorType : testColorTypes) {
|
|
|
|
SkImageGenerator* gen = new TestImageGenerator(test, r, testColorType);
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> image(SkImage::MakeFromGenerator(gen));
|
|
|
|
if (nullptr == image) {
|
2015-11-10 12:55:15 +00:00
|
|
|
ERRORF(r, "SkImage::NewFromGenerator unexpecedly failed ["
|
|
|
|
SK_SIZE_T_SPECIFIER "]", i);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(r, TestImageGenerator::Width() == image->width());
|
|
|
|
REPORTER_ASSERT(r, TestImageGenerator::Height() == image->height());
|
|
|
|
REPORTER_ASSERT(r, image->isLazyGenerated());
|
2014-08-18 15:27:09 +00:00
|
|
|
|
2015-11-10 12:55:15 +00:00
|
|
|
SkBitmap bitmap;
|
|
|
|
bitmap.allocN32Pixels(TestImageGenerator::Width(), TestImageGenerator::Height());
|
|
|
|
SkCanvas canvas(bitmap);
|
|
|
|
const SkColor kDefaultColor = 0xffabcdef;
|
|
|
|
canvas.clear(kDefaultColor);
|
|
|
|
canvas.drawImage(image, 0, 0, nullptr);
|
|
|
|
if (TestImageGenerator::kSucceedGetPixels_TestType == test) {
|
|
|
|
REPORTER_ASSERT(
|
|
|
|
r, TestImageGenerator::Color() == bitmap.getColor(0, 0));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
REPORTER_ASSERT(r, kDefaultColor == bitmap.getColor(0, 0));
|
|
|
|
}
|
2014-08-18 15:27:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|