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

Unified Diff: samplecode/SampleAtlas.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
Index: samplecode/SampleAtlas.cpp
diff --git a/samplecode/SampleAtlas.cpp b/samplecode/SampleAtlas.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..e5abaf4878f8d706c9d7d366d7e9d09eb62e2d6f
--- /dev/null
+++ b/samplecode/SampleAtlas.cpp
@@ -0,0 +1,216 @@
+/*
+ * Copyright 2015 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SampleCode.h"
+#include "SkAnimTimer.h"
+#include "SkView.h"
+#include "SkCanvas.h"
+#include "SkDrawable.h"
+#include "SkPath.h"
+#include "SkRandom.h"
+#include "SkRSXform.h"
+#include "SkSurface.h"
+
+static SkImage* make_atlas() {
+ static int N = 512;
+ SkImageInfo info = SkImageInfo::MakeN32Premul(N, N);
+ SkAutoTUnref<SkSurface> surface(SkSurface::NewRaster(info));
+ SkCanvas* canvas = surface->getCanvas();
+
+ SkPaint paint;
+ paint.setAntiAlias(true);
+ SkRandom rand;
+
+ const SkScalar half = 16;
+ const char* s = "01234567890!@#$%^&*=+<>?abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
+ paint.setTextSize(28);
+ paint.setTextAlign(SkPaint::kCenter_Align);
+ int i = 0;
+ for (int y = 0; y < N; y += 32) {
+ for (int x = 0; x < N; x += 32) {
+ paint.setColor(rand.nextU());
+ paint.setAlpha(0xFF);
+ int index = i % strlen(s);
+ canvas->drawText(&s[index], 1, x + half, y + half + half/2, paint);
+ i += 1;
+ }
+ }
+ return surface->newImageSnapshot();
+}
+
+class DrawAtlasDrawable : public SkDrawable {
+ enum {
+ kMaxScale = 2,
+ kCellSize = 32,
+ };
+
+ struct Rec {
+ SkPoint fCenter;
+ SkVector fVelocity;
+ SkScalar fScale;
+ SkScalar fDScale;
+ SkScalar fRadian;
+ SkScalar fDRadian;
+ SkScalar fAlpha;
+ SkScalar fDAlpha;
+
+ void advance(const SkRect& bounds) {
+ fCenter += fVelocity;
+ if (fCenter.fX > bounds.right()) {
+ SkASSERT(fVelocity.fX > 0);
+ fVelocity.fX = -fVelocity.fX;
+ } else if (fCenter.fX < bounds.left()) {
+ SkASSERT(fVelocity.fX < 0);
+ fVelocity.fX = -fVelocity.fX;
+ }
+ if (fCenter.fY > bounds.bottom()) {
+ SkASSERT(fVelocity.fY > 0);
+ fVelocity.fY = -fVelocity.fY;
+ } else if (fCenter.fY < bounds.top()) {
+ SkASSERT(fVelocity.fY < 0);
+ fVelocity.fY = -fVelocity.fY;
+ }
+
+ fScale += fDScale;
+ if (fScale > 2 || fScale < SK_Scalar1/2) {
+ fDScale = -fDScale;
+ }
+
+ fRadian += fDRadian;
+ fRadian = SkScalarMod(fRadian, 2 * SK_ScalarPI);
+
+ fAlpha += fDAlpha;
+ if (fAlpha > 1) {
+ fAlpha = 1;
+ fDAlpha = -fDAlpha;
+ } else if (fAlpha < 0) {
+ fAlpha = 0;
+ fDAlpha = -fDAlpha;
+ }
+ }
+
+ SkRSXform asRSXform() const {
+ SkMatrix m;
+ m.setTranslate(-8, -8);
+ m.postScale(fScale, fScale);
+ m.postRotate(SkRadiansToDegrees(fRadian));
+ m.postTranslate(fCenter.fX, fCenter.fY);
+
+ SkRSXform x;
+ x.fSCos = m.getScaleX();
+ x.fSSin = m.getSkewY();
+ x.fTx = m.getTranslateX();
+ x.fTy = m.getTranslateY();
+ return x;
+ }
+ };
+
+ enum {
+ N = 256,
+ };
+
+ SkAutoTUnref<SkImage> fAtlas;
+ Rec fRec[N];
+ SkRect fTex[N];
+ SkRect fBounds;
+
+public:
+ DrawAtlasDrawable(const SkRect& r) : fBounds(r) {
+ SkRandom rand;
+ fAtlas.reset(make_atlas());
+ const SkScalar half = 8;
robertphillips 2015/06/24 15:11:02 speed -> kMaxSpeed ?
reed1 2015/06/24 17:02:42 Done.
+ const SkScalar speed = 5;
+ int i = 0;
robertphillips 2015/06/24 15:11:02 Have 512 be kAtlasWH (and use in make_atlas too) ?
reed1 2015/06/24 17:02:42 Done.
+ for (int y = 0; y < 512; y += 32) {
+ for (int x = 0; x < 512; x += 32) {
+ fTex[i].setXYWH(SkIntToScalar(x), SkIntToScalar(y), 32, 32);
+
+ fRec[i].fCenter.set(x/2 + half, y/2 + half);
+ fRec[i].fVelocity.fX = rand.nextSScalar1() * speed;
+ fRec[i].fVelocity.fY = rand.nextSScalar1() * speed;
+ fRec[i].fScale = 1;
+ fRec[i].fDScale = rand.nextSScalar1() / 4;
+ fRec[i].fRadian = 0;
+ fRec[i].fDRadian = rand.nextSScalar1() / 8;
+ fRec[i].fAlpha = rand.nextUScalar1();
+ fRec[i].fDAlpha = rand.nextSScalar1() / 10;
+ i += 1;
+ }
+ }
robertphillips 2015/06/24 15:11:02 rm this ?
reed1 2015/06/24 17:02:42 Done.
+ SkDebugf("total %d\n", i);
+ }
+
+ void onDraw(SkCanvas* canvas) override {
+ SkRSXform xform[N];
robertphillips 2015/06/24 15:11:02 Why not have a keystroke to enable/disable animati
reed1 2015/06/24 17:02:42 Done.
+// SkColor colors[N];
+
+ for (int i = 0; i < N; ++i) {
+ fRec[i].advance(fBounds);
+ xform[i] = fRec[i].asRSXform();
+// colors[i] = SkColorSetARGB(fRec[i].fAlpha * 0xFF, 0xFF, 0xFF, 0xFF);
+ }
+ SkPaint paint;
+// paint.setFilterQuality(kLow_SkFilterQuality);
+// paint.setAntiAlias(true);
+
+ const SkRect cull = this->getBounds();
+ canvas->drawAtlas(fAtlas, xform, fTex, N, &cull, &paint);
+ }
+
+ SkRect onGetBounds() override {
robertphillips 2015/06/24 15:11:02 border ?
reed1 2015/06/24 17:02:42 Done.
+ const SkScalar boarder = kMaxScale * kCellSize;
+ SkRect r = fBounds;
+ r.outset(boarder, boarder);
+ return r;
+ }
+
+private:
+ typedef SkDrawable INHERITED;
+};
+
+class DrawAtlasView : public SampleView {
robertphillips 2015/06/24 15:11:02 Why not just have this be a member ?
reed1 2015/06/24 17:02:42 canvas->drawDrawable(fDrawable) this guy may need
+ SkDrawable* fDrawable;
+
+public:
+ DrawAtlasView() {
+ fDrawable = new DrawAtlasDrawable(SkRect::MakeWH(640, 480));
+ }
+
robertphillips 2015/06/24 15:11:02 rm virtual ?
reed1 2015/06/24 17:02:42 Done.
+ virtual ~DrawAtlasView() override {
+ fDrawable->unref();
+ }
+
+protected:
+ bool onQuery(SkEvent* evt) override {
+ if (SampleCode::TitleQ(*evt)) {
+ SampleCode::TitleR(evt, "DrawAtlas");
+ return true;
+ }
+ return this->INHERITED::onQuery(evt);
+ }
+
+ void onDrawContent(SkCanvas* canvas) override {
+ canvas->drawDrawable(fDrawable);
+ this->inval(NULL);
+ }
+
robertphillips 2015/06/24 15:11:02 Do we still need this code ?
reed1 2015/06/24 17:02:42 Leaving it as a reminder/todo
+#if 0
+ bool onAnimate(const SkAnimTimer& timer) override {
+ SkScalar angle = SkDoubleToScalar(fmod(timer.secs() * 360 / 24, 360));
+ fAnimatingDrawable->setSweep(angle);
+ return true;
+ }
+#endif
+
+private:
+ typedef SampleView INHERITED;
+};
+
+//////////////////////////////////////////////////////////////////////////////
+
+static SkView* MyFactory() { return new DrawAtlasView; }
+static SkViewRegister reg(MyFactory);

Powered by Google App Engine
This is Rietveld 408576698