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.
7 #include "TextureCopier.h"
9 #include "CCRendererGL.h" // For the GLC() macro.
10 #include "GraphicsContext3D.h"
11 #include "TraceEvent.h"
12 #include <public/WebGraphicsContext3D.h>
16 #if USE(ACCELERATED_COMPOSITING)
17 AcceleratedTextureCopier::AcceleratedTextureCopier(WebKit::WebGraphicsContext3D
* context
, bool usingBindUniforms
)
19 , m_usingBindUniforms(usingBindUniforms
)
22 GLC(m_context
, m_fbo
= m_context
->createFramebuffer());
23 GLC(m_context
, m_positionBuffer
= m_context
->createBuffer());
25 static const float kPositions
[4][4] = {
32 GLC(m_context
, m_context
->bindBuffer(GraphicsContext3D::ARRAY_BUFFER
, m_positionBuffer
));
33 GLC(m_context
, m_context
->bufferData(GraphicsContext3D::ARRAY_BUFFER
, sizeof(kPositions
), kPositions
, GraphicsContext3D::STATIC_DRAW
));
34 GLC(m_context
, m_context
->bindBuffer(GraphicsContext3D::ARRAY_BUFFER
, 0));
36 m_blitProgram
= adoptPtr(new BlitProgram(m_context
));
39 AcceleratedTextureCopier::~AcceleratedTextureCopier()
42 m_blitProgram
->cleanup(m_context
);
44 GLC(m_context
, m_context
->deleteBuffer(m_positionBuffer
));
46 GLC(m_context
, m_context
->deleteFramebuffer(m_fbo
));
49 void AcceleratedTextureCopier::copyTexture(Parameters parameters
)
51 TRACE_EVENT0("cc", "TextureCopier::copyTexture");
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(GraphicsContext3D::FRAMEBUFFER
, m_fbo
));
55 GLC(m_context
, m_context
->framebufferTexture2D(GraphicsContext3D::FRAMEBUFFER
, GraphicsContext3D::COLOR_ATTACHMENT0
, GraphicsContext3D::TEXTURE_2D
, parameters
.destTexture
, 0));
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(GraphicsContext3D::COLOR_BUFFER_BIT
));
63 GLC(m_context
, m_context
->bindTexture(GraphicsContext3D::TEXTURE_2D
, parameters
.sourceTexture
));
64 GLC(m_context
, m_context
->texParameteri(GraphicsContext3D::TEXTURE_2D
, GraphicsContext3D::TEXTURE_MIN_FILTER
, GraphicsContext3D::NEAREST
));
65 GLC(m_context
, m_context
->texParameteri(GraphicsContext3D::TEXTURE_2D
, GraphicsContext3D::TEXTURE_MAG_FILTER
, GraphicsContext3D::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(GraphicsContext3D::ARRAY_BUFFER
, m_positionBuffer
));
75 GLC(m_context
, m_context
->vertexAttribPointer(kPositionAttribute
, 4, GraphicsContext3D::FLOAT
, false, 0, 0));
76 GLC(m_context
, m_context
->enableVertexAttribArray(kPositionAttribute
));
77 GLC(m_context
, m_context
->bindBuffer(GraphicsContext3D::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(GraphicsContext3D::BLEND
));
81 GLC(m_context
, m_context
->drawArrays(GraphicsContext3D::TRIANGLE_FAN
, 0, 4));
83 GLC(m_context
, m_context
->texParameteri(GraphicsContext3D::TEXTURE_2D
, GraphicsContext3D::TEXTURE_MIN_FILTER
, GraphicsContext3D::LINEAR
));
84 GLC(m_context
, m_context
->texParameteri(GraphicsContext3D::TEXTURE_2D
, GraphicsContext3D::TEXTURE_MAG_FILTER
, GraphicsContext3D::LINEAR
));
85 GLC(m_context
, m_context
->disableVertexAttribArray(kPositionAttribute
));
87 GLC(m_context
, m_context
->useProgram(0));
89 GLC(m_context
, m_context
->framebufferTexture2D(GraphicsContext3D::FRAMEBUFFER
, GraphicsContext3D::COLOR_ATTACHMENT0
, GraphicsContext3D::TEXTURE_2D
, 0, 0));
90 GLC(m_context
, m_context
->bindFramebuffer(GraphicsContext3D::FRAMEBUFFER
, 0));
91 GLC(m_context
, m_context
->bindTexture(GraphicsContext3D::TEXTURE_2D
, 0));
94 void AcceleratedTextureCopier::flush()
96 GLC(m_context
, m_context
->flush());
101 #endif // USE(ACCELERATED_COMPOSITING)