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

Unified Diff: gm/pictureshader.cpp

Issue 221923007: Initial picture shader implementation (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Win build fix + SkShader factory. Created 6 years, 9 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') | src/core/SkPictureShader.h » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: gm/pictureshader.cpp
diff --git a/gm/pictureshader.cpp b/gm/pictureshader.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..898dff28d4d1cf20c94791ac24d124893473321c
--- /dev/null
+++ b/gm/pictureshader.cpp
@@ -0,0 +1,147 @@
+/*
+ * 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 "SkBitmap.h"
+#include "SkPaint.h"
+#include "SkPicture.h"
+#include "SkShader.h"
+
+namespace skiagm {
+
+class PictureShaderGM : public GM {
+public:
+
robertphillips 2014/04/02 18:15:35 Should we also test some of the other tiling modes
f(malita) 2014/04/07 15:06:24 Done.
+ PictureShaderGM(SkScalar tileSize, SkScalar sceneSize)
+ : fTileSize(tileSize)
+ , fSceneSize(sceneSize) {
+
+ // Build the picture shader.
+ SkAutoTUnref<SkPicture> p(SkNEW(SkPicture));
+ SkCanvas* pictureCanvas = p->beginRecording(SkScalarRoundToInt(tileSize),
+ SkScalarRoundToInt(tileSize));
robertphillips 2014/04/02 18:15:35 this->
f(malita) 2014/04/07 15:06:24 Argh, of all the Skia style rules this one never s
+ drawTile(pictureCanvas);
+ p->endRecording();
+ fPictureShader.reset(SkShader::CreatePictureShader(*p,
+ SkShader::kRepeat_TileMode,
+ SkShader::kRepeat_TileMode));
+
+ // Build a reference bitmap shader.
+ SkBitmap bm;
+ bm.allocN32Pixels(SkScalarRoundToInt(tileSize), SkScalarRoundToInt(tileSize));
+ bm.eraseColor(SK_ColorTRANSPARENT);
+ SkCanvas bitmapCanvas(bm);
robertphillips 2014/04/02 18:15:35 this->
+ drawTile(&bitmapCanvas);
+ fBitmapShader.reset(SkShader::CreateBitmapShader(bm,
+ SkShader::kRepeat_TileMode,
+ SkShader::kRepeat_TileMode));
+ }
+
+protected:
robertphillips 2014/04/02 18:15:35 SK_OVERRIDE
f(malita) 2014/04/07 15:06:24 Done (all).
+ virtual SkString onShortName() {
+ return SkString("pictureshader");
+ }
+
robertphillips 2014/04/02 18:15:35 SK_OVERRIDE
+ virtual SkISize onISize() {
+ return SkISize::Make(1300, 1100);
+ }
+
robertphillips 2014/04/02 18:15:35 SK_OVERRIDE
+ virtual void onDraw(SkCanvas* canvas) {
robertphillips 2014/04/02 18:15:35 this-> for all of them
+ drawSceneColumn(canvas, 0, 1, 1);
+ drawSceneColumn(canvas, fSceneSize * 2.4f, 1, 2);
+ drawSceneColumn(canvas, fSceneSize * 4.8f, 2, 1);
+ drawSceneColumn(canvas, fSceneSize * 9.6f, 2, 2);
+ }
+
+private:
+ void drawSceneColumn(SkCanvas* canvas, SkScalar xpos, SkScalar scale, SkScalar localScale) {
+ SkMatrix ctm, localMatrix;
+
+ ctm.setTranslate(xpos, 0);
+ ctm.preScale(scale, scale);
+ localMatrix.setScale(localScale, localScale);
robertphillips 2014/04/02 18:15:35 this->
+ drawScene(canvas, ctm, localMatrix);
+
+ ctm.setTranslate(xpos, fSceneSize * 1.2f * scale);
+ ctm.preScale(scale, scale);
+ localMatrix.setTranslate(fTileSize / 4, fTileSize / 4);
+ localMatrix.preScale(localScale, localScale);
robertphillips 2014/04/02 18:15:35 this->
+ drawScene(canvas, ctm, localMatrix);
+
+ ctm.setTranslate(xpos, fSceneSize * 2.4f * scale);
+ ctm.preScale(scale, scale);
+ localMatrix.setRotate(45);
+ localMatrix.preScale(localScale, localScale);
robertphillips 2014/04/02 18:15:35 this->
+ drawScene(canvas, ctm, localMatrix);
+
+ ctm.setTranslate(xpos, fSceneSize * 3.6f * scale);
+ ctm.preScale(scale, scale);
+ localMatrix.setSkew(1, 0);
+ localMatrix.preScale(localScale, localScale);
robertphillips 2014/04/02 18:15:35 this->
+ drawScene(canvas, ctm, localMatrix);
+
+ ctm.setTranslate(xpos, fSceneSize * 4.8f * scale);
+ ctm.preScale(scale, scale);
+ localMatrix.setTranslate(fTileSize / 4, fTileSize / 4);
+ localMatrix.preRotate(45);
+ localMatrix.preScale(localScale, localScale);
robertphillips 2014/04/02 18:15:35 this->
+ drawScene(canvas, ctm, localMatrix);
+ }
+
+ void drawTile(SkCanvas* canvas) {
+ SkPaint paint;
+ paint.setColor(SK_ColorGREEN);
+ paint.setStyle(SkPaint::kFill_Style);
+ paint.setAntiAlias(true);
+
+ canvas->drawCircle(fTileSize / 4, fTileSize / 4, fTileSize / 4, paint);
+ canvas->drawRect(SkRect::MakeXYWH(fTileSize / 2, fTileSize / 2,
+ fTileSize / 2, fTileSize / 2), paint);
+
+ paint.setColor(SK_ColorRED);
+ canvas->drawLine(fTileSize / 2, fTileSize * 1 / 3,
+ fTileSize / 2, fTileSize * 2 / 3, paint);
+ canvas->drawLine(fTileSize * 1 / 3, fTileSize / 2,
+ fTileSize * 2 / 3, fTileSize / 2, paint);
+ }
+
+ void drawScene(SkCanvas* canvas, const SkMatrix& matrix, const SkMatrix& localMatrix) {
+ SkPaint paint;
+ paint.setStyle(SkPaint::kFill_Style);
+ paint.setColor(SK_ColorLTGRAY);
+
+ canvas->save();
+ canvas->concat(matrix);
+ canvas->drawRect(SkRect::MakeWH(fSceneSize, fSceneSize), paint);
+ canvas->drawRect(SkRect::MakeXYWH(fSceneSize * 1.1f, 0, fSceneSize, fSceneSize), paint);
+
+ fPictureShader->setLocalMatrix(localMatrix);
+ paint.setShader(fPictureShader.get());
+ canvas->drawRect(SkRect::MakeWH(fSceneSize, fSceneSize), paint);
+
+ canvas->translate(fSceneSize * 1.1f, 0);
+
+ fBitmapShader->setLocalMatrix(localMatrix);
+ paint.setShader(fBitmapShader.get());
+ canvas->drawRect(SkRect::MakeWH(fSceneSize, fSceneSize), paint);
+
+ canvas->restore();
+ }
+
robertphillips 2014/04/02 18:15:35 This should go last.
+ typedef GM INHERITED;
+
+ SkScalar fTileSize;
+ SkScalar fSceneSize;
+
+ SkAutoTUnref<SkShader> fPictureShader;
+ SkAutoTUnref<SkShader> fBitmapShader;
+};
+
robertphillips 2014/04/02 18:15:35 DEF_GM?
+static GM* MyFactory(void*) { return SkNEW_ARGS(PictureShaderGM, (50, 100)); }
+static GMRegistry reg(MyFactory);
+}
« no previous file with comments | « no previous file | gyp/core.gypi » ('j') | src/core/SkPictureShader.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698