blob: 9836817074157681d78b7474e277aef88606e206 [file] [log] [blame]
Nader Jawad5f0a8002023-02-21 17:00:51 -08001/*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef MESH_H_
18#define MESH_H_
19
20#include <GrDirectContext.h>
21#include <SkMesh.h>
22#include <jni.h>
23#include <log/log.h>
24
25#include <utility>
26
27class MeshUniformBuilder {
28public:
29 struct MeshUniform {
30 template <typename T>
31 std::enable_if_t<std::is_trivially_copyable<T>::value, MeshUniform> operator=(
32 const T& val) {
33 if (!fVar) {
34 LOG_FATAL("Assigning to missing variable");
35 } else if (sizeof(val) != fVar->sizeInBytes()) {
36 LOG_FATAL("Incorrect value size");
37 } else {
38 void* dst = reinterpret_cast<void*>(
39 reinterpret_cast<uint8_t*>(fOwner->writableUniformData()) + fVar->offset);
40 memcpy(dst, &val, sizeof(val));
41 }
42 }
43
44 MeshUniform& operator=(const SkMatrix& val) {
45 if (!fVar) {
46 LOG_FATAL("Assigning to missing variable");
47 } else if (fVar->sizeInBytes() != 9 * sizeof(float)) {
48 LOG_FATAL("Incorrect value size");
49 } else {
50 float* data = reinterpret_cast<float*>(
51 reinterpret_cast<uint8_t*>(fOwner->writableUniformData()) + fVar->offset);
52 data[0] = val.get(0);
53 data[1] = val.get(3);
54 data[2] = val.get(6);
55 data[3] = val.get(1);
56 data[4] = val.get(4);
57 data[5] = val.get(7);
58 data[6] = val.get(2);
59 data[7] = val.get(5);
60 data[8] = val.get(8);
61 }
62 return *this;
63 }
64
65 template <typename T>
66 bool set(const T val[], const int count) {
67 static_assert(std::is_trivially_copyable<T>::value, "Value must be trivial copyable");
68 if (!fVar) {
69 LOG_FATAL("Assigning to missing variable");
70 return false;
71 } else if (sizeof(T) * count != fVar->sizeInBytes()) {
72 LOG_FATAL("Incorrect value size");
73 return false;
74 } else {
75 void* dst = reinterpret_cast<void*>(
76 reinterpret_cast<uint8_t*>(fOwner->writableUniformData()) + fVar->offset);
77 memcpy(dst, val, sizeof(T) * count);
78 }
79 return true;
80 }
81
82 MeshUniformBuilder* fOwner;
83 const SkRuntimeEffect::Uniform* fVar;
84 };
85 MeshUniform uniform(std::string_view name) { return {this, fMeshSpec->findUniform(name)}; }
86
87 explicit MeshUniformBuilder(sk_sp<SkMeshSpecification> meshSpec) {
88 fMeshSpec = sk_sp(meshSpec);
89 fUniforms = (SkData::MakeZeroInitialized(meshSpec->uniformSize()));
90 }
91
92 sk_sp<SkData> fUniforms;
93
94private:
95 void* writableUniformData() {
96 if (!fUniforms->unique()) {
97 fUniforms = SkData::MakeWithCopy(fUniforms->data(), fUniforms->size());
98 }
99 return fUniforms->writable_data();
100 }
101
102 sk_sp<SkMeshSpecification> fMeshSpec;
103};
104
105class Mesh {
106public:
107 Mesh(const sk_sp<SkMeshSpecification>& meshSpec, int mode, const void* vertexBuffer,
108 size_t vertexBufferSize, jint vertexCount, jint vertexOffset,
109 std::unique_ptr<MeshUniformBuilder> builder, const SkRect& bounds)
110 : mMeshSpec(meshSpec)
111 , mMode(mode)
112 , mVertexCount(vertexCount)
113 , mVertexOffset(vertexOffset)
114 , mBuilder(std::move(builder))
115 , mBounds(bounds) {
116 copyToVector(mVertexBufferData, vertexBuffer, vertexBufferSize);
117 }
118
119 Mesh(const sk_sp<SkMeshSpecification>& meshSpec, int mode, const void* vertexBuffer,
120 size_t vertexBufferSize, jint vertexCount, jint vertexOffset, const void* indexBuffer,
121 size_t indexBufferSize, jint indexCount, jint indexOffset,
122 std::unique_ptr<MeshUniformBuilder> builder, const SkRect& bounds)
123 : mMeshSpec(meshSpec)
124 , mMode(mode)
125 , mVertexCount(vertexCount)
126 , mVertexOffset(vertexOffset)
127 , mIndexCount(indexCount)
128 , mIndexOffset(indexOffset)
129 , mBuilder(std::move(builder))
130 , mBounds(bounds) {
131 copyToVector(mVertexBufferData, vertexBuffer, vertexBufferSize);
132 copyToVector(mIndexBufferData, indexBuffer, indexBufferSize);
133 }
134
135 Mesh(Mesh&&) = default;
136
137 Mesh& operator=(Mesh&&) = default;
138
139 [[nodiscard]] std::tuple<bool, SkString> validate();
140
141 void updateSkMesh(GrDirectContext* context) const {
142 GrDirectContext::DirectContextID genId = GrDirectContext::DirectContextID();
143 if (context) {
144 genId = context->directContextID();
145 }
146
147 if (mIsDirty || genId != mGenerationId) {
148 auto vb = SkMesh::MakeVertexBuffer(
149 context, reinterpret_cast<const void*>(mVertexBufferData.data()),
150 mVertexBufferData.size());
151 auto meshMode = SkMesh::Mode(mMode);
152 if (!mIndexBufferData.empty()) {
153 auto ib = SkMesh::MakeIndexBuffer(
154 context, reinterpret_cast<const void*>(mIndexBufferData.data()),
155 mIndexBufferData.size());
156 mMesh = SkMesh::MakeIndexed(mMeshSpec, meshMode, vb, mVertexCount, mVertexOffset,
157 ib, mIndexCount, mIndexOffset, mBuilder->fUniforms,
158 mBounds)
159 .mesh;
160 } else {
161 mMesh = SkMesh::Make(mMeshSpec, meshMode, vb, mVertexCount, mVertexOffset,
162 mBuilder->fUniforms, mBounds)
163 .mesh;
164 }
165 mIsDirty = false;
166 mGenerationId = genId;
167 }
168 }
169
170 SkMesh& getSkMesh() const {
171 LOG_FATAL_IF(mIsDirty,
172 "Attempt to obtain SkMesh when Mesh is dirty, did you "
173 "forget to call updateSkMesh with a GrDirectContext? "
174 "Defensively creating a CPU mesh");
175 return mMesh;
176 }
177
178 void markDirty() { mIsDirty = true; }
179
180 MeshUniformBuilder* uniformBuilder() { return mBuilder.get(); }
181
182private:
183 void copyToVector(std::vector<uint8_t>& dst, const void* src, size_t srcSize) {
184 if (src) {
185 dst.resize(srcSize);
186 memcpy(dst.data(), src, srcSize);
187 }
188 }
189
190 sk_sp<SkMeshSpecification> mMeshSpec;
191 int mMode = 0;
192
193 std::vector<uint8_t> mVertexBufferData;
194 size_t mVertexCount = 0;
195 size_t mVertexOffset = 0;
196
197 std::vector<uint8_t> mIndexBufferData;
198 size_t mIndexCount = 0;
199 size_t mIndexOffset = 0;
200
201 std::unique_ptr<MeshUniformBuilder> mBuilder;
202 SkRect mBounds{};
203
204 mutable SkMesh mMesh{};
205 mutable bool mIsDirty = true;
206 mutable GrDirectContext::DirectContextID mGenerationId = GrDirectContext::DirectContextID();
207};
208#endif // MESH_H_