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/GrMeshDrawTarget.h"
9
10 #include "src/gpu/BufferWriter.h"
11 #include "src/gpu/ganesh/GrResourceProvider.h"
12
contextUniqueID() const13 uint32_t GrMeshDrawTarget::contextUniqueID() const {
14 return this->resourceProvider()->contextUniqueID();
15 }
16
17 template<typename W>
make_writer(void * p,int count,size_t elementSize)18 static W make_writer(void* p, int count, size_t elementSize) {
19 // Don't worry about overflow in calculating byte size if 'p' is not null, presumably the actual
20 // allocation validated it, so it should be safe after the fact.
21 return p ? W{p, count * elementSize} : W{};
22 }
23
makeVertexWriter(size_t vertexSize,int vertexCount,sk_sp<const GrBuffer> * buffer,int * startVertex)24 skgpu::VertexWriter GrMeshDrawTarget::makeVertexWriter(
25 size_t vertexSize, int vertexCount, sk_sp<const GrBuffer>* buffer, int* startVertex) {
26 void* p = this->makeVertexSpace(vertexSize, vertexCount, buffer, startVertex);
27 return make_writer<skgpu::VertexWriter>(p, vertexCount, vertexSize);
28 }
29
makeIndexWriter(int indexCount,sk_sp<const GrBuffer> * buffer,int * startIndex)30 skgpu::IndexWriter GrMeshDrawTarget::makeIndexWriter(
31 int indexCount, sk_sp<const GrBuffer>* buffer, int* startIndex) {
32 void* p = this->makeIndexSpace(indexCount, buffer, startIndex);
33 return make_writer<skgpu::IndexWriter>(p, indexCount, sizeof(uint16_t));
34 }
35
makeVertexWriterAtLeast(size_t vertexSize,int minVertexCount,int fallbackVertexCount,sk_sp<const GrBuffer> * buffer,int * startVertex,int * actualVertexCount)36 skgpu::VertexWriter GrMeshDrawTarget::makeVertexWriterAtLeast(
37 size_t vertexSize, int minVertexCount, int fallbackVertexCount,
38 sk_sp<const GrBuffer>* buffer, int* startVertex, int* actualVertexCount) {
39 void* p = this->makeVertexSpaceAtLeast(vertexSize, minVertexCount, fallbackVertexCount,
40 buffer, startVertex, actualVertexCount);
41 return make_writer<skgpu::VertexWriter>(p, *actualVertexCount, vertexSize);
42 }
43
makeIndexWriterAtLeast(int minIndexCount,int fallbackIndexCount,sk_sp<const GrBuffer> * buffer,int * startIndex,int * actualIndexCount)44 skgpu::IndexWriter GrMeshDrawTarget::makeIndexWriterAtLeast(
45 int minIndexCount, int fallbackIndexCount, sk_sp<const GrBuffer>* buffer,
46 int* startIndex, int* actualIndexCount) {
47 void* p = this->makeIndexSpaceAtLeast(minIndexCount, fallbackIndexCount, buffer,
48 startIndex, actualIndexCount);
49 return make_writer<skgpu::IndexWriter>(p, *actualIndexCount, sizeof(uint16_t));
50 }
51