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

Unified 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, 1 month 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 | include/core/SkImage.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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; )
« 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