Explicitly add python-numpy dependency to install-build-deps.
[chromium-blink-merge.git] / cc / output / geometry_binding.cc
blob054a96293f12629eb3f014250ee754ae04df1edc
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
3 // found in the LICENSE file.
5 #include "cc/output/geometry_binding.h"
7 #include "cc/output/gl_renderer.h" // For the GLC() macro.
8 #include "gpu/command_buffer/client/gles2_interface.h"
9 #include "ui/gfx/geometry/rect_f.h"
11 namespace cc {
13 GeometryBinding::GeometryBinding(gpu::gles2::GLES2Interface* gl,
14 const gfx::RectF& quad_vertex_rect)
15 : gl_(gl), quad_vertices_vbo_(0), quad_elements_vbo_(0) {
16 struct Vertex {
17 float a_position[3];
18 float a_texCoord[2];
19 // Index of the vertex, divide by 4 to have the matrix for this quad.
20 float a_index;
22 struct Quad {
23 Vertex v0, v1, v2, v3;
25 struct QuadIndex {
26 uint16 data[6];
29 COMPILE_ASSERT(sizeof(Quad) == 24 * sizeof(float), struct_is_densely_packed);
30 COMPILE_ASSERT(sizeof(QuadIndex) == 6 * sizeof(uint16_t),
31 struct_is_densely_packed);
33 Quad quad_list[8];
34 QuadIndex quad_index_list[8];
35 for (int i = 0; i < 8; i++) {
36 Vertex v0 = {{quad_vertex_rect.x(), quad_vertex_rect.bottom(), 0.0f, },
37 {0.0f, 1.0f, }, i * 4.0f + 0.0f};
38 Vertex v1 = {{quad_vertex_rect.x(), quad_vertex_rect.y(), 0.0f, },
39 {0.0f, 0.0f, }, i * 4.0f + 1.0f};
40 Vertex v2 = {{quad_vertex_rect.right(), quad_vertex_rect.y(), 0.0f, },
41 {1.0f, .0f, }, i * 4.0f + 2.0f};
42 Vertex v3 = {{quad_vertex_rect.right(), quad_vertex_rect.bottom(), 0.0f, },
43 {1.0f, 1.0f, }, i * 4.0f + 3.0f};
44 Quad x = {v0, v1, v2, v3};
45 quad_list[i] = x;
46 QuadIndex y = {
47 {static_cast<uint16>(0 + 4 * i), static_cast<uint16>(1 + 4 * i),
48 static_cast<uint16>(2 + 4 * i), static_cast<uint16>(3 + 4 * i),
49 static_cast<uint16>(0 + 4 * i), static_cast<uint16>(2 + 4 * i)}};
50 quad_index_list[i] = y;
53 gl_->GenBuffers(1, &quad_vertices_vbo_);
54 gl_->GenBuffers(1, &quad_elements_vbo_);
55 GLC(gl_, gl_->BindBuffer(GL_ARRAY_BUFFER, quad_vertices_vbo_));
56 GLC(gl_,
57 gl_->BufferData(
58 GL_ARRAY_BUFFER, sizeof(quad_list), quad_list, GL_STATIC_DRAW));
59 GLC(gl_, gl_->BindBuffer(GL_ELEMENT_ARRAY_BUFFER, quad_elements_vbo_));
60 GLC(gl_,
61 gl_->BufferData(GL_ELEMENT_ARRAY_BUFFER,
62 sizeof(quad_index_list),
63 quad_index_list,
64 GL_STATIC_DRAW));
67 GeometryBinding::~GeometryBinding() {
68 gl_->DeleteBuffers(1, &quad_vertices_vbo_);
69 gl_->DeleteBuffers(1, &quad_elements_vbo_);
72 void GeometryBinding::PrepareForDraw() {
73 GLC(gl_, gl_->BindBuffer(GL_ELEMENT_ARRAY_BUFFER, quad_elements_vbo_));
75 GLC(gl_, gl_->BindBuffer(GL_ARRAY_BUFFER, quad_vertices_vbo_));
76 // OpenGL defines the last parameter to VertexAttribPointer as type
77 // "const GLvoid*" even though it is actually an offset into the buffer
78 // object's data store and not a pointer to the client's address space.
79 const void* offsets[3] = {
81 reinterpret_cast<const void*>(3 * sizeof(float)),
82 reinterpret_cast<const void*>(5 * sizeof(float)),
85 GLC(gl_, gl_->VertexAttribPointer(PositionAttribLocation(), 3, GL_FLOAT,
86 false, 6 * sizeof(float), offsets[0]));
87 GLC(gl_, gl_->VertexAttribPointer(TexCoordAttribLocation(), 2, GL_FLOAT,
88 false, 6 * sizeof(float), offsets[1]));
89 GLC(gl_, gl_->VertexAttribPointer(TriangleIndexAttribLocation(), 1, GL_FLOAT,
90 false, 6 * sizeof(float), offsets[2]));
91 GLC(gl_, gl_->EnableVertexAttribArray(PositionAttribLocation()));
92 GLC(gl_, gl_->EnableVertexAttribArray(TexCoordAttribLocation()));
93 GLC(gl_, gl_->EnableVertexAttribArray(TriangleIndexAttribLocation()));
96 } // namespace cc