OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // 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 | 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 "gpu/command_buffer/service/context_state.h" | 5 #include "gpu/command_buffer/service/context_state.h" |
6 | 6 |
7 #include "gpu/command_buffer/common/gles2_cmd_utils.h" | 7 #include "gpu/command_buffer/common/gles2_cmd_utils.h" |
8 #include "gpu/command_buffer/service/buffer_manager.h" | 8 #include "gpu/command_buffer/service/buffer_manager.h" |
9 #include "gpu/command_buffer/service/error_state.h" | 9 #include "gpu/command_buffer/service/error_state.h" |
10 #include "gpu/command_buffer/service/framebuffer_manager.h" | 10 #include "gpu/command_buffer/service/framebuffer_manager.h" |
11 #include "gpu/command_buffer/service/program_manager.h" | 11 #include "gpu/command_buffer/service/program_manager.h" |
12 #include "gpu/command_buffer/service/renderbuffer_manager.h" | 12 #include "gpu/command_buffer/service/renderbuffer_manager.h" |
13 #include "ui/gl/gl_bindings.h" | 13 #include "ui/gl/gl_bindings.h" |
14 #include "ui/gl/gl_implementation.h" | 14 #include "ui/gl/gl_implementation.h" |
15 | 15 |
16 namespace gpu { | 16 namespace gpu { |
17 namespace gles2 { | 17 namespace gles2 { |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 void EnableDisable(GLenum pname, bool enable) { | 21 void EnableDisable(GLenum pname, bool enable) { |
22 if (enable) { | 22 if (enable) { |
23 glEnable(pname); | 23 glEnable(pname); |
24 } else { | 24 } else { |
25 glDisable(pname); | 25 glDisable(pname); |
26 } | 26 } |
27 } | 27 } |
28 | 28 |
29 GLuint Get2dServiceId(const TextureUnit& unit) { | |
30 return unit.bound_texture_2d.get() | |
31 ? unit.bound_texture_2d->service_id() : 0; | |
32 } | |
33 | |
34 GLuint GetCubeServiceId(const TextureUnit& unit) { | |
35 return unit.bound_texture_cube_map.get() | |
36 ? unit.bound_texture_cube_map->service_id() : 0; | |
37 } | |
38 | |
39 GLuint GetOesServiceId(const TextureUnit& unit) { | |
40 return unit.bound_texture_external_oes.get() | |
41 ? unit.bound_texture_external_oes->service_id() : 0; | |
42 } | |
43 | |
44 GLuint GetArbServiceId(const TextureUnit& unit) { | |
45 return unit.bound_texture_rectangle_arb.get() | |
46 ? unit.bound_texture_rectangle_arb->service_id() : 0; | |
47 } | |
48 | |
29 } // anonymous namespace. | 49 } // anonymous namespace. |
30 | 50 |
31 TextureUnit::TextureUnit() | 51 TextureUnit::TextureUnit() |
32 : bind_target(GL_TEXTURE_2D) { | 52 : bind_target(GL_TEXTURE_2D) { |
33 } | 53 } |
34 | 54 |
35 TextureUnit::~TextureUnit() { | 55 TextureUnit::~TextureUnit() { |
36 } | 56 } |
37 | 57 |
38 ContextState::ContextState(FeatureInfo* feature_info, Logger* logger) | 58 ContextState::ContextState(FeatureInfo* feature_info, Logger* logger) |
39 : active_texture_unit(0), | 59 : active_texture_unit(0), |
40 pack_reverse_row_order(false), | 60 pack_reverse_row_order(false), |
41 fbo_binding_for_scissor_workaround_dirty_(false), | 61 fbo_binding_for_scissor_workaround_dirty_(false), |
42 feature_info_(feature_info), | 62 feature_info_(feature_info), |
43 error_state_(ErrorState::Create(logger)) { | 63 error_state_(ErrorState::Create(logger)) { |
44 Initialize(); | 64 Initialize(); |
45 } | 65 } |
46 | 66 |
47 ContextState::~ContextState() { | 67 ContextState::~ContextState() { |
48 } | 68 } |
49 | 69 |
50 void ContextState::RestoreTextureUnitBindings(GLuint unit) const { | 70 void ContextState::RestoreTextureUnitBindings( |
71 GLuint unit, const ContextState* prev_state) const { | |
51 DCHECK_LT(unit, texture_units.size()); | 72 DCHECK_LT(unit, texture_units.size()); |
52 const TextureUnit& texture_unit = texture_units[unit]; | 73 const TextureUnit& texture_unit = texture_units[unit]; |
53 glActiveTexture(GL_TEXTURE0 + unit); | 74 GLuint service_id_2d = Get2dServiceId(texture_unit); |
54 GLuint service_id = texture_unit.bound_texture_2d.get() | 75 GLuint service_id_cube = GetCubeServiceId(texture_unit); |
55 ? texture_unit.bound_texture_2d->service_id() | 76 GLuint service_id_oes = GetOesServiceId(texture_unit); |
56 : 0; | 77 GLuint service_id_arb = GetArbServiceId(texture_unit); |
57 glBindTexture(GL_TEXTURE_2D, service_id); | |
58 service_id = texture_unit.bound_texture_cube_map.get() | |
59 ? texture_unit.bound_texture_cube_map->service_id() | |
60 : 0; | |
61 glBindTexture(GL_TEXTURE_CUBE_MAP, service_id); | |
62 | 78 |
no sievers
2013/12/19 19:35:38
Can you just put 'if (prev_state)' around the whol
kaanb
2013/12/20 13:20:31
Done.
| |
63 if (feature_info_->feature_flags().oes_egl_image_external) { | 79 const TextureUnit* prev_texture_unit = |
64 service_id = texture_unit.bound_texture_external_oes.get() | 80 (prev_state) ? &prev_state->texture_units[unit] : NULL; |
no sievers
2013/12/19 19:35:38
nit: no need for '(',')'
kaanb
2013/12/20 13:20:31
I removed all ternary conditional operators.
| |
65 ? texture_unit.bound_texture_external_oes->service_id() | 81 GLuint prev_service_id_2d = |
66 : 0; | 82 (prev_texture_unit) ? Get2dServiceId(*prev_texture_unit) : 0; |
67 glBindTexture(GL_TEXTURE_EXTERNAL_OES, service_id); | 83 GLuint prev_service_id_cube = |
84 (prev_texture_unit) ? GetCubeServiceId(*prev_texture_unit) : 0; | |
85 GLuint prev_service_id_oes = | |
86 (prev_texture_unit) ? GetOesServiceId(*prev_texture_unit) : 0; | |
87 GLuint prev_service_id_arb = | |
88 (prev_texture_unit) ? GetArbServiceId(*prev_texture_unit) : 0; | |
89 | |
90 bool binding_2d_changed = prev_service_id_2d != service_id_2d; | |
no sievers
2013/12/19 19:35:38
There is one bug here:
- context A binds texture 0
Ken Russell (switch to Gerrit)
2013/12/19 21:39:33
Doesn't the code below already handle this case? I
no sievers
2013/12/19 21:44:18
Actually, you are right, I missed that. But still
Ken Russell (switch to Gerrit)
2013/12/19 22:13:16
Yes, I agree. :) It's a lot of code for a simple s
kaanb
2013/12/20 13:20:31
I simplified the code as suggested by sievers@, I'
| |
91 bool binding_cube_changed = prev_service_id_cube != service_id_cube; | |
92 bool binding_oes_changed = prev_service_id_oes != service_id_oes; | |
93 bool binding_arb_changed = prev_service_id_arb != service_id_arb; | |
94 | |
95 // Early-out if nothing has changed from the previous state. | |
96 if (prev_state && !binding_2d_changed && !binding_cube_changed | |
97 && !binding_oes_changed && !binding_arb_changed) { | |
98 return; | |
68 } | 99 } |
69 | 100 |
70 if (feature_info_->feature_flags().arb_texture_rectangle) { | 101 glActiveTexture(GL_TEXTURE0 + unit); |
71 service_id = texture_unit.bound_texture_rectangle_arb.get() | 102 if (!prev_state || binding_2d_changed) { |
72 ? texture_unit.bound_texture_rectangle_arb->service_id() | 103 glBindTexture(GL_TEXTURE_2D, service_id_2d); |
73 : 0; | 104 } |
74 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, service_id); | 105 if (!prev_state || binding_cube_changed) { |
106 glBindTexture(GL_TEXTURE_CUBE_MAP, service_id_cube); | |
107 } | |
108 if (feature_info_->feature_flags().oes_egl_image_external | |
109 && (!prev_state || binding_oes_changed)) { | |
110 glBindTexture(GL_TEXTURE_EXTERNAL_OES, service_id_oes); | |
111 } | |
112 if (feature_info_->feature_flags().arb_texture_rectangle | |
113 && (!prev_state || binding_arb_changed)) { | |
114 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, service_id_arb); | |
75 } | 115 } |
76 } | 116 } |
77 | 117 |
78 void ContextState::RestoreBufferBindings() const { | 118 void ContextState::RestoreBufferBindings() const { |
79 if (vertex_attrib_manager.get()) { | 119 if (vertex_attrib_manager.get()) { |
80 Buffer* element_array_buffer = | 120 Buffer* element_array_buffer = |
81 vertex_attrib_manager->element_array_buffer(); | 121 vertex_attrib_manager->element_array_buffer(); |
82 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, | 122 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, |
83 element_array_buffer ? element_array_buffer->service_id() : 0); | 123 element_array_buffer ? element_array_buffer->service_id() : 0); |
84 } | 124 } |
85 glBindBuffer(GL_ARRAY_BUFFER, | 125 glBindBuffer(GL_ARRAY_BUFFER, |
86 bound_array_buffer.get() ? bound_array_buffer->service_id() : 0); | 126 bound_array_buffer.get() ? bound_array_buffer->service_id() : 0); |
87 } | 127 } |
88 | 128 |
89 void ContextState::RestoreRenderbufferBindings() const { | 129 void ContextState::RestoreRenderbufferBindings() const { |
90 // Restore Bindings | 130 // Restore Bindings |
91 glBindRenderbufferEXT( | 131 glBindRenderbufferEXT( |
92 GL_RENDERBUFFER, | 132 GL_RENDERBUFFER, |
93 bound_renderbuffer.get() ? bound_renderbuffer->service_id() : 0); | 133 bound_renderbuffer.get() ? bound_renderbuffer->service_id() : 0); |
94 } | 134 } |
95 | 135 |
96 void ContextState::RestoreProgramBindings() const { | 136 void ContextState::RestoreProgramBindings() const { |
97 glUseProgram(current_program.get() ? current_program->service_id() : 0); | 137 glUseProgram(current_program.get() ? current_program->service_id() : 0); |
98 } | 138 } |
99 | 139 |
100 void ContextState::RestoreActiveTexture() const { | 140 void ContextState::RestoreActiveTexture() const { |
101 glActiveTexture(GL_TEXTURE0 + active_texture_unit); | 141 glActiveTexture(GL_TEXTURE0 + active_texture_unit); |
102 } | 142 } |
103 | 143 |
104 void ContextState::RestoreAllTextureUnitBindings() const { | 144 void ContextState::RestoreAllTextureUnitBindings( |
145 const ContextState* prev_state) const { | |
105 // Restore Texture state. | 146 // Restore Texture state. |
106 for (size_t ii = 0; ii < texture_units.size(); ++ii) { | 147 for (size_t ii = 0; ii < texture_units.size(); ++ii) { |
107 RestoreTextureUnitBindings(ii); | 148 RestoreTextureUnitBindings(ii, prev_state); |
108 } | 149 } |
109 RestoreActiveTexture(); | 150 RestoreActiveTexture(); |
110 } | 151 } |
111 | 152 |
112 void ContextState::RestoreAttribute(GLuint attrib_index) const { | 153 void ContextState::RestoreAttribute(GLuint attrib_index) const { |
113 const VertexAttrib* attrib = | 154 const VertexAttrib* attrib = |
114 vertex_attrib_manager->GetVertexAttrib(attrib_index); | 155 vertex_attrib_manager->GetVertexAttrib(attrib_index); |
115 const void* ptr = reinterpret_cast<const void*>(attrib->offset()); | 156 const void* ptr = reinterpret_cast<const void*>(attrib->offset()); |
116 Buffer* buffer = attrib->buffer(); | 157 Buffer* buffer = attrib->buffer(); |
117 glBindBuffer(GL_ARRAY_BUFFER, buffer ? buffer->service_id() : 0); | 158 glBindBuffer(GL_ARRAY_BUFFER, buffer ? buffer->service_id() : 0); |
(...skipping 14 matching lines...) Expand all Loading... | |
132 } | 173 } |
133 } | 174 } |
134 glVertexAttrib4fv(attrib_index, attrib_values[attrib_index].v); | 175 glVertexAttrib4fv(attrib_index, attrib_values[attrib_index].v); |
135 } | 176 } |
136 | 177 |
137 void ContextState::RestoreGlobalState() const { | 178 void ContextState::RestoreGlobalState() const { |
138 InitCapabilities(); | 179 InitCapabilities(); |
139 InitState(); | 180 InitState(); |
140 } | 181 } |
141 | 182 |
142 void ContextState::RestoreState() const { | 183 void ContextState::RestoreState(const ContextState* prev_state) const { |
143 RestoreAllTextureUnitBindings(); | 184 RestoreAllTextureUnitBindings(prev_state); |
144 | 185 |
145 // Restore Attrib State | 186 // Restore Attrib State |
146 // TODO: This if should not be needed. RestoreState is getting called | 187 // TODO: This if should not be needed. RestoreState is getting called |
147 // before GLES2Decoder::Initialize which is a bug. | 188 // before GLES2Decoder::Initialize which is a bug. |
148 if (vertex_attrib_manager.get()) { | 189 if (vertex_attrib_manager.get()) { |
149 // TODO(gman): Move this restoration to VertexAttribManager. | 190 // TODO(gman): Move this restoration to VertexAttribManager. |
150 for (size_t attrib = 0; attrib < vertex_attrib_manager->num_attribs(); | 191 for (size_t attrib = 0; attrib < vertex_attrib_manager->num_attribs(); |
151 ++attrib) { | 192 ++attrib) { |
152 RestoreAttribute(attrib); | 193 RestoreAttribute(attrib); |
153 } | 194 } |
(...skipping 11 matching lines...) Expand all Loading... | |
165 | 206 |
166 // Include the auto-generated part of this file. We split this because it means | 207 // Include the auto-generated part of this file. We split this because it means |
167 // we can easily edit the non-auto generated parts right here in this file | 208 // we can easily edit the non-auto generated parts right here in this file |
168 // instead of having to edit some template or the code generator. | 209 // instead of having to edit some template or the code generator. |
169 #include "gpu/command_buffer/service/context_state_impl_autogen.h" | 210 #include "gpu/command_buffer/service/context_state_impl_autogen.h" |
170 | 211 |
171 } // namespace gles2 | 212 } // namespace gles2 |
172 } // namespace gpu | 213 } // namespace gpu |
173 | 214 |
174 | 215 |
OLD | NEW |