blob: 11a49591cdaf69857f102a2d60de7f7cf0e983c1 [file] [log] [blame]
gman@chromium.orge259eb412012-10-13 05:47:241// Copyright (c) 2012 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.
4
5#include "gpu/command_buffer/service/context_state.h"
6
gman@chromium.orgf731b9462012-10-30 00:35:227#include "gpu/command_buffer/common/gles2_cmd_utils.h"
gman@chromium.org1868a342012-11-07 15:56:028#include "ui/gl/gl_bindings.h"
9#include "ui/gl/gl_implementation.h"
gman@chromium.orgf731b9462012-10-30 00:35:2210
gman@chromium.orge259eb412012-10-13 05:47:2411namespace gpu {
12namespace gles2 {
13
gman@chromium.orgf731b9462012-10-30 00:35:2214namespace {
15
16void EnableDisable(GLenum pname, bool enable) {
17 if (enable) {
18 glEnable(pname);
19 } else {
20 glDisable(pname);
21 }
22}
23
24} // anonymous namespace.
25
gman@chromium.orge259eb412012-10-13 05:47:2426TextureUnit::TextureUnit()
27 : bind_target(GL_TEXTURE_2D) {
28}
29
30TextureUnit::~TextureUnit() {
31}
32
sievers@chromium.orgb3cbad12012-12-05 19:56:3633ContextState::ContextState(FeatureInfo* feature_info)
gman@chromium.orge259eb412012-10-13 05:47:2434 : pack_alignment(4),
35 unpack_alignment(4),
36 active_texture_unit(0),
gman@chromium.org88a61bf2012-10-27 13:00:4237 hint_generate_mipmap(GL_DONT_CARE),
38 hint_fragment_shader_derivative(GL_DONT_CARE),
sievers@chromium.orgb3cbad12012-12-05 19:56:3639 pack_reverse_row_order(false),
40 feature_info_(feature_info) {
gman@chromium.orgf731b9462012-10-30 00:35:2241 Initialize();
gman@chromium.orge259eb412012-10-13 05:47:2442}
43
44ContextState::~ContextState() {
45}
46
gman@chromium.org29a4d902013-02-26 20:18:0647void ContextState::RestoreTextureUnitBindings(GLuint unit) const {
48 DCHECK_LT(unit, texture_units.size());
49 const TextureUnit& texture_unit = texture_units[unit];
50 glActiveTexture(GL_TEXTURE0 + unit);
51 GLuint service_id = texture_unit.bound_texture_2d ?
52 texture_unit.bound_texture_2d->service_id() : 0;
53 glBindTexture(GL_TEXTURE_2D, service_id);
54 service_id = texture_unit.bound_texture_cube_map ?
55 texture_unit.bound_texture_cube_map->service_id() : 0;
56 glBindTexture(GL_TEXTURE_CUBE_MAP, service_id);
57
58 if (feature_info_->feature_flags().oes_egl_image_external) {
59 service_id = texture_unit.bound_texture_external_oes ?
60 texture_unit.bound_texture_external_oes->service_id() : 0;
61 glBindTexture(GL_TEXTURE_EXTERNAL_OES, service_id);
62 }
63
64 if (feature_info_->feature_flags().arb_texture_rectangle) {
65 service_id = texture_unit.bound_texture_rectangle_arb ?
66 texture_unit.bound_texture_rectangle_arb->service_id() : 0;
67 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, service_id);
68 }
69}
70
71void ContextState::RestoreBufferBindings() const {
72 if (vertex_attrib_manager) {
gman@chromium.org16ccec12013-02-28 03:40:2173 Buffer* element_array_buffer =
gman@chromium.org29a4d902013-02-26 20:18:0674 vertex_attrib_manager->element_array_buffer();
75 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,
76 element_array_buffer ? element_array_buffer->service_id() : 0);
77 }
78 glBindBuffer(
79 GL_ARRAY_BUFFER,
80 bound_array_buffer ? bound_array_buffer->service_id() : 0);
81}
82
83void ContextState::RestoreRenderbufferBindings() const {
84 // Restore Bindings
85 glBindRenderbufferEXT(
86 GL_RENDERBUFFER,
87 bound_renderbuffer ? bound_renderbuffer->service_id() : 0);
88}
89
90void ContextState::RestoreProgramBindings() const {
91 glUseProgram(current_program ? current_program->service_id() : 0);
92}
93
94void ContextState::RestoreActiveTexture() const {
95 glActiveTexture(GL_TEXTURE0 + active_texture_unit);
96}
97
98void ContextState::RestoreAttribute(GLuint attrib) const {
gman@chromium.orged9f9cd2013-02-27 21:12:3599 const VertexAttrib* info =
100 vertex_attrib_manager->GetVertexAttrib(attrib);
gman@chromium.org29a4d902013-02-26 20:18:06101 const void* ptr = reinterpret_cast<const void*>(info->offset());
gman@chromium.org16ccec12013-02-28 03:40:21102 Buffer* buffer_info = info->buffer();
gman@chromium.org29a4d902013-02-26 20:18:06103 glBindBuffer(
104 GL_ARRAY_BUFFER, buffer_info ? buffer_info->service_id() : 0);
105 glVertexAttribPointer(
106 attrib, info->size(), info->type(), info->normalized(),
107 info->gl_stride(), ptr);
108 if (info->divisor())
109 glVertexAttribDivisorANGLE(attrib, info->divisor());
110 // Never touch vertex attribute 0's state (in particular, never
111 // disable it) when running on desktop GL because it will never be
112 // re-enabled.
113 if (attrib != 0 ||
114 gfx::GetGLImplementation() == gfx::kGLImplementationEGLGLES2) {
115 if (info->enabled()) {
116 glEnableVertexAttribArray(attrib);
117 } else {
118 glDisableVertexAttribArray(attrib);
119 }
120 }
121 glVertexAttrib4fv(attrib, attrib_values[attrib].v);
122}
123
124void ContextState::RestoreGlobalState() const {
gman@chromium.org1868a342012-11-07 15:56:02125 InitCapabilities();
126 InitState();
127
128 glPixelStorei(GL_PACK_ALIGNMENT, pack_alignment);
129 glPixelStorei(GL_UNPACK_ALIGNMENT, unpack_alignment);
130
131 glHint(GL_GENERATE_MIPMAP_HINT, hint_generate_mipmap);
132 // TODO: If OES_standard_derivatives is available
133 // restore GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES
gman@chromium.org29a4d902013-02-26 20:18:06134}
gman@chromium.org1868a342012-11-07 15:56:02135
gman@chromium.org29a4d902013-02-26 20:18:06136void ContextState::RestoreState() const {
gman@chromium.org1868a342012-11-07 15:56:02137 // Restore Texture state.
138 for (size_t ii = 0; ii < texture_units.size(); ++ii) {
gman@chromium.org29a4d902013-02-26 20:18:06139 RestoreTextureUnitBindings(ii);
gman@chromium.org1868a342012-11-07 15:56:02140 }
gman@chromium.org29a4d902013-02-26 20:18:06141 RestoreActiveTexture();
gman@chromium.org1868a342012-11-07 15:56:02142
143 // Restore Attrib State
144 // TODO: This if should not be needed. RestoreState is getting called
145 // before GLES2Decoder::Initialize which is a bug.
146 if (vertex_attrib_manager) {
gman@chromium.orgaf6380962012-11-29 23:24:13147 // TODO(gman): Move this restoration to VertexAttribManager.
gman@chromium.org1868a342012-11-07 15:56:02148 for (size_t attrib = 0; attrib < vertex_attrib_manager->num_attribs();
149 ++attrib) {
gman@chromium.org29a4d902013-02-26 20:18:06150 RestoreAttribute(attrib);
gman@chromium.org1868a342012-11-07 15:56:02151 }
gman@chromium.org1868a342012-11-07 15:56:02152 }
153
gman@chromium.org15cc23fa2013-02-26 21:56:25154 RestoreBufferBindings();
gman@chromium.org29a4d902013-02-26 20:18:06155 RestoreRenderbufferBindings();
156 RestoreProgramBindings();
gman@chromium.org15cc23fa2013-02-26 21:56:25157 RestoreGlobalState();
gman@chromium.org1868a342012-11-07 15:56:02158}
159
gman@chromium.orgf731b9462012-10-30 00:35:22160// Include the auto-generated part of this file. We split this because it means
161// we can easily edit the non-auto generated parts right here in this file
162// instead of having to edit some template or the code generator.
163#include "gpu/command_buffer/service/context_state_impl_autogen.h"
164
gman@chromium.orge259eb412012-10-13 05:47:24165} // namespace gles2
166} // namespace gpu
167
168