Add API to copy a window

Change-Id: I9bb5209010db6665be4b6f8db81a6fc1b7debc45
diff --git a/libs/hwui/Readback.cpp b/libs/hwui/Readback.cpp
index 65a8ebb..ddca122 100644
--- a/libs/hwui/Readback.cpp
+++ b/libs/hwui/Readback.cpp
@@ -32,7 +32,8 @@
 namespace uirenderer {
 
 static CopyResult copyTextureInto(Caches& caches, RenderState& renderState,
-        Texture& sourceTexture, Matrix4& texTransform, SkBitmap* bitmap) {
+        Texture& sourceTexture, Matrix4& texTransform, const Rect& srcRect,
+        SkBitmap* bitmap) {
     int destWidth = bitmap->width();
     int destHeight = bitmap->height();
     if (destWidth > caches.maxTextureSize
@@ -100,11 +101,19 @@
         renderState.blend().syncEnabled();
         renderState.stencil().disable();
 
+        Matrix4 croppedTexTransform(texTransform);
+        if (!srcRect.isEmpty()) {
+            croppedTexTransform.loadTranslate(srcRect.left / sourceTexture.width(),
+                    srcRect.top / sourceTexture.height(), 0);
+            croppedTexTransform.scale(srcRect.getWidth() / sourceTexture.width(),
+                    srcRect.getHeight() / sourceTexture.height(), 1);
+            croppedTexTransform.multiply(texTransform);
+        }
         Glop glop;
         GlopBuilder(renderState, caches, &glop)
                 .setRoundRectClipState(nullptr)
                 .setMeshTexturedUnitQuad(nullptr)
-                .setFillExternalTexture(sourceTexture, texTransform)
+                .setFillExternalTexture(sourceTexture, croppedTexTransform)
                 .setTransform(Matrix4::identity(), TransformFlags::None)
                 .setModelViewMapUnitToRect(Rect(destWidth, destHeight))
                 .build();
@@ -126,7 +135,8 @@
 }
 
 CopyResult Readback::copySurfaceInto(renderthread::RenderThread& renderThread,
-        Surface& surface, SkBitmap* bitmap) {
+        Surface& surface, const Rect& srcRect, SkBitmap* bitmap) {
+    ATRACE_CALL();
     renderThread.eglManager().initialize();
 
     Caches& caches = Caches::getInstance();
@@ -190,7 +200,7 @@
             sourceBuffer->getWidth(), sourceBuffer->getHeight(), 0 /* total lie */);
 
     CopyResult copyResult = copyTextureInto(caches, renderThread.renderState(),
-            sourceTexture, texTransform, bitmap);
+            sourceTexture, texTransform, srcRect, bitmap);
     sourceTexture.deleteTexture();
     // All we're flushing & finishing is the deletion of the texture since
     // copyTextureInto already did a major flush & finish as an implicit
@@ -202,8 +212,9 @@
 
 CopyResult Readback::copyTextureLayerInto(renderthread::RenderThread& renderThread,
         Layer& layer, SkBitmap* bitmap) {
+    ATRACE_CALL();
     return copyTextureInto(Caches::getInstance(), renderThread.renderState(),
-            layer.getTexture(), layer.getTexTransform(), bitmap);
+            layer.getTexture(), layer.getTexTransform(), Rect(), bitmap);
 }
 
 } // namespace uirenderer
diff --git a/libs/hwui/Readback.h b/libs/hwui/Readback.h
index bd73734..55c943c 100644
--- a/libs/hwui/Readback.h
+++ b/libs/hwui/Readback.h
@@ -17,6 +17,7 @@
 #pragma once
 
 #include "renderthread/RenderThread.h"
+#include "Rect.h"
 
 #include <SkBitmap.h>
 #include <gui/Surface.h>
@@ -42,7 +43,7 @@
      * Copies the surface's most recently queued buffer into the provided bitmap.
      */
     static CopyResult copySurfaceInto(renderthread::RenderThread& renderThread,
-            Surface& surface, SkBitmap* bitmap);
+            Surface& surface, const Rect& srcRect, SkBitmap* bitmap);
 
     /**
      * Copies the TextureLayer's texture content (thus, the currently rendering buffer) into the
diff --git a/libs/hwui/renderthread/RenderProxy.cpp b/libs/hwui/renderthread/RenderProxy.cpp
index 3edd423..dcbc980 100644
--- a/libs/hwui/renderthread/RenderProxy.cpp
+++ b/libs/hwui/renderthread/RenderProxy.cpp
@@ -617,17 +617,19 @@
     post(task);
 }
 
-CREATE_BRIDGE3(copySurfaceInto, RenderThread* thread,
-        Surface* surface, SkBitmap* bitmap) {
+CREATE_BRIDGE4(copySurfaceInto, RenderThread* thread,
+        Surface* surface, Rect srcRect, SkBitmap* bitmap) {
     return (void*) Readback::copySurfaceInto(*args->thread,
-            *args->surface, args->bitmap);
+            *args->surface, args->srcRect, args->bitmap);
 }
 
-int RenderProxy::copySurfaceInto(sp<Surface>& surface, SkBitmap* bitmap) {
+int RenderProxy::copySurfaceInto(sp<Surface>& surface, int left, int top,
+        int right, int bottom,  SkBitmap* bitmap) {
     SETUP_TASK(copySurfaceInto);
     args->bitmap = bitmap;
     args->surface = surface.get();
     args->thread = &RenderThread::getInstance();
+    args->srcRect.set(left, top, right, bottom);
     return static_cast<int>(
             reinterpret_cast<intptr_t>( staticPostAndWait(task) ));
 }
diff --git a/libs/hwui/renderthread/RenderProxy.h b/libs/hwui/renderthread/RenderProxy.h
index d812970..d4aaea6 100644
--- a/libs/hwui/renderthread/RenderProxy.h
+++ b/libs/hwui/renderthread/RenderProxy.h
@@ -127,7 +127,8 @@
     ANDROID_API void removeFrameMetricsObserver(FrameMetricsObserver* observer);
     ANDROID_API long getDroppedFrameReportCount();
 
-    ANDROID_API static int copySurfaceInto(sp<Surface>& surface, SkBitmap* bitmap);
+    ANDROID_API static int copySurfaceInto(sp<Surface>& surface,
+            int left, int top, int right, int bottom, SkBitmap* bitmap);
     ANDROID_API static void prepareToDraw(const SkBitmap& bitmap);
 
 private: