blob: d7afbe9a21e7d630f7ef0e3cedf0d883da2248cf [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkCanvas.h"
10#include "include/core/SkColor.h"
11#include "include/core/SkMatrix.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkPaint.h"
13#include "include/core/SkPicture.h"
14#include "include/core/SkPictureRecorder.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040015#include "include/core/SkPoint.h"
16#include "include/core/SkRect.h"
17#include "include/core/SkRefCnt.h"
18#include "include/core/SkScalar.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/core/SkShader.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040020#include "include/core/SkSize.h"
21#include "include/core/SkString.h"
22#include "include/core/SkTileMode.h"
23#include "include/core/SkTypes.h"
fmalitab5f78262014-08-06 13:07:15 -070024
mtkleindbfd7ab2016-09-01 11:24:54 -070025constexpr SkScalar kPictureSize = SK_Scalar1;
26constexpr SkScalar kFillSize = 100;
27constexpr unsigned kRowSize = 6;
fmalitab5f78262014-08-06 13:07:15 -070028
mtkleindbfd7ab2016-09-01 11:24:54 -070029constexpr struct {
fmalitab5f78262014-08-06 13:07:15 -070030 SkScalar x, y, w, h;
31 SkScalar offsetX, offsetY;
32} tiles[] = {
33 { 0, 0, 1, 1, 0, 0 },
fmalitab5f78262014-08-06 13:07:15 -070034 { -0.5f, -0.5f, 1, 1, 0, 0 },
fmalitaa2bd24f2014-12-08 11:13:27 -080035 { 0.5f, 0.5f, 1, 1, 0, 0 },
fmalitab5f78262014-08-06 13:07:15 -070036
37 { 0, 0, 1.5f, 1.5f, 0, 0 },
fmalitab5f78262014-08-06 13:07:15 -070038 { -0.5f, -0.5f, 1.5f, 1.5f, 0, 0 },
fmalitaa2bd24f2014-12-08 11:13:27 -080039 { 0.5f, 0.5f, 1.5f, 1.5f, 0, 0 },
fmalitab5f78262014-08-06 13:07:15 -070040
41 { 0, 0, 0.5f, 0.5f, 0, 0 },
fmalitab5f78262014-08-06 13:07:15 -070042 { 0.25f, 0.25f, 0.5f, 0.5f, 0, 0 },
fmalitaa2bd24f2014-12-08 11:13:27 -080043 { -0.25f, -0.25f, 0.5f, 0.5f, 0, 0 },
fmalitab5f78262014-08-06 13:07:15 -070044
45 { 0, 0, 1, 1, 0.5f, 0.5f },
fmalitab5f78262014-08-06 13:07:15 -070046 { -0.5f, -0.5f, 1, 1, 0.5f, 0.5f },
fmalitaa2bd24f2014-12-08 11:13:27 -080047 { 0.5f, 0.5f, 1, 1, 0.5f, 0.5f },
fmalitab5f78262014-08-06 13:07:15 -070048
49 { 0, 0, 1.5f, 1.5f, 0.5f, 0.5f },
fmalitab5f78262014-08-06 13:07:15 -070050 { -0.5f, -0.5f, 1.5f, 1.5f, 0.5f, 0.5f },
fmalitaa2bd24f2014-12-08 11:13:27 -080051 { 0.5f, 0.5f, 1.5f, 1.5f, 0.5f, 0.5f },
fmalitab5f78262014-08-06 13:07:15 -070052
53 { 0, 0, 1.5f, 1, 0, 0 },
fmalitab5f78262014-08-06 13:07:15 -070054 { -0.5f, -0.5f, 1.5f, 1, 0, 0 },
fmalitaa2bd24f2014-12-08 11:13:27 -080055 { 0.5f, 0.5f, 1.5f, 1, 0, 0 },
fmalitab5f78262014-08-06 13:07:15 -070056
57 { 0, 0, 0.5f, 1, 0, 0 },
fmalitab5f78262014-08-06 13:07:15 -070058 { 0.25f, 0.25f, 0.5f, 1, 0, 0 },
fmalitaa2bd24f2014-12-08 11:13:27 -080059 { -0.25f, -0.25f, 0.5f, 1, 0, 0 },
fmalitab5f78262014-08-06 13:07:15 -070060
61 { 0, 0, 1, 1.5f, 0, 0 },
fmalitab5f78262014-08-06 13:07:15 -070062 { -0.5f, -0.5f, 1, 1.5f, 0, 0 },
fmalitaa2bd24f2014-12-08 11:13:27 -080063 { 0.5f, 0.5f, 1, 1.5f, 0, 0 },
fmalitab5f78262014-08-06 13:07:15 -070064
65 { 0, 0, 1, 0.5f, 0, 0 },
fmalitab5f78262014-08-06 13:07:15 -070066 { 0.25f, 0.25f, 1, 0.5f, 0, 0 },
fmalitaa2bd24f2014-12-08 11:13:27 -080067 { -0.25f, -0.25f, 1, 0.5f, 0, 0 },
fmalitab5f78262014-08-06 13:07:15 -070068};
69
fmalita6cd11812014-12-09 05:23:20 -080070static void draw_scene(SkCanvas* canvas, SkScalar pictureSize) {
71 canvas->clear(SK_ColorWHITE);
72
73 SkPaint paint;
74 paint.setColor(SK_ColorGREEN);
75 paint.setStyle(SkPaint::kFill_Style);
76 paint.setAntiAlias(true);
77
78 canvas->drawCircle(pictureSize / 4, pictureSize / 4, pictureSize / 4, paint);
79 canvas->drawRect(SkRect::MakeXYWH(pictureSize / 2, pictureSize / 2,
80 pictureSize / 2, pictureSize / 2), paint);
81
82 paint.setColor(SK_ColorRED);
83 canvas->drawLine(pictureSize / 2, pictureSize * 1 / 3,
84 pictureSize / 2, pictureSize * 2 / 3, paint);
85 canvas->drawLine(pictureSize * 1 / 3, pictureSize / 2,
86 pictureSize * 2 / 3, pictureSize / 2, paint);
87
88 paint.setColor(SK_ColorBLACK);
89 paint.setStyle(SkPaint::kStroke_Style);
90 canvas->drawRect(SkRect::MakeWH(pictureSize, pictureSize), paint);
91}
92
fmalitab5f78262014-08-06 13:07:15 -070093class PictureShaderTileGM : public skiagm::GM {
fmalitab5f78262014-08-06 13:07:15 -070094protected:
fmalitab5f78262014-08-06 13:07:15 -070095
mtklein36352bf2015-03-25 18:17:31 -070096 SkString onShortName() override {
fmalitab5f78262014-08-06 13:07:15 -070097 return SkString("pictureshadertile");
98 }
99
mtklein36352bf2015-03-25 18:17:31 -0700100 SkISize onISize() override {
fmalitab5f78262014-08-06 13:07:15 -0700101 return SkISize::Make(800, 600);
102 }
103
mtklein36352bf2015-03-25 18:17:31 -0700104 void onOnceBeforeDraw() override {
fmalitaa2bd24f2014-12-08 11:13:27 -0800105 SkPictureRecorder recorder;
106 SkCanvas* pictureCanvas = recorder.beginRecording(kPictureSize, kPictureSize);
fmalita6cd11812014-12-09 05:23:20 -0800107 draw_scene(pictureCanvas, kPictureSize);
reedca2622b2016-03-18 07:25:55 -0700108 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
fmalitaa2bd24f2014-12-08 11:13:27 -0800109
110 SkPoint offset = SkPoint::Make(100, 100);
111 pictureCanvas = recorder.beginRecording(SkRect::MakeXYWH(offset.x(), offset.y(),
112 kPictureSize, kPictureSize));
113 pictureCanvas->translate(offset.x(), offset.y());
fmalita6cd11812014-12-09 05:23:20 -0800114 draw_scene(pictureCanvas, kPictureSize);
reedca2622b2016-03-18 07:25:55 -0700115 sk_sp<SkPicture> offsetPicture(recorder.finishRecordingAsPicture());
fmalitaa2bd24f2014-12-08 11:13:27 -0800116
117 for (unsigned i = 0; i < SK_ARRAY_COUNT(tiles); ++i) {
118 SkRect tile = SkRect::MakeXYWH(tiles[i].x * kPictureSize,
119 tiles[i].y * kPictureSize,
120 tiles[i].w * kPictureSize,
121 tiles[i].h * kPictureSize);
122 SkMatrix localMatrix;
123 localMatrix.setTranslate(tiles[i].offsetX * kPictureSize,
124 tiles[i].offsetY * kPictureSize);
125 localMatrix.postScale(kFillSize / (2 * kPictureSize),
126 kFillSize / (2 * kPictureSize));
127
reed1a9b9642016-03-13 14:13:58 -0700128 sk_sp<SkPicture> pictureRef = picture;
fmalitaa2bd24f2014-12-08 11:13:27 -0800129 SkRect* tilePtr = &tile;
130
131 if (tile == SkRect::MakeWH(kPictureSize, kPictureSize)) {
132 // When the tile == picture bounds, exercise the picture + offset path.
reed1a9b9642016-03-13 14:13:58 -0700133 pictureRef = offsetPicture;
halcanary96fcdcc2015-08-27 07:41:13 -0700134 tilePtr = nullptr;
fmalitaa2bd24f2014-12-08 11:13:27 -0800135 }
136
Mike Reedfae8fce2019-04-03 10:27:45 -0400137 fShaders[i] = pictureRef->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
138 &localMatrix, tilePtr);
fmalitaa2bd24f2014-12-08 11:13:27 -0800139 }
140 }
141
mtklein36352bf2015-03-25 18:17:31 -0700142 void onDraw(SkCanvas* canvas) override {
fmalitab5f78262014-08-06 13:07:15 -0700143 canvas->clear(SK_ColorBLACK);
144
145 SkPaint paint;
146 paint.setStyle(SkPaint::kFill_Style);
147
148 for (unsigned i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
149 paint.setShader(fShaders[i]);
150
151 canvas->save();
152 canvas->translate((i % kRowSize) * kFillSize * 1.1f,
153 (i / kRowSize) * kFillSize * 1.1f);
154 canvas->drawRect(SkRect::MakeWH(kFillSize, kFillSize), paint);
155 canvas->restore();
156 }
157 }
158
159private:
reed1a9b9642016-03-13 14:13:58 -0700160 sk_sp<SkShader> fShaders[SK_ARRAY_COUNT(tiles)];
fmalitab5f78262014-08-06 13:07:15 -0700161
162 typedef GM INHERITED;
163};
164
halcanary385fe4d2015-08-26 13:07:48 -0700165DEF_GM(return new PictureShaderTileGM;)