Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / gpu / command_buffer / service / gles2_cmd_copy_texture_chromium.cc
blob974cbd7d8e0c7d85dd99dff0049b04019ec007e2
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.
5 #include "gpu/command_buffer/service/gles2_cmd_copy_texture_chromium.h"
7 #include <algorithm>
9 #include "base/basictypes.h"
10 #include "gpu/command_buffer/service/gl_utils.h"
11 #include "gpu/command_buffer/service/gles2_cmd_decoder.h"
13 #define SHADER(src) \
14 "#ifdef GL_ES\n" \
15 "precision mediump float;\n" \
16 "#define TexCoordPrecision mediump\n" \
17 "#else\n" \
18 "#define TexCoordPrecision\n" \
19 "#endif\n" #src
20 #define SHADER_2D(src) \
21 "#define SamplerType sampler2D\n" \
22 "#define TextureLookup texture2D\n" SHADER(src)
23 #define SHADER_RECTANGLE_ARB(src) \
24 "#define SamplerType sampler2DRect\n" \
25 "#define TextureLookup texture2DRect\n" SHADER(src)
26 #define SHADER_EXTERNAL_OES(src) \
27 "#extension GL_OES_EGL_image_external : require\n" \
28 "#define SamplerType samplerExternalOES\n" \
29 "#define TextureLookup texture2D\n" SHADER(src)
30 #define FRAGMENT_SHADERS(src) \
31 SHADER_2D(src), SHADER_RECTANGLE_ARB(src), SHADER_EXTERNAL_OES(src)
33 namespace {
35 const GLfloat kIdentityMatrix[16] = {1.0f, 0.0f, 0.0f, 0.0f,
36 0.0f, 1.0f, 0.0f, 0.0f,
37 0.0f, 0.0f, 1.0f, 0.0f,
38 0.0f, 0.0f, 0.0f, 1.0f};
40 enum VertexShaderId {
41 VERTEX_SHADER_COPY_TEXTURE,
42 VERTEX_SHADER_COPY_TEXTURE_FLIP_Y,
43 NUM_VERTEX_SHADERS,
46 enum FragmentShaderId {
47 FRAGMENT_SHADER_COPY_TEXTURE_2D,
48 FRAGMENT_SHADER_COPY_TEXTURE_RECTANGLE_ARB,
49 FRAGMENT_SHADER_COPY_TEXTURE_EXTERNAL_OES,
50 FRAGMENT_SHADER_COPY_TEXTURE_PREMULTIPLY_ALPHA_2D,
51 FRAGMENT_SHADER_COPY_TEXTURE_PREMULTIPLY_ALPHA_RECTANGLE_ARB,
52 FRAGMENT_SHADER_COPY_TEXTURE_PREMULTIPLY_ALPHA_EXTERNAL_OES,
53 FRAGMENT_SHADER_COPY_TEXTURE_UNPREMULTIPLY_ALPHA_2D,
54 FRAGMENT_SHADER_COPY_TEXTURE_UNPREMULTIPLY_ALPHA_RECTANGLE_ARB,
55 FRAGMENT_SHADER_COPY_TEXTURE_UNPREMULTIPLY_ALPHA_EXTERNAL_OES,
56 NUM_FRAGMENT_SHADERS,
59 const char* vertex_shader_source[NUM_VERTEX_SHADERS] = {
60 // VERTEX_SHADER_COPY_TEXTURE
61 SHADER(
62 uniform mat4 u_matrix;
63 uniform vec2 u_half_size;
64 attribute vec4 a_position;
65 varying TexCoordPrecision vec2 v_uv;
66 void main(void) {
67 gl_Position = u_matrix * a_position;
68 v_uv = a_position.xy * vec2(u_half_size.s, u_half_size.t) +
69 vec2(u_half_size.s, u_half_size.t);
70 }),
71 // VERTEX_SHADER_COPY_TEXTURE_FLIP_Y
72 SHADER(
73 uniform mat4 u_matrix;
74 uniform vec2 u_half_size;
75 attribute vec4 a_position;
76 varying TexCoordPrecision vec2 v_uv;
77 void main(void) {
78 gl_Position = u_matrix * a_position;
79 v_uv = a_position.xy * vec2(u_half_size.s, -u_half_size.t) +
80 vec2(u_half_size.s, u_half_size.t);
81 }),
84 const char* fragment_shader_source[NUM_FRAGMENT_SHADERS] = {
85 // FRAGMENT_SHADER_COPY_TEXTURE_*
86 FRAGMENT_SHADERS(
87 uniform SamplerType u_sampler;
88 varying TexCoordPrecision vec2 v_uv;
89 void main(void) {
90 gl_FragColor = TextureLookup(u_sampler, v_uv.st);
91 }),
92 // FRAGMENT_SHADER_COPY_TEXTURE_PREMULTIPLY_ALPHA_*
93 FRAGMENT_SHADERS(
94 uniform SamplerType u_sampler;
95 varying TexCoordPrecision vec2 v_uv;
96 void main(void) {
97 gl_FragColor = TextureLookup(u_sampler, v_uv.st);
98 gl_FragColor.rgb *= gl_FragColor.a;
99 }),
100 // FRAGMENT_SHADER_COPY_TEXTURE_UNPREMULTIPLY_ALPHA_*
101 FRAGMENT_SHADERS(
102 uniform SamplerType u_sampler;
103 varying TexCoordPrecision vec2 v_uv;
104 void main(void) {
105 gl_FragColor = TextureLookup(u_sampler, v_uv.st);
106 if (gl_FragColor.a > 0.0)
107 gl_FragColor.rgb /= gl_FragColor.a;
111 // Returns the correct vertex shader id to evaluate the copy operation for
112 // the CHROMIUM_flipy setting.
113 VertexShaderId GetVertexShaderId(bool flip_y) {
114 // bit 0: flip y
115 static VertexShaderId shader_ids[] = {
116 VERTEX_SHADER_COPY_TEXTURE,
117 VERTEX_SHADER_COPY_TEXTURE_FLIP_Y,
120 unsigned index = flip_y ? 1 : 0;
121 return shader_ids[index];
124 // Returns the correct fragment shader id to evaluate the copy operation for
125 // the premultiply alpha pixel store settings and target.
126 FragmentShaderId GetFragmentShaderId(bool premultiply_alpha,
127 bool unpremultiply_alpha,
128 GLenum target) {
129 enum {
130 SAMPLER_2D,
131 SAMPLER_RECTANGLE_ARB,
132 SAMPLER_EXTERNAL_OES,
133 NUM_SAMPLERS
136 // bit 0: premultiply alpha
137 // bit 1: unpremultiply alpha
138 static FragmentShaderId shader_ids[][NUM_SAMPLERS] = {
140 FRAGMENT_SHADER_COPY_TEXTURE_2D,
141 FRAGMENT_SHADER_COPY_TEXTURE_RECTANGLE_ARB,
142 FRAGMENT_SHADER_COPY_TEXTURE_EXTERNAL_OES,
145 FRAGMENT_SHADER_COPY_TEXTURE_PREMULTIPLY_ALPHA_2D,
146 FRAGMENT_SHADER_COPY_TEXTURE_PREMULTIPLY_ALPHA_RECTANGLE_ARB,
147 FRAGMENT_SHADER_COPY_TEXTURE_PREMULTIPLY_ALPHA_EXTERNAL_OES,
150 FRAGMENT_SHADER_COPY_TEXTURE_UNPREMULTIPLY_ALPHA_2D,
151 FRAGMENT_SHADER_COPY_TEXTURE_UNPREMULTIPLY_ALPHA_RECTANGLE_ARB,
152 FRAGMENT_SHADER_COPY_TEXTURE_UNPREMULTIPLY_ALPHA_EXTERNAL_OES,
155 FRAGMENT_SHADER_COPY_TEXTURE_2D,
156 FRAGMENT_SHADER_COPY_TEXTURE_RECTANGLE_ARB,
157 FRAGMENT_SHADER_COPY_TEXTURE_EXTERNAL_OES,
160 unsigned index = (premultiply_alpha ? (1 << 0) : 0) |
161 (unpremultiply_alpha ? (1 << 1) : 0);
163 switch (target) {
164 case GL_TEXTURE_2D:
165 return shader_ids[index][SAMPLER_2D];
166 case GL_TEXTURE_RECTANGLE_ARB:
167 return shader_ids[index][SAMPLER_RECTANGLE_ARB];
168 case GL_TEXTURE_EXTERNAL_OES:
169 return shader_ids[index][SAMPLER_EXTERNAL_OES];
170 default:
171 break;
174 NOTREACHED();
175 return shader_ids[0][SAMPLER_2D];
178 void CompileShader(GLuint shader, const char* shader_source) {
179 glShaderSource(shader, 1, &shader_source, 0);
180 glCompileShader(shader);
181 #ifndef NDEBUG
182 GLint compile_status;
183 glGetShaderiv(shader, GL_COMPILE_STATUS, &compile_status);
184 if (GL_TRUE != compile_status)
185 DLOG(ERROR) << "CopyTextureCHROMIUM: shader compilation failure.";
186 #endif
189 void DeleteShader(GLuint shader) {
190 if (shader)
191 glDeleteShader(shader);
194 bool BindFramebufferTexture2D(GLenum target,
195 GLuint texture_id,
196 GLuint framebuffer) {
197 DCHECK(target == GL_TEXTURE_2D || target == GL_TEXTURE_RECTANGLE_ARB);
198 glActiveTexture(GL_TEXTURE0);
199 glBindTexture(target, texture_id);
200 // NVidia drivers require texture settings to be a certain way
201 // or they won't report FRAMEBUFFER_COMPLETE.
202 glTexParameterf(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
203 glTexParameterf(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
204 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
205 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
206 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebuffer);
207 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, target,
208 texture_id, 0);
210 #ifndef NDEBUG
211 GLenum fb_status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER);
212 if (GL_FRAMEBUFFER_COMPLETE != fb_status) {
213 DLOG(ERROR) << "CopyTextureCHROMIUM: Incomplete framebuffer.";
214 return false;
216 #endif
217 return true;
220 void DoCopyTexImage2D(const gpu::gles2::GLES2Decoder* decoder,
221 GLenum source_target,
222 GLuint source_id,
223 GLuint dest_id,
224 GLenum dest_internal_format,
225 GLsizei width,
226 GLsizei height,
227 GLuint framebuffer) {
228 DCHECK(source_target == GL_TEXTURE_2D ||
229 source_target == GL_TEXTURE_RECTANGLE_ARB);
230 if (BindFramebufferTexture2D(source_target, source_id, framebuffer)) {
231 glBindTexture(GL_TEXTURE_2D, dest_id);
232 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
233 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
234 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
235 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
236 glCopyTexImage2D(GL_TEXTURE_2D, 0 /* level */, dest_internal_format,
237 0 /* x */, 0 /* y */, width, height, 0 /* border */);
240 decoder->RestoreTextureState(source_id);
241 decoder->RestoreTextureState(dest_id);
242 decoder->RestoreTextureUnitBindings(0);
243 decoder->RestoreActiveTexture();
244 decoder->RestoreFramebufferBindings();
247 void DoCopyTexSubImage2D(const gpu::gles2::GLES2Decoder* decoder,
248 GLenum source_target,
249 GLuint source_id,
250 GLuint dest_id,
251 GLint xoffset,
252 GLint yoffset,
253 GLint source_x,
254 GLint source_y,
255 GLsizei source_width,
256 GLsizei source_height,
257 GLuint framebuffer) {
258 DCHECK(source_target == GL_TEXTURE_2D ||
259 source_target == GL_TEXTURE_RECTANGLE_ARB);
260 if (BindFramebufferTexture2D(source_target, source_id, framebuffer)) {
261 glBindTexture(GL_TEXTURE_2D, dest_id);
262 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
263 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
264 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
265 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
266 glCopyTexSubImage2D(GL_TEXTURE_2D, 0 /* level */, xoffset, yoffset,
267 source_x, source_y, source_width, source_height);
270 decoder->RestoreTextureState(source_id);
271 decoder->RestoreTextureState(dest_id);
272 decoder->RestoreTextureUnitBindings(0);
273 decoder->RestoreActiveTexture();
274 decoder->RestoreFramebufferBindings();
277 // Copy from SkMatrix44::preTranslate
278 void PreTranslate(GLfloat* matrix, GLfloat dx, GLfloat dy, GLfloat dz) {
279 if (!dx && !dy && !dz)
280 return;
282 for (int i = 0; i < 4; ++i) {
283 matrix[(3 * 4) + i] = matrix[(0 * 4) + i] * dx + matrix[(1 * 4) + i] * dy +
284 matrix[(2 * 4) + i] * dz + matrix[(3 * 4) + i];
288 } // namespace
290 namespace gpu {
292 CopyTextureCHROMIUMResourceManager::CopyTextureCHROMIUMResourceManager()
293 : initialized_(false),
294 vertex_shaders_(NUM_VERTEX_SHADERS, 0u),
295 fragment_shaders_(NUM_FRAGMENT_SHADERS, 0u),
296 buffer_id_(0u),
297 framebuffer_(0u) {}
299 CopyTextureCHROMIUMResourceManager::~CopyTextureCHROMIUMResourceManager() {
300 // |buffer_id_| and |framebuffer_| can be not-null because when GPU context is
301 // lost, this class can be deleted without releasing resources like
302 // GLES2DecoderImpl.
305 void CopyTextureCHROMIUMResourceManager::Initialize(
306 const gles2::GLES2Decoder* decoder) {
307 static_assert(
308 kVertexPositionAttrib == 0u,
309 "kVertexPositionAttrib must be 0");
310 DCHECK(!buffer_id_);
311 DCHECK(!framebuffer_);
312 DCHECK(programs_.empty());
314 // Initialize all of the GPU resources required to perform the copy.
315 glGenBuffersARB(1, &buffer_id_);
316 glBindBuffer(GL_ARRAY_BUFFER, buffer_id_);
317 const GLfloat kQuadVertices[] = {-1.0f, -1.0f,
318 1.0f, -1.0f,
319 1.0f, 1.0f,
320 -1.0f, 1.0f};
321 glBufferData(
322 GL_ARRAY_BUFFER, sizeof(kQuadVertices), kQuadVertices, GL_STATIC_DRAW);
324 glGenFramebuffersEXT(1, &framebuffer_);
326 decoder->RestoreBufferBindings();
328 initialized_ = true;
331 void CopyTextureCHROMIUMResourceManager::Destroy() {
332 if (!initialized_)
333 return;
335 glDeleteFramebuffersEXT(1, &framebuffer_);
336 framebuffer_ = 0;
338 std::for_each(vertex_shaders_.begin(), vertex_shaders_.end(), DeleteShader);
339 std::for_each(
340 fragment_shaders_.begin(), fragment_shaders_.end(), DeleteShader);
342 for (ProgramMap::const_iterator it = programs_.begin(); it != programs_.end();
343 ++it) {
344 const ProgramInfo& info = it->second;
345 glDeleteProgram(info.program);
348 glDeleteBuffersARB(1, &buffer_id_);
349 buffer_id_ = 0;
352 void CopyTextureCHROMIUMResourceManager::DoCopyTexture(
353 const gles2::GLES2Decoder* decoder,
354 GLenum source_target,
355 GLuint source_id,
356 GLenum source_internal_format,
357 GLuint dest_id,
358 GLenum dest_internal_format,
359 GLsizei width,
360 GLsizei height,
361 bool flip_y,
362 bool premultiply_alpha,
363 bool unpremultiply_alpha) {
364 bool premultiply_alpha_change = premultiply_alpha ^ unpremultiply_alpha;
365 // GL_INVALID_OPERATION is generated if the currently bound framebuffer's
366 // format does not contain a superset of the components required by the base
367 // format of internalformat.
368 // https://www.khronos.org/opengles/sdk/docs/man/xhtml/glCopyTexImage2D.xml
369 bool source_format_contain_superset_of_dest_format =
370 (source_internal_format == dest_internal_format &&
371 source_internal_format != GL_BGRA_EXT) ||
372 (source_internal_format == GL_RGBA && dest_internal_format == GL_RGB);
373 // GL_TEXTURE_RECTANGLE_ARB on FBO is supported by OpenGL, not GLES2,
374 // so restrict this to GL_TEXTURE_2D.
375 if (source_target == GL_TEXTURE_2D && !flip_y && !premultiply_alpha_change &&
376 source_format_contain_superset_of_dest_format) {
377 DoCopyTexImage2D(decoder,
378 source_target,
379 source_id,
380 dest_id,
381 dest_internal_format,
382 width,
383 height,
384 framebuffer_);
385 return;
388 // Use kIdentityMatrix if no transform passed in.
389 DoCopyTextureWithTransform(decoder, source_target, source_id, dest_id, width,
390 height, flip_y, premultiply_alpha,
391 unpremultiply_alpha, kIdentityMatrix);
394 void CopyTextureCHROMIUMResourceManager::DoCopySubTexture(
395 const gles2::GLES2Decoder* decoder,
396 GLenum source_target,
397 GLuint source_id,
398 GLenum source_internal_format,
399 GLuint dest_id,
400 GLenum dest_internal_format,
401 GLint xoffset,
402 GLint yoffset,
403 GLint x,
404 GLint y,
405 GLsizei width,
406 GLsizei height,
407 GLsizei dest_width,
408 GLsizei dest_height,
409 GLsizei source_width,
410 GLsizei source_height,
411 bool flip_y,
412 bool premultiply_alpha,
413 bool unpremultiply_alpha) {
414 bool premultiply_alpha_change = premultiply_alpha ^ unpremultiply_alpha;
415 // GL_INVALID_OPERATION is generated if the currently bound framebuffer's
416 // format does not contain a superset of the components required by the base
417 // format of internalformat.
418 // https://www.khronos.org/opengles/sdk/docs/man/xhtml/glCopyTexImage2D.xml
419 bool source_format_contain_superset_of_dest_format =
420 (source_internal_format == dest_internal_format &&
421 source_internal_format != GL_BGRA_EXT) ||
422 (source_internal_format == GL_RGBA && dest_internal_format == GL_RGB);
423 // GL_TEXTURE_RECTANGLE_ARB on FBO is supported by OpenGL, not GLES2,
424 // so restrict this to GL_TEXTURE_2D.
425 if (source_target == GL_TEXTURE_2D && !flip_y && !premultiply_alpha_change &&
426 source_format_contain_superset_of_dest_format) {
427 DoCopyTexSubImage2D(decoder, source_target, source_id, dest_id, xoffset,
428 yoffset, x, y, width, height, framebuffer_);
429 return;
432 DoCopyTextureInternal(decoder, source_target, source_id, dest_id, xoffset - x,
433 yoffset - y, dest_width, dest_height, source_width,
434 source_height, flip_y, premultiply_alpha,
435 unpremultiply_alpha, kIdentityMatrix, xoffset, yoffset,
436 width, height);
439 void CopyTextureCHROMIUMResourceManager::DoCopyTextureWithTransform(
440 const gles2::GLES2Decoder* decoder,
441 GLenum source_target,
442 GLuint source_id,
443 GLuint dest_id,
444 GLsizei width,
445 GLsizei height,
446 bool flip_y,
447 bool premultiply_alpha,
448 bool unpremultiply_alpha,
449 const GLfloat transform_matrix[16]) {
450 GLsizei dest_width = width;
451 GLsizei dest_height = height;
452 DoCopyTextureInternal(decoder, source_target, source_id, dest_id, 0, 0,
453 dest_width, dest_height, width, height, flip_y,
454 premultiply_alpha, unpremultiply_alpha,
455 transform_matrix, 0, 0, dest_width, dest_height);
458 void CopyTextureCHROMIUMResourceManager::DoCopyTextureInternal(
459 const gles2::GLES2Decoder* decoder,
460 GLenum source_target,
461 GLuint source_id,
462 GLuint dest_id,
463 GLint xoffset,
464 GLint yoffset,
465 GLsizei dest_width,
466 GLsizei dest_height,
467 GLsizei source_width,
468 GLsizei source_height,
469 bool flip_y,
470 bool premultiply_alpha,
471 bool unpremultiply_alpha,
472 const GLfloat transform_matrix[16],
473 GLint scissor_x,
474 GLint scissor_y,
475 GLsizei scissor_width,
476 GLsizei scissor_height) {
477 DCHECK(source_target == GL_TEXTURE_2D ||
478 source_target == GL_TEXTURE_RECTANGLE_ARB ||
479 source_target == GL_TEXTURE_EXTERNAL_OES);
480 DCHECK(xoffset >= 0 && xoffset + source_width <= dest_width);
481 DCHECK(yoffset >= 0 && yoffset + source_height <= dest_height);
482 if (!initialized_) {
483 DLOG(ERROR) << "CopyTextureCHROMIUM: Uninitialized manager.";
484 return;
487 VertexShaderId vertex_shader_id = GetVertexShaderId(flip_y);
488 DCHECK_LT(static_cast<size_t>(vertex_shader_id), vertex_shaders_.size());
489 FragmentShaderId fragment_shader_id = GetFragmentShaderId(
490 premultiply_alpha, unpremultiply_alpha, source_target);
491 DCHECK_LT(static_cast<size_t>(fragment_shader_id), fragment_shaders_.size());
493 ProgramMapKey key(vertex_shader_id, fragment_shader_id);
494 ProgramInfo* info = &programs_[key];
495 // Create program if necessary.
496 if (!info->program) {
497 info->program = glCreateProgram();
498 GLuint* vertex_shader = &vertex_shaders_[vertex_shader_id];
499 if (!*vertex_shader) {
500 *vertex_shader = glCreateShader(GL_VERTEX_SHADER);
501 CompileShader(*vertex_shader, vertex_shader_source[vertex_shader_id]);
503 glAttachShader(info->program, *vertex_shader);
504 GLuint* fragment_shader = &fragment_shaders_[fragment_shader_id];
505 if (!*fragment_shader) {
506 *fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
507 CompileShader(*fragment_shader,
508 fragment_shader_source[fragment_shader_id]);
510 glAttachShader(info->program, *fragment_shader);
511 glBindAttribLocation(info->program, kVertexPositionAttrib, "a_position");
512 glLinkProgram(info->program);
513 #ifndef NDEBUG
514 GLint linked;
515 glGetProgramiv(info->program, GL_LINK_STATUS, &linked);
516 if (!linked)
517 DLOG(ERROR) << "CopyTextureCHROMIUM: program link failure.";
518 #endif
519 info->matrix_handle = glGetUniformLocation(info->program, "u_matrix");
520 info->half_size_handle = glGetUniformLocation(info->program, "u_half_size");
521 info->sampler_handle = glGetUniformLocation(info->program, "u_sampler");
523 glUseProgram(info->program);
525 if (!xoffset && !yoffset) {
526 glUniformMatrix4fv(info->matrix_handle, 1, GL_FALSE, transform_matrix);
527 } else {
528 // transform offsets from ([0, dest_width], [0, dest_height]) coord.
529 // to ([-1, 1], [-1, 1]) coord.
530 GLfloat xoffset_on_vertex = ((2.f * xoffset) / dest_width);
531 GLfloat yoffset_on_vertex = ((2.f * yoffset) / dest_height);
533 // Pass view_matrix * offset_matrix to the program.
534 GLfloat view_transform[16];
535 memcpy(view_transform, transform_matrix, 16 * sizeof(GLfloat));
536 PreTranslate(view_transform, xoffset_on_vertex, yoffset_on_vertex, 0);
537 glUniformMatrix4fv(info->matrix_handle, 1, GL_FALSE, view_transform);
539 if (source_target == GL_TEXTURE_RECTANGLE_ARB)
540 glUniform2f(info->half_size_handle, source_width / 2.0f,
541 source_height / 2.0f);
542 else
543 glUniform2f(info->half_size_handle, 0.5f, 0.5f);
545 if (BindFramebufferTexture2D(GL_TEXTURE_2D, dest_id, framebuffer_)) {
546 #ifndef NDEBUG
547 // glValidateProgram of MACOSX validates FBO unlike other platforms, so
548 // glValidateProgram must be called after FBO binding. crbug.com/463439
549 glValidateProgram(info->program);
550 GLint validation_status;
551 glGetProgramiv(info->program, GL_VALIDATE_STATUS, &validation_status);
552 if (GL_TRUE != validation_status) {
553 DLOG(ERROR) << "CopyTextureCHROMIUM: Invalid shader.";
554 return;
556 #endif
557 decoder->ClearAllAttributes();
558 glEnableVertexAttribArray(kVertexPositionAttrib);
560 glBindBuffer(GL_ARRAY_BUFFER, buffer_id_);
561 glVertexAttribPointer(kVertexPositionAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
563 glUniform1i(info->sampler_handle, 0);
565 glBindTexture(source_target, source_id);
566 glTexParameterf(source_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
567 glTexParameterf(source_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
568 glTexParameteri(source_target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
569 glTexParameteri(source_target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
571 glDisable(GL_DEPTH_TEST);
572 glDisable(GL_STENCIL_TEST);
573 glDisable(GL_CULL_FACE);
574 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
575 glDepthMask(GL_FALSE);
576 glDisable(GL_BLEND);
578 glEnable(GL_SCISSOR_TEST);
579 glScissor(scissor_x, scissor_y, scissor_width, scissor_height);
580 glViewport(0, 0, dest_width, dest_height);
581 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
584 decoder->RestoreAllAttributes();
585 decoder->RestoreTextureState(source_id);
586 decoder->RestoreTextureState(dest_id);
587 decoder->RestoreTextureUnitBindings(0);
588 decoder->RestoreActiveTexture();
589 decoder->RestoreProgramBindings();
590 decoder->RestoreBufferBindings();
591 decoder->RestoreFramebufferBindings();
592 decoder->RestoreGlobalState();
595 } // namespace gpu