File: textured_quad_renderer.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (377 lines) | stat: -rw-r--r-- 12,917 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/vr/renderers/textured_quad_renderer.h"

#include "device/vr/vr_gl_util.h"
#include "ui/gfx/geometry/transform.h"

namespace vr {

namespace {

// clang-format off
//
// A rounded rect is subdivided into a number of triangles.
// _______________
// | /    _,-' \ |
// |/_,,-'______\|
// |            /|
// |           / |
// |          /  |
// |         /   |
// |        /    |
// |       /     |
// |      /      |
// |     /       |
// |    /        |
// |   /         |
// |  /          |
// | /           |
// |/____________|
// |\     _,-'' /|
// |_\ ,-'____ /_|
//
// Most of these do not contain an arc. To simplify the rendering of those
// that do, we include a "corner position" attribute. The corner position is
// the distance from the center of the nearest "corner circle". Only those
// triangles containing arcs have a non-zero corner position set. The result
// is that for interior triangles, their corner position is uniformly (0, 0).
// I.e., they are always deemed "inside".
//
// A further complication is that different corner radii will require these
// various triangles to be sized differently relative to one another. We
// would prefer not no continually recreate our vertex buffer, so we include
// another attribute, the "offset scalars". These scalars are only ever 1.0,
// 0.0, or -1.0 and control the addition or subtraction of the horizontal
// and vertical corner offset. This lets the corners of the triangles be
// computed in the vertex shader dynamically. It also happens that the
// texture coordinates can also be easily computed in the vertex shader.
//
// So if the the corner offsets are vr and hr where
//     vr = corner_radius / height;
//     hr = corner_radius / width;
//
// Then the full position is then given by
//   p = (x + osx * hr, y + osy * vr, 0.0, 1.0)
//
// And the full texture coordinate is given by
//   (0.5 + p[0], 0.5 - p[1])
//
static constexpr float kVertices[120] = {
    //  x      y   osx   osy  cpx  cpy
    -0.5f,  0.5f,  0.0, -1.0, 0.0, 0.0,
    -0.5f,  0.5f,  1.0,  0.0, 0.0, 0.0,
     0.5f,  0.5f, -1.0,  0.0, 0.0, 0.0,
     0.5f,  0.5f,  0.0, -1.0, 0.0, 0.0,
    -0.5f, -0.5f,  0.0,  1.0, 0.0, 0.0,
     0.5f, -0.5f,  0.0,  1.0, 0.0, 0.0,
    -0.5f, -0.5f,  1.0,  0.0, 0.0, 0.0,
     0.5f, -0.5f, -1.0,  0.0, 0.0, 0.0,

     // These are the corner triangles (note the non-zero cpx and cpy).
    -0.5f,  0.5f,  0.0, -1.0, 1.0, 0.0,
    -0.5f,  0.5f,  0.0,  0.0, 1.0, 1.0,
    -0.5f,  0.5f,  1.0,  0.0, 0.0, 1.0,
     0.5f,  0.5f, -1.0,  0.0, 0.0, 1.0,
     0.5f,  0.5f,  0.0,  0.0, 1.0, 1.0,
     0.5f,  0.5f,  0.0, -1.0, 1.0, 0.0,
    -0.5f, -0.5f,  0.0,  0.0, 1.0, 1.0,
    -0.5f, -0.5f,  0.0,  1.0, 1.0, 0.0,
    -0.5f, -0.5f,  1.0,  0.0, 0.0, 1.0,
     0.5f, -0.5f, -1.0,  0.0, 0.0, 1.0,
     0.5f, -0.5f,  0.0,  1.0, 1.0, 0.0,
     0.5f, -0.5f,  0.0,  0.0, 1.0, 1.0,
};

static constexpr GLushort kIndices[30] = {
    // This is the top trapezoid.
    0,  2,  1,
    0,  3,  2,

    // These are the central triangles (the only triangles that matter if our
    // corner radius is zero).
    4,  3,  0,
    4,  5,  3,

    // This is the bottom trapezoid.
    4,  6,  5,
    6,  7,  5,

    // These are the corners.
    8,  10, 9,
    11, 13, 12,
    14, 16, 15,
    17, 19, 18,
};

static constexpr int kPositionDataSize = 2;
static constexpr size_t kPositionDataOffset = 0;
static constexpr int kOffsetScaleDataSize = 2;
static constexpr size_t kOffsetScaleDataOffset = 2 * sizeof(float);
static constexpr int kCornerPositionDataSize = 2;
static constexpr size_t kCornerPositionDataOffset = 4 * sizeof(float);
static constexpr size_t kDataStride = 6 * sizeof(float);
static constexpr size_t kInnerRectOffset = 6 * sizeof(GLushort);

static constexpr char const* kVertexShader = SHADER(
  precision mediump float;
  uniform mat4 u_ModelViewProjMatrix;
  uniform vec2 u_CornerOffset;
  attribute vec4 a_Position;
  attribute vec2 a_CornerPosition;
  attribute vec2 a_OffsetScale;
  varying vec2 v_TexCoordinate;
  varying vec2 v_CornerPosition;
  uniform bool u_UsesOverlay;

  void main() {
    v_CornerPosition = a_CornerPosition;
    vec4 position = vec4(
        a_Position[0] + u_CornerOffset[0] * a_OffsetScale[0],
        a_Position[1] + u_CornerOffset[1] * a_OffsetScale[1],
        a_Position[2],
        a_Position[3]);
    v_TexCoordinate = vec2(0.5 + position[0], 0.5 - position[1]);
    gl_Position = u_ModelViewProjMatrix * position;
  }
);

static constexpr char const* kFragmentShader = SHADER(
  precision highp float;
  uniform sampler2D u_Texture;
  uniform sampler2D u_OverlayTexture;
  uniform vec2 u_ClipRect[2];
  varying vec2 v_TexCoordinate;
  varying vec2 v_CornerPosition;
  uniform mediump float u_Opacity;
  uniform mediump float u_OverlayOpacity;

  void main() {
    vec2 s = step(u_ClipRect[0], v_TexCoordinate)
        - step(u_ClipRect[1], v_TexCoordinate);
    float insideClip = s.x * s.y;

    lowp vec4 color = texture2D(u_Texture, v_TexCoordinate);
    float mask = 1.0 - step(1.0, length(v_CornerPosition));
    gl_FragColor = insideClip * color * u_Opacity * mask;
  }
);
// clang-format on

}  // namespace

TexturedQuadRenderer::TexturedQuadRenderer()
    : TexturedQuadRenderer(kVertexShader, kFragmentShader) {}

TexturedQuadRenderer::TexturedQuadRenderer(const char* vertex_src,
                                           const char* fragment_src)
    : BaseRenderer(vertex_src, fragment_src) {
  model_view_proj_matrix_handle_ =
      glGetUniformLocation(program_handle_, "u_ModelViewProjMatrix");
  corner_offset_handle_ =
      glGetUniformLocation(program_handle_, "u_CornerOffset");
  corner_position_handle_ =
      glGetAttribLocation(program_handle_, "a_CornerPosition");
  offset_scale_handle_ = glGetAttribLocation(program_handle_, "a_OffsetScale");

  opacity_handle_ = glGetUniformLocation(program_handle_, "u_Opacity");
  overlay_opacity_handle_ =
      glGetUniformLocation(program_handle_, "u_OverlayOpacity");
  texture_handle_ = glGetUniformLocation(program_handle_, "u_Texture");
  overlay_texture_handle_ =
      glGetUniformLocation(program_handle_, "u_OverlayTexture");
  uses_overlay_handle_ = glGetUniformLocation(program_handle_, "u_UsesOverlay");
}

TexturedQuadRenderer::~TexturedQuadRenderer() = default;

void TexturedQuadRenderer::AddQuad(int texture_data_handle,
                                   int overlay_texture_data_handle,
                                   const gfx::Transform& model_view_proj_matrix,
                                   const gfx::RectF& clip_rect,
                                   float opacity,
                                   const gfx::SizeF& element_size,
                                   float corner_radius,
                                   bool blend) {
  if (!clip_rect.Intersects(gfx::RectF(1.0f, 1.0f)))
    return;
  QuadData quad;
  quad.texture_data_handle = texture_data_handle;
  quad.overlay_texture_data_handle = overlay_texture_data_handle;
  quad.model_view_proj_matrix = model_view_proj_matrix;
  quad.clip_rect = clip_rect;
  quad.opacity = opacity;
  quad.element_size = element_size;
  quad.corner_radius = corner_radius;
  quad.blend = blend;
  quad_queue_.push(quad);
}

void TexturedQuadRenderer::Flush() {
  if (quad_queue_.empty())
    return;

  int last_texture = -1;
  int last_overlay_texture = -1;
  float last_opacity = -1.0f;
  gfx::SizeF last_element_size;
  float last_corner_radius = -1.0f;
  gfx::RectF last_clip_rect;
  bool last_blend = true;  // All elements blend by default.

  // Set up GL state that doesn't change between draw calls.
  glUseProgram(program_handle_);

  glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_);
  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer_);

  // Link texture data with texture unit.
  glUniform1i(texture_handle_, 0);

  glUniform1i(overlay_texture_handle_, 1);

  // Set up position attribute.
  glVertexAttribPointer(position_handle_, kPositionDataSize, GL_FLOAT, false,
                        kDataStride, VOID_OFFSET(kPositionDataOffset));
  glEnableVertexAttribArray(position_handle_);

  // Set up offset scale attribute.
  glVertexAttribPointer(offset_scale_handle_, kOffsetScaleDataSize, GL_FLOAT,
                        false, kDataStride,
                        VOID_OFFSET(kOffsetScaleDataOffset));
  glEnableVertexAttribArray(offset_scale_handle_);

  // Set up corner position attribute.
  glVertexAttribPointer(corner_position_handle_, kCornerPositionDataSize,
                        GL_FLOAT, false, kDataStride,
                        VOID_OFFSET(kCornerPositionDataOffset));
  glEnableVertexAttribArray(corner_position_handle_);

  glUniform1i(uses_overlay_handle_, false);

  // TODO(bajones): This should eventually be changed to use instancing so that
  // the entire queue can be processed in one draw call. For now this still
  // significantly reduces the amount of state changes made per draw.
  while (!quad_queue_.empty()) {
    const QuadData& quad = quad_queue_.front();

    if (last_blend != quad.blend) {
      last_blend = quad.blend;
      if (quad.blend) {
        glEnable(GL_BLEND);
        glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
      } else {
        glDisable(GL_BLEND);
      }
    }

    // Only change texture ID or opacity when they differ between quads.
    if (last_texture != quad.texture_data_handle) {
      last_texture = quad.texture_data_handle;
      glActiveTexture(GL_TEXTURE0);
      glBindTexture(TextureType(), last_texture);
      SetTexParameters(TextureType());
    }

    if (last_overlay_texture != quad.overlay_texture_data_handle) {
      last_overlay_texture = quad.overlay_texture_data_handle;
      glUniform1i(uses_overlay_handle_, quad.overlay_texture_data_handle != 0);
      glActiveTexture(GL_TEXTURE1);
      glBindTexture(TextureType(), last_overlay_texture);
      SetTexParameters(TextureType());
      glUniform1f(overlay_opacity_handle_, last_overlay_texture ? 1.0f : 0.0f);
    }

    if (last_opacity != quad.opacity) {
      last_opacity = quad.opacity;
      glUniform1f(opacity_handle_, last_opacity);
    }

    bool corner_attributes_changed = quad.corner_radius != last_corner_radius ||
                                     quad.element_size != last_element_size;

    if (corner_attributes_changed) {
      last_corner_radius = quad.corner_radius;
      last_element_size = quad.element_size;
      if (quad.corner_radius == 0.0f) {
        glUniform2f(corner_offset_handle_, 0.0, 0.0);
      } else {
        glUniform2f(corner_offset_handle_,
                    quad.corner_radius / quad.element_size.width(),
                    quad.corner_radius / quad.element_size.height());
      }
    }

    // Pass in model view project matrix.
    glUniformMatrix4fv(model_view_proj_matrix_handle_, 1, false,
                       MatrixToGLArray(quad.model_view_proj_matrix).data());

    if (last_clip_rect != quad.clip_rect) {
      last_clip_rect = quad.clip_rect;
      const GLfloat clip_rect_data[4] = {quad.clip_rect.x(), quad.clip_rect.y(),
                                         quad.clip_rect.right(),
                                         quad.clip_rect.bottom()};
      glUniform2fv(clip_rect_handle_, 2, clip_rect_data);
    }

    if (quad.corner_radius == 0.0f) {
      glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT,
                     VOID_OFFSET(kInnerRectOffset));
    } else {
      glDrawElements(GL_TRIANGLES, std::size(kIndices), GL_UNSIGNED_SHORT, 0);
    }

    quad_queue_.pop();
  }

  if (!last_blend) {
    glEnable(GL_BLEND);
    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  }

  glDisableVertexAttribArray(position_handle_);
  glDisableVertexAttribArray(offset_scale_handle_);
  glDisableVertexAttribArray(corner_position_handle_);
}

GLuint TexturedQuadRenderer::vertex_buffer_ = 0;
GLuint TexturedQuadRenderer::index_buffer_ = 0;

void TexturedQuadRenderer::CreateBuffers() {
  GLuint buffers[2];
  glGenBuffers(2, buffers);
  vertex_buffer_ = buffers[0];
  index_buffer_ = buffers[1];

  glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_);
  glBufferData(GL_ARRAY_BUFFER, std::size(kVertices) * sizeof(float), kVertices,
               GL_STATIC_DRAW);

  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer_);
  glBufferData(GL_ELEMENT_ARRAY_BUFFER, std::size(kIndices) * sizeof(GLushort),
               kIndices, GL_STATIC_DRAW);
}

GLenum TexturedQuadRenderer::TextureType() const {
  return GL_TEXTURE_2D;
}

const char* TexturedQuadRenderer::VertexShader() {
  return kVertexShader;
}

GLuint TexturedQuadRenderer::VertexBuffer() {
  return vertex_buffer_;
}

GLuint TexturedQuadRenderer::IndexBuffer() {
  return index_buffer_;
}

int TexturedQuadRenderer::NumQuadIndices() {
  return std::size(kIndices);
}

}  // namespace vr