diff options
author | halcanary <halcanary@google.com> | 2015-01-26 11:24:32 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-01-26 11:24:32 -0800 |
commit | 878fa0204bc246ec5fbaca4aa3c81aaefccc30a1 (patch) | |
tree | faef3e19c04dbabc482d43d33c4c2d15bc173639 /tools | |
parent | 4b952751c093801675307df6f3c4c25749e72925 (diff) |
Factor out checkerboard function in gm and sampleapp into tools.
Review URL: https://codereview.chromium.org/834303005
Diffstat (limited to 'tools')
-rw-r--r-- | tools/Checkerboard.cpp | 28 | ||||
-rw-r--r-- | tools/Checkerboard.h | 34 |
2 files changed, 62 insertions, 0 deletions
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 |