Adds read pixels to GrTexture and GrRenderTarget
Adds SkGrRenderTargetPixelRef for SkBitmaps that are backed by RTs that aren't textures.
Adds onReadPixels implementations for SkGr pixel ref types



git-svn-id: http://skia.googlecode.com/svn/trunk@1056 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/gpu/include/GrContext.h b/gpu/include/GrContext.h
index cccc14f..c178ed6 100644
--- a/gpu/include/GrContext.h
+++ b/gpu/include/GrContext.h
@@ -423,20 +423,49 @@
      *                          FlushBits.
      */
     void flush(int flagsBitfield = 0);
+    
     /**
-     *  Return true on success, i.e. if we could copy the specified range of
-     *  pixels from the current render-target into the buffer, converting into
-     *  the specified pixel-config.
+     * Reads a rectangle of pixels from a render target.
+     * @param renderTarget  the render target to read from. NULL means the
+     *                      current render target.
+     * @param left          left edge of the rectangle to read (inclusive)
+     * @param top           top edge of the rectangle to read (inclusive)
+     * @param width         width of rectangle to read in pixels.
+     * @param height        height of rectangle to read in pixels.
+     * @param config        the pixel config of the destination buffer
+     * @param buffer        memory to read the rectangle into.
+     *
+     * @return true if the read succeeded, false if not. The read can fail
+     *              because of a unsupported pixel config or because no render
+     *              target is currently set.
      */
-    bool readPixels(int left, int top, int width, int height,
-                    GrTexture::PixelConfig, void* buffer);
+    bool readRenderTargetPixels(GrRenderTarget* target,
+                                int left, int top, int width, int height,
+                                GrPixelConfig config, void* buffer);
+
+    /**
+     * Reads a rectangle of pixels from a texture.
+     * @param texture       the render target to read from.
+     * @param left          left edge of the rectangle to read (inclusive)
+     * @param top           top edge of the rectangle to read (inclusive)
+     * @param width         width of rectangle to read in pixels.
+     * @param height        height of rectangle to read in pixels.
+     * @param config        the pixel config of the destination buffer
+     * @param buffer        memory to read the rectangle into.
+     *
+     * @return true if the read succeeded, false if not. The read can fail
+     *              because of a unsupported pixel config.
+     */
+    bool readTexturePixels(GrTexture* target,
+                           int left, int top, int width, int height,
+                           GrPixelConfig config, void* buffer);
 
     /**
      *  Copy the src pixels [buffer, stride, pixelconfig] into the current
      *  render-target at the specified rectangle.
      */
     void writePixels(int left, int top, int width, int height,
-                     GrTexture::PixelConfig, const void* buffer, size_t stride);
+                     GrPixelConfig, const void* buffer, size_t stride);
 
 
     ///////////////////////////////////////////////////////////////////////////
diff --git a/gpu/include/GrGLTexture.h b/gpu/include/GrGLTexture.h
index 0624f40..2cd833c 100644
--- a/gpu/include/GrGLTexture.h
+++ b/gpu/include/GrGLTexture.h
@@ -180,17 +180,17 @@
 protected:
 
     struct GLTextureDesc {
-        uint32_t    fContentWidth;
-        uint32_t    fContentHeight;
-        uint32_t    fAllocWidth;
-        uint32_t    fAllocHeight;
-        PixelConfig fFormat;
-        GrGLuint    fTextureID;
-        GrGLenum    fUploadFormat;
-        GrGLenum    fUploadByteCount;
-        GrGLenum    fUploadType;
-        GrGLuint    fStencilBits;
-        Orientation fOrientation;
+        uint32_t        fContentWidth;
+        uint32_t        fContentHeight;
+        uint32_t        fAllocWidth;
+        uint32_t        fAllocHeight;
+        GrPixelConfig   fFormat;
+        GrGLuint        fTextureID;
+        GrGLenum        fUploadFormat;
+        GrGLenum        fUploadByteCount;
+        GrGLenum        fUploadType;
+        GrGLuint        fStencilBits;
+        Orientation     fOrientation;
     };
     typedef GrGLRenderTarget::GLRenderTargetIDs GLRenderTargetIDs;
     GrGLTexture(GrGpuGL* gpu,
diff --git a/gpu/include/GrGpu.h b/gpu/include/GrGpu.h
index c4f5e4b..cbe3e95 100644
--- a/gpu/include/GrGpu.h
+++ b/gpu/include/GrGpu.h
@@ -17,15 +17,16 @@
 #ifndef GrGpu_DEFINED
 #define GrGpu_DEFINED
 
+#include "GrDrawTarget.h"
+#include "GrPathRenderer.h"
 #include "GrRect.h"
 #include "GrRefCnt.h"
-#include "GrDrawTarget.h"
 #include "GrTexture.h"
-#include "GrPathRenderer.h"
 
-class GrVertexBufferAllocPool;
+class GrContext;
 class GrIndexBufferAllocPool;
 class GrResource;
+class GrVertexBufferAllocPool;
 
 class GrGpu : public GrDrawTarget {
 
@@ -104,7 +105,7 @@
                                         //   kRenderTarget_TextureFlag.
         uint32_t               fWidth;  //!< Width of the texture
         uint32_t               fHeight; //!< Height of the texture
-        GrTexture::PixelConfig fFormat; //!< Format of source data of the
+        GrPixelConfig          fFormat; //!< Format of source data of the
                                         //   texture. Not guaraunteed to be the
                                         //   same as internal format used by
                                         //   3D API.
@@ -143,6 +144,14 @@
     GrGpu();
     virtual ~GrGpu();
 
+    // The GrContext sets itself as the owner of this Gpu object
+    void setContext(GrContext* context) {
+        GrAssert(NULL == fContext); 
+        fContext = context;
+    }
+    GrContext* getContext() { return fContext; }
+    const GrContext* getContext() const { return fContext; }
+
     /**
      * The GrGpu object normally assumes that no outsider is setting state
      * within the underlying 3D API's context/device/whatever. This call informs
@@ -340,19 +349,23 @@
     void forceRenderTargetFlush();
 
     /**
-     * Reads a rectangle of pixels from the current render target.
-     * @param left      left edge of the rectangle to read (inclusive)
-     * @param top       top edge of the rectangle to read (inclusive)
-     * @param width     width of rectangle to read in pixels.
-     * @param height    height of rectangle to read in pixels.
-     * @param buffer    memory to read the rectangle into.
+     * Reads a rectangle of pixels from a render target.
+     * @param renderTarget  the render target to read from. NULL means the
+     *                      current render target.
+     * @param left          left edge of the rectangle to read (inclusive)
+     * @param top           top edge of the rectangle to read (inclusive)
+     * @param width         width of rectangle to read in pixels.
+     * @param height        height of rectangle to read in pixels.
+     * @param config        the pixel config of the destination buffer
+     * @param buffer        memory to read the rectangle into.
      *
      * @return true if the read succeeded, false if not. The read can fail
      *              because of a unsupported pixel config or because no render
      *              target is currently set.
      */
-    bool readPixels(int left, int top, int width, int height,
-                    GrTexture::PixelConfig, void* buffer);
+    bool readPixels(GrRenderTarget* renderTarget,
+                    int left, int top, int width, int height,
+                    GrPixelConfig config, void* buffer);
 
     const Stats& getStats() const;
     void resetStats();
@@ -499,7 +512,7 @@
 
     // overridden by API-specific derived class to perform the read pixels.
     virtual bool readPixelsHelper(int left, int top, int width, int height,
-                                  GrTexture::PixelConfig, void* buffer) = 0;
+                                  GrPixelConfig, void* buffer) = 0;
 
     // called to program the vertex data, indexCount will be 0 if drawing non-
     // indexed geometry. The subclass may adjust the startVertex and/or
@@ -523,20 +536,7 @@
     virtual void eraseStencilClip(const GrIRect& rect) = 0;
 
 private:
-    // readies the pools to provide vertex/index data.
-    void prepareVertexPool();
-    void prepareIndexPool();
-
-    // determines the path renderer used to draw a clip path element.
-    GrPathRenderer* getClipPathRenderer(GrPathIter* path,
-                                        GrPathFill fill);
-
-    void handleDirtyContext() {
-        if (fContextIsDirty) {
-            this->resetContext();
-            fContextIsDirty = false;
-        }
-    }
+    GrContext*                  fContext; // not reffed (context refs gpu)
 
     GrVertexBufferAllocPool*    fVertexPool;
 
@@ -561,6 +561,21 @@
 
     GrResource*                 fResourceHead;
 
+    // readies the pools to provide vertex/index data.
+    void prepareVertexPool();
+    void prepareIndexPool();
+
+    // determines the path renderer used to draw a clip path element.
+    GrPathRenderer* getClipPathRenderer(GrPathIter* path,
+                                        GrPathFill fill);
+
+    void handleDirtyContext() {
+        if (fContextIsDirty) {
+            this->resetContext();
+            fContextIsDirty = false;
+        }
+    }
+
     // used to save and restore state when the GrGpu needs
     // to make its geometry pools available internally
     class AutoInternalDrawGeomRestore {
diff --git a/gpu/include/GrTexture.h b/gpu/include/GrTexture.h
index d1853e3..2aa80ab 100644
--- a/gpu/include/GrTexture.h
+++ b/gpu/include/GrTexture.h
@@ -52,6 +52,21 @@
      */
     GrTexture* asTexture() {return fTexture;}
 
+    /**
+     * Reads a rectangle of pixels from the render target.
+     * @param left          left edge of the rectangle to read (inclusive)
+     * @param top           top edge of the rectangle to read (inclusive)
+     * @param width         width of rectangle to read in pixels.
+     * @param height        height of rectangle to read in pixels.
+     * @param config        the pixel config of the destination buffer
+     * @param buffer        memory to read the rectangle into.
+     *
+     * @return true if the read succeeded, false if not. The read can fail
+     *              because of a unsupported pixel config.
+     */
+    bool readPixels(int left, int top, int width, int height,
+                    GrPixelConfig config, void* buffer);
+
 protected:
     GrRenderTarget(GrGpu* gpu,
                    GrTexture* texture,
@@ -81,25 +96,11 @@
 };
 
 class GrTexture : public GrResource {
-public:
-    enum PixelConfig {
-        kUnknown_PixelConfig,
-        kAlpha_8_PixelConfig,
-        kIndex_8_PixelConfig,
-        kRGB_565_PixelConfig,
-        kRGBA_4444_PixelConfig, //!< premultiplied
-        kRGBA_8888_PixelConfig, //!< premultiplied
-        kRGBX_8888_PixelConfig, //!< treat the alpha channel as opaque
-    };
-    static size_t BytesPerPixel(PixelConfig);
-    static bool PixelConfigIsOpaque(PixelConfig);
-    static bool PixelConfigIsAlphaOnly(PixelConfig);
-
 protected:
     GrTexture(GrGpu* gpu,
               int width,
               int height,
-              PixelConfig config)
+              GrPixelConfig config)
     : INHERITED(gpu)
     , fWidth(width)
     , fHeight(height)
@@ -138,13 +139,13 @@
     /**
      * Retrieves the pixel config specified when the texture was created.
      */
-    PixelConfig config() const { return fConfig; }
+    GrPixelConfig config() const { return fConfig; }
 
     /**
      *  Approximate number of bytes used by the texture
      */
     size_t sizeInBytes() const {
-        return fWidth * fHeight * BytesPerPixel(fConfig);
+        return fWidth * fHeight * GrBytesPerPixel(fConfig);
     }
 
     /**
@@ -164,6 +165,21 @@
                                    const void* srcData) = 0;
 
     /**
+     * Reads a rectangle of pixels from the texture.
+     * @param left          left edge of the rectangle to read (inclusive)
+     * @param top           top edge of the rectangle to read (inclusive)
+     * @param width         width of rectangle to read in pixels.
+     * @param height        height of rectangle to read in pixels.
+     * @param config        the pixel config of the destination buffer
+     * @param buffer        memory to read the rectangle into.
+     *
+     * @return true if the read succeeded, false if not. The read can fail
+     *              because of a unsupported pixel config.
+     */
+    bool readPixels(int left, int top, int width, int height,
+                    GrPixelConfig config, void* buffer);
+
+    /**
      * Retrieves the render target underlying this texture that can be passed to
      * GrGpu::setRenderTarget().
      *
@@ -200,7 +216,8 @@
     // for this texture if the texture is power of two sized.
     int      fShiftFixedX;
     int      fShiftFixedY;
-    PixelConfig fConfig;
+
+    GrPixelConfig fConfig;
 
     typedef GrResource INHERITED;
 };
diff --git a/gpu/include/GrTypes.h b/gpu/include/GrTypes.h
index cc78c3e..2d8c116 100644
--- a/gpu/include/GrTypes.h
+++ b/gpu/include/GrTypes.h
@@ -213,6 +213,54 @@
 }
 
 /**
+ * Pixel configurations.
+ */
+enum GrPixelConfig {
+    kUnknown_GrPixelConfig,
+    kAlpha_8_GrPixelConfig,
+    kIndex_8_GrPixelConfig,
+    kRGB_565_GrPixelConfig,
+    kRGBA_4444_GrPixelConfig, //!< premultiplied
+    kRGBA_8888_GrPixelConfig, //!< premultiplied
+    kRGBX_8888_GrPixelConfig, //!< treat the alpha channel as opaque
+};
+
+static inline size_t GrBytesPerPixel(GrPixelConfig config) {
+    switch (config) {
+        case kAlpha_8_GrPixelConfig:
+        case kIndex_8_GrPixelConfig:
+            return 1;
+        case kRGB_565_GrPixelConfig:
+        case kRGBA_4444_GrPixelConfig:
+            return 2;
+        case kRGBA_8888_GrPixelConfig:
+        case kRGBX_8888_GrPixelConfig:
+            return 4;
+        default:
+            return 0;
+    }
+}
+
+static inline bool GrPixelConfigIsOpaque(GrPixelConfig config) {
+    switch (config) {
+        case kRGB_565_GrPixelConfig:
+        case kRGBX_8888_GrPixelConfig:
+            return true;
+        default:
+            return false;
+    }
+}
+
+static inline bool GrPixelConfigIsAlphaOnly(GrPixelConfig config) {
+    switch (config) {
+        case kAlpha_8_GrPixelConfig:
+            return true;
+        default:
+            return false;
+    }
+}
+
+/**
  * Set Operations used to construct clips.
  */
 enum GrSetOp {