drawinfo carries bufferinfo
authorjoshualitt <joshualitt@chromium.org>
Tue, 18 Nov 2014 22:24:27 +0000 (14:24 -0800)
committerCommit bot <commit-bot@chromium.org>
Tue, 18 Nov 2014 22:24:27 +0000 (14:24 -0800)
BUG=skia:

Review URL: https://codereview.chromium.org/737723003

include/gpu/GrGpuResourceRef.h
src/gpu/GrDrawTarget.cpp
src/gpu/GrDrawTarget.h
src/gpu/GrGpu.cpp
src/gpu/GrGpu.h
src/gpu/GrInOrderDrawBuffer.cpp
src/gpu/GrInOrderDrawBuffer.h
src/gpu/gl/GrGpuGL_program.cpp

index 93298f0656f4e5e03529c27e04e59233c0dba1bc..3742e2519519dfaafaf251ca38fe2a553a7e44fc 100644 (file)
@@ -159,25 +159,38 @@ private:
  */
 template <typename T, GrIOType IO_TYPE> class GrPendingIOResource : SkNoncopyable {
 public:
-    GrPendingIOResource(T* resource) : fResource(resource) {
-        if (NULL != fResource) {
+    GrPendingIOResource(T* resource = NULL) : fResource(NULL) {
+        this->reset(resource);
+    }
+
+    void reset(T* resource) {
+        if (resource) {
             switch (IO_TYPE) {
                 case kRead_GrIOType:
-                    fResource->addPendingRead();
+                    resource->addPendingRead();
                     break;
                 case kWrite_GrIOType:
-                    fResource->addPendingWrite();
+                    resource->addPendingWrite();
                     break;
                 case kRW_GrIOType:
-                    fResource->addPendingRead();
-                    fResource->addPendingWrite();
+                    resource->addPendingRead();
+                    resource->addPendingWrite();
                     break;
             }
         }
+        this->release();
+        fResource = resource;
     }
 
     ~GrPendingIOResource() {
-        if (NULL != fResource) {
+        this->release();
+    }
+
+    T* get() const { return fResource; }
+
+private:
+    void release() {
+        if (fResource) {
             switch (IO_TYPE) {
                 case kRead_GrIOType:
                     fResource->completedRead();
@@ -193,9 +206,6 @@ public:
         }
     }
 
-    T* get() const { return fResource; }
-
-private:
     T* fResource;
 };
 #endif
index cd1e0b5bf10cc05bb712c58dbe42f5dda9e60a1f..4043eb8dee544d26789c35e725dc40d8de5b17d9 100644 (file)
@@ -43,6 +43,9 @@ GrDrawTarget::DrawInfo& GrDrawTarget::DrawInfo::operator =(const DrawInfo& di) {
 
     fDstCopy = di.fDstCopy;
 
+    this->setVertexBuffer(di.vertexBuffer());
+    this->setIndexBuffer(di.indexBuffer());
+
     return *this;
 }
 
@@ -469,6 +472,9 @@ void GrDrawTarget::drawIndexed(GrDrawState* ds,
         if (!this->setupDstReadIfNecessary(ds, &info)) {
             return;
         }
+
+        this->setDrawBuffers(&info);
+
         this->onDraw(*ds, info, scissorState);
     }
 }
@@ -508,6 +514,9 @@ void GrDrawTarget::drawNonIndexed(GrDrawState* ds,
         if (!this->setupDstReadIfNecessary(ds, &info)) {
             return;
         }
+
+        this->setDrawBuffers(&info);
+
         this->onDraw(*ds, info, scissorState);
     }
 }
@@ -754,11 +763,14 @@ void GrDrawTarget::drawIndexedInstances(GrDrawState* ds,
     if (!this->setupDstReadIfNecessary(ds, &info)) {
         return;
     }
+
     while (instanceCount) {
         info.fInstanceCount = SkTMin(instanceCount, maxInstancesPerDraw);
         info.fVertexCount = info.fInstanceCount * verticesPerInstance;
         info.fIndexCount = info.fInstanceCount * indicesPerInstance;
 
+        this->setDrawBuffers(&info);
+
         if (this->checkDraw(*ds,
                             type,
                             info.fStartVertex,
index a41fc43ea4675d36f5a8e4b59a710b0cfccd62eb..b13db61cc13f7104277a68e589985aa94dd313c8 100644 (file)
@@ -15,6 +15,7 @@
 #include "GrIndexBuffer.h"
 #include "GrPathRendering.h"
 #include "GrTraceMarker.h"
+#include "GrVertexBuffer.h"
 
 #include "SkClipStack.h"
 #include "SkMatrix.h"
@@ -29,7 +30,6 @@ class GrClipData;
 class GrDrawTargetCaps;
 class GrPath;
 class GrPathRange;
-class GrVertexBuffer;
 
 class GrDrawTarget : public SkRefCnt {
 public:
@@ -559,6 +559,14 @@ public:
             fDevBoundsStorage = bounds;
             fDevBounds = &fDevBoundsStorage;
         }
+        const GrVertexBuffer* vertexBuffer() const { return fVertexBuffer.get(); }
+        const GrIndexBuffer* indexBuffer() const { return fIndexBuffer.get(); }
+        void setVertexBuffer(const GrVertexBuffer* vb) {
+            fVertexBuffer.reset(vb);
+        }
+        void setIndexBuffer(const GrIndexBuffer* ib) {
+            fIndexBuffer.reset(ib);
+        }
         const SkRect* getDevBounds() const { return fDevBounds; }
 
         // NULL if no copy of the dst is needed for the draw.
@@ -589,9 +597,13 @@ public:
         SkRect                  fDevBoundsStorage;
         SkRect*                 fDevBounds;
 
+        GrPendingIOResource<const GrVertexBuffer, kRead_GrIOType> fVertexBuffer;
+        GrPendingIOResource<const GrIndexBuffer, kRead_GrIOType>  fIndexBuffer;
+
         GrDeviceCoordTexture    fDstCopy;
     };
 
+    virtual void setDrawBuffers(DrawInfo*) = 0;;
     bool programUnitTest(int maxStages);
 
 protected:
index d3fcddf432bf5f06c3ca02455ee320bb7085e538..523c6177a9e28d73e8fedeb9a30a7dbd48131610 100644 (file)
@@ -30,8 +30,6 @@ GrGpu::GrGpu(GrContext* context)
 
 GrGpu::~GrGpu() {
     SkSafeSetNull(fQuadIndexBuffer);
-    SkSafeUnref(fGeoSrcState.fVertexBuffer);
-    SkSafeUnref(fGeoSrcState.fIndexBuffer);
 }
 
 void GrGpu::contextAbandoned() {}
@@ -260,19 +258,6 @@ void GrGpu::removeGpuTraceMarker(const GrGpuTraceMarker* marker) {
     }
 }
 
-void GrGpu::setVertexSourceToBuffer(const GrVertexBuffer* buffer, size_t vertexStride) {
-    SkSafeUnref(fGeoSrcState.fVertexBuffer);
-    fGeoSrcState.fVertexBuffer = buffer;
-    buffer->ref();
-    fGeoSrcState.fVertexSize = vertexStride;
-}
-
-void GrGpu::setIndexSourceToBuffer(const GrIndexBuffer* buffer) {
-    SkSafeUnref(fGeoSrcState.fIndexBuffer);
-    fGeoSrcState.fIndexBuffer = buffer;
-    buffer->ref();
-}
-
 ////////////////////////////////////////////////////////////////////////////////
 
 static const int MAX_QUADS = 1 << 12; // max possible: (1 << 14) - 1;
index 98191f5738f298e9f8e4941632a4179867b04b8d..bf259f92ed1b2bd87b4b68925b1742fc4090e40e 100644 (file)
@@ -356,25 +356,6 @@ public:
                              const SkIRect& srcRect,
                              const SkIPoint& dstPoint) = 0;
 
-    /**
-     * Sets source of vertex data for the next draw. Data does not have to be
-     * in the buffer until drawIndexed, drawNonIndexed, or drawIndexedInstances.
-     *
-     * @param buffer        vertex buffer containing vertex data. Must be
-     *                      unlocked before draw call. Vertex size is queried
-     *                      from current GrDrawState.
-     */
-    void setVertexSourceToBuffer(const GrVertexBuffer* buffer, size_t vertexStride);
-
-    /**
-     * Sets source of index data for the next indexed draw. Data does not have
-     * to be in the buffer until drawIndexed.
-     *
-     * @param buffer index buffer containing indices. Must be unlocked
-     *               before indexed draw call.
-     */
-    void setIndexSourceToBuffer(const GrIndexBuffer* buffer);
-
     virtual void draw(const GrOptDrawState&,
                       const GrDrawTarget::DrawInfo&,
                       const GrClipMaskManager::ScissorState&);
@@ -426,23 +407,6 @@ protected:
                                           unsigned int* ref,
                                           unsigned int* mask);
 
-    struct GeometrySrcState {
-        GeometrySrcState() : fVertexBuffer(NULL), fIndexBuffer(NULL), fVertexSize(0) {}
-        const GrVertexBuffer*   fVertexBuffer;
-        const GrIndexBuffer*    fIndexBuffer;
-        size_t                  fVertexSize;
-    };
-
-    // accessors for derived classes
-    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().fVertexBuffer);
-        return this->getGeomSrc().fVertexSize;
-    }
-
     const GrTraceMarkerSet& getActiveTraceMarkers() { return fActiveTraceMarkers; }
 
     GrContext::GPUStats         fGPUStats;
@@ -536,7 +500,6 @@ private:
         }
     }
 
-    GeometrySrcState                                                    fGeoSrcState;
     ResetTimestamp                                                      fResetTimestamp;
     uint32_t                                                            fResetBits;
     // these are mutable so they can be created on-demand
index b0504b75a970e1c19cfbecfc9a32d7129085d488..778b8cf590894d042f1c7b7b99d09bc7ff93c837 100644 (file)
@@ -219,8 +219,8 @@ int GrInOrderDrawBuffer::concatInstancedDraw(const GrDrawState& ds,
     if (!draw->fInfo.isInstanced() ||
         draw->fInfo.verticesPerInstance() != info.verticesPerInstance() ||
         draw->fInfo.indicesPerInstance() != info.indicesPerInstance() ||
-        draw->vertexBuffer() != vertexBuffer ||
-        draw->indexBuffer() != geomSrc.fIndexBuffer ||
+        draw->fInfo.vertexBuffer() != vertexBuffer ||
+        draw->fInfo.indexBuffer() != geomSrc.fIndexBuffer ||
         draw->fScissorState != scissorState) {
         return 0;
     }
@@ -260,39 +260,25 @@ int GrInOrderDrawBuffer::concatInstancedDraw(const GrDrawState& ds,
 void GrInOrderDrawBuffer::onDraw(const GrDrawState& ds,
                                  const DrawInfo& info,
                                  const GrClipMaskManager::ScissorState& scissorState) {
+    SkASSERT(info.vertexBuffer() && (!info.isIndexed() || info.indexBuffer()));
+
     GeometryPoolState& poolState = fGeoPoolStateStack.back();
 
     this->recordStateIfNecessary(ds,
                                  GrGpu::PrimTypeToDrawType(info.primitiveType()),
                                  info.getDstCopy());
 
-    const GrVertexBuffer* vb;
-    if (kBuffer_GeometrySrcType == this->getGeomSrc().fVertexSrc) {
-        vb = this->getGeomSrc().fVertexBuffer;
-    } else {
-        vb = poolState.fPoolVertexBuffer;
-    }
-
-    const GrIndexBuffer* ib = NULL;
-    if (info.isIndexed()) {
-        if (kBuffer_GeometrySrcType == this->getGeomSrc().fIndexSrc) {
-            ib = this->getGeomSrc().fIndexBuffer;
-        } else {
-            ib = poolState.fPoolIndexBuffer;
-        }
-    }
-
     Draw* draw;
     if (info.isInstanced()) {
         int instancesConcated = this->concatInstancedDraw(ds, info, scissorState);
         if (info.instanceCount() > instancesConcated) {
-            draw = GrNEW_APPEND_TO_RECORDER(fCmdBuffer, Draw, (info, scissorState, vb, ib));
+            draw = GrNEW_APPEND_TO_RECORDER(fCmdBuffer, Draw, (info, scissorState));
             draw->fInfo.adjustInstanceCount(-instancesConcated);
         } else {
             return;
         }
     } else {
-        draw = GrNEW_APPEND_TO_RECORDER(fCmdBuffer, Draw, (info, scissorState, vb, ib));
+        draw = GrNEW_APPEND_TO_RECORDER(fCmdBuffer, Draw, (info, scissorState));
     }
     this->recordTraceMarkersIfNecessary();
 
@@ -432,6 +418,23 @@ void GrInOrderDrawBuffer::discard(GrRenderTarget* renderTarget) {
     this->recordTraceMarkersIfNecessary();
 }
 
+void GrInOrderDrawBuffer::setDrawBuffers(DrawInfo* info) {
+    GeometryPoolState& poolState = fGeoPoolStateStack.back();
+    if (kBuffer_GeometrySrcType == this->getGeomSrc().fVertexSrc) {
+        info->setVertexBuffer(this->getGeomSrc().fVertexBuffer);
+    } else {
+        info->setVertexBuffer(poolState.fPoolVertexBuffer);
+    }
+
+    if (info->isIndexed()) {
+        if (kBuffer_GeometrySrcType == this->getGeomSrc().fIndexSrc) {
+            info->setIndexBuffer(this->getGeomSrc().fIndexBuffer);
+        } else {
+            info->setIndexBuffer(poolState.fPoolIndexBuffer);
+        }
+    }
+}
+
 void GrInOrderDrawBuffer::reset() {
     SkASSERT(1 == fGeoPoolStateStack.count());
     this->resetVertexSource();
@@ -511,12 +514,7 @@ void GrInOrderDrawBuffer::Draw::execute(GrInOrderDrawBuffer* buf, const GrOptDra
     if (!optState) {
         return;
     }
-    GrGpu* dstGpu = buf->fDstGpu;
-    dstGpu->setVertexSourceToBuffer(this->vertexBuffer(), optState->getVertexStride());
-    if (fInfo.isIndexed()) {
-        dstGpu->setIndexSourceToBuffer(this->indexBuffer());
-    }
-    dstGpu->draw(*optState, fInfo, fScissorState);
+    buf->fDstGpu->draw(*optState, fInfo, fScissorState);
 }
 
 void GrInOrderDrawBuffer::StencilPath::execute(GrInOrderDrawBuffer* buf,
index c89823636bc093f6610580483954f6ffd31ff86a..1d5480afc61470730b14821b5dcb5d8e69103f08 100644 (file)
@@ -117,27 +117,15 @@ private:
     };
 
     struct Draw : public Cmd {
-        Draw(const DrawInfo& info,
-             const ScissorState& scissorState,
-             const GrVertexBuffer* vb,
-             const GrIndexBuffer* ib)
+        Draw(const DrawInfo& info, const ScissorState& scissorState)
             : Cmd(kDraw_Cmd)
             , fInfo(info)
-            , fScissorState(scissorState)
-            , fVertexBuffer(vb)
-            , fIndexBuffer(ib) {}
-
-        const GrVertexBuffer* vertexBuffer() const { return fVertexBuffer.get(); }
-        const GrIndexBuffer* indexBuffer() const { return fIndexBuffer.get(); }
+            , fScissorState(scissorState){}
 
         virtual void execute(GrInOrderDrawBuffer*, const GrOptDrawState*);
 
         DrawInfo     fInfo;
         ScissorState fScissorState;
-
-    private:
-        GrPendingIOResource<const GrVertexBuffer, kRead_GrIOType>    fVertexBuffer;
-        GrPendingIOResource<const GrIndexBuffer, kRead_GrIOType>     fIndexBuffer;
     };
 
     struct StencilPath : public Cmd {
@@ -279,6 +267,7 @@ private:
                          GrColor color,
                          bool canIgnoreRect,
                          GrRenderTarget* renderTarget) SK_OVERRIDE;
+    virtual void setDrawBuffers(DrawInfo*) SK_OVERRIDE;
 
     virtual bool onReserveVertexSpace(size_t vertexSize,
                                       int vertexCount,
index 68f38df9f0e13a9a05e9fc28f754dce1c864eda6..cfea2c508c74caf29669a1d633cc5d7608f8c83a 100644 (file)
@@ -264,7 +264,7 @@ void GrGpuGL::setupGeometry(const GrOptDrawState& optState,
     size_t vertexOffsetInBytes = stride * info.startVertex();
 
     GrGLVertexBuffer* vbuf;
-    vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
+    vbuf = (GrGLVertexBuffer*) info.vertexBuffer();
 
     SkASSERT(vbuf);
     SkASSERT(!vbuf->isMapped());
@@ -275,7 +275,7 @@ void GrGpuGL::setupGeometry(const GrOptDrawState& optState,
         SkASSERT(indexOffsetInBytes);
 
         *indexOffsetInBytes = 0;
-        ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
+        ibuf = (GrGLIndexBuffer*)info.indexBuffer();
 
         SkASSERT(ibuf);
         SkASSERT(!ibuf->isMapped());