Revert of make drawImage a virtual on SkDevice (patchset #3 id:40001 of https://codereview.chromium.org/1118823004/)
Reason for revert:
need to make serialization work for pictures, now that images are "real"
Original issue's description:
> make drawImage a virtual on SkDevice
>
> default impl will turn the Image into a (raster) bitmap and call drawBitmap.
>
> BUG=skia:3803
>
> Committed: https://skia.googlesource.com/skia/+/f603fb3d0e5fa57c3f228d66945bef198b001135
TBR=bsalomon@google.com,robertphillips@google.com
NOPRESUBMIT=true
NOTREECHECKS=true
NOTRY=true
BUG=skia:3803
Review URL: https://codereview.chromium.org/1122803002
diff --git a/include/core/SkDevice.h b/include/core/SkDevice.h
index b474687..8a219d7 100644
--- a/include/core/SkDevice.h
+++ b/include/core/SkDevice.h
@@ -209,10 +209,6 @@
const SkPaint& paint,
SkCanvas::DrawBitmapRectFlags flags) = 0;
- virtual void drawImage(const SkDraw&, const SkImage*, SkScalar x, SkScalar y, const SkPaint&);
- virtual void drawImageRect(const SkDraw&, const SkImage*, const SkRect* src, const SkRect& dst,
- const SkPaint&);
-
/**
* Does not handle text decoration.
* Decorations (underline and stike-thru) will be handled by SkCanvas.
diff --git a/include/core/SkImage.h b/include/core/SkImage.h
index ebc9a29..ec85516 100644
--- a/include/core/SkImage.h
+++ b/include/core/SkImage.h
@@ -172,6 +172,19 @@
static uint32_t NextUniqueID();
typedef SkRefCnt INHERITED;
+
+ friend class SkCanvas;
+
+ void draw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) const;
+
+ /**
+ * 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 drawRect(SkCanvas*, const SkRect* src, const SkRect& dst, const SkPaint*) const;
};
#endif
diff --git a/src/core/SkCanvas.cpp b/src/core/SkCanvas.cpp
index 34d6aaa..3e3f714 100644
--- a/src/core/SkCanvas.cpp
+++ b/src/core/SkCanvas.cpp
@@ -1734,15 +1734,12 @@
this->onDrawPath(path, paint);
}
-void SkCanvas::drawImage(const SkImage* image, SkScalar x, SkScalar y, const SkPaint* paint) {
- this->onDrawImage(image, x, y, paint);
+void SkCanvas::drawImage(const SkImage* image, SkScalar dx, SkScalar dy, const SkPaint* paint) {
+ this->onDrawImage(image, dx, dy, paint);
}
void SkCanvas::drawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
const SkPaint* paint) {
- if (dst.isEmpty()) {
- return;
- }
this->onDrawImageRect(image, src, dst, paint);
}
@@ -1962,58 +1959,15 @@
LOOPER_END
}
-void SkCanvas::onDrawImage(const SkImage* image, SkScalar x, SkScalar y, const SkPaint* paint) {
+void SkCanvas::onDrawImage(const SkImage* image, SkScalar dx, SkScalar dy, const SkPaint* paint) {
TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawImage()");
- SkRect bounds = SkRect::MakeXYWH(x, y,
- SkIntToScalar(image->width()), SkIntToScalar(image->height()));
- if (NULL == paint || paint->canComputeFastBounds()) {
- if (paint) {
- paint->computeFastBounds(bounds, &bounds);
- }
- if (this->quickReject(bounds)) {
- return;
- }
- }
-
- SkLazyPaint lazy;
- if (NULL == paint) {
- paint = lazy.init();
- }
-
- LOOPER_BEGIN(*paint, SkDrawFilter::kBitmap_Type, &bounds)
-
- while (iter.next()) {
- iter.fDevice->drawImage(iter, image, x, y, looper.paint());
- }
-
- LOOPER_END
+ image->draw(this, dx, dy, paint);
}
void SkCanvas::onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
const SkPaint* paint) {
TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawImageRect()");
- SkRect storage;
- const SkRect* bounds = &dst;
- if (NULL == paint || paint->canComputeFastBounds()) {
- if (paint) {
- bounds = &paint->computeFastBounds(dst, &storage);
- }
- if (this->quickReject(*bounds)) {
- return;
- }
- }
- SkLazyPaint lazy;
- if (NULL == paint) {
- paint = lazy.init();
- }
-
- LOOPER_BEGIN(*paint, SkDrawFilter::kBitmap_Type, bounds)
-
- while (iter.next()) {
- iter.fDevice->drawImageRect(iter, image, src, dst, looper.paint());
- }
-
- LOOPER_END
+ image->drawRect(this, src, dst, paint);
}
void SkCanvas::onDrawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y, const SkPaint* paint) {
diff --git a/src/core/SkDevice.cpp b/src/core/SkDevice.cpp
index 9a43fd0..6be9178 100644
--- a/src/core/SkDevice.cpp
+++ b/src/core/SkDevice.cpp
@@ -165,26 +165,6 @@
}
}
-#include "SkImage_Base.h"
-
-void SkBaseDevice::drawImage(const SkDraw& draw, const SkImage* image, SkScalar x, SkScalar y,
- const SkPaint& paint) {
- // Default impl : turns everything into raster bitmap
- SkBitmap bm;
- if (as_IB(image)->getROPixels(&bm)) {
- this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
- }
-}
-
-void SkBaseDevice::drawImageRect(const SkDraw& draw, const SkImage* image, const SkRect* src,
- const SkRect& dst, const SkPaint& paint) {
- // Default impl : turns everything into raster bitmap
- SkBitmap bm;
- if (as_IB(image)->getROPixels(&bm)) {
- this->drawBitmapRect(draw, bm, src, dst, paint, SkCanvas::DrawBitmapRectFlags(0));
- }
-}
-
bool SkBaseDevice::readPixels(const SkImageInfo& info, void* dstP, size_t rowBytes, int x, int y) {
#ifdef SK_DEBUG
SkASSERT(info.width() > 0 && info.height() > 0);
diff --git a/src/gpu/SkGpuDevice.cpp b/src/gpu/SkGpuDevice.cpp
index 5fcc435..6ff0538 100644
--- a/src/gpu/SkGpuDevice.cpp
+++ b/src/gpu/SkGpuDevice.cpp
@@ -1730,49 +1730,6 @@
filter, ctx, result, offset);
}
-#include "SkImage_Base.h"
-
-static SkImageInfo make_info(GrTexture* tex, int w, int h, bool isOpaque) {
- const GrPixelConfig config = tex->config();
- SkColorType ct;
- SkAlphaType at = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
- if (!GrPixelConfig2ColorAndProfileType(config, &ct, NULL)) {
- ct = kUnknown_SkColorType;
- }
- return SkImageInfo::Make(w, h, ct, at);
-}
-
-static bool wrap_as_bm(const SkImage* image, SkBitmap* bm) {
- GrTexture* tex = image->getTexture();
- if (tex) {
- // TODO: handle the GrTexture directly, and skip GrPixelRef
- const SkImageInfo info = make_info(tex, image->width(), image->height(), image->isOpaque());
- bm->setInfo(info);
- bm->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, tex)))->unref();
- } else {
- if (!as_IB(image)->getROPixels(bm)) {
- return false;
- }
- }
- return true;
-}
-
-void SkGpuDevice::drawImage(const SkDraw& draw, const SkImage* image, SkScalar x, SkScalar y,
- const SkPaint& paint) {
- SkBitmap bm;
- if (wrap_as_bm(image, &bm)) {
- this->drawBitmap(draw, bm, SkMatrix::MakeTrans(x, y), paint);
- }
-}
-
-void SkGpuDevice::drawImageRect(const SkDraw& draw, const SkImage* image, const SkRect* src,
- const SkRect& dst, const SkPaint& paint) {
- SkBitmap bm;
- if (wrap_as_bm(image, &bm)) {
- this->drawBitmapRect(draw, bm, src, dst, paint, SkCanvas::DrawBitmapRectFlags(0));
- }
-}
-
///////////////////////////////////////////////////////////////////////////////
// must be in SkCanvas::VertexMode order
diff --git a/src/gpu/SkGpuDevice.h b/src/gpu/SkGpuDevice.h
index 89959e1..30cf68d 100644
--- a/src/gpu/SkGpuDevice.h
+++ b/src/gpu/SkGpuDevice.h
@@ -115,9 +115,6 @@
const SkPaint&) override;
virtual void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y,
const SkPaint&) override;
- void drawImage(const SkDraw&, const SkImage*, SkScalar x, SkScalar y, const SkPaint&) override;
- void drawImageRect(const SkDraw&, const SkImage*, const SkRect* src, const SkRect& dst,
- const SkPaint&) override;
void flush() override;
diff --git a/src/image/SkImage.cpp b/src/image/SkImage.cpp
index 494e210..f427755 100644
--- a/src/image/SkImage.cpp
+++ b/src/image/SkImage.cpp
@@ -25,6 +25,15 @@
return id;
}
+void SkImage::draw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const {
+ as_IB(this)->onDraw(canvas, x, y, paint);
+}
+
+void SkImage::drawRect(SkCanvas* canvas, const SkRect* src, const SkRect& dst,
+ const SkPaint* paint) const {
+ as_IB(this)->onDrawRect(canvas, src, dst, paint);
+}
+
const void* SkImage::peekPixels(SkImageInfo* info, size_t* rowBytes) const {
SkImageInfo infoStorage;
size_t rowBytesStorage;
diff --git a/src/image/SkImage_Base.h b/src/image/SkImage_Base.h
index 5daf419..512c80c 100644
--- a/src/image/SkImage_Base.h
+++ b/src/image/SkImage_Base.h
@@ -36,6 +36,9 @@
const SkSurfaceProps& props() const { return fProps; }
+ virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) const = 0;
+ virtual void onDrawRect(SkCanvas*, const SkRect* src,
+ const SkRect& dst, const SkPaint*) const = 0;
virtual SkSurface* onNewSurface(const SkImageInfo&, const SkSurfaceProps&) const = 0;
virtual const void* onPeekPixels(SkImageInfo*, size_t* /*rowBytes*/) const {
diff --git a/src/image/SkImage_Gpu.cpp b/src/image/SkImage_Gpu.cpp
index eec0d19..7ccff17 100644
--- a/src/image/SkImage_Gpu.cpp
+++ b/src/image/SkImage_Gpu.cpp
@@ -26,6 +26,15 @@
return SkShader::CreateBitmapShader(fBitmap, tileX, tileY, localMatrix);
}
+void SkImage_Gpu::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const {
+ canvas->drawBitmap(fBitmap, x, y, paint);
+}
+
+void SkImage_Gpu::onDrawRect(SkCanvas* canvas, const SkRect* src, const SkRect& dst,
+ const SkPaint* paint) const {
+ canvas->drawBitmapRectToRect(fBitmap, src, dst, paint);
+}
+
SkSurface* SkImage_Gpu::onNewSurface(const SkImageInfo& info, const SkSurfaceProps& props) const {
GrContext* ctx = this->getTexture()->getContext();
// TODO: Change signature of onNewSurface to take a budgeted param.
diff --git a/src/image/SkImage_Gpu.h b/src/image/SkImage_Gpu.h
index 7b38e60..a771dda 100644
--- a/src/image/SkImage_Gpu.h
+++ b/src/image/SkImage_Gpu.h
@@ -21,6 +21,9 @@
SkImage_Gpu(const SkBitmap&, int sampleCountForNewSurfaces, SkSurface::Budgeted);
+ void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) const override;
+ void onDrawRect(SkCanvas*, const SkRect* src, const SkRect& dst,
+ const SkPaint*) const override;
SkSurface* onNewSurface(const SkImageInfo&, const SkSurfaceProps&) const override;
GrTexture* onGetTexture() const override;
bool getROPixels(SkBitmap*) const override;
diff --git a/src/image/SkImage_Raster.cpp b/src/image/SkImage_Raster.cpp
index 20ae62c..acf8058 100644
--- a/src/image/SkImage_Raster.cpp
+++ b/src/image/SkImage_Raster.cpp
@@ -53,6 +53,8 @@
SkImage_Raster(const SkImageInfo&, SkData*, size_t rb, const SkSurfaceProps*);
virtual ~SkImage_Raster();
+ void onDraw(SkCanvas*, SkScalar, SkScalar, const SkPaint*) const override;
+ void onDrawRect(SkCanvas*, const SkRect*, const SkRect&, const SkPaint*) const override;
SkSurface* onNewSurface(const SkImageInfo&, const SkSurfaceProps&) const override;
bool onReadPixels(const SkImageInfo&, void*, size_t, int srcX, int srcY) const override;
const void* onPeekPixels(SkImageInfo*, size_t* /*rowBytes*/) const override;
@@ -118,6 +120,17 @@
return SkShader::CreateBitmapShader(fBitmap, tileX, tileY, localMatrix);
}
+void SkImage_Raster::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const {
+ SkBitmap shallowCopy(fBitmap);
+ canvas->drawBitmap(shallowCopy, x, y, paint);
+}
+
+void SkImage_Raster::onDrawRect(SkCanvas* canvas, const SkRect* src, const SkRect& dst,
+ const SkPaint* paint) const {
+ SkBitmap shallowCopy(fBitmap);
+ canvas->drawBitmapRectToRect(shallowCopy, src, dst, paint);
+}
+
SkSurface* SkImage_Raster::onNewSurface(const SkImageInfo& info, const SkSurfaceProps& props) const {
return SkSurface::NewRaster(info, &props);
}