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 | |
| 8 | #include "gm.h" |
| 9 | |
| 10 | #include "SkPaint.h" |
| 11 | #include "SkPicture.h" |
| 12 | #include "SkPictureRecorder.h" |
| 13 | #include "SkShader.h" |
| 14 | |
| 15 | static const SkScalar kPictureSize = SK_Scalar1; |
| 16 | static const SkScalar kFillSize = 100; |
| 17 | static const unsigned kRowSize = 6; |
| 18 | |
| 19 | static const struct { |
| 20 | SkScalar x, y, w, h; |
| 21 | SkScalar offsetX, offsetY; |
| 22 | } tiles[] = { |
| 23 | { 0, 0, 1, 1, 0, 0 }, |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 24 | { -0.5f, -0.5f, 1, 1, 0, 0 }, |
fmalita | a2bd24f | 2014-12-08 11:13:27 -0800 | [diff] [blame] | 25 | { 0.5f, 0.5f, 1, 1, 0, 0 }, |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 26 | |
| 27 | { 0, 0, 1.5f, 1.5f, 0, 0 }, |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 28 | { -0.5f, -0.5f, 1.5f, 1.5f, 0, 0 }, |
fmalita | a2bd24f | 2014-12-08 11:13:27 -0800 | [diff] [blame] | 29 | { 0.5f, 0.5f, 1.5f, 1.5f, 0, 0 }, |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 30 | |
| 31 | { 0, 0, 0.5f, 0.5f, 0, 0 }, |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 32 | { 0.25f, 0.25f, 0.5f, 0.5f, 0, 0 }, |
fmalita | a2bd24f | 2014-12-08 11:13:27 -0800 | [diff] [blame] | 33 | { -0.25f, -0.25f, 0.5f, 0.5f, 0, 0 }, |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 34 | |
| 35 | { 0, 0, 1, 1, 0.5f, 0.5f }, |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 36 | { -0.5f, -0.5f, 1, 1, 0.5f, 0.5f }, |
fmalita | a2bd24f | 2014-12-08 11:13:27 -0800 | [diff] [blame] | 37 | { 0.5f, 0.5f, 1, 1, 0.5f, 0.5f }, |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 38 | |
| 39 | { 0, 0, 1.5f, 1.5f, 0.5f, 0.5f }, |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 40 | { -0.5f, -0.5f, 1.5f, 1.5f, 0.5f, 0.5f }, |
fmalita | a2bd24f | 2014-12-08 11:13:27 -0800 | [diff] [blame] | 41 | { 0.5f, 0.5f, 1.5f, 1.5f, 0.5f, 0.5f }, |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 42 | |
| 43 | { 0, 0, 1.5f, 1, 0, 0 }, |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 44 | { -0.5f, -0.5f, 1.5f, 1, 0, 0 }, |
fmalita | a2bd24f | 2014-12-08 11:13:27 -0800 | [diff] [blame] | 45 | { 0.5f, 0.5f, 1.5f, 1, 0, 0 }, |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 46 | |
| 47 | { 0, 0, 0.5f, 1, 0, 0 }, |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 48 | { 0.25f, 0.25f, 0.5f, 1, 0, 0 }, |
fmalita | a2bd24f | 2014-12-08 11:13:27 -0800 | [diff] [blame] | 49 | { -0.25f, -0.25f, 0.5f, 1, 0, 0 }, |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 50 | |
| 51 | { 0, 0, 1, 1.5f, 0, 0 }, |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 52 | { -0.5f, -0.5f, 1, 1.5f, 0, 0 }, |
fmalita | a2bd24f | 2014-12-08 11:13:27 -0800 | [diff] [blame] | 53 | { 0.5f, 0.5f, 1, 1.5f, 0, 0 }, |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 54 | |
| 55 | { 0, 0, 1, 0.5f, 0, 0 }, |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 56 | { 0.25f, 0.25f, 1, 0.5f, 0, 0 }, |
fmalita | a2bd24f | 2014-12-08 11:13:27 -0800 | [diff] [blame] | 57 | { -0.25f, -0.25f, 1, 0.5f, 0, 0 }, |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 58 | }; |
| 59 | |
fmalita | 6cd1181 | 2014-12-09 05:23:20 -0800 | [diff] [blame] | 60 | static 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 | |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 83 | class PictureShaderTileGM : public skiagm::GM { |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 84 | protected: |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 85 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 86 | SkString onShortName() override { |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 87 | return SkString("pictureshadertile"); |
| 88 | } |
| 89 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 90 | SkISize onISize() override { |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 91 | return SkISize::Make(800, 600); |
| 92 | } |
| 93 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 94 | void onOnceBeforeDraw() override { |
fmalita | a2bd24f | 2014-12-08 11:13:27 -0800 | [diff] [blame] | 95 | SkPictureRecorder recorder; |
| 96 | SkCanvas* pictureCanvas = recorder.beginRecording(kPictureSize, kPictureSize); |
fmalita | 6cd1181 | 2014-12-09 05:23:20 -0800 | [diff] [blame] | 97 | draw_scene(pictureCanvas, kPictureSize); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 98 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
fmalita | a2bd24f | 2014-12-08 11:13:27 -0800 | [diff] [blame] | 99 | |
| 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()); |
fmalita | 6cd1181 | 2014-12-09 05:23:20 -0800 | [diff] [blame] | 104 | draw_scene(pictureCanvas, kPictureSize); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 105 | sk_sp<SkPicture> offsetPicture(recorder.finishRecordingAsPicture()); |
fmalita | a2bd24f | 2014-12-08 11:13:27 -0800 | [diff] [blame] | 106 | |
| 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 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 118 | sk_sp<SkPicture> pictureRef = picture; |
fmalita | a2bd24f | 2014-12-08 11:13:27 -0800 | [diff] [blame] | 119 | SkRect* tilePtr = &tile; |
| 120 | |
| 121 | if (tile == SkRect::MakeWH(kPictureSize, kPictureSize)) { |
| 122 | // When the tile == picture bounds, exercise the picture + offset path. |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 123 | pictureRef = offsetPicture; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 124 | tilePtr = nullptr; |
fmalita | a2bd24f | 2014-12-08 11:13:27 -0800 | [diff] [blame] | 125 | } |
| 126 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 127 | fShaders[i] = SkShader::MakePictureShader(pictureRef, SkShader::kRepeat_TileMode, |
| 128 | SkShader::kRepeat_TileMode, &localMatrix, |
| 129 | tilePtr); |
fmalita | a2bd24f | 2014-12-08 11:13:27 -0800 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 133 | void onDraw(SkCanvas* canvas) override { |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 134 | 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 | |
| 150 | private: |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 151 | sk_sp<SkShader> fShaders[SK_ARRAY_COUNT(tiles)]; |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 152 | |
| 153 | typedef GM INHERITED; |
| 154 | }; |
| 155 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 156 | DEF_GM(return new PictureShaderTileGM;) |