Make drawImage a virtual on SkDevice (patchset #3 id:40001 of https://codereview...
authorreed <reed@google.com>
Mon, 4 May 2015 17:49:41 +0000 (10:49 -0700)
committerCommit bot <commit-bot@chromium.org>
Mon, 4 May 2015 17:49:41 +0000 (10:49 -0700)
Fixed serialization from prev CL

This reverts commit 973d1f1f60f2710a268a3165aa4f3460502d4be5.

BUG=skia:3803
TBR=

Review URL: https://codereview.chromium.org/1122813002

13 files changed:
include/core/SkDevice.h
include/core/SkImage.h
src/core/SkCanvas.cpp
src/core/SkDevice.cpp
src/core/SkPictureRecord.cpp
src/core/SkPictureRecord.h
src/gpu/SkGpuDevice.cpp
src/gpu/SkGpuDevice.h
src/image/SkImage.cpp
src/image/SkImage_Base.h
src/image/SkImage_Gpu.cpp
src/image/SkImage_Gpu.h
src/image/SkImage_Raster.cpp

index 8a219d7053b0cd662fcb6fa03ba64b908be199f1..b474687cfbf0ce4c8fda8ca4a958e59585cc3415 100644 (file)
@@ -209,6 +209,10 @@ protected:
                                 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.
index ec855166195d863bd014156deda1bf03742f2451..ebc9a2939408ad113b14329d7545cb51a5b3279d 100644 (file)
@@ -172,19 +172,6 @@ private:
     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
index 3e3f714880276191cdb9a29e38c9bb7f2eba86c7..34d6aaa0ab4ddbb9ae5f158a846d77636c326642 100644 (file)
@@ -1734,12 +1734,15 @@ void SkCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
     this->onDrawPath(path, paint);
 }
 
-void SkCanvas::drawImage(const SkImage* image, SkScalar dx, SkScalar dy, const SkPaint* paint) {
-    this->onDrawImage(image, dx, dy, paint);
+void SkCanvas::drawImage(const SkImage* image, SkScalar x, SkScalar y, const SkPaint* paint) {
+    this->onDrawImage(image, x, y, 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);
 }
 
@@ -1959,15 +1962,58 @@ void SkCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
     LOOPER_END
 }
 
-void SkCanvas::onDrawImage(const SkImage* image, SkScalar dx, SkScalar dy, const SkPaint* paint) {
+void SkCanvas::onDrawImage(const SkImage* image, SkScalar x, SkScalar y, const SkPaint* paint) {
     TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawImage()");
-    image->draw(this, dx, dy, paint);
+    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
 }
 
 void SkCanvas::onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
                                const SkPaint* paint) {
     TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawImageRect()");
-    image->drawRect(this, src, dst, paint);
+    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
 }
 
 void SkCanvas::onDrawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y, const SkPaint* paint) {
index 6be9178f2386d785e5467f0b46d0aa86c343cf8a..9a43fd0e32b0ad531150f218b6919b75484548e0 100644 (file)
@@ -165,6 +165,26 @@ void SkBaseDevice::drawTextBlob(const SkDraw& draw, const SkTextBlob* blob, SkSc
     }
 }
 
+#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);
index 53ecdd0be68c08e332c93cc8598d92b32b76b264..c92ee02dbd1066e1a5f51d9b7c0e605b56f24e69 100644 (file)
@@ -7,6 +7,7 @@
 
 #include "SkPictureRecord.h"
 #include "SkDevice.h"
+#include "SkImage_Base.h"
 #include "SkPatchUtils.h"
 #include "SkPixelRef.h"
 #include "SkRRect.h"
@@ -563,6 +564,22 @@ void SkPictureRecord::onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src
     this->validate(initialOffset, size);
 }
 
+void SkPictureRecord::onDrawImage(const SkImage* image, SkScalar x, SkScalar y,
+                                  const SkPaint* paint) {
+    SkBitmap bm;
+    if (as_IB(image)->getROPixels(&bm)) {
+        this->SkPictureRecord::onDrawBitmap(bm, x, y, paint);
+    }
+}
+
+void SkPictureRecord::onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
+                                      const SkPaint* paint) {
+    SkBitmap bm;
+    if (as_IB(image)->getROPixels(&bm)) {
+        this->SkPictureRecord::onDrawBitmapRect(bm, src, dst, paint, DrawBitmapRectFlags(0));
+    }
+}
+
 void SkPictureRecord::onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
                                        const SkRect& dst, const SkPaint* paint) {
     // op + paint index + bitmap id + center + dst rect
index 5e2d5a10e483f92bd3a90726c1e30c26b12b7c10..7a6fc813a524e5b24efd5d6994e9630629bedf52 100644 (file)
@@ -184,12 +184,9 @@ protected:
     void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override;
     void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint*,
                           DrawBitmapRectFlags flags) override;
-#if 0
-    // rely on conversion to bitmap (for now)
     void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override;
     void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
                          const SkPaint*) override;
-#endif
     void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst,
                           const SkPaint*) override;
     void onDrawSprite(const SkBitmap&, int left, int top, const SkPaint*) override;
index 6ff0538ee2ca833bfbfcc145d8dbd0ba605fef4c..5fcc4355cdb9be4af20e908e19e5f4f5b5e868ea 100644 (file)
@@ -1730,6 +1730,49 @@ bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
                                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
index 30cf68daa8ad565c086f148eab86558de5d3d2d1..89959e1f9cf8c1c9e94b5c5e4bb05316c3e4d102 100644 (file)
@@ -115,6 +115,9 @@ public:
                               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;
 
index f427755d470dc4be61f436895e780c5b2fc542e9..494e21004acde43ac7e370da07765831d8d4910b 100644 (file)
@@ -25,15 +25,6 @@ uint32_t SkImage::NextUniqueID() {
     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;
index 512c80c44beca03134cbeee92585b8c46a46e85c..5daf4195e20d61659385b6cf36e07ee332e48950 100644 (file)
@@ -36,9 +36,6 @@ public:
 
     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 {
index 7ccff17d9b09fd6946685b81dce25e865c966245..eec0d1976001a7a3fddc23f797ca46089cde020d 100644 (file)
@@ -26,15 +26,6 @@ SkShader* SkImage_Gpu::onNewShader(SkShader::TileMode tileX,
     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.
index a771ddab244dd64b02412944e8e8fd11453acf8f..7b38e60ea348056e4208de9b1603ad775d2d0204 100644 (file)
@@ -21,9 +21,6 @@ public:
 
     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;
index acf805829d5d97579d97eb35e27976635fd711d4..20ae62c9f75ff191db6620e3b3cdf212f9c6129b 100644 (file)
@@ -53,8 +53,6 @@ public:
     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;
@@ -120,17 +118,6 @@ SkShader* SkImage_Raster::onNewShader(SkShader::TileMode tileX, SkShader::TileMo
     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);
 }