joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [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 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 11 | #include "GrBatchAtlas.h" |
joshualitt | 6065b88 | 2015-02-24 13:20:59 -0800 | [diff] [blame] | 12 | #include "GrBufferAllocPool.h" |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 13 | #include "GrPendingProgramElement.h" |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 14 | #include "GrPipeline.h" |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 15 | #include "GrGpu.h" |
| 16 | #include "GrTRecorder.h" |
| 17 | |
| 18 | /* |
| 19 | * GrBatch instances use this object to allocate space for their geometry and to issue the draws |
| 20 | * that render their batch. |
| 21 | */ |
| 22 | |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 23 | class GrIndexBufferAllocPool; |
| 24 | class GrVertexBufferAllocPool; |
| 25 | |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 26 | class GrBatchTarget : public SkNoncopyable { |
| 27 | public: |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 28 | typedef GrBatchAtlas::BatchToken BatchToken; |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 29 | GrBatchTarget(GrGpu* gpu, |
| 30 | GrVertexBufferAllocPool* vpool, |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 31 | GrIndexBufferAllocPool* ipool); |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 32 | |
| 33 | typedef GrDrawTarget::DrawInfo DrawInfo; |
| 34 | void initDraw(const GrPrimitiveProcessor* primProc, const GrPipeline* pipeline) { |
| 35 | GrNEW_APPEND_TO_RECORDER(fFlushBuffer, BufferedFlush, (primProc, pipeline)); |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 36 | fNumberOfDraws++; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 37 | fCurrentToken++; |
| 38 | } |
| 39 | |
| 40 | class TextureUploader { |
| 41 | public: |
| 42 | TextureUploader(GrGpu* gpu) : fGpu(gpu) { SkASSERT(gpu); } |
| 43 | |
| 44 | /** |
| 45 | * Updates the pixels in a rectangle of a texture. |
| 46 | * |
| 47 | * @param left left edge of the rectangle to write (inclusive) |
| 48 | * @param top top edge of the rectangle to write (inclusive) |
| 49 | * @param width width of rectangle to write in pixels. |
| 50 | * @param height height of rectangle to write in pixels. |
| 51 | * @param config the pixel config of the source buffer |
| 52 | * @param buffer memory to read pixels from |
| 53 | * @param rowBytes number of bytes between consecutive rows. Zero |
| 54 | * means rows are tightly packed. |
| 55 | */ |
| 56 | bool writeTexturePixels(GrTexture* texture, |
| 57 | int left, int top, int width, int height, |
| 58 | GrPixelConfig config, const void* buffer, |
| 59 | size_t rowBytes) { |
| 60 | return fGpu->writeTexturePixels(texture, left, top, width, height, config, buffer, |
| 61 | rowBytes); |
| 62 | } |
| 63 | |
| 64 | private: |
| 65 | GrGpu* fGpu; |
| 66 | }; |
| 67 | |
| 68 | class Uploader : public SkRefCnt { |
| 69 | public: |
| 70 | Uploader(BatchToken lastUploadToken) : fLastUploadToken(lastUploadToken) {} |
| 71 | BatchToken lastUploadToken() const { return fLastUploadToken; } |
| 72 | virtual void upload(TextureUploader)=0; |
| 73 | |
| 74 | private: |
| 75 | BatchToken fLastUploadToken; |
| 76 | }; |
| 77 | |
| 78 | void upload(Uploader* upload) { |
| 79 | if (this->asapToken() == upload->lastUploadToken()) { |
| 80 | fAsapUploads.push_back().reset(SkRef(upload)); |
| 81 | } else { |
| 82 | fInlineUploads.push_back().reset(SkRef(upload)); |
| 83 | } |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void draw(const GrDrawTarget::DrawInfo& draw) { |
| 87 | fFlushBuffer.back().fDraws.push_back(draw); |
| 88 | } |
| 89 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 90 | bool isIssued(BatchToken token) const { return fLastFlushedToken >= token; } |
| 91 | BatchToken currentToken() const { return fCurrentToken; } |
| 92 | BatchToken asapToken() const { return fLastFlushedToken + 1; } |
| 93 | |
| 94 | // TODO much of this complexity goes away when batch is everywhere |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 95 | void resetNumberOfDraws() { fNumberOfDraws = 0; } |
| 96 | int numberOfDraws() const { return fNumberOfDraws; } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 97 | void preFlush() { |
| 98 | int updateCount = fAsapUploads.count(); |
| 99 | for (int i = 0; i < updateCount; i++) { |
| 100 | fAsapUploads[i]->upload(TextureUploader(fGpu)); |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 101 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 102 | fInlineUpdatesIndex = 0; |
| 103 | fIter = FlushBuffer::Iter(fFlushBuffer); |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 104 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 105 | void flushNext(int n); |
| 106 | void postFlush() { |
| 107 | SkASSERT(!fIter.next()); |
| 108 | fFlushBuffer.reset(); |
| 109 | fAsapUploads.reset(); |
| 110 | fInlineUploads.reset(); |
| 111 | } |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 112 | |
| 113 | // TODO This goes away when everything uses batch |
| 114 | GrBatchTracker* currentBatchTracker() { |
| 115 | SkASSERT(!fFlushBuffer.empty()); |
| 116 | return &fFlushBuffer.back().fBatchTracker; |
| 117 | } |
| 118 | |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 119 | const GrDrawTargetCaps& caps() const { return *fGpu->caps(); } |
| 120 | |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 121 | GrVertexBufferAllocPool* vertexPool() { return fVertexPool; } |
| 122 | GrIndexBufferAllocPool* indexPool() { return fIndexPool; } |
| 123 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 124 | const static int kVertsPerRect = 4; |
| 125 | const static int kIndicesPerRect = 6; |
joshualitt | 76e7fb6 | 2015-02-11 08:52:27 -0800 | [diff] [blame] | 126 | const GrIndexBuffer* quadIndexBuffer() const { return fGpu->getQuadIndexBuffer(); } |
| 127 | |
joshualitt | 6065b88 | 2015-02-24 13:20:59 -0800 | [diff] [blame] | 128 | // A helper for draws which overallocate and then return data to the pool |
| 129 | void putBackIndices(size_t indices) { fIndexPool->putBack(indices * sizeof(uint16_t)); } |
| 130 | |
| 131 | void putBackVertices(size_t vertices, size_t vertexStride) { |
| 132 | fVertexPool->putBack(vertices * vertexStride); |
| 133 | } |
| 134 | |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 135 | private: |
| 136 | GrGpu* fGpu; |
| 137 | GrVertexBufferAllocPool* fVertexPool; |
| 138 | GrIndexBufferAllocPool* fIndexPool; |
| 139 | |
| 140 | typedef void* TBufferAlign; // This wouldn't be enough align if a command used long double. |
| 141 | |
| 142 | struct BufferedFlush { |
| 143 | BufferedFlush(const GrPrimitiveProcessor* primProc, const GrPipeline* pipeline) |
| 144 | : fPrimitiveProcessor(primProc) |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 145 | , fPipeline(pipeline) {} |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 146 | typedef GrPendingProgramElement<const GrPrimitiveProcessor> ProgramPrimitiveProcessor; |
| 147 | ProgramPrimitiveProcessor fPrimitiveProcessor; |
| 148 | const GrPipeline* fPipeline; |
| 149 | GrBatchTracker fBatchTracker; |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 150 | SkSTArray<1, DrawInfo, true> fDraws; |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | enum { |
| 154 | kFlushBufferInitialSizeInBytes = 8 * sizeof(BufferedFlush), |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | typedef GrTRecorder<BufferedFlush, TBufferAlign> FlushBuffer; |
| 158 | |
| 159 | FlushBuffer fFlushBuffer; |
| 160 | // TODO this is temporary |
| 161 | FlushBuffer::Iter fIter; |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 162 | int fNumberOfDraws; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 163 | BatchToken fCurrentToken; |
| 164 | BatchToken fLastFlushedToken; // The next token to be flushed |
| 165 | SkTArray<SkAutoTUnref<Uploader>, true> fAsapUploads; |
| 166 | SkTArray<SkAutoTUnref<Uploader>, true> fInlineUploads; |
| 167 | int fInlineUpdatesIndex; |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 168 | }; |
| 169 | |
| 170 | #endif |