Index: gm/image.cpp |
diff --git a/gm/image.cpp b/gm/image.cpp |
index 27cce2342e24a10ffddb13b3f61c036170de7433..03ed1059cf33298901f181cbd9401b4b5b309899 100644 |
--- a/gm/image.cpp |
+++ b/gm/image.cpp |
@@ -192,3 +192,111 @@ private: |
typedef skiagm::GM INHERITED; |
}; |
DEF_GM( return new ImageGM; ) |
+ |
+/////////////////////////////////////////////////////////////////////////////////////////////////// |
+ |
+#include "SkPictureRecorder.h" |
+ |
+static void draw_pixmap(SkCanvas* canvas, const SkPixmap& pmap) { |
+ SkBitmap bitmap; |
+ bitmap.installPixels(pmap.info(), (void*)pmap.addr(), pmap.rowBytes()); |
+ canvas->drawBitmap(bitmap, 0, 0, nullptr); |
+} |
+ |
+static void show_scaled_pixels(SkCanvas* canvas, SkImage* image) { |
+ SkAutoCanvasRestore acr(canvas, true); |
+ |
+ canvas->drawImage(image, 0, 0, nullptr); |
+ canvas->translate(110, 10); |
+ |
+ const SkImageInfo info = SkImageInfo::MakeN32Premul(40, 40); |
+ SkAutoPixmapStorage storage; |
+ storage.alloc(info); |
+ |
+ const SkImage::CachingHint chints[] = { |
+ SkImage::kAllow_CachingHint, // SkImage::kDisallow_CachingHint, |
+ }; |
+ const SkFilterQuality qualities[] = { |
+ kNone_SkFilterQuality, kLow_SkFilterQuality, kMedium_SkFilterQuality, kHigh_SkFilterQuality, |
+ }; |
+ |
+ for (auto ch : chints) { |
+ canvas->save(); |
+ for (auto q : qualities) { |
+ if (image->scalePixels(storage, q, ch)) { |
+ draw_pixmap(canvas, storage); |
+ } |
+ canvas->translate(70, 0); |
+ } |
+ canvas->restore(); |
+ canvas->translate(0, 45); |
+ } |
+} |
+ |
+static void draw_contents(SkCanvas* canvas) { |
+ SkPaint paint; |
+ paint.setStyle(SkPaint::kStroke_Style); |
+ paint.setStrokeWidth(20); |
+ canvas->drawCircle(50, 50, 35, paint); |
+} |
+ |
+static SkImage* make_raster(const SkImageInfo& info, GrContext*) { |
+ SkAutoTUnref<SkSurface> surface(SkSurface::NewRaster(info)); |
+ draw_contents(surface->getCanvas()); |
+ return surface->newImageSnapshot(); |
+} |
+ |
+static SkImage* make_picture(const SkImageInfo& info, GrContext*) { |
+ SkPictureRecorder recorder; |
+ draw_contents(recorder.beginRecording(SkRect::MakeIWH(info.width(), info.height()))); |
+ SkAutoTUnref<SkPicture> pict(recorder.endRecording()); |
+ return SkImage::NewFromPicture(pict, info.dimensions(), nullptr, nullptr); |
+} |
+ |
+static SkImage* make_codec(const SkImageInfo& info, GrContext*) { |
+ SkAutoTUnref<SkImage> image(make_raster(info, nullptr)); |
+ SkAutoTUnref<SkData> data(image->encode()); |
+ return SkImage::NewFromEncoded(data); |
+} |
+ |
+static SkImage* make_gpu(const SkImageInfo& info, GrContext* ctx) { |
+ if (!ctx) { return nullptr; } |
+ SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(ctx, SkSurface::kNo_Budgeted, info)); |
+ draw_contents(surface->getCanvas()); |
+ return surface->newImageSnapshot(); |
+} |
+ |
+typedef SkImage* (*ImageMakerProc)(const SkImageInfo&, GrContext*); |
+ |
+class ScalePixelsGM : public skiagm::GM { |
+public: |
+ ScalePixelsGM() {} |
+ |
+protected: |
+ SkString onShortName() override { |
+ return SkString("scale-pixels"); |
+ } |
+ |
+ SkISize onISize() override { |
+ return SkISize::Make(960, 1200); |
+ } |
+ |
+ void onDraw(SkCanvas* canvas) override { |
+ const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100); |
+ |
+ const ImageMakerProc procs[] = { |
+ make_raster, make_picture, make_codec, make_gpu, |
+ }; |
+ for (auto& proc : procs) { |
+ SkAutoTUnref<SkImage> image(proc(info, canvas->getGrContext())); |
+ if (image) { |
+ show_scaled_pixels(canvas, image); |
+ } |
+ canvas->translate(0, 120); |
+ } |
+ } |
+ |
+private: |
+ typedef skiagm::GM INHERITED; |
+}; |
+DEF_GM( return new ScalePixelsGM; ) |