Bug 1367058 - Clarify CanvasRenderingContext2D::GetImageDataArray when there is no overlapping data to read. r=mchang
authorAndrew Osmond <aosmond@mozilla.com>
Thu, 25 May 2017 14:54:15 -0400
changeset 584701 6b6a15d2bf8ae957ab54f05c1a3f921ddf11f9e7
parent 584700 dd1c5aecc373a8bbba824c445a4a8c4e127adda0
child 584702 9b0c40989203ad39bc4a7bcaaeb4b97000684464
child 584710 faed4e20927536e53fbd410d60776d86394bf80e
child 584745 76c12088981a49a75d0b9d0763c286cc015f3480
child 584808 67e4f5e8a105f135f004fd40cb2e4ab28c530e8c
push id60843
push userbschouten@mozilla.com
push dateThu, 25 May 2017 21:57:22 +0000
reviewersmchang
bugs1367058
milestone55.0a1
Bug 1367058 - Clarify CanvasRenderingContext2D::GetImageDataArray when there is no overlapping data to read. r=mchang
dom/canvas/CanvasRenderingContext2D.cpp
--- a/dom/canvas/CanvasRenderingContext2D.cpp
+++ b/dom/canvas/CanvasRenderingContext2D.cpp
@@ -5867,79 +5867,71 @@ CanvasRenderingContext2D::GetImageDataAr
   if (mZero) {
     *aRetval = darray;
     return NS_OK;
   }
 
   IntRect srcRect(0, 0, mWidth, mHeight);
   IntRect destRect(aX, aY, aWidth, aHeight);
   IntRect srcReadRect = srcRect.Intersect(destRect);
+  if (srcReadRect.IsEmpty()) {
+    *aRetval = darray;
+    return NS_OK;
+  }
+
   RefPtr<DataSourceSurface> readback;
   DataSourceSurface::MappedSurface rawData;
-  if (!srcReadRect.IsEmpty()) {
-    RefPtr<SourceSurface> snapshot;
-    if (!mTarget && mBufferProvider) {
-      snapshot = mBufferProvider->BorrowSnapshot();
-    } else {
-      EnsureTarget();
-      if (!IsTargetValid()) {
-        return NS_ERROR_FAILURE;
-      }
-      snapshot = mTarget->Snapshot();
+  RefPtr<SourceSurface> snapshot;
+  if (!mTarget && mBufferProvider) {
+    snapshot = mBufferProvider->BorrowSnapshot();
+  } else {
+    EnsureTarget();
+    if (!IsTargetValid()) {
+      return NS_ERROR_FAILURE;
     }
-
-    if (snapshot) {
-      readback = snapshot->GetDataSurface();
-    }
-
-    if (!mTarget && mBufferProvider) {
-      mBufferProvider->ReturnSnapshot(snapshot.forget());
-    }
-
-    if (!readback || !readback->Map(DataSourceSurface::READ, &rawData)) {
-      return NS_ERROR_OUT_OF_MEMORY;
-    }
+    snapshot = mTarget->Snapshot();
+  }
+
+  if (snapshot) {
+    readback = snapshot->GetDataSurface();
+  }
+
+  if (!mTarget && mBufferProvider) {
+    mBufferProvider->ReturnSnapshot(snapshot.forget());
+  }
+
+  if (!readback || !readback->Map(DataSourceSurface::READ, &rawData)) {
+    return NS_ERROR_OUT_OF_MEMORY;
   }
 
   IntRect dstWriteRect = srcReadRect;
   dstWriteRect.MoveBy(-aX, -aY);
 
   {
     JS::AutoCheckCannotGC nogc;
     bool isShared;
     uint8_t* data = JS_GetUint8ClampedArrayData(darray, &isShared, nogc);
     MOZ_ASSERT(!isShared);        // Should not happen, data was created above
 
-    uint8_t* src;
-    uint32_t srcStride;
-    if (readback) {
-      srcStride = rawData.mStride;
-      src = rawData.mData + srcReadRect.y * srcStride + srcReadRect.x * 4;
-    } else {
-      src = data;
-      srcStride = aWidth * 4;
-    }
-
+    uint32_t srcStride = rawData.mStride;
+    uint8_t* src = rawData.mData + srcReadRect.y * srcStride + srcReadRect.x * 4;
     uint8_t* dst = data + dstWriteRect.y * (aWidth * 4) + dstWriteRect.x * 4;
 
     if (mOpaque) {
       SwizzleData(src, srcStride, SurfaceFormat::X8R8G8B8_UINT32,
                   dst, aWidth * 4, SurfaceFormat::R8G8B8A8,
                   dstWriteRect.Size());
     } else {
       UnpremultiplyData(src, srcStride, SurfaceFormat::A8R8G8B8_UINT32,
                         dst, aWidth * 4, SurfaceFormat::R8G8B8A8,
                         dstWriteRect.Size());
     }
   }
 
-  if (readback) {
-    readback->Unmap();
-  }
-
+  readback->Unmap();
   *aRetval = darray;
   return NS_OK;
 }
 
 void
 CanvasRenderingContext2D::EnsureErrorTarget()
 {
   if (sErrorTarget) {