fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 9 | #include "include/core/SkCanvas.h" |
| 10 | #include "include/core/SkColor.h" |
| 11 | #include "include/core/SkMatrix.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/core/SkPaint.h" |
| 13 | #include "include/core/SkPicture.h" |
| 14 | #include "include/core/SkPictureRecorder.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 15 | #include "include/core/SkPoint.h" |
| 16 | #include "include/core/SkRect.h" |
| 17 | #include "include/core/SkRefCnt.h" |
| 18 | #include "include/core/SkScalar.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "include/core/SkShader.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 20 | #include "include/core/SkSize.h" |
| 21 | #include "include/core/SkString.h" |
| 22 | #include "include/core/SkTileMode.h" |
| 23 | #include "include/core/SkTypes.h" |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 24 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 25 | constexpr SkScalar kPictureSize = SK_Scalar1; |
| 26 | constexpr SkScalar kFillSize = 100; |
| 27 | constexpr unsigned kRowSize = 6; |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 28 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 29 | constexpr struct { |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 30 | SkScalar x, y, w, h; |
| 31 | SkScalar offsetX, offsetY; |
| 32 | } tiles[] = { |
| 33 | { 0, 0, 1, 1, 0, 0 }, |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 34 | { -0.5f, -0.5f, 1, 1, 0, 0 }, |
fmalita | a2bd24f | 2014-12-08 11:13:27 -0800 | [diff] [blame] | 35 | { 0.5f, 0.5f, 1, 1, 0, 0 }, |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 36 | |
| 37 | { 0, 0, 1.5f, 1.5f, 0, 0 }, |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 38 | { -0.5f, -0.5f, 1.5f, 1.5f, 0, 0 }, |
fmalita | a2bd24f | 2014-12-08 11:13:27 -0800 | [diff] [blame] | 39 | { 0.5f, 0.5f, 1.5f, 1.5f, 0, 0 }, |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 40 | |
| 41 | { 0, 0, 0.5f, 0.5f, 0, 0 }, |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 42 | { 0.25f, 0.25f, 0.5f, 0.5f, 0, 0 }, |
fmalita | a2bd24f | 2014-12-08 11:13:27 -0800 | [diff] [blame] | 43 | { -0.25f, -0.25f, 0.5f, 0.5f, 0, 0 }, |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 44 | |
| 45 | { 0, 0, 1, 1, 0.5f, 0.5f }, |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 46 | { -0.5f, -0.5f, 1, 1, 0.5f, 0.5f }, |
fmalita | a2bd24f | 2014-12-08 11:13:27 -0800 | [diff] [blame] | 47 | { 0.5f, 0.5f, 1, 1, 0.5f, 0.5f }, |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 48 | |
| 49 | { 0, 0, 1.5f, 1.5f, 0.5f, 0.5f }, |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 50 | { -0.5f, -0.5f, 1.5f, 1.5f, 0.5f, 0.5f }, |
fmalita | a2bd24f | 2014-12-08 11:13:27 -0800 | [diff] [blame] | 51 | { 0.5f, 0.5f, 1.5f, 1.5f, 0.5f, 0.5f }, |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 52 | |
| 53 | { 0, 0, 1.5f, 1, 0, 0 }, |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 54 | { -0.5f, -0.5f, 1.5f, 1, 0, 0 }, |
fmalita | a2bd24f | 2014-12-08 11:13:27 -0800 | [diff] [blame] | 55 | { 0.5f, 0.5f, 1.5f, 1, 0, 0 }, |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 56 | |
| 57 | { 0, 0, 0.5f, 1, 0, 0 }, |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 58 | { 0.25f, 0.25f, 0.5f, 1, 0, 0 }, |
fmalita | a2bd24f | 2014-12-08 11:13:27 -0800 | [diff] [blame] | 59 | { -0.25f, -0.25f, 0.5f, 1, 0, 0 }, |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 60 | |
| 61 | { 0, 0, 1, 1.5f, 0, 0 }, |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 62 | { -0.5f, -0.5f, 1, 1.5f, 0, 0 }, |
fmalita | a2bd24f | 2014-12-08 11:13:27 -0800 | [diff] [blame] | 63 | { 0.5f, 0.5f, 1, 1.5f, 0, 0 }, |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 64 | |
| 65 | { 0, 0, 1, 0.5f, 0, 0 }, |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 66 | { 0.25f, 0.25f, 1, 0.5f, 0, 0 }, |
fmalita | a2bd24f | 2014-12-08 11:13:27 -0800 | [diff] [blame] | 67 | { -0.25f, -0.25f, 1, 0.5f, 0, 0 }, |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 68 | }; |
| 69 | |
fmalita | 6cd1181 | 2014-12-09 05:23:20 -0800 | [diff] [blame] | 70 | static 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 | |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 93 | class PictureShaderTileGM : public skiagm::GM { |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 94 | protected: |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 95 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 96 | SkString onShortName() override { |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 97 | return SkString("pictureshadertile"); |
| 98 | } |
| 99 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 100 | SkISize onISize() override { |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 101 | return SkISize::Make(800, 600); |
| 102 | } |
| 103 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 104 | void onOnceBeforeDraw() override { |
fmalita | a2bd24f | 2014-12-08 11:13:27 -0800 | [diff] [blame] | 105 | SkPictureRecorder recorder; |
| 106 | SkCanvas* pictureCanvas = recorder.beginRecording(kPictureSize, kPictureSize); |
fmalita | 6cd1181 | 2014-12-09 05:23:20 -0800 | [diff] [blame] | 107 | draw_scene(pictureCanvas, kPictureSize); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 108 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
fmalita | a2bd24f | 2014-12-08 11:13:27 -0800 | [diff] [blame] | 109 | |
| 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()); |
fmalita | 6cd1181 | 2014-12-09 05:23:20 -0800 | [diff] [blame] | 114 | draw_scene(pictureCanvas, kPictureSize); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 115 | sk_sp<SkPicture> offsetPicture(recorder.finishRecordingAsPicture()); |
fmalita | a2bd24f | 2014-12-08 11:13:27 -0800 | [diff] [blame] | 116 | |
| 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 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 128 | sk_sp<SkPicture> pictureRef = picture; |
fmalita | a2bd24f | 2014-12-08 11:13:27 -0800 | [diff] [blame] | 129 | SkRect* tilePtr = &tile; |
| 130 | |
| 131 | if (tile == SkRect::MakeWH(kPictureSize, kPictureSize)) { |
| 132 | // When the tile == picture bounds, exercise the picture + offset path. |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 133 | pictureRef = offsetPicture; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 134 | tilePtr = nullptr; |
fmalita | a2bd24f | 2014-12-08 11:13:27 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 137 | fShaders[i] = pictureRef->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, |
| 138 | &localMatrix, tilePtr); |
fmalita | a2bd24f | 2014-12-08 11:13:27 -0800 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 142 | void onDraw(SkCanvas* canvas) override { |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 143 | 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 | |
| 159 | private: |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 160 | sk_sp<SkShader> fShaders[SK_ARRAY_COUNT(tiles)]; |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 161 | |
| 162 | typedef GM INHERITED; |
| 163 | }; |
| 164 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 165 | DEF_GM(return new PictureShaderTileGM;) |