1d167a556e6a1dfcd11ac6f84918f2d1d8894d9a
[WebKit-https.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 "GraphicsContext3DPrivate.h"
39 #include <algorithm>
40 #include <public/WebCompositor.h>
41 #include <public/WebExternalTextureLayer.h>
42 #include <public/WebExternalTextureLayerClient.h>
43 #include <public/WebGraphicsContext3D.h>
44
45 using namespace std;
46
47 namespace WebCore {
48
49 static unsigned generateColorTexture(GraphicsContext3D* context, const IntSize& size)
50 {
51     unsigned offscreenColorTexture = context->createTexture();
52     if (!offscreenColorTexture)
53         return 0;
54
55     context->bindTexture(GraphicsContext3D::TEXTURE_2D, offscreenColorTexture);
56     context->texParameteri(GraphicsContext3D::TEXTURE_2D, GraphicsContext3D::TEXTURE_MAG_FILTER, GraphicsContext3D::LINEAR);
57     context->texParameteri(GraphicsContext3D::TEXTURE_2D, GraphicsContext3D::TEXTURE_MIN_FILTER, GraphicsContext3D::LINEAR);
58     context->texParameteri(GraphicsContext3D::TEXTURE_2D, GraphicsContext3D::TEXTURE_WRAP_S, GraphicsContext3D::CLAMP_TO_EDGE);
59     context->texParameteri(GraphicsContext3D::TEXTURE_2D, GraphicsContext3D::TEXTURE_WRAP_T, GraphicsContext3D::CLAMP_TO_EDGE);
60     context->texImage2DResourceSafe(GraphicsContext3D::TEXTURE_2D, 0, GraphicsContext3D::RGBA, size.width(), size.height(), 0, GraphicsContext3D::RGBA, GraphicsContext3D::UNSIGNED_BYTE);
61
62     return offscreenColorTexture;
63 }
64
65 DrawingBuffer::DrawingBuffer(GraphicsContext3D* context,
66                              const IntSize& size,
67                              bool multisampleExtensionSupported,
68                              bool packedDepthStencilExtensionSupported,
69                              PreserveDrawingBuffer preserve,
70                              AlphaRequirement alpha)
71     : m_preserveDrawingBuffer(preserve)
72     , m_alpha(alpha)
73     , m_scissorEnabled(false)
74     , m_texture2DBinding(0)
75     , m_framebufferBinding(0)
76     , m_activeTextureUnit(GraphicsContext3D::TEXTURE0)
77     , m_context(context)
78     , m_size(-1, -1)
79     , m_multisampleExtensionSupported(multisampleExtensionSupported)
80     , m_packedDepthStencilExtensionSupported(packedDepthStencilExtensionSupported)
81     , m_fbo(0)
82     , m_colorBuffer(0)
83     , m_frontColorBuffer(0)
84     , m_separateFrontTexture(m_preserveDrawingBuffer == Preserve || WebKit::WebCompositor::threadingEnabled())
85     , m_depthStencilBuffer(0)
86     , m_depthBuffer(0)
87     , m_stencilBuffer(0)
88     , m_multisampleFBO(0)
89     , m_multisampleColorBuffer(0)
90 {
91     initialize(size);
92 }
93
94 DrawingBuffer::~DrawingBuffer()
95 {
96     if (!m_context)
97         return;
98
99     clear();
100 }
101
102 void DrawingBuffer::initialize(const IntSize& size)
103 {
104     m_fbo = m_context->createFramebuffer();
105
106     if (m_separateFrontTexture)
107         m_frontColorBuffer = generateColorTexture(m_context.get(), size);
108
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();
113     if (!reset(size)) {
114         m_context.clear();
115         return;
116     }
117 }
118
119 #if USE(ACCELERATED_COMPOSITING)
120 void DrawingBuffer::prepareBackBuffer()
121 {
122     m_context->makeContextCurrent();
123
124     if (multisample())
125         commit();
126
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);
135     }
136
137     if (multisample() && !m_framebufferBinding)
138         bind();
139     else
140         restoreFramebufferBinding();
141 }
142
143 bool DrawingBuffer::requiresCopyFromBackToFrontBuffer() const
144 {
145     return m_separateFrontTexture && m_preserveDrawingBuffer == Preserve;
146 }
147
148 unsigned DrawingBuffer::frontColorBuffer() const
149 {
150     return m_separateFrontTexture ? m_frontColorBuffer : m_colorBuffer;
151 }
152 #endif
153
154 class DrawingBufferPrivate : public WebKit::WebExternalTextureLayerClient {
155     WTF_MAKE_NONCOPYABLE(DrawingBufferPrivate);
156 public:
157     explicit DrawingBufferPrivate(DrawingBuffer* drawingBuffer)
158         : m_drawingBuffer(drawingBuffer)
159         , m_layer(WebKit::WebExternalTextureLayer::create(this))
160     {
161         GraphicsContext3D::Attributes attributes = m_drawingBuffer->graphicsContext3D()->getContextAttributes();
162         m_layer.setOpaque(!attributes.alpha);
163         m_layer.setPremultipliedAlpha(attributes.premultipliedAlpha);
164     }
165
166     virtual ~DrawingBufferPrivate()
167     {
168         m_layer.clearClient();
169     }
170
171     virtual unsigned prepareTexture(WebKit::WebTextureUpdater& updater) OVERRIDE
172     {
173         m_drawingBuffer->prepareBackBuffer();
174
175         m_drawingBuffer->graphicsContext3D()->flush();
176         m_drawingBuffer->graphicsContext3D()->markLayerComposited();
177
178         unsigned textureId = m_drawingBuffer->frontColorBuffer();
179         if (m_drawingBuffer->requiresCopyFromBackToFrontBuffer())
180             updater.appendCopy(m_drawingBuffer->colorBuffer(), textureId, m_drawingBuffer->size());
181
182         return textureId;
183     }
184
185     virtual WebKit::WebGraphicsContext3D* context() OVERRIDE
186     {
187         return GraphicsContext3DPrivate::extractWebGraphicsContext3D(m_drawingBuffer->graphicsContext3D());
188     }
189
190     WebKit::WebLayer* layer() { return &m_layer; }
191
192 private:
193     DrawingBuffer* m_drawingBuffer;
194     WebKit::WebExternalTextureLayer m_layer;
195 };
196
197 #if USE(ACCELERATED_COMPOSITING)
198 PlatformLayer* DrawingBuffer::platformLayer()
199 {
200     if (!m_private)
201         m_private = adoptPtr(new DrawingBufferPrivate(this));
202
203     return m_private->layer();
204 }
205 #endif
206
207 Platform3DObject DrawingBuffer::framebuffer() const
208 {
209     return m_fbo;
210 }
211
212 #if USE(ACCELERATED_COMPOSITING)
213 void DrawingBuffer::paintCompositedResultsToCanvas(ImageBuffer* imageBuffer)
214 {
215     if (!m_context->makeContextCurrent() || m_context->getExtensions()->getGraphicsResetStatusARB() != GraphicsContext3D::NO_ERROR)
216         return;
217
218     IntSize framebufferSize = m_context->getInternalFramebufferSize();
219
220     // Since we're using the same context as WebGL, we have to restore any state we change (in this case, just the framebuffer binding).
221     // FIXME: The WebGLRenderingContext tracks the current framebuffer binding, it would be slightly more efficient to use this value
222     // rather than querying it off of the context.
223     GC3Dint previousFramebuffer = 0;
224     m_context->getIntegerv(GraphicsContext3D::FRAMEBUFFER_BINDING, &previousFramebuffer);
225
226     Platform3DObject framebuffer = m_context->createFramebuffer();
227     m_context->bindFramebuffer(GraphicsContext3D::FRAMEBUFFER, framebuffer);
228     m_context->framebufferTexture2D(GraphicsContext3D::FRAMEBUFFER, GraphicsContext3D::COLOR_ATTACHMENT0, GraphicsContext3D::TEXTURE_2D, frontColorBuffer(), 0);
229
230     Extensions3DChromium* extensions = static_cast<Extensions3DChromium*>(m_context->getExtensions());
231     extensions->paintFramebufferToCanvas(framebuffer, framebufferSize.width(), framebufferSize.height(), !m_context->getContextAttributes().premultipliedAlpha, imageBuffer);
232     m_context->deleteFramebuffer(framebuffer);
233
234     m_context->bindFramebuffer(GraphicsContext3D::FRAMEBUFFER, previousFramebuffer);
235 }
236 #endif
237
238 }