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"
|
2016-10-25 13:57:13 +00:00
|
|
|
#include "SkImageGenerator.h"
|
2017-02-15 20:14:16 +00:00
|
|
|
#include "SkMakeUnique.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"
|
|
|
|
|
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));
|
|
|
|
}
|
2017-03-22 16:05:03 +00:00
|
|
|
~TestImageGenerator() override {}
|
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,
|
2017-05-12 15:41:27 +00:00
|
|
|
const Options& options) 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 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-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,
|
|
|
|
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) {
|
2017-02-15 20:14:16 +00:00
|
|
|
auto gen = skstd::make_unique<TestImageGenerator>(test, r, testColorType);
|
|
|
|
sk_sp<SkImage> image(SkImage::MakeFromGenerator(std::move(gen)));
|
2016-03-17 17:51:11 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|