From 943a462fef57832e2683894bb9f2f36ac25d98f7 Mon Sep 17 00:00:00 2001 From: robertphillips Date: Thu, 3 Sep 2015 13:32:33 -0700 Subject: [PATCH] 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 --- gm/bitmapsource.cpp | 25 ++++---------- gm/displacement.cpp | 60 +++++++++++----------------------- gm/imagefiltersclipped.cpp | 25 ++++++-------- gm/imagefiltersgraph.cpp | 26 ++++----------- gm/imagefiltersscaled.cpp | 28 +++++++--------- gm/imagefilterstransformed.cpp | 6 ++-- gm/lighting.cpp | 27 ++++----------- gm/lightingshader.cpp | 18 +++------- gm/matrixconvolution.cpp | 25 +++++++------- gm/offsetimagefilter.cpp | 27 +++++---------- gm/perspshaders.cpp | 13 ++------ gm/repeated_bitmap.cpp | 4 +-- gm/tileimagefilter.cpp | 28 ++++------------ gm/xfermodeimagefilter.cpp | 25 ++++---------- tools/sk_tool_utils.cpp | 27 +++++++++++++++ tools/sk_tool_utils.h | 12 +++++-- 16 files changed, 141 insertions(+), 235 deletions(-) diff --git a/gm/bitmapsource.cpp b/gm/bitmapsource.cpp index a0a3eca663..155785b200 100644 --- a/gm/bitmapsource.cpp +++ b/gm/bitmapsource.cpp @@ -21,26 +21,13 @@ protected: 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); } 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; paint.setImageFilter(filter); canvas->save(); @@ -63,19 +50,19 @@ protected: SkAutoTUnref bitmapSourceDstRectOnly(SkBitmapSource::Create(fBitmap, bounds, dstRect)); // Draw an unscaled bitmap. - fillRectFiltered(canvas, clipRect, bitmapSource); + FillRectFiltered(canvas, clipRect, bitmapSource); canvas->translate(SkIntToScalar(100), 0); // Draw an unscaled subset of the source bitmap (srcRect -> srcRect). - fillRectFiltered(canvas, clipRect, bitmapSourceSrcRect); + FillRectFiltered(canvas, clipRect, bitmapSourceSrcRect); canvas->translate(SkIntToScalar(100), 0); // 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); // Draw the entire bitmap scaled to a destination rect (bounds -> dstRect). - fillRectFiltered(canvas, clipRect, bitmapSourceDstRectOnly); + FillRectFiltered(canvas, clipRect, bitmapSourceDstRectOnly); canvas->translate(SkIntToScalar(100), 0); } } diff --git a/gm/displacement.cpp b/gm/displacement.cpp index 353d14ecd8..4746ef7118 100644 --- a/gm/displacement.cpp +++ b/gm/displacement.cpp @@ -14,49 +14,33 @@ namespace skiagm { class DisplacementMapGM : public GM { public: - DisplacementMapGM() : fInitialized(false) { + DisplacementMapGM() { this->setBGColor(0xFF000000); } protected: - virtual SkString onShortName() { + SkString onShortName() override { return SkString("displacement"); } - void make_bitmaps() { - make_bitmap(); - make_checkerboard(&fCheckerboard, 80, 80); - make_checkerboard(&fSmall, 64, 64); - make_checkerboard(&fLarge, 96, 96); - make_checkerboard(&fLargeW, 96, 64); - make_checkerboard(&fLargeH, 64, 96); + void onOnceBeforeDraw() override { + fBitmap = sk_tool_utils::create_string_bitmap(80, 80, 0xFF884422, 15, 55, 96, "g"); + + SkColor c1 = sk_tool_utils::color_to_565(0xFF244484); + SkColor c2 = sk_tool_utils::color_to_565(0xFF804020); + + 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() { - 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() { + SkISize onISize() override { 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->translate(SkIntToScalar(x), SkIntToScalar(y)); canvas->clipRect(SkRect::MakeWH(SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height()))); @@ -64,11 +48,7 @@ protected: canvas->restore(); } - virtual void onDraw(SkCanvas* canvas) { - if (!fInitialized) { - make_bitmaps(); - fInitialized = true; - } + void onDraw(SkCanvas* canvas) override { canvas->clear(SK_ColorBLACK); SkPaint paint; SkAutoTUnref displ(SkBitmapSource::Create(fCheckerboard)); @@ -204,14 +184,12 @@ protected: } private: - typedef GM INHERITED; SkBitmap fBitmap, fCheckerboard, fSmall, fLarge, fLargeW, fLargeH; - bool fInitialized; + + typedef GM INHERITED; }; ////////////////////////////////////////////////////////////////////////////// -static GM* MyFactory(void*) { return new DisplacementMapGM; } -static GMRegistry reg(MyFactory); - +DEF_GM(return new DisplacementMapGM;) } diff --git a/gm/imagefiltersclipped.cpp b/gm/imagefiltersclipped.cpp index 362d327b37..bf651a831e 100644 --- a/gm/imagefiltersclipped.cpp +++ b/gm/imagefiltersclipped.cpp @@ -28,12 +28,11 @@ namespace skiagm { class ImageFiltersClippedGM : public GM { public: - ImageFiltersClippedGM() : fInitialized(false) { + ImageFiltersClippedGM() { this->setBGColor(0x00000000); } protected: - SkString onShortName() override { return SkString("imagefiltersclipped"); } @@ -42,7 +41,7 @@ protected: 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 y = SkIntToScalar(height / 2); SkScalar radius = SkMinScalar(x, y) * 0.8f; @@ -80,15 +79,13 @@ protected: canvas->restore(); } - void onDraw(SkCanvas* canvas) override { - if (!fInitialized) { - fCheckerboard.allocN32Pixels(64, 64); - SkCanvas checkerboardCanvas(fCheckerboard); - sk_tool_utils::draw_checkerboard(&checkerboardCanvas, 0xFFA0A0A0, 0xFF404040, 8); + void onOnceBeforeDraw() override { + fCheckerboard = sk_tool_utils::create_checkerboard_bitmap(64, 64, + 0xFFA0A0A0, 0xFF404040, 8); + this->makeGradientCircle(64, 64); + } - this->make_gradient_circle(64, 64); - fInitialized = true; - } + void onDraw(SkCanvas* canvas) override { canvas->clear(SK_ColorBLACK); SkAutoTUnref gradient(SkBitmapSource::Create(fGradientCircle)); @@ -150,15 +147,13 @@ protected: } private: - bool fInitialized; SkBitmap fCheckerboard; SkBitmap fGradientCircle; + typedef GM INHERITED; }; ////////////////////////////////////////////////////////////////////////////// -static GM* MyFactory(void*) { return new ImageFiltersClippedGM; } -static GMRegistry reg(MyFactory); - +DEF_GM(return new ImageFiltersClippedGM;) } diff --git a/gm/imagefiltersgraph.cpp b/gm/imagefiltersgraph.cpp index 66b32e99c3..3f9fe3fa95 100644 --- a/gm/imagefiltersgraph.cpp +++ b/gm/imagefiltersgraph.cpp @@ -103,23 +103,10 @@ public: protected: - virtual SkString onShortName() { + SkString onShortName() override { 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) { canvas->save(); canvas->clipRect(SkRect::MakeXYWH(0, 0, @@ -128,13 +115,13 @@ protected: canvas->restore(); } - virtual SkISize onISize() { return SkISize::Make(500, 150); } + SkISize onISize() override { return SkISize::Make(500, 150); } - virtual void onOnceBeforeDraw() { - this->make_bitmap(); + void onOnceBeforeDraw() override { + 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); { SkAutoTUnref bitmapSource(SkBitmapSource::Create(fBitmap)); @@ -229,5 +216,4 @@ private: /////////////////////////////////////////////////////////////////////////////// -static skiagm::GM* MyFactory(void*) { return new ImageFiltersGraphGM; } -static skiagm::GMRegistry reg(MyFactory); +DEF_GM(return new ImageFiltersGraphGM;) diff --git a/gm/imagefiltersscaled.cpp b/gm/imagefiltersscaled.cpp index b53263606c..e98749ebac 100644 --- a/gm/imagefiltersscaled.cpp +++ b/gm/imagefiltersscaled.cpp @@ -27,21 +27,21 @@ namespace skiagm { class ImageFiltersScaledGM : public GM { public: - ImageFiltersScaledGM() : fInitialized(false) { + ImageFiltersScaledGM() { this->setBGColor(0x00000000); } protected: - virtual SkString onShortName() { + SkString onShortName() override { return SkString("imagefiltersscaled"); } - virtual SkISize onISize() { + SkISize onISize() override { 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 y = SkIntToScalar(height / 2); SkScalar radius = SkScalarMul(SkMinScalar(x, y), SkIntToScalar(4) / SkIntToScalar(5)); @@ -60,15 +60,13 @@ protected: canvas.drawCircle(x, y, radius, paint); } - virtual void onDraw(SkCanvas* canvas) { - if (!fInitialized) { - fCheckerboard.allocN32Pixels(64, 64); - SkCanvas checkerboardCanvas(fCheckerboard); - sk_tool_utils::draw_checkerboard(&checkerboardCanvas, 0xFFA0A0A0, 0xFF404040, 8); + void onOnceBeforeDraw() override { + fCheckerboard = sk_tool_utils::create_checkerboard_bitmap(64, 64, + 0xFFA0A0A0, 0xFF404040, 8); + this->makeGradientCircle(64, 64); + } - this->make_gradient_circle(64, 64); - fInitialized = true; - } + void onDraw(SkCanvas* canvas) override { canvas->clear(SK_ColorBLACK); SkAutoTUnref gradient(SkBitmapSource::Create(fGradientCircle)); @@ -152,15 +150,13 @@ protected: } private: - bool fInitialized; SkBitmap fCheckerboard; SkBitmap fGradientCircle; + typedef GM INHERITED; }; ////////////////////////////////////////////////////////////////////////////// -static GM* MyFactory(void*) { return new ImageFiltersScaledGM; } -static GMRegistry reg(MyFactory); - +DEF_GM(return new ImageFiltersScaledGM;) } diff --git a/gm/imagefilterstransformed.cpp b/gm/imagefilterstransformed.cpp index 9259ce4fce..c6ff5a7931 100644 --- a/gm/imagefilterstransformed.cpp +++ b/gm/imagefilterstransformed.cpp @@ -54,10 +54,8 @@ protected: } void onOnceBeforeDraw() override { - fCheckerboard.allocN32Pixels(64, 64); - SkCanvas checkerboardCanvas(fCheckerboard); - sk_tool_utils::draw_checkerboard(&checkerboardCanvas, 0xFFA0A0A0, 0xFF404040, 8); - + fCheckerboard = sk_tool_utils::create_checkerboard_bitmap(64, 64, + 0xFFA0A0A0, 0xFF404040, 8); this->makeGradientCircle(64, 64); } diff --git a/gm/lighting.cpp b/gm/lighting.cpp index d68e67db16..af102d7d03 100644 --- a/gm/lighting.cpp +++ b/gm/lighting.cpp @@ -17,7 +17,7 @@ namespace skiagm { class ImageLightingGM : public GM { public: - ImageLightingGM() : fInitialized(false) { + ImageLightingGM() { this->setBGColor(0xFF000000); } @@ -27,19 +27,6 @@ protected: 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 { return SkISize::Make(WIDTH, HEIGHT); } @@ -53,11 +40,11 @@ protected: canvas->restore(); } + void onOnceBeforeDraw() override { + fBitmap = sk_tool_utils::create_string_bitmap(100, 100, 0xFFFFFFFF, 20, 70, 96, "e"); + } + void onDraw(SkCanvas* canvas) override { - if (!fInitialized) { - make_bitmap(); - fInitialized = true; - } canvas->clear(sk_tool_utils::color_to_565(0xFF101010)); SkPaint checkPaint; checkPaint.setColor(sk_tool_utils::color_to_565(0xFF202020)); @@ -163,9 +150,9 @@ protected: } private: - typedef GM INHERITED; SkBitmap fBitmap; - bool fInitialized; + + typedef GM INHERITED; }; ////////////////////////////////////////////////////////////////////////////// diff --git a/gm/lightingshader.cpp b/gm/lightingshader.cpp index 8bb3595a78..9c45c9b783 100644 --- a/gm/lightingshader.cpp +++ b/gm/lightingshader.cpp @@ -11,18 +11,6 @@ #include "SkPoint3.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 static SkBitmap make_hemi_normalmap(int texSize) { SkBitmap hemi; @@ -87,7 +75,11 @@ protected: } 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[kFrustum_NormalMap] = make_frustum_normalmap(kTexSize); diff --git a/gm/matrixconvolution.cpp b/gm/matrixconvolution.cpp index 1e6a49d4b6..6d16f8dfa7 100644 --- a/gm/matrixconvolution.cpp +++ b/gm/matrixconvolution.cpp @@ -14,17 +14,17 @@ namespace skiagm { class MatrixConvolutionGM : public GM { public: - MatrixConvolutionGM() : fInitialized(false) { + MatrixConvolutionGM() { this->setBGColor(0x00000000); } protected: - virtual SkString onShortName() { + SkString onShortName() override { return SkString("matrixconvolution"); } - void make_bitmap() { + void makeBitmap() { fBitmap.allocN32Pixels(80, 80); SkCanvas canvas(fBitmap); canvas.clear(0x00000000); @@ -43,7 +43,7 @@ protected: canvas.drawText(str, strlen(str), SkIntToScalar(-10), SkIntToScalar(80), paint); } - virtual SkISize onISize() { + SkISize onISize() override { return SkISize::Make(500, 300); } @@ -79,11 +79,11 @@ protected: typedef SkMatrixConvolutionImageFilter MCIF; - virtual void onDraw(SkCanvas* canvas) { - if (!fInitialized) { - make_bitmap(); - fInitialized = true; - } + void onOnceBeforeDraw() override { + this->makeBitmap(); + } + + void onDraw(SkCanvas* canvas) override { canvas->clear(SK_ColorBLACK); SkIPoint kernelOffset = SkIPoint::Make(1, 0); for (int x = 10; x < 310; x += 100) { @@ -104,14 +104,13 @@ protected: } private: - typedef GM INHERITED; SkBitmap fBitmap; - bool fInitialized; + + typedef GM INHERITED; }; ////////////////////////////////////////////////////////////////////////////// -static GM* MyFactory(void*) { return new MatrixConvolutionGM; } -static GMRegistry reg(MyFactory); +DEF_GM(return new MatrixConvolutionGM;) } diff --git a/gm/offsetimagefilter.cpp b/gm/offsetimagefilter.cpp index fdf2aa85fa..fcf2b9843f 100644 --- a/gm/offsetimagefilter.cpp +++ b/gm/offsetimagefilter.cpp @@ -25,18 +25,6 @@ protected: 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 { return SkISize::Make(WIDTH, HEIGHT); } @@ -64,13 +52,13 @@ protected: } void onOnceBeforeDraw() override { - make_bitmap(); + fBitmap = sk_tool_utils::create_string_bitmap(80, 80, 0xD000D000, 15, 65, 96, "e"); - fCheckerboard.allocN32Pixels(80, 80); - SkCanvas checkerboardCanvas(fCheckerboard); - sk_tool_utils::draw_checkerboard(&checkerboardCanvas, - sk_tool_utils::color_to_565(0xFFA0A0A0), - sk_tool_utils::color_to_565(0xFF404040), 8); + fCheckerboard = sk_tool_utils::create_checkerboard_bitmap( + 80, 80, + sk_tool_utils::color_to_565(0xFFA0A0A0), + sk_tool_utils::color_to_565(0xFF404040), + 8); } void onDraw(SkCanvas* canvas) override { @@ -101,8 +89,9 @@ protected: drawClippedBitmap(canvas, fBitmap, paint, 2, cropRect); } private: - typedef skiagm::GM INHERITED; SkBitmap fBitmap, fCheckerboard; + + typedef skiagm::GM INHERITED; }; DEF_GM( return new OffsetImageFilterGM; ) diff --git a/gm/perspshaders.cpp b/gm/perspshaders.cpp index 07a41e9466..59fab47fc8 100644 --- a/gm/perspshaders.cpp +++ b/gm/perspshaders.cpp @@ -23,15 +23,6 @@ static SkImage* make_image(SkCanvas* origCanvas, int w, int h) { 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 { class PerspShadersGM : public GM { @@ -51,7 +42,9 @@ protected: } 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, SkShader::kClamp_TileMode, diff --git a/gm/repeated_bitmap.cpp b/gm/repeated_bitmap.cpp index bda368b738..dc2a2aab07 100644 --- a/gm/repeated_bitmap.cpp +++ b/gm/repeated_bitmap.cpp @@ -11,7 +11,7 @@ DEF_SIMPLE_GM(repeated_bitmap, canvas, 576, 576) { 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); SkPaint paint; paint.setColor(0xFF333333); @@ -33,7 +33,7 @@ DEF_SIMPLE_GM(repeated_bitmap, 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_ColorWHITE, 12); + SK_ColorWHITE, 12); SkRect rect = SkRect::MakeLTRB(-68.0f, -68.0f, 68.0f, 68.0f); SkPaint paint; paint.setColor(0xFF333333); diff --git a/gm/tileimagefilter.cpp b/gm/tileimagefilter.cpp index e47e8fa659..093f12dd43 100644 --- a/gm/tileimagefilter.cpp +++ b/gm/tileimagefilter.cpp @@ -16,22 +16,6 @@ #define HEIGHT 100 #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 { class TileImageFilterGM : public GM { @@ -50,13 +34,13 @@ protected: } void onOnceBeforeDraw() override { - fBitmap = make_bitmap(); + fBitmap = sk_tool_utils::create_string_bitmap(50, 50, 0xD000D000, 10, 45, 50, "e"); - fCheckerboard.allocN32Pixels(80, 80); - SkCanvas checkerboardCanvas(fCheckerboard); - sk_tool_utils::draw_checkerboard(&checkerboardCanvas, - sk_tool_utils::color_to_565(0xFFA0A0A0), - sk_tool_utils::color_to_565(0xFF404040), 8); + fCheckerboard = sk_tool_utils::create_checkerboard_bitmap( + 80, 80, + sk_tool_utils::color_to_565(0xFFA0A0A0), + sk_tool_utils::color_to_565(0xFF404040), + 8); } void onDraw(SkCanvas* canvas) override { diff --git a/gm/xfermodeimagefilter.cpp b/gm/xfermodeimagefilter.cpp index 80994a6ed8..e09c0abe94 100644 --- a/gm/xfermodeimagefilter.cpp +++ b/gm/xfermodeimagefilter.cpp @@ -29,19 +29,6 @@ protected: 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 { return SkISize::Make(WIDTH, HEIGHT); } @@ -66,13 +53,13 @@ protected: } void onOnceBeforeDraw() override { - make_bitmap(); + fBitmap = sk_tool_utils::create_string_bitmap(80, 80, 0xD000D000, 15, 65, 96, "e"); - fCheckerboard.allocN32Pixels(80, 80); - SkCanvas checkerboardCanvas(fCheckerboard); - sk_tool_utils::draw_checkerboard(&checkerboardCanvas, - sk_tool_utils::color_to_565(0xFFA0A0A0), - sk_tool_utils::color_to_565(0xFF404040), 8); + fCheckerboard = sk_tool_utils::create_checkerboard_bitmap( + 80, 80, + sk_tool_utils::color_to_565(0xFFA0A0A0), + sk_tool_utils::color_to_565(0xFF404040), + 8); } void onDraw(SkCanvas* canvas) override { diff --git a/tools/sk_tool_utils.cpp b/tools/sk_tool_utils.cpp index a746d8b9fd..aab068cbd7 100644 --- a/tools/sk_tool_utils.cpp +++ b/tools/sk_tool_utils.cpp @@ -188,6 +188,15 @@ SkShader* create_checkerboard_shader(SkColor c1, SkColor c2, int size) { 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) { SkPaint paint; 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); } +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, SkScalar x, SkScalar y) { SkPaint paint(origPaint); diff --git a/tools/sk_tool_utils.h b/tools/sk_tool_utils.h index 2f70090e49..b20c60ba76 100644 --- a/tools/sk_tool_utils.h +++ b/tools/sk_tool_utils.h @@ -86,7 +86,7 @@ namespace sk_tool_utils { void write_pixels(SkCanvas*, const SkBitmap&, int x, int y, SkColorType, SkAlphaType); // 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. */ SkShader* create_checkerboard_shader(SkColor c1, SkColor c2, int size); @@ -96,13 +96,21 @@ namespace sk_tool_utils { void draw_checkerboard(SkCanvas* canvas, SkColor color1, 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. */ inline void draw_checkerboard(SkCanvas* canvas) { 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 // used. class PngPixelSerializer : public SkPixelSerializer {