blob: 877431854c1106055125352463afdfdb1a5d9697 [file] [log] [blame]
fmalitab5f78262014-08-06 13:07:15 -07001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "gm.h"
9
10#include "SkPaint.h"
11#include "SkPicture.h"
12#include "SkPictureRecorder.h"
13#include "SkShader.h"
14
15static const SkScalar kPictureSize = SK_Scalar1;
16static const SkScalar kFillSize = 100;
17static const unsigned kRowSize = 6;
18
19static const struct {
20 SkScalar x, y, w, h;
21 SkScalar offsetX, offsetY;
22} tiles[] = {
23 { 0, 0, 1, 1, 0, 0 },
fmalitab5f78262014-08-06 13:07:15 -070024 { -0.5f, -0.5f, 1, 1, 0, 0 },
fmalitaa2bd24f2014-12-08 11:13:27 -080025 { 0.5f, 0.5f, 1, 1, 0, 0 },
fmalitab5f78262014-08-06 13:07:15 -070026
27 { 0, 0, 1.5f, 1.5f, 0, 0 },
fmalitab5f78262014-08-06 13:07:15 -070028 { -0.5f, -0.5f, 1.5f, 1.5f, 0, 0 },
fmalitaa2bd24f2014-12-08 11:13:27 -080029 { 0.5f, 0.5f, 1.5f, 1.5f, 0, 0 },
fmalitab5f78262014-08-06 13:07:15 -070030
31 { 0, 0, 0.5f, 0.5f, 0, 0 },
fmalitab5f78262014-08-06 13:07:15 -070032 { 0.25f, 0.25f, 0.5f, 0.5f, 0, 0 },
fmalitaa2bd24f2014-12-08 11:13:27 -080033 { -0.25f, -0.25f, 0.5f, 0.5f, 0, 0 },
fmalitab5f78262014-08-06 13:07:15 -070034
35 { 0, 0, 1, 1, 0.5f, 0.5f },
fmalitab5f78262014-08-06 13:07:15 -070036 { -0.5f, -0.5f, 1, 1, 0.5f, 0.5f },
fmalitaa2bd24f2014-12-08 11:13:27 -080037 { 0.5f, 0.5f, 1, 1, 0.5f, 0.5f },
fmalitab5f78262014-08-06 13:07:15 -070038
39 { 0, 0, 1.5f, 1.5f, 0.5f, 0.5f },
fmalitab5f78262014-08-06 13:07:15 -070040 { -0.5f, -0.5f, 1.5f, 1.5f, 0.5f, 0.5f },
fmalitaa2bd24f2014-12-08 11:13:27 -080041 { 0.5f, 0.5f, 1.5f, 1.5f, 0.5f, 0.5f },
fmalitab5f78262014-08-06 13:07:15 -070042
43 { 0, 0, 1.5f, 1, 0, 0 },
fmalitab5f78262014-08-06 13:07:15 -070044 { -0.5f, -0.5f, 1.5f, 1, 0, 0 },
fmalitaa2bd24f2014-12-08 11:13:27 -080045 { 0.5f, 0.5f, 1.5f, 1, 0, 0 },
fmalitab5f78262014-08-06 13:07:15 -070046
47 { 0, 0, 0.5f, 1, 0, 0 },
fmalitab5f78262014-08-06 13:07:15 -070048 { 0.25f, 0.25f, 0.5f, 1, 0, 0 },
fmalitaa2bd24f2014-12-08 11:13:27 -080049 { -0.25f, -0.25f, 0.5f, 1, 0, 0 },
fmalitab5f78262014-08-06 13:07:15 -070050
51 { 0, 0, 1, 1.5f, 0, 0 },
fmalitab5f78262014-08-06 13:07:15 -070052 { -0.5f, -0.5f, 1, 1.5f, 0, 0 },
fmalitaa2bd24f2014-12-08 11:13:27 -080053 { 0.5f, 0.5f, 1, 1.5f, 0, 0 },
fmalitab5f78262014-08-06 13:07:15 -070054
55 { 0, 0, 1, 0.5f, 0, 0 },
fmalitab5f78262014-08-06 13:07:15 -070056 { 0.25f, 0.25f, 1, 0.5f, 0, 0 },
fmalitaa2bd24f2014-12-08 11:13:27 -080057 { -0.25f, -0.25f, 1, 0.5f, 0, 0 },
fmalitab5f78262014-08-06 13:07:15 -070058};
59
fmalita6cd11812014-12-09 05:23:20 -080060static void draw_scene(SkCanvas* canvas, SkScalar pictureSize) {
61 canvas->clear(SK_ColorWHITE);
62
63 SkPaint paint;
64 paint.setColor(SK_ColorGREEN);
65 paint.setStyle(SkPaint::kFill_Style);
66 paint.setAntiAlias(true);
67
68 canvas->drawCircle(pictureSize / 4, pictureSize / 4, pictureSize / 4, paint);
69 canvas->drawRect(SkRect::MakeXYWH(pictureSize / 2, pictureSize / 2,
70 pictureSize / 2, pictureSize / 2), paint);
71
72 paint.setColor(SK_ColorRED);
73 canvas->drawLine(pictureSize / 2, pictureSize * 1 / 3,
74 pictureSize / 2, pictureSize * 2 / 3, paint);
75 canvas->drawLine(pictureSize * 1 / 3, pictureSize / 2,
76 pictureSize * 2 / 3, pictureSize / 2, paint);
77
78 paint.setColor(SK_ColorBLACK);
79 paint.setStyle(SkPaint::kStroke_Style);
80 canvas->drawRect(SkRect::MakeWH(pictureSize, pictureSize), paint);
81}
82
fmalitab5f78262014-08-06 13:07:15 -070083class PictureShaderTileGM : public skiagm::GM {
fmalitab5f78262014-08-06 13:07:15 -070084protected:
fmalitab5f78262014-08-06 13:07:15 -070085
mtklein36352bf2015-03-25 18:17:31 -070086 SkString onShortName() override {
fmalitab5f78262014-08-06 13:07:15 -070087 return SkString("pictureshadertile");
88 }
89
mtklein36352bf2015-03-25 18:17:31 -070090 SkISize onISize() override {
fmalitab5f78262014-08-06 13:07:15 -070091 return SkISize::Make(800, 600);
92 }
93
mtklein36352bf2015-03-25 18:17:31 -070094 void onOnceBeforeDraw() override {
fmalitaa2bd24f2014-12-08 11:13:27 -080095 SkPictureRecorder recorder;
96 SkCanvas* pictureCanvas = recorder.beginRecording(kPictureSize, kPictureSize);
fmalita6cd11812014-12-09 05:23:20 -080097 draw_scene(pictureCanvas, kPictureSize);
reedca2622b2016-03-18 07:25:55 -070098 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
fmalitaa2bd24f2014-12-08 11:13:27 -080099
100 SkPoint offset = SkPoint::Make(100, 100);
101 pictureCanvas = recorder.beginRecording(SkRect::MakeXYWH(offset.x(), offset.y(),
102 kPictureSize, kPictureSize));
103 pictureCanvas->translate(offset.x(), offset.y());
fmalita6cd11812014-12-09 05:23:20 -0800104 draw_scene(pictureCanvas, kPictureSize);
reedca2622b2016-03-18 07:25:55 -0700105 sk_sp<SkPicture> offsetPicture(recorder.finishRecordingAsPicture());
fmalitaa2bd24f2014-12-08 11:13:27 -0800106
107 for (unsigned i = 0; i < SK_ARRAY_COUNT(tiles); ++i) {
108 SkRect tile = SkRect::MakeXYWH(tiles[i].x * kPictureSize,
109 tiles[i].y * kPictureSize,
110 tiles[i].w * kPictureSize,
111 tiles[i].h * kPictureSize);
112 SkMatrix localMatrix;
113 localMatrix.setTranslate(tiles[i].offsetX * kPictureSize,
114 tiles[i].offsetY * kPictureSize);
115 localMatrix.postScale(kFillSize / (2 * kPictureSize),
116 kFillSize / (2 * kPictureSize));
117
reed1a9b9642016-03-13 14:13:58 -0700118 sk_sp<SkPicture> pictureRef = picture;
fmalitaa2bd24f2014-12-08 11:13:27 -0800119 SkRect* tilePtr = &tile;
120
121 if (tile == SkRect::MakeWH(kPictureSize, kPictureSize)) {
122 // When the tile == picture bounds, exercise the picture + offset path.
reed1a9b9642016-03-13 14:13:58 -0700123 pictureRef = offsetPicture;
halcanary96fcdcc2015-08-27 07:41:13 -0700124 tilePtr = nullptr;
fmalitaa2bd24f2014-12-08 11:13:27 -0800125 }
126
reed1a9b9642016-03-13 14:13:58 -0700127 fShaders[i] = SkShader::MakePictureShader(pictureRef, SkShader::kRepeat_TileMode,
128 SkShader::kRepeat_TileMode, &localMatrix,
129 tilePtr);
fmalitaa2bd24f2014-12-08 11:13:27 -0800130 }
131 }
132
mtklein36352bf2015-03-25 18:17:31 -0700133 void onDraw(SkCanvas* canvas) override {
fmalitab5f78262014-08-06 13:07:15 -0700134 canvas->clear(SK_ColorBLACK);
135
136 SkPaint paint;
137 paint.setStyle(SkPaint::kFill_Style);
138
139 for (unsigned i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
140 paint.setShader(fShaders[i]);
141
142 canvas->save();
143 canvas->translate((i % kRowSize) * kFillSize * 1.1f,
144 (i / kRowSize) * kFillSize * 1.1f);
145 canvas->drawRect(SkRect::MakeWH(kFillSize, kFillSize), paint);
146 canvas->restore();
147 }
148 }
149
150private:
reed1a9b9642016-03-13 14:13:58 -0700151 sk_sp<SkShader> fShaders[SK_ARRAY_COUNT(tiles)];
fmalitab5f78262014-08-06 13:07:15 -0700152
153 typedef GM INHERITED;
154};
155
halcanary385fe4d2015-08-26 13:07:48 -0700156DEF_GM(return new PictureShaderTileGM;)