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#ifndef DrawingBuffer_h 32#define DrawingBuffer_h 33 34#include "GraphicsContext3D.h" 35#include "GraphicsTypes3D.h" 36#include "IntSize.h" 37#include "PlatformLayer.h" 38 39#include <wtf/Noncopyable.h> 40#include <wtf/OwnPtr.h> 41#include <wtf/PassOwnPtr.h> 42#if PLATFORM(MAC) 43#include <wtf/RetainPtr.h> 44#endif 45 46namespace WebCore { 47class GraphicsContext3D; 48class ImageData; 49 50// Manages a rendering target (framebuffer + attachment) for a canvas. Can publish its rendering 51// results to a PlatformLayer for compositing. 52class DrawingBuffer : public RefCounted<DrawingBuffer> { 53public: 54 enum PreserveDrawingBuffer { 55 Preserve, 56 Discard 57 }; 58 59 enum AlphaRequirement { 60 Alpha, 61 Opaque 62 }; 63 64 static PassRefPtr<DrawingBuffer> create(GraphicsContext3D*, const IntSize&, PreserveDrawingBuffer, AlphaRequirement); 65 friend class GraphicsContext3D; 66 67 ~DrawingBuffer(); 68 69 // Issues a glClear() on all framebuffers associated with this DrawingBuffer. The caller is responsible for 70 // making the context current and setting the clear values and masks. Modifies the framebuffer binding. 71 void clearFramebuffers(GC3Dbitfield clearMask); 72 73 // Returns true if the buffer was successfully resized. 74 bool reset(const IntSize&); 75 void bind(); 76 IntSize size() const { return m_size; } 77 Platform3DObject colorBuffer() const { return m_colorBuffer; } 78 79 // Clear all resources from this object, as well as context. Called when context is destroyed 80 // to prevent invalid accesses to the resources. 81 void clear(); 82 83 // Create the depth/stencil and multisample buffers, if needed. 84 void createSecondaryBuffers(); 85 86 void resizeDepthStencil(int sampleCount); 87 88 // Copies the multisample color buffer to the normal color buffer and leaves m_fbo bound 89 void commit(long x = 0, long y = 0, long width = -1, long height = -1); 90 91 // commit should copy the full multisample buffer, and not respect the 92 // current scissor bounds. Track the state of the scissor test so that it 93 // can be disabled during calls to commit. 94 void setScissorEnabled(bool scissorEnabled) { m_scissorEnabled = scissorEnabled; } 95 96 // The DrawingBuffer needs to track the texture bound to texture unit 0. 97 // The bound texture is tracked to avoid costly queries during rendering. 98 void setTexture2DBinding(Platform3DObject texture) { m_texture2DBinding = texture; } 99 100 // The DrawingBuffer needs to track the currently bound framebuffer so it 101 // restore the binding when needed. 102 void setFramebufferBinding(Platform3DObject fbo) { m_framebufferBinding = fbo; } 103 104 // Bind to the m_framebufferBinding if it's not 0. 105 void restoreFramebufferBinding(); 106 107 // Track the currently active texture unit. Texture unit 0 is used as host for a scratch 108 // texture. 109 void setActiveTextureUnit(GC3Dint textureUnit) { m_activeTextureUnit = textureUnit; } 110 111 bool multisample() const; 112 113 Platform3DObject framebuffer() const; 114 115 PassRefPtr<ImageData> paintRenderingResultsToImageData(); 116 117 // Immediately releases ownership of all resources. Call upon loss of the 118 // graphics context to prevent freeing invalid resources. 119 void discardResources(); 120 121 void markContentsChanged() { m_contentsChanged = true; } 122 123#if USE(ACCELERATED_COMPOSITING) 124 PlatformLayer* platformLayer(); 125 void prepareBackBuffer(); 126 bool requiresCopyFromBackToFrontBuffer() const; 127 unsigned frontColorBuffer() const; 128 void paintCompositedResultsToCanvas(ImageBuffer*); 129 void clearPlatformLayer(); 130#endif 131 132 GraphicsContext3D* graphicsContext3D() const { return m_context.get(); } 133 134private: 135 DrawingBuffer(GraphicsContext3D*, const IntSize&, bool multisampleExtensionSupported, 136 bool packedDepthStencilExtensionSupported, PreserveDrawingBuffer, AlphaRequirement); 137 138 void initialize(const IntSize&); 139 140 bool checkBufferIntegrity(); 141 142 PreserveDrawingBuffer m_preserveDrawingBuffer; 143 AlphaRequirement m_alpha; 144 bool m_scissorEnabled; 145 Platform3DObject m_texture2DBinding; 146 Platform3DObject m_framebufferBinding; 147 GC3Denum m_activeTextureUnit; 148 149 RefPtr<GraphicsContext3D> m_context; 150 IntSize m_size; 151 bool m_multisampleExtensionSupported; 152 bool m_packedDepthStencilExtensionSupported; 153 Platform3DObject m_fbo; 154 Platform3DObject m_colorBuffer; 155 Platform3DObject m_frontColorBuffer; 156 bool m_separateFrontTexture; 157 158 // This is used when we have OES_packed_depth_stencil. 159 Platform3DObject m_depthStencilBuffer; 160 161 // These are used when we don't. 162 Platform3DObject m_depthBuffer; 163 Platform3DObject m_stencilBuffer; 164 165 // For multisampling 166 Platform3DObject m_multisampleFBO; 167 Platform3DObject m_multisampleColorBuffer; 168 169 // True if our contents have been modified since the last presentation of this buffer. 170 bool m_contentsChanged; 171 172#if PLATFORM(MAC) 173 RetainPtr<WebGLLayer> m_platformLayer; 174#endif 175}; 176 177} // namespace WebCore 178 179#endif // DrawingBuffer_h 180