aboutsummaryrefslogtreecommitdiffhomepage
path: root/gm/pictureshadertile.cpp
diff options
context:
space:
mode:
authorGravatar fmalita <fmalita@chromium.org>2014-08-06 13:07:15 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2014-08-06 13:07:15 -0700
commitb5f7826c51af2862aebcabe61e1ba684f925e488 (patch)
treed4f679b74224259eaa59f57fd83fd7a984b4a22d /gm/pictureshadertile.cpp
parent3c7edda88e275bcdaeb5d3afd0428a21f1635d13 (diff)
Explicit tile bounds for SkPictureShader
The integer picture size is not granular enough to allow precise tiling in arbitrary coordinate systems. This CL adds an optional tile bounds float rect param to control the tile size and location. (this also allows tile spacing emulation for picture shaders). R=reed@google.com, robertphillips@google.com Author: fmalita@chromium.org Review URL: https://codereview.chromium.org/437393003
Diffstat (limited to 'gm/pictureshadertile.cpp')
-rw-r--r--gm/pictureshadertile.cpp147
1 files changed, 147 insertions, 0 deletions
diff --git a/gm/pictureshadertile.cpp b/gm/pictureshadertile.cpp
new file mode 100644
index 0000000000..9343da3649
--- /dev/null
+++ b/gm/pictureshadertile.cpp
@@ -0,0 +1,147 @@
+/*
+ * Copyright 2014 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "gm.h"
+
+#include "SkPaint.h"
+#include "SkPicture.h"
+#include "SkPictureRecorder.h"
+#include "SkShader.h"
+
+static const SkScalar kPictureSize = SK_Scalar1;
+static const SkScalar kFillSize = 100;
+static const unsigned kRowSize = 6;
+
+static const struct {
+ SkScalar x, y, w, h;
+ SkScalar offsetX, offsetY;
+} tiles[] = {
+ { 0, 0, 1, 1, 0, 0 },
+ { 0.5f, 0.5f, 1, 1, 0, 0 },
+ { -0.5f, -0.5f, 1, 1, 0, 0 },
+
+ { 0, 0, 1.5f, 1.5f, 0, 0 },
+ { 0.5f, 0.5f, 1.5f, 1.5f, 0, 0 },
+ { -0.5f, -0.5f, 1.5f, 1.5f, 0, 0 },
+
+ { 0, 0, 0.5f, 0.5f, 0, 0 },
+ { -0.25f, -0.25f, 0.5f, 0.5f, 0, 0 },
+ { 0.25f, 0.25f, 0.5f, 0.5f, 0, 0 },
+
+ { 0, 0, 1, 1, 0.5f, 0.5f },
+ { 0.5f, 0.5f, 1, 1, 0.5f, 0.5f },
+ { -0.5f, -0.5f, 1, 1, 0.5f, 0.5f },
+
+ { 0, 0, 1.5f, 1.5f, 0.5f, 0.5f },
+ { 0.5f, 0.5f, 1.5f, 1.5f, 0.5f, 0.5f },
+ { -0.5f, -0.5f, 1.5f, 1.5f, 0.5f, 0.5f },
+
+ { 0, 0, 1.5f, 1, 0, 0 },
+ { 0.5f, 0.5f, 1.5f, 1, 0, 0 },
+ { -0.5f, -0.5f, 1.5f, 1, 0, 0 },
+
+ { 0, 0, 0.5f, 1, 0, 0 },
+ { -0.25f, -0.25f, 0.5f, 1, 0, 0 },
+ { 0.25f, 0.25f, 0.5f, 1, 0, 0 },
+
+ { 0, 0, 1, 1.5f, 0, 0 },
+ { 0.5f, 0.5f, 1, 1.5f, 0, 0 },
+ { -0.5f, -0.5f, 1, 1.5f, 0, 0 },
+
+ { 0, 0, 1, 0.5f, 0, 0 },
+ { -0.25f, -0.25f, 1, 0.5f, 0, 0 },
+ { 0.25f, 0.25f, 1, 0.5f, 0, 0 },
+};
+
+class PictureShaderTileGM : public skiagm::GM {
+public:
+ PictureShaderTileGM() {
+ SkPictureRecorder recorder;
+ SkCanvas* pictureCanvas = recorder.beginRecording(SkScalarCeilToInt(kPictureSize),
+ SkScalarCeilToInt(kPictureSize),
+ NULL, 0);
+ drawScene(pictureCanvas, kPictureSize);
+ SkAutoTUnref<SkPicture> picture(recorder.endRecording());
+
+ for (unsigned i = 0; i < SK_ARRAY_COUNT(tiles); ++i) {
+ SkRect tile = SkRect::MakeXYWH(tiles[i].x * kPictureSize,
+ tiles[i].y * kPictureSize,
+ tiles[i].w * kPictureSize,
+ tiles[i].h * kPictureSize);
+ SkMatrix localMatrix;
+ localMatrix.setTranslate(tiles[i].offsetX * kPictureSize,
+ tiles[i].offsetY * kPictureSize);
+ localMatrix.postScale(kFillSize / (2 * kPictureSize),
+ kFillSize / (2 * kPictureSize));
+ fShaders[i].reset(SkShader::CreatePictureShader(picture,
+ SkShader::kRepeat_TileMode,
+ SkShader::kRepeat_TileMode,
+ &localMatrix,
+ &tile));
+ }
+ }
+
+protected:
+ virtual uint32_t onGetFlags() const SK_OVERRIDE {
+ return kSkipTiled_Flag;
+ }
+
+ virtual SkString onShortName() SK_OVERRIDE {
+ return SkString("pictureshadertile");
+ }
+
+ virtual SkISize onISize() SK_OVERRIDE {
+ return SkISize::Make(800, 600);
+ }
+
+ virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
+ canvas->clear(SK_ColorBLACK);
+
+ SkPaint paint;
+ paint.setStyle(SkPaint::kFill_Style);
+
+ for (unsigned i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
+ paint.setShader(fShaders[i]);
+
+ canvas->save();
+ canvas->translate((i % kRowSize) * kFillSize * 1.1f,
+ (i / kRowSize) * kFillSize * 1.1f);
+ canvas->drawRect(SkRect::MakeWH(kFillSize, kFillSize), paint);
+ canvas->restore();
+ }
+ }
+
+private:
+ void drawScene(SkCanvas* canvas, SkScalar pictureSize) {
+ canvas->clear(SK_ColorWHITE);
+
+ SkPaint paint;
+ paint.setColor(SK_ColorGREEN);
+ paint.setStyle(SkPaint::kFill_Style);
+ paint.setAntiAlias(true);
+
+ canvas->drawCircle(pictureSize / 4, pictureSize / 4, pictureSize / 4, paint);
+ canvas->drawRect(SkRect::MakeXYWH(pictureSize / 2, pictureSize / 2,
+ pictureSize / 2, pictureSize / 2), paint);
+
+ paint.setColor(SK_ColorRED);
+ canvas->drawLine(pictureSize / 2, pictureSize * 1 / 3,
+ pictureSize / 2, pictureSize * 2 / 3, paint);
+ canvas->drawLine(pictureSize * 1 / 3, pictureSize / 2,
+ pictureSize * 2 / 3, pictureSize / 2, paint);
+
+ paint.setColor(SK_ColorBLACK);
+ paint.setStyle(SkPaint::kStroke_Style);
+ canvas->drawRect(SkRect::MakeWH(pictureSize, pictureSize), paint);
+ }
+
+ SkAutoTUnref<SkShader> fShaders[SK_ARRAY_COUNT(tiles)];
+
+ typedef GM INHERITED;
+};
+
+DEF_GM( return SkNEW(PictureShaderTileGM); )