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

Side by Side Diff: gm/image.cpp

Issue 1463373002: scaling API on SkPixmap (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 5 years 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 | include/core/SkImage.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2011 Google Inc. 2 * Copyright 2011 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 "SkData.h" 9 #include "SkData.h"
10 #include "SkCanvas.h" 10 #include "SkCanvas.h"
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 if (surf2) { 185 if (surf2) {
186 canvas->translate(80, 0); 186 canvas->translate(80, 0);
187 test_surface(canvas, surf2, true); 187 test_surface(canvas, surf2, true);
188 } 188 }
189 } 189 }
190 190
191 private: 191 private:
192 typedef skiagm::GM INHERITED; 192 typedef skiagm::GM INHERITED;
193 }; 193 };
194 DEF_GM( return new ImageGM; ) 194 DEF_GM( return new ImageGM; )
195
196 //////////////////////////////////////////////////////////////////////////////// ///////////////////
197
198 #include "SkPictureRecorder.h"
199
200 static void draw_pixmap(SkCanvas* canvas, const SkPixmap& pmap) {
201 SkBitmap bitmap;
202 bitmap.installPixels(pmap.info(), (void*)pmap.addr(), pmap.rowBytes());
203 canvas->drawBitmap(bitmap, 0, 0, nullptr);
204 }
205
206 static void show_scaled_pixels(SkCanvas* canvas, SkImage* image) {
207 SkAutoCanvasRestore acr(canvas, true);
208
209 canvas->drawImage(image, 0, 0, nullptr);
210 canvas->translate(110, 10);
211
212 const SkImageInfo info = SkImageInfo::MakeN32Premul(40, 40);
213 SkAutoPixmapStorage storage;
214 storage.alloc(info);
215
216 const SkImage::CachingHint chints[] = {
217 SkImage::kAllow_CachingHint, // SkImage::kDisallow_CachingHint,
218 };
219 const SkFilterQuality qualities[] = {
220 kNone_SkFilterQuality, kLow_SkFilterQuality, kMedium_SkFilterQuality, kH igh_SkFilterQuality,
221 };
222
223 for (auto ch : chints) {
224 canvas->save();
225 for (auto q : qualities) {
226 if (image->scalePixels(storage, q, ch)) {
227 draw_pixmap(canvas, storage);
228 }
229 canvas->translate(70, 0);
230 }
231 canvas->restore();
232 canvas->translate(0, 45);
233 }
234 }
235
236 static void draw_contents(SkCanvas* canvas) {
237 SkPaint paint;
238 paint.setStyle(SkPaint::kStroke_Style);
239 paint.setStrokeWidth(20);
240 canvas->drawCircle(50, 50, 35, paint);
241 }
242
243 static SkImage* make_raster(const SkImageInfo& info, GrContext*) {
244 SkAutoTUnref<SkSurface> surface(SkSurface::NewRaster(info));
245 draw_contents(surface->getCanvas());
246 return surface->newImageSnapshot();
247 }
248
249 static SkImage* make_picture(const SkImageInfo& info, GrContext*) {
250 SkPictureRecorder recorder;
251 draw_contents(recorder.beginRecording(SkRect::MakeIWH(info.width(), info.hei ght())));
252 SkAutoTUnref<SkPicture> pict(recorder.endRecording());
253 return SkImage::NewFromPicture(pict, info.dimensions(), nullptr, nullptr);
254 }
255
256 static SkImage* make_codec(const SkImageInfo& info, GrContext*) {
257 SkAutoTUnref<SkImage> image(make_raster(info, nullptr));
258 SkAutoTUnref<SkData> data(image->encode());
259 return SkImage::NewFromEncoded(data);
260 }
261
262 static SkImage* make_gpu(const SkImageInfo& info, GrContext* ctx) {
263 if (!ctx) { return nullptr; }
264 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(ctx, SkSurface::k No_Budgeted, info));
265 draw_contents(surface->getCanvas());
266 return surface->newImageSnapshot();
267 }
268
269 typedef SkImage* (*ImageMakerProc)(const SkImageInfo&, GrContext*);
270
271 class ScalePixelsGM : public skiagm::GM {
272 public:
273 ScalePixelsGM() {}
274
275 protected:
276 SkString onShortName() override {
277 return SkString("scale-pixels");
278 }
279
280 SkISize onISize() override {
281 return SkISize::Make(960, 1200);
282 }
283
284 void onDraw(SkCanvas* canvas) override {
285 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
286
287 const ImageMakerProc procs[] = {
288 make_raster, make_picture, make_codec, make_gpu,
289 };
290 for (auto& proc : procs) {
291 SkAutoTUnref<SkImage> image(proc(info, canvas->getGrContext()));
292 if (image) {
293 show_scaled_pixels(canvas, image);
294 }
295 canvas->translate(0, 120);
296 }
297 }
298
299 private:
300 typedef skiagm::GM INHERITED;
301 };
302 DEF_GM( return new ScalePixelsGM; )
OLDNEW
« no previous file with comments | « no previous file | include/core/SkImage.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698