joshualitt | 23ac62c | 2015-03-30 09:53:47 -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 | |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 8 | #include "GrBatch.h" |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 9 | #include "GrBatchTarget.h" |
| 10 | #include "GrResourceProvider.h" |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 11 | |
| 12 | #include "GrMemoryPool.h" |
joshualitt | 23ac62c | 2015-03-30 09:53:47 -0700 | [diff] [blame] | 13 | #include "SkSpinlock.h" |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 14 | |
| 15 | // TODO I noticed a small benefit to using a larger exclusive pool for batches. Its very small, |
| 16 | // but seems to be mostly consistent. There is a lot in flux right now, but we should really |
| 17 | // revisit this when batch is everywhere |
| 18 | |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 19 | |
joshualitt | 23ac62c | 2015-03-30 09:53:47 -0700 | [diff] [blame] | 20 | // We use a global pool protected by a mutex(spinlock). Chrome may use the same GrContext on |
| 21 | // different threads. The GrContext is not used concurrently on different threads and there is a |
| 22 | // memory barrier between accesses of a context on different threads. Also, there may be multiple |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 23 | // GrContexts and those contexts may be in use concurrently on different threads. |
| 24 | namespace { |
joshualitt | 23ac62c | 2015-03-30 09:53:47 -0700 | [diff] [blame] | 25 | SK_DECLARE_STATIC_SPINLOCK(gBatchSpinlock); |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 26 | class MemoryPoolAccessor { |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 27 | public: |
joshualitt | 23ac62c | 2015-03-30 09:53:47 -0700 | [diff] [blame] | 28 | MemoryPoolAccessor() { gBatchSpinlock.acquire(); } |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 29 | |
joshualitt | 23ac62c | 2015-03-30 09:53:47 -0700 | [diff] [blame] | 30 | ~MemoryPoolAccessor() { gBatchSpinlock.release(); } |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 31 | |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 32 | GrMemoryPool* pool() const { |
| 33 | static GrMemoryPool gPool(16384, 16384); |
| 34 | return &gPool; |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 35 | } |
| 36 | }; |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 37 | } |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 38 | |
joshualitt | ca1f07e | 2015-08-07 08:11:19 -0700 | [diff] [blame] | 39 | int32_t GrBatch::gCurrBatchClassID = GrBatch::kIllegalBatchID; |
| 40 | |
| 41 | GrBATCH_SPEW(int32_t GrBatch::gCurrBatchUniqueID = GrBatch::kIllegalBatchID;) |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 42 | |
| 43 | void* GrBatch::operator new(size_t size) { |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 44 | return MemoryPoolAccessor().pool()->allocate(size); |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void GrBatch::operator delete(void* target) { |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 48 | return MemoryPoolAccessor().pool()->release(target); |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 49 | } |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 50 | |
bsalomon | a387a11 | 2015-08-11 14:47:42 -0700 | [diff] [blame] | 51 | GrBatch::GrBatch() |
| 52 | : fClassID(kIllegalBatchID) |
bsalomon | a387a11 | 2015-08-11 14:47:42 -0700 | [diff] [blame] | 53 | #if GR_BATCH_SPEW |
| 54 | , fUniqueID(GenID(&gCurrBatchUniqueID)) |
| 55 | #endif |
| 56 | { |
| 57 | SkDEBUGCODE(fUsed = false;) |
| 58 | } |
| 59 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame^] | 60 | GrBatch::~GrBatch() {} |
| 61 | |
| 62 | ////////////////////////////////////////////////////////////////////////////// |
| 63 | |
| 64 | GrDrawBatch::GrDrawBatch() : fPipelineInstalled(false) { } |
| 65 | |
| 66 | GrDrawBatch::~GrDrawBatch() { |
bsalomon | a387a11 | 2015-08-11 14:47:42 -0700 | [diff] [blame] | 67 | if (fPipelineInstalled) { |
| 68 | this->pipeline()->~GrPipeline(); |
| 69 | } |
| 70 | } |
| 71 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame^] | 72 | bool GrDrawBatch::installPipeline(const GrPipeline::CreateArgs& args) { |
| 73 | GrPipelineOptimizations opts; |
| 74 | void* location = fPipelineStorage.get(); |
| 75 | if (!GrPipeline::CreateAt(location, args, &opts)) { |
| 76 | return false; |
| 77 | } |
| 78 | this->initBatchTracker(opts); |
| 79 | fPipelineInstalled = true; |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | ////////////////////////////////////////////////////////////////////////////// |
| 84 | |
| 85 | GrVertexBatch::GrVertexBatch() : fNumberOfDraws(0) {} |
| 86 | |
| 87 | void* GrVertexBatch::InstancedHelper::init(GrBatchTarget* batchTarget, GrPrimitiveType primType, |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 88 | size_t vertexStride, const GrIndexBuffer* indexBuffer, |
| 89 | int verticesPerInstance, int indicesPerInstance, |
| 90 | int instancesToDraw) { |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 91 | SkASSERT(batchTarget); |
| 92 | if (!indexBuffer) { |
| 93 | return NULL; |
| 94 | } |
| 95 | const GrVertexBuffer* vertexBuffer; |
| 96 | int firstVertex; |
| 97 | int vertexCount = verticesPerInstance * instancesToDraw; |
robertphillips | e40d397 | 2015-05-07 09:51:43 -0700 | [diff] [blame] | 98 | void* vertices = batchTarget->makeVertSpace(vertexStride, vertexCount, |
| 99 | &vertexBuffer, &firstVertex); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 100 | if (!vertices) { |
| 101 | SkDebugf("Vertices could not be allocated for instanced rendering."); |
| 102 | return NULL; |
| 103 | } |
| 104 | SkASSERT(vertexBuffer); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 105 | size_t ibSize = indexBuffer->gpuMemorySize(); |
bsalomon | e64eb57 | 2015-05-07 11:35:55 -0700 | [diff] [blame] | 106 | int maxInstancesPerDraw = static_cast<int>(ibSize / (sizeof(uint16_t) * indicesPerInstance)); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 107 | |
bsalomon | cb8979d | 2015-05-05 09:51:38 -0700 | [diff] [blame] | 108 | fVertices.initInstanced(primType, vertexBuffer, indexBuffer, |
bsalomon | e64eb57 | 2015-05-07 11:35:55 -0700 | [diff] [blame] | 109 | firstVertex, verticesPerInstance, indicesPerInstance, instancesToDraw, |
| 110 | maxInstancesPerDraw); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 111 | return vertices; |
| 112 | } |
| 113 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame^] | 114 | void* GrVertexBatch::QuadHelper::init(GrBatchTarget* batchTarget, size_t vertexStride, |
| 115 | int quadsToDraw) { |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 116 | SkAutoTUnref<const GrIndexBuffer> quadIndexBuffer( |
| 117 | batchTarget->resourceProvider()->refQuadIndexBuffer()); |
| 118 | if (!quadIndexBuffer) { |
| 119 | SkDebugf("Could not get quad index buffer."); |
| 120 | return NULL; |
| 121 | } |
| 122 | return this->INHERITED::init(batchTarget, kTriangles_GrPrimitiveType, vertexStride, |
| 123 | quadIndexBuffer, kVerticesPerQuad, kIndicesPerQuad, quadsToDraw); |
| 124 | } |