summaryrefslogtreecommitdiffstats
path: root/cc/output/static_geometry_binding.cc
blob: df540671e9829c389af15c1879d6e63a038248e0 (plain)
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
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "cc/output/static_geometry_binding.h"

#include <stddef.h>
#include <stdint.h>

#include "gpu/command_buffer/client/gles2_interface.h"
#include "ui/gfx/geometry/rect_f.h"

namespace cc {

StaticGeometryBinding::StaticGeometryBinding(gpu::gles2::GLES2Interface* gl,
                                             const gfx::RectF& quad_vertex_rect)
    : gl_(gl), quad_vertices_vbo_(0), quad_elements_vbo_(0) {
  GeometryBindingQuad quads[NUM_QUADS];
  GeometryBindingQuadIndex quad_indices[NUM_QUADS];

  static_assert(sizeof(GeometryBindingQuad) == 24 * sizeof(float),
                "struct Quad should be densely packed");
  static_assert(sizeof(GeometryBindingQuadIndex) == 6 * sizeof(uint16_t),
                "struct QuadIndex should be densely packed");

  for (size_t i = 0; i < NUM_QUADS; i++) {
    GeometryBindingVertex v0 = {
        {quad_vertex_rect.x(), quad_vertex_rect.bottom(), 0.0f},
        {0.0f, 1.0f},
        i * 4.0f + 0.0f};
    GeometryBindingVertex v1 = {
        {quad_vertex_rect.x(), quad_vertex_rect.y(), 0.0f},
        {0.0f, 0.0f},
        i * 4.0f + 1.0f};
    GeometryBindingVertex v2 = {
        {quad_vertex_rect.right(), quad_vertex_rect.y(), 0.0f},
        {1.0f, 0.0f},
        i * 4.0f + 2.0f};
    GeometryBindingVertex v3 = {
        {quad_vertex_rect.right(), quad_vertex_rect.bottom(), 0.0f},
        {1.0f, 1.0f},
        i * 4.0f + 3.0f};
    GeometryBindingQuad x(v0, v1, v2, v3);
    quads[i] = x;
    GeometryBindingQuadIndex y(
        static_cast<uint16_t>(0 + 4 * i), static_cast<uint16_t>(1 + 4 * i),
        static_cast<uint16_t>(2 + 4 * i), static_cast<uint16_t>(3 + 4 * i),
        static_cast<uint16_t>(0 + 4 * i), static_cast<uint16_t>(2 + 4 * i));
    quad_indices[i] = y;
  }

  gl_->GenBuffers(1, &quad_vertices_vbo_);
  gl_->GenBuffers(1, &quad_elements_vbo_);

  gl_->BindBuffer(GL_ARRAY_BUFFER, quad_vertices_vbo_);
  gl_->BufferData(GL_ARRAY_BUFFER, sizeof(GeometryBindingQuad) * NUM_QUADS,
                  quads, GL_STATIC_DRAW);

  gl_->BindBuffer(GL_ELEMENT_ARRAY_BUFFER, quad_elements_vbo_);
  gl_->BufferData(GL_ELEMENT_ARRAY_BUFFER,
                  sizeof(GeometryBindingQuadIndex) * NUM_QUADS, &quad_indices,
                  GL_STATIC_DRAW);
}

StaticGeometryBinding::~StaticGeometryBinding() {
  gl_->DeleteBuffers(1, &quad_vertices_vbo_);
  gl_->DeleteBuffers(1, &quad_elements_vbo_);
}

void StaticGeometryBinding::PrepareForDraw() {
  SetupGLContext(gl_, quad_elements_vbo_, quad_vertices_vbo_);
}

}  // namespace cc