Home | History | Annotate | Download | only in gpu
      1 
      2 /*
      3  * Copyright 2011 Google Inc.
      4  *
      5  * Use of this source code is governed by a BSD-style license that can be
      6  * found in the LICENSE file.
      7  */
      8 
      9 
     10 #ifndef GrGeometryBuffer_DEFINED
     11 #define GrGeometryBuffer_DEFINED
     12 
     13 #include "GrGpuObject.h"
     14 
     15 class GrGpu;
     16 
     17 /**
     18  * Parent class for vertex and index buffers
     19  */
     20 class GrGeometryBuffer : public GrGpuObject {
     21 public:
     22     SK_DECLARE_INST_COUNT(GrGeometryBuffer);
     23 
     24     /**
     25      *Retrieves whether the buffer was created with the dynamic flag
     26      *
     27      * @return true if the buffer was created with the dynamic flag
     28      */
     29     bool dynamic() const { return fDynamic; }
     30 
     31     /**
     32      * Returns true if the buffer is a wrapper around a CPU array. If true it
     33      * indicates that map will always succeed and will be free.
     34      */
     35     bool isCPUBacked() const { return fCPUBacked; }
     36 
     37     /**
     38      * Maps the buffer to be written by the CPU.
     39      *
     40      * The previous content of the buffer is invalidated. It is an error
     41      * to draw from the buffer while it is mapped. It is an error to call map
     42      * on an already mapped buffer. It may fail if the backend doesn't support
     43      * mapping the buffer. If the buffer is CPU backed then it will always
     44      * succeed and is a free operation. Must be matched by an unmap() call.
     45      * Currently only one map at a time is supported (no nesting of
     46      * map/unmap).
     47      *
     48      * Note that buffer mapping does not go through GrContext and therefore is
     49      * not serialized with other operations.
     50      *
     51      * @return a pointer to the data or NULL if the map fails.
     52      */
     53      void* map() { return (fMapPtr = this->onMap()); }
     54 
     55     /**
     56      * Unmaps the buffer.
     57      *
     58      * The pointer returned by the previous map call will no longer be valid.
     59      */
     60      void unmap() {
     61          SkASSERT(NULL != fMapPtr);
     62          this->onUnmap();
     63          fMapPtr = NULL;
     64      }
     65 
     66     /**
     67      * Returns the same ptr that map() returned at time of map or NULL if the
     68      * is not mapped.
     69      *
     70      * @return ptr to mapped buffer data or NULL if buffer is not mapped.
     71      */
     72      void* mapPtr() const { return fMapPtr; }
     73 
     74     /**
     75      Queries whether the buffer has been mapped.
     76 
     77      @return true if the buffer is mapped, false otherwise.
     78      */
     79      bool isMapped() const { return NULL != fMapPtr; }
     80 
     81     /**
     82      * Updates the buffer data.
     83      *
     84      * The size of the buffer will be preserved. The src data will be
     85      * placed at the beginning of the buffer and any remaining contents will
     86      * be undefined. srcSizeInBytes must be <= to the buffer size.
     87      *
     88      * The buffer must not be mapped.
     89      *
     90      * Note that buffer updates do not go through GrContext and therefore are
     91      * not serialized with other operations.
     92      *
     93      * @return returns true if the update succeeds, false otherwise.
     94      */
     95     bool updateData(const void* src, size_t srcSizeInBytes) {
     96         SkASSERT(!this->isMapped());
     97         SkASSERT(srcSizeInBytes <= fGpuMemorySize);
     98         return this->onUpdateData(src, srcSizeInBytes);
     99     }
    100 
    101     // GrGpuObject overrides
    102     virtual size_t gpuMemorySize() const { return fGpuMemorySize; }
    103 
    104 protected:
    105     GrGeometryBuffer(GrGpu* gpu, bool isWrapped, size_t gpuMemorySize, bool dynamic, bool cpuBacked)
    106         : INHERITED(gpu, isWrapped)
    107         , fMapPtr(NULL)
    108         , fGpuMemorySize(gpuMemorySize)
    109         , fDynamic(dynamic)
    110         , fCPUBacked(cpuBacked) {}
    111 
    112 private:
    113     virtual void* onMap() = 0;
    114     virtual void onUnmap() = 0;
    115     virtual bool onUpdateData(const void* src, size_t srcSizeInBytes) = 0;
    116 
    117     void*    fMapPtr;
    118     size_t   fGpuMemorySize;
    119     bool     fDynamic;
    120     bool     fCPUBacked;
    121 
    122     typedef GrGpuObject INHERITED;
    123 };
    124 
    125 #endif
    126