Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(720)

Side by Side Diff: gm/multipicturedraw.cpp

Issue 491313003: SkMultiPictureDraw API (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: fix clang complaint Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | gyp/core.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 "SkColorFilter.h"
11 #include "SkMultiPictureDraw.h"
12 #include "SkPictureRecorder.h"
13 #include "SkSurface.h"
14
15 static const SkScalar kRoot3Over2 = 0.86602545f; // sin(60)
16
17 static const int kHexSide = 30;
18 static const int kNumHexX = 6;
19 static const int kNumHexY = 6;
20 static const int kPicWidth = kNumHexX * kHexSide;
21 static const int kPicHeight = SkScalarCeilToInt((kNumHexY - 0.5f) * 2 * kHexSide * kRoot3Over2);
22 static const SkScalar kInset = 20.0f;
23
24 // Create a hexagon centered at (originX, originY)
25 static SkPath make_hex_path(SkScalar originX, SkScalar originY) {
26 SkPath hex;
27 hex.moveTo(originX-kHexSide, originY);
28 hex.rLineTo(SkScalarHalf(kHexSide), kRoot3Over2 * kHexSide);
29 hex.rLineTo(SkIntToScalar(kHexSide), 0);
30 hex.rLineTo(SkScalarHalf(kHexSide), -kHexSide * kRoot3Over2);
31 hex.rLineTo(-SkScalarHalf(kHexSide), -kHexSide * kRoot3Over2);
32 hex.rLineTo(-SkIntToScalar(kHexSide), 0);
33 hex.close();
34 return hex;
35 }
36
37 // Make a picture that is a tiling of the plane with stroked hexagons where
38 // each hexagon is in its own layer. The layers are to exercise Ganesh's
39 // layer hoisting.
40 static const SkPicture* make_picture(SkColor fillColor) {
41
42 // Create a hexagon with its center at the origin
43 SkPath hex = make_hex_path(0, 0);
44
45 SkPaint fill;
46 fill.setStyle(SkPaint::kFill_Style);
47 fill.setColor(fillColor);
48
49 SkPaint stroke;
50 stroke.setStyle(SkPaint::kStroke_Style);
51 stroke.setStrokeWidth(3);
52
53 SkPictureRecorder recorder;
54
55 SkCanvas* canvas = recorder.beginRecording(kPicWidth, kPicHeight);
56
57 SkScalar xPos, yPos = 0;
58
59 for (int y = 0; y < kNumHexY; ++y) {
60 xPos = 0;
61
62 for (int x = 0; x < kNumHexX; ++x) {
63 canvas->saveLayer(NULL, NULL);
64 canvas->translate(xPos, yPos + ((x % 2) ? kRoot3Over2 * kHexSide : 0 ));
65 canvas->drawPath(hex, fill);
66 canvas->drawPath(hex, stroke);
67 canvas->restore();
68
69 xPos += 1.5f * kHexSide;
70 }
71
72 yPos += 2 * kHexSide * kRoot3Over2;
73 }
74
75 return recorder.endRecording();
76 }
77
78 static SkSurface* compat_surface(SkCanvas* canvas, int width, int height) {
79 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
80
81 SkSurface* surface = canvas->newSurface(info);
82 if (NULL == surface) {
83 // picture canvas returns NULL so fall back to raster
84 surface = SkSurface::NewRaster(info);
85 }
86
87 return surface;
88 }
89
90 // This class stores the information required to compose all the result
91 // fragments potentially generated by the MultiPictureDraw object
92 class ComposeStep {
93 public:
94 ComposeStep() : fSurf(NULL), fX(0.0f), fY(0.0f), fPaint(NULL) { }
95 ~ComposeStep() { SkSafeUnref(fSurf); SkDELETE(fPaint); }
96
97 SkSurface* fSurf;
98 SkScalar fX;
99 SkScalar fY;
100 SkPaint* fPaint;
101 };
102
103 typedef void (*PFContentMtd)(SkCanvas* canvas, const SkPicture* pictures[2]);
104
105 // Just a single picture with no clip
106 static void no_clip(SkCanvas* canvas, const SkPicture* pictures[2]) {
107 canvas->drawPicture(pictures[0]);
108 }
109
110 // Two pictures with a rect clip on the second one
111 static void rect_clip(SkCanvas* canvas, const SkPicture* pictures[2]) {
112 canvas->drawPicture(pictures[0]);
113
114 SkRect rect = SkRect::MakeWH(SkIntToScalar(kPicWidth), SkIntToScalar(kPicHei ght));
115 rect.inset(kInset, kInset);
116
117 canvas->clipRect(rect);
118
119 canvas->drawPicture(pictures[1]);
120 }
121
122 // Two pictures with a round rect clip on the second one
123 static void rrect_clip(SkCanvas* canvas, const SkPicture* pictures[2]) {
124 canvas->drawPicture(pictures[0]);
125
126 SkRect rect = SkRect::MakeWH(SkIntToScalar(kPicWidth), SkIntToScalar(kPicHei ght));
127 rect.inset(kInset, kInset);
128
129 SkRRect rrect;
130 rrect.setRectXY(rect, kInset, kInset);
131
132 canvas->clipRRect(rrect);
133
134 canvas->drawPicture(pictures[1]);
135 }
136
137 // Two pictures with a clip path on the second one
138 static void path_clip(SkCanvas* canvas, const SkPicture* pictures[2]) {
139 canvas->drawPicture(pictures[0]);
140
141 // Create a hexagon centered on the middle of the hex grid
142 SkPath hex = make_hex_path((kNumHexX / 2.0f) * kHexSide, kNumHexY * kHexSide * kRoot3Over2);
143
144 canvas->clipPath(hex);
145
146 canvas->drawPicture(pictures[1]);
147 }
148
149 // Two pictures with an inverse clip path on the second one
150 static void invpath_clip(SkCanvas* canvas, const SkPicture* pictures[2]) {
151 canvas->drawPicture(pictures[0]);
152
153 // Create a hexagon centered on the middle of the hex grid
154 SkPath hex = make_hex_path((kNumHexX / 2.0f) * kHexSide, kNumHexY * kHexSide * kRoot3Over2);
155 hex.setFillType(SkPath::kInverseEvenOdd_FillType);
156
157 canvas->clipPath(hex);
158
159 canvas->drawPicture(pictures[1]);
160 }
161
162 static const PFContentMtd gContentMthds[] = {
163 no_clip,
164 rect_clip,
165 rrect_clip,
166 path_clip,
167 invpath_clip
168 };
169
170 static void create_content(SkMultiPictureDraw* mpd, PFContentMtd pfGen,
171 const SkPicture* pictures[2],
172 SkCanvas* dest, const SkMatrix& xform) {
173 SkAutoTUnref<SkPicture> composite;
174
175 {
176 SkPictureRecorder recorder;
177
178 SkCanvas* pictureCanvas = recorder.beginRecording(kPicWidth, kPicHeight) ;
179
180 (*pfGen)(pictureCanvas, pictures);
181
182 composite.reset(recorder.endRecording());
183 }
184
185 mpd->add(dest, composite, &xform);
186 }
187
188 typedef void(*PFLayoutMtd)(SkCanvas* finalCanvas, SkMultiPictureDraw* mpd,
189 PFContentMtd pfGen, const SkPicture* pictures[2],
190 SkTArray<ComposeStep>* composeSteps);
191
192 // Draw the content into a single canvas
193 static void simple(SkCanvas* finalCanvas, SkMultiPictureDraw* mpd,
194 PFContentMtd pfGen,
195 const SkPicture* pictures[2],
196 SkTArray<ComposeStep> *composeSteps) {
197
198 ComposeStep& step = composeSteps->push_back();
199
200 step.fSurf = SkSafeRef(compat_surface(finalCanvas, kPicWidth, kPicHeight));
201
202 SkCanvas* subCanvas = step.fSurf->getCanvas();
203
204 create_content(mpd, pfGen, pictures, subCanvas, SkMatrix::I());
205 }
206
207 // Draw the content into multiple canvases/tiles
208 static void tiled(SkCanvas* finalCanvas, SkMultiPictureDraw* mpd,
209 PFContentMtd pfGen,
210 const SkPicture* pictures[2],
211 SkTArray<ComposeStep> *composeSteps) {
212 static const int kNumTilesX = 2;
213 static const int kNumTilesY = 2;
214 static const int kTileWidth = kPicWidth / kNumTilesX;
215 static const int kTileHeight = kPicHeight / kNumTilesY;
216
217 SkASSERT(kPicWidth == kNumTilesX * kTileWidth);
218 SkASSERT(kPicHeight == kNumTilesY * kTileHeight);
219
220 static const SkColor colors[kNumTilesX][kNumTilesY] = {
221 { SK_ColorCYAN, SK_ColorMAGENTA },
222 { SK_ColorYELLOW, SK_ColorGREEN }
223 };
224
225 for (int y = 0; y < kNumTilesY; ++y) {
226 for (int x = 0; x < kNumTilesX; ++x) {
227 ComposeStep& step = composeSteps->push_back();
228
229 step.fX = SkIntToScalar(x*kTileWidth);
230 step.fY = SkIntToScalar(y*kTileHeight);
231 step.fPaint = SkNEW(SkPaint);
232 step.fPaint->setColorFilter(
233 SkColorFilter::CreateModeFilter(colors[x][y], SkXfermode::kModul ate_Mode))->unref();
234
235 step.fSurf = SkSafeRef(compat_surface(finalCanvas, kTileWidth, kTile Height));
236
237 SkCanvas* subCanvas = step.fSurf->getCanvas();
238
239 SkMatrix trans;
240 trans.setTranslate(-SkIntToScalar(x*kTileWidth), -SkIntToScalar(y*kT ileHeight));
241
242 create_content(mpd, pfGen, pictures, subCanvas, trans);
243 }
244 }
245 }
246
247 static const PFLayoutMtd gLayoutMthds[] = { simple, tiled };
248
249 namespace skiagm {
250 /**
251 * This GM exercises the SkMultiPictureDraw object. It tests the
252 * cross product of:
253 * tiled vs. all-at-once rendering (e.g., into many or just 1 canvas)
254 * different clips (e.g., none, rect, rrect)
255 * single vs. multiple pictures (e.g., normal vs. picture-pile-style co ntent)
256 */
257 class MultiPictureDraw : public GM {
258 public:
259 enum Content {
260 kNoClipSingle_Content,
261 kRectClipMulti_Content,
262 kRRectClipMulti_Content,
263 kPathClipMulti_Content,
264 kInvPathClipMulti_Content,
265
266 kLast_Content = kInvPathClipMulti_Content
267 };
268
269 static const int kContentCnt = kLast_Content + 1;
270
271 enum Layout {
272 kSimple_Layout,
273 kTiled_Layout,
274
275 kLast_Layout = kTiled_Layout
276 };
277
278 static const int kLayoutCnt = kLast_Layout + 1;
279
280 MultiPictureDraw(Content content, Layout layout) : fContent(content), fL ayout(layout) {
281 SkASSERT(SK_ARRAY_COUNT(gLayoutMthds) == kLayoutCnt);
282 SkASSERT(SK_ARRAY_COUNT(gContentMthds) == kContentCnt);
283
284 fPictures[0] = fPictures[1] = NULL;
285 }
286
287 virtual ~MultiPictureDraw() {
288 SkSafeUnref(fPictures[0]);
289 SkSafeUnref(fPictures[1]);
290 }
291
292 protected:
293 Content fContent;
294 Layout fLayout;
295 const SkPicture* fPictures[2];
296
297 virtual void onOnceBeforeDraw() SK_OVERRIDE {
298 fPictures[0] = SkRef(make_picture(SK_ColorWHITE));
299 fPictures[1] = SkRef(make_picture(SK_ColorGRAY));
300 }
301
302 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE{
303 SkMultiPictureDraw mpd;
304 SkTArray<ComposeStep> composeSteps;
305
306 // Fill up the MultiPictureDraw
307 (*gLayoutMthds[fLayout])(canvas, &mpd,
308 gContentMthds[fContent],
309 fPictures, &composeSteps);
310
311 mpd.draw();
312
313 // Compose all the drawn canvases into the final canvas
314 for (int i = 0; i < composeSteps.count(); ++i) {
315 const ComposeStep& step = composeSteps[i];
316
317 SkAutoTUnref<SkImage> image(step.fSurf->newImageSnapshot());
318
319 image->draw(canvas, step.fX, step.fY, step.fPaint);
320 }
321 }
322
323 virtual SkISize onISize() SK_OVERRIDE{ return SkISize::Make(kPicWidth, k PicHeight); }
324
325 virtual SkString onShortName() SK_OVERRIDE{
326 static const char* gContentNames[] = {
327 "noclip", "rectclip", "rrectclip", "pathclip", "invpathclip"
328 };
329 static const char* gLayoutNames[] = { "simple", "tiled" };
330
331 SkASSERT(SK_ARRAY_COUNT(gLayoutNames) == kLayoutCnt);
332 SkASSERT(SK_ARRAY_COUNT(gContentNames) == kContentCnt);
333
334 SkString name("multipicturedraw_");
335
336 name.append(gContentNames[fContent]);
337 name.append("_");
338 name.append(gLayoutNames[fLayout]);
339 return name;
340 }
341
342 virtual uint32_t onGetFlags() const SK_OVERRIDE { return kAsBench_Flag | kSkipTiled_Flag; }
343
344 private:
345 typedef GM INHERITED;
346 };
347
348 DEF_GM(return SkNEW_ARGS(MultiPictureDraw, (MultiPictureDraw::kNoClipSingle_ Content,
349 MultiPictureDraw::kSimple_Layout ));)
350 DEF_GM(return SkNEW_ARGS(MultiPictureDraw, (MultiPictureDraw::kRectClipMulti _Content,
351 MultiPictureDraw::kSimple_Layout ));)
352 DEF_GM(return SkNEW_ARGS(MultiPictureDraw, (MultiPictureDraw::kRRectClipMult i_Content,
353 MultiPictureDraw::kSimple_Layout ));)
354 DEF_GM(return SkNEW_ARGS(MultiPictureDraw, (MultiPictureDraw::kPathClipMulti _Content,
355 MultiPictureDraw::kSimple_Layout ));)
356 DEF_GM(return SkNEW_ARGS(MultiPictureDraw, (MultiPictureDraw::kInvPathClipMu lti_Content,
357 MultiPictureDraw::kSimple_Layout ));)
358
359 DEF_GM(return SkNEW_ARGS(MultiPictureDraw, (MultiPictureDraw::kNoClipSingle_ Content,
360 MultiPictureDraw::kTiled_Layout) );)
361 DEF_GM(return SkNEW_ARGS(MultiPictureDraw, (MultiPictureDraw::kRectClipMulti _Content,
362 MultiPictureDraw::kTiled_Layout) );)
363 DEF_GM(return SkNEW_ARGS(MultiPictureDraw, (MultiPictureDraw::kRRectClipMult i_Content,
364 MultiPictureDraw::kTiled_Layout) );)
365 DEF_GM(return SkNEW_ARGS(MultiPictureDraw, (MultiPictureDraw::kPathClipMulti _Content,
366 MultiPictureDraw::kTiled_Layout) );)
367 DEF_GM(return SkNEW_ARGS(MultiPictureDraw, (MultiPictureDraw::kInvPathClipMu lti_Content,
368 MultiPictureDraw::kTiled_Layout) );)
369 }
OLDNEW
« no previous file with comments | « no previous file | gyp/core.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698