diff options
author | joshualitt <joshualitt@chromium.org> | 2014-11-07 12:58:46 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2014-11-07 12:58:47 -0800 |
commit | f4e5e3394f9fddeb2e9ddd6ebde0688aec4876b0 (patch) | |
tree | 72b9b32ef74689b884807838556554b765ea9ca3 | |
parent | b48af0e9992549e4d5f12100c92ad96eee737b03 (diff) |
cleaning up geometry handling in gpu
BUG=skia:
Review URL: https://codereview.chromium.org/707953004
-rw-r--r-- | src/gpu/GrGpu.cpp | 253 | ||||
-rw-r--r-- | src/gpu/GrGpu.h | 99 | ||||
-rw-r--r-- | src/gpu/GrInOrderDrawBuffer.cpp | 5 | ||||
-rw-r--r-- | src/gpu/gl/GrGpuGL.cpp | 3 | ||||
-rw-r--r-- | src/gpu/gl/GrGpuGL_program.cpp | 32 |
5 files changed, 20 insertions, 372 deletions
diff --git a/src/gpu/GrGpu.cpp b/src/gpu/GrGpu.cpp index 47863bb6fc..e9bf0001aa 100644 --- a/src/gpu/GrGpu.cpp +++ b/src/gpu/GrGpu.cpp @@ -16,12 +16,6 @@ #include "GrStencilBuffer.h" #include "GrVertexBuffer.h" -// probably makes no sense for this to be less than a page -static const size_t VERTEX_POOL_VB_SIZE = 1 << 18; -static const int VERTEX_POOL_VB_COUNT = 4; -static const size_t INDEX_POOL_IB_SIZE = 1 << 16; -static const int INDEX_POOL_IB_COUNT = 4; - //////////////////////////////////////////////////////////////////////////////// #define DEBUG_INVAL_BUFFER 0xdeadcafe @@ -30,46 +24,18 @@ static const int INDEX_POOL_IB_COUNT = 4; GrGpu::GrGpu(GrContext* context) : fResetTimestamp(kExpiredTimestamp+1) , fResetBits(kAll_GrBackendState) - , fVertexPool(NULL) - , fIndexPool(NULL) - , fVertexPoolUseCnt(0) - , fIndexPoolUseCnt(0) , fQuadIndexBuffer(NULL) , fContext(context) { - fGeomPoolStateStack.push_back(); fDrawState = &fDefaultDrawState; // We assume that fDrawState always owns a ref to the object it points at. fDefaultDrawState.ref(); -#ifdef SK_DEBUG - GeometryPoolState& poolState = fGeomPoolStateStack.back(); - poolState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER; - poolState.fPoolStartVertex = DEBUG_INVAL_START_IDX; - poolState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER; - poolState.fPoolStartIndex = DEBUG_INVAL_START_IDX; -#endif - - GrDrawTarget::GeometrySrcState& geoSrc = fGeoSrcStateStack.push_back(); -#ifdef SK_DEBUG - geoSrc.fVertexCount = DEBUG_INVAL_START_IDX; - geoSrc.fVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER; - geoSrc.fIndexCount = DEBUG_INVAL_START_IDX; - geoSrc.fIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER; -#endif - geoSrc.fVertexSrc = GrDrawTarget::kNone_GeometrySrcType; - geoSrc.fIndexSrc = GrDrawTarget::kNone_GeometrySrcType; } GrGpu::~GrGpu() { SkSafeSetNull(fQuadIndexBuffer); - delete fVertexPool; - fVertexPool = NULL; - delete fIndexPool; - fIndexPool = NULL; - SkASSERT(1 == fGeoSrcStateStack.count()); - SkDEBUGCODE(GrDrawTarget::GeometrySrcState& geoSrc = fGeoSrcStateStack.back()); - SkASSERT(GrDrawTarget::kNone_GeometrySrcType == geoSrc.fIndexSrc); - SkASSERT(GrDrawTarget::kNone_GeometrySrcType == geoSrc.fVertexSrc); SkSafeUnref(fDrawState); + SkSafeUnref(fGeoSrcState.fVertexBuffer); + SkSafeUnref(fGeoSrcState.fIndexBuffer); } void GrGpu::contextAbandoned() {} @@ -308,19 +274,15 @@ void GrGpu::removeGpuTraceMarker(const GrGpuTraceMarker* marker) { } void GrGpu::setVertexSourceToBuffer(const GrVertexBuffer* buffer) { - this->releasePreviousVertexSource(); - GrDrawTarget::GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); - geoSrc.fVertexSrc = GrDrawTarget::kBuffer_GeometrySrcType; - geoSrc.fVertexBuffer = buffer; + SkSafeUnref(fGeoSrcState.fVertexBuffer); + fGeoSrcState.fVertexBuffer = buffer; buffer->ref(); - geoSrc.fVertexSize = this->drawState()->getVertexStride(); + fGeoSrcState.fVertexSize = this->drawState()->getVertexStride(); } void GrGpu::setIndexSourceToBuffer(const GrIndexBuffer* buffer) { - this->releasePreviousIndexSource(); - GrDrawTarget::GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); - geoSrc.fIndexSrc = GrDrawTarget::kBuffer_GeometrySrcType; - geoSrc.fIndexBuffer = buffer; + SkSafeUnref(fGeoSrcState.fIndexBuffer); + fGeoSrcState.fIndexBuffer = buffer; buffer->ref(); } @@ -336,41 +298,6 @@ void GrGpu::setDrawState(GrDrawState* drawState) { } } -void GrGpu::resetVertexSource() { - this->releasePreviousVertexSource(); - GrDrawTarget::GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); - geoSrc.fVertexSrc = GrDrawTarget::kNone_GeometrySrcType; -} - -void GrGpu::resetIndexSource() { - this->releasePreviousIndexSource(); - GrDrawTarget::GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); - geoSrc.fIndexSrc = GrDrawTarget::kNone_GeometrySrcType; -} - -void GrGpu::pushGeometrySource() { - this->geometrySourceWillPush(); - GrDrawTarget::GeometrySrcState& newState = fGeoSrcStateStack.push_back(); - newState.fIndexSrc = GrDrawTarget::kNone_GeometrySrcType; - newState.fVertexSrc = GrDrawTarget::kNone_GeometrySrcType; -#ifdef SK_DEBUG - newState.fVertexCount = ~0; - newState.fVertexBuffer = (GrVertexBuffer*)~0; - newState.fIndexCount = ~0; - newState.fIndexBuffer = (GrIndexBuffer*)~0; -#endif -} - -void GrGpu::popGeometrySource() { - // if popping last element then pops are unbalanced with pushes - SkASSERT(fGeoSrcStateStack.count() > 1); - - this->geometrySourceWillPop(fGeoSrcStateStack.fromBack(1)); - this->releasePreviousVertexSource(); - this->releasePreviousIndexSource(); - fGeoSrcStateStack.pop_back(); -} - //////////////////////////////////////////////////////////////////////////////// static const int MAX_QUADS = 1 << 12; // max possible: (1 << 14) - 1; @@ -396,31 +323,6 @@ const GrIndexBuffer* GrGpu::getQuadIndexBuffer() const { //////////////////////////////////////////////////////////////////////////////// -void GrGpu::geometrySourceWillPush() { - const GrDrawTarget::GeometrySrcState& geoSrc = this->getGeomSrc(); - if (GrDrawTarget::kReserved_GeometrySrcType == geoSrc.fVertexSrc) { - this->finalizeReservedVertices(); - } - if (GrDrawTarget::kReserved_GeometrySrcType == geoSrc.fIndexSrc) { - this->finalizeReservedIndices(); - } - GeometryPoolState& newState = fGeomPoolStateStack.push_back(); -#ifdef SK_DEBUG - newState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER; - newState.fPoolStartVertex = DEBUG_INVAL_START_IDX; - newState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER; - newState.fPoolStartIndex = DEBUG_INVAL_START_IDX; -#else - (void) newState; // silence compiler warning -#endif -} - -void GrGpu::geometrySourceWillPop(const GrDrawTarget::GeometrySrcState& restoredState) { - // if popping last entry then pops are unbalanced with pushes - SkASSERT(fGeomPoolStateStack.count() > 1); - fGeomPoolStateStack.pop_back(); -} - void GrGpu::onDraw(const GrDrawTarget::DrawInfo& info, const GrClipMaskManager::ScissorState& scissorState) { this->handleDirtyContext(); @@ -476,144 +378,3 @@ void GrGpu::onDrawPaths(const GrPathRange* pathRange, this->pathRendering()->drawPaths(pathRange, indices, count, transforms, transformsType, stencilSettings); } - -void GrGpu::finalizeReservedVertices() { - SkASSERT(fVertexPool); - fVertexPool->unmap(); -} - -void GrGpu::finalizeReservedIndices() { - SkASSERT(fIndexPool); - fIndexPool->unmap(); -} - -void GrGpu::prepareVertexPool() { - if (NULL == fVertexPool) { - SkASSERT(0 == fVertexPoolUseCnt); - fVertexPool = SkNEW_ARGS(GrVertexBufferAllocPool, (this, true, - VERTEX_POOL_VB_SIZE, - VERTEX_POOL_VB_COUNT)); - fVertexPool->releaseGpuRef(); - } else if (!fVertexPoolUseCnt) { - // the client doesn't have valid data in the pool - fVertexPool->reset(); - } -} - -void GrGpu::prepareIndexPool() { - if (NULL == fIndexPool) { - SkASSERT(0 == fIndexPoolUseCnt); - fIndexPool = SkNEW_ARGS(GrIndexBufferAllocPool, (this, true, - INDEX_POOL_IB_SIZE, - INDEX_POOL_IB_COUNT)); - fIndexPool->releaseGpuRef(); - } else if (!fIndexPoolUseCnt) { - // the client doesn't have valid data in the pool - fIndexPool->reset(); - } -} - -bool GrGpu::onReserveVertexSpace(size_t vertexSize, - int vertexCount, - void** vertices) { - GeometryPoolState& geomPoolState = fGeomPoolStateStack.back(); - - SkASSERT(vertexCount > 0); - SkASSERT(vertices); - - this->prepareVertexPool(); - - *vertices = fVertexPool->makeSpace(vertexSize, - vertexCount, - &geomPoolState.fPoolVertexBuffer, - &geomPoolState.fPoolStartVertex); - if (NULL == *vertices) { - return false; - } - ++fVertexPoolUseCnt; - return true; -} - -bool GrGpu::onReserveIndexSpace(int indexCount, void** indices) { - GeometryPoolState& geomPoolState = fGeomPoolStateStack.back(); - - SkASSERT(indexCount > 0); - SkASSERT(indices); - - this->prepareIndexPool(); - - *indices = fIndexPool->makeSpace(indexCount, - &geomPoolState.fPoolIndexBuffer, - &geomPoolState.fPoolStartIndex); - if (NULL == *indices) { - return false; - } - ++fIndexPoolUseCnt; - return true; -} - -void GrGpu::releaseReservedVertexSpace() { - const GrDrawTarget::GeometrySrcState& geoSrc = this->getGeomSrc(); - SkASSERT(GrDrawTarget::kReserved_GeometrySrcType == geoSrc.fVertexSrc); - size_t bytes = geoSrc.fVertexCount * geoSrc.fVertexSize; - fVertexPool->putBack(bytes); - --fVertexPoolUseCnt; -} - -void GrGpu::releaseReservedIndexSpace() { - const GrDrawTarget::GeometrySrcState& geoSrc = this->getGeomSrc(); - SkASSERT(GrDrawTarget::kReserved_GeometrySrcType == geoSrc.fIndexSrc); - size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t); - fIndexPool->putBack(bytes); - --fIndexPoolUseCnt; -} - -void GrGpu::releasePreviousVertexSource() { - GrDrawTarget::GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); - switch (geoSrc.fVertexSrc) { - case GrDrawTarget::kNone_GeometrySrcType: - break; - case GrDrawTarget::kReserved_GeometrySrcType: - this->releaseReservedVertexSpace(); - break; - case GrDrawTarget::kBuffer_GeometrySrcType: - geoSrc.fVertexBuffer->unref(); -#ifdef SK_DEBUG - geoSrc.fVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER; -#endif - break; - default: - SkFAIL("Unknown Vertex Source Type."); - break; - } -} - -void GrGpu::releasePreviousIndexSource() { - GrDrawTarget::GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); - switch (geoSrc.fIndexSrc) { - case GrDrawTarget::kNone_GeometrySrcType: // these two don't require - break; - case GrDrawTarget::kReserved_GeometrySrcType: - this->releaseReservedIndexSpace(); - break; - case GrDrawTarget::kBuffer_GeometrySrcType: - geoSrc.fIndexBuffer->unref(); -#ifdef SK_DEBUG - geoSrc.fIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER; -#endif - break; - default: - SkFAIL("Unknown Index Source Type."); - break; - } -} - -void GrGpu::releaseGeometry() { - int popCnt = fGeoSrcStateStack.count() - 1; - while (popCnt) { - this->popGeometrySource(); - --popCnt; - } - this->resetVertexSource(); - this->resetIndexSource(); -} diff --git a/src/gpu/GrGpu.h b/src/gpu/GrGpu.h index 02fc75e554..af3502add7 100644 --- a/src/gpu/GrGpu.h +++ b/src/gpu/GrGpu.h @@ -374,14 +374,6 @@ public: void saveActiveTraceMarkers(); void restoreActiveTraceMarkers(); - /** - * Query to find out if the vertex or index source is reserved. - */ - bool hasReservedVerticesOrIndices() const { - return GrDrawTarget::kReserved_GeometrySrcType == this->getGeomSrc().fVertexSrc || - GrDrawTarget::kReserved_GeometrySrcType == this->getGeomSrc().fIndexSrc; - } - // Called to determine whether an onCopySurface call would succeed or not. This is useful for // proxy subclasses to test whether the copy would succeed without executing it yet. Derived // classes must keep this consistent with their implementation of onCopySurface(). The inputs @@ -420,34 +412,6 @@ public: */ void setIndexSourceToBuffer(const GrIndexBuffer* buffer); - /** - * Resets vertex source. Drawing from reset vertices is illegal. Set vertex - * source to reserved, array, or buffer before next draw. May be able to free - * up temporary storage allocated by setVertexSourceToArray or - * reserveVertexSpace. - */ - void resetVertexSource(); - - /** - * Resets index source. Indexed Drawing from reset indices is illegal. Set - * index source to reserved, array, or buffer before next indexed draw. May - * be able to free up temporary storage allocated by setIndexSourceToArray - * or reserveIndexSpace. - */ - void resetIndexSource(); - - /** - * Pushes and resets the vertex/index sources. Any reserved vertex / index - * data is finalized (i.e. cannot be updated after the matching pop but can - * be drawn from). Must be balanced by a pop. - */ - void pushGeometrySource(); - - /** - * Pops the vertex / index sources from the matching push. - */ - void popGeometrySource(); - protected: DrawType PrimTypeToDrawType(GrPrimitiveType type) { switch (type) { @@ -477,18 +441,20 @@ protected: unsigned int* ref, unsigned int* mask); - // subclasses must call this in their destructors to ensure all vertex - // and index sources have been released (including those held by - // pushGeometrySource()) - void releaseGeometry(); + struct GeometrySrcState { + GeometrySrcState() : fVertexBuffer(NULL), fIndexBuffer(NULL), fVertexSize(0) {} + const GrVertexBuffer* fVertexBuffer; + const GrIndexBuffer* fIndexBuffer; + size_t fVertexSize; + }; // accessors for derived classes - const GrDrawTarget::GeometrySrcState& getGeomSrc() const { return fGeoSrcStateStack.back(); } + const GeometrySrcState& getGeomSrc() const { return fGeoSrcState; } // it is preferable to call this rather than getGeomSrc()->fVertexSize because of the assert. size_t getVertexSize() const { // the vertex layout is only valid if a vertex source has been specified. - SkASSERT(this->getGeomSrc().fVertexSrc != GrDrawTarget::kNone_GeometrySrcType); + SkASSERT(this->getGeomSrc().fVertexBuffer); return this->getGeomSrc().fVertexSize; } @@ -496,35 +462,12 @@ protected: GrContext::GPUStats fGPUStats; - struct GeometryPoolState { - const GrVertexBuffer* fPoolVertexBuffer; - int fPoolStartVertex; - - const GrIndexBuffer* fPoolIndexBuffer; - int fPoolStartIndex; - }; - const GeometryPoolState& getGeomPoolState() { - return fGeomPoolStateStack.back(); - } - - // Helpers for setting up geometry state - void finalizeReservedVertices(); - void finalizeReservedIndices(); - SkAutoTDelete<GrPathRendering> fPathRendering; // Subclass must initialize this in its constructor. SkAutoTUnref<const GrDrawTargetCaps> fCaps; private: - // GrDrawTarget overrides - virtual bool onReserveVertexSpace(size_t vertexSize, int vertexCount, void** vertices); - virtual bool onReserveIndexSpace(int indexCount, void** indices); - virtual void releaseReservedVertexSpace(); - virtual void releaseReservedIndexSpace(); - virtual void geometrySourceWillPush(); - virtual void geometrySourceWillPop(const GrDrawTarget::GeometrySrcState& restoredState); - // called when the 3D context state is unknown. Subclass should emit any // assumed 3D context state and dirty any state cache. virtual void onResetContext(uint32_t resetBits) = 0; @@ -614,11 +557,6 @@ private: virtual void didAddGpuTraceMarker() = 0; virtual void didRemoveGpuTraceMarker() = 0; - - // readies the pools to provide vertex/index data. - void prepareVertexPool(); - void prepareIndexPool(); - void resetContext() { this->onResetContext(fResetBits); fResetBits = 0; @@ -631,29 +569,12 @@ private: } } - // called when setting a new vert/idx source to unref prev vb/ib - void releasePreviousVertexSource(); - void releasePreviousIndexSource(); - - enum { - kPreallocGeoSrcStateStackCnt = 4, - }; - SkSTArray<kPreallocGeoSrcStateStackCnt, GrDrawTarget::GeometrySrcState, true> fGeoSrcStateStack; - - enum { - kPreallocGeomPoolStateStackCnt = 4, - }; - SkSTArray<kPreallocGeomPoolStateStackCnt, GeometryPoolState, true> fGeomPoolStateStack; + GeometrySrcState fGeoSrcState; ResetTimestamp fResetTimestamp; uint32_t fResetBits; - GrVertexBufferAllocPool* fVertexPool; - GrIndexBufferAllocPool* fIndexPool; - // counts number of uses of vertex/index pool in the geometry stack - int fVertexPoolUseCnt; - int fIndexPoolUseCnt; // these are mutable so they can be created on-demand mutable GrIndexBuffer* fQuadIndexBuffer; - GrDrawState fDefaultDrawState; + GrDrawState fDefaultDrawState; GrDrawState* fDrawState; // To keep track that we always have at least as many debug marker adds as removes int fGpuTraceMarkerCount; diff --git a/src/gpu/GrInOrderDrawBuffer.cpp b/src/gpu/GrInOrderDrawBuffer.cpp index f1031ce17c..875734d8a5 100644 --- a/src/gpu/GrInOrderDrawBuffer.cpp +++ b/src/gpu/GrInOrderDrawBuffer.cpp @@ -559,17 +559,12 @@ void GrInOrderDrawBuffer::willReserveVertexAndIndexSpace(int vertexCount, !indexCount && kReserved_GeometrySrcType == this->getGeomSrc().fIndexSrc; - // we don't want to finalize any reserved geom on the target since - // we don't know that the client has finished writing to it. - bool targetHasReservedGeom = fDstGpu->hasReservedVerticesOrIndices(); - int vcount = vertexCount; int icount = indexCount; if (!insideGeoPush && !unreleasedVertexSpace && !unreleasedIndexSpace && - !targetHasReservedGeom && this->geometryHints(&vcount, &icount)) { this->flush(); } diff --git a/src/gpu/gl/GrGpuGL.cpp b/src/gpu/gl/GrGpuGL.cpp index 0a90e82661..3fc5543fb4 100644 --- a/src/gpu/gl/GrGpuGL.cpp +++ b/src/gpu/gl/GrGpuGL.cpp @@ -164,9 +164,6 @@ GrGpuGL::~GrGpuGL() { } delete fProgramCache; - - // This must be called by before the GrDrawTarget destructor - this->releaseGeometry(); } void GrGpuGL::contextAbandoned() { diff --git a/src/gpu/gl/GrGpuGL_program.cpp b/src/gpu/gl/GrGpuGL_program.cpp index 60692f8fe3..b197dc9caa 100644 --- a/src/gpu/gl/GrGpuGL_program.cpp +++ b/src/gpu/gl/GrGpuGL_program.cpp @@ -277,22 +277,8 @@ void GrGpuGL::setupGeometry(const GrDrawTarget::DrawInfo& info, size_t* indexOff size_t vertexOffsetInBytes = stride * info.startVertex(); - const GeometryPoolState& geoPoolState = this->getGeomPoolState(); - GrGLVertexBuffer* vbuf; - switch (this->getGeomSrc().fVertexSrc) { - case GrDrawTarget::kBuffer_GeometrySrcType: - vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer; - break; - case GrDrawTarget::kReserved_GeometrySrcType: - this->finalizeReservedVertices(); - vertexOffsetInBytes += geoPoolState.fPoolStartVertex * this->getGeomSrc().fVertexSize; - vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer; - break; - default: - vbuf = NULL; // suppress warning - SkFAIL("Unknown geometry src type!"); - } + vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer; SkASSERT(vbuf); SkASSERT(!vbuf->isMapped()); @@ -302,20 +288,8 @@ void GrGpuGL::setupGeometry(const GrDrawTarget::DrawInfo& info, size_t* indexOff if (info.isIndexed()) { SkASSERT(indexOffsetInBytes); - switch (this->getGeomSrc().fIndexSrc) { - case GrDrawTarget::kBuffer_GeometrySrcType: - *indexOffsetInBytes = 0; - ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer; - break; - case GrDrawTarget::kReserved_GeometrySrcType: - this->finalizeReservedIndices(); - *indexOffsetInBytes = geoPoolState.fPoolStartIndex * sizeof(GrGLushort); - ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer; - break; - default: - ibuf = NULL; // suppress warning - SkFAIL("Unknown geometry src type!"); - } + *indexOffsetInBytes = 0; + ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer; SkASSERT(ibuf); SkASSERT(!ibuf->isMapped()); |