Add SkImage->draw() call with src and dst rects.
R=junov@chromium.org, senorblanco@chromium.org, reed@google.com
Author: arbesfeld@chromium.org
Review URL: https://chromiumcodereview.appspot.com/19729007
git-svn-id: http://skia.googlecode.com/svn/trunk@10237 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/gm/image.cpp b/gm/image.cpp
index 71fe0e1..7ac2fe9 100644
--- a/gm/image.cpp
+++ b/gm/image.cpp
@@ -36,11 +36,18 @@
// rebaseline.
SkAutoDataUnref data(fileToData("/Users/mike/Downloads/skia.google.jpeg"));
SkImage* image = SkImage::NewEncodedData(data);
+
if (image) {
+ SkRect* src = NULL;
+ SkRect dst;
+ src->set(0, 0, image->width(), image->height());
+ dst.set(0, 0, size.width(), size.height());
+
SkAutoCanvasRestore acr(canvas, true);
canvas->scale(size.width() * 1.0f / image->width(),
size.height() * 1.0f / image->height());
- image->draw(canvas,0, 0, NULL);
+ image->draw(canvas, 0, 0, NULL);
+ image->draw(canvas, src, dst, NULL);
image->unref();
}
}
@@ -65,7 +72,7 @@
canvas->drawCircle(size.fWidth/2, size.fHeight/2, radius, paint);
}
-static void test_surface(SkCanvas* canvas, SkSurface* surf) {
+static void test_surface(SkCanvas* canvas, SkSurface* surf, bool usePaint) {
drawContents(surf, SK_ColorRED);
SkImage* imgR = surf->newImageSnapshot();
@@ -87,9 +94,26 @@
// paint.setFilterBitmap(true);
// paint.setAlpha(0x80);
- imgR->draw(canvas, 0, 0, &paint);
- imgG->draw(canvas, 0, 80, &paint);
- surf->draw(canvas, 0, 160, &paint);
+ imgR->draw(canvas, 0, 0, usePaint ? &paint : NULL);
+ imgG->draw(canvas, 0, 80, usePaint ? &paint : NULL);
+ surf->draw(canvas, 0, 160, usePaint ? &paint : NULL);
+
+ SkRect src1, src2, src3;
+ src1.set(0, 0, surf->width(), surf->height());
+ src2.set(-surf->width() / 2, -surf->height() / 2,
+ surf->width(), surf->height());
+ src3.set(0, 0, surf->width() / 2, surf->height() / 2);
+
+ SkRect dst1, dst2, dst3, dst4;
+ dst1.set(0, 240, 65, 305);
+ dst2.set(0, 320, 65, 385);
+ dst3.set(0, 400, 65, 465);
+ dst4.set(0, 480, 65, 545);
+
+ imgR->draw(canvas, &src1, dst1, usePaint ? &paint : NULL);
+ imgG->draw(canvas, &src2, dst2, usePaint ? &paint : NULL);
+ imgR->draw(canvas, &src3, dst3, usePaint ? &paint : NULL);
+ imgG->draw(canvas, NULL, dst4, usePaint ? &paint : NULL);
imgG->unref();
imgR->unref();
@@ -122,7 +146,7 @@
}
virtual SkISize onISize() {
- return SkISize::Make(800, 500);
+ return SkISize::Make(960, 1200);
}
virtual void onDraw(SkCanvas* canvas) {
@@ -133,22 +157,32 @@
static const char* kLabel1 = "Original Img";
static const char* kLabel2 = "Modified Img";
static const char* kLabel3 = "Cur Surface";
+ static const char* kLabel4 = "Full Crop";
+ static const char* kLabel5 = "Over-crop";
+ static const char* kLabel6 = "Upper-left";
+ static const char* kLabel7 = "No Crop";
- static const char* kLabel4 = "Pre-Alloc Img";
- static const char* kLabel5 = "New Alloc Img";
- static const char* kLabel6 = "SkPicture";
- static const char* kLabel7 = "GPU";
+ static const char* kLabel8 = "Pre-Alloc Img";
+ static const char* kLabel9 = "New Alloc Img";
+ static const char* kLabel10 = "SkPicture";
+ static const char* kLabel11 = "Null Paint";
+ static const char* kLabel12 = "GPU";
SkPaint textPaint;
canvas->drawText(kLabel1, strlen(kLabel1), 10, 60, textPaint);
canvas->drawText(kLabel2, strlen(kLabel2), 10, 140, textPaint);
canvas->drawText(kLabel3, strlen(kLabel3), 10, 220, textPaint);
+ canvas->drawText(kLabel4, strlen(kLabel4), 10, 300, textPaint);
+ canvas->drawText(kLabel5, strlen(kLabel5), 10, 380, textPaint);
+ canvas->drawText(kLabel6, strlen(kLabel6), 10, 460, textPaint);
+ canvas->drawText(kLabel7, strlen(kLabel7), 10, 540, textPaint);
- canvas->drawText(kLabel4, strlen(kLabel4), 80, 10, textPaint);
- canvas->drawText(kLabel5, strlen(kLabel5), 160, 10, textPaint);
- canvas->drawText(kLabel6, strlen(kLabel6), 250, 10, textPaint);
- canvas->drawText(kLabel7, strlen(kLabel7), 340, 10, textPaint);
+ canvas->drawText(kLabel8, strlen(kLabel8), 80, 10, textPaint);
+ canvas->drawText(kLabel9, strlen(kLabel9), 160, 10, textPaint);
+ canvas->drawText(kLabel10, strlen(kLabel10), 250, 10, textPaint);
+ canvas->drawText(kLabel11, strlen(kLabel11), 320, 10, textPaint);
+ canvas->drawText(kLabel12, strlen(kLabel12), 410, 10, textPaint);
canvas->translate(80, 20);
@@ -164,21 +198,24 @@
SkAutoTUnref<SkSurface> surf0(SkSurface::NewRasterDirect(info, fBuffer, RB));
SkAutoTUnref<SkSurface> surf1(SkSurface::NewRaster(info));
SkAutoTUnref<SkSurface> surf2(SkSurface::NewPicture(info.fWidth, info.fHeight));
+ SkAutoTUnref<SkSurface> surf3(SkSurface::NewPicture(info.fWidth, info.fHeight));
#if SK_SUPPORT_GPU
GrContext* ctx = skiagm::GetGr();
- SkAutoTUnref<SkSurface> surf3(SkSurface::NewRenderTarget(ctx, info, 0));
+ SkAutoTUnref<SkSurface> surf4(SkSurface::NewRenderTarget(ctx, info, 0));
#endif
- test_surface(canvas, surf0);
+ test_surface(canvas, surf0, true);
canvas->translate(80, 0);
- test_surface(canvas, surf1);
+ test_surface(canvas, surf1, true);
canvas->translate(80, 0);
- test_surface(canvas, surf2);
+ test_surface(canvas, surf2, true);
+ canvas->translate(80, 0);
+ test_surface(canvas, surf3, false);
#if SK_SUPPORT_GPU
if (NULL != ctx) {
canvas->translate(80, 0);
- test_surface(canvas, surf3);
+ test_surface(canvas, surf4, true);
}
#endif
}
diff --git a/include/core/SkImage.h b/include/core/SkImage.h
index c2ee509..85875d5 100644
--- a/include/core/SkImage.h
+++ b/include/core/SkImage.h
@@ -84,6 +84,15 @@
void draw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*);
/**
+ * Draw the image, cropped to the src rect, to the dst rect of a canvas.
+ * If src is larger than the bounds of the image, the rest of the image is
+ * filled with transparent black pixels.
+ *
+ * See SkCanvas::drawBitmapRectToRect for similar behavior.
+ */
+ void draw(SkCanvas*, const SkRect* src, const SkRect& dst, const SkPaint*);
+
+ /**
* Encode the image's pixels and return the result as a new SkData, which
* the caller must manage (i.e. call unref() when they are done).
*
diff --git a/src/image/SkImage.cpp b/src/image/SkImage.cpp
index 9c60f55..39fd93a 100644
--- a/src/image/SkImage.cpp
+++ b/src/image/SkImage.cpp
@@ -36,6 +36,11 @@
as_IB(this)->onDraw(canvas, x, y, paint);
}
+void SkImage::draw(SkCanvas* canvas, const SkRect* src, const SkRect& dst,
+ const SkPaint* paint) {
+ as_IB(this)->onDrawRectToRect(canvas, src, dst, paint);
+}
+
GrTexture* SkImage::getTexture() {
return as_IB(this)->onGetTexture();
}
diff --git a/src/image/SkImagePriv.cpp b/src/image/SkImagePriv.cpp
index c44e439..4ff1c92 100644
--- a/src/image/SkImagePriv.cpp
+++ b/src/image/SkImagePriv.cpp
@@ -141,3 +141,33 @@
canvas->drawPicture(*picture);
canvas->restoreToCount(saveCount);
}
+
+void SkImagePrivDrawPicture(SkCanvas* canvas, SkPicture* picture,
+ const SkRect* src, const SkRect& dst, const SkPaint* paint) {
+ int saveCount = canvas->getSaveCount();
+
+ SkMatrix matrix;
+ SkRect tmpSrc;
+
+ if (NULL != src) {
+ tmpSrc = *src;
+ } else {
+ tmpSrc.set(0, 0,
+ SkIntToScalar(picture->width()),
+ SkIntToScalar(picture->height()));
+ }
+
+ matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
+ if (paint && needs_layer(*paint)) {
+ canvas->saveLayer(&dst, paint);
+ } else {
+ canvas->save();
+ }
+ canvas->concat(matrix);
+ if (!paint || !needs_layer(*paint)) {
+ canvas->clipRect(tmpSrc);
+ }
+
+ canvas->drawPicture(*picture);
+ canvas->restoreToCount(saveCount);
+}
diff --git a/src/image/SkImagePriv.h b/src/image/SkImagePriv.h
index fc14f67..2af8bbe 100644
--- a/src/image/SkImagePriv.h
+++ b/src/image/SkImagePriv.h
@@ -39,6 +39,9 @@
extern void SkImagePrivDrawPicture(SkCanvas*, SkPicture*,
SkScalar x, SkScalar y, const SkPaint*);
+extern void SkImagePrivDrawPicture(SkCanvas*, SkPicture*,
+ const SkRect*, const SkRect&, const SkPaint*);
+
/**
* Return an SkImage whose contents are those of the specified picture. Note:
* The picture itself is unmodified, and may continue to be used for recording
diff --git a/src/image/SkImage_Base.h b/src/image/SkImage_Base.h
index 9ae13de..431444d 100644
--- a/src/image/SkImage_Base.h
+++ b/src/image/SkImage_Base.h
@@ -15,6 +15,8 @@
SkImage_Base(int width, int height) : INHERITED(width, height) {}
virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) = 0;
+ virtual void onDrawRectToRect(SkCanvas*, const SkRect* src,
+ const SkRect& dst, const SkPaint*) = 0;
virtual GrTexture* onGetTexture() { return NULL; }
// return a read-only copy of the pixels. We promise to not modify them,
diff --git a/src/image/SkImage_Codec.cpp b/src/image/SkImage_Codec.cpp
index 61661de..b9a8990 100644
--- a/src/image/SkImage_Codec.cpp
+++ b/src/image/SkImage_Codec.cpp
@@ -19,6 +19,7 @@
virtual ~SkImage_Codec();
virtual void onDraw(SkCanvas*, SkScalar, SkScalar, const SkPaint*) SK_OVERRIDE;
+ virtual void onDrawRectToRect(SkCanvas*, const SkRect*, const SkRect&, const SkPaint*) SK_OVERRIDE;
private:
SkData* fEncodedData;
@@ -48,6 +49,17 @@
canvas->drawBitmap(fBitmap, x, y, paint);
}
+void SkImage_Codec::onDrawRectToRect(SkCanvas* canvas, const SkRect* src,
+ const SkRect& dst, const SkPaint* paint) {
+ if (!fBitmap.pixelRef()) {
+ if (!SkImageDecoder::DecodeMemory(fEncodedData->bytes(), fEncodedData->size(),
+ &fBitmap)) {
+ return;
+ }
+ }
+ canvas->drawBitmapRectToRect(fBitmap, src, dst, paint);
+}
+
///////////////////////////////////////////////////////////////////////////////
SkImage* SkImage::NewEncodedData(SkData* data) {
diff --git a/src/image/SkImage_Gpu.cpp b/src/image/SkImage_Gpu.cpp
index b6e4211..d19d210 100644
--- a/src/image/SkImage_Gpu.cpp
+++ b/src/image/SkImage_Gpu.cpp
@@ -21,6 +21,7 @@
virtual ~SkImage_Gpu();
virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) SK_OVERRIDE;
+ virtual void onDrawRectToRect(SkCanvas*, const SkRect* src, const SkRect& dst, const SkPaint*) SK_OVERRIDE;
virtual GrTexture* onGetTexture() SK_OVERRIDE;
virtual bool getROPixels(SkBitmap*) const SK_OVERRIDE {
// TODO
@@ -61,6 +62,11 @@
canvas->drawBitmap(fBitmap, x, y, paint);
}
+void SkImage_Gpu::onDrawRectToRect(SkCanvas* canvas, const SkRect* src, const SkRect& dst,
+ const SkPaint* paint) {
+ canvas->drawBitmapRectToRect(fBitmap, src, dst, paint);
+}
+
GrTexture* SkImage_Gpu::onGetTexture() {
return fTexture;
}
diff --git a/src/image/SkImage_Picture.cpp b/src/image/SkImage_Picture.cpp
index be934fa..87221de 100644
--- a/src/image/SkImage_Picture.cpp
+++ b/src/image/SkImage_Picture.cpp
@@ -15,6 +15,7 @@
virtual ~SkImage_Picture();
virtual void onDraw(SkCanvas*, SkScalar, SkScalar, const SkPaint*) SK_OVERRIDE;
+ virtual void onDrawRectToRect(SkCanvas*, const SkRect*, const SkRect&, const SkPaint*) SK_OVERRIDE;
SkPicture* getPicture() { return fPicture; }
@@ -41,6 +42,11 @@
SkImagePrivDrawPicture(canvas, fPicture, x, y, paint);
}
+void SkImage_Picture::onDrawRectToRect(SkCanvas* canvas, const SkRect* src, const SkRect& dst,
+ const SkPaint* paint) {
+ SkImagePrivDrawPicture(canvas, fPicture, src, dst, paint);
+}
+
SkImage* SkNewImageFromPicture(const SkPicture* srcPicture) {
/**
* We want to snapshot the playback status of the picture, w/o affecting
diff --git a/src/image/SkImage_Raster.cpp b/src/image/SkImage_Raster.cpp
index 5e1e8a0..3e26856 100644
--- a/src/image/SkImage_Raster.cpp
+++ b/src/image/SkImage_Raster.cpp
@@ -55,6 +55,7 @@
virtual ~SkImage_Raster();
virtual void onDraw(SkCanvas*, SkScalar, SkScalar, const SkPaint*) SK_OVERRIDE;
+ virtual void onDrawRectToRect(SkCanvas*, const SkRect*, const SkRect&, const SkPaint*) SK_OVERRIDE;
virtual bool getROPixels(SkBitmap*) const SK_OVERRIDE;
// exposed for SkSurface_Raster via SkNewImageFromPixelRef
@@ -109,6 +110,10 @@
canvas->drawBitmap(fBitmap, x, y, paint);
}
+void SkImage_Raster::onDrawRectToRect(SkCanvas* canvas, const SkRect* src, const SkRect& dst, const SkPaint* paint) {
+ canvas->drawBitmapRectToRect(fBitmap, src, dst, paint);
+}
+
bool SkImage_Raster::getROPixels(SkBitmap* dst) const {
*dst = fBitmap;
return true;