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