Home | History | Annotate | Download | only in gpu
      1 /*
      2  * Copyright 2012 Google Inc.
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  */
      7 
      8 
      9 #ifndef GrSurface_DEFINED
     10 #define GrSurface_DEFINED
     11 
     12 #include "GrTypes.h"
     13 #include "GrResource.h"
     14 
     15 class GrTexture;
     16 class GrRenderTarget;
     17 
     18 class GrSurface : public GrResource {
     19 public:
     20     SK_DECLARE_INST_COUNT(GrSurface);
     21 
     22     /**
     23      * Retrieves the width of the surface.
     24      *
     25      * @return the width in texels
     26      */
     27     int width() const { return fDesc.fWidth; }
     28 
     29     /**
     30      * Retrieves the height of the surface.
     31      *
     32      * @return the height in texels
     33      */
     34     int height() const { return fDesc.fHeight; }
     35 
     36     GrSurfaceOrigin origin() const {
     37         GrAssert(kTopLeft_GrSurfaceOrigin == fDesc.fOrigin || kBottomLeft_GrSurfaceOrigin == fDesc.fOrigin);
     38         return fDesc.fOrigin;
     39     }
     40 
     41     /**
     42      * Retrieves the pixel config specified when the surface was created.
     43      * For render targets this can be kUnknown_GrPixelConfig
     44      * if client asked us to render to a target that has a pixel
     45      * config that isn't equivalent with one of our configs.
     46      */
     47     GrPixelConfig config() const { return fDesc.fConfig; }
     48 
     49     /**
     50      * Return the descriptor describing the surface
     51      */
     52     const GrTextureDesc& desc() const { return fDesc; }
     53 
     54     /**
     55      * @return the texture associated with the surface, may be NULL.
     56      */
     57     virtual GrTexture* asTexture() = 0;
     58     virtual const GrTexture* asTexture() const = 0;
     59 
     60     /**
     61      * @return the render target underlying this surface, may be NULL.
     62      */
     63     virtual GrRenderTarget* asRenderTarget() = 0;
     64     virtual const GrRenderTarget* asRenderTarget() const = 0;
     65 
     66     /**
     67      * Checks whether this GrSurface refers to the same GPU object as other. This
     68      * catches the case where a GrTexture and GrRenderTarget refer to the same
     69      * GPU memory.
     70      */
     71     bool isSameAs(const GrSurface* other) const {
     72         const GrRenderTarget* thisRT = this->asRenderTarget();
     73         if (NULL != thisRT) {
     74             return thisRT == other->asRenderTarget();
     75         } else {
     76             const GrTexture* thisTex = this->asTexture();
     77             GrAssert(NULL != thisTex); // We must be one or the other
     78             return thisTex == other->asTexture();
     79         }
     80     }
     81 
     82     /**
     83      * Reads a rectangle of pixels from the surface.
     84      * @param left          left edge of the rectangle to read (inclusive)
     85      * @param top           top edge of the rectangle to read (inclusive)
     86      * @param width         width of rectangle to read in pixels.
     87      * @param height        height of rectangle to read in pixels.
     88      * @param config        the pixel config of the destination buffer
     89      * @param buffer        memory to read the rectangle into.
     90      * @param rowBytes      number of bytes between consecutive rows. Zero means rows are tightly
     91      *                      packed.
     92      * @param pixelOpsFlags See the GrContext::PixelOpsFlags enum.
     93      *
     94      * @return true if the read succeeded, false if not. The read can fail because of an unsupported
     95      *              pixel config.
     96      */
     97     virtual bool readPixels(int left, int top, int width, int height,
     98                             GrPixelConfig config,
     99                             void* buffer,
    100                             size_t rowBytes = 0,
    101                             uint32_t pixelOpsFlags = 0) = 0;
    102 
    103     /**
    104      * Copy the src pixels [buffer, rowbytes, pixelconfig] into the surface at the specified
    105      * rectangle.
    106      * @param left          left edge of the rectangle to write (inclusive)
    107      * @param top           top edge of the rectangle to write (inclusive)
    108      * @param width         width of rectangle to write in pixels.
    109      * @param height        height of rectangle to write in pixels.
    110      * @param config        the pixel config of the source buffer
    111      * @param buffer        memory to read the rectangle from.
    112      * @param rowBytes      number of bytes between consecutive rows. Zero means rows are tightly
    113      *                      packed.
    114      * @param pixelOpsFlags See the GrContext::PixelOpsFlags enum.
    115      */
    116     virtual void writePixels(int left, int top, int width, int height,
    117                              GrPixelConfig config,
    118                              const void* buffer,
    119                              size_t rowBytes = 0,
    120                              uint32_t pixelOpsFlags = 0) = 0;
    121 
    122 protected:
    123     GrSurface(GrGpu* gpu, bool isWrapped, const GrTextureDesc& desc)
    124     : INHERITED(gpu, isWrapped)
    125     , fDesc(desc) {
    126     }
    127 
    128     GrTextureDesc fDesc;
    129 
    130 private:
    131     typedef GrResource INHERITED;
    132 };
    133 
    134 #endif // GrSurface_DEFINED
    135