diff --git a/src/gpu/GrGeometryBuffer.h b/src/gpu/GrGeometryBuffer.h index 1e1a367260..67e91cc4d9 100644 --- a/src/gpu/GrGeometryBuffer.h +++ b/src/gpu/GrGeometryBuffer.h @@ -45,42 +45,58 @@ public: * Currently only one map at a time is supported (no nesting of * map/unmap). * + * Note that buffer mapping does not go through GrContext and therefore is + * not serialized with other operations. + * * @return a pointer to the data or NULL if the map fails. */ - virtual void* map() = 0; - - /** - * Returns the same ptr that map() returned at time of map or NULL if the - * is not mapped. - * - * @return ptr to mapped buffer data or undefined if buffer is not mapped. - */ - virtual void* mapPtr() const = 0; + void* map() { return (fMapPtr = this->onMap()); } /** * Unmaps the buffer. * * The pointer returned by the previous map call will no longer be valid. */ - virtual void unmap() = 0; + void unmap() { + SkASSERT(NULL != fMapPtr); + this->onUnmap(); + fMapPtr = NULL; + } + + /** + * Returns the same ptr that map() returned at time of map or NULL if the + * is not mapped. + * + * @return ptr to mapped buffer data or NULL if buffer is not mapped. + */ + void* mapPtr() const { return fMapPtr; } /** Queries whether the buffer has been mapped. @return true if the buffer is mapped, false otherwise. */ - virtual bool isMapped() const = 0; + bool isMapped() const { return NULL != fMapPtr; } /** * Updates the buffer data. * * The size of the buffer will be preserved. The src data will be * placed at the beginning of the buffer and any remaining contents will - * be undefined. + * be undefined. srcSizeInBytes must be <= to the buffer size. + * + * The buffer must not be mapped. + * + * Note that buffer updates do not go through GrContext and therefore are + * not serialized with other operations. * * @return returns true if the update succeeds, false otherwise. */ - virtual bool updateData(const void* src, size_t srcSizeInBytes) = 0; + bool updateData(const void* src, size_t srcSizeInBytes) { + SkASSERT(!this->isMapped()); + SkASSERT(srcSizeInBytes <= fGpuMemorySize); + return this->onUpdateData(src, srcSizeInBytes); + } // GrGpuObject overrides virtual size_t gpuMemorySize() const { return fGpuMemorySize; } @@ -88,11 +104,17 @@ public: protected: GrGeometryBuffer(GrGpu* gpu, bool isWrapped, size_t gpuMemorySize, bool dynamic, bool cpuBacked) : INHERITED(gpu, isWrapped) + , fMapPtr(NULL) , fGpuMemorySize(gpuMemorySize) , fDynamic(dynamic) , fCPUBacked(cpuBacked) {} private: + virtual void* onMap() = 0; + virtual void onUnmap() = 0; + virtual bool onUpdateData(const void* src, size_t srcSizeInBytes) = 0; + + void* fMapPtr; size_t fGpuMemorySize; bool fDynamic; bool fCPUBacked; diff --git a/src/gpu/gl/GrGLBufferImpl.h b/src/gpu/gl/GrGLBufferImpl.h index 3ab84f67e2..279075f4c9 100644 --- a/src/gpu/gl/GrGLBufferImpl.h +++ b/src/gpu/gl/GrGLBufferImpl.h @@ -41,7 +41,6 @@ public: void bind(GrGpuGL* gpu) const; void* map(GrGpuGL* gpu); - void* mapPtr() const { return fMapPtr; } void unmap(GrGpuGL* gpu); bool isMapped() const; bool updateData(GrGpuGL* gpu, const void* src, size_t srcSizeInBytes); diff --git a/src/gpu/gl/GrGLIndexBuffer.cpp b/src/gpu/gl/GrGLIndexBuffer.cpp index 38dd15d5f6..a137348ef7 100644 --- a/src/gpu/gl/GrGLIndexBuffer.cpp +++ b/src/gpu/gl/GrGLIndexBuffer.cpp @@ -26,7 +26,7 @@ void GrGLIndexBuffer::onAbandon() { INHERITED::onAbandon(); } -void* GrGLIndexBuffer::map() { +void* GrGLIndexBuffer::onMap() { if (!this->wasDestroyed()) { return fImpl.map(this->getGpuGL()); } else { @@ -34,21 +34,13 @@ void* GrGLIndexBuffer::map() { } } -void* GrGLIndexBuffer::mapPtr() const { - return fImpl.mapPtr(); -} - -void GrGLIndexBuffer::unmap() { +void GrGLIndexBuffer::onUnmap() { if (!this->wasDestroyed()) { fImpl.unmap(this->getGpuGL()); } } -bool GrGLIndexBuffer::isMapped() const { - return fImpl.isMapped(); -} - -bool GrGLIndexBuffer::updateData(const void* src, size_t srcSizeInBytes) { +bool GrGLIndexBuffer::onUpdateData(const void* src, size_t srcSizeInBytes) { if (!this->wasDestroyed()) { return fImpl.updateData(this->getGpuGL(), src, srcSizeInBytes); } else { diff --git a/src/gpu/gl/GrGLIndexBuffer.h b/src/gpu/gl/GrGLIndexBuffer.h index 3960f58437..5d5de43eee 100644 --- a/src/gpu/gl/GrGLIndexBuffer.h +++ b/src/gpu/gl/GrGLIndexBuffer.h @@ -31,19 +31,15 @@ public: } } - // overrides of GrIndexBuffer - virtual void* map() SK_OVERRIDE; - virtual void* mapPtr() const SK_OVERRIDE; - virtual void unmap() SK_OVERRIDE; - virtual bool isMapped() const SK_OVERRIDE; - virtual bool updateData(const void* src, size_t srcSizeInBytes); - protected: - // overrides of GrResource virtual void onAbandon() SK_OVERRIDE; virtual void onRelease() SK_OVERRIDE; private: + virtual void* onMap() SK_OVERRIDE; + virtual void onUnmap() SK_OVERRIDE; + virtual bool onUpdateData(const void* src, size_t srcSizeInBytes) SK_OVERRIDE; + GrGpuGL* getGpuGL() const { SkASSERT(!this->wasDestroyed()); return (GrGpuGL*)(this->getGpu()); diff --git a/src/gpu/gl/GrGLVertexBuffer.cpp b/src/gpu/gl/GrGLVertexBuffer.cpp index af6099353a..a13ad126ec 100644 --- a/src/gpu/gl/GrGLVertexBuffer.cpp +++ b/src/gpu/gl/GrGLVertexBuffer.cpp @@ -21,13 +21,12 @@ void GrGLVertexBuffer::onRelease() { INHERITED::onRelease(); } - void GrGLVertexBuffer::onAbandon() { fImpl.abandon(); INHERITED::onAbandon(); } -void* GrGLVertexBuffer::map() { +void* GrGLVertexBuffer::onMap() { if (!this->wasDestroyed()) { return fImpl.map(this->getGpuGL()); } else { @@ -35,21 +34,13 @@ void* GrGLVertexBuffer::map() { } } -void* GrGLVertexBuffer::mapPtr() const { - return fImpl.mapPtr(); -} - -void GrGLVertexBuffer::unmap() { +void GrGLVertexBuffer::onUnmap() { if (!this->wasDestroyed()) { fImpl.unmap(this->getGpuGL()); } } -bool GrGLVertexBuffer::isMapped() const { - return fImpl.isMapped(); -} - -bool GrGLVertexBuffer::updateData(const void* src, size_t srcSizeInBytes) { +bool GrGLVertexBuffer::onUpdateData(const void* src, size_t srcSizeInBytes) { if (!this->wasDestroyed()) { return fImpl.updateData(this->getGpuGL(), src, srcSizeInBytes); } else { diff --git a/src/gpu/gl/GrGLVertexBuffer.h b/src/gpu/gl/GrGLVertexBuffer.h index ddab8299a7..db413ac990 100644 --- a/src/gpu/gl/GrGLVertexBuffer.h +++ b/src/gpu/gl/GrGLVertexBuffer.h @@ -31,19 +31,15 @@ public: } } - // overrides of GrVertexBuffer - virtual void* map() SK_OVERRIDE; - virtual void* mapPtr() const SK_OVERRIDE; - virtual void unmap() SK_OVERRIDE; - virtual bool isMapped() const SK_OVERRIDE; - virtual bool updateData(const void* src, size_t srcSizeInBytes); - protected: - // overrides of GrResource virtual void onAbandon() SK_OVERRIDE; virtual void onRelease() SK_OVERRIDE; private: + virtual void* onMap() SK_OVERRIDE; + virtual void onUnmap() SK_OVERRIDE; + virtual bool onUpdateData(const void* src, size_t srcSizeInBytes) SK_OVERRIDE; + GrGpuGL* getGpuGL() const { SkASSERT(!this->wasDestroyed()); return (GrGpuGL*)(this->getGpu());