blob: 069d136469c6cea7d0641db42a8befc2433f27ce [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.org31494b82013-02-28 10:10:268#include "gpu/command_buffer/service/buffer_manager.h"
kloveless@chromium.orgd3eba342013-04-18 21:11:509#include "gpu/command_buffer/service/error_state.h"
gman@chromium.org31494b82013-02-28 10:10:2610#include "gpu/command_buffer/service/framebuffer_manager.h"
11#include "gpu/command_buffer/service/program_manager.h"
12#include "gpu/command_buffer/service/renderbuffer_manager.h"
gman@chromium.org1868a342012-11-07 15:56:0213#include "ui/gl/gl_bindings.h"
14#include "ui/gl/gl_implementation.h"
gman@chromium.orgf731b9462012-10-30 00:35:2215
gman@chromium.orge259eb412012-10-13 05:47:2416namespace gpu {
17namespace gles2 {
18
gman@chromium.orgf731b9462012-10-30 00:35:2219namespace {
20
21void EnableDisable(GLenum pname, bool enable) {
22 if (enable) {
23 glEnable(pname);
24 } else {
25 glDisable(pname);
26 }
27}
28
29} // anonymous namespace.
30
gman@chromium.orge259eb412012-10-13 05:47:2431TextureUnit::TextureUnit()
32 : bind_target(GL_TEXTURE_2D) {
33}
34
35TextureUnit::~TextureUnit() {
36}
37
kloveless@chromium.orgd3eba342013-04-18 21:11:5038ContextState::ContextState(FeatureInfo* feature_info, Logger* logger)
gman@chromium.orge259eb412012-10-13 05:47:2439 : pack_alignment(4),
40 unpack_alignment(4),
41 active_texture_unit(0),
gman@chromium.org88a61bf2012-10-27 13:00:4242 hint_generate_mipmap(GL_DONT_CARE),
43 hint_fragment_shader_derivative(GL_DONT_CARE),
sievers@chromium.orgb3cbad12012-12-05 19:56:3644 pack_reverse_row_order(false),
sievers@chromium.org28718a92013-04-04 12:12:5145 fbo_binding_for_scissor_workaround_dirty_(false),
kloveless@chromium.orgd3eba342013-04-18 21:11:5046 feature_info_(feature_info),
47 error_state_(ErrorState::Create(logger)) {
gman@chromium.orgf731b9462012-10-30 00:35:2248 Initialize();
gman@chromium.orge259eb412012-10-13 05:47:2449}
50
51ContextState::~ContextState() {
52}
53
gman@chromium.org29a4d902013-02-26 20:18:0654void ContextState::RestoreTextureUnitBindings(GLuint unit) const {
55 DCHECK_LT(unit, texture_units.size());
56 const TextureUnit& texture_unit = texture_units[unit];
57 glActiveTexture(GL_TEXTURE0 + unit);
58 GLuint service_id = texture_unit.bound_texture_2d ?
59 texture_unit.bound_texture_2d->service_id() : 0;
60 glBindTexture(GL_TEXTURE_2D, service_id);
61 service_id = texture_unit.bound_texture_cube_map ?
62 texture_unit.bound_texture_cube_map->service_id() : 0;
63 glBindTexture(GL_TEXTURE_CUBE_MAP, service_id);
64
65 if (feature_info_->feature_flags().oes_egl_image_external) {
66 service_id = texture_unit.bound_texture_external_oes ?
67 texture_unit.bound_texture_external_oes->service_id() : 0;
68 glBindTexture(GL_TEXTURE_EXTERNAL_OES, service_id);
69 }
70
71 if (feature_info_->feature_flags().arb_texture_rectangle) {
72 service_id = texture_unit.bound_texture_rectangle_arb ?
73 texture_unit.bound_texture_rectangle_arb->service_id() : 0;
74 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, service_id);
75 }
76}
77
78void ContextState::RestoreBufferBindings() const {
79 if (vertex_attrib_manager) {
gman@chromium.org16ccec12013-02-28 03:40:2180 Buffer* element_array_buffer =
gman@chromium.org29a4d902013-02-26 20:18:0681 vertex_attrib_manager->element_array_buffer();
82 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,
83 element_array_buffer ? element_array_buffer->service_id() : 0);
84 }
85 glBindBuffer(
86 GL_ARRAY_BUFFER,
87 bound_array_buffer ? bound_array_buffer->service_id() : 0);
88}
89
90void ContextState::RestoreRenderbufferBindings() const {
91 // Restore Bindings
92 glBindRenderbufferEXT(
93 GL_RENDERBUFFER,
94 bound_renderbuffer ? bound_renderbuffer->service_id() : 0);
95}
96
97void ContextState::RestoreProgramBindings() const {
98 glUseProgram(current_program ? current_program->service_id() : 0);
99}
100
101void ContextState::RestoreActiveTexture() const {
102 glActiveTexture(GL_TEXTURE0 + active_texture_unit);
103}
104
backer@chromium.org217004512013-05-10 21:25:55105void ContextState::RestoreAllTextureUnitBindings() const {
106 // Restore Texture state.
107 for (size_t ii = 0; ii < texture_units.size(); ++ii) {
108 RestoreTextureUnitBindings(ii);
109 }
110 RestoreActiveTexture();
111}
112
gman@chromium.orgac77603c72013-03-08 13:52:06113void ContextState::RestoreAttribute(GLuint attrib_index) const {
114 const VertexAttrib* attrib =
115 vertex_attrib_manager->GetVertexAttrib(attrib_index);
116 const void* ptr = reinterpret_cast<const void*>(attrib->offset());
117 Buffer* buffer = attrib->buffer();
gman@chromium.orgb10492f2013-03-08 05:24:07118 glBindBuffer(GL_ARRAY_BUFFER, buffer ? buffer->service_id() : 0);
gman@chromium.org29a4d902013-02-26 20:18:06119 glVertexAttribPointer(
gman@chromium.orgac77603c72013-03-08 13:52:06120 attrib_index, attrib->size(), attrib->type(), attrib->normalized(),
121 attrib->gl_stride(), ptr);
122 if (attrib->divisor())
123 glVertexAttribDivisorANGLE(attrib_index, attrib->divisor());
gman@chromium.org29a4d902013-02-26 20:18:06124 // Never touch vertex attribute 0's state (in particular, never
125 // disable it) when running on desktop GL because it will never be
126 // re-enabled.
gman@chromium.orgac77603c72013-03-08 13:52:06127 if (attrib_index != 0 ||
gman@chromium.org29a4d902013-02-26 20:18:06128 gfx::GetGLImplementation() == gfx::kGLImplementationEGLGLES2) {
gman@chromium.orgac77603c72013-03-08 13:52:06129 if (attrib->enabled()) {
130 glEnableVertexAttribArray(attrib_index);
gman@chromium.org29a4d902013-02-26 20:18:06131 } else {
gman@chromium.orgac77603c72013-03-08 13:52:06132 glDisableVertexAttribArray(attrib_index);
gman@chromium.org29a4d902013-02-26 20:18:06133 }
134 }
gman@chromium.orgac77603c72013-03-08 13:52:06135 glVertexAttrib4fv(attrib_index, attrib_values[attrib_index].v);
gman@chromium.org29a4d902013-02-26 20:18:06136}
137
138void ContextState::RestoreGlobalState() const {
gman@chromium.org1868a342012-11-07 15:56:02139 InitCapabilities();
140 InitState();
141
142 glPixelStorei(GL_PACK_ALIGNMENT, pack_alignment);
143 glPixelStorei(GL_UNPACK_ALIGNMENT, unpack_alignment);
144
145 glHint(GL_GENERATE_MIPMAP_HINT, hint_generate_mipmap);
146 // TODO: If OES_standard_derivatives is available
147 // restore GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES
gman@chromium.org29a4d902013-02-26 20:18:06148}
gman@chromium.org1868a342012-11-07 15:56:02149
gman@chromium.org29a4d902013-02-26 20:18:06150void ContextState::RestoreState() const {
backer@chromium.org217004512013-05-10 21:25:55151 RestoreAllTextureUnitBindings();
gman@chromium.org1868a342012-11-07 15:56:02152
153 // Restore Attrib State
154 // TODO: This if should not be needed. RestoreState is getting called
155 // before GLES2Decoder::Initialize which is a bug.
156 if (vertex_attrib_manager) {
gman@chromium.orgaf6380962012-11-29 23:24:13157 // TODO(gman): Move this restoration to VertexAttribManager.
gman@chromium.org1868a342012-11-07 15:56:02158 for (size_t attrib = 0; attrib < vertex_attrib_manager->num_attribs();
159 ++attrib) {
gman@chromium.org29a4d902013-02-26 20:18:06160 RestoreAttribute(attrib);
gman@chromium.org1868a342012-11-07 15:56:02161 }
gman@chromium.org1868a342012-11-07 15:56:02162 }
163
gman@chromium.org15cc23fa2013-02-26 21:56:25164 RestoreBufferBindings();
gman@chromium.org29a4d902013-02-26 20:18:06165 RestoreRenderbufferBindings();
166 RestoreProgramBindings();
gman@chromium.org15cc23fa2013-02-26 21:56:25167 RestoreGlobalState();
gman@chromium.org1868a342012-11-07 15:56:02168}
169
kloveless@chromium.orgd3eba342013-04-18 21:11:50170ErrorState* ContextState::GetErrorState() {
171 return error_state_.get();
172}
173
gman@chromium.orgf731b9462012-10-30 00:35:22174// Include the auto-generated part of this file. We split this because it means
175// we can easily edit the non-auto generated parts right here in this file
176// instead of having to edit some template or the code generator.
177#include "gpu/command_buffer/service/context_state_impl_autogen.h"
178
gman@chromium.orge259eb412012-10-13 05:47:24179} // namespace gles2
180} // namespace gpu
181
182