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.
31 #ifndef DrawingBuffer_h
32 #define DrawingBuffer_h
34 #include "GraphicsContext3D.h"
35 #include "GraphicsLayer.h"
36 #include "GraphicsTypes3D.h"
39 #include <wtf/Noncopyable.h>
40 #include <wtf/OwnPtr.h>
41 #include <wtf/PassOwnPtr.h>
43 #include <wtf/RetainPtr.h>
47 class CanvasRenderingContext;
48 class GraphicsContext3D;
50 #if PLATFORM(CHROMIUM)
51 class WebGLLayerChromium;
54 // Manages a rendering target (framebuffer + attachment) for a canvas. Can publish its rendering
55 // results to a PlatformLayer for compositing.
56 class DrawingBuffer : public RefCounted<DrawingBuffer> {
58 enum PreserveDrawingBuffer {
63 enum AlphaRequirement {
68 static PassRefPtr<DrawingBuffer> create(GraphicsContext3D*, const IntSize&, PreserveDrawingBuffer, AlphaRequirement);
69 friend class GraphicsContext3D;
73 // Issues a glClear() on all framebuffers associated with this DrawingBuffer. The caller is responsible for
74 // making the context current and setting the clear values and masks. Modifies the framebuffer binding.
75 void clearFramebuffers(GC3Dbitfield clearMask);
77 // Returns true if the buffer was successfully resized.
78 bool reset(const IntSize&);
80 IntSize size() const { return m_size; }
81 Platform3DObject colorBuffer() const { return m_colorBuffer; }
83 // Clear all resources from this object, as well as context. Called when context is destroyed
84 // to prevent invalid accesses to the resources.
87 // Create the depth/stencil and multisample buffers, if needed.
88 void createSecondaryBuffers();
90 void resizeDepthStencil(int sampleCount);
92 // Copies the multisample color buffer to the normal color buffer and leaves m_fbo bound
93 void commit(long x = 0, long y = 0, long width = -1, long height = -1);
95 // commit should copy the full multisample buffer, and not respect the
96 // current scissor bounds. Track the state of the scissor test so that it
97 // can be disabled during calls to commit.
98 void setScissorEnabled(bool scissorEnabled) { m_scissorEnabled = scissorEnabled; }
100 // The DrawingBuffer needs to track the texture bound to texture unit 0.
101 // The bound texture is tracked to avoid costly queries during rendering.
102 void setTexture2DBinding(GC3Dint texture) { m_texture2DBinding = texture; }
104 // Track the currently active texture unit. Texture unit 0 is used as host for a scratch
106 void setActiveTextureUnit(GC3Dint textureUnit) { m_activeTextureUnit = textureUnit; }
108 bool multisample() const;
110 Platform3DObject framebuffer() const;
112 PassRefPtr<ImageData> paintRenderingResultsToImageData();
114 // Immediately releases ownership of all resources. Call upon loss of the
115 // graphics context to prevent freeing invalid resources.
116 void discardResources();
118 #if USE(ACCELERATED_COMPOSITING)
119 PlatformLayer* platformLayer();
120 void prepareBackBuffer();
121 bool requiresCopyFromBackToFrontBuffer() const;
122 unsigned frontColorBuffer() const;
123 void paintCompositedResultsToCanvas(CanvasRenderingContext*);
126 PassRefPtr<GraphicsContext3D> graphicsContext3D() const { return m_context; }
129 DrawingBuffer(GraphicsContext3D*, const IntSize&, bool multisampleExtensionSupported,
130 bool packedDepthStencilExtensionSupported, PreserveDrawingBuffer, AlphaRequirement);
132 void initialize(const IntSize&);
134 PreserveDrawingBuffer m_preserveDrawingBuffer;
135 AlphaRequirement m_alpha;
136 bool m_scissorEnabled;
137 Platform3DObject m_texture2DBinding;
138 GC3Denum m_activeTextureUnit;
140 RefPtr<GraphicsContext3D> m_context;
142 bool m_multisampleExtensionSupported;
143 bool m_packedDepthStencilExtensionSupported;
144 Platform3DObject m_fbo;
145 Platform3DObject m_colorBuffer;
146 Platform3DObject m_frontColorBuffer;
147 bool m_separateFrontTexture;
149 // This is used when we have OES_packed_depth_stencil.
150 Platform3DObject m_depthStencilBuffer;
152 // These are used when we don't.
153 Platform3DObject m_depthBuffer;
154 Platform3DObject m_stencilBuffer;
157 Platform3DObject m_multisampleFBO;
158 Platform3DObject m_multisampleColorBuffer;
160 #if PLATFORM(CHROMIUM)
161 RefPtr<WebGLLayerChromium> m_platformLayer;
165 RetainPtr<WebGLLayer> m_platformLayer;
169 } // namespace WebCore
171 #endif // DrawingBuffer_h