SkImage::NewFromGenerator(SkImageGenerator*), and a unit test.
R=reed@google.com Author: halcanary@google.com Review URL: https://codereview.chromium.org/465823003
This commit is contained in:
parent
0ed4e863c0
commit
ea4673fde6
@ -20,6 +20,7 @@
|
|||||||
'../include/ports',
|
'../include/ports',
|
||||||
'../include/utils',
|
'../include/utils',
|
||||||
'../include/xml',
|
'../include/xml',
|
||||||
|
'../include/images',
|
||||||
'../src/core',
|
'../src/core',
|
||||||
'../src/sfnt',
|
'../src/sfnt',
|
||||||
'../src/image',
|
'../src/image',
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
class SkData;
|
class SkData;
|
||||||
class SkCanvas;
|
class SkCanvas;
|
||||||
|
class SkImageGenerator;
|
||||||
class SkPaint;
|
class SkPaint;
|
||||||
class GrContext;
|
class GrContext;
|
||||||
class GrTexture;
|
class GrTexture;
|
||||||
@ -47,6 +48,13 @@ public:
|
|||||||
*/
|
*/
|
||||||
static SkImage* NewTexture(const SkBitmap&);
|
static SkImage* NewTexture(const SkBitmap&);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct a new SkImage based on the given ImageGenerator.
|
||||||
|
* This function will always take ownership of the passed
|
||||||
|
* ImageGenerator. Returns NULL on error.
|
||||||
|
*/
|
||||||
|
static SkImage* NewFromGenerator(SkImageGenerator*);
|
||||||
|
|
||||||
int width() const { return fWidth; }
|
int width() const { return fWidth; }
|
||||||
int height() const { return fHeight; }
|
int height() const { return fHeight; }
|
||||||
uint32_t uniqueID() const { return fUniqueID; }
|
uint32_t uniqueID() const { return fUniqueID; }
|
||||||
|
@ -10,6 +10,7 @@
|
|||||||
#include "SkBitmap.h"
|
#include "SkBitmap.h"
|
||||||
#include "SkCanvas.h"
|
#include "SkCanvas.h"
|
||||||
#include "SkData.h"
|
#include "SkData.h"
|
||||||
|
#include "SkDecodingImageGenerator.h"
|
||||||
#include "SkMallocPixelRef.h"
|
#include "SkMallocPixelRef.h"
|
||||||
|
|
||||||
class SkImage_Raster : public SkImage_Base {
|
class SkImage_Raster : public SkImage_Base {
|
||||||
@ -69,6 +70,10 @@ public:
|
|||||||
SkShader::TileMode,
|
SkShader::TileMode,
|
||||||
const SkMatrix* localMatrix) const SK_OVERRIDE;
|
const SkMatrix* localMatrix) const SK_OVERRIDE;
|
||||||
|
|
||||||
|
SkImage_Raster(const SkBitmap& bm)
|
||||||
|
: INHERITED(bm.width(), bm.height())
|
||||||
|
, fBitmap(bm) {}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
SkImage_Raster() : INHERITED(0, 0) {}
|
SkImage_Raster() : INHERITED(0, 0) {}
|
||||||
|
|
||||||
@ -198,6 +203,14 @@ SkImage* SkImage::NewRasterData(const SkImageInfo& info, SkData* data, size_t ro
|
|||||||
return SkNEW_ARGS(SkImage_Raster, (info, data, rowBytes));
|
return SkNEW_ARGS(SkImage_Raster, (info, data, rowBytes));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SkImage* SkImage::NewFromGenerator(SkImageGenerator* generator) {
|
||||||
|
SkBitmap bitmap;
|
||||||
|
if (!SkInstallDiscardablePixelRef(generator, &bitmap)) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return SkNEW_ARGS(SkImage_Raster, (bitmap));
|
||||||
|
}
|
||||||
|
|
||||||
SkImage* SkNewImageFromPixelRef(const SkImageInfo& info, SkPixelRef* pr,
|
SkImage* SkNewImageFromPixelRef(const SkImageInfo& info, SkPixelRef* pr,
|
||||||
size_t rowBytes) {
|
size_t rowBytes) {
|
||||||
return SkNEW_ARGS(SkImage_Raster, (info, pr, rowBytes));
|
return SkNEW_ARGS(SkImage_Raster, (info, pr, rowBytes));
|
||||||
|
@ -175,7 +175,7 @@ public:
|
|||||||
};
|
};
|
||||||
static int Width() { return 10; }
|
static int Width() { return 10; }
|
||||||
static int Height() { return 10; }
|
static int Height() { return 10; }
|
||||||
static SkColor Color() { return SK_ColorCYAN; }
|
static uint32_t Color() { return 0xff123456; }
|
||||||
TestImageGenerator(TestType type, skiatest::Reporter* reporter)
|
TestImageGenerator(TestType type, skiatest::Reporter* reporter)
|
||||||
: fType(type), fReporter(reporter) {
|
: fType(type), fReporter(reporter) {
|
||||||
SkASSERT((fType <= kLast_TestType) && (fType >= 0));
|
SkASSERT((fType <= kLast_TestType) && (fType >= 0));
|
||||||
@ -322,3 +322,43 @@ DEF_TEST(DiscardableAndCachingPixelRef, reporter) {
|
|||||||
check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType,
|
check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType,
|
||||||
reporter, kSkDiscardable_PixelRefType, globalPool);
|
reporter, kSkDiscardable_PixelRefType, globalPool);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
DEF_TEST(Image_NewFromGenerator, r) {
|
||||||
|
TestImageGenerator::TestType testTypes[] = {
|
||||||
|
TestImageGenerator::kFailGetInfo_TestType,
|
||||||
|
TestImageGenerator::kFailGetPixels_TestType,
|
||||||
|
TestImageGenerator::kSucceedGetPixels_TestType,
|
||||||
|
};
|
||||||
|
for (size_t i = 0; i < SK_ARRAY_COUNT(testTypes); ++i) {
|
||||||
|
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);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
REPORTER_ASSERT(r, TestImageGenerator::Width() == image->width());
|
||||||
|
REPORTER_ASSERT(r, TestImageGenerator::Height() == image->height());
|
||||||
|
|
||||||
|
SkBitmap bitmap;
|
||||||
|
SkAssertResult(bitmap.allocN32Pixels(TestImageGenerator::Width(),
|
||||||
|
TestImageGenerator::Height()));
|
||||||
|
SkCanvas canvas(bitmap);
|
||||||
|
const SkColor kDefaultColor = 0xffabcdef;
|
||||||
|
canvas.clear(kDefaultColor);
|
||||||
|
image->draw(&canvas, 0, 0, NULL);
|
||||||
|
if (TestImageGenerator::kSucceedGetPixels_TestType == test) {
|
||||||
|
REPORTER_ASSERT(
|
||||||
|
r, TestImageGenerator::Color() == *bitmap.getAddr32(0, 0));
|
||||||
|
} else {
|
||||||
|
REPORTER_ASSERT(r, kDefaultColor == bitmap.getColor(0,0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user