Minor code cleanup (left over from prior CL)

This was requested in https://codereview.chromium.org/1309543012/ (Add new GM to directly exercise perspective bitmap drawing)

Review URL: https://codereview.chromium.org/1320673011
This commit is contained in:
robertphillips 2015-09-03 13:32:33 -07:00 committed by Commit bot
parent 44c3128bd8
commit 943a462fef
16 changed files with 141 additions and 235 deletions

View File

@ -21,26 +21,13 @@ protected:
return SkString("bitmapsource"); return SkString("bitmapsource");
} }
void makeBitmap() {
fBitmap.allocN32Pixels(100, 100);
SkCanvas canvas(fBitmap);
canvas.clear(0x00000000);
SkPaint paint;
paint.setAntiAlias(true);
sk_tool_utils::set_portable_typeface(&paint);
paint.setColor(0xFFFFFFFF);
paint.setTextSize(SkIntToScalar(96));
const char* str = "e";
canvas.drawText(str, strlen(str), SkIntToScalar(20), SkIntToScalar(70), paint);
}
SkISize onISize() override { return SkISize::Make(500, 150); } SkISize onISize() override { return SkISize::Make(500, 150); }
void onOnceBeforeDraw() override { void onOnceBeforeDraw() override {
this->makeBitmap(); fBitmap = sk_tool_utils::create_string_bitmap(100, 100, 0xFFFFFFFF, 20, 70, 96, "e");
} }
static void fillRectFiltered(SkCanvas* canvas, const SkRect& clipRect, SkImageFilter* filter) { static void FillRectFiltered(SkCanvas* canvas, const SkRect& clipRect, SkImageFilter* filter) {
SkPaint paint; SkPaint paint;
paint.setImageFilter(filter); paint.setImageFilter(filter);
canvas->save(); canvas->save();
@ -63,19 +50,19 @@ protected:
SkAutoTUnref<SkImageFilter> bitmapSourceDstRectOnly(SkBitmapSource::Create(fBitmap, bounds, dstRect)); SkAutoTUnref<SkImageFilter> bitmapSourceDstRectOnly(SkBitmapSource::Create(fBitmap, bounds, dstRect));
// Draw an unscaled bitmap. // Draw an unscaled bitmap.
fillRectFiltered(canvas, clipRect, bitmapSource); FillRectFiltered(canvas, clipRect, bitmapSource);
canvas->translate(SkIntToScalar(100), 0); canvas->translate(SkIntToScalar(100), 0);
// Draw an unscaled subset of the source bitmap (srcRect -> srcRect). // Draw an unscaled subset of the source bitmap (srcRect -> srcRect).
fillRectFiltered(canvas, clipRect, bitmapSourceSrcRect); FillRectFiltered(canvas, clipRect, bitmapSourceSrcRect);
canvas->translate(SkIntToScalar(100), 0); canvas->translate(SkIntToScalar(100), 0);
// Draw a subset of the bitmap scaled to a destination rect (srcRect -> dstRect). // Draw a subset of the bitmap scaled to a destination rect (srcRect -> dstRect).
fillRectFiltered(canvas, clipRect, bitmapSourceSrcRectDstRect); FillRectFiltered(canvas, clipRect, bitmapSourceSrcRectDstRect);
canvas->translate(SkIntToScalar(100), 0); canvas->translate(SkIntToScalar(100), 0);
// Draw the entire bitmap scaled to a destination rect (bounds -> dstRect). // Draw the entire bitmap scaled to a destination rect (bounds -> dstRect).
fillRectFiltered(canvas, clipRect, bitmapSourceDstRectOnly); FillRectFiltered(canvas, clipRect, bitmapSourceDstRectOnly);
canvas->translate(SkIntToScalar(100), 0); canvas->translate(SkIntToScalar(100), 0);
} }
} }

View File

@ -14,49 +14,33 @@ namespace skiagm {
class DisplacementMapGM : public GM { class DisplacementMapGM : public GM {
public: public:
DisplacementMapGM() : fInitialized(false) { DisplacementMapGM() {
this->setBGColor(0xFF000000); this->setBGColor(0xFF000000);
} }
protected: protected:
virtual SkString onShortName() { SkString onShortName() override {
return SkString("displacement"); return SkString("displacement");
} }
void make_bitmaps() { void onOnceBeforeDraw() override {
make_bitmap(); fBitmap = sk_tool_utils::create_string_bitmap(80, 80, 0xFF884422, 15, 55, 96, "g");
make_checkerboard(&fCheckerboard, 80, 80);
make_checkerboard(&fSmall, 64, 64); SkColor c1 = sk_tool_utils::color_to_565(0xFF244484);
make_checkerboard(&fLarge, 96, 96); SkColor c2 = sk_tool_utils::color_to_565(0xFF804020);
make_checkerboard(&fLargeW, 96, 64);
make_checkerboard(&fLargeH, 64, 96); fCheckerboard = sk_tool_utils::create_checkerboard_bitmap(80, 80, c1, c2, 8);
fSmall = sk_tool_utils::create_checkerboard_bitmap(64, 64, c1, c2, 8);
fLarge = sk_tool_utils::create_checkerboard_bitmap(96, 96, c1, c2, 8);
fLargeW = sk_tool_utils::create_checkerboard_bitmap(96, 64, c1, c2, 8);
fLargeH = sk_tool_utils::create_checkerboard_bitmap(64, 96, c1, c2, 8);
} }
void make_bitmap() { SkISize onISize() override {
fBitmap.allocN32Pixels(80, 80);
SkCanvas canvas(fBitmap);
canvas.clear(0x00000000);
SkPaint paint;
paint.setAntiAlias(true);
sk_tool_utils::set_portable_typeface(&paint);
paint.setColor(sk_tool_utils::color_to_565(0xFF884422));
paint.setTextSize(SkIntToScalar(96));
const char* str = "g";
canvas.drawText(str, strlen(str), SkIntToScalar(15), SkIntToScalar(55), paint);
}
void make_checkerboard(SkBitmap* bitmap, int w, int h) {
bitmap->allocN32Pixels(w, h);
SkCanvas canvas(*bitmap);
sk_tool_utils::draw_checkerboard(&canvas, sk_tool_utils::color_to_565(0xFF244484),
sk_tool_utils::color_to_565(0xFF804020), 8);
}
virtual SkISize onISize() {
return SkISize::Make(500, 500); return SkISize::Make(500, 500);
} }
void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint) { void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint) const {
canvas->save(); canvas->save();
canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
canvas->clipRect(SkRect::MakeWH(SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height()))); canvas->clipRect(SkRect::MakeWH(SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height())));
@ -64,11 +48,7 @@ protected:
canvas->restore(); canvas->restore();
} }
virtual void onDraw(SkCanvas* canvas) { void onDraw(SkCanvas* canvas) override {
if (!fInitialized) {
make_bitmaps();
fInitialized = true;
}
canvas->clear(SK_ColorBLACK); canvas->clear(SK_ColorBLACK);
SkPaint paint; SkPaint paint;
SkAutoTUnref<SkImageFilter> displ(SkBitmapSource::Create(fCheckerboard)); SkAutoTUnref<SkImageFilter> displ(SkBitmapSource::Create(fCheckerboard));
@ -204,14 +184,12 @@ protected:
} }
private: private:
typedef GM INHERITED;
SkBitmap fBitmap, fCheckerboard, fSmall, fLarge, fLargeW, fLargeH; SkBitmap fBitmap, fCheckerboard, fSmall, fLarge, fLargeW, fLargeH;
bool fInitialized;
typedef GM INHERITED;
}; };
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
static GM* MyFactory(void*) { return new DisplacementMapGM; } DEF_GM(return new DisplacementMapGM;)
static GMRegistry reg(MyFactory);
} }

View File

@ -28,12 +28,11 @@ namespace skiagm {
class ImageFiltersClippedGM : public GM { class ImageFiltersClippedGM : public GM {
public: public:
ImageFiltersClippedGM() : fInitialized(false) { ImageFiltersClippedGM() {
this->setBGColor(0x00000000); this->setBGColor(0x00000000);
} }
protected: protected:
SkString onShortName() override { SkString onShortName() override {
return SkString("imagefiltersclipped"); return SkString("imagefiltersclipped");
} }
@ -42,7 +41,7 @@ protected:
return SkISize::Make(860, 500); return SkISize::Make(860, 500);
} }
void make_gradient_circle(int width, int height) { void makeGradientCircle(int width, int height) {
SkScalar x = SkIntToScalar(width / 2); SkScalar x = SkIntToScalar(width / 2);
SkScalar y = SkIntToScalar(height / 2); SkScalar y = SkIntToScalar(height / 2);
SkScalar radius = SkMinScalar(x, y) * 0.8f; SkScalar radius = SkMinScalar(x, y) * 0.8f;
@ -80,15 +79,13 @@ protected:
canvas->restore(); canvas->restore();
} }
void onDraw(SkCanvas* canvas) override { void onOnceBeforeDraw() override {
if (!fInitialized) { fCheckerboard = sk_tool_utils::create_checkerboard_bitmap(64, 64,
fCheckerboard.allocN32Pixels(64, 64); 0xFFA0A0A0, 0xFF404040, 8);
SkCanvas checkerboardCanvas(fCheckerboard); this->makeGradientCircle(64, 64);
sk_tool_utils::draw_checkerboard(&checkerboardCanvas, 0xFFA0A0A0, 0xFF404040, 8); }
this->make_gradient_circle(64, 64); void onDraw(SkCanvas* canvas) override {
fInitialized = true;
}
canvas->clear(SK_ColorBLACK); canvas->clear(SK_ColorBLACK);
SkAutoTUnref<SkImageFilter> gradient(SkBitmapSource::Create(fGradientCircle)); SkAutoTUnref<SkImageFilter> gradient(SkBitmapSource::Create(fGradientCircle));
@ -150,15 +147,13 @@ protected:
} }
private: private:
bool fInitialized;
SkBitmap fCheckerboard; SkBitmap fCheckerboard;
SkBitmap fGradientCircle; SkBitmap fGradientCircle;
typedef GM INHERITED; typedef GM INHERITED;
}; };
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
static GM* MyFactory(void*) { return new ImageFiltersClippedGM; } DEF_GM(return new ImageFiltersClippedGM;)
static GMRegistry reg(MyFactory);
} }

View File

@ -103,23 +103,10 @@ public:
protected: protected:
virtual SkString onShortName() { SkString onShortName() override {
return SkString("imagefiltersgraph"); return SkString("imagefiltersgraph");
} }
void make_bitmap() {
fBitmap.allocN32Pixels(100, 100);
SkCanvas canvas(fBitmap);
canvas.clear(SK_ColorTRANSPARENT);
SkPaint paint;
paint.setAntiAlias(true);
sk_tool_utils::set_portable_typeface(&paint);
paint.setColor(SK_ColorWHITE);
paint.setTextSize(SkIntToScalar(96));
const char* str = "e";
canvas.drawText(str, strlen(str), SkIntToScalar(20), SkIntToScalar(70), paint);
}
void drawClippedBitmap(SkCanvas* canvas, const SkBitmap& bitmap, const SkPaint& paint) { void drawClippedBitmap(SkCanvas* canvas, const SkBitmap& bitmap, const SkPaint& paint) {
canvas->save(); canvas->save();
canvas->clipRect(SkRect::MakeXYWH(0, 0, canvas->clipRect(SkRect::MakeXYWH(0, 0,
@ -128,13 +115,13 @@ protected:
canvas->restore(); canvas->restore();
} }
virtual SkISize onISize() { return SkISize::Make(500, 150); } SkISize onISize() override { return SkISize::Make(500, 150); }
virtual void onOnceBeforeDraw() { void onOnceBeforeDraw() override {
this->make_bitmap(); fBitmap = sk_tool_utils::create_string_bitmap(100, 100, SK_ColorWHITE, 20, 70, 96, "e");
} }
virtual void onDraw(SkCanvas* canvas) { void onDraw(SkCanvas* canvas) override {
canvas->clear(SK_ColorBLACK); canvas->clear(SK_ColorBLACK);
{ {
SkAutoTUnref<SkImageFilter> bitmapSource(SkBitmapSource::Create(fBitmap)); SkAutoTUnref<SkImageFilter> bitmapSource(SkBitmapSource::Create(fBitmap));
@ -229,5 +216,4 @@ private:
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
static skiagm::GM* MyFactory(void*) { return new ImageFiltersGraphGM; } DEF_GM(return new ImageFiltersGraphGM;)
static skiagm::GMRegistry reg(MyFactory);

View File

@ -27,21 +27,21 @@ namespace skiagm {
class ImageFiltersScaledGM : public GM { class ImageFiltersScaledGM : public GM {
public: public:
ImageFiltersScaledGM() : fInitialized(false) { ImageFiltersScaledGM() {
this->setBGColor(0x00000000); this->setBGColor(0x00000000);
} }
protected: protected:
virtual SkString onShortName() { SkString onShortName() override {
return SkString("imagefiltersscaled"); return SkString("imagefiltersscaled");
} }
virtual SkISize onISize() { SkISize onISize() override {
return SkISize::Make(1428, 500); return SkISize::Make(1428, 500);
} }
void make_gradient_circle(int width, int height) { void makeGradientCircle(int width, int height) {
SkScalar x = SkIntToScalar(width / 2); SkScalar x = SkIntToScalar(width / 2);
SkScalar y = SkIntToScalar(height / 2); SkScalar y = SkIntToScalar(height / 2);
SkScalar radius = SkScalarMul(SkMinScalar(x, y), SkIntToScalar(4) / SkIntToScalar(5)); SkScalar radius = SkScalarMul(SkMinScalar(x, y), SkIntToScalar(4) / SkIntToScalar(5));
@ -60,15 +60,13 @@ protected:
canvas.drawCircle(x, y, radius, paint); canvas.drawCircle(x, y, radius, paint);
} }
virtual void onDraw(SkCanvas* canvas) { void onOnceBeforeDraw() override {
if (!fInitialized) { fCheckerboard = sk_tool_utils::create_checkerboard_bitmap(64, 64,
fCheckerboard.allocN32Pixels(64, 64); 0xFFA0A0A0, 0xFF404040, 8);
SkCanvas checkerboardCanvas(fCheckerboard); this->makeGradientCircle(64, 64);
sk_tool_utils::draw_checkerboard(&checkerboardCanvas, 0xFFA0A0A0, 0xFF404040, 8); }
this->make_gradient_circle(64, 64); void onDraw(SkCanvas* canvas) override {
fInitialized = true;
}
canvas->clear(SK_ColorBLACK); canvas->clear(SK_ColorBLACK);
SkAutoTUnref<SkImageFilter> gradient(SkBitmapSource::Create(fGradientCircle)); SkAutoTUnref<SkImageFilter> gradient(SkBitmapSource::Create(fGradientCircle));
@ -152,15 +150,13 @@ protected:
} }
private: private:
bool fInitialized;
SkBitmap fCheckerboard; SkBitmap fCheckerboard;
SkBitmap fGradientCircle; SkBitmap fGradientCircle;
typedef GM INHERITED; typedef GM INHERITED;
}; };
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
static GM* MyFactory(void*) { return new ImageFiltersScaledGM; } DEF_GM(return new ImageFiltersScaledGM;)
static GMRegistry reg(MyFactory);
} }

View File

@ -54,10 +54,8 @@ protected:
} }
void onOnceBeforeDraw() override { void onOnceBeforeDraw() override {
fCheckerboard.allocN32Pixels(64, 64); fCheckerboard = sk_tool_utils::create_checkerboard_bitmap(64, 64,
SkCanvas checkerboardCanvas(fCheckerboard); 0xFFA0A0A0, 0xFF404040, 8);
sk_tool_utils::draw_checkerboard(&checkerboardCanvas, 0xFFA0A0A0, 0xFF404040, 8);
this->makeGradientCircle(64, 64); this->makeGradientCircle(64, 64);
} }

View File

@ -17,7 +17,7 @@ namespace skiagm {
class ImageLightingGM : public GM { class ImageLightingGM : public GM {
public: public:
ImageLightingGM() : fInitialized(false) { ImageLightingGM() {
this->setBGColor(0xFF000000); this->setBGColor(0xFF000000);
} }
@ -27,19 +27,6 @@ protected:
return SkString("lighting"); return SkString("lighting");
} }
void make_bitmap() {
fBitmap.allocN32Pixels(100, 100);
SkCanvas canvas(fBitmap);
canvas.clear(0x00000000);
SkPaint paint;
paint.setAntiAlias(true);
sk_tool_utils::set_portable_typeface(&paint);
paint.setColor(0xFFFFFFFF);
paint.setTextSize(SkIntToScalar(96));
const char* str = "e";
canvas.drawText(str, strlen(str), SkIntToScalar(20), SkIntToScalar(70), paint);
}
SkISize onISize() override { SkISize onISize() override {
return SkISize::Make(WIDTH, HEIGHT); return SkISize::Make(WIDTH, HEIGHT);
} }
@ -53,11 +40,11 @@ protected:
canvas->restore(); canvas->restore();
} }
void onOnceBeforeDraw() override {
fBitmap = sk_tool_utils::create_string_bitmap(100, 100, 0xFFFFFFFF, 20, 70, 96, "e");
}
void onDraw(SkCanvas* canvas) override { void onDraw(SkCanvas* canvas) override {
if (!fInitialized) {
make_bitmap();
fInitialized = true;
}
canvas->clear(sk_tool_utils::color_to_565(0xFF101010)); canvas->clear(sk_tool_utils::color_to_565(0xFF101010));
SkPaint checkPaint; SkPaint checkPaint;
checkPaint.setColor(sk_tool_utils::color_to_565(0xFF202020)); checkPaint.setColor(sk_tool_utils::color_to_565(0xFF202020));
@ -163,9 +150,9 @@ protected:
} }
private: private:
typedef GM INHERITED;
SkBitmap fBitmap; SkBitmap fBitmap;
bool fInitialized;
typedef GM INHERITED;
}; };
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////

View File

@ -11,18 +11,6 @@
#include "SkPoint3.h" #include "SkPoint3.h"
#include "SkShader.h" #include "SkShader.h"
static SkBitmap make_checkerboard(int texSize) {
SkBitmap bitmap;
bitmap.allocN32Pixels(texSize, texSize);
SkCanvas canvas(bitmap);
sk_tool_utils::draw_checkerboard(&canvas,
sk_tool_utils::color_to_565(0x0),
sk_tool_utils::color_to_565(0xFF804020),
8);
return bitmap;
}
// Create a hemispherical normal map // Create a hemispherical normal map
static SkBitmap make_hemi_normalmap(int texSize) { static SkBitmap make_hemi_normalmap(int texSize) {
SkBitmap hemi; SkBitmap hemi;
@ -87,7 +75,11 @@ protected:
} }
void onOnceBeforeDraw() override { void onOnceBeforeDraw() override {
fDiffuse = make_checkerboard(kTexSize); fDiffuse = sk_tool_utils::create_checkerboard_bitmap(
kTexSize, kTexSize,
sk_tool_utils::color_to_565(0x0),
sk_tool_utils::color_to_565(0xFF804020),
8);
fNormalMaps[kHemi_NormalMap] = make_hemi_normalmap(kTexSize); fNormalMaps[kHemi_NormalMap] = make_hemi_normalmap(kTexSize);
fNormalMaps[kFrustum_NormalMap] = make_frustum_normalmap(kTexSize); fNormalMaps[kFrustum_NormalMap] = make_frustum_normalmap(kTexSize);

View File

@ -14,17 +14,17 @@ namespace skiagm {
class MatrixConvolutionGM : public GM { class MatrixConvolutionGM : public GM {
public: public:
MatrixConvolutionGM() : fInitialized(false) { MatrixConvolutionGM() {
this->setBGColor(0x00000000); this->setBGColor(0x00000000);
} }
protected: protected:
virtual SkString onShortName() { SkString onShortName() override {
return SkString("matrixconvolution"); return SkString("matrixconvolution");
} }
void make_bitmap() { void makeBitmap() {
fBitmap.allocN32Pixels(80, 80); fBitmap.allocN32Pixels(80, 80);
SkCanvas canvas(fBitmap); SkCanvas canvas(fBitmap);
canvas.clear(0x00000000); canvas.clear(0x00000000);
@ -43,7 +43,7 @@ protected:
canvas.drawText(str, strlen(str), SkIntToScalar(-10), SkIntToScalar(80), paint); canvas.drawText(str, strlen(str), SkIntToScalar(-10), SkIntToScalar(80), paint);
} }
virtual SkISize onISize() { SkISize onISize() override {
return SkISize::Make(500, 300); return SkISize::Make(500, 300);
} }
@ -79,11 +79,11 @@ protected:
typedef SkMatrixConvolutionImageFilter MCIF; typedef SkMatrixConvolutionImageFilter MCIF;
virtual void onDraw(SkCanvas* canvas) { void onOnceBeforeDraw() override {
if (!fInitialized) { this->makeBitmap();
make_bitmap(); }
fInitialized = true;
} void onDraw(SkCanvas* canvas) override {
canvas->clear(SK_ColorBLACK); canvas->clear(SK_ColorBLACK);
SkIPoint kernelOffset = SkIPoint::Make(1, 0); SkIPoint kernelOffset = SkIPoint::Make(1, 0);
for (int x = 10; x < 310; x += 100) { for (int x = 10; x < 310; x += 100) {
@ -104,14 +104,13 @@ protected:
} }
private: private:
typedef GM INHERITED;
SkBitmap fBitmap; SkBitmap fBitmap;
bool fInitialized;
typedef GM INHERITED;
}; };
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
static GM* MyFactory(void*) { return new MatrixConvolutionGM; } DEF_GM(return new MatrixConvolutionGM;)
static GMRegistry reg(MyFactory);
} }

View File

@ -25,18 +25,6 @@ protected:
return SkString("offsetimagefilter"); return SkString("offsetimagefilter");
} }
void make_bitmap() {
fBitmap.allocN32Pixels(80, 80);
SkCanvas canvas(fBitmap);
canvas.clear(0);
SkPaint paint;
paint.setAntiAlias(true);
sk_tool_utils::set_portable_typeface(&paint);
paint.setColor(sk_tool_utils::color_to_565(0xD000D000));
paint.setTextSize(96);
canvas.drawText("e", 1, 15, 65, paint);
}
SkISize onISize() override { SkISize onISize() override {
return SkISize::Make(WIDTH, HEIGHT); return SkISize::Make(WIDTH, HEIGHT);
} }
@ -64,13 +52,13 @@ protected:
} }
void onOnceBeforeDraw() override { void onOnceBeforeDraw() override {
make_bitmap(); fBitmap = sk_tool_utils::create_string_bitmap(80, 80, 0xD000D000, 15, 65, 96, "e");
fCheckerboard.allocN32Pixels(80, 80); fCheckerboard = sk_tool_utils::create_checkerboard_bitmap(
SkCanvas checkerboardCanvas(fCheckerboard); 80, 80,
sk_tool_utils::draw_checkerboard(&checkerboardCanvas, sk_tool_utils::color_to_565(0xFFA0A0A0),
sk_tool_utils::color_to_565(0xFFA0A0A0), sk_tool_utils::color_to_565(0xFF404040),
sk_tool_utils::color_to_565(0xFF404040), 8); 8);
} }
void onDraw(SkCanvas* canvas) override { void onDraw(SkCanvas* canvas) override {
@ -101,8 +89,9 @@ protected:
drawClippedBitmap(canvas, fBitmap, paint, 2, cropRect); drawClippedBitmap(canvas, fBitmap, paint, 2, cropRect);
} }
private: private:
typedef skiagm::GM INHERITED;
SkBitmap fBitmap, fCheckerboard; SkBitmap fBitmap, fCheckerboard;
typedef skiagm::GM INHERITED;
}; };
DEF_GM( return new OffsetImageFilterGM; ) DEF_GM( return new OffsetImageFilterGM; )

View File

@ -23,15 +23,6 @@ static SkImage* make_image(SkCanvas* origCanvas, int w, int h) {
return surface->newImageSnapshot(); return surface->newImageSnapshot();
} }
static SkBitmap make_bitmap(int w, int h) {
SkBitmap bitmap;
bitmap.allocN32Pixels(w, h);
SkCanvas canvas(bitmap);
sk_tool_utils::draw_checkerboard(&canvas, SK_ColorBLUE, SK_ColorYELLOW, w/10);
return bitmap;
}
namespace skiagm { namespace skiagm {
class PerspShadersGM : public GM { class PerspShadersGM : public GM {
@ -51,7 +42,9 @@ protected:
} }
void onOnceBeforeDraw() override { void onOnceBeforeDraw() override {
fBitmap = make_bitmap(kCellSize, kCellSize); fBitmap = sk_tool_utils::create_checkerboard_bitmap(kCellSize, kCellSize,
SK_ColorBLUE, SK_ColorYELLOW,
kCellSize/10);
fBitmapShader.reset(SkShader::CreateBitmapShader(fBitmap, fBitmapShader.reset(SkShader::CreateBitmapShader(fBitmap,
SkShader::kClamp_TileMode, SkShader::kClamp_TileMode,

View File

@ -11,7 +11,7 @@
DEF_SIMPLE_GM(repeated_bitmap, canvas, 576, 576) { DEF_SIMPLE_GM(repeated_bitmap, canvas, 576, 576) {
sk_tool_utils::draw_checkerboard(canvas, sk_tool_utils::color_to_565(0xFF999999), sk_tool_utils::draw_checkerboard(canvas, sk_tool_utils::color_to_565(0xFF999999),
SK_ColorWHITE, 12); SK_ColorWHITE, 12);
SkRect rect = SkRect::MakeLTRB(-4.25f, -4.25f, 4.25f, 4.25f); SkRect rect = SkRect::MakeLTRB(-4.25f, -4.25f, 4.25f, 4.25f);
SkPaint paint; SkPaint paint;
paint.setColor(0xFF333333); paint.setColor(0xFF333333);
@ -33,7 +33,7 @@ DEF_SIMPLE_GM(repeated_bitmap, canvas, 576, 576) {
DEF_SIMPLE_GM(repeated_bitmap_jpg, canvas, 576, 576) { DEF_SIMPLE_GM(repeated_bitmap_jpg, canvas, 576, 576) {
sk_tool_utils::draw_checkerboard(canvas, sk_tool_utils::color_to_565(0xFF999999), sk_tool_utils::draw_checkerboard(canvas, sk_tool_utils::color_to_565(0xFF999999),
SK_ColorWHITE, 12); SK_ColorWHITE, 12);
SkRect rect = SkRect::MakeLTRB(-68.0f, -68.0f, 68.0f, 68.0f); SkRect rect = SkRect::MakeLTRB(-68.0f, -68.0f, 68.0f, 68.0f);
SkPaint paint; SkPaint paint;
paint.setColor(0xFF333333); paint.setColor(0xFF333333);

View File

@ -16,22 +16,6 @@
#define HEIGHT 100 #define HEIGHT 100
#define MARGIN 12 #define MARGIN 12
static SkBitmap make_bitmap() {
SkBitmap bitmap;
bitmap.allocN32Pixels(50, 50);
SkCanvas canvas(bitmap);
canvas.clear(0xFF000000);
SkPaint paint;
paint.setAntiAlias(true);
sk_tool_utils::set_portable_typeface(&paint);
paint.setColor(0xD000D000);
paint.setTextSize(SkIntToScalar(50));
const char* str = "e";
canvas.drawText(str, strlen(str), SkIntToScalar(10), SkIntToScalar(45), paint);
return bitmap;
}
namespace skiagm { namespace skiagm {
class TileImageFilterGM : public GM { class TileImageFilterGM : public GM {
@ -50,13 +34,13 @@ protected:
} }
void onOnceBeforeDraw() override { void onOnceBeforeDraw() override {
fBitmap = make_bitmap(); fBitmap = sk_tool_utils::create_string_bitmap(50, 50, 0xD000D000, 10, 45, 50, "e");
fCheckerboard.allocN32Pixels(80, 80); fCheckerboard = sk_tool_utils::create_checkerboard_bitmap(
SkCanvas checkerboardCanvas(fCheckerboard); 80, 80,
sk_tool_utils::draw_checkerboard(&checkerboardCanvas, sk_tool_utils::color_to_565(0xFFA0A0A0),
sk_tool_utils::color_to_565(0xFFA0A0A0), sk_tool_utils::color_to_565(0xFF404040),
sk_tool_utils::color_to_565(0xFF404040), 8); 8);
} }
void onDraw(SkCanvas* canvas) override { void onDraw(SkCanvas* canvas) override {

View File

@ -29,19 +29,6 @@ protected:
return SkString("xfermodeimagefilter"); return SkString("xfermodeimagefilter");
} }
void make_bitmap() {
fBitmap.allocN32Pixels(80, 80);
SkCanvas canvas(fBitmap);
canvas.clear(0x00000000);
SkPaint paint;
paint.setAntiAlias(true);
sk_tool_utils::set_portable_typeface(&paint);
paint.setColor(0xD000D000);
paint.setTextSize(SkIntToScalar(96));
const char* str = "e";
canvas.drawText(str, strlen(str), SkIntToScalar(15), SkIntToScalar(65), paint);
}
SkISize onISize() override { SkISize onISize() override {
return SkISize::Make(WIDTH, HEIGHT); return SkISize::Make(WIDTH, HEIGHT);
} }
@ -66,13 +53,13 @@ protected:
} }
void onOnceBeforeDraw() override { void onOnceBeforeDraw() override {
make_bitmap(); fBitmap = sk_tool_utils::create_string_bitmap(80, 80, 0xD000D000, 15, 65, 96, "e");
fCheckerboard.allocN32Pixels(80, 80); fCheckerboard = sk_tool_utils::create_checkerboard_bitmap(
SkCanvas checkerboardCanvas(fCheckerboard); 80, 80,
sk_tool_utils::draw_checkerboard(&checkerboardCanvas, sk_tool_utils::color_to_565(0xFFA0A0A0),
sk_tool_utils::color_to_565(0xFFA0A0A0), sk_tool_utils::color_to_565(0xFF404040),
sk_tool_utils::color_to_565(0xFF404040), 8); 8);
} }
void onDraw(SkCanvas* canvas) override { void onDraw(SkCanvas* canvas) override {

View File

@ -188,6 +188,15 @@ SkShader* create_checkerboard_shader(SkColor c1, SkColor c2, int size) {
bm, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode); bm, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);
} }
SkBitmap create_checkerboard_bitmap(int w, int h, SkColor c1, SkColor c2, int checkSize) {
SkBitmap bitmap;
bitmap.allocN32Pixels(w, h);
SkCanvas canvas(bitmap);
sk_tool_utils::draw_checkerboard(&canvas, c1, c2, checkSize);
return bitmap;
}
void draw_checkerboard(SkCanvas* canvas, SkColor c1, SkColor c2, int size) { void draw_checkerboard(SkCanvas* canvas, SkColor c1, SkColor c2, int size) {
SkPaint paint; SkPaint paint;
paint.setShader(create_checkerboard_shader(c1, c2, size))->unref(); paint.setShader(create_checkerboard_shader(c1, c2, size))->unref();
@ -195,6 +204,24 @@ void draw_checkerboard(SkCanvas* canvas, SkColor c1, SkColor c2, int size) {
canvas->drawPaint(paint); canvas->drawPaint(paint);
} }
SkBitmap create_string_bitmap(int w, int h, SkColor c, int x, int y,
int textSize, const char* str) {
SkBitmap bitmap;
bitmap.allocN32Pixels(w, h);
SkCanvas canvas(bitmap);
SkPaint paint;
paint.setAntiAlias(true);
sk_tool_utils::set_portable_typeface(&paint);
paint.setColor(c);
paint.setTextSize(SkIntToScalar(textSize));
canvas.clear(0x00000000);
canvas.drawText(str, strlen(str), SkIntToScalar(x), SkIntToScalar(y), paint);
return bitmap;
}
void add_to_text_blob(SkTextBlobBuilder* builder, const char* text, const SkPaint& origPaint, void add_to_text_blob(SkTextBlobBuilder* builder, const char* text, const SkPaint& origPaint,
SkScalar x, SkScalar y) { SkScalar x, SkScalar y) {
SkPaint paint(origPaint); SkPaint paint(origPaint);

View File

@ -86,7 +86,7 @@ namespace sk_tool_utils {
void write_pixels(SkCanvas*, const SkBitmap&, int x, int y, SkColorType, SkAlphaType); void write_pixels(SkCanvas*, const SkBitmap&, int x, int y, SkColorType, SkAlphaType);
// private to sk_tool_utils // private to sk_tool_utils
SkTypeface* create_font(const char* name, SkTypeface::Style ); SkTypeface* create_font(const char* name, SkTypeface::Style);
/** Returns a newly created CheckerboardShader. */ /** Returns a newly created CheckerboardShader. */
SkShader* create_checkerboard_shader(SkColor c1, SkColor c2, int size); SkShader* create_checkerboard_shader(SkColor c1, SkColor c2, int size);
@ -96,13 +96,21 @@ namespace sk_tool_utils {
void draw_checkerboard(SkCanvas* canvas, void draw_checkerboard(SkCanvas* canvas,
SkColor color1, SkColor color1,
SkColor color2, SkColor color2,
int size); int checkSize);
/** Make it easier to create a bitmap-based checkerboard */
SkBitmap create_checkerboard_bitmap(int w, int h,
SkColor c1, SkColor c2,
int checkSize);
/** A default checkerboard. */ /** A default checkerboard. */
inline void draw_checkerboard(SkCanvas* canvas) { inline void draw_checkerboard(SkCanvas* canvas) {
sk_tool_utils::draw_checkerboard(canvas, 0xFF999999, 0xFF666666, 8); sk_tool_utils::draw_checkerboard(canvas, 0xFF999999, 0xFF666666, 8);
} }
SkBitmap create_string_bitmap(int w, int h, SkColor c, int x, int y,
int textSize, const char* str);
// Encodes to PNG, unless there is already encoded data, in which case that gets // Encodes to PNG, unless there is already encoded data, in which case that gets
// used. // used.
class PngPixelSerializer : public SkPixelSerializer { class PngPixelSerializer : public SkPixelSerializer {