bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | #ifndef GrBatchBuffer_DEFINED |
| 9 | #define GrBatchBuffer_DEFINED |
| 10 | |
| 11 | #include "GrBufferAllocPool.h" |
| 12 | #include "batches/GrVertexBatch.h" |
| 13 | |
| 14 | class GrResourceProvider; |
| 15 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 16 | /** Tracks the state across all the GrBatches in a GrDrawTarget flush. */ |
| 17 | class GrBatchFlushState { |
| 18 | public: |
robertphillips | 1f0e350 | 2015-11-10 10:19:50 -0800 | [diff] [blame] | 19 | GrBatchFlushState(GrGpu*, GrResourceProvider*); |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 20 | |
joshualitt | f6d259b | 2015-10-02 11:27:14 -0700 | [diff] [blame] | 21 | ~GrBatchFlushState() { this->reset(); } |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 22 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 23 | /** Inserts an upload to be executed after all batches in the flush prepared their draws |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 24 | but before the draws are executed to the backend 3D API. */ |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 25 | void addASAPUpload(GrDrawBatch::DeferredUploadFn&& upload) { |
| 26 | fAsapUploads.emplace_back(std::move(upload)); |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | const GrCaps& caps() const { return *fGpu->caps(); } |
| 30 | GrResourceProvider* resourceProvider() const { return fResourceProvider; } |
| 31 | |
| 32 | /** Has the token been flushed to the backend 3D API. */ |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 33 | bool hasDrawBeenFlushed(GrBatchDrawToken token) const { |
| 34 | return token.fSequenceNumber <= fLastFlushedToken.fSequenceNumber; |
| 35 | } |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 36 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 37 | /** Issue a token to an operation that is being enqueued. */ |
| 38 | GrBatchDrawToken issueDrawToken() { |
| 39 | return GrBatchDrawToken(++fLastIssuedToken.fSequenceNumber); |
| 40 | } |
| 41 | |
| 42 | /** Call every time a draw that was issued a token is flushed */ |
| 43 | void flushToken() { ++fLastFlushedToken.fSequenceNumber; } |
| 44 | |
| 45 | /** Gets the next draw token that will be issued. */ |
| 46 | GrBatchDrawToken nextDrawToken() const { |
| 47 | return GrBatchDrawToken(fLastIssuedToken.fSequenceNumber + 1); |
| 48 | } |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 49 | |
| 50 | /** The last token flushed to all the way to the backend API. */ |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 51 | GrBatchDrawToken nextTokenToFlush() const { |
| 52 | return GrBatchDrawToken(fLastFlushedToken.fSequenceNumber + 1); |
| 53 | } |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 54 | |
| 55 | void* makeVertexSpace(size_t vertexSize, int vertexCount, |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 56 | const GrBuffer** buffer, int* startVertex); |
| 57 | uint16_t* makeIndexSpace(int indexCount, const GrBuffer** buffer, int* startIndex); |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 58 | |
| 59 | /** This is called after each batch has a chance to prepare its draws and before the draws |
| 60 | are issued. */ |
| 61 | void preIssueDraws() { |
| 62 | fVertexPool.unmap(); |
| 63 | fIndexPool.unmap(); |
| 64 | int uploadCount = fAsapUploads.count(); |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 65 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 66 | for (int i = 0; i < uploadCount; i++) { |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 67 | this->doUpload(fAsapUploads[i]); |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 68 | } |
| 69 | fAsapUploads.reset(); |
| 70 | } |
| 71 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 72 | void doUpload(GrDrawBatch::DeferredUploadFn& upload) { |
| 73 | GrDrawBatch::WritePixelsFn wp = [this] (GrSurface* surface, |
| 74 | int left, int top, int width, int height, |
| 75 | GrPixelConfig config, const void* buffer, |
| 76 | size_t rowBytes) -> bool { |
| 77 | return this->fGpu->writePixels(surface, left, top, width, height, config, buffer, |
| 78 | rowBytes); |
| 79 | }; |
jvanverth | c3d706f | 2016-04-20 10:33:27 -0700 | [diff] [blame^] | 80 | upload(wp); |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 81 | } |
| 82 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 83 | void putBackIndices(size_t indices) { fIndexPool.putBack(indices * sizeof(uint16_t)); } |
| 84 | |
| 85 | void putBackVertexSpace(size_t sizeInBytes) { fVertexPool.putBack(sizeInBytes); } |
| 86 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 87 | GrGpu* gpu() { return fGpu; } |
| 88 | |
joshualitt | f6d259b | 2015-10-02 11:27:14 -0700 | [diff] [blame] | 89 | void reset() { |
| 90 | fVertexPool.reset(); |
| 91 | fIndexPool.reset(); |
| 92 | } |
| 93 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 94 | private: |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 95 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 96 | GrGpu* fGpu; |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 97 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 98 | GrResourceProvider* fResourceProvider; |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 99 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 100 | GrVertexBufferAllocPool fVertexPool; |
| 101 | GrIndexBufferAllocPool fIndexPool; |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 102 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 103 | SkSTArray<4, GrDrawBatch::DeferredUploadFn> fAsapUploads; |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 104 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 105 | GrBatchDrawToken fLastIssuedToken; |
| 106 | |
| 107 | GrBatchDrawToken fLastFlushedToken; |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | /** |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 111 | * A word about uploads and tokens: Batches should usually schedule their uploads to occur at the |
| 112 | * begining of a frame whenever possible. These are called ASAP uploads. Of course, this requires |
| 113 | * that there are no draws that have yet to be flushed that rely on the old texture contents. In |
| 114 | * that case the ASAP upload would happen prior to the previous draw causing the draw to read the |
| 115 | * new (wrong) texture data. In that case they should schedule an inline upload. |
| 116 | * |
| 117 | * Batches, in conjunction with helpers such as GrBatchAtlas, can use the token system to know |
| 118 | * what the most recent draw was that referenced a resource (or portion of a resource). Each draw |
| 119 | * is assigned a token. A resource (or portion) can be tagged with the most recent draw's |
| 120 | * token. The target provides a facility for testing whether the draw corresponding to the token |
| 121 | * has been flushed. If it has not been flushed then the batch must perform an inline upload |
| 122 | * instead. When scheduling an inline upload the batch provides the token of the draw that the |
| 123 | * upload must occur before. The upload will then occur between the draw that requires the new |
| 124 | * data but after the token that requires the old data. |
| 125 | * |
| 126 | * TODO: Currently the token/upload interface is spread over GrDrawBatch, GrVertexBatch, |
| 127 | * GrDrawBatch::Target, and GrVertexBatch::Target. However, the interface at the GrDrawBatch |
| 128 | * level is not complete and isn't useful. We should push it down to GrVertexBatch until it |
| 129 | * is required at the GrDrawBatch level. |
| 130 | */ |
| 131 | |
| 132 | /** |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 133 | * GrDrawBatch instances use this object to allocate space for their geometry and to issue the draws |
| 134 | * that render their batch. |
| 135 | */ |
| 136 | class GrDrawBatch::Target { |
| 137 | public: |
| 138 | Target(GrBatchFlushState* state, GrDrawBatch* batch) : fState(state), fBatch(batch) {} |
| 139 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 140 | /** Returns the token of the draw that this upload will occur before. */ |
| 141 | GrBatchDrawToken addInlineUpload(DeferredUploadFn&& upload) { |
| 142 | fBatch->fInlineUploads.emplace_back(std::move(upload), fState->nextDrawToken()); |
| 143 | return fBatch->fInlineUploads.back().fUploadBeforeToken; |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 144 | } |
| 145 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 146 | /** Returns the token of the draw that this upload will occur before. Since ASAP uploads |
| 147 | are done first during a flush, this will be the first token since the most recent |
| 148 | flush. */ |
| 149 | GrBatchDrawToken addAsapUpload(DeferredUploadFn&& upload) { |
| 150 | fState->addASAPUpload(std::move(upload)); |
| 151 | return fState->nextTokenToFlush(); |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 152 | } |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 153 | |
| 154 | bool hasDrawBeenFlushed(GrBatchDrawToken token) const { |
| 155 | return fState->hasDrawBeenFlushed(token); |
| 156 | } |
| 157 | |
| 158 | /** Gets the next draw token that will be issued by this target. This can be used by a batch |
| 159 | to record that the next draw it issues will use a resource (e.g. texture) while preparing |
| 160 | that draw. */ |
| 161 | GrBatchDrawToken nextDrawToken() const { return fState->nextDrawToken(); } |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 162 | |
| 163 | const GrCaps& caps() const { return fState->caps(); } |
| 164 | |
| 165 | GrResourceProvider* resourceProvider() const { return fState->resourceProvider(); } |
| 166 | |
| 167 | protected: |
| 168 | GrDrawBatch* batch() { return fBatch; } |
| 169 | GrBatchFlushState* state() { return fState; } |
| 170 | |
| 171 | private: |
| 172 | GrBatchFlushState* fState; |
| 173 | GrDrawBatch* fBatch; |
| 174 | }; |
| 175 | |
| 176 | /** Extension of GrDrawBatch::Target for use by GrVertexBatch. Adds the ability to create vertex |
| 177 | draws. */ |
| 178 | class GrVertexBatch::Target : public GrDrawBatch::Target { |
| 179 | public: |
| 180 | Target(GrBatchFlushState* state, GrVertexBatch* batch) : INHERITED(state, batch) {} |
| 181 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 182 | void draw(const GrGeometryProcessor* gp, const GrMesh& mesh); |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 183 | |
| 184 | void* makeVertexSpace(size_t vertexSize, int vertexCount, |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 185 | const GrBuffer** buffer, int* startVertex) { |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 186 | return this->state()->makeVertexSpace(vertexSize, vertexCount, buffer, startVertex); |
| 187 | } |
| 188 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 189 | uint16_t* makeIndexSpace(int indexCount, const GrBuffer** buffer, int* startIndex) { |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 190 | return this->state()->makeIndexSpace(indexCount, buffer, startIndex); |
| 191 | } |
| 192 | |
| 193 | /** Helpers for batches which over-allocate and then return data to the pool. */ |
| 194 | void putBackIndices(int indices) { this->state()->putBackIndices(indices); } |
| 195 | void putBackVertices(int vertices, size_t vertexStride) { |
| 196 | this->state()->putBackVertexSpace(vertices * vertexStride); |
| 197 | } |
| 198 | |
| 199 | private: |
| 200 | GrVertexBatch* vertexBatch() { return static_cast<GrVertexBatch*>(this->batch()); } |
| 201 | typedef GrDrawBatch::Target INHERITED; |
| 202 | }; |
| 203 | |
| 204 | #endif |