aboutsummaryrefslogtreecommitdiffhomepage
path: root/gm
diff options
context:
space:
mode:
authorGravatar reed <reed@google.com>2015-12-16 08:27:10 -0800
committerGravatar Commit bot <commit-bot@chromium.org>2015-12-16 08:27:10 -0800
commit32704674f64cb6a14356dfebe060cd3484c06cc7 (patch)
tree7a72757624ed59f421c931ce5c02e7644f4987d3 /gm
parent4657ce2324ea197507c4ba728d81138f56da13b1 (diff)
Revert of remove drawSprite from canvas (patchset #4 id:60001 of https://codereview.chromium.org/1534443003/ )
Reason for revert: need to remove an override in chrome Original issue's description: > remove drawSprite from canvas > > BUG=skia: > > Committed: https://skia.googlesource.com/skia/+/4657ce2324ea197507c4ba728d81138f56da13b1 TBR=fmalita@chromium.org,robertphillips@google.com,senorblanco@chromium.org,mtklein@google.com NOPRESUBMIT=true NOTREECHECKS=true NOTRY=true BUG=skia: Review URL: https://codereview.chromium.org/1529803004
Diffstat (limited to 'gm')
-rw-r--r--gm/dropshadowimagefilter.cpp25
-rw-r--r--gm/imagefiltersbase.cpp21
-rw-r--r--gm/imagefilterscropped.cpp25
-rw-r--r--gm/spritebitmap.cpp186
4 files changed, 249 insertions, 8 deletions
diff --git a/gm/dropshadowimagefilter.cpp b/gm/dropshadowimagefilter.cpp
index 512c82de01..81824ab797 100644
--- a/gm/dropshadowimagefilter.cpp
+++ b/gm/dropshadowimagefilter.cpp
@@ -67,6 +67,29 @@ static void draw_bitmap(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) {
canvas->restore();
}
+static void draw_sprite(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) {
+ SkPaint paint;
+
+ SkIRect bounds;
+ r.roundOut(&bounds);
+
+ SkBitmap bm;
+ bm.allocN32Pixels(bounds.width(), bounds.height());
+ bm.eraseColor(SK_ColorRED);
+ SkCanvas c(bm);
+
+ SkIRect cropRect = SkIRect::MakeXYWH(10, 10, 44, 44);
+ paint.setColor(SK_ColorGREEN);
+ c.drawRect(SkRect::Make(cropRect), paint);
+
+ paint.setImageFilter(imf);
+ SkPoint loc = { r.fLeft, r.fTop };
+ canvas->getTotalMatrix().mapPoints(&loc, 1);
+ canvas->drawSprite(bm,
+ SkScalarRoundToInt(loc.fX), SkScalarRoundToInt(loc.fY),
+ &paint);
+}
+
///////////////////////////////////////////////////////////////////////////////
class DropShadowImageFilterGM : public skiagm::GM {
@@ -90,7 +113,7 @@ protected:
virtual void onDraw(SkCanvas* canvas) {
void (*drawProc[])(SkCanvas*, const SkRect&, SkImageFilter*) = {
- draw_bitmap, draw_path, draw_paint, draw_text
+ draw_sprite, draw_bitmap, draw_path, draw_paint, draw_text
};
SkAutoTUnref<SkColorFilter> cf(
diff --git a/gm/imagefiltersbase.cpp b/gm/imagefiltersbase.cpp
index fbe4681da4..0101a9c2a0 100644
--- a/gm/imagefiltersbase.cpp
+++ b/gm/imagefiltersbase.cpp
@@ -165,6 +165,26 @@ static void draw_bitmap(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) {
canvas->drawBitmap(bm, 0, 0, &paint);
}
+static void draw_sprite(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) {
+ SkPaint paint;
+ paint.setImageFilter(imf);
+
+ SkIRect bounds;
+ r.roundOut(&bounds);
+
+ SkBitmap bm;
+ bm.allocN32Pixels(bounds.width(), bounds.height());
+ bm.eraseColor(SK_ColorTRANSPARENT);
+ SkCanvas c(bm);
+ draw_path(&c, r, nullptr);
+
+ SkPoint loc = { r.fLeft, r.fTop };
+ canvas->getTotalMatrix().mapPoints(&loc, 1);
+ canvas->drawSprite(bm,
+ SkScalarRoundToInt(loc.fX), SkScalarRoundToInt(loc.fY),
+ &paint);
+}
+
///////////////////////////////////////////////////////////////////////////////
class ImageFiltersBaseGM : public skiagm::GM {
@@ -190,6 +210,7 @@ protected:
draw_paint,
draw_line, draw_rect, draw_path, draw_text,
draw_bitmap,
+ draw_sprite
};
SkColorFilter* cf = SkColorFilter::CreateModeFilter(SK_ColorRED,
diff --git a/gm/imagefilterscropped.cpp b/gm/imagefilterscropped.cpp
index 4c6ef62356..745e8984db 100644
--- a/gm/imagefilterscropped.cpp
+++ b/gm/imagefilterscropped.cpp
@@ -65,6 +65,29 @@ static void draw_bitmap(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) {
canvas->drawBitmap(bm, 0, 0, &paint);
}
+static void draw_sprite(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) {
+ SkPaint paint;
+
+ SkIRect bounds;
+ r.roundOut(&bounds);
+
+ SkBitmap bm;
+ bm.allocN32Pixels(bounds.width(), bounds.height());
+ bm.eraseColor(SK_ColorRED);
+ SkCanvas c(bm);
+
+ SkIRect cropRect = SkIRect::MakeXYWH(10, 10, 44, 44);
+ paint.setColor(SK_ColorGREEN);
+ c.drawRect(SkRect::Make(cropRect), paint);
+
+ paint.setImageFilter(imf);
+ SkPoint loc = { r.fLeft, r.fTop };
+ canvas->getTotalMatrix().mapPoints(&loc, 1);
+ canvas->drawSprite(bm,
+ SkScalarRoundToInt(loc.fX), SkScalarRoundToInt(loc.fY),
+ &paint);
+}
+
///////////////////////////////////////////////////////////////////////////////
class ImageFiltersCroppedGM : public skiagm::GM {
@@ -112,7 +135,7 @@ protected:
virtual void onDraw(SkCanvas* canvas) override {
void (*drawProc[])(SkCanvas*, const SkRect&, SkImageFilter*) = {
- draw_bitmap, draw_path, draw_paint, draw_text
+ draw_sprite, draw_bitmap, draw_path, draw_paint, draw_text
};
SkAutoTUnref<SkColorFilter> cf(
diff --git a/gm/spritebitmap.cpp b/gm/spritebitmap.cpp
index 4f221d49fc..b199371e89 100644
--- a/gm/spritebitmap.cpp
+++ b/gm/spritebitmap.cpp
@@ -22,8 +22,8 @@ static void make_bm(SkBitmap* bm) {
canvas.drawCircle(50, 50, 50, paint);
}
-static void draw_1_bitmap(SkCanvas* canvas, const SkBitmap& bm, bool doClip,
- int dx, int dy, SkImageFilter* filter = nullptr) {
+static void draw_2_bitmaps(SkCanvas* canvas, const SkBitmap& bm, bool doClip,
+ int dx, int dy, SkImageFilter* filter = nullptr) {
SkAutoCanvasRestore acr(canvas, true);
SkPaint paint;
@@ -35,6 +35,15 @@ static void draw_1_bitmap(SkCanvas* canvas, const SkBitmap& bm, bool doClip,
paint.setImageFilter(filter);
clipR.inset(5, 5);
+ if (doClip) {
+ canvas->save();
+ canvas->clipRect(clipR);
+ }
+ canvas->drawSprite(bm, dx, dy, &paint);
+ if (doClip) {
+ canvas->restore();
+ }
+
canvas->translate(SkIntToScalar(bm.width() + 20), 0);
if (doClip) {
@@ -74,13 +83,13 @@ protected:
SkScalar sigma = 8;
SkAutoTUnref<SkImageFilter> filter(SkBlurImageFilter::Create(sigma, sigma));
- draw_1_bitmap(canvas, bm, false, dx, dy);
+ draw_2_bitmaps(canvas, bm, false, dx, dy);
dy += bm.height() + 20;
- draw_1_bitmap(canvas, bm, false, dx, dy, filter);
+ draw_2_bitmaps(canvas, bm, false, dx, dy, filter);
dy += bm.height() + 20;
- draw_1_bitmap(canvas, bm, true, dx, dy);
+ draw_2_bitmaps(canvas, bm, true, dx, dy);
dy += bm.height() + 20;
- draw_1_bitmap(canvas, bm, true, dx, dy, filter);
+ draw_2_bitmaps(canvas, bm, true, dx, dy, filter);
}
private:
@@ -187,6 +196,22 @@ protected:
canvas->translate(spacer, 0);
show_image(canvas, image2, offset2); // not snug
+ // Try drawing the original w/ the filter, to see that it "draws" the same as
+ // when we have manually applied the filter (above).
+ {
+ SkPaint paint;
+ paint.setImageFilter(filter);
+
+ SkBitmap bm;
+ image0->asLegacyBitmap(&bm, SkImage::kRO_LegacyBitmapMode);
+ SkPoint loc = { 0, 0 };
+ canvas->translate(spacer, 0);
+ canvas->getTotalMatrix().mapPoints(&loc, 1);
+ canvas->drawSprite(bm, (int)loc.x(), (int)loc.y(), &paint); // like snug
+
+ canvas->translate(spacer, 0);
+ canvas->drawImage(image0, 0, 0, &paint); // like not snug
+ }
canvas->restore();
canvas->translate(0, spacer);
@@ -197,3 +222,152 @@ private:
typedef GM INHERITED;
};
DEF_GM( return new ApplyFilterGM; )
+
+//////////////////////
+
+#include "SkDisplacementMapEffect.h"
+#include "SkMatrixConvolutionImageFilter.h"
+
+static SkPMColor max_component(SkPMColor a, SkPMColor b) {
+ int dr = SkAbs32(SkGetPackedR32(a) - SkGetPackedR32(b));
+ int dg = SkAbs32(SkGetPackedG32(a) - SkGetPackedG32(b));
+ int db = SkAbs32(SkGetPackedB32(a) - SkGetPackedB32(b));
+ int d = SkTMax(dr, SkTMax(dg, db));
+ d = 0xFF - d;
+ return SkPackARGB32(0xFF, d, d, d);
+}
+
+static SkImage* compute_diff(SkImage* a, SkImage* b) {
+ SkASSERT(a->width() == b->width() && a->height() == b->height());
+ const SkImageInfo info = SkImageInfo::MakeN32Premul(a->width(), a->height());
+ SkBitmap bma, bmb, bmdiff;
+ bma.allocPixels(info);
+ bmb.allocPixels(info);
+ bmdiff.allocPixels(info);
+
+ a->readPixels(info, bma.getPixels(), bma.rowBytes(), 0, 0);
+ b->readPixels(info, bmb.getPixels(), bmb.rowBytes(), 0, 0);
+ for (int y = 0; y < info.height(); ++y) {
+ for (int x = 0; x < info.width(); ++x) {
+ *bmdiff.getAddr32(x, y) = max_component(*bma.getAddr32(x, y), *bmb.getAddr32(x, y));
+ }
+ }
+ bmdiff.setImmutable(); // avoid the copy
+ return SkImage::NewFromBitmap(bmdiff);
+}
+
+static SkImage* make_native_red_oval(SkCanvas* rootCanvas) {
+ SkImageInfo info = SkImageInfo::MakeN32Premul(160, 90);
+ SkAutoTUnref<SkSurface> surface(rootCanvas->newSurface(info));
+ if (!surface) {
+ surface.reset(SkSurface::NewRaster(info));
+ }
+
+ SkPaint paint;
+ paint.setAntiAlias(true);
+ paint.setColor(SK_ColorRED);
+ surface->getCanvas()->drawOval(SkRect::MakeWH(160, 90), paint);
+ return surface->newImageSnapshot();
+}
+
+
+static SkSurface* make_surface(SkCanvas* factory, const SkImageInfo& info) {
+ SkSurface* surface = factory->newSurface(info);
+ if (!surface) {
+ surface = SkSurface::NewRaster(info);
+ }
+ return surface;
+}
+
+template <typename DrawProc> SkImage* snapshot(SkCanvas* canvas, const SkImageInfo& info,
+ DrawProc p) {
+ SkAutoTUnref<SkSurface> surface(make_surface(canvas, info));
+ p(surface->getCanvas());
+ return surface->newImageSnapshot();
+}
+
+/**
+ * Try drawing an image+imagefilter in two different ways
+ * 1. as drawSprite
+ * 2. as drawImage + clipped to image bounds
+ * The two should draw the same. To try to visualize this, we draw a 4th column of the difference
+ * between the two versions. If it is all black (where there is alpha), they drew the same!
+ */
+class DrawWithFilterGM : public skiagm::GM {
+public:
+ DrawWithFilterGM() {}
+
+protected:
+ SkString onShortName() override {
+ return SkString("draw-with-filter");
+ }
+
+ SkISize onISize() override {
+ return SkISize::Make(780, 780);
+ }
+
+ void onDraw(SkCanvas* canvas) override {
+ SkAutoTUnref<SkImage> image0(make_native_red_oval(canvas));
+ SkAutoTUnref<SkImage> image1(make_native_red_oval(canvas));
+
+ const ImageFilterFactory factories[] = {
+ IFCCast([]{ return SkBlurImageFilter::Create(8, 8); }),
+ IFCCast([]{ SkAutoTUnref<SkColorFilter> cf(SkModeColorFilter::Create(SK_ColorBLUE,
+ SkXfermode::kSrcIn_Mode));
+ return SkColorFilterImageFilter::Create(cf);
+ }),
+ IFCCast([]{ return SkDilateImageFilter::Create(8, 8); }),
+ IFCCast([]{ return SkErodeImageFilter::Create(8, 8); }),
+ IFCCast([]{ return SkOffsetImageFilter::Create(8, 8); }),
+
+ IFCCast([]{ return (SkImageFilter*)SkDisplacementMapEffect::Create(
+ SkDisplacementMapEffect::kR_ChannelSelectorType,
+ SkDisplacementMapEffect::kG_ChannelSelectorType,
+ 10, nullptr); }),
+ IFCCast([]{
+ const SkScalar kernel[] = { 1, 1, 1, 1, -7, 1, 1, 1, 1 };
+ return (SkImageFilter*)SkMatrixConvolutionImageFilter::Create(
+ SkISize::Make(3, 3),
+ kernel, 1, 0,
+ SkIPoint::Make(0, 0),
+ SkMatrixConvolutionImageFilter::kClamp_TileMode,
+ true); }),
+ };
+
+ const SkScalar dx = 180;
+ const SkScalar dy = 110;
+ const SkImageInfo info = SkImageInfo::MakeN32Premul(image0->width(), image0->height());
+
+ canvas->translate(20, 20);
+ for (auto&& factory : factories) {
+ SkAutoTUnref<SkImageFilter> filter(factory());
+ SkPaint paint;
+ paint.setImageFilter(filter);
+
+ SkAutoTUnref<SkImage> snap0(snapshot(canvas, info, [&](SkCanvas* c) {
+ c->drawImage(image0, 0, 0, &paint);
+ }));
+ canvas->drawImage(snap0, 0, 0);
+
+ SkAutoTUnref<SkImage> snap1(snapshot(canvas, info, [&](SkCanvas* c) {
+ SkBitmap bm;
+ image1->asLegacyBitmap(&bm, SkImage::kRO_LegacyBitmapMode);
+ c->drawSprite(bm, 0, 0, &paint);
+ }));
+ canvas->drawImage(snap1, dx, 0);
+
+ SkAutoTUnref<SkImage> diff(snapshot(canvas, info, [&](SkCanvas* c) {
+ SkAutoTUnref<SkImage> diff(compute_diff(snap0, snap1));
+ c->drawImage(diff, 0, 0);
+ }));
+ canvas->drawImage(diff, 2*dx, 0);
+
+ canvas->translate(0, dy);
+ }
+ }
+
+private:
+ typedef GM INHERITED;
+};
+DEF_GM( return new DrawWithFilterGM; )
+