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