OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "gm.h" | 8 #include "gm.h" |
9 | 9 |
10 #include "SkPaint.h" | 10 #include "SkPaint.h" |
11 #include "SkPicture.h" | 11 #include "SkPicture.h" |
12 #include "SkPictureRecorder.h" | 12 #include "SkPictureRecorder.h" |
13 #include "SkShader.h" | 13 #include "SkShader.h" |
14 | 14 |
15 static const SkScalar kPictureSize = SK_Scalar1; | 15 static const SkScalar kPictureSize = SK_Scalar1; |
16 static const SkScalar kFillSize = 100; | 16 static const SkScalar kFillSize = 100; |
17 static const unsigned kRowSize = 6; | 17 static const unsigned kRowSize = 6; |
18 | 18 |
19 static const struct { | 19 static const struct { |
20 SkScalar x, y, w, h; | 20 SkScalar x, y, w, h; |
21 SkScalar offsetX, offsetY; | 21 SkScalar offsetX, offsetY; |
22 } tiles[] = { | 22 } tiles[] = { |
23 { 0, 0, 1, 1, 0, 0 }, | 23 { 0, 0, 1, 1, 0, 0 }, |
24 { -0.5f, -0.5f, 1, 1, 0, 0 }, | |
24 { 0.5f, 0.5f, 1, 1, 0, 0 }, | 25 { 0.5f, 0.5f, 1, 1, 0, 0 }, |
25 { -0.5f, -0.5f, 1, 1, 0, 0 }, | |
26 | 26 |
27 { 0, 0, 1.5f, 1.5f, 0, 0 }, | 27 { 0, 0, 1.5f, 1.5f, 0, 0 }, |
28 { -0.5f, -0.5f, 1.5f, 1.5f, 0, 0 }, | |
28 { 0.5f, 0.5f, 1.5f, 1.5f, 0, 0 }, | 29 { 0.5f, 0.5f, 1.5f, 1.5f, 0, 0 }, |
29 { -0.5f, -0.5f, 1.5f, 1.5f, 0, 0 }, | |
30 | 30 |
31 { 0, 0, 0.5f, 0.5f, 0, 0 }, | 31 { 0, 0, 0.5f, 0.5f, 0, 0 }, |
32 { 0.25f, 0.25f, 0.5f, 0.5f, 0, 0 }, | |
32 { -0.25f, -0.25f, 0.5f, 0.5f, 0, 0 }, | 33 { -0.25f, -0.25f, 0.5f, 0.5f, 0, 0 }, |
33 { 0.25f, 0.25f, 0.5f, 0.5f, 0, 0 }, | |
34 | 34 |
35 { 0, 0, 1, 1, 0.5f, 0.5f }, | 35 { 0, 0, 1, 1, 0.5f, 0.5f }, |
36 { -0.5f, -0.5f, 1, 1, 0.5f, 0.5f }, | |
36 { 0.5f, 0.5f, 1, 1, 0.5f, 0.5f }, | 37 { 0.5f, 0.5f, 1, 1, 0.5f, 0.5f }, |
37 { -0.5f, -0.5f, 1, 1, 0.5f, 0.5f }, | |
38 | 38 |
39 { 0, 0, 1.5f, 1.5f, 0.5f, 0.5f }, | 39 { 0, 0, 1.5f, 1.5f, 0.5f, 0.5f }, |
40 { -0.5f, -0.5f, 1.5f, 1.5f, 0.5f, 0.5f }, | |
40 { 0.5f, 0.5f, 1.5f, 1.5f, 0.5f, 0.5f }, | 41 { 0.5f, 0.5f, 1.5f, 1.5f, 0.5f, 0.5f }, |
41 { -0.5f, -0.5f, 1.5f, 1.5f, 0.5f, 0.5f }, | |
42 | 42 |
43 { 0, 0, 1.5f, 1, 0, 0 }, | 43 { 0, 0, 1.5f, 1, 0, 0 }, |
44 { -0.5f, -0.5f, 1.5f, 1, 0, 0 }, | |
44 { 0.5f, 0.5f, 1.5f, 1, 0, 0 }, | 45 { 0.5f, 0.5f, 1.5f, 1, 0, 0 }, |
45 { -0.5f, -0.5f, 1.5f, 1, 0, 0 }, | |
46 | 46 |
47 { 0, 0, 0.5f, 1, 0, 0 }, | 47 { 0, 0, 0.5f, 1, 0, 0 }, |
48 { 0.25f, 0.25f, 0.5f, 1, 0, 0 }, | |
48 { -0.25f, -0.25f, 0.5f, 1, 0, 0 }, | 49 { -0.25f, -0.25f, 0.5f, 1, 0, 0 }, |
49 { 0.25f, 0.25f, 0.5f, 1, 0, 0 }, | |
50 | 50 |
51 { 0, 0, 1, 1.5f, 0, 0 }, | 51 { 0, 0, 1, 1.5f, 0, 0 }, |
52 { -0.5f, -0.5f, 1, 1.5f, 0, 0 }, | |
52 { 0.5f, 0.5f, 1, 1.5f, 0, 0 }, | 53 { 0.5f, 0.5f, 1, 1.5f, 0, 0 }, |
53 { -0.5f, -0.5f, 1, 1.5f, 0, 0 }, | |
54 | 54 |
55 { 0, 0, 1, 0.5f, 0, 0 }, | 55 { 0, 0, 1, 0.5f, 0, 0 }, |
56 { 0.25f, 0.25f, 1, 0.5f, 0, 0 }, | |
56 { -0.25f, -0.25f, 1, 0.5f, 0, 0 }, | 57 { -0.25f, -0.25f, 1, 0.5f, 0, 0 }, |
57 { 0.25f, 0.25f, 1, 0.5f, 0, 0 }, | |
58 }; | 58 }; |
59 | 59 |
60 class PictureShaderTileGM : public skiagm::GM { | 60 class PictureShaderTileGM : public skiagm::GM { |
61 public: | 61 protected: |
robertphillips
2014/12/08 20:01:31
rm these virtual keywords?
| |
62 PictureShaderTileGM() { | 62 virtual uint32_t onGetFlags() const SK_OVERRIDE { |
63 return kSkipTiled_Flag; | |
64 } | |
65 | |
66 virtual SkString onShortName() SK_OVERRIDE { | |
67 return SkString("pictureshadertile"); | |
68 } | |
69 | |
70 virtual SkISize onISize() SK_OVERRIDE { | |
71 return SkISize::Make(800, 600); | |
72 } | |
73 | |
74 virtual void onOnceBeforeDraw() SK_OVERRIDE { | |
63 SkPictureRecorder recorder; | 75 SkPictureRecorder recorder; |
64 SkCanvas* pictureCanvas = recorder.beginRecording(kPictureSize, kPicture Size, NULL, 0); | 76 SkCanvas* pictureCanvas = recorder.beginRecording(kPictureSize, kPicture Size); |
65 drawScene(pictureCanvas, kPictureSize); | 77 drawScene(pictureCanvas, kPictureSize); |
66 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); | 78 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
67 | 79 |
80 SkPoint offset = SkPoint::Make(100, 100); | |
81 pictureCanvas = recorder.beginRecording(SkRect::MakeXYWH(offset.x(), off set.y(), | |
82 kPictureSize, k PictureSize)); | |
83 pictureCanvas->translate(offset.x(), offset.y()); | |
84 drawScene(pictureCanvas, kPictureSize); | |
85 SkAutoTUnref<SkPicture> offsetPicture(recorder.endRecording()); | |
86 | |
68 for (unsigned i = 0; i < SK_ARRAY_COUNT(tiles); ++i) { | 87 for (unsigned i = 0; i < SK_ARRAY_COUNT(tiles); ++i) { |
69 SkRect tile = SkRect::MakeXYWH(tiles[i].x * kPictureSize, | 88 SkRect tile = SkRect::MakeXYWH(tiles[i].x * kPictureSize, |
70 tiles[i].y * kPictureSize, | 89 tiles[i].y * kPictureSize, |
71 tiles[i].w * kPictureSize, | 90 tiles[i].w * kPictureSize, |
72 tiles[i].h * kPictureSize); | 91 tiles[i].h * kPictureSize); |
73 SkMatrix localMatrix; | 92 SkMatrix localMatrix; |
74 localMatrix.setTranslate(tiles[i].offsetX * kPictureSize, | 93 localMatrix.setTranslate(tiles[i].offsetX * kPictureSize, |
75 tiles[i].offsetY * kPictureSize); | 94 tiles[i].offsetY * kPictureSize); |
76 localMatrix.postScale(kFillSize / (2 * kPictureSize), | 95 localMatrix.postScale(kFillSize / (2 * kPictureSize), |
77 kFillSize / (2 * kPictureSize)); | 96 kFillSize / (2 * kPictureSize)); |
78 fShaders[i].reset(SkShader::CreatePictureShader(picture, | 97 |
98 SkPicture* picturePtr = picture.get(); | |
99 SkRect* tilePtr = &tile; | |
100 | |
101 if (tile == SkRect::MakeWH(kPictureSize, kPictureSize)) { | |
102 // When the tile == picture bounds, exercise the picture + offse t path. | |
103 picturePtr = offsetPicture.get(); | |
104 tilePtr = NULL; | |
105 } | |
106 | |
107 fShaders[i].reset(SkShader::CreatePictureShader(picturePtr, | |
robertphillips
2014/12/08 20:01:31
tab these guys over?
| |
79 SkShader::kRepeat_TileMode, | 108 SkShader::kRepeat_TileMode, |
80 SkShader::kRepeat_TileMode, | 109 SkShader::kRepeat_TileMode, |
81 &localMatrix, | 110 &localMatrix, |
82 &tile)); | 111 tilePtr)); |
83 } | 112 } |
84 } | 113 } |
85 | 114 |
86 protected: | |
87 virtual uint32_t onGetFlags() const SK_OVERRIDE { | |
88 return kSkipTiled_Flag; | |
89 } | |
90 | |
91 virtual SkString onShortName() SK_OVERRIDE { | |
92 return SkString("pictureshadertile"); | |
93 } | |
94 | |
95 virtual SkISize onISize() SK_OVERRIDE { | |
96 return SkISize::Make(800, 600); | |
97 } | |
98 | |
99 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { | 115 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
100 canvas->clear(SK_ColorBLACK); | 116 canvas->clear(SK_ColorBLACK); |
101 | 117 |
102 SkPaint paint; | 118 SkPaint paint; |
103 paint.setStyle(SkPaint::kFill_Style); | 119 paint.setStyle(SkPaint::kFill_Style); |
104 | 120 |
105 for (unsigned i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) { | 121 for (unsigned i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) { |
106 paint.setShader(fShaders[i]); | 122 paint.setShader(fShaders[i]); |
107 | 123 |
108 canvas->save(); | 124 canvas->save(); |
109 canvas->translate((i % kRowSize) * kFillSize * 1.1f, | 125 canvas->translate((i % kRowSize) * kFillSize * 1.1f, |
110 (i / kRowSize) * kFillSize * 1.1f); | 126 (i / kRowSize) * kFillSize * 1.1f); |
111 canvas->drawRect(SkRect::MakeWH(kFillSize, kFillSize), paint); | 127 canvas->drawRect(SkRect::MakeWH(kFillSize, kFillSize), paint); |
112 canvas->restore(); | 128 canvas->restore(); |
113 } | 129 } |
114 } | 130 } |
115 | 131 |
116 private: | 132 private: |
robertphillips
2014/12/08 20:01:31
can this be: static void draw_scene ?
Otherwise th
| |
117 void drawScene(SkCanvas* canvas, SkScalar pictureSize) { | 133 void drawScene(SkCanvas* canvas, SkScalar pictureSize) { |
118 canvas->clear(SK_ColorWHITE); | 134 canvas->clear(SK_ColorWHITE); |
119 | 135 |
120 SkPaint paint; | 136 SkPaint paint; |
121 paint.setColor(SK_ColorGREEN); | 137 paint.setColor(SK_ColorGREEN); |
122 paint.setStyle(SkPaint::kFill_Style); | 138 paint.setStyle(SkPaint::kFill_Style); |
123 paint.setAntiAlias(true); | 139 paint.setAntiAlias(true); |
124 | 140 |
125 canvas->drawCircle(pictureSize / 4, pictureSize / 4, pictureSize / 4, pa int); | 141 canvas->drawCircle(pictureSize / 4, pictureSize / 4, pictureSize / 4, pa int); |
126 canvas->drawRect(SkRect::MakeXYWH(pictureSize / 2, pictureSize / 2, | 142 canvas->drawRect(SkRect::MakeXYWH(pictureSize / 2, pictureSize / 2, |
127 pictureSize / 2, pictureSize / 2), pai nt); | 143 pictureSize / 2, pictureSize / 2), pai nt); |
128 | 144 |
129 paint.setColor(SK_ColorRED); | 145 paint.setColor(SK_ColorRED); |
130 canvas->drawLine(pictureSize / 2, pictureSize * 1 / 3, | 146 canvas->drawLine(pictureSize / 2, pictureSize * 1 / 3, |
131 pictureSize / 2, pictureSize * 2 / 3, paint); | 147 pictureSize / 2, pictureSize * 2 / 3, paint); |
132 canvas->drawLine(pictureSize * 1 / 3, pictureSize / 2, | 148 canvas->drawLine(pictureSize * 1 / 3, pictureSize / 2, |
133 pictureSize * 2 / 3, pictureSize / 2, paint); | 149 pictureSize * 2 / 3, pictureSize / 2, paint); |
134 | 150 |
135 paint.setColor(SK_ColorBLACK); | 151 paint.setColor(SK_ColorBLACK); |
136 paint.setStyle(SkPaint::kStroke_Style); | 152 paint.setStyle(SkPaint::kStroke_Style); |
137 canvas->drawRect(SkRect::MakeWH(pictureSize, pictureSize), paint); | 153 canvas->drawRect(SkRect::MakeWH(pictureSize, pictureSize), paint); |
138 } | 154 } |
139 | 155 |
140 SkAutoTUnref<SkShader> fShaders[SK_ARRAY_COUNT(tiles)]; | 156 SkAutoTUnref<SkShader> fShaders[SK_ARRAY_COUNT(tiles)]; |
141 | 157 |
142 typedef GM INHERITED; | 158 typedef GM INHERITED; |
143 }; | 159 }; |
144 | 160 |
145 DEF_GM( return SkNEW(PictureShaderTileGM); ) | 161 DEF_GM( return SkNEW(PictureShaderTileGM); ) |
OLD | NEW |