Empty picture shaders should draw nothing.

BUG=skia:

Review URL: https://codereview.chromium.org/1142053004
This commit is contained in:
mtklein 2015-05-18 13:44:35 -07:00 committed by Commit bot
parent 7be0ce0ab1
commit cfa90a1aaf
3 changed files with 28 additions and 12 deletions

View File

@ -114,10 +114,10 @@ SkPictureShader::~SkPictureShader() {
fPicture->unref();
}
SkPictureShader* SkPictureShader::Create(const SkPicture* picture, TileMode tmx, TileMode tmy,
SkShader* SkPictureShader::Create(const SkPicture* picture, TileMode tmx, TileMode tmy,
const SkMatrix* localMatrix, const SkRect* tile) {
if (!picture || picture->cullRect().isEmpty() || (tile && tile->isEmpty())) {
return NULL;
return SkShader::CreateEmptyShader();
}
return SkNEW_ARGS(SkPictureShader, (picture, tmx, tmy, localMatrix, tile));
}
@ -185,7 +185,7 @@ SkShader* SkPictureShader::refBitmapShader(const SkMatrix& matrix, const SkMatri
SkISize tileSize = scaledSize.toRound();
if (tileSize.isEmpty()) {
return NULL;
return SkShader::CreateEmptyShader();
}
// The actual scale, compensating for rounding & clamping.
@ -204,7 +204,7 @@ SkShader* SkPictureShader::refBitmapShader(const SkMatrix& matrix, const SkMatri
SkBitmap bm;
bm.setInfo(SkImageInfo::MakeN32Premul(tileSize));
if (!cache_try_alloc_pixels(&bm)) {
return NULL;
return SkShader::CreateEmptyShader();
}
bm.eraseColor(SK_ColorTRANSPARENT);

View File

@ -21,7 +21,7 @@ class SkPicture;
*/
class SkPictureShader : public SkShader {
public:
static SkPictureShader* Create(const SkPicture*, TileMode, TileMode, const SkMatrix*,
static SkShader* Create(const SkPicture*, TileMode, TileMode, const SkMatrix*,
const SkRect*);
virtual ~SkPictureShader();

View File

@ -5,22 +5,38 @@
* found in the LICENSE file.
*/
#include "SkCanvas.h"
#include "SkPicture.h"
#include "SkPictureRecorder.h"
#include "SkShader.h"
#include "Test.h"
// Test that attempting to create a picture shader with a NULL picture or
// empty picture returns NULL.
// empty picture returns a shader that draws nothing.
DEF_TEST(PictureShader_empty, reporter) {
SkShader* shader = SkShader::CreatePictureShader(NULL,
SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, NULL, NULL);
REPORTER_ASSERT(reporter, NULL == shader);
SkPaint paint;
SkBitmap bitmap;
bitmap.allocN32Pixels(1,1);
SkCanvas canvas(bitmap);
canvas.clear(SK_ColorGREEN);
SkShader* shader = SkShader::CreatePictureShader(
NULL, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, NULL, NULL);
paint.setShader(shader)->unref();
canvas.drawRect(SkRect::MakeWH(1,1), paint);
REPORTER_ASSERT(reporter, *bitmap.getAddr32(0,0) == SK_ColorGREEN);
SkPictureRecorder factory;
factory.beginRecording(0, 0, NULL, 0);
SkAutoTUnref<SkPicture> picture(factory.endRecording());
shader = SkShader::CreatePictureShader(picture.get(),
SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, NULL, NULL);
REPORTER_ASSERT(reporter, NULL == shader);
shader = SkShader::CreatePictureShader(
picture.get(), SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, NULL, NULL);
paint.setShader(shader)->unref();
canvas.drawRect(SkRect::MakeWH(1,1), paint);
REPORTER_ASSERT(reporter, *bitmap.getAddr32(0,0) == SK_ColorGREEN);
}