1 /*
2 * Copyright 2017 Google Inc.
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 "include/core/SkBitmap.h"
9 #include "src/gpu/gl/GrGLDefines.h"
10 #include "src/gpu/gl/GrGLUtil.h"
11 #include "tools/gpu/atlastext/GLTestAtlasTextRenderer.h"
12 #include "tools/gpu/atlastext/TestAtlasTextRenderer.h"
13 #include "tools/gpu/gl/GLTestContext.h"
14
15 using sk_gpu_test::GLTestContext;
16
17 namespace {
18
19 class GLTestAtlasTextRenderer : public sk_gpu_test::TestAtlasTextRenderer {
20 public:
21 GLTestAtlasTextRenderer(std::unique_ptr<GLTestContext>);
22
23 void* createTexture(AtlasFormat, int width, int height) override;
24
25 void deleteTexture(void* textureHandle) override;
26
27 void setTextureData(void* textureHandle, const void* data, int x, int y, int width, int height,
28 size_t rowBytes) override;
29
30 void drawSDFGlyphs(void* targetHandle, void* textureHandle, const SDFVertex vertices[],
31 int quadCnt) override;
32
33 void* makeTargetHandle(int width, int height) override;
34
35 void targetDeleted(void* targetHandle) override;
36
37 SkBitmap readTargetHandle(void* targetHandle) override;
38
39 void clearTarget(void* targetHandle, uint32_t color) override;
40
initialized() const41 bool initialized() const { return 0 != fProgram; }
42
43 private:
44 struct AtlasTexture {
45 GrGLuint fID;
46 AtlasFormat fFormat;
47 int fWidth;
48 int fHeight;
49 };
50
51 struct Target {
52 GrGLuint fFBOID;
53 GrGLuint fRBID;
54 int fWidth;
55 int fHeight;
56 };
57
58 std::unique_ptr<GLTestContext> fContext;
59 GrGLuint fProgram = 0;
60 GrGLint fDstScaleAndTranslateLocation = 0;
61 GrGLint fAtlasInvSizeLocation = 0;
62 GrGLint fSamplerLocation = 0;
63 };
64
65 #define callgl(NAME, ...) fContext->gl()->fFunctions.f##NAME(__VA_ARGS__)
66 #define checkgl() \
67 do { \
68 static constexpr auto line = __LINE__; \
69 auto error = fContext->gl()->fFunctions.fGetError(); \
70 if (error != GR_GL_NO_ERROR) { \
71 SkDebugf("GL ERROR: 0x%x, line %d\n", error, line); \
72 } \
73 } while (false)
74
GLTestAtlasTextRenderer(std::unique_ptr<GLTestContext> context)75 GLTestAtlasTextRenderer::GLTestAtlasTextRenderer(std::unique_ptr<GLTestContext> context)
76 : fContext(std::move(context)) {
77 auto restore = fContext->makeCurrentAndAutoRestore();
78
79 // First check whether the GL is supported so we can avoid spammy failures on systems
80 // where the GL simply doesn't work with this class.
81 const char* versionStr = reinterpret_cast<const char*>(callgl(GetString, GR_GL_VERSION));
82 auto version = GrGLGetVersionFromString(versionStr);
83 auto standard = GrGLGetStandardInUseFromString(versionStr);
84 switch (standard) {
85 case kWebGL_GrGLStandard:
86 case kNone_GrGLStandard:
87 return;
88 case kGLES_GrGLStandard:
89 if (version < GR_GL_VER(3, 0)) {
90 return;
91 }
92 break;
93 case kGL_GrGLStandard: {
94 if (version < GR_GL_VER(4, 3)) {
95 return;
96 }
97 GrGLint profileMask;
98 callgl(GetIntegerv, GR_GL_CONTEXT_PROFILE_MASK, &profileMask);
99 if (profileMask & GR_GL_CONTEXT_CORE_PROFILE_BIT) {
100 return;
101 }
102 }
103 }
104
105 auto vs = callgl(CreateShader, GR_GL_VERTEX_SHADER);
106 static constexpr char kGLVersionString[] = "#version 430 compatibility";
107 static constexpr char kGLESVersionString[] = "#version 300 es";
108 GrGLint lengths[2];
109 const GrGLchar* strings[2];
110 switch (fContext->gl()->fStandard) {
111 case kGL_GrGLStandard:
112 strings[0] = kGLVersionString;
113 lengths[0] = static_cast<GrGLint>(SK_ARRAY_COUNT(kGLVersionString)) - 1;
114 break;
115 case kGLES_GrGLStandard:
116 strings[0] = kGLESVersionString;
117 lengths[0] = static_cast<GrGLint>(SK_ARRAY_COUNT(kGLESVersionString)) - 1;
118 break;
119 default:
120 strings[0] = nullptr;
121 lengths[0] = 0;
122 break;
123 }
124
125 static constexpr const char kVS[] = R"(
126 uniform vec4 uDstScaleAndTranslate;
127 uniform vec2 uAtlasInvSize;
128
129 layout (location = 0) in vec3 inPosition;
130 layout (location = 1) in vec4 inColor;
131 layout (location = 2) in uvec2 inTextureCoords;
132
133 out vec2 vTexCoord;
134 out vec4 vColor;
135 out vec2 vIntTexCoord;
136
137 void main() {
138 vec2 intCoords;
139 // floor(vec2) doesn't seem to work on some ES devices.
140 intCoords.x = floor(float(inTextureCoords.x));
141 intCoords.y = floor(float(inTextureCoords.y));
142 vTexCoord = intCoords * uAtlasInvSize;
143 vIntTexCoord = intCoords;
144 vColor = inColor;
145 gl_Position = vec4(inPosition.x * uDstScaleAndTranslate.x + uDstScaleAndTranslate.y,
146 inPosition.y * uDstScaleAndTranslate.z + uDstScaleAndTranslate.w,
147 0.0, inPosition.z);
148 }
149 )";
150 strings[1] = kVS;
151 lengths[1] = SK_ARRAY_COUNT(kVS) - 1;
152 callgl(ShaderSource, vs, 2, strings, lengths);
153 callgl(CompileShader, vs);
154 GrGLint compileStatus;
155 callgl(GetShaderiv, vs, GR_GL_COMPILE_STATUS, &compileStatus);
156 if (compileStatus == GR_GL_FALSE) {
157 GrGLint logLength;
158 callgl(GetShaderiv, vs, GR_GL_INFO_LOG_LENGTH, &logLength);
159 std::unique_ptr<GrGLchar[]> log(new GrGLchar[logLength + 1]);
160 log[logLength] = '\0';
161 callgl(GetShaderInfoLog, vs, logLength, &logLength, log.get());
162 SkDebugf("Vertex Shader failed to compile\n%s", log.get());
163 callgl(DeleteShader, vs);
164 return;
165 }
166
167 auto fs = callgl(CreateShader, GR_GL_FRAGMENT_SHADER);
168 static constexpr const char kFS[] = R"(
169 uniform sampler2D uSampler;
170
171 in vec2 vTexCoord;
172 in vec4 vColor;
173 in vec2 vIntTexCoord;
174
175 layout (location = 0) out vec4 outColor;
176
177 void main() {
178 float sdfValue = texture(uSampler, vTexCoord).r;
179 float distance = 7.96875 * (sdfValue - 0.50196078431000002);
180 vec2 dist_grad = vec2(dFdx(distance), dFdy(distance));
181 vec2 Jdx = dFdx(vIntTexCoord);
182 vec2 Jdy = dFdy(vIntTexCoord);
183 float dg_len2 = dot(dist_grad, dist_grad);
184 if (dg_len2 < 0.0001) {
185 dist_grad = vec2(0.7071, 0.7071);
186 } else {
187 dist_grad = dist_grad * inversesqrt(dg_len2);
188 }
189 vec2 grad = vec2(dist_grad.x * Jdx.x + dist_grad.y * Jdy.x,
190 dist_grad.x * Jdx.y + dist_grad.y * Jdy.y);
191 float afwidth = abs(0.65000000000000002 * length(grad));
192 float value = smoothstep(-afwidth, afwidth, distance);
193 outColor = value * vec4(vColor.rgb * vColor.a, vColor.a);
194 }
195 )";
196 strings[1] = kFS;
197 lengths[1] = SK_ARRAY_COUNT(kFS) - 1;
198 callgl(ShaderSource, fs, 2, strings, lengths);
199 callgl(CompileShader, fs);
200 callgl(GetShaderiv, fs, GR_GL_COMPILE_STATUS, &compileStatus);
201 if (compileStatus == GR_GL_FALSE) {
202 GrGLint logLength;
203 callgl(GetShaderiv, fs, GR_GL_INFO_LOG_LENGTH, &logLength);
204 std::unique_ptr<GrGLchar[]> log(new GrGLchar[logLength + 1]);
205 log[logLength] = '\0';
206 callgl(GetShaderInfoLog, fs, logLength, &logLength, log.get());
207 SkDebugf("Fragment Shader failed to compile\n%s", log.get());
208 callgl(DeleteShader, vs);
209 callgl(DeleteShader, fs);
210 return;
211 }
212
213 fProgram = callgl(CreateProgram);
214 if (!fProgram) {
215 callgl(DeleteShader, vs);
216 callgl(DeleteShader, fs);
217 return;
218 }
219
220 callgl(AttachShader, fProgram, vs);
221 callgl(AttachShader, fProgram, fs);
222 callgl(LinkProgram, fProgram);
223 GrGLint linkStatus;
224 callgl(GetProgramiv, fProgram, GR_GL_LINK_STATUS, &linkStatus);
225 if (linkStatus == GR_GL_FALSE) {
226 GrGLint logLength = 0;
227 callgl(GetProgramiv, vs, GR_GL_INFO_LOG_LENGTH, &logLength);
228 std::unique_ptr<GrGLchar[]> log(new GrGLchar[logLength + 1]);
229 log[logLength] = '\0';
230 callgl(GetProgramInfoLog, vs, logLength, &logLength, log.get());
231 SkDebugf("Program failed to link\n%s", log.get());
232 callgl(DeleteShader, vs);
233 callgl(DeleteShader, fs);
234 callgl(DeleteProgram, fProgram);
235 fProgram = 0;
236 return;
237 }
238 fDstScaleAndTranslateLocation = callgl(GetUniformLocation, fProgram, "uDstScaleAndTranslate");
239 fAtlasInvSizeLocation = callgl(GetUniformLocation, fProgram, "uAtlasInvSize");
240 fSamplerLocation = callgl(GetUniformLocation, fProgram, "uSampler");
241 if (fDstScaleAndTranslateLocation < 0 || fAtlasInvSizeLocation < 0 || fSamplerLocation < 0) {
242 callgl(DeleteShader, vs);
243 callgl(DeleteShader, fs);
244 callgl(DeleteProgram, fProgram);
245 fProgram = 0;
246 }
247
248 checkgl();
249 }
250
atlas_format_to_gl_types(SkAtlasTextRenderer::AtlasFormat format,GrGLenum * internalFormat,GrGLenum * externalFormat,GrGLenum * type)251 inline bool atlas_format_to_gl_types(SkAtlasTextRenderer::AtlasFormat format,
252 GrGLenum* internalFormat, GrGLenum* externalFormat,
253 GrGLenum* type) {
254 switch (format) {
255 case SkAtlasTextRenderer::AtlasFormat::kA8:
256 *internalFormat = GR_GL_R8;
257 *externalFormat = GR_GL_RED;
258 *type = GR_GL_UNSIGNED_BYTE;
259 return true;
260 }
261 return false;
262 }
263
atlas_format_bytes_per_pixel(SkAtlasTextRenderer::AtlasFormat format)264 inline int atlas_format_bytes_per_pixel(SkAtlasTextRenderer::AtlasFormat format) {
265 switch (format) {
266 case SkAtlasTextRenderer::AtlasFormat::kA8:
267 return 1;
268 }
269 return 0;
270 }
271
createTexture(AtlasFormat format,int width,int height)272 void* GLTestAtlasTextRenderer::createTexture(AtlasFormat format, int width, int height) {
273 GrGLenum internalFormat;
274 GrGLenum externalFormat;
275 GrGLenum type;
276 if (!atlas_format_to_gl_types(format, &internalFormat, &externalFormat, &type)) {
277 return nullptr;
278 }
279 auto restore = fContext->makeCurrentAndAutoRestore();
280
281 GrGLuint id;
282 callgl(GenTextures, 1, &id);
283 if (!id) {
284 return nullptr;
285 }
286
287 callgl(BindTexture, GR_GL_TEXTURE_2D, id);
288 callgl(TexImage2D, GR_GL_TEXTURE_2D, 0, internalFormat, width, height, 0, externalFormat, type,
289 nullptr);
290 checkgl();
291
292 AtlasTexture* atlas = new AtlasTexture;
293 atlas->fID = id;
294 atlas->fFormat = format;
295 atlas->fWidth = width;
296 atlas->fHeight = height;
297 return atlas;
298 }
299
deleteTexture(void * textureHandle)300 void GLTestAtlasTextRenderer::deleteTexture(void* textureHandle) {
301 auto restore = fContext->makeCurrentAndAutoRestore();
302
303 auto* atlasTexture = reinterpret_cast<const AtlasTexture*>(textureHandle);
304
305 callgl(DeleteTextures, 1, &atlasTexture->fID);
306 checkgl();
307
308 delete atlasTexture;
309 }
310
setTextureData(void * textureHandle,const void * data,int x,int y,int width,int height,size_t rowBytes)311 void GLTestAtlasTextRenderer::setTextureData(void* textureHandle, const void* data, int x, int y,
312 int width, int height, size_t rowBytes) {
313 auto restore = fContext->makeCurrentAndAutoRestore();
314
315 auto atlasTexture = reinterpret_cast<const AtlasTexture*>(textureHandle);
316
317 GrGLenum internalFormat;
318 GrGLenum externalFormat;
319 GrGLenum type;
320 if (!atlas_format_to_gl_types(atlasTexture->fFormat, &internalFormat, &externalFormat, &type)) {
321 return;
322 }
323 int bpp = atlas_format_bytes_per_pixel(atlasTexture->fFormat);
324 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
325 if (static_cast<size_t>(rowLength * bpp) != rowBytes) {
326 return;
327 }
328 callgl(PixelStorei, GR_GL_UNPACK_ALIGNMENT, 1);
329 callgl(PixelStorei, GR_GL_UNPACK_ROW_LENGTH, rowLength);
330 callgl(BindTexture, GR_GL_TEXTURE_2D, atlasTexture->fID);
331 callgl(TexSubImage2D, GR_GL_TEXTURE_2D, 0, x, y, width, height, externalFormat, type, data);
332 checkgl();
333 }
334
drawSDFGlyphs(void * targetHandle,void * textureHandle,const SDFVertex vertices[],int quadCnt)335 void GLTestAtlasTextRenderer::drawSDFGlyphs(void* targetHandle, void* textureHandle,
336 const SDFVertex vertices[], int quadCnt) {
337 auto restore = fContext->makeCurrentAndAutoRestore();
338
339 auto target = reinterpret_cast<const Target*>(targetHandle);
340 auto atlas = reinterpret_cast<const AtlasTexture*>(textureHandle);
341
342 callgl(UseProgram, fProgram);
343
344 callgl(ActiveTexture, GR_GL_TEXTURE0);
345 callgl(BindTexture, GR_GL_TEXTURE_2D, atlas->fID);
346 callgl(TexParameteri, GR_GL_TEXTURE_2D, GR_GL_TEXTURE_MAG_FILTER, GR_GL_LINEAR);
347 callgl(TexParameteri, GR_GL_TEXTURE_2D, GR_GL_TEXTURE_MIN_FILTER, GR_GL_LINEAR);
348
349 float uniformScaleAndTranslate[4] = {2.f / target->fWidth, -1.f, 2.f / target->fHeight, -1.f};
350 callgl(Uniform4fv, fDstScaleAndTranslateLocation, 1, uniformScaleAndTranslate);
351 callgl(Uniform2f, fAtlasInvSizeLocation, 1.f / atlas->fWidth, 1.f / atlas->fHeight);
352 callgl(Uniform1i, fSamplerLocation, 0);
353
354 callgl(BindFramebuffer, GR_GL_FRAMEBUFFER, target->fFBOID);
355 callgl(Viewport, 0, 0, target->fWidth, target->fHeight);
356
357 callgl(Enable, GR_GL_BLEND);
358 callgl(BlendFunc, GR_GL_ONE, GR_GL_ONE_MINUS_SRC_ALPHA);
359 callgl(Disable, GR_GL_DEPTH_TEST);
360
361 callgl(BindVertexArray, 0);
362 callgl(BindBuffer, GR_GL_ARRAY_BUFFER, 0);
363 callgl(BindBuffer, GR_GL_ELEMENT_ARRAY_BUFFER, 0);
364 callgl(VertexAttribPointer, 0, 3, GR_GL_FLOAT, GR_GL_FALSE, sizeof(SDFVertex), vertices);
365 size_t colorOffset = 3 * sizeof(float);
366 callgl(VertexAttribPointer, 1, 4, GR_GL_UNSIGNED_BYTE, GR_GL_TRUE, sizeof(SDFVertex),
367 reinterpret_cast<const char*>(vertices) + colorOffset);
368 size_t texOffset = colorOffset + sizeof(uint32_t);
369 callgl(VertexAttribIPointer, 2, 2, GR_GL_UNSIGNED_SHORT, sizeof(SDFVertex),
370 reinterpret_cast<const char*>(vertices) + texOffset);
371 callgl(EnableVertexAttribArray, 0);
372 callgl(EnableVertexAttribArray, 1);
373 callgl(EnableVertexAttribArray, 2);
374
375 std::unique_ptr<uint16_t[]> indices(new uint16_t[quadCnt * 6]);
376 for (int q = 0; q < quadCnt; ++q) {
377 indices[q * 6 + 0] = 0 + 4 * q;
378 indices[q * 6 + 1] = 1 + 4 * q;
379 indices[q * 6 + 2] = 2 + 4 * q;
380 indices[q * 6 + 3] = 2 + 4 * q;
381 indices[q * 6 + 4] = 1 + 4 * q;
382 indices[q * 6 + 5] = 3 + 4 * q;
383 }
384 callgl(DrawElements, GR_GL_TRIANGLES, 6 * quadCnt, GR_GL_UNSIGNED_SHORT, indices.get());
385 checkgl();
386 }
387
makeTargetHandle(int width,int height)388 void* GLTestAtlasTextRenderer::makeTargetHandle(int width, int height) {
389 auto restore = fContext->makeCurrentAndAutoRestore();
390
391 GrGLuint fbo;
392 callgl(GenFramebuffers, 1, &fbo);
393 if (!fbo) {
394 return nullptr;
395 }
396 GrGLuint rb;
397 callgl(GenRenderbuffers, 1, &rb);
398 if (!rb) {
399 callgl(DeleteFramebuffers, 1, &fbo);
400 return nullptr;
401 }
402 callgl(BindFramebuffer, GR_GL_FRAMEBUFFER, fbo);
403 callgl(BindRenderbuffer, GR_GL_RENDERBUFFER, rb);
404 callgl(RenderbufferStorage, GR_GL_RENDERBUFFER, GR_GL_RGBA8, width, height);
405 callgl(FramebufferRenderbuffer, GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_RENDERBUFFER,
406 rb);
407 GrGLenum status = callgl(CheckFramebufferStatus, GR_GL_FRAMEBUFFER);
408 if (GR_GL_FRAMEBUFFER_COMPLETE != status) {
409 callgl(DeleteFramebuffers, 1, &fbo);
410 callgl(DeleteRenderbuffers, 1, &rb);
411 return nullptr;
412 }
413 callgl(Disable, GR_GL_SCISSOR_TEST);
414 callgl(ClearColor, 0, 0, 0, 0.0);
415 callgl(Clear, GR_GL_COLOR_BUFFER_BIT);
416 checkgl();
417 Target* target = new Target;
418 target->fFBOID = fbo;
419 target->fRBID = rb;
420 target->fWidth = width;
421 target->fHeight = height;
422 return target;
423 }
424
targetDeleted(void * targetHandle)425 void GLTestAtlasTextRenderer::targetDeleted(void* targetHandle) {
426 auto restore = fContext->makeCurrentAndAutoRestore();
427
428 Target* target = reinterpret_cast<Target*>(targetHandle);
429 callgl(DeleteFramebuffers, 1, &target->fFBOID);
430 callgl(DeleteRenderbuffers, 1, &target->fRBID);
431 delete target;
432 }
433
readTargetHandle(void * targetHandle)434 SkBitmap GLTestAtlasTextRenderer::readTargetHandle(void* targetHandle) {
435 auto restore = fContext->makeCurrentAndAutoRestore();
436
437 Target* target = reinterpret_cast<Target*>(targetHandle);
438
439 auto info =
440 SkImageInfo::Make(target->fWidth, target->fHeight, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
441 SkBitmap bmp;
442 bmp.setInfo(info, sizeof(uint32_t) * target->fWidth);
443 bmp.allocPixels();
444
445 callgl(BindFramebuffer, GR_GL_FRAMEBUFFER, target->fFBOID);
446 callgl(ReadPixels, 0, 0, target->fWidth, target->fHeight, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE,
447 bmp.getPixels());
448 checkgl();
449 return bmp;
450 }
451
clearTarget(void * targetHandle,uint32_t color)452 void GLTestAtlasTextRenderer::clearTarget(void* targetHandle, uint32_t color) {
453 auto restore = fContext->makeCurrentAndAutoRestore();
454
455 Target* target = reinterpret_cast<Target*>(targetHandle);
456 callgl(BindFramebuffer, GR_GL_FRAMEBUFFER, target->fFBOID);
457 callgl(Disable, GR_GL_SCISSOR_TEST);
458 float r = ((color >> 0) & 0xff) / 255.f;
459 float g = ((color >> 8) & 0xff) / 255.f;
460 float b = ((color >> 16) & 0xff) / 255.f;
461 float a = ((color >> 24) & 0xff) / 255.f;
462 callgl(ClearColor, r, g, b, a);
463 callgl(Clear, GR_GL_COLOR_BUFFER_BIT);
464 }
465
466 } // anonymous namespace
467
468 namespace sk_gpu_test {
469
MakeGLTestAtlasTextRenderer()470 sk_sp<TestAtlasTextRenderer> MakeGLTestAtlasTextRenderer() {
471 std::unique_ptr<GLTestContext> context(CreatePlatformGLTestContext(kGL_GrGLStandard));
472 if (!context) {
473 context.reset(CreatePlatformGLTestContext(kGLES_GrGLStandard));
474 }
475 if (!context) {
476 return nullptr;
477 }
478 auto restorer = context->makeCurrentAndAutoRestore();
479 auto renderer = sk_make_sp<GLTestAtlasTextRenderer>(std::move(context));
480 return renderer->initialized() ? std::move(renderer) : nullptr;
481 }
482
483 } // namespace sk_gpu_test
484