blob: 38ab1429a2f87766c3afe192946f7620c6a5128b [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
bsalomon5baedd62015-03-09 12:15:53 -070039int32_t GrBatch::gCurrBatchClassID = GrBatch::kIllegalBatchClassID;
joshualitt4d8da812015-01-28 12:53:54 -080040
41void* GrBatch::operator new(size_t size) {
bsalomon5baedd62015-03-09 12:15:53 -070042 return MemoryPoolAccessor().pool()->allocate(size);
joshualitt4d8da812015-01-28 12:53:54 -080043}
44
45void GrBatch::operator delete(void* target) {
bsalomon5baedd62015-03-09 12:15:53 -070046 return MemoryPoolAccessor().pool()->release(target);
joshualitt4d8da812015-01-28 12:53:54 -080047}
bsalomonb5238a72015-05-05 07:49:49 -070048
49void* GrBatch::InstancedHelper::init(GrBatchTarget* batchTarget, GrPrimitiveType primType,
50 size_t vertexStride, const GrIndexBuffer* indexBuffer,
51 int verticesPerInstance, int indicesPerInstance,
52 int instancesToDraw) {
53 SkASSERT(!fInstancesRemaining);
54 SkASSERT(batchTarget);
55 if (!indexBuffer) {
56 return NULL;
57 }
58 const GrVertexBuffer* vertexBuffer;
59 int firstVertex;
60 int vertexCount = verticesPerInstance * instancesToDraw;
61 void* vertices = batchTarget->vertexPool()->makeSpace(vertexStride, vertexCount, &vertexBuffer,
62 &firstVertex);
63 if (!vertices) {
64 SkDebugf("Vertices could not be allocated for instanced rendering.");
65 return NULL;
66 }
67 SkASSERT(vertexBuffer);
68 fInstancesRemaining = instancesToDraw;
69 size_t ibSize = indexBuffer->gpuMemorySize();
70 fMaxInstancesPerDraw = static_cast<int>(ibSize / (sizeof(uint16_t) * indicesPerInstance));
71
72 fDrawInfo.initInstanced(primType, vertexBuffer, indexBuffer,
73 firstVertex, verticesPerInstance, indicesPerInstance, &fInstancesRemaining,
74 fMaxInstancesPerDraw);
75 SkASSERT(fMaxInstancesPerDraw > 0);
76 return vertices;
77}
78
79void* GrBatch::QuadHelper::init(GrBatchTarget* batchTarget, size_t vertexStride, int quadsToDraw) {
80 SkAutoTUnref<const GrIndexBuffer> quadIndexBuffer(
81 batchTarget->resourceProvider()->refQuadIndexBuffer());
82 if (!quadIndexBuffer) {
83 SkDebugf("Could not get quad index buffer.");
84 return NULL;
85 }
86 return this->INHERITED::init(batchTarget, kTriangles_GrPrimitiveType, vertexStride,
87 quadIndexBuffer, kVerticesPerQuad, kIndicesPerQuad, quadsToDraw);
88}