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 "GrGpuResource.h"
     14 #include "SkImageInfo.h"
     15 #include "SkRect.h"
     16 
     17 class GrRenderTarget;
     18 class GrSurfacePriv;
     19 class GrTexture;
     20 
     21 class GrSurface : public GrGpuResource {
     22 public:
     23     SK_DECLARE_INST_COUNT(GrSurface);
     24 
     25     /**
     26      * Retrieves the width of the surface.
     27      */
     28     int width() const { return fDesc.fWidth; }
     29 
     30     /**
     31      * Retrieves the height of the surface.
     32      */
     33     int height() const { return fDesc.fHeight; }
     34 
     35     /**
     36      * Helper that gets the width and height of the surface as a bounding rectangle.
     37      */
     38     void getBoundsRect(SkRect* rect) const { rect->setWH(SkIntToScalar(this->width()),
     39                                                          SkIntToScalar(this->height())); }
     40 
     41     GrSurfaceOrigin origin() const {
     42         SkASSERT(kTopLeft_GrSurfaceOrigin == fDesc.fOrigin || kBottomLeft_GrSurfaceOrigin == fDesc.fOrigin);
     43         return fDesc.fOrigin;
     44     }
     45 
     46     /**
     47      * Retrieves the pixel config specified when the surface was created.
     48      * For render targets this can be kUnknown_GrPixelConfig
     49      * if client asked us to render to a target that has a pixel
     50      * config that isn't equivalent with one of our configs.
     51      */
     52     GrPixelConfig config() const { return fDesc.fConfig; }
     53 
     54     /**
     55      * Return the descriptor describing the surface
     56      */
     57     const GrSurfaceDesc& desc() const { return fDesc; }
     58 
     59     /**
     60      * @return the texture associated with the surface, may be NULL.
     61      */
     62     virtual GrTexture* asTexture() { return NULL; }
     63     virtual const GrTexture* asTexture() const { return NULL; }
     64 
     65     /**
     66      * @return the render target underlying this surface, may be NULL.
     67      */
     68     virtual GrRenderTarget* asRenderTarget() { return NULL; }
     69     virtual const GrRenderTarget* asRenderTarget() const { return NULL; }
     70 
     71     /**
     72      * Reads a rectangle of pixels from the surface.
     73      * @param left          left edge of the rectangle to read (inclusive)
     74      * @param top           top edge of the rectangle to read (inclusive)
     75      * @param width         width of rectangle to read in pixels.
     76      * @param height        height of rectangle to read in pixels.
     77      * @param config        the pixel config of the destination buffer
     78      * @param buffer        memory to read the rectangle into.
     79      * @param rowBytes      number of bytes between consecutive rows. Zero means rows are tightly
     80      *                      packed.
     81      * @param pixelOpsFlags See the GrContext::PixelOpsFlags enum.
     82      *
     83      * @return true if the read succeeded, false if not. The read can fail because of an unsupported
     84      *              pixel config.
     85      */
     86     bool readPixels(int left, int top, int width, int height,
     87                     GrPixelConfig config,
     88                     void* buffer,
     89                     size_t rowBytes = 0,
     90                     uint32_t pixelOpsFlags = 0);
     91 
     92     /**
     93      * Copy the src pixels [buffer, rowbytes, pixelconfig] into the surface at the specified
     94      * rectangle.
     95      * @param left          left edge of the rectangle to write (inclusive)
     96      * @param top           top edge of the rectangle to write (inclusive)
     97      * @param width         width of rectangle to write in pixels.
     98      * @param height        height of rectangle to write in pixels.
     99      * @param config        the pixel config of the source buffer
    100      * @param buffer        memory to read the rectangle from.
    101      * @param rowBytes      number of bytes between consecutive rows. Zero means rows are tightly
    102      *                      packed.
    103      * @param pixelOpsFlags See the GrContext::PixelOpsFlags enum.
    104      *
    105      * @return true if the read succeeded, false if not. The read can fail because of an unsupported
    106      *              pixel config.
    107      */
    108     bool writePixels(int left, int top, int width, int height,
    109                      GrPixelConfig config,
    110                      const void* buffer,
    111                      size_t rowBytes = 0,
    112                      uint32_t pixelOpsFlags = 0);
    113 
    114     /**
    115      * After this returns any pending writes to the surface will be issued to the backend 3D API.
    116      */
    117     void flushWrites();
    118 
    119 
    120     /**
    121      * After this returns any pending writes to the surface will be issued to the backend 3D API and
    122      * if the surface has MSAA it will be resolved.
    123      */
    124     void prepareForExternalRead();
    125 
    126     /** Access methods that are only to be used within Skia code. */
    127     inline GrSurfacePriv surfacePriv();
    128     inline const GrSurfacePriv surfacePriv() const;
    129 
    130 protected:
    131     // Methods made available via GrSurfacePriv
    132     SkImageInfo info() const;
    133     bool savePixels(const char* filename);
    134     bool hasPendingRead() const;
    135     bool hasPendingWrite() const;
    136     bool hasPendingIO() const;
    137 
    138     // Provides access to methods that should be public within Skia code.
    139     friend class GrSurfacePriv;
    140 
    141     GrSurface(GrGpu* gpu, LifeCycle lifeCycle, const GrSurfaceDesc& desc)
    142         : INHERITED(gpu, lifeCycle)
    143         , fDesc(desc) {
    144     }
    145 
    146     GrSurfaceDesc fDesc;
    147 
    148 private:
    149     typedef GrGpuResource INHERITED;
    150 };
    151 
    152 #endif
    153