1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2021 Google LLC.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#include "src/gpu/GrVertexChunkArray.h"
9cb93a386Sopenharmony_ci
10cb93a386Sopenharmony_ci#include "src/gpu/GrMeshDrawTarget.h"
11cb93a386Sopenharmony_ci
12cb93a386Sopenharmony_ciGrVertexChunkBuilder::~GrVertexChunkBuilder() {
13cb93a386Sopenharmony_ci    if (!fChunks->empty()) {
14cb93a386Sopenharmony_ci        fTarget->putBackVertices(fCurrChunkVertexCapacity - fCurrChunkVertexCount, fStride);
15cb93a386Sopenharmony_ci        fChunks->back().fCount = fCurrChunkVertexCount;
16cb93a386Sopenharmony_ci    }
17cb93a386Sopenharmony_ci}
18cb93a386Sopenharmony_ci
19cb93a386Sopenharmony_cibool GrVertexChunkBuilder::allocChunk(int minCount) {
20cb93a386Sopenharmony_ci    if (!fChunks->empty()) {
21cb93a386Sopenharmony_ci        // No need to put back vertices; the buffer is full.
22cb93a386Sopenharmony_ci        fChunks->back().fCount = fCurrChunkVertexCount;
23cb93a386Sopenharmony_ci    }
24cb93a386Sopenharmony_ci    fCurrChunkVertexCount = 0;
25cb93a386Sopenharmony_ci    GrVertexChunk* chunk = &fChunks->push_back();
26cb93a386Sopenharmony_ci    int minAllocCount = std::max(minCount, fMinVerticesPerChunk);
27cb93a386Sopenharmony_ci    fCurrChunkVertexWriter = {fTarget->makeVertexSpaceAtLeast(fStride, minAllocCount,
28cb93a386Sopenharmony_ci                                                                minAllocCount, &chunk->fBuffer,
29cb93a386Sopenharmony_ci                                                                &chunk->fBase,
30cb93a386Sopenharmony_ci                                                                &fCurrChunkVertexCapacity)};
31cb93a386Sopenharmony_ci    if (!fCurrChunkVertexWriter || !chunk->fBuffer || fCurrChunkVertexCapacity < minCount) {
32cb93a386Sopenharmony_ci        SkDebugf("WARNING: Failed to allocate vertex buffer for GrVertexChunk.\n");
33cb93a386Sopenharmony_ci        fChunks->pop_back();
34cb93a386Sopenharmony_ci        SkASSERT(fCurrChunkVertexCount == 0);
35cb93a386Sopenharmony_ci        fCurrChunkVertexCapacity = 0;
36cb93a386Sopenharmony_ci        return false;
37cb93a386Sopenharmony_ci    }
38cb93a386Sopenharmony_ci    fMinVerticesPerChunk *= 2;
39cb93a386Sopenharmony_ci    return true;
40cb93a386Sopenharmony_ci}
41