OLD | NEW |
(Empty) | |
| 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 |
| 16 /** Simple class that performs the upload on behalf of a GrBatchUploader. */ |
| 17 class GrBatchUploader::TextureUploader { |
| 18 public: |
| 19 TextureUploader(GrGpu* gpu) : fGpu(gpu) { SkASSERT(gpu); } |
| 20 |
| 21 /** |
| 22 * Updates the pixels in a rectangle of a texture. |
| 23 * |
| 24 * @param left left edge of the rectangle to write (inclusive) |
| 25 * @param top top edge of the rectangle to write (inclusive) |
| 26 * @param width width of rectangle to write in pixels. |
| 27 * @param height height of rectangle to write in pixels. |
| 28 * @param config the pixel config of the source buffer |
| 29 * @param buffer memory to read pixels from |
| 30 * @param rowBytes number of bytes between consecutive rows. Zero |
| 31 * means rows are tightly packed. |
| 32 */ |
| 33 bool writeTexturePixels(GrTexture* texture, |
| 34 int left, int top, int width, int height, |
| 35 GrPixelConfig config, const void* buffer, |
| 36 size_t rowBytes) { |
| 37 return fGpu->writePixels(texture, left, top, width, height, config, buff
er, rowBytes); |
| 38 } |
| 39 |
| 40 private: |
| 41 GrGpu* fGpu; |
| 42 }; |
| 43 |
| 44 /** Tracks the state across all the GrBatches in a GrDrawTarget flush. */ |
| 45 class GrBatchFlushState { |
| 46 public: |
| 47 GrBatchFlushState(GrGpu*, GrResourceProvider*, GrBatchToken lastFlushedToken
); |
| 48 |
| 49 ~GrBatchFlushState() { SkASSERT(fLastFlushedToken == fCurrentToken); } |
| 50 |
| 51 void advanceToken() { ++fCurrentToken; } |
| 52 |
| 53 void advanceLastFlushedToken() { ++fLastFlushedToken; } |
| 54 |
| 55 /** Inserts an upload to be executred after all batches in the flush prepare
d their draws |
| 56 but before the draws are executed to the backend 3D API. */ |
| 57 void addASAPUpload(GrBatchUploader* upload) { |
| 58 fAsapUploads.push_back().reset(SkRef(upload)); |
| 59 } |
| 60 |
| 61 const GrCaps& caps() const { return *fGpu->caps(); } |
| 62 GrResourceProvider* resourceProvider() const { return fResourceProvider; } |
| 63 |
| 64 /** Has the token been flushed to the backend 3D API. */ |
| 65 bool hasTokenBeenFlushed(GrBatchToken token) const { return fLastFlushedToke
n >= token; } |
| 66 |
| 67 /** The current token advances once for every contiguous set of uninterrupte
d draws prepared |
| 68 by a batch. */ |
| 69 GrBatchToken currentToken() const { return fCurrentToken; } |
| 70 |
| 71 /** The last token flushed to all the way to the backend API. */ |
| 72 GrBatchToken lastFlushedToken() const { return fLastFlushedToken; } |
| 73 |
| 74 /** This is a magic token that can be used to indicate that an upload should
occur before |
| 75 any draws for any batch in the current flush execute. */ |
| 76 GrBatchToken asapToken() const { return fLastFlushedToken + 1; } |
| 77 |
| 78 void* makeVertexSpace(size_t vertexSize, int vertexCount, |
| 79 const GrVertexBuffer** buffer, int* startVertex); |
| 80 uint16_t* makeIndexSpace(int indexCount, const GrIndexBuffer** buffer, int*
startIndex); |
| 81 |
| 82 /** This is called after each batch has a chance to prepare its draws and be
fore the draws |
| 83 are issued. */ |
| 84 void preIssueDraws() { |
| 85 fVertexPool.unmap(); |
| 86 fIndexPool.unmap(); |
| 87 int uploadCount = fAsapUploads.count(); |
| 88 for (int i = 0; i < uploadCount; i++) { |
| 89 fAsapUploads[i]->upload(&fUploader); |
| 90 } |
| 91 fAsapUploads.reset(); |
| 92 } |
| 93 |
| 94 void putBackIndices(size_t indices) { fIndexPool.putBack(indices * sizeof(ui
nt16_t)); } |
| 95 |
| 96 void putBackVertexSpace(size_t sizeInBytes) { fVertexPool.putBack(sizeInByte
s); } |
| 97 |
| 98 GrBatchUploader::TextureUploader* uploader() { return &fUploader; } |
| 99 |
| 100 GrGpu* gpu() { return fGpu; } |
| 101 |
| 102 private: |
| 103 GrGpu* fGpu; |
| 104 GrBatchUploader::TextureUploader fUploader; |
| 105 |
| 106 GrResourceProvider* fResourceProvider; |
| 107 |
| 108 GrVertexBufferAllocPool fVertexPool; |
| 109 GrIndexBufferAllocPool fIndexPool; |
| 110 |
| 111 SkTArray<SkAutoTUnref<GrBatchUploader>, true> fAsapUploads; |
| 112 |
| 113 GrBatchToken fCurrentToken; |
| 114 |
| 115 GrBatchToken fLastFlushedToken; |
| 116 }; |
| 117 |
| 118 /** |
| 119 * GrDrawBatch instances use this object to allocate space for their geometry an
d to issue the draws |
| 120 * that render their batch. |
| 121 */ |
| 122 class GrDrawBatch::Target { |
| 123 public: |
| 124 Target(GrBatchFlushState* state, GrDrawBatch* batch) : fState(state), fBatch
(batch) {} |
| 125 |
| 126 void upload(GrBatchUploader* upload) { |
| 127 if (this->asapToken() == upload->lastUploadToken()) { |
| 128 fState->addASAPUpload(upload); |
| 129 } else { |
| 130 fBatch->fInlineUploads.push_back().reset(SkRef(upload)); |
| 131 } |
| 132 } |
| 133 |
| 134 bool hasTokenBeenFlushed(GrBatchToken token) const { |
| 135 return fState->hasTokenBeenFlushed(token); |
| 136 } |
| 137 GrBatchToken currentToken() const { return fState->currentToken(); } |
| 138 GrBatchToken asapToken() const { return fState->asapToken(); } |
| 139 |
| 140 const GrCaps& caps() const { return fState->caps(); } |
| 141 |
| 142 GrResourceProvider* resourceProvider() const { return fState->resourceProvid
er(); } |
| 143 |
| 144 protected: |
| 145 GrDrawBatch* batch() { return fBatch; } |
| 146 GrBatchFlushState* state() { return fState; } |
| 147 |
| 148 private: |
| 149 GrBatchFlushState* fState; |
| 150 GrDrawBatch* fBatch; |
| 151 }; |
| 152 |
| 153 /** Extension of GrDrawBatch::Target for use by GrVertexBatch. Adds the ability
to create vertex |
| 154 draws. */ |
| 155 class GrVertexBatch::Target : public GrDrawBatch::Target { |
| 156 public: |
| 157 Target(GrBatchFlushState* state, GrVertexBatch* batch) : INHERITED(state, ba
tch) {} |
| 158 |
| 159 void initDraw(const GrPrimitiveProcessor* primProc, const GrPipeline* pipeli
ne) { |
| 160 GrVertexBatch::DrawArray* draws = this->vertexBatch()->fDrawArrays.addTo
Tail(); |
| 161 draws->fPrimitiveProcessor.reset(primProc); |
| 162 this->state()->advanceToken(); |
| 163 } |
| 164 |
| 165 void draw(const GrVertices& vertices) { |
| 166 this->vertexBatch()->fDrawArrays.tail()->fDraws.push_back(vertices); |
| 167 } |
| 168 |
| 169 void* makeVertexSpace(size_t vertexSize, int vertexCount, |
| 170 const GrVertexBuffer** buffer, int* startVertex) { |
| 171 return this->state()->makeVertexSpace(vertexSize, vertexCount, buffer, s
tartVertex); |
| 172 } |
| 173 |
| 174 uint16_t* makeIndexSpace(int indexCount, const GrIndexBuffer** buffer, int*
startIndex) { |
| 175 return this->state()->makeIndexSpace(indexCount, buffer, startIndex); |
| 176 } |
| 177 |
| 178 /** Helpers for batches which over-allocate and then return data to the pool
. */ |
| 179 void putBackIndices(int indices) { this->state()->putBackIndices(indices); } |
| 180 void putBackVertices(int vertices, size_t vertexStride) { |
| 181 this->state()->putBackVertexSpace(vertices * vertexStride); |
| 182 } |
| 183 |
| 184 private: |
| 185 GrVertexBatch* vertexBatch() { return static_cast<GrVertexBatch*>(this->batc
h()); } |
| 186 typedef GrDrawBatch::Target INHERITED; |
| 187 }; |
| 188 |
| 189 #endif |
OLD | NEW |