blob: 3840b4c83bb90e51f0cac66e32538c599ef2ebd5 [file] [log] [blame]
joshualitt23ac62c2015-03-30 09:53:47 -07001/*
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
joshualitt4d8da812015-01-28 12:53:54 -08008#include "GrBatch.h"
bsalomonb5238a72015-05-05 07:49:49 -07009#include "GrBatchTarget.h"
10#include "GrResourceProvider.h"
joshualitt4d8da812015-01-28 12:53:54 -080011
12#include "GrMemoryPool.h"
joshualitt23ac62c2015-03-30 09:53:47 -070013#include "SkSpinlock.h"
joshualitt4d8da812015-01-28 12:53:54 -080014
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
bsalomon5baedd62015-03-09 12:15:53 -070019
joshualitt23ac62c2015-03-30 09:53:47 -070020// 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
bsalomon5baedd62015-03-09 12:15:53 -070023// GrContexts and those contexts may be in use concurrently on different threads.
24namespace {
joshualitt23ac62c2015-03-30 09:53:47 -070025SK_DECLARE_STATIC_SPINLOCK(gBatchSpinlock);
bsalomon5baedd62015-03-09 12:15:53 -070026class MemoryPoolAccessor {
joshualitt4d8da812015-01-28 12:53:54 -080027public:
joshualitt23ac62c2015-03-30 09:53:47 -070028 MemoryPoolAccessor() { gBatchSpinlock.acquire(); }
joshualitt4d8da812015-01-28 12:53:54 -080029
joshualitt23ac62c2015-03-30 09:53:47 -070030 ~MemoryPoolAccessor() { gBatchSpinlock.release(); }
joshualitt4d8da812015-01-28 12:53:54 -080031
bsalomon5baedd62015-03-09 12:15:53 -070032 GrMemoryPool* pool() const {
33 static GrMemoryPool gPool(16384, 16384);
34 return &gPool;
joshualitt4d8da812015-01-28 12:53:54 -080035 }
36};
bsalomon5baedd62015-03-09 12:15:53 -070037}
joshualitt4d8da812015-01-28 12:53:54 -080038
joshualittca1f07e2015-08-07 08:11:19 -070039int32_t GrBatch::gCurrBatchClassID = GrBatch::kIllegalBatchID;
40
41GrBATCH_SPEW(int32_t GrBatch::gCurrBatchUniqueID = GrBatch::kIllegalBatchID;)
joshualitt4d8da812015-01-28 12:53:54 -080042
43void* GrBatch::operator new(size_t size) {
bsalomon5baedd62015-03-09 12:15:53 -070044 return MemoryPoolAccessor().pool()->allocate(size);
joshualitt4d8da812015-01-28 12:53:54 -080045}
46
47void GrBatch::operator delete(void* target) {
bsalomon5baedd62015-03-09 12:15:53 -070048 return MemoryPoolAccessor().pool()->release(target);
joshualitt4d8da812015-01-28 12:53:54 -080049}
bsalomonb5238a72015-05-05 07:49:49 -070050
bsalomona387a112015-08-11 14:47:42 -070051GrBatch::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
62GrBatch::~GrBatch() {
63 if (fPipelineInstalled) {
64 this->pipeline()->~GrPipeline();
65 }
66}
67
bsalomonb5238a72015-05-05 07:49:49 -070068void* GrBatch::InstancedHelper::init(GrBatchTarget* batchTarget, GrPrimitiveType primType,
69 size_t vertexStride, const GrIndexBuffer* indexBuffer,
70 int verticesPerInstance, int indicesPerInstance,
71 int instancesToDraw) {
bsalomonb5238a72015-05-05 07:49:49 -070072 SkASSERT(batchTarget);
73 if (!indexBuffer) {
74 return NULL;
75 }
76 const GrVertexBuffer* vertexBuffer;
77 int firstVertex;
78 int vertexCount = verticesPerInstance * instancesToDraw;
robertphillipse40d3972015-05-07 09:51:43 -070079 void* vertices = batchTarget->makeVertSpace(vertexStride, vertexCount,
80 &vertexBuffer, &firstVertex);
bsalomonb5238a72015-05-05 07:49:49 -070081 if (!vertices) {
82 SkDebugf("Vertices could not be allocated for instanced rendering.");
83 return NULL;
84 }
85 SkASSERT(vertexBuffer);
bsalomonb5238a72015-05-05 07:49:49 -070086 size_t ibSize = indexBuffer->gpuMemorySize();
bsalomone64eb572015-05-07 11:35:55 -070087 int maxInstancesPerDraw = static_cast<int>(ibSize / (sizeof(uint16_t) * indicesPerInstance));
bsalomonb5238a72015-05-05 07:49:49 -070088
bsalomoncb8979d2015-05-05 09:51:38 -070089 fVertices.initInstanced(primType, vertexBuffer, indexBuffer,
bsalomone64eb572015-05-07 11:35:55 -070090 firstVertex, verticesPerInstance, indicesPerInstance, instancesToDraw,
91 maxInstancesPerDraw);
bsalomonb5238a72015-05-05 07:49:49 -070092 return vertices;
93}
94
95void* 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}
bsalomona387a112015-08-11 14:47:42 -0700105
106bool 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}