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

Unified Diff: gm/multipicturedraw.cpp

Issue 491313003: SkMultiPictureDraw API (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | gyp/core.gypi » ('j') | include/core/SkMultiPictureDraw.h » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: gm/multipicturedraw.cpp
diff --git a/gm/multipicturedraw.cpp b/gm/multipicturedraw.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..199a2b7bb0bcf9b3fa752ad55118da1186c175ed
--- /dev/null
+++ b/gm/multipicturedraw.cpp
@@ -0,0 +1,295 @@
+/*
+ * Copyright 2014 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "gm.h"
+
+#include "SkColorFilter.h"
+#include "SkMultiPictureDraw.h"
+#include "SkPictureRecorder.h"
+#include "SkSurface.h"
+
+// TODO:
+// pass around SkPicture*[2]
+// add diff clips (path, invpath, region)
+// research Chrome SkRegion usage
+// break out make_hex_path mtd
+// add rest of clip types to mpd object
+// add static asserts
+// fix up pub vs. protected in GM
+
+static const SkScalar kRoot3Over2 = 0.86602545f; // sin(60)
+
+static const int kHexSide = 30;
+static const int kNumHexX = 6;
+static const int kNumHexY = 6;
+static const int kPicWidth = kNumHexX * kHexSide;
+static const int kPicHeight = SkScalarCeilToInt((kNumHexY - 0.5f) * 2 * kHexSide * kRoot3Over2);
+static const SkScalar kInset = 20.0f;
+
+// Make a picture that is a tiling of the plane with stroked hexagons where
+// each hexagon is in its own layer. The layers are to exercise Ganesh's
+// layer hoisting.
+static const SkPicture* make_picture(SkColor fillColor) {
+
+ // Create a hexagon with its center at the origin
+ SkPath hex;
+ hex.moveTo(-kHexSide, 0);
+ hex.rLineTo(SkScalarHalf(kHexSide), kRoot3Over2 * kHexSide);
+ hex.rLineTo(SkIntToScalar(kHexSide), 0);
+ hex.rLineTo(SkScalarHalf(kHexSide), -kHexSide * kRoot3Over2);
+ hex.rLineTo(-SkScalarHalf(kHexSide), -kHexSide * kRoot3Over2);
+ hex.rLineTo(-SkIntToScalar(kHexSide), 0);
+ hex.close();
+
+ SkPaint fill;
+ fill.setStyle(SkPaint::kFill_Style);
+ fill.setColor(fillColor);
+
+ SkPaint stroke;
+ stroke.setStyle(SkPaint::kStroke_Style);
+ stroke.setStrokeWidth(3);
+
+ SkPictureRecorder recorder;
+
+ SkCanvas* canvas = recorder.beginRecording(kPicWidth, kPicHeight);
+
+ SkScalar xPos, yPos = 0;
+
+ for (int y = 0; y < kNumHexY; ++y) {
+ xPos = 0;
+
+ for (int x = 0; x < kNumHexX; ++x) {
+ canvas->saveLayer(NULL, NULL);
+ canvas->translate(xPos, yPos + ((x % 2) ? kRoot3Over2 * kHexSide : 0));
+ canvas->drawPath(hex, fill);
+ canvas->drawPath(hex, stroke);
+ canvas->restore();
+
+ xPos += 1.5f * kHexSide;
+ }
+
+ yPos += 2 * kHexSide * kRoot3Over2;
+ }
+
+ return recorder.endRecording();
+}
+
+static SkSurface* compat_surface(SkCanvas* canvas, int width, int height) {
+ SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
+
+ SkSurface* surface = canvas->newSurface(info);
+ if (NULL == surface) {
+ // picture canvas returns NULL so fall back to raster
+ surface = SkSurface::NewRaster(info);
+ }
+
+ return surface;
+}
+
+// This class stores the information required to compose all the result
+// fragments potentially generated by the MultiPictureDraw object
+class ComposeStep {
+public:
+ ComposeStep() : fSurf(NULL), fX(0.0f), fY(0.0f), fPaint(NULL) { }
+ ~ComposeStep() { SkSafeUnref(fSurf); SkDELETE(fPaint); }
+
+ SkSurface* fSurf;
+ SkScalar fX;
+ SkScalar fY;
+ SkPaint* fPaint;
+};
+
+typedef void (*PFContentMtd)(SkMultiPictureDraw* mpd,
+ const SkPicture* pic0, const SkPicture* pic1,
+ SkCanvas* dest,
+ const SkMatrix& xform);
+
+// Just a single picture with no clip
+static void no_clip(SkMultiPictureDraw* mpd,
+ const SkPicture* pic0, const SkPicture* pic1,
+ SkCanvas* dest, const SkMatrix& xform) {
+ mpd->add(pic0, dest, &xform);
+}
+
+// Two pictures with a rect clip on the second one
+static void rect_clip(SkMultiPictureDraw* mpd,
+ const SkPicture* pic0, const SkPicture* pic1,
+ SkCanvas* dest, const SkMatrix& xform) {
+ mpd->add(pic0, dest, &xform);
+
+ SkRect rect = SkRect::MakeWH(SkIntToScalar(kPicWidth), SkIntToScalar(kPicHeight));
+ rect.inset(kInset, kInset);
+ xform.mapRect(&rect);
+
+ mpd->add(pic1, dest, rect, &xform);
+}
+
+// Two pictures with a round rect clip on the second one
+static void rrect_clip(SkMultiPictureDraw* mpd,
+ const SkPicture* pic0, const SkPicture* pic1,
+ SkCanvas* dest, const SkMatrix& xform) {
+ mpd->add(pic0, dest, &xform);
+
+ SkRect rect = SkRect::MakeWH(SkIntToScalar(kPicWidth), SkIntToScalar(kPicHeight));
+ rect.inset(kInset, kInset);
+ xform.mapRect(&rect);
+
+ SkRRect rrect;
+ rrect.setRectXY(rect, kInset, kInset);
+
+ mpd->add(pic1, dest, rrect, &xform);
+}
+
+static const PFContentMtd gContentMtds[] = {
+ no_clip,
+ rect_clip,
+ rrect_clip
+};
+// TODO: static asserts
+
+typedef void(*PFLayoutMtd)(SkCanvas*, SkMultiPictureDraw*, PFContentMtd,
+ const SkPicture*, const SkPicture*,
+ SkTArray<ComposeStep>*);
+
+// Draw the content into a single canvas
+static void simple(SkCanvas* finalCanvas, SkMultiPictureDraw* mpd,
+ PFContentMtd pfGen,
+ const SkPicture* pic0, const SkPicture* pic1,
+ SkTArray<ComposeStep> *composeSteps) {
+
+ ComposeStep& step = composeSteps->push_back();
+
+ step.fSurf = SkSafeRef(compat_surface(finalCanvas, kPicWidth, kPicHeight));
+
+ SkCanvas* subCanvas = step.fSurf->getCanvas();
+
+ (*pfGen)(mpd, pic0, pic1, subCanvas, SkMatrix::I());
+}
+
+// Draw the content into multiple canvases/tiles
+static void tiled(SkCanvas* finalCanvas, SkMultiPictureDraw* mpd,
+ PFContentMtd pfGen,
+ const SkPicture* pic0, const SkPicture* pic1,
+ SkTArray<ComposeStep> *composeSteps) {
+ static const int kNumTilesX = 2;
+ static const int kNumTilesY = 2;
+ static const int kTileWidth = kPicWidth / kNumTilesX;
+ static const int kTileHeight = kPicHeight / kNumTilesY;
+
+ SkASSERT(kPicWidth == kNumTilesX * kTileWidth);
+ SkASSERT(kPicHeight == kNumTilesY * kTileHeight);
+
+ static const SkColor colors[kNumTilesX][kNumTilesY] = {
+ SK_ColorCYAN, SK_ColorMAGENTA, SK_ColorYELLOW, SK_ColorGREEN
+ };
+
+ for (int y = 0; y < kNumTilesY; ++y) {
+ for (int x = 0; x < kNumTilesX; ++x) {
+ ComposeStep& step = composeSteps->push_back();
+
+ step.fX = SkIntToScalar(x*kTileWidth);
+ step.fY = SkIntToScalar(y*kTileHeight);
+ step.fPaint = SkNEW(SkPaint);
+ step.fPaint->setColorFilter(
+ SkColorFilter::CreateModeFilter(colors[x][y], SkXfermode::kModulate_Mode))->unref();
+
+ step.fSurf = SkSafeRef(compat_surface(finalCanvas, kTileWidth, kTileHeight));
+
+ SkCanvas* subCanvas = step.fSurf->getCanvas();
+
+ SkMatrix trans;
+ trans.setTranslate(-SkIntToScalar(x*kTileWidth), -SkIntToScalar(y*kTileHeight));
+
+ (*pfGen)(mpd, pic0, pic1, subCanvas, trans);
+ }
+ }
+}
+
+static const PFLayoutMtd gLayoutMthds[] = { simple, tiled };
+// TODO: static asserts
+
+namespace skiagm {
+ /**
+ * This GM exercises the SkMultiPictureDraw object. It tests the
+ * cross product of:
+ * tiled vs. all-at-once rendering (e.g., into many or just 1 canvas)
+ * different clips (e.g., none, rect, rrect)
+ * single vs. multiple pictures (e.g., normal vs. picture-pile-style content)
+ */
+ class MultiPictureDraw : public GM {
+ public:
+ enum Content {
+ NoClipSingle_Content,
+ RectClipMulti_Content,
+ RRectClipMulti_Content,
+ };
+
+ enum Layout {
+ Simple_Layout,
+ Tiled_Layout
+ };
+
+ MultiPictureDraw(Content content, Layout layout) : fContent(content), fLayout(layout) { }
+
+ virtual SkString onShortName() SK_OVERRIDE {
+ static const char* gContentNames[] = { "noclip", "rectclip", "rrectclip" };
+ static const char* gLayoutNames[] = { "simple", "tiled" };
+ // TODO: static asserts
+
+ SkString name("multipicturedraw_");
+
+ name.append(gContentNames[fContent]);
+ name.append("_");
+ name.append(gLayoutNames[fLayout]);
+ return name;
+ }
+
+ virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
+ SkMultiPictureDraw mpd;
+ SkTArray<ComposeStep> composeSteps;
+
+ // Fill up the MultiPictureDraw
+ (*gLayoutMthds[fLayout])(canvas, &mpd, gContentMtds[fContent], fPictures[0], fPictures[1], &composeSteps);
+
+ mpd.draw();
+
+ // Compose all the drawn canvases into the final canvas
+ for (int i = 0; i < composeSteps.count(); ++i) {
+ const ComposeStep& step = composeSteps[i];
+
+ SkAutoTUnref<SkImage> image(step.fSurf->newImageSnapshot());
+
+ image->draw(canvas, step.fX, step.fY, step.fPaint);
+ }
+ }
+
+ protected:
+ Content fContent;
+ Layout fLayout;
+ SkAutoTUnref<const SkPicture> fPictures[2];
+
+ virtual SkISize onISize() SK_OVERRIDE { return SkISize::Make(kPicWidth, kPicHeight); }
+
+ virtual void onOnceBeforeDraw() SK_OVERRIDE {
+ fPictures[0].reset(make_picture(SK_ColorWHITE));
+ fPictures[1].reset(make_picture(SK_ColorLTGRAY));
+ }
+
+ virtual uint32_t onGetFlags() const SK_OVERRIDE { return kAsBench_Flag | kSkipTiled_Flag; }
+
+ private:
+ typedef GM INHERITED;
+ };
+
+ DEF_GM(return SkNEW_ARGS(MultiPictureDraw, (MultiPictureDraw::NoClipSingle_Content, MultiPictureDraw::Simple_Layout));)
+ DEF_GM(return SkNEW_ARGS(MultiPictureDraw, (MultiPictureDraw::RectClipMulti_Content, MultiPictureDraw::Simple_Layout));)
+ DEF_GM(return SkNEW_ARGS(MultiPictureDraw, (MultiPictureDraw::RRectClipMulti_Content, MultiPictureDraw::Simple_Layout));)
+
+ DEF_GM(return SkNEW_ARGS(MultiPictureDraw, (MultiPictureDraw::NoClipSingle_Content, MultiPictureDraw::Tiled_Layout));)
+ DEF_GM(return SkNEW_ARGS(MultiPictureDraw, (MultiPictureDraw::RectClipMulti_Content, MultiPictureDraw::Tiled_Layout));)
+ DEF_GM(return SkNEW_ARGS(MultiPictureDraw, (MultiPictureDraw::RRectClipMulti_Content, MultiPictureDraw::Tiled_Layout));)
+}
« no previous file with comments | « no previous file | gyp/core.gypi » ('j') | include/core/SkMultiPictureDraw.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698