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