WebKit Roll 139512:139548
[chromium-blink-merge.git] / cc / texture_copier.cc
blob0b457982404d5d8f331aff46c8663cb25e69c2fa
1 // Copyright 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 "cc/texture_copier.h"
7 #include "base/debug/trace_event.h"
8 #include "build/build_config.h"
9 #include "cc/gl_renderer.h" // For the GLC() macro.
10 #include "third_party/WebKit/Source/Platform/chromium/public/WebGraphicsContext3D.h"
11 #include "third_party/khronos/GLES2/gl2.h"
13 namespace cc {
15 AcceleratedTextureCopier::AcceleratedTextureCopier(WebKit::WebGraphicsContext3D* context, bool usingBindUniforms)
16 : m_context(context)
17 , m_usingBindUniforms(usingBindUniforms)
19 DCHECK(m_context);
20 GLC(m_context, m_fbo = m_context->createFramebuffer());
21 GLC(m_context, m_positionBuffer = m_context->createBuffer());
23 static const float kPositions[4][4] = {
24 {-1, -1, 0, 1},
25 { 1, -1, 0, 1},
26 { 1, 1, 0, 1},
27 {-1, 1, 0, 1}
30 GLC(m_context, m_context->bindBuffer(GL_ARRAY_BUFFER, m_positionBuffer));
31 GLC(m_context, m_context->bufferData(GL_ARRAY_BUFFER, sizeof(kPositions), kPositions, GL_STATIC_DRAW));
32 GLC(m_context, m_context->bindBuffer(GL_ARRAY_BUFFER, 0));
34 m_blitProgram.reset(new BlitProgram(m_context));
37 AcceleratedTextureCopier::~AcceleratedTextureCopier()
39 if (m_blitProgram)
40 m_blitProgram->cleanup(m_context);
41 if (m_positionBuffer)
42 GLC(m_context, m_context->deleteBuffer(m_positionBuffer));
43 if (m_fbo)
44 GLC(m_context, m_context->deleteFramebuffer(m_fbo));
47 void AcceleratedTextureCopier::copyTexture(Parameters parameters)
49 TRACE_EVENT0("cc", "TextureCopier::copyTexture");
51 GLC(m_context, m_context->disable(GL_SCISSOR_TEST));
53 // Note: this code does not restore the viewport, bound program, 2D texture, framebuffer, buffer or blend enable.
54 GLC(m_context, m_context->bindFramebuffer(GL_FRAMEBUFFER, m_fbo));
55 GLC(m_context, m_context->framebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, parameters.destTexture, 0));
57 #if defined(OS_ANDROID)
58 // Clear destination to improve performance on tiling GPUs.
59 // TODO: Use EXT_discard_framebuffer or skip clearing if it isn't available.
60 GLC(m_context, m_context->clear(GL_COLOR_BUFFER_BIT));
61 #endif
63 GLC(m_context, m_context->bindTexture(GL_TEXTURE_2D, parameters.sourceTexture));
64 GLC(m_context, m_context->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST));
65 GLC(m_context, m_context->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST));
67 if (!m_blitProgram->initialized())
68 m_blitProgram->initialize(m_context, m_usingBindUniforms);
70 // TODO: Use EXT_framebuffer_blit if available.
71 GLC(m_context, m_context->useProgram(m_blitProgram->program()));
73 const int kPositionAttribute = 0;
74 GLC(m_context, m_context->bindBuffer(GL_ARRAY_BUFFER, m_positionBuffer));
75 GLC(m_context, m_context->vertexAttribPointer(kPositionAttribute, 4, GL_FLOAT, false, 0, 0));
76 GLC(m_context, m_context->enableVertexAttribArray(kPositionAttribute));
77 GLC(m_context, m_context->bindBuffer(GL_ARRAY_BUFFER, 0));
79 GLC(m_context, m_context->viewport(0, 0, parameters.size.width(), parameters.size.height()));
80 GLC(m_context, m_context->disable(GL_BLEND));
81 GLC(m_context, m_context->drawArrays(GL_TRIANGLE_FAN, 0, 4));
83 GLC(m_context, m_context->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
84 GLC(m_context, m_context->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
85 GLC(m_context, m_context->disableVertexAttribArray(kPositionAttribute));
87 GLC(m_context, m_context->useProgram(0));
89 GLC(m_context, m_context->framebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0));
90 GLC(m_context, m_context->bindFramebuffer(GL_FRAMEBUFFER, 0));
91 GLC(m_context, m_context->bindTexture(GL_TEXTURE_2D, 0));
93 GLC(m_context, m_context->enable(GL_SCISSOR_TEST));
96 void AcceleratedTextureCopier::flush()
98 GLC(m_context, m_context->flush());
101 } // namespace cc