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) |
| 53 | , fNumberOfDraws(0) |
| 54 | , fPipelineInstalled(false) |
| 55 | #if GR_BATCH_SPEW |
| 56 | , fUniqueID(GenID(&gCurrBatchUniqueID)) |
| 57 | #endif |
| 58 | { |
| 59 | SkDEBUGCODE(fUsed = false;) |
| 60 | } |
| 61 | |
| 62 | GrBatch::~GrBatch() { |
| 63 | if (fPipelineInstalled) { |
| 64 | this->pipeline()->~GrPipeline(); |
| 65 | } |
| 66 | } |
| 67 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 68 | void* GrBatch::InstancedHelper::init(GrBatchTarget* batchTarget, GrPrimitiveType primType, |
| 69 | size_t vertexStride, const GrIndexBuffer* indexBuffer, |
| 70 | int verticesPerInstance, int indicesPerInstance, |
| 71 | int instancesToDraw) { |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 72 | SkASSERT(batchTarget); |
| 73 | if (!indexBuffer) { |
| 74 | return NULL; |
| 75 | } |
| 76 | const GrVertexBuffer* vertexBuffer; |
| 77 | int firstVertex; |
| 78 | int vertexCount = verticesPerInstance * instancesToDraw; |
robertphillips | e40d397 | 2015-05-07 09:51:43 -0700 | [diff] [blame] | 79 | void* vertices = batchTarget->makeVertSpace(vertexStride, vertexCount, |
| 80 | &vertexBuffer, &firstVertex); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 81 | if (!vertices) { |
| 82 | SkDebugf("Vertices could not be allocated for instanced rendering."); |
| 83 | return NULL; |
| 84 | } |
| 85 | SkASSERT(vertexBuffer); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 86 | size_t ibSize = indexBuffer->gpuMemorySize(); |
bsalomon | e64eb57 | 2015-05-07 11:35:55 -0700 | [diff] [blame] | 87 | int maxInstancesPerDraw = static_cast<int>(ibSize / (sizeof(uint16_t) * indicesPerInstance)); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 88 | |
bsalomon | cb8979d | 2015-05-05 09:51:38 -0700 | [diff] [blame] | 89 | fVertices.initInstanced(primType, vertexBuffer, indexBuffer, |
bsalomon | e64eb57 | 2015-05-07 11:35:55 -0700 | [diff] [blame] | 90 | firstVertex, verticesPerInstance, indicesPerInstance, instancesToDraw, |
| 91 | maxInstancesPerDraw); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 92 | return vertices; |
| 93 | } |
| 94 | |
| 95 | void* GrBatch::QuadHelper::init(GrBatchTarget* batchTarget, size_t vertexStride, int quadsToDraw) { |
| 96 | SkAutoTUnref<const GrIndexBuffer> quadIndexBuffer( |
| 97 | batchTarget->resourceProvider()->refQuadIndexBuffer()); |
| 98 | if (!quadIndexBuffer) { |
| 99 | SkDebugf("Could not get quad index buffer."); |
| 100 | return NULL; |
| 101 | } |
| 102 | return this->INHERITED::init(batchTarget, kTriangles_GrPrimitiveType, vertexStride, |
| 103 | quadIndexBuffer, kVerticesPerQuad, kIndicesPerQuad, quadsToDraw); |
| 104 | } |
bsalomon | a387a11 | 2015-08-11 14:47:42 -0700 | [diff] [blame] | 105 | |
| 106 | bool GrBatch::installPipeline(const GrPipeline::CreateArgs& args) { |
| 107 | GrPipelineOptimizations opts; |
| 108 | void* location = fPipelineStorage.get(); |
| 109 | if (!GrPipeline::CreateAt(location, args, &opts)) { |
| 110 | return false; |
| 111 | } |
| 112 | this->initBatchTracker(opts); |
| 113 | fPipelineInstalled = true; |
| 114 | return true; |
| 115 | } |