|
OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "gpu/command_buffer/service/gles2_cmd_copy_texture_chromium.h" | |
6 #include "gpu/command_buffer/common/types.h" | |
7 #include "gpu/command_buffer/service/gl_utils.h" | |
8 | |
9 #define SHADER0(Src) #Src | |
10 #define SHADER(Src) SHADER0(Src) | |
11 | |
12 namespace { | |
13 | |
14 const int kNumShaders = 5; | |
15 enum ShaderId { | |
16 VERTEX_SHADER_POS_TEX, | |
17 FRAGMENT_SHADER_TEX, | |
18 FRAGMENT_SHADER_TEX_FLIP_Y, | |
19 FRAGMENT_SHADER_TEX_PREMULTIPLY_ALPHA, | |
20 FRAGMENT_SHADER_TEX_PREMULTIPLY_ALPHA_FLIP_Y | |
21 }; | |
22 | |
23 enum ProgramId { | |
24 PROGRAM_COPY_TEXTURE, | |
25 PROGRAM_COPY_TEXTURE_FLIP_Y, | |
26 PROGRAM_COPY_TEXTURE_PREMULTIPLY_ALPHA, | |
27 PROGRAM_COPY_TEXTURE_PREMULTIPLY_ALPHA_FLIPY | |
28 }; | |
29 | |
30 // Returns the correct program to evaluate the copy operation for | |
31 // the CHROMIUM_flipy and premultiply alpha pixel store settings. | |
32 ProgramId GetProgram(bool flip_y, bool premultiply_alpha) { | |
33 if (flip_y && premultiply_alpha) | |
34 return PROGRAM_COPY_TEXTURE_PREMULTIPLY_ALPHA_FLIPY; | |
35 | |
36 if (flip_y) | |
37 return PROGRAM_COPY_TEXTURE_FLIP_Y; | |
38 | |
39 if (premultiply_alpha) | |
40 return PROGRAM_COPY_TEXTURE_PREMULTIPLY_ALPHA; | |
41 | |
42 return PROGRAM_COPY_TEXTURE; | |
43 } | |
44 | |
45 const char* GetShaderSource(ShaderId shader) { | |
46 switch (shader) { | |
47 case VERTEX_SHADER_POS_TEX: | |
48 return SHADER( | |
49 precision mediump float; | |
50 attribute vec4 a_position; | |
51 attribute vec2 a_texCoord; | |
52 varying vec2 v_uv; | |
53 void main(void) { | |
54 gl_Position = a_position; | |
55 v_uv = a_texCoord; | |
56 }); | |
57 case FRAGMENT_SHADER_TEX: | |
58 return SHADER( | |
59 precision mediump float; | |
60 uniform sampler2D u_texSampler; | |
61 varying vec2 v_uv; | |
62 void main(void) { | |
63 gl_FragColor = texture2D(u_texSampler, v_uv.st); | |
64 }); | |
65 case FRAGMENT_SHADER_TEX_FLIP_Y: | |
66 return SHADER( | |
67 precision mediump float; | |
68 uniform sampler2D u_texSampler; | |
69 varying vec2 v_uv; | |
70 void main(void) { | |
71 gl_FragColor = texture2D(u_texSampler, vec2(v_uv.s, 1.0 - v_uv.t)); | |
72 }); | |
73 case FRAGMENT_SHADER_TEX_PREMULTIPLY_ALPHA: | |
74 return SHADER( | |
75 precision mediump float; | |
76 uniform sampler2D u_texSampler; | |
77 varying vec2 v_uv; | |
78 void main(void) { | |
79 gl_FragColor = texture2D(u_texSampler, v_uv.st); | |
80 gl_FragColor.rgb *= gl_FragColor.a; | |
81 }); | |
82 case FRAGMENT_SHADER_TEX_PREMULTIPLY_ALPHA_FLIP_Y: | |
83 return SHADER( | |
84 precision mediump float; | |
85 uniform sampler2D u_texSampler; | |
86 varying vec2 v_uv; | |
87 void main(void) { | |
88 gl_FragColor = texture2D(u_texSampler, vec2(v_uv.s, 1.0 - v_uv.t)); | |
89 gl_FragColor.rgb *= gl_FragColor.a; | |
90 }); | |
91 default: | |
92 return 0; | |
93 } | |
94 } | |
95 | |
96 } // namespace | |
97 | |
98 CopyTextureCHROMIUMResourceManager::~CopyTextureCHROMIUMResourceManager() { | |
99 FreeResources(); | |
apatrick_chromium
2012/04/05 21:58:39
What if the wrong GL context is current when these
Jeff Timanus
2012/04/13 00:13:29
That's a good point. This class is presently dest
greggman
2012/04/13 00:19:15
Not if "have_context" is false in GLES2DecoderImpl
Jeff Timanus
2012/04/13 04:13:46
I believe that I fixed this issue by conditionally
| |
100 } | |
101 | |
102 void CopyTextureCHROMIUMResourceManager::Initialize() { | |
103 // Initialize all of the GPU resources required to perform the copy. | |
104 GLfloat texture_vertices[] = { -1.0f, -1.0f, 0.0f, 1.0f, | |
105 1.0f, -1.0f, 0.0f, 1.0f, | |
106 1.0f, 1.0f, 0.0f, 1.0f, | |
107 -1.0f, 1.0f, 0.0f, 1.0f }; | |
108 | |
109 GLfloat texture_coords[] = { 0.0f, 0.0f, | |
110 1.0f, 0.0f, | |
111 1.0f, 1.0f, | |
112 0.0f, 1.0f }; | |
113 | |
114 glGenBuffersARB(2, buffer_ids_); | |
115 glBindBuffer(GL_ARRAY_BUFFER, buffer_ids_[0]); | |
116 glBufferData(GL_ARRAY_BUFFER, 16 * sizeof(GLfloat), texture_vertices, | |
117 GL_STATIC_DRAW); | |
118 | |
119 glBindBuffer(GL_ARRAY_BUFFER, buffer_ids_[1]); | |
120 glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(GLfloat), texture_coords, | |
121 GL_STATIC_DRAW); | |
122 | |
123 glGenFramebuffersEXT(1, &framebuffer_); | |
124 | |
125 GLuint shaders[kNumShaders]; | |
126 shaders[0] = glCreateShader(GL_VERTEX_SHADER); | |
127 const char* shader_source = GetShaderSource(VERTEX_SHADER_POS_TEX); | |
128 glShaderSource(shaders[0], 1, &shader_source, 0); | |
129 for (int shader = 1; shader < kNumShaders; ++shader) { | |
130 shaders[shader] = glCreateShader(GL_FRAGMENT_SHADER); | |
131 shader_source = GetShaderSource(static_cast<ShaderId>(shader)); | |
132 glShaderSource(shaders[shader], 1, &shader_source, 0); | |
133 glCompileShader(shaders[shader]); | |
134 #ifndef NDEBUG | |
135 GLint compile_status; | |
136 glGetShaderiv(shaders[shader], GL_COMPILE_STATUS, &compile_status); | |
137 if (GL_TRUE != compile_status) | |
138 DLOG(ERROR) << "CopyTextureCHROMIUM: shader compilation failure."; | |
139 #endif | |
140 } | |
141 | |
142 for (int program = 0; program < kNumPrograms; ++program) { | |
143 programs_[program] = glCreateProgram(); | |
144 glAttachShader(programs_[program], shaders[0]); | |
145 glAttachShader(programs_[program], shaders[program + 1]); | |
146 | |
147 glLinkProgram(programs_[program]); | |
148 #ifndef NDEBUG | |
149 GLint linked; | |
150 glGetProgramiv(programs_[program], GL_LINK_STATUS, &linked); | |
151 if (!linked) | |
152 DLOG(ERROR) << "CopyTextureCHROMIUM: program link failure."; | |
153 #endif | |
154 | |
155 position_locations_[program] = glGetAttribLocation(programs_[program], | |
156 "a_position"); | |
157 texture_coordinate_locations_[program] = glGetAttribLocation( | |
158 programs_[program], "a_texCoord"); | |
159 sampler_locations_[program] = glGetUniformLocation(programs_[program], | |
160 "u_texSampler"); | |
161 } | |
162 | |
163 for (int shader = 0; shader < kNumShaders; ++shader) | |
164 glDeleteShader(shaders[shader]); | |
165 } | |
166 | |
167 void CopyTextureCHROMIUMResourceManager::FreeResources() { | |
168 glDeleteFramebuffersEXT(1, &framebuffer_); | |
169 | |
170 for (int program = 0; program < kNumPrograms; ++program) | |
171 glDeleteProgram(programs_[program]); | |
172 | |
173 glDeleteBuffersARB(2, buffer_ids_); | |
174 } | |
175 | |
176 void CopyTextureCHROMIUMResourceManager::DoCopyTexture( | |
177 GLenum target, | |
178 GLuint source_id, | |
179 GLuint dest_id, | |
180 GLint level, | |
181 bool flip_y, | |
182 bool premultiply_alpha) { | |
183 GLuint program = GetProgram(flip_y, premultiply_alpha); | |
184 glUseProgram(programs_[program]); | |
185 | |
186 #ifndef NDEBUG | |
187 glValidateProgram(program); | |
188 GLint validation_status; | |
189 glGetProgramiv(program, GL_VALIDATE_STATUS, &validation_status); | |
190 DLOG(ERROR) << "CopyTextureCHROMIUM: Invalid shader."; | |
191 #endif | |
192 | |
193 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebuffer_); | |
194 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, target, | |
195 dest_id, level); | |
196 | |
197 #ifndef NDEBUG | |
198 GLenum fb_status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER); | |
199 DLOG(ERROR) << "CopyTextureCHROMIUM: Incomplete framebuffer."; | |
200 #endif | |
201 | |
202 glBindBuffer(GL_ARRAY_BUFFER, 0); | |
203 glEnableVertexAttribArray(position_locations_[program]); | |
204 glEnableVertexAttribArray(texture_coordinate_locations_[program]); | |
205 | |
206 glBindBuffer(GL_ARRAY_BUFFER, buffer_ids_[0]); | |
207 glVertexAttribPointer(position_locations_[program], 4, GL_FLOAT, GL_FALSE, | |
208 4 * sizeof(GLfloat), 0); | |
209 | |
210 glBindBuffer(GL_ARRAY_BUFFER, buffer_ids_[1]); | |
211 glVertexAttribPointer(texture_coordinate_locations_[program], 2, GL_FLOAT, | |
212 GL_FALSE, 2 * sizeof(GLfloat), 0); | |
213 | |
214 glActiveTexture(GL_TEXTURE0); | |
215 glUniform1i(sampler_locations_[program], 0); | |
216 | |
217 glBindTexture(GL_TEXTURE_2D, source_id); | |
218 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
219 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
220 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | |
221 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | |
222 | |
223 glDisable(GL_DEPTH_TEST); | |
224 glDisable(GL_SCISSOR_TEST); | |
225 glDisable(GL_STENCIL_TEST); | |
226 glDisable(GL_CULL_FACE); | |
227 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); | |
228 glDepthMask(GL_FALSE); | |
229 glDisable(GL_BLEND); | |
230 | |
231 glDrawArrays(GL_TRIANGLE_FAN, 0, 4); | |
232 } | |
OLD | NEW |