add writePixels to SkBitmap
authorMike Reed <reed@google.com>
Wed, 4 Jan 2017 21:34:31 +0000 (16:34 -0500)
committerSkia Commit-Bot <skia-commit-bot@chromium.org>
Wed, 4 Jan 2017 22:05:27 +0000 (22:05 +0000)
BUG=skia:

Change-Id: I6f4db9ddc0364d9785e0f1794d86b73e66845ea9
Reviewed-on: https://skia-review.googlesource.com/6593
Commit-Queue: Mike Reed <reed@google.com>
Reviewed-by: Florin Malita <fmalita@chromium.org>
include/core/SkBitmap.h
src/core/SkBitmap.cpp
src/core/SkBitmapDevice.cpp

index 1f195c0..4d2af43 100644 (file)
@@ -644,6 +644,21 @@ public:
      */
     bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
                     int srcX, int srcY) const;
+    bool readPixels(const SkPixmap& dst, int srcX, int srcY) const;
+    bool readPixels(const SkPixmap& dst) const {
+        return this->readPixels(dst, 0, 0);
+    }
+
+    /**
+     *  Copy the src pixmap's pixels into this bitmap, offset by dstX, dstY.
+     *
+     *  This is logically the same as creating a bitmap around src, and calling readPixels on it
+     *  with this bitmap as the dst.
+     */
+    bool writePixels(const SkPixmap& src, int dstX, int dstY);
+    bool writePixels(const SkPixmap& src) {
+        return this->writePixels(src, 0, 0);
+    }
 
     /**
      *  Returns true if this bitmap's pixels can be converted into the requested
index 422faa5..e7c75b2 100644 (file)
@@ -713,6 +713,25 @@ bool SkBitmap::readPixels(const SkImageInfo& requestedDstInfo, void* dstPixels,
     return src.pixmap().readPixels(requestedDstInfo, dstPixels, dstRB, x, y);
 }
 
+bool SkBitmap::readPixels(const SkPixmap& dst, int srcX, int srcY) const {
+    return this->readPixels(dst.info(), dst.writable_addr(), dst.rowBytes(), srcX, srcY);
+}
+
+bool SkBitmap::writePixels(const SkPixmap& src, int dstX, int dstY) {
+    SkAutoPixmapUnlock dst;
+    if (!this->requestLock(&dst)) {
+        return false;
+    }
+
+    SkPixmap subset;
+    if (!dst.pixmap().extractSubset(&subset,
+                                    SkIRect::MakeXYWH(dstX, dstY, src.width(), src.height()))) {
+        return false;
+    }
+
+    return src.readPixels(subset);
+}
+
 bool SkBitmap::copyTo(SkBitmap* dst, SkColorType dstColorType, Allocator* alloc) const {
     if (!this->canCopyTo(dstColorType)) {
         return false;
index a5dadbb..8140190 100644 (file)
@@ -167,12 +167,7 @@ bool SkBitmapDevice::onWritePixels(const SkImageInfo& srcInfo, const void* srcPi
         return false;
     }
 
-    const SkImageInfo dstInfo = fBitmap.info().makeWH(srcInfo.width(), srcInfo.height());
-
-    void* dstPixels = fBitmap.getAddr(x, y);
-    size_t dstRowBytes = fBitmap.rowBytes();
-
-    if (SkPixelInfo::CopyPixels(dstInfo, dstPixels, dstRowBytes, srcInfo, srcPixels, srcRowBytes)) {
+    if (fBitmap.writePixels(SkPixmap(srcInfo, srcPixels, srcRowBytes), x, y)) {
         fBitmap.notifyPixelsChanged();
         return true;
     }