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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « no previous file | gyp/SampleApp.gyp » ('j') | include/core/SkCanvas.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2013 Google Inc. 2 * Copyright 2013 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "gm.h" 8 #include "gm.h"
9 #include "SkCanvas.h" 9 #include "SkCanvas.h"
10 #include "SkBlurImageFilter.h" 10 #include "SkBlurImageFilter.h"
11 #include "SkRSXform.h"
12 #include "SkSurface.h"
11 13
12 static void make_bm(SkBitmap* bm) { 14 static void make_bm(SkBitmap* bm) {
13 bm->allocN32Pixels(100, 100); 15 bm->allocN32Pixels(100, 100);
14 bm->eraseColor(SK_ColorBLUE); 16 bm->eraseColor(SK_ColorBLUE);
15 17
16 SkCanvas canvas(*bm); 18 SkCanvas canvas(*bm);
17 SkPaint paint; 19 SkPaint paint;
18 paint.setAntiAlias(true); 20 paint.setAntiAlias(true);
19 paint.setColor(SK_ColorRED); 21 paint.setColor(SK_ColorRED);
20 canvas.drawCircle(50, 50, 50, paint); 22 canvas.drawCircle(50, 50, 50, paint);
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 draw_2_bitmaps(canvas, bm, false, dx, dy, filter); 88 draw_2_bitmaps(canvas, bm, false, dx, dy, filter);
87 dy += bm.height() + 20; 89 dy += bm.height() + 20;
88 draw_2_bitmaps(canvas, bm, true, dx, dy); 90 draw_2_bitmaps(canvas, bm, true, dx, dy);
89 dy += bm.height() + 20; 91 dy += bm.height() + 20;
90 draw_2_bitmaps(canvas, bm, true, dx, dy, filter); 92 draw_2_bitmaps(canvas, bm, true, dx, dy, filter);
91 } 93 }
92 94
93 private: 95 private:
94 typedef GM INHERITED; 96 typedef GM INHERITED;
95 }; 97 };
98 DEF_GM( return new SpriteBitmapGM; )
96 99
97 ////////////////////////////////////////////////////////////////////////////// 100 //////////////////////////////////////////////////////////////////////////////
98 101
robertphillips 2015/06/24 15:11:02 should this go in its own file ? // This GM ...
reed1 2015/06/24 16:17:47 Done.
99 DEF_GM( return new SpriteBitmapGM; ) 102 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
103 static SkImage* MakeAtlas(SkCanvas* caller, const SkRect& target) {
104 SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
105 SkAutoTUnref<SkSurface> surface(caller->newSurface(info));
106 if (NULL == surface) {
107 surface.reset(SkSurface::NewRaster(info));
108 }
109 SkCanvas* canvas = surface->getCanvas();
110 canvas->clear(SK_ColorRED);
111
112 SkPaint paint;
113 paint.setXfermodeMode(SkXfermode::kClear_Mode);
114 SkRect r(target);
115 r.inset(-1, -1);
116 canvas->drawRect(r, paint);
117 paint.setXfermode(NULL);
118 paint.setColor(SK_ColorBLUE);
119 paint.setAntiAlias(true);
120 canvas->drawOval(target, paint);
121 return surface->newImageSnapshot();
122 }
123
124 SkAutoTUnref<SkImage> fAtlas;
125
126 public:
127 DrawAtlasGM() {}
128
129 protected:
130
131 SkString onShortName() override {
132 return SkString("draw-atlas");
133 }
134
135 SkISize onISize() override {
136 return SkISize::Make(640, 480);
137 }
138
139 void onDraw(SkCanvas* canvas) override {
140 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.
141 if (NULL == fAtlas) {
142 fAtlas.reset(MakeAtlas(canvas, target));
143 }
144
145 const struct {
146 SkScalar fScale;
147 SkScalar fDegrees;
148 SkScalar fTx;
149 SkScalar fTy;
150
151 void apply(SkRSXform* xform) const {
152 const SkScalar rad = SkDegreesToRadians(fDegrees);
153 xform->fSCos = fScale * SkScalarCos(rad);
154 xform->fSSin = fScale * SkScalarSin(rad);
155 xform->fTx = fTx;
156 xform->fTy = fTy;
157 }
158 } rec[] = {
159 { 1, 0, 10, 10 }, // just translate
160 { 2, 0, 110, 10 }, // scale + translate
161 { 1, 30, 210, 10 }, // rotate + translate
162 { 2, -30, 310, 30 }, // scale + rotate + translate
163 };
164
165 const int N = SK_ARRAY_COUNT(rec);
166 SkRSXform xform[N];
167 SkRect tex[N];
168 SkColor colors[N];
169
170 for (int i = 0; i < N; ++i) {
171 rec[i].apply(&xform[i]);
172 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
173 colors[i] = 0x80FF0000 + (i * 40 * 256);
174 }
175
176 SkPaint paint;
177 paint.setFilterQuality(kLow_SkFilterQuality);
178 paint.setAntiAlias(true);
179
180 canvas->drawAtlas(fAtlas, xform, tex, N, NULL, &paint);
181 canvas->translate(0, 100);
182 canvas->drawAtlas(fAtlas, xform, tex, colors, N, SkXfermode::kSrcIn_Mode , NULL, &paint);
183 }
184
185 private:
186 typedef GM INHERITED;
187 };
188 DEF_GM( return new DrawAtlasGM; )
189
OLDNEW
« 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