[chromium] Use TextureLayerChromium for WebGL content instead of a dedicated layer...
[WebKit.git] / Source / WebCore / platform / graphics / chromium / DrawingBufferChromium.cpp
1 /*
2  * Copyright (c) 2010, Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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
13  * distribution.
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.
17  *
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.
29  */
30
31 #include "config.h"
32
33 #include "DrawingBuffer.h"
34
35 #include "CanvasRenderingContext.h"
36 #include "Extensions3DChromium.h"
37 #include "GraphicsContext3D.h"
38 #include "TextureLayerChromium.h"
39 #include "cc/CCProxy.h"
40 #include "cc/CCTextureUpdater.h"
41 #include <algorithm>
42
43 using namespace std;
44
45 namespace WebCore {
46
47 static unsigned generateColorTexture(GraphicsContext3D* context, const IntSize& size)
48 {
49     unsigned offscreenColorTexture = context->createTexture();
50     if (!offscreenColorTexture)
51         return 0;
52
53     context->bindTexture(GraphicsContext3D::TEXTURE_2D, offscreenColorTexture);
54     context->texParameteri(GraphicsContext3D::TEXTURE_2D, GraphicsContext3D::TEXTURE_MAG_FILTER, GraphicsContext3D::LINEAR);
55     context->texParameteri(GraphicsContext3D::TEXTURE_2D, GraphicsContext3D::TEXTURE_MIN_FILTER, GraphicsContext3D::LINEAR);
56     context->texParameteri(GraphicsContext3D::TEXTURE_2D, GraphicsContext3D::TEXTURE_WRAP_S, GraphicsContext3D::CLAMP_TO_EDGE);
57     context->texParameteri(GraphicsContext3D::TEXTURE_2D, GraphicsContext3D::TEXTURE_WRAP_T, GraphicsContext3D::CLAMP_TO_EDGE);
58     context->texImage2DResourceSafe(GraphicsContext3D::TEXTURE_2D, 0, GraphicsContext3D::RGBA, size.width(), size.height(), 0, GraphicsContext3D::RGBA, GraphicsContext3D::UNSIGNED_BYTE);
59
60     return offscreenColorTexture;
61 }
62
63 DrawingBuffer::DrawingBuffer(GraphicsContext3D* context,
64                              const IntSize& size,
65                              bool multisampleExtensionSupported,
66                              bool packedDepthStencilExtensionSupported,
67                              PreserveDrawingBuffer preserve,
68                              AlphaRequirement alpha)
69     : m_preserveDrawingBuffer(preserve)
70     , m_alpha(alpha)
71     , m_scissorEnabled(false)
72     , m_texture2DBinding(0)
73     , m_framebufferBinding(0)
74     , m_activeTextureUnit(GraphicsContext3D::TEXTURE0)
75     , m_context(context)
76     , m_size(-1, -1)
77     , m_multisampleExtensionSupported(multisampleExtensionSupported)
78     , m_packedDepthStencilExtensionSupported(packedDepthStencilExtensionSupported)
79     , m_fbo(0)
80     , m_colorBuffer(0)
81     , m_frontColorBuffer(0)
82     , m_separateFrontTexture(m_preserveDrawingBuffer == Preserve || CCProxy::implThread())
83     , m_depthStencilBuffer(0)
84     , m_depthBuffer(0)
85     , m_stencilBuffer(0)
86     , m_multisampleFBO(0)
87     , m_multisampleColorBuffer(0)
88 {
89     initialize(size);
90 }
91
92 DrawingBuffer::~DrawingBuffer()
93 {
94     if (!m_context)
95         return;
96
97     clear();
98 }
99
100 void DrawingBuffer::initialize(const IntSize& size)
101 {
102     m_fbo = m_context->createFramebuffer();
103
104     if (m_separateFrontTexture)
105         m_frontColorBuffer = generateColorTexture(m_context.get(), size);
106
107     m_context->bindFramebuffer(GraphicsContext3D::FRAMEBUFFER, m_fbo);
108     m_colorBuffer = generateColorTexture(m_context.get(), size);
109     m_context->framebufferTexture2D(GraphicsContext3D::FRAMEBUFFER, GraphicsContext3D::COLOR_ATTACHMENT0, GraphicsContext3D::TEXTURE_2D, m_colorBuffer, 0);
110     createSecondaryBuffers();
111     if (!reset(size)) {
112         m_context.clear();
113         return;
114     }
115 }
116
117 #if USE(ACCELERATED_COMPOSITING)
118 void DrawingBuffer::prepareBackBuffer()
119 {
120     m_context->makeContextCurrent();
121
122     if (multisample())
123         commit();
124
125     if (m_preserveDrawingBuffer == Discard && m_separateFrontTexture) {
126         swap(m_frontColorBuffer, m_colorBuffer);
127         // It appears safe to overwrite the context's framebuffer binding in the Discard case since there will always be a
128         // WebGLRenderingContext::clearIfComposited() call made before the next draw call which restores the framebuffer binding.
129         // If this stops being true at some point, we should track the current framebuffer binding in the DrawingBuffer and restore
130         // it after attaching the new back buffer here.
131         m_context->bindFramebuffer(GraphicsContext3D::FRAMEBUFFER, m_fbo);
132         m_context->framebufferTexture2D(GraphicsContext3D::FRAMEBUFFER, GraphicsContext3D::COLOR_ATTACHMENT0, GraphicsContext3D::TEXTURE_2D, m_colorBuffer, 0);
133     }
134
135     if (multisample())
136         bind();
137 }
138
139 bool DrawingBuffer::requiresCopyFromBackToFrontBuffer() const
140 {
141     return m_separateFrontTexture && m_preserveDrawingBuffer == Preserve;
142 }
143
144 unsigned DrawingBuffer::frontColorBuffer() const
145 {
146     return m_separateFrontTexture ? m_frontColorBuffer : m_colorBuffer;
147 }
148 #endif
149
150 class DrawingBufferPrivate : public TextureLayerChromiumClient {
151     WTF_MAKE_NONCOPYABLE(DrawingBufferPrivate);
152 public:
153     explicit DrawingBufferPrivate(DrawingBuffer* drawingBuffer)
154         : m_drawingBuffer(drawingBuffer)
155         , m_layer(TextureLayerChromium::create(this))
156     {
157         GraphicsContext3D::Attributes attributes = m_drawingBuffer->graphicsContext3D()->getContextAttributes();
158         m_layer->setOpaque(!attributes.alpha);
159         m_layer->setPremultipliedAlpha(attributes.premultipliedAlpha);
160     }
161
162     virtual ~DrawingBufferPrivate()
163     {
164         m_layer->clearClient();
165     }
166
167     virtual unsigned prepareTexture(CCTextureUpdater& updater) OVERRIDE
168     {
169         m_drawingBuffer->prepareBackBuffer();
170
171         context()->flush();
172         context()->markLayerComposited();
173
174         unsigned textureId = m_drawingBuffer->frontColorBuffer();
175         if (m_drawingBuffer->requiresCopyFromBackToFrontBuffer())
176             updater.appendCopy(m_drawingBuffer->colorBuffer(), textureId, m_drawingBuffer->size());
177
178         return textureId;
179     }
180
181     virtual GraphicsContext3D* context() OVERRIDE
182     {
183         return m_drawingBuffer->graphicsContext3D();
184     }
185
186     LayerChromium* layer() const { return m_layer.get(); }
187
188 private:
189     DrawingBuffer* m_drawingBuffer;
190     RefPtr<TextureLayerChromium> m_layer;
191 };
192
193 #if USE(ACCELERATED_COMPOSITING)
194 PlatformLayer* DrawingBuffer::platformLayer()
195 {
196     if (!m_private)
197         m_private = adoptPtr(new DrawingBufferPrivate(this));
198
199     return m_private->layer();
200 }
201 #endif
202
203 Platform3DObject DrawingBuffer::framebuffer() const
204 {
205     return m_fbo;
206 }
207
208 #if USE(ACCELERATED_COMPOSITING)
209 void DrawingBuffer::paintCompositedResultsToCanvas(CanvasRenderingContext* context)
210 {
211     if (!m_context->makeContextCurrent() || m_context->getExtensions()->getGraphicsResetStatusARB() != GraphicsContext3D::NO_ERROR)
212         return;
213
214     IntSize framebufferSize = m_context->getInternalFramebufferSize();
215
216     // Since we're using the same context as WebGL, we have to restore any state we change (in this case, just the framebuffer binding).
217     // FIXME: The WebGLRenderingContext tracks the current framebuffer binding, it would be slightly more efficient to use this value
218     // rather than querying it off of the context.
219     GC3Dint previousFramebuffer = 0;
220     m_context->getIntegerv(GraphicsContext3D::FRAMEBUFFER_BINDING, &previousFramebuffer);
221
222     Platform3DObject framebuffer = m_context->createFramebuffer();
223     m_context->bindFramebuffer(GraphicsContext3D::FRAMEBUFFER, framebuffer);
224     m_context->framebufferTexture2D(GraphicsContext3D::FRAMEBUFFER, GraphicsContext3D::COLOR_ATTACHMENT0, GraphicsContext3D::TEXTURE_2D, frontColorBuffer(), 0);
225
226     Extensions3DChromium* extensions = static_cast<Extensions3DChromium*>(m_context->getExtensions());
227     extensions->paintFramebufferToCanvas(framebuffer, framebufferSize.width(), framebufferSize.height(), !m_context->getContextAttributes().premultipliedAlpha, context->canvas()->buffer());
228     m_context->deleteFramebuffer(framebuffer);
229
230     m_context->bindFramebuffer(GraphicsContext3D::FRAMEBUFFER, previousFramebuffer);
231 }
232 #endif
233
234 }