Flutter Engine
The Flutter Engine
GrMeshBuffers.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2023 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
9
10#include "include/core/SkData.h"
11#include "include/core/SkMesh.h"
16#include "src/core/SkMeshPriv.h"
25
26#include <cstring>
27#include <utility>
28
29template <typename Base, GrGpuBufferType Type> GrMeshBuffer<Base, Type>::~GrMeshBuffer() {
30 GrResourceCache::ReturnResourceFromThread(std::move(fBuffer), fContextID);
31}
32
33template <typename Base, GrGpuBufferType Type>
35 SkASSERT(dc);
36
38 size,
39 Type,
42 if (!buffer) {
43 return nullptr;
44 }
45
46 if (data && !buffer->updateData(data, 0, size, /*preserve=*/false)) {
47 return nullptr;
48 }
49
50 auto result = new GrMeshBuffer;
51 result->fBuffer = std::move(buffer);
52 result->fContextID = dc->directContextID();
53 return sk_sp<Base>(result);
54}
55
56template <typename Base, GrGpuBufferType Type>
58 const void* data,
59 size_t offset,
60 size_t size) {
61 if (!dc || dc != fBuffer->getContext()) {
62 return false;
63 }
64 SkASSERT(!dc->abandoned()); // If dc is abandoned then fBuffer->getContext() should be null.
65
67 auto ownedData = SkData::MakeWithCopy(data, size);
69 std::move(ownedData), fBuffer, offset);
70 return true;
71 }
72
73 sk_sp<GrGpuBuffer> tempBuffer;
74 size_t tempOffset = 0;
75 if (auto* sbm = dc->priv().getGpu()->stagingBufferManager()) {
76 auto alignment = dc->priv().caps()->transferFromBufferToBufferAlignment();
77 auto [sliceBuffer, sliceOffset, ptr] = sbm->allocateStagingBufferSlice(size, alignment);
78 if (sliceBuffer) {
79 std::memcpy(ptr, data, size);
80 tempBuffer.reset(SkRef(sliceBuffer));
81 tempOffset = sliceOffset;
82 }
83 }
84
85 if (!tempBuffer) {
86 tempBuffer = dc->priv().resourceProvider()->createBuffer(size,
90 if (!tempBuffer) {
91 return false;
92 }
93 if (!tempBuffer->updateData(data, 0, size, /*preserve=*/false)) {
94 return false;
95 }
96 }
97
99 std::move(tempBuffer), tempOffset, fBuffer, offset, size);
100
101 return true;
102}
103
104namespace SkMeshes {
106 if (!dc) {
107 // Fallback to a CPU buffer.
108 return MakeIndexBuffer(data, size);
109 }
111}
112
114 if (!src) {
115 return nullptr;
116 }
117 auto* ib = static_cast<SkMeshPriv::IB*>(src.get());
118 const void* data = ib->peek();
119 if (!data) {
120 return nullptr;
121 }
122 if (!dc) {
123 return MakeIndexBuffer(data, ib->size());
124 }
125 return MakeIndexBuffer(dc, data, ib->size());
126}
127
129 if (!dc) {
130 return MakeVertexBuffer(data, size);
131 }
133}
134
136 if (!src) {
137 return nullptr;
138 }
139 auto* vb = static_cast<SkMeshPriv::VB*>(src.get());
140 const void* data = vb->peek();
141 if (!data) {
142 return nullptr;
143 }
144 if (!dc) {
145 return MakeVertexBuffer(data, vb->size());
146 }
147 return MakeVertexBuffer(dc, data, vb->size());
148}
149} // namespace SkMeshes
@ kDynamic_GrAccessPattern
Definition: GrTypesPriv.h:426
@ kStatic_GrAccessPattern
Definition: GrTypesPriv.h:428
#define SkASSERT(cond)
Definition: SkAssert.h:116
static T * SkRef(T *obj)
Definition: SkRefCnt.h:132
const GrCaps * caps() const
bool transferFromBufferToBufferSupport() const
Definition: GrCaps.h:369
size_t transferFromBufferToBufferAlignment() const
Definition: GrCaps.h:244
GrResourceProvider * resourceProvider()
bool abandoned() override
DirectContextID directContextID() const
GrDirectContextPriv priv()
void newBufferUpdateTask(sk_sp< SkData > src, sk_sp< GrGpuBuffer > dst, size_t dstOffset)
void newBufferTransferTask(sk_sp< GrGpuBuffer > src, size_t srcOffset, sk_sp< GrGpuBuffer > dst, size_t dstOffset, size_t size)
bool updateData(const void *src, size_t offset, size_t size, bool preserve)
Definition: GrGpuBuffer.cpp:63
virtual GrStagingBufferManager * stagingBufferManager()
Definition: GrGpu.h:76
~GrMeshBuffer() override
static sk_sp< Base > Make(GrDirectContext *, const void *data, size_t size)
GrDrawingManager * drawingManager()
static std::enable_if_t< std::is_base_of_v< GrGpuResource, T >, void > ReturnResourceFromThread(sk_sp< T > &&resource, GrDirectContext::DirectContextID id)
sk_sp< GrGpuBuffer > createBuffer(size_t size, GrGpuBufferType, GrAccessPattern, ZeroInit)
static sk_sp< SkData > MakeWithCopy(const void *data, size_t length)
Definition: SkData.cpp:111
void reset(T *ptr=nullptr)
Definition: SkRefCnt.h:310
GAsyncResult * result
SK_API sk_sp< SkMesh::IndexBuffer > CopyIndexBuffer(const sk_sp< SkMesh::IndexBuffer > &)
Definition: SkMesh.cpp:893
SK_API sk_sp< SkMesh::IndexBuffer > MakeIndexBuffer(const void *data, size_t size)
Definition: SkMesh.cpp:889
SK_API sk_sp< SkMesh::VertexBuffer > MakeVertexBuffer(const void *, size_t size)
Definition: SkMesh.cpp:905
SK_API sk_sp< SkMesh::VertexBuffer > CopyVertexBuffer(const sk_sp< SkMesh::VertexBuffer > &)
Definition: SkMesh.cpp:909
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
SeparatedVector2 offset
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63