guarded change to SkImageGenerator to make getInfo() const

BUG=skia:

Review URL: https://codereview.chromium.org/1017293002
This commit is contained in:
reed 2015-03-19 08:31:14 -07:00 committed by Commit bot
parent 647211f124
commit 3ef71e343b
14 changed files with 85 additions and 71 deletions

View File

@ -57,12 +57,7 @@ Error CodecSrc::draw(SkCanvas* canvas) const {
return SkStringPrintf("Couldn't decode %s.", fPath.c_str());
}
SkImageInfo decodeInfo;
if (!codec->getInfo(&decodeInfo)) {
return SkStringPrintf("Couldn't getInfo %s.", fPath.c_str());
}
decodeInfo = decodeInfo.makeColorType(canvasInfo.colorType());
SkImageInfo decodeInfo = codec->getInfo().makeColorType(canvasInfo.colorType());
if (decodeInfo.alphaType() == kUnpremul_SkAlphaType) {
// FIXME: Currently we cannot draw unpremultiplied sources.
decodeInfo = decodeInfo.makeAlphaType(kPremul_SkAlphaType);
@ -93,11 +88,7 @@ Error CodecSrc::draw(SkCanvas* canvas) const {
SkISize CodecSrc::size() const {
SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
SkImageInfo info;
if (codec && codec->getInfo(&info)) {
return info.dimensions();
}
return SkISize::Make(0,0);
return codec->getInfo().dimensions();
}
Name CodecSrc::name() const {

View File

@ -58,10 +58,12 @@ protected:
* initially returns a non-opaque answer, but completing the decode
* reveals that the image is actually opaque.
*/
#ifdef SK_SUPPORT_LEGACY_BOOL_ONGETINFO
bool onGetInfo(SkImageInfo* info) SK_OVERRIDE {
*info = fInfo;
return true;
}
#endif
// Helper for subclasses.
const SkImageInfo& getOriginalInfo() { return fInfo; }
@ -99,5 +101,7 @@ private:
const SkImageInfo fInfo;
SkAutoTDelete<SkStream> fStream;
bool fNeedsRewind;
typedef SkImageGenerator INHERITED;
};
#endif // SkCodec_DEFINED

View File

@ -65,15 +65,19 @@ public:
SkData* refEncodedData() { return this->onRefEncodedData(); }
/**
* Return some information about the image, allowing the owner of
* this object to allocate pixels.
*
* Repeated calls to this function should give the same results,
* allowing the PixelRef to be immutable.
*
* @return false if anything goes wrong.
* Return the ImageInfo associated with this generator.
*/
bool getInfo(SkImageInfo* info);
#ifdef SK_SUPPORT_LEGACY_BOOL_ONGETINFO
SkImageInfo getInfo();
bool getInfo(SkImageInfo* info) {
if (info) {
*info = this->getInfo();
}
return true;
}
#else
const SkImageInfo& getInfo() const { return fInfo; }
#endif
/**
* Used to describe the result of a call to getPixels().
@ -206,8 +210,14 @@ public:
static SkImageGenerator* NewFromData(SkData*);
protected:
virtual SkData* onRefEncodedData();
#ifdef SK_SUPPORT_LEGACY_BOOL_ONGETINFO
SkImageGenerator() : fInfo(SkImageInfo::MakeUnknown(0, 0) ) {}
virtual bool onGetInfo(SkImageInfo* info);
#endif
SkImageGenerator(const SkImageInfo& info) : fInfo(info) {}
virtual SkData* onRefEncodedData();
#ifdef SK_SUPPORT_LEGACY_OPTIONLESS_GET_PIXELS
virtual Result onGetPixels(const SkImageInfo& info,
void* pixels, size_t rowBytes,
@ -219,6 +229,9 @@ protected:
virtual bool onGetYUV8Planes(SkISize sizes[3], void* planes[3], size_t rowBytes[3]);
virtual bool onGetYUV8Planes(SkISize sizes[3], void* planes[3], size_t rowBytes[3],
SkYUVColorSpace* colorSpace);
private:
const SkImageInfo fInfo;
};
#endif // SkImageGenerator_DEFINED

View File

@ -46,7 +46,8 @@ SkCodec* SkCodec::NewFromData(SkData* data) {
}
SkCodec::SkCodec(const SkImageInfo& info, SkStream* stream)
: fInfo(info)
: INHERITED(info)
, fInfo(info)
, fStream(stream)
, fNeedsRewind(false)
{}

View File

@ -7,13 +7,15 @@
#include "SkImageGenerator.h"
bool SkImageGenerator::getInfo(SkImageInfo* info) {
SkImageInfo dummy;
if (NULL == info) {
info = &dummy;
#ifdef SK_SUPPORT_LEGACY_BOOL_ONGETINFO
SkImageInfo SkImageGenerator::getInfo() {
SkImageInfo info;
if (!this->onGetInfo(&info)) {
info = SkImageInfo::MakeUnknown(0, 0);
}
return this->onGetInfo(info);
return info;
}
#endif
SkImageGenerator::Result SkImageGenerator::getPixels(const SkImageInfo& info, void* pixels,
size_t rowBytes, const Options* options,
@ -120,9 +122,11 @@ SkData* SkImageGenerator::onRefEncodedData() {
return NULL;
}
#ifdef SK_SUPPORT_LEGACY_BOOL_ONGETINFO
bool SkImageGenerator::onGetInfo(SkImageInfo*) {
return false;
}
#endif
#ifdef SK_SUPPORT_LEGACY_OPTIONLESS_GET_PIXELS
SkImageGenerator::Result SkImageGenerator::onGetPixels(const SkImageInfo&, void*, size_t,

View File

@ -38,10 +38,12 @@ public:
protected:
SkData* onRefEncodedData() SK_OVERRIDE;
#ifdef SK_SUPPORT_LEGACY_BOOL_ONGETINFO
bool onGetInfo(SkImageInfo* info) SK_OVERRIDE {
*info = fInfo;
return true;
}
#endif
virtual Result onGetPixels(const SkImageInfo& info,
void* pixels, size_t rowBytes, const Options&,
SkPMColor ctable[], int* ctableCount) SK_OVERRIDE;
@ -116,7 +118,8 @@ DecodingImageGenerator::DecodingImageGenerator(
const SkImageInfo& info,
int sampleSize,
bool ditherImage)
: fData(data)
: INHERITED(info)
, fData(data)
, fStream(stream)
, fInfo(info)
, fSampleSize(sampleSize)

View File

@ -11,11 +11,12 @@
bool SkCachingPixelRef::Install(SkImageGenerator* generator,
SkBitmap* dst) {
SkImageInfo info;
SkASSERT(dst != NULL);
if ((NULL == generator)
|| !(generator->getInfo(&info))
|| !dst->setInfo(info)) {
if (NULL == generator) {
return false;
}
const SkImageInfo info = generator->getInfo();
if (!dst->setInfo(info)) {
SkDELETE(generator);
return false;
}

View File

@ -109,12 +109,12 @@ void SkDiscardablePixelRef::onUnlockPixels() {
bool SkInstallDiscardablePixelRef(SkImageGenerator* generator, SkBitmap* dst,
SkDiscardableMemory::Factory* factory) {
SkImageInfo info;
SkAutoTDelete<SkImageGenerator> autoGenerator(generator);
if ((NULL == autoGenerator.get())
|| (!autoGenerator->getInfo(&info))
|| info.isEmpty()
|| (!dst->setInfo(info))) {
if (NULL == autoGenerator.get()) {
return false;
}
SkImageInfo info = autoGenerator->getInfo();
if (info.isEmpty() || !dst->setInfo(info)) {
return false;
}
// Since dst->setInfo() may have changed/fixed-up info, we copy it back from that bitmap

View File

@ -39,7 +39,7 @@ class SkImageDecoderGenerator : public SkImageGenerator {
public:
SkImageDecoderGenerator(const SkImageInfo& info, SkImageDecoder* decoder, SkData* data)
: fInfo(info), fDecoder(decoder), fData(SkRef(data))
: INHERITED(info), fInfo(info), fDecoder(decoder), fData(SkRef(data))
{}
protected:
@ -47,10 +47,12 @@ protected:
return SkRef(fData.get());
}
#ifdef SK_SUPPORT_LEGACY_BOOL_ONGETINFO
virtual bool onGetInfo(SkImageInfo* info) SK_OVERRIDE {
*info = fInfo;
return true;
}
#endif
virtual Result onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
const Options&,
@ -92,7 +94,9 @@ protected:
SkMemoryStream stream(fData->data(), fData->size(), false);
return fDecoder->decodeYUV8Planes(&stream, sizes, planes, rowBytes, colorSpace);
}
private:
typedef SkImageGenerator INHERITED;
};
SkImageGenerator* SkImageGenerator::NewFromData(SkData* data) {

View File

@ -163,7 +163,6 @@ DEF_TEST(DecodingImageGenerator, reporter) {
class TestImageGenerator : public SkImageGenerator {
public:
enum TestType {
kFailGetInfo_TestType,
kFailGetPixels_TestType,
kSucceedGetPixels_TestType,
kLast_TestType = kSucceedGetPixels_TestType
@ -172,22 +171,24 @@ public:
static int Height() { return 10; }
static uint32_t Color() { return 0xff123456; }
TestImageGenerator(TestType type, skiatest::Reporter* reporter)
: fType(type), fReporter(reporter) {
: INHERITED(GetMyInfo()), fType(type), fReporter(reporter) {
SkASSERT((fType <= kLast_TestType) && (fType >= 0));
}
virtual ~TestImageGenerator() { }
protected:
static SkImageInfo GetMyInfo() {
return SkImageInfo::MakeN32(TestImageGenerator::Width(), TestImageGenerator::Height(),
kOpaque_SkAlphaType);
}
#ifdef SK_SUPPORT_LEGACY_BOOL_ONGETINFO
bool onGetInfo(SkImageInfo* info) SK_OVERRIDE {
REPORTER_ASSERT(fReporter, info);
if ((NULL == info) || (kFailGetInfo_TestType == fType)) {
return false;
}
*info = SkImageInfo::MakeN32(TestImageGenerator::Width(),
TestImageGenerator::Height(),
kOpaque_SkAlphaType);
*info = GetMyInfo();
return true;
}
#endif
virtual Result onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
const Options&,
@ -212,6 +213,8 @@ protected:
private:
const TestType fType;
skiatest::Reporter* const fReporter;
typedef SkImageGenerator INHERITED;
};
static void check_test_image_generator_bitmap(skiatest::Reporter* reporter,
@ -256,8 +259,7 @@ static void check_pixelref(TestImageGenerator::TestType type,
} else {
success = SkInstallDiscardablePixelRef(gen.detach(), &lazy, factory);
}
REPORTER_ASSERT(reporter, success
== (TestImageGenerator::kFailGetInfo_TestType != type));
REPORTER_ASSERT(reporter, success);
if (TestImageGenerator::kSucceedGetPixels_TestType == type) {
check_test_image_generator_bitmap(reporter, lazy);
} else if (TestImageGenerator::kFailGetPixels_TestType == type) {
@ -283,15 +285,11 @@ static void test_newlockdelete(skiatest::Reporter* reporter) {
DEF_TEST(DiscardableAndCachingPixelRef, reporter) {
test_newlockdelete(reporter);
check_pixelref(TestImageGenerator::kFailGetInfo_TestType,
reporter, kSkCaching_PixelRefType, NULL);
check_pixelref(TestImageGenerator::kFailGetPixels_TestType,
reporter, kSkCaching_PixelRefType, NULL);
check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType,
reporter, kSkCaching_PixelRefType, NULL);
check_pixelref(TestImageGenerator::kFailGetInfo_TestType,
reporter, kSkDiscardable_PixelRefType, NULL);
check_pixelref(TestImageGenerator::kFailGetPixels_TestType,
reporter, kSkDiscardable_PixelRefType, NULL);
check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType,
@ -321,7 +319,6 @@ DEF_TEST(DiscardableAndCachingPixelRef, reporter) {
DEF_TEST(Image_NewFromGenerator, r) {
TestImageGenerator::TestType testTypes[] = {
TestImageGenerator::kFailGetInfo_TestType,
TestImageGenerator::kFailGetPixels_TestType,
TestImageGenerator::kSucceedGetPixels_TestType,
};
@ -329,10 +326,6 @@ DEF_TEST(Image_NewFromGenerator, r) {
TestImageGenerator::TestType test = testTypes[i];
SkImageGenerator* gen = SkNEW_ARGS(TestImageGenerator, (test, r));
SkAutoTUnref<SkImage> image(SkImage::NewFromGenerator(gen));
if (TestImageGenerator::kFailGetInfo_TestType == test) {
REPORTER_ASSERT(r, NULL == image.get());
continue;
}
if (NULL == image.get()) {
ERRORF(r, "SkImage::NewFromGenerator unexpecedly failed ["
SK_SIZE_T_SPECIFIER "]", i);

View File

@ -20,14 +20,14 @@
// A BitmapFactory that always fails when asked to return pixels.
class FailureImageGenerator : public SkImageGenerator {
public:
FailureImageGenerator() { }
virtual ~FailureImageGenerator() { }
FailureImageGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(100, 100)) {}
protected:
#ifdef SK_SUPPORT_LEGACY_BOOL_ONGETINFO
bool onGetInfo(SkImageInfo* info) SK_OVERRIDE {
*info = SkImageInfo::MakeN32Premul(100, 100);
return true;
}
#endif
// default onGetPixels() returns kUnimplemented, which is what we want.
};

View File

@ -8,8 +8,13 @@
#include "SkImageGenerator.h"
#include "Test.h"
class MyImageGenerator : public SkImageGenerator {
public:
MyImageGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(0, 0)) {}
};
DEF_TEST(ImageGenerator, reporter) {
SkImageGenerator ig;
MyImageGenerator ig;
SkISize sizes[3];
sizes[0] = SkISize::Make(200, 200);
sizes[1] = SkISize::Make(100, 100);

View File

@ -67,11 +67,8 @@ enum ImageType {
#include "SkImageGenerator.h"
class EmptyGenerator : public SkImageGenerator {
protected:
bool onGetInfo(SkImageInfo* info) SK_OVERRIDE {
*info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
return true;
}
public:
EmptyGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(0, 0)) {}
};
static void test_empty_image(skiatest::Reporter* reporter) {

View File

@ -22,19 +22,17 @@ DEFINE_bool(useVolatileCache, false, "Use a volatile cache for deferred image de
// Fits SkPicture::InstallPixelRefProc call signature.
// Used in SkPictureData::CreateFromStream
bool sk_tools::LazyDecodeBitmap(const void* src,
size_t length,
SkBitmap* dst) {
bool sk_tools::LazyDecodeBitmap(const void* src, size_t length, SkBitmap* dst) {
SkAutoDataUnref data(SkData::NewWithCopy(src, length));
if (NULL == data.get()) {
return false;
}
SkAutoTDelete<SkImageGenerator> gen(SkImageGenerator::NewFromData(data));
SkImageInfo info;
if ((NULL == gen.get()) || !gen->getInfo(&info)) {
if (NULL == gen.get()) {
return false;
}
const SkImageInfo info = gen->getInfo();
SkDiscardableMemory::Factory* pool = NULL;
if ((!FLAGS_useVolatileCache) || (info.width() * info.height() < 32 * 1024)) {
// how to do switching with SkDiscardableMemory.