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

Unified Diff: gm/spritebitmap.cpp

Issue 1181913003: add SkCanvas::drawAtlas (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: fix another warning Created 5 years, 6 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/SampleApp.gyp » ('j') | include/core/SkCanvas.h » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: gm/spritebitmap.cpp
diff --git a/gm/spritebitmap.cpp b/gm/spritebitmap.cpp
index 7c8677d143cb735315c54d159b9dd334c457309d..5659964e3bcbc31621ad19a53121849c20076d76 100644
--- a/gm/spritebitmap.cpp
+++ b/gm/spritebitmap.cpp
@@ -8,6 +8,8 @@
#include "gm.h"
#include "SkCanvas.h"
#include "SkBlurImageFilter.h"
+#include "SkRSXform.h"
+#include "SkSurface.h"
static void make_bm(SkBitmap* bm) {
bm->allocN32Pixels(100, 100);
@@ -93,7 +95,95 @@ protected:
private:
typedef GM INHERITED;
};
+DEF_GM( return new SpriteBitmapGM; )
//////////////////////////////////////////////////////////////////////////////
robertphillips 2015/06/24 15:11:02 should this go in its own file ? // This GM ...
reed1 2015/06/24 16:17:47 Done.
-DEF_GM( return new SpriteBitmapGM; )
+class DrawAtlasGM : public skiagm::GM {
robertphillips 2015/06/24 15:11:01 So we're not really using the entire atlas ?
reed1 2015/06/24 16:17:47 I'll add dox. I wanted explicitly to use a subset
+ static SkImage* MakeAtlas(SkCanvas* caller, const SkRect& target) {
+ SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
+ SkAutoTUnref<SkSurface> surface(caller->newSurface(info));
+ if (NULL == surface) {
+ surface.reset(SkSurface::NewRaster(info));
+ }
+ SkCanvas* canvas = surface->getCanvas();
+ canvas->clear(SK_ColorRED);
+
+ SkPaint paint;
+ paint.setXfermodeMode(SkXfermode::kClear_Mode);
+ SkRect r(target);
+ r.inset(-1, -1);
+ canvas->drawRect(r, paint);
+ paint.setXfermode(NULL);
+ paint.setColor(SK_ColorBLUE);
+ paint.setAntiAlias(true);
+ canvas->drawOval(target, paint);
+ return surface->newImageSnapshot();
+ }
+
+ SkAutoTUnref<SkImage> fAtlas;
+
+public:
+ DrawAtlasGM() {}
+
+protected:
+
+ SkString onShortName() override {
+ return SkString("draw-atlas");
+ }
+
+ SkISize onISize() override {
+ return SkISize::Make(640, 480);
+ }
+
+ void onDraw(SkCanvas* canvas) override {
+ const SkRect target = { 50, 50, 80, 90 };
robertphillips 2015/06/24 15:11:01 If we're going to use this pattern more widely (cr
reed1 2015/06/24 16:17:47 Perhaps so.
+ if (NULL == fAtlas) {
+ fAtlas.reset(MakeAtlas(canvas, target));
+ }
+
+ const struct {
+ SkScalar fScale;
+ SkScalar fDegrees;
+ SkScalar fTx;
+ SkScalar fTy;
+
+ void apply(SkRSXform* xform) const {
+ const SkScalar rad = SkDegreesToRadians(fDegrees);
+ xform->fSCos = fScale * SkScalarCos(rad);
+ xform->fSSin = fScale * SkScalarSin(rad);
+ xform->fTx = fTx;
+ xform->fTy = fTy;
+ }
+ } rec[] = {
+ { 1, 0, 10, 10 }, // just translate
+ { 2, 0, 110, 10 }, // scale + translate
+ { 1, 30, 210, 10 }, // rotate + translate
+ { 2, -30, 310, 30 }, // scale + rotate + translate
+ };
+
+ const int N = SK_ARRAY_COUNT(rec);
+ SkRSXform xform[N];
+ SkRect tex[N];
+ SkColor colors[N];
+
+ for (int i = 0; i < N; ++i) {
+ rec[i].apply(&xform[i]);
+ tex[i] = target;
robertphillips 2015/06/24 15:11:02 Do we need to 565/8888 safety these colors ?
reed1 2015/06/24 16:17:47 Perhaps, though I'm antialiasing and blending, so
+ colors[i] = 0x80FF0000 + (i * 40 * 256);
+ }
+
+ SkPaint paint;
+ paint.setFilterQuality(kLow_SkFilterQuality);
+ paint.setAntiAlias(true);
+
+ canvas->drawAtlas(fAtlas, xform, tex, N, NULL, &paint);
+ canvas->translate(0, 100);
+ canvas->drawAtlas(fAtlas, xform, tex, colors, N, SkXfermode::kSrcIn_Mode, NULL, &paint);
+ }
+
+private:
+ typedef GM INHERITED;
+};
+DEF_GM( return new DrawAtlasGM; )
+
« no previous file with comments | « no previous file | gyp/SampleApp.gyp » ('j') | include/core/SkCanvas.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698