Add GrSurface parent class above GrTexture & GrRenderTarget

http://codereview.appspot.com/6297092/



git-svn-id: http://skia.googlecode.com/svn/trunk@4292 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/gyp/gpu.gyp b/gyp/gpu.gyp
index 5acf244..b601721 100644
--- a/gyp/gpu.gyp
+++ b/gyp/gpu.gyp
@@ -195,6 +195,7 @@
         '../include/gpu/GrResource.h',
         '../include/gpu/GrSamplerState.h',
         '../include/gpu/GrScalar.h',
+        '../include/gpu/GrSurface.h',
         '../include/gpu/GrTemplates.h',
         '../include/gpu/GrTextContext.h',
         '../include/gpu/GrTexture.h',
@@ -271,6 +272,7 @@
         '../src/gpu/GrTDArray.h',
         '../src/gpu/GrSoftwarePathRenderer.cpp',
         '../src/gpu/GrSoftwarePathRenderer.h',
+        '../src/gpu/GrSurface.cpp',
         '../src/gpu/GrTextStrike.cpp',
         '../src/gpu/GrTextStrike.h',
         '../src/gpu/GrTextStrike_impl.h',
diff --git a/include/gpu/GrRenderTarget.h b/include/gpu/GrRenderTarget.h
index c26ca4b..d69e5ca 100644
--- a/include/gpu/GrRenderTarget.h
+++ b/include/gpu/GrRenderTarget.h
@@ -19,7 +19,7 @@
 #define GrRenderTarget_DEFINED
 
 #include "GrRect.h"
-#include "GrResource.h"
+#include "GrSurface.h"
 
 class GrStencilBuffer;
 class GrTexture;
@@ -31,32 +31,64 @@
  * Additionally, GrContext provides methods for creating GrRenderTargets
  * that wrap externally created render targets.
  */
-class GrRenderTarget : public GrResource {
+class GrRenderTarget : public GrSurface {
 public:
     SK_DECLARE_INST_COUNT(GrRenderTarget)
 
-    /**
-     * @return the width of the rendertarget
-     */
-    int width() const { return fDesc.fWidth; }
-    /**
-     * @return the height of the rendertarget
-     */
-    int height() const { return fDesc.fHeight; }
+    // GrResource overrides
+    virtual size_t sizeInBytes() const SK_OVERRIDE;
 
-    /**
-     * @return the pixel config. Can be kUnknown_GrPixelConfig
-     * if client asked us to render to a target that has a pixel
-     * config that isn't equivalent with one of our configs.
-     */
-    GrPixelConfig config() const { return fDesc.fConfig; }
-
+    // GrSurface overrides
     /**
      * @return the texture associated with the rendertarget, may be NULL.
      */
-    GrTexture* asTexture() {return fTexture;}
+    virtual GrTexture* asTexture() SK_OVERRIDE { return fTexture; }
+    virtual const GrTexture* asTexture() const SK_OVERRIDE { return fTexture; }
 
     /**
+     * @return this render target.
+     */
+    virtual GrRenderTarget* asRenderTarget() SK_OVERRIDE { return this; }
+    virtual const GrRenderTarget* asRenderTarget() const  SK_OVERRIDE { 
+        return this; 
+    }
+
+    /**
+     * 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.
+     * @param rowBytes      number of bytes bewtween consecutive rows. Zero
+     *                      means rows are tightly packed.
+     *
+     * @return true if the read succeeded, false if not. The read can fail
+     *              because of an unsupported pixel config.
+     */
+    virtual bool readPixels(int left, int top, int width, int height,
+                            GrPixelConfig config, void* buffer, 
+                            size_t rowBytes) SK_OVERRIDE;
+
+    /**
+     * Copy the src pixels [buffer, rowbytes, pixelconfig] into the render
+     * target at the specified rectangle.
+     * @param left          left edge of the rectangle to write (inclusive)
+     * @param top           top edge of the rectangle to write (inclusive)
+     * @param width         width of rectangle to write in pixels.
+     * @param height        height of rectangle to write in pixels.
+     * @param config        the pixel config of the source buffer
+     * @param buffer        memory to read the rectangle from.
+     * @param rowBytes      number of bytes bewtween consecutive rows. Zero
+     *                      means rows are tightly packed.
+     */
+    virtual void writePixels(int left, int top, int width, int height,
+                             GrPixelConfig config, const void* buffer, 
+                             size_t rowBytes) SK_OVERRIDE;
+
+    // GrRenderTarget
+    /**
      * If this RT is multisampled, this is the multisample buffer
      * @return the 3D API's handle to this object (e.g. FBO ID in OpenGL)
      */
@@ -71,7 +103,7 @@
     virtual intptr_t getRenderTargetResolvedHandle() const = 0;
 
     /**
-     * @return true if the render target is multisampled, false otherwise
+     * @return true if the surface is multisampled, false otherwise
      */
     bool isMultisampled() const { return 0 != fDesc.fSampleCnt; }
 
@@ -121,41 +153,6 @@
      */
     void resolve();
 
-    // GrResource overrides
-    virtual size_t sizeInBytes() const;
-
-    /**
-     * 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.
-     * @param rowBytes      number of bytes bewtween consecutive rows. Zero
-     *                      means rows are tightly packed.
-     *
-     * @return true if the read succeeded, false if not. The read can fail
-     *              because of an unsupported pixel config.
-     */
-    bool readPixels(int left, int top, int width, int height,
-                    GrPixelConfig config, void* buffer, size_t rowBytes);
-
-    /**
-     * Copy the src pixels [buffer, rowbytes, pixelconfig] into the render
-     * target at the specified rectangle.
-     * @param left          left edge of the rectangle to write (inclusive)
-     * @param top           top edge of the rectangle to write (inclusive)
-     * @param width         width of rectangle to write in pixels.
-     * @param height        height of rectangle to write in pixels.
-     * @param config        the pixel config of the source buffer
-     * @param buffer        memory to read the rectangle from.
-     * @param rowBytes      number of bytes bewtween consecutive rows. Zero
-     *                      means rows are tightly packed.
-     */
-    void writePixels(int left, int top, int width, int height,
-                     GrPixelConfig config, const void* buffer, size_t rowBytes);
-
     // a MSAA RT may require explicit resolving , it may auto-resolve (e.g. FBO
     // 0 in GL), or be unresolvable because the client didn't give us the 
     // resolve destination.
@@ -175,26 +172,18 @@
 protected:
     GrRenderTarget(GrGpu* gpu,
                    GrTexture* texture,
-                   int width,
-                   int height,
-                   GrPixelConfig config,
-                   int sampleCnt)
-        : INHERITED(gpu)
+                   const GrTextureDesc& desc)
+        : INHERITED(gpu, desc)
         , fStencilBuffer(NULL)
         , fTexture(texture) {
         fResolveRect.setLargestInverted();
-
-        fDesc.fWidth = width;
-        fDesc.fHeight = height;
-        fDesc.fConfig = config;
-        fDesc.fSampleCnt = sampleCnt;
     }
 
     friend class GrTexture;
     // When a texture unrefs an owned rendertarget this func
-    // removes the back pointer. This could be done called from 
+    // removes the back pointer. This could be called from 
     // texture's destructor but would have to be done in derived
-    // class. By the time of texture base destructor it has already
+    // classes. By the time of texture base destructor it has already
     // lost its pointer to the rt.
     void onTextureReleaseRenderTarget() {
         GrAssert(NULL != fTexture);
@@ -205,11 +194,9 @@
     GrStencilBuffer*  fStencilBuffer;
     GrTexture*        fTexture; // not ref'ed
 
-    GrTextureDesc     fDesc;
-
     GrIRect           fResolveRect;
 
-    typedef GrResource INHERITED;
+    typedef GrSurface INHERITED;
 };
 
 #endif
diff --git a/include/gpu/GrSurface.h b/include/gpu/GrSurface.h
new file mode 100644
index 0000000..2d39b5e
--- /dev/null
+++ b/include/gpu/GrSurface.h
@@ -0,0 +1,107 @@
+/*
+ * Copyright 2012 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+
+#ifndef GrSurface_DEFINED
+#define GrSurface_DEFINED
+
+#include "GrTypes.h"
+#include "GrResource.h"
+
+class GrTexture;
+class GrRenderTarget;
+
+class GrSurface : public GrResource {
+public:
+    SK_DECLARE_INST_COUNT(GrSurface);
+
+    /**
+     * Retrieves the width of the surface.
+     *
+     * @return the width in texels
+     */
+    int width() const { return fDesc.fWidth; }
+
+    /**
+     * Retrieves the height of the surface.
+     *
+     * @return the height in texels
+     */
+    int height() const { return fDesc.fHeight; }
+
+    /**
+     * Retrieves the pixel config specified when the surface was created.
+     * For render targets this can be kUnknown_GrPixelConfig
+     * if client asked us to render to a target that has a pixel
+     * config that isn't equivalent with one of our configs.
+     */
+    GrPixelConfig config() const { return fDesc.fConfig; }
+
+    /**
+     * Return the descriptor describing the surface
+     */
+    const GrTextureDesc& desc() const { return fDesc; }
+
+    /**
+     * @return the texture associated with the surface, may be NULL.
+     */
+    virtual GrTexture* asTexture() = 0;
+    virtual const GrTexture* asTexture() const = 0;
+
+    /**
+     * @return the render target underlying this surface, may be NULL.
+     */
+    virtual GrRenderTarget* asRenderTarget() = 0;
+    virtual const GrRenderTarget* asRenderTarget() const = 0;
+
+    /**
+     * Reads a rectangle of pixels from the surface.
+     * @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.
+     * @param rowBytes      number of bytes bewtween consecutive rows. Zero
+     *                      means rows are tightly packed.
+     *
+     * @return true if the read succeeded, false if not. The read can fail
+     *              because of an unsupported pixel config.
+     */
+    virtual bool readPixels(int left, int top, int width, int height,
+                            GrPixelConfig config, void* buffer, 
+                            size_t rowBytes) = 0;
+
+    /**
+     * Copy the src pixels [buffer, rowbytes, pixelconfig] into the surface
+     * at the specified rectangle.
+     * @param left          left edge of the rectangle to write (inclusive)
+     * @param top           top edge of the rectangle to write (inclusive)
+     * @param width         width of rectangle to write in pixels.
+     * @param height        height of rectangle to write in pixels.
+     * @param config        the pixel config of the source buffer
+     * @param buffer        memory to read the rectangle from.
+     * @param rowBytes      number of bytes bewtween consecutive rows. Zero
+     *                      means rows are tightly packed.
+     */
+    virtual void writePixels(int left, int top, int width, int height,
+                             GrPixelConfig config, const void* buffer, 
+                             size_t rowBytes) = 0;
+
+protected:
+    GrTextureDesc     fDesc;
+
+    GrSurface(GrGpu* gpu, const GrTextureDesc& desc)
+    : INHERITED(gpu)
+    , fDesc(desc) {
+    }
+
+private:
+    typedef GrResource INHERITED;
+};
+
+#endif // GrSurface_DEFINED
diff --git a/include/gpu/GrTexture.h b/include/gpu/GrTexture.h
index 704bb28..79b39ec 100644
--- a/include/gpu/GrTexture.h
+++ b/include/gpu/GrTexture.h
@@ -6,12 +6,10 @@
  * found in the LICENSE file.
  */
 
-
-
 #ifndef GrTexture_DEFINED
 #define GrTexture_DEFINED
 
-#include "GrResource.h"
+#include "GrSurface.h"
 
 class GrRenderTarget;
 class GrResourceKey;
@@ -28,57 +26,22 @@
 static const uint64_t kScratch_CacheID = 0xBBBBBBBB;
 
 
-class GrTexture : public GrResource {
+class GrTexture : public GrSurface {
 
 public:
     SK_DECLARE_INST_COUNT(GrTexture)
 
-    /**
-     * Retrieves the width of the texture.
-     *
-     * @return the width in texels
-     */
-    int width() const { return fDesc.fWidth; }
-
-    /**
-     * Retrieves the height of the texture.
-     *
-     * @return the height in texels
-     */
-    int height() const { return fDesc.fHeight; }
-
-    /**
-     * Convert from texels to normalized texture coords for POT textures
-     * only.
-     */
-    GrFixed normalizeFixedX(GrFixed x) const { 
-        GrAssert(GrIsPow2(fDesc.fWidth));
-        return x >> fShiftFixedX; 
-    }
-    GrFixed normalizeFixedY(GrFixed y) const { 
-        GrAssert(GrIsPow2(fDesc.fHeight));
-        return y >> fShiftFixedY; 
-    }
-
-    /**
-     * Retrieves the pixel config specified when the texture was created.
-     */
-    GrPixelConfig config() const { return fDesc.fConfig; }
-
-    /**
-     * Return the descriptor describing the texture
-     */
-    const GrTextureDesc& desc() const { return fDesc; }
-
+    // from GrResource
     /**
      *  Approximate number of bytes used by the texture
      */
-    virtual size_t sizeInBytes() const {
+    virtual size_t sizeInBytes() const SK_OVERRIDE {
         return (size_t) fDesc.fWidth * 
                         fDesc.fHeight * 
                         GrBytesPerPixel(fDesc.fConfig);
     }
 
+    // from GrSurface
     /**
      * Read a rectangle of pixels from the texture.
      * @param left          left edge of the rectangle to read (inclusive)
@@ -93,9 +56,9 @@
      * @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,
-                    size_t rowBytes);
+    virtual bool readPixels(int left, int top, int width, int height,
+                            GrPixelConfig config, void* buffer,
+                            size_t rowBytes) SK_OVERRIDE;
 
     /**
      * Writes a rectangle of pixels to the texture.
@@ -108,9 +71,15 @@
      * @param rowBytes      number of bytes between consecutive rows. Zero
      *                      means rows are tightly packed.
      */
-    void writePixels(int left, int top, int width, int height,
-                     GrPixelConfig config, const void* buffer,
-                     size_t rowBytes);
+    virtual void writePixels(int left, int top, int width, int height,
+                             GrPixelConfig config, const void* buffer,
+                             size_t rowBytes) SK_OVERRIDE;
+
+    /**
+     * @return this texture
+     */
+    virtual GrTexture* asTexture() SK_OVERRIDE { return this; }
+    virtual const GrTexture* asTexture() const SK_OVERRIDE { return this; }
 
     /**
      * Retrieves the render target underlying this texture that can be passed to
@@ -119,8 +88,26 @@
      * @return    handle to render target or NULL if the texture is not a
      *            render target
      */
-    GrRenderTarget* asRenderTarget() { return fRenderTarget; }
-    const GrRenderTarget* asRenderTarget() const { return fRenderTarget; }
+    virtual GrRenderTarget* asRenderTarget() SK_OVERRIDE { 
+        return fRenderTarget; 
+    }
+    virtual const GrRenderTarget* asRenderTarget() const SK_OVERRIDE { 
+        return fRenderTarget; 
+    }
+
+    // GrTexture
+    /**
+     * Convert from texels to normalized texture coords for POT textures
+     * only.
+     */
+    GrFixed normalizeFixedX(GrFixed x) const { 
+        GrAssert(GrIsPow2(fDesc.fWidth));
+        return x >> fShiftFixedX; 
+    }
+    GrFixed normalizeFixedY(GrFixed y) const { 
+        GrAssert(GrIsPow2(fDesc.fHeight));
+        return y >> fShiftFixedY; 
+    }
 
     /**
      * Removes the reference on the associated GrRenderTarget held by this
@@ -166,9 +153,8 @@
                                    // subclass cons can create and set
 
     GrTexture(GrGpu* gpu, const GrTextureDesc& desc)
-    : INHERITED(gpu)
-    , fRenderTarget(NULL)
-    , fDesc(desc) {
+    : INHERITED(gpu, desc)
+    , fRenderTarget(NULL) {
 
         // only make sense if alloc size is pow2
         fShiftFixedX = 31 - Gr_clz(fDesc.fWidth);
@@ -185,14 +171,12 @@
     void validateDesc() const;
 
 private:
-    GrTextureDesc       fDesc;
-
     // these two shift a fixed-point value into normalized coordinates
     // for this texture if the texture is power of two sized.
     int                 fShiftFixedX;
     int                 fShiftFixedY;
 
-    typedef GrResource INHERITED;
+    typedef GrSurface INHERITED;
 };
 
 #endif
diff --git a/src/gpu/GrSurface.cpp b/src/gpu/GrSurface.cpp
new file mode 100644
index 0000000..3fd90d9
--- /dev/null
+++ b/src/gpu/GrSurface.cpp
@@ -0,0 +1,11 @@
+/*
+ * Copyright 2012 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "GrSurface.h"
+
+SK_DEFINE_INST_COUNT(GrSurface)
+
diff --git a/src/gpu/gl/GrGLRenderTarget.cpp b/src/gpu/gl/GrGLRenderTarget.cpp
index cce3def..4193519 100644
--- a/src/gpu/gl/GrGLRenderTarget.cpp
+++ b/src/gpu/gl/GrGLRenderTarget.cpp
@@ -25,6 +25,23 @@
     GrSafeRef(fTexIDObj);
 }
 
+namespace {
+GrTextureDesc MakeDesc(GrTextureFlags flags,
+                       int width, int height,
+                       GrPixelConfig config, int sampleCnt,
+                       uint64_t clientCacheID) {
+    GrTextureDesc temp;
+    temp.fFlags = flags;
+    temp.fWidth = width;
+    temp.fHeight = height;
+    temp.fConfig = config;
+    temp.fSampleCnt = sampleCnt;
+    temp.fClientCacheID = clientCacheID;
+    return temp;
+}
+
+};
+
 GrGLRenderTarget::GrGLRenderTarget(GrGpuGL* gpu,
                                    const Desc& desc,
                                    const GrGLIRect& viewport,
@@ -32,10 +49,10 @@
                                    GrGLTexture* texture)
     : INHERITED(gpu,
                 texture,
-                viewport.fWidth,
-                viewport.fHeight,
-                desc.fConfig,
-                desc.fSampleCnt) {
+                MakeDesc(kNone_GrTextureFlags,
+                         viewport.fWidth, viewport.fHeight,
+                         desc.fConfig, desc.fSampleCnt, 
+                         kDefault_CacheID)) {
     GrAssert(NULL != texID);
     GrAssert(NULL != texture);
     // FBO 0 can't also be a texture, right?
@@ -54,10 +71,10 @@
                                    const GrGLIRect& viewport)
     : INHERITED(gpu,
                 NULL,
-                viewport.fWidth,
-                viewport.fHeight,
-                desc.fConfig,
-                desc.fSampleCnt) {
+                MakeDesc(kNone_GrTextureFlags,
+                         viewport.fWidth, viewport.fHeight,
+                         desc.fConfig, desc.fSampleCnt, 
+                         kDefault_CacheID)) {
     this->init(desc, viewport, NULL);
 }