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.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkImage.h"
|
|
|
|
#include "include/core/SkImageGenerator.h"
|
|
|
|
#include "include/core/SkImageInfo.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
|
|
|
#include "include/private/SkColorData.h"
|
2021-11-03 12:49:00 +00:00
|
|
|
#include "src/core/SkOpts.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tests/Test.h"
|
|
|
|
#include "tools/ToolUtils.h"
|
2013-10-29 19:55:00 +00:00
|
|
|
|
2018-03-12 17:46:21 +00:00
|
|
|
#include <utility>
|
|
|
|
|
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
|
2019-03-20 16:12:10 +00:00
|
|
|
static SkColor Color() { return ToolUtils::color_to_565(0xffaabbcc); }
|
2015-11-10 12:55:15 +00:00
|
|
|
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
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = SkImageGenerator;
|
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) {
|
2019-12-11 15:45:01 +00:00
|
|
|
auto gen = std::make_unique<TestImageGenerator>(test, r, testColorType);
|
2017-02-15 20:14:16 +00:00
|
|
|
sk_sp<SkImage> image(SkImage::MakeFromGenerator(std::move(gen)));
|
2016-03-17 17:51:11 +00:00
|
|
|
if (nullptr == image) {
|
2020-05-05 03:17:21 +00:00
|
|
|
ERRORF(r, "SkImage::NewFromGenerator unexpecedly failed [%zu]", i);
|
2015-11-10 12:55:15 +00:00
|
|
|
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);
|
2021-01-28 02:21:08 +00:00
|
|
|
canvas.drawImage(image, 0, 0);
|
2015-11-10 12:55:15 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|