blob: e69819e2d3911c562f7d0e3589d497ddf2445213 [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) {
bsalomonb5238a72015-05-05 07:49:49 -070053 SkASSERT(batchTarget);
54 if (!indexBuffer) {
55 return NULL;
56 }
57 const GrVertexBuffer* vertexBuffer;
58 int firstVertex;
59 int vertexCount = verticesPerInstance * instancesToDraw;
robertphillipse40d3972015-05-07 09:51:43 -070060 void* vertices = batchTarget->makeVertSpace(vertexStride, vertexCount,
61 &vertexBuffer, &firstVertex);
bsalomonb5238a72015-05-05 07:49:49 -070062 if (!vertices) {
63 SkDebugf("Vertices could not be allocated for instanced rendering.");
64 return NULL;
65 }
66 SkASSERT(vertexBuffer);
bsalomonb5238a72015-05-05 07:49:49 -070067 size_t ibSize = indexBuffer->gpuMemorySize();
bsalomone64eb572015-05-07 11:35:55 -070068 int maxInstancesPerDraw = static_cast<int>(ibSize / (sizeof(uint16_t) * indicesPerInstance));
bsalomonb5238a72015-05-05 07:49:49 -070069
bsalomoncb8979d2015-05-05 09:51:38 -070070 fVertices.initInstanced(primType, vertexBuffer, indexBuffer,
bsalomone64eb572015-05-07 11:35:55 -070071 firstVertex, verticesPerInstance, indicesPerInstance, instancesToDraw,
72 maxInstancesPerDraw);
bsalomonb5238a72015-05-05 07:49:49 -070073 return vertices;
74}
75
76void* GrBatch::QuadHelper::init(GrBatchTarget* batchTarget, size_t vertexStride, int quadsToDraw) {
77 SkAutoTUnref<const GrIndexBuffer> quadIndexBuffer(
78 batchTarget->resourceProvider()->refQuadIndexBuffer());
79 if (!quadIndexBuffer) {
80 SkDebugf("Could not get quad index buffer.");
81 return NULL;
82 }
83 return this->INHERITED::init(batchTarget, kTriangles_GrPrimitiveType, vertexStride,
84 quadIndexBuffer, kVerticesPerQuad, kIndicesPerQuad, quadsToDraw);
85}