1 // Copyright 2015 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/dynamic_geometry_binding.h"
7 #include "gpu/command_buffer/client/gles2_interface.h"
8 #include "ui/gfx/geometry/quad_f.h"
9 #include "ui/gfx/geometry/rect_f.h"
13 DynamicGeometryBinding::DynamicGeometryBinding(gpu::gles2::GLES2Interface
* gl
)
14 : gl_(gl
), quad_vertices_vbo_(0), quad_elements_vbo_(0) {
15 GeometryBindingQuad quads
[1];
16 GeometryBindingQuadIndex quad_indices
[1];
18 static_assert(sizeof(GeometryBindingQuad
) == 24 * sizeof(float),
19 "struct Quad should be densely packed");
20 static_assert(sizeof(GeometryBindingQuadIndex
) == 6 * sizeof(uint16_t),
21 "struct QuadIndex should be densely packed");
23 gl_
->GenBuffers(1, &quad_vertices_vbo_
);
24 gl_
->GenBuffers(1, &quad_elements_vbo_
);
26 gl_
->BindBuffer(GL_ARRAY_BUFFER
, quad_vertices_vbo_
);
27 gl_
->BufferData(GL_ARRAY_BUFFER
, sizeof(GeometryBindingQuad
) * 1, quads
,
30 gl_
->BindBuffer(GL_ELEMENT_ARRAY_BUFFER
, quad_elements_vbo_
);
31 gl_
->BufferData(GL_ELEMENT_ARRAY_BUFFER
, sizeof(GeometryBindingQuadIndex
) * 1,
32 &quad_indices
, GL_DYNAMIC_DRAW
);
35 void DynamicGeometryBinding::InitializeCustomQuad(const gfx::QuadF
& quad
) {
36 float uv
[] = {0.0f
, 0.0f
, 1.0f
, 0.0f
, 1.0f
, 1.0f
, 0.0f
, 1.0f
};
37 InitializeCustomQuadWithUVs(quad
, uv
);
40 void DynamicGeometryBinding::InitializeCustomQuadWithUVs(const gfx::QuadF
& quad
,
42 GeometryBindingVertex v0
= {
43 {quad
.p1().x(), quad
.p1().y(), 0.0f
}, {uv
[0], uv
[1]}, 0.0f
};
44 GeometryBindingVertex v1
= {
45 {quad
.p2().x(), quad
.p2().y(), 0.0f
}, {uv
[2], uv
[3]}, 1.0f
};
46 GeometryBindingVertex v2
= {
47 {quad
.p3().x(), quad
.p3().y(), 0.0f
}, {uv
[4], uv
[5]}, 2.0f
};
48 GeometryBindingVertex v3
= {
49 {quad
.p4().x(), quad
.p4().y(), 0.0f
}, {uv
[6], uv
[7]}, 3.0f
};
51 GeometryBindingQuad local_quad
= {v0
, v1
, v2
, v3
};
52 GeometryBindingQuadIndex
quad_index(
53 static_cast<uint16
>(0), static_cast<uint16
>(1), static_cast<uint16
>(2),
54 static_cast<uint16
>(3), static_cast<uint16
>(0), static_cast<uint16
>(2));
56 gl_
->BufferSubData(GL_ARRAY_BUFFER
, 0, sizeof(GeometryBindingQuad
),
58 gl_
->BufferSubData(GL_ELEMENT_ARRAY_BUFFER
, 0,
59 sizeof(GeometryBindingQuadIndex
), &quad_index
);
62 void DynamicGeometryBinding::PrepareForDraw() {
63 SetupGLContext(gl_
, quad_elements_vbo_
, quad_vertices_vbo_
);