OLD | NEW |
---|---|
1 // Copyright 2011 The Chromium Authors. All rights reserved. | 1 // Copyright 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "cc/output/geometry_binding.h" | 5 #include "cc/output/geometry_binding.h" |
6 | |
7 #include "cc/output/gl_renderer.h" // For the GLC() macro. | 6 #include "cc/output/gl_renderer.h" // For the GLC() macro. |
8 #include "gpu/command_buffer/client/gles2_interface.h" | 7 #include "gpu/command_buffer/client/gles2_interface.h" |
9 #include "ui/gfx/geometry/rect_f.h" | 8 #include "ui/gfx/geometry/rect_f.h" |
10 | 9 |
11 namespace cc { | 10 namespace cc { |
12 | 11 |
13 GeometryBinding::GeometryBinding(gpu::gles2::GLES2Interface* gl, | 12 GeometryBinding::GeometryBinding(gpu::gles2::GLES2Interface* gl, |
14 const gfx::RectF& quad_vertex_rect) | 13 const gfx::RectF& quad_vertex_rect, |
15 : gl_(gl), quad_vertices_vbo_(0), quad_elements_vbo_(0) { | 14 size_t buffer_size) |
16 struct Vertex { | 15 : gl_(gl), |
17 float a_position[3]; | 16 quad_vertices_vbo_(0), |
18 float a_texCoord[2]; | 17 quad_elements_vbo_(0), |
19 // Index of the vertex, divide by 4 to have the matrix for this quad. | 18 quad_vertex_rect_(quad_vertex_rect), |
20 float a_index; | 19 initialized_(false) { |
21 }; | 20 Quad quads[buffer_size]; |
22 struct Quad { | 21 QuadIndex quad_indexes[buffer_size]; |
23 Vertex v0, v1, v2, v3; | |
24 }; | |
25 struct QuadIndex { | |
26 uint16 data[6]; | |
27 }; | |
28 | 22 |
29 COMPILE_ASSERT(sizeof(Quad) == 24 * sizeof(float), // NOLINT(runtime/sizeof) | 23 COMPILE_ASSERT(sizeof(Quad) == 24 * sizeof(float), // NOLINT(runtime/sizeof) |
30 struct_is_densely_packed); | 24 struct_is_densely_packed); |
31 COMPILE_ASSERT( | 25 COMPILE_ASSERT( |
32 sizeof(QuadIndex) == 6 * sizeof(uint16_t), // NOLINT(runtime/sizeof) | 26 sizeof(QuadIndex) == 6 * sizeof(uint16_t), // NOLINT(runtime/sizeof) |
33 struct_is_densely_packed); | 27 struct_is_densely_packed); |
34 | 28 |
35 Quad quad_list[8]; | 29 // XXX: Should check for OUT_OF_MEMORY here |
36 QuadIndex quad_index_list[8]; | 30 GLC(gl_, gl_->GenBuffers(1, &quad_vertices_vbo_)); |
37 for (int i = 0; i < 8; i++) { | 31 GLC(gl_, gl_->GenBuffers(1, &quad_elements_vbo_)); |
38 Vertex v0 = {{quad_vertex_rect.x(), quad_vertex_rect.bottom(), 0.0f, }, | 32 |
39 {0.0f, 1.0f, }, i * 4.0f + 0.0f}; | 33 GLC(gl_, gl_->BindBuffer(GL_ARRAY_BUFFER, quad_vertices_vbo_)); |
40 Vertex v1 = {{quad_vertex_rect.x(), quad_vertex_rect.y(), 0.0f, }, | 34 GLC(gl_, |
41 {0.0f, 0.0f, }, i * 4.0f + 1.0f}; | 35 gl_->BufferData(GL_ARRAY_BUFFER, sizeof(quads), quads, GL_DYNAMIC_DRAW)); |
42 Vertex v2 = {{quad_vertex_rect.right(), quad_vertex_rect.y(), 0.0f, }, | 36 |
43 {1.0f, .0f, }, i * 4.0f + 2.0f}; | 37 GLC(gl_, gl_->BindBuffer(GL_ELEMENT_ARRAY_BUFFER, quad_elements_vbo_)); |
44 Vertex v3 = {{quad_vertex_rect.right(), quad_vertex_rect.bottom(), 0.0f, }, | 38 GLC(gl_, |
45 {1.0f, 1.0f, }, i * 4.0f + 3.0f}; | 39 gl_->BufferData(GL_ELEMENT_ARRAY_BUFFER, |
40 sizeof(quad_indexes), | |
41 quad_indexes, | |
42 GL_DYNAMIC_DRAW)); | |
43 | |
44 InitializeBaseQuads(buffer_size); | |
45 } | |
46 | |
47 void GeometryBinding::InitializeBaseQuads(size_t buffer_size) { | |
48 Quad quad_list[buffer_size]; | |
49 QuadIndex quad_index_list[buffer_size]; | |
50 for (size_t i = 0; i < buffer_size; i++) { | |
51 Vertex v0 = {{quad_vertex_rect_.x(), quad_vertex_rect_.bottom(), 0.0f}, | |
52 {0.0f, 1.0f}, | |
53 i * 4.0f + 0.0f}; | |
54 Vertex v1 = {{quad_vertex_rect_.x(), quad_vertex_rect_.y(), 0.0f}, | |
55 {0.0f, 0.0f}, | |
56 i * 4.0f + 1.0f}; | |
57 Vertex v2 = {{quad_vertex_rect_.right(), quad_vertex_rect_.y(), 0.0f}, | |
58 {1.0f, 0.0f}, | |
59 i * 4.0f + 2.0f}; | |
60 Vertex v3 = {{quad_vertex_rect_.right(), quad_vertex_rect_.bottom(), 0.0f}, | |
61 {1.0f, 1.0f}, | |
62 i * 4.0f + 3.0f}; | |
46 Quad x = {v0, v1, v2, v3}; | 63 Quad x = {v0, v1, v2, v3}; |
47 quad_list[i] = x; | 64 quad_list[i] = x; |
48 QuadIndex y = { | 65 QuadIndex y = { |
49 {static_cast<uint16>(0 + 4 * i), static_cast<uint16>(1 + 4 * i), | 66 {static_cast<uint16>(0 + 4 * i), static_cast<uint16>(1 + 4 * i), |
50 static_cast<uint16>(2 + 4 * i), static_cast<uint16>(3 + 4 * i), | 67 static_cast<uint16>(2 + 4 * i), static_cast<uint16>(3 + 4 * i), |
51 static_cast<uint16>(0 + 4 * i), static_cast<uint16>(2 + 4 * i)}}; | 68 static_cast<uint16>(0 + 4 * i), static_cast<uint16>(2 + 4 * i)}}; |
52 quad_index_list[i] = y; | 69 quad_index_list[i] = y; |
53 } | 70 } |
54 | 71 SetBuffers<Quad, QuadIndex>( |
55 gl_->GenBuffers(1, &quad_vertices_vbo_); | 72 quad_list, quad_index_list, 0, 0, buffer_size, buffer_size); |
56 gl_->GenBuffers(1, &quad_elements_vbo_); | |
57 GLC(gl_, gl_->BindBuffer(GL_ARRAY_BUFFER, quad_vertices_vbo_)); | |
58 GLC(gl_, | |
59 gl_->BufferData( | |
60 GL_ARRAY_BUFFER, sizeof(quad_list), quad_list, GL_STATIC_DRAW)); | |
61 GLC(gl_, gl_->BindBuffer(GL_ELEMENT_ARRAY_BUFFER, quad_elements_vbo_)); | |
62 GLC(gl_, | |
63 gl_->BufferData(GL_ELEMENT_ARRAY_BUFFER, | |
64 sizeof(quad_index_list), | |
65 quad_index_list, | |
66 GL_STATIC_DRAW)); | |
67 } | 73 } |
68 | 74 |
69 GeometryBinding::~GeometryBinding() { | 75 GeometryBinding::~GeometryBinding() { |
70 gl_->DeleteBuffers(1, &quad_vertices_vbo_); | 76 gl_->DeleteBuffers(1, &quad_vertices_vbo_); |
71 gl_->DeleteBuffers(1, &quad_elements_vbo_); | 77 gl_->DeleteBuffers(1, &quad_elements_vbo_); |
72 } | 78 } |
73 | 79 |
74 void GeometryBinding::PrepareForDraw() { | 80 void GeometryBinding::PrepareForDraw() { |
75 GLC(gl_, gl_->BindBuffer(GL_ELEMENT_ARRAY_BUFFER, quad_elements_vbo_)); | 81 GLC(gl_, gl_->BindBuffer(GL_ELEMENT_ARRAY_BUFFER, quad_elements_vbo_)); |
76 | 82 |
(...skipping 27 matching lines...) Expand all Loading... | |
104 1, | 110 1, |
105 GL_FLOAT, | 111 GL_FLOAT, |
106 false, | 112 false, |
107 6 * sizeof(float), // NOLINT(runtime/sizeof) | 113 6 * sizeof(float), // NOLINT(runtime/sizeof) |
108 offsets[2])); | 114 offsets[2])); |
109 GLC(gl_, gl_->EnableVertexAttribArray(PositionAttribLocation())); | 115 GLC(gl_, gl_->EnableVertexAttribArray(PositionAttribLocation())); |
110 GLC(gl_, gl_->EnableVertexAttribArray(TexCoordAttribLocation())); | 116 GLC(gl_, gl_->EnableVertexAttribArray(TexCoordAttribLocation())); |
111 GLC(gl_, gl_->EnableVertexAttribArray(TriangleIndexAttribLocation())); | 117 GLC(gl_, gl_->EnableVertexAttribArray(TriangleIndexAttribLocation())); |
112 } | 118 } |
113 | 119 |
120 GeometryBindingQuad::GeometryBindingQuad(gpu::gles2::GLES2Interface* gl, | |
121 const gfx::RectF& quad_vertex_rect) | |
122 : GeometryBinding(gl, quad_vertex_rect, 1) { | |
123 } | |
124 | |
125 void GeometryBindingQuad::InitializeCustomQuad(const gfx::QuadF& quad) { | |
enne (OOO)
2014/09/24 17:35:30
Can the GeometryBinding stuff become two classes,
| |
126 float uv[] = {0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f}; | |
127 InitializeCustomQuadWithUVs(quad, uv); | |
128 } | |
129 | |
130 void GeometryBindingQuad::InitializeCustomQuadWithUVs(const gfx::QuadF& quad, | |
131 float uv[8]) { | |
132 Vertex v0 = {{quad.p1().x(), quad.p1().y(), 0.0f}, {uv[0], uv[1]}, 0.0f}; | |
133 Vertex v1 = {{quad.p2().x(), quad.p2().y(), 0.0f}, {uv[2], uv[3]}, 1.0f}; | |
134 Vertex v2 = {{quad.p3().x(), quad.p3().y(), 0.0f}, {uv[4], uv[5]}, 2.0f}; | |
135 Vertex v3 = {{quad.p4().x(), quad.p4().y(), 0.0f}, {uv[6], uv[7]}, 3.0f}; | |
136 | |
137 Quad local_quad = {v0, v1, v2, v3}; | |
138 QuadIndex quad_index = {{static_cast<uint16>(0), static_cast<uint16>(1), | |
139 static_cast<uint16>(2), static_cast<uint16>(3), | |
140 static_cast<uint16>(0), static_cast<uint16>(2)}}; | |
141 | |
142 SetBuffers<Quad, QuadIndex>(&local_quad, &quad_index, 0, 0, 1, 1); | |
143 } | |
144 | |
114 } // namespace cc | 145 } // namespace cc |
OLD | NEW |