aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar halcanary <halcanary@google.com>2015-01-26 11:24:32 -0800
committerGravatar Commit bot <commit-bot@chromium.org>2015-01-26 11:24:32 -0800
commit878fa0204bc246ec5fbaca4aa3c81aaefccc30a1 (patch)
treefaef3e19c04dbabc482d43d33c4c2d15bc173639
parent4b952751c093801675307df6f3c4c25749e72925 (diff)
Factor out checkerboard function in gm and sampleapp into tools.
-rw-r--r--gm/colorwheel.cpp22
-rw-r--r--gm/displacement.cpp22
-rw-r--r--gm/imagefiltersclipped.cpp33
-rw-r--r--gm/imagefiltersscaled.cpp33
-rw-r--r--gm/offsetimagefilter.cpp32
-rw-r--r--gm/tileimagefilter.cpp34
-rw-r--r--gm/xfermodeimagefilter.cpp27
-rw-r--r--gyp/SampleApp.gyp1
-rw-r--r--gyp/gmslides.gypi3
-rw-r--r--gyp/tools.gyp7
-rw-r--r--samplecode/SampleColorFilter.cpp18
-rw-r--r--samplecode/SampleFatBits.cpp16
-rw-r--r--samplecode/SampleUnpremul.cpp9
-rw-r--r--tools/Checkerboard.cpp28
-rw-r--r--tools/Checkerboard.h34
15 files changed, 125 insertions, 194 deletions
diff --git a/gm/colorwheel.cpp b/gm/colorwheel.cpp
index f74eaf6a98..8cf94646e5 100644
--- a/gm/colorwheel.cpp
+++ b/gm/colorwheel.cpp
@@ -5,29 +5,12 @@
* found in the LICENSE file.
*/
+#include "Checkerboard.h"
#include "Resources.h"
#include "SkData.h"
#include "gm.h"
#include "sk_tool_utils.h"
-static void checkerboard(
- SkCanvas* canvas, int w, int h, int size, SkColor c1, SkColor c2) {
- SkAutoCanvasRestore autoCanvasRestore(canvas, true);
- canvas->clipRect(SkRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)));
- canvas->drawColor(c1);
- SkPaint paint;
- paint.setColor(c2);
- SkScalar s = SkIntToScalar(size);
- for (int y = 0; y < h; y += size) {
- SkScalar ty = SkIntToScalar(y);
- bool oddRow = (y % (2 * size)) != 0;
- for (int x = oddRow ? size : 0; x < w; x += (2 * size)) {
- SkScalar tx = SkIntToScalar(x);
- canvas->drawRect(SkRect::MakeXYWH(tx, ty, s, s), paint);
- }
- }
-}
-
static void draw_bitmap(SkCanvas* canvas, const char* resource, int x, int y) {
SkBitmap bitmap;
if (GetResourceAsBitmap(resource, &bitmap)) {
@@ -48,8 +31,7 @@ static void draw_bitmap(SkCanvas* canvas, const char* resource, int x, int y) {
background and compression artifacts.
*/
DEF_SIMPLE_GM(colorwheel, canvas, 256, 256) {
- canvas->clear(SK_ColorWHITE);
- checkerboard(canvas, 256, 556, 8, 0xFF999999, 0xFF666666);
+ sk_tools::DrawCheckerboard(canvas);
draw_bitmap(canvas, "color_wheel.png", 0, 0); // top left
draw_bitmap(canvas, "color_wheel.gif", 128, 0); // top right
draw_bitmap(canvas, "color_wheel.webp", 0, 128); // bottom left
diff --git a/gm/displacement.cpp b/gm/displacement.cpp
index dddf8a53c4..f35d5acd3f 100644
--- a/gm/displacement.cpp
+++ b/gm/displacement.cpp
@@ -5,9 +5,10 @@
* found in the LICENSE file.
*/
-#include "gm.h"
-#include "SkDisplacementMapEffect.h"
+#include "Checkerboard.h"
#include "SkBitmapSource.h"
+#include "SkDisplacementMapEffect.h"
+#include "gm.h"
namespace skiagm {
@@ -47,22 +48,7 @@ protected:
void make_checkerboard(SkBitmap* bitmap, int w, int h) {
bitmap->allocN32Pixels(w, h);
SkCanvas canvas(*bitmap);
- canvas.clear(0x00000000);
- SkPaint darkPaint;
- darkPaint.setColor(0xFF804020);
- SkPaint lightPaint;
- lightPaint.setColor(0xFF244484);
- for (int y = 0; y < h; y += 16) {
- for (int x = 0; x < w; x += 16) {
- canvas.save();
- canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
- canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
- canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
- canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
- canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
- canvas.restore();
- }
- }
+ sk_tools::DrawCheckerboard(&canvas, 0xFF244484, 0xFF804020, 8);
}
virtual SkISize onISize() {
diff --git a/gm/imagefiltersclipped.cpp b/gm/imagefiltersclipped.cpp
index beb2e0977a..259342cc27 100644
--- a/gm/imagefiltersclipped.cpp
+++ b/gm/imagefiltersclipped.cpp
@@ -5,19 +5,20 @@
* found in the LICENSE file.
*/
-#include "gm.h"
-#include "SkColor.h"
+#include "Checkerboard.h"
#include "SkBitmapSource.h"
#include "SkBlurImageFilter.h"
+#include "SkColor.h"
#include "SkDisplacementMapEffect.h"
#include "SkDropShadowImageFilter.h"
#include "SkGradientShader.h"
+#include "SkMatrixImageFilter.h"
#include "SkMorphologyImageFilter.h"
#include "SkOffsetImageFilter.h"
#include "SkPerlinNoiseShader.h"
#include "SkRectShaderImageFilter.h"
-#include "SkMatrixImageFilter.h"
#include "SkScalar.h"
+#include "gm.h"
#define RESIZE_FACTOR_X SkIntToScalar(2)
#define RESIZE_FACTOR_Y SkIntToScalar(5)
@@ -40,27 +41,6 @@ protected:
return SkISize::Make(860, 500);
}
- void make_checkerboard() {
- fCheckerboard.allocN32Pixels(64, 64);
- SkCanvas canvas(fCheckerboard);
- canvas.clear(0x00000000);
- SkPaint darkPaint;
- darkPaint.setColor(0xFF404040);
- SkPaint lightPaint;
- lightPaint.setColor(0xFFA0A0A0);
- for (int y = 0; y < 64; y += 16) {
- for (int x = 0; x < 64; x += 16) {
- canvas.save();
- canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
- canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
- canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
- canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
- canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
- canvas.restore();
- }
- }
- }
-
void make_gradient_circle(int width, int height) {
SkScalar x = SkIntToScalar(width / 2);
SkScalar y = SkIntToScalar(height / 2);
@@ -82,7 +62,10 @@ protected:
void onDraw(SkCanvas* canvas) SK_OVERRIDE {
if (!fInitialized) {
- this->make_checkerboard();
+ fCheckerboard.allocN32Pixels(64, 64);
+ SkCanvas checkerboardCanvas(fCheckerboard);
+ sk_tools::DrawCheckerboard(&checkerboardCanvas, 0xFFA0A0A0, 0xFF404040, 8);
+
this->make_gradient_circle(64, 64);
fInitialized = true;
}
diff --git a/gm/imagefiltersscaled.cpp b/gm/imagefiltersscaled.cpp
index b419187fff..084de00fbe 100644
--- a/gm/imagefiltersscaled.cpp
+++ b/gm/imagefiltersscaled.cpp
@@ -5,20 +5,21 @@
* found in the LICENSE file.
*/
-#include "gm.h"
-#include "SkColor.h"
+#include "Checkerboard.h"
#include "SkBitmapSource.h"
#include "SkBlurImageFilter.h"
+#include "SkColor.h"
#include "SkDisplacementMapEffect.h"
#include "SkDropShadowImageFilter.h"
#include "SkGradientShader.h"
#include "SkLightingImageFilter.h"
+#include "SkMatrixImageFilter.h"
#include "SkMorphologyImageFilter.h"
#include "SkOffsetImageFilter.h"
#include "SkPerlinNoiseShader.h"
#include "SkRectShaderImageFilter.h"
-#include "SkMatrixImageFilter.h"
#include "SkScalar.h"
+#include "gm.h"
#define RESIZE_FACTOR SkIntToScalar(4)
@@ -40,27 +41,6 @@ protected:
return SkISize::Make(1428, 500);
}
- void make_checkerboard() {
- fCheckerboard.allocN32Pixels(64, 64);
- SkCanvas canvas(fCheckerboard);
- canvas.clear(0x00000000);
- SkPaint darkPaint;
- darkPaint.setColor(0xFF404040);
- SkPaint lightPaint;
- lightPaint.setColor(0xFFA0A0A0);
- for (int y = 0; y < 64; y += 16) {
- for (int x = 0; x < 64; x += 16) {
- canvas.save();
- canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
- canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
- canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
- canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
- canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
- canvas.restore();
- }
- }
- }
-
void make_gradient_circle(int width, int height) {
SkScalar x = SkIntToScalar(width / 2);
SkScalar y = SkIntToScalar(height / 2);
@@ -82,7 +62,10 @@ protected:
virtual void onDraw(SkCanvas* canvas) {
if (!fInitialized) {
- this->make_checkerboard();
+ fCheckerboard.allocN32Pixels(64, 64);
+ SkCanvas checkerboardCanvas(fCheckerboard);
+ sk_tools::DrawCheckerboard(&checkerboardCanvas, 0xFFA0A0A0, 0xFF404040, 8);
+
this->make_gradient_circle(64, 64);
fInitialized = true;
}
diff --git a/gm/offsetimagefilter.cpp b/gm/offsetimagefilter.cpp
index 3dafed8823..2e80a96e34 100644
--- a/gm/offsetimagefilter.cpp
+++ b/gm/offsetimagefilter.cpp
@@ -5,9 +5,10 @@
* found in the LICENSE file.
*/
-#include "gm.h"
-#include "SkOffsetImageFilter.h"
+#include "Checkerboard.h"
#include "SkBitmapSource.h"
+#include "SkOffsetImageFilter.h"
+#include "gm.h"
#define WIDTH 600
#define HEIGHT 100
@@ -39,27 +40,6 @@ protected:
canvas.drawText(str, strlen(str), SkIntToScalar(15), SkIntToScalar(65), paint);
}
- void make_checkerboard() {
- fCheckerboard.allocN32Pixels(80, 80);
- SkCanvas canvas(fCheckerboard);
- canvas.clear(0x00000000);
- SkPaint darkPaint;
- darkPaint.setColor(0xFF404040);
- SkPaint lightPaint;
- lightPaint.setColor(0xFFA0A0A0);
- for (int y = 0; y < 80; y += 16) {
- for (int x = 0; x < 80; x += 16) {
- canvas.save();
- canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
- canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
- canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
- canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
- canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
- canvas.restore();
- }
- }
- }
-
virtual SkISize onISize() {
return SkISize::Make(WIDTH, HEIGHT);
}
@@ -90,7 +70,11 @@ protected:
virtual void onDraw(SkCanvas* canvas) {
if (!fInitialized) {
make_bitmap();
- make_checkerboard();
+
+ fCheckerboard.allocN32Pixels(80, 80);
+ SkCanvas checkerboardCanvas(fCheckerboard);
+ sk_tools::DrawCheckerboard(&checkerboardCanvas, 0xFFA0A0A0, 0xFF404040, 8);
+
fInitialized = true;
}
canvas->clear(0x00000000);
diff --git a/gm/tileimagefilter.cpp b/gm/tileimagefilter.cpp
index 7d1a3f08c3..1d58def66b 100644
--- a/gm/tileimagefilter.cpp
+++ b/gm/tileimagefilter.cpp
@@ -5,11 +5,12 @@
* found in the LICENSE file.
*/
-#include "gm.h"
-#include "SkColorMatrixFilter.h"
+#include "Checkerboard.h"
+#include "SkBitmapSource.h"
#include "SkColorFilterImageFilter.h"
+#include "SkColorMatrixFilter.h"
#include "SkTileImageFilter.h"
-#include "SkBitmapSource.h"
+#include "gm.h"
#define WIDTH 400
#define HEIGHT 100
@@ -41,27 +42,6 @@ protected:
canvas.drawText(str, strlen(str), SkIntToScalar(10), SkIntToScalar(45), paint);
}
- void make_checkerboard() {
- fCheckerboard.allocN32Pixels(80, 80);
- SkCanvas canvas(fCheckerboard);
- canvas.clear(0x00000000);
- SkPaint darkPaint;
- darkPaint.setColor(0xFF404040);
- SkPaint lightPaint;
- lightPaint.setColor(0xFFA0A0A0);
- for (int y = 0; y < 80; y += 16) {
- for (int x = 0; x < 80; x += 16) {
- canvas.save();
- canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
- canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
- canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
- canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
- canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
- canvas.restore();
- }
- }
- }
-
virtual SkISize onISize() {
return SkISize::Make(WIDTH, HEIGHT);
}
@@ -69,7 +49,11 @@ protected:
virtual void onDraw(SkCanvas* canvas) {
if (!fInitialized) {
make_bitmap();
- make_checkerboard();
+
+ fCheckerboard.allocN32Pixels(80, 80);
+ SkCanvas checkerboardCanvas(fCheckerboard);
+ sk_tools::DrawCheckerboard(&checkerboardCanvas, 0xFFA0A0A0, 0xFF404040, 8);
+
fInitialized = true;
}
canvas->clear(0x00000000);
diff --git a/gm/xfermodeimagefilter.cpp b/gm/xfermodeimagefilter.cpp
index ac5fa5e4b2..b90543ffb4 100644
--- a/gm/xfermodeimagefilter.cpp
+++ b/gm/xfermodeimagefilter.cpp
@@ -6,6 +6,7 @@
*/
#include "gm.h"
+#include "Checkerboard.h"
#include "SkArithmeticMode.h"
#include "SkOffsetImageFilter.h"
#include "SkXfermodeImageFilter.h"
@@ -41,27 +42,6 @@ protected:
canvas.drawText(str, strlen(str), SkIntToScalar(15), SkIntToScalar(65), paint);
}
- void make_checkerboard() {
- fCheckerboard.allocN32Pixels(80, 80);
- SkCanvas canvas(fCheckerboard);
- canvas.clear(0x00000000);
- SkPaint darkPaint;
- darkPaint.setColor(0xFF404040);
- SkPaint lightPaint;
- lightPaint.setColor(0xFFA0A0A0);
- for (int y = 0; y < 80; y += 16) {
- for (int x = 0; x < 80; x += 16) {
- canvas.save();
- canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
- canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
- canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
- canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
- canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
- canvas.restore();
- }
- }
- }
-
SkISize onISize() SK_OVERRIDE {
return SkISize::Make(WIDTH, HEIGHT);
}
@@ -87,7 +67,10 @@ protected:
void onOnceBeforeDraw() SK_OVERRIDE {
make_bitmap();
- make_checkerboard();
+
+ fCheckerboard.allocN32Pixels(80, 80);
+ SkCanvas checkerboardCanvas(fCheckerboard);
+ sk_tools::DrawCheckerboard(&checkerboardCanvas, 0xFFA0A0A0, 0xFF404040, 8);
}
void onDraw(SkCanvas* canvas) SK_OVERRIDE {
diff --git a/gyp/SampleApp.gyp b/gyp/SampleApp.gyp
index 7767835437..f961f1182b 100644
--- a/gyp/SampleApp.gyp
+++ b/gyp/SampleApp.gyp
@@ -152,6 +152,7 @@
'skia_lib.gyp:skia_lib',
'tools.gyp:resources',
'tools.gyp:sk_tool_utils',
+ 'tools.gyp:checkerboard',
'views.gyp:views',
'views_animated.gyp:views_animated',
'xml.gyp:xml',
diff --git a/gyp/gmslides.gypi b/gyp/gmslides.gypi
index abcb86767b..e716644750 100644
--- a/gyp/gmslides.gypi
+++ b/gyp/gmslides.gypi
@@ -7,6 +7,9 @@
'../src/images',
'../src/lazy',
],
+ 'dependencies': [
+ 'tools.gyp:checkerboard',
+ ],
'conditions': [
# If we're building SampleApp on the bots, no need to link in the GM slides.
# We're not going to run it; we're only making sure it still builds.
diff --git a/gyp/tools.gyp b/gyp/tools.gyp
index 1b196a75b1..8d2c7037b1 100644
--- a/gyp/tools.gyp
+++ b/gyp/tools.gyp
@@ -89,6 +89,13 @@
},
},
{
+ 'target_name': 'checkerboard',
+ 'type': 'static_library',
+ 'sources': [ '../tools/Checkerboard.cpp', ],
+ 'dependencies': [ 'skia_lib.gyp:skia_lib', ],
+ 'direct_dependent_settings': { 'include_dirs': [ '../tools', ], },
+ },
+ {
'target_name' : 'timer',
'type': 'static_library',
'sources': [
diff --git a/samplecode/SampleColorFilter.cpp b/samplecode/SampleColorFilter.cpp
index fb5d42744f..175f724710 100644
--- a/samplecode/SampleColorFilter.cpp
+++ b/samplecode/SampleColorFilter.cpp
@@ -5,6 +5,7 @@
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
+#include "Checkerboard.h"
#include "SampleCode.h"
#include "SkView.h"
#include "SkCanvas.h"
@@ -84,20 +85,6 @@ static void test_5bits() {
SkDebugf("--- trunc: %d %d round: %d %d new: %d %d\n", e0, ae0, e1, ae1, e2, ae2);
}
-// No longer marked static, since it is externed in SampleUnpremul.
-SkShader* createChecker();
-SkShader* createChecker() {
- SkBitmap bm;
- bm.allocN32Pixels(2, 2);
- bm.lockPixels();
- *bm.getAddr32(0, 0) = *bm.getAddr32(1, 1) = SkPreMultiplyColor(0xFFFFFFFF);
- *bm.getAddr32(0, 1) = *bm.getAddr32(1, 0) = SkPreMultiplyColor(0xFFCCCCCC);
- SkMatrix m;
- m.setScale(12, 12);
- return SkShader::CreateBitmapShader(bm, SkShader::kRepeat_TileMode,
- SkShader::kRepeat_TileMode, &m);
-}
-
static SkBitmap createBitmap(int n) {
SkBitmap bitmap;
bitmap.allocN32Pixels(n, n);
@@ -131,7 +118,8 @@ class ColorFilterView : public SampleView {
public:
ColorFilterView() {
fBitmap = createBitmap(N);
- fShader = createChecker();
+ fShader = sk_tools::CreateCheckerboardShader(
+ 0xFFCCCCCC, 0xFFFFFFFF, 12);
if (false) { // avoid bit rot, suppress warning
test_5bits();
diff --git a/samplecode/SampleFatBits.cpp b/samplecode/SampleFatBits.cpp
index 9b9af3e3e9..9da351370a 100644
--- a/samplecode/SampleFatBits.cpp
+++ b/samplecode/SampleFatBits.cpp
@@ -5,6 +5,7 @@
* found in the LICENSE file.
*/
+#include "Checkerboard.h"
#include "SampleCode.h"
#include "SkView.h"
#include "SkCanvas.h"
@@ -35,18 +36,6 @@ static void erase(SkSurface* surface) {
surface->getCanvas()->clear(SK_ColorTRANSPARENT);
}
-static SkShader* createChecker(const SkMatrix& localMatrix) {
-// SkColor colors[] = { 0xFFFDFDFD, 0xFFF4F4F4 };
- SkColor colors[] = { 0xFFFFFFFF, 0xFFFFFFFF };
- SkBitmap bm;
- bm.allocN32Pixels(2, 2);
- bm.lockPixels();
- *bm.getAddr32(0, 0) = *bm.getAddr32(1, 1) = SkPreMultiplyColor(colors[0]);
- *bm.getAddr32(0, 1) = *bm.getAddr32(1, 0) = SkPreMultiplyColor(colors[1]);
- return SkShader::CreateBitmapShader(bm, SkShader::kRepeat_TileMode,
- SkShader::kRepeat_TileMode, &localMatrix);
-}
-
class FatBits {
public:
FatBits() {
@@ -98,7 +87,8 @@ public:
fBounds.set(0, 0, SkIntToScalar(width * zoom), SkIntToScalar(height * zoom));
fMatrix.setScale(SkIntToScalar(zoom), SkIntToScalar(zoom));
fInverse.setScale(SK_Scalar1 / zoom, SK_Scalar1 / zoom);
- fShader.reset(createChecker(fMatrix));
+ fShader.reset(sk_tools::CreateCheckerboardShader(
+ 0xFFCCCCCC, 0xFFFFFFFF, zoom));
SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
fMinSurface.reset(SkSurface::NewRaster(info));
diff --git a/samplecode/SampleUnpremul.cpp b/samplecode/SampleUnpremul.cpp
index 9591fec159..889d06780e 100644
--- a/samplecode/SampleUnpremul.cpp
+++ b/samplecode/SampleUnpremul.cpp
@@ -7,6 +7,7 @@
#include "gm.h"
+#include "Checkerboard.h"
#include "Resources.h"
#include "SampleCode.h"
#include "SkBlurMask.h"
@@ -25,9 +26,6 @@
__SK_FORCE_IMAGE_DECODER_LINKING;
-// Defined in SampleColorFilter.cpp
-extern SkShader* createChecker();
-
/**
* Interprets c as an unpremultiplied color, and returns the
* premultiplied equivalent.
@@ -78,10 +76,7 @@ protected:
}
void onDrawBackground(SkCanvas* canvas) SK_OVERRIDE {
- SkPaint paint;
- SkAutoTUnref<SkShader> shader(createChecker());
- paint.setShader(shader.get());
- canvas->drawPaint(paint);
+ sk_tools::DrawCheckerboard(canvas, 0xFFCCCCCC, 0xFFFFFFFF, 12);
}
void onDrawContent(SkCanvas* canvas) SK_OVERRIDE {
diff --git a/tools/Checkerboard.cpp b/tools/Checkerboard.cpp
new file mode 100644
index 0000000000..80de125f15
--- /dev/null
+++ b/tools/Checkerboard.cpp
@@ -0,0 +1,28 @@
+/*
+ * Copyright 2015 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "Checkerboard.h"
+#include "SkCanvas.h"
+#include "SkShader.h"
+
+SkShader* sk_tools::CreateCheckerboardShader(
+ SkColor c1, SkColor c2, int size) {
+ SkBitmap bm;
+ bm.allocN32Pixels(2 * size, 2 * size);
+ bm.eraseColor(c1);
+ bm.eraseArea(SkIRect::MakeLTRB(0, 0, size, size), c2);
+ bm.eraseArea(SkIRect::MakeLTRB(size, size, 2 * size, 2 * size), c2);
+ return SkShader::CreateBitmapShader(
+ bm, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);
+}
+
+void sk_tools::DrawCheckerboard(SkCanvas* canvas,
+ SkColor c1, SkColor c2, int size) {
+ SkPaint paint;
+ paint.setShader(CreateCheckerboardShader(c1, c2, size))->unref();
+ canvas->drawPaint(paint);
+}
diff --git a/tools/Checkerboard.h b/tools/Checkerboard.h
new file mode 100644
index 0000000000..b81e43616f
--- /dev/null
+++ b/tools/Checkerboard.h
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2015 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+#ifndef Checkerboard_DEFINED
+#define Checkerboard_DEFINED
+
+#include "SkColor.h"
+
+class SkCanvas;
+class SkShader;
+
+namespace sk_tools {
+
+/** Returns a newly created CheckerboardShader. */
+SkShader* CreateCheckerboardShader(SkColor c1, SkColor c2, int size);
+
+/** Draw a checkerboard pattern in the current canvas, restricted to
+ the current clip. */
+void DrawCheckerboard(SkCanvas* canvas,
+ SkColor color1,
+ SkColor color2,
+ int size);
+
+/** A default checkerboard. */
+inline void DrawCheckerboard(SkCanvas* canvas) {
+ sk_tools::DrawCheckerboard(canvas, 0xFF999999, 0xFF666666, 8);
+}
+
+} // namespace sk_tools
+
+#endif // Checkerboard_DEFINED