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