2 * Copyright (c) 2010, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "DrawingBuffer.h"
35 #include "CanvasRenderingContext.h"
36 #include "Extensions3DChromium.h"
37 #include "GraphicsContext3D.h"
38 #include "WebGLLayerChromium.h"
39 #include "cc/CCProxy.h"
46 static unsigned generateColorTexture(GraphicsContext3D* context, const IntSize& size)
48 unsigned offscreenColorTexture = context->createTexture();
49 if (!offscreenColorTexture)
52 context->bindTexture(GraphicsContext3D::TEXTURE_2D, offscreenColorTexture);
53 context->texParameteri(GraphicsContext3D::TEXTURE_2D, GraphicsContext3D::TEXTURE_MAG_FILTER, GraphicsContext3D::LINEAR);
54 context->texParameteri(GraphicsContext3D::TEXTURE_2D, GraphicsContext3D::TEXTURE_MIN_FILTER, GraphicsContext3D::LINEAR);
55 context->texParameteri(GraphicsContext3D::TEXTURE_2D, GraphicsContext3D::TEXTURE_WRAP_S, GraphicsContext3D::CLAMP_TO_EDGE);
56 context->texParameteri(GraphicsContext3D::TEXTURE_2D, GraphicsContext3D::TEXTURE_WRAP_T, GraphicsContext3D::CLAMP_TO_EDGE);
57 context->texImage2DResourceSafe(GraphicsContext3D::TEXTURE_2D, 0, GraphicsContext3D::RGBA, size.width(), size.height(), 0, GraphicsContext3D::RGBA, GraphicsContext3D::UNSIGNED_BYTE);
59 return offscreenColorTexture;
62 DrawingBuffer::DrawingBuffer(GraphicsContext3D* context,
64 bool multisampleExtensionSupported,
65 bool packedDepthStencilExtensionSupported,
66 PreserveDrawingBuffer preserve,
67 AlphaRequirement alpha)
68 : m_preserveDrawingBuffer(preserve)
70 , m_scissorEnabled(false)
71 , m_texture2DBinding(0)
72 , m_framebufferBinding(0)
73 , m_activeTextureUnit(GraphicsContext3D::TEXTURE0)
76 , m_multisampleExtensionSupported(multisampleExtensionSupported)
77 , m_packedDepthStencilExtensionSupported(packedDepthStencilExtensionSupported)
80 , m_frontColorBuffer(0)
81 , m_separateFrontTexture(m_preserveDrawingBuffer == Preserve || CCProxy::implThread())
82 , m_depthStencilBuffer(0)
86 , m_multisampleColorBuffer(0)
91 DrawingBuffer::~DrawingBuffer()
94 m_platformLayer->setDrawingBuffer(0);
102 void DrawingBuffer::initialize(const IntSize& size)
104 m_fbo = m_context->createFramebuffer();
106 if (m_separateFrontTexture)
107 m_frontColorBuffer = generateColorTexture(m_context.get(), size);
109 m_context->bindFramebuffer(GraphicsContext3D::FRAMEBUFFER, m_fbo);
110 m_colorBuffer = generateColorTexture(m_context.get(), size);
111 m_context->framebufferTexture2D(GraphicsContext3D::FRAMEBUFFER, GraphicsContext3D::COLOR_ATTACHMENT0, GraphicsContext3D::TEXTURE_2D, m_colorBuffer, 0);
112 createSecondaryBuffers();
119 #if USE(ACCELERATED_COMPOSITING)
120 void DrawingBuffer::prepareBackBuffer()
122 m_context->makeContextCurrent();
127 if (m_preserveDrawingBuffer == Discard && m_separateFrontTexture) {
128 swap(m_frontColorBuffer, m_colorBuffer);
129 // It appears safe to overwrite the context's framebuffer binding in the Discard case since there will always be a
130 // WebGLRenderingContext::clearIfComposited() call made before the next draw call which restores the framebuffer binding.
131 // If this stops being true at some point, we should track the current framebuffer binding in the DrawingBuffer and restore
132 // it after attaching the new back buffer here.
133 m_context->bindFramebuffer(GraphicsContext3D::FRAMEBUFFER, m_fbo);
134 m_context->framebufferTexture2D(GraphicsContext3D::FRAMEBUFFER, GraphicsContext3D::COLOR_ATTACHMENT0, GraphicsContext3D::TEXTURE_2D, m_colorBuffer, 0);
141 bool DrawingBuffer::requiresCopyFromBackToFrontBuffer() const
143 return m_separateFrontTexture && m_preserveDrawingBuffer == Preserve;
146 unsigned DrawingBuffer::frontColorBuffer() const
148 return m_separateFrontTexture ? m_frontColorBuffer : m_colorBuffer;
152 #if USE(ACCELERATED_COMPOSITING)
153 PlatformLayer* DrawingBuffer::platformLayer()
155 if (!m_platformLayer) {
156 m_platformLayer = WebGLLayerChromium::create();
157 m_platformLayer->setDrawingBuffer(this);
158 m_platformLayer->setOpaque(m_alpha == Opaque);
161 return m_platformLayer.get();
165 Platform3DObject DrawingBuffer::framebuffer() const
170 #if USE(ACCELERATED_COMPOSITING)
171 void DrawingBuffer::paintCompositedResultsToCanvas(CanvasRenderingContext* context)
173 if (!m_context->makeContextCurrent() || m_context->getExtensions()->getGraphicsResetStatusARB() != GraphicsContext3D::NO_ERROR)
176 IntSize framebufferSize = m_context->getInternalFramebufferSize();
178 // Since we're using the same context as WebGL, we have to restore any state we change (in this case, just the framebuffer binding).
179 // FIXME: The WebGLRenderingContext tracks the current framebuffer binding, it would be slightly more efficient to use this value
180 // rather than querying it off of the context.
181 GC3Dint previousFramebuffer = 0;
182 m_context->getIntegerv(GraphicsContext3D::FRAMEBUFFER_BINDING, &previousFramebuffer);
184 Platform3DObject framebuffer = m_context->createFramebuffer();
185 m_context->bindFramebuffer(GraphicsContext3D::FRAMEBUFFER, framebuffer);
186 m_context->framebufferTexture2D(GraphicsContext3D::FRAMEBUFFER, GraphicsContext3D::COLOR_ATTACHMENT0, GraphicsContext3D::TEXTURE_2D, frontColorBuffer(), 0);
188 Extensions3DChromium* extensions = static_cast<Extensions3DChromium*>(m_context->getExtensions());
189 extensions->paintFramebufferToCanvas(framebuffer, framebufferSize.width(), framebufferSize.height(), !m_context->getContextAttributes().premultipliedAlpha, context->canvas()->buffer());
190 m_context->deleteFramebuffer(framebuffer);
192 m_context->bindFramebuffer(GraphicsContext3D::FRAMEBUFFER, previousFramebuffer);