1 /*
2 * Copyright 2021 Google LLC
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
8 #include "src/gpu/GrEagerVertexAllocator.h"
9
10 #include "src/gpu/GrMeshDrawTarget.h"
11
12 //-------------------------------------------------------------------------------------------------
lock(size_t stride,int eagerCount)13 void* GrEagerDynamicVertexAllocator::lock(size_t stride, int eagerCount) {
14 SkASSERT(!fLockCount);
15 SkASSERT(eagerCount);
16 if (void* data = fTarget->makeVertexSpace(stride, eagerCount, fVertexBuffer, fBaseVertex)) {
17 fLockStride = stride;
18 fLockCount = eagerCount;
19 return data;
20 }
21 fVertexBuffer->reset();
22 *fBaseVertex = 0;
23 return nullptr;
24 }
25
unlock(int actualCount)26 void GrEagerDynamicVertexAllocator::unlock(int actualCount) {
27 SkASSERT(fLockCount);
28 SkASSERT(actualCount <= fLockCount);
29 fTarget->putBackVertices(fLockCount - actualCount, fLockStride);
30 if (!actualCount) {
31 fVertexBuffer->reset();
32 *fBaseVertex = 0;
33 }
34 fLockCount = 0;
35 }
36
37 //-------------------------------------------------------------------------------------------------
lock(size_t stride,int eagerCount)38 void* GrCpuVertexAllocator::lock(size_t stride, int eagerCount) {
39 SkASSERT(!fLockStride && !fVertices && !fVertexData);
40 SkASSERT(stride && eagerCount);
41
42 fVertices = sk_malloc_throw(eagerCount * stride);
43 fLockStride = stride;
44
45 return fVertices;
46 }
47
unlock(int actualCount)48 void GrCpuVertexAllocator::unlock(int actualCount) {
49 SkASSERT(fLockStride && fVertices && !fVertexData);
50
51 fVertices = sk_realloc_throw(fVertices, actualCount * fLockStride);
52
53 fVertexData = GrThreadSafeCache::MakeVertexData(fVertices, actualCount, fLockStride);
54
55 fVertices = nullptr;
56 fLockStride = 0;
57 }
58
detachVertexData()59 sk_sp<GrThreadSafeCache::VertexData> GrCpuVertexAllocator::detachVertexData() {
60 SkASSERT(!fLockStride && !fVertices && fVertexData);
61
62 return std::move(fVertexData);
63 }
64