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 "platform/PlatformExport.h"
35 #include "platform/geometry/IntSize.h"
36 #include "platform/graphics/GraphicsTypes3D.h"
37 #include "platform/graphics/gpu/WebGLImageConversion.h"
38 #include "public/platform/WebExternalTextureLayerClient.h"
39 #include "public/platform/WebExternalTextureMailbox.h"
40 #include "public/platform/WebGraphicsContext3D.h"
41 #include "third_party/khronos/GLES2/gl2.h"
42 #include "third_party/khronos/GLES2/gl2ext.h"
43 #include "third_party/skia/include/core/SkBitmap.h"
44 #include "wtf/Noncopyable.h"
45 #include "wtf/OwnPtr.h"
46 #include "wtf/PassOwnPtr.h"
49 class WebExternalBitmap;
50 class WebExternalTextureLayer;
51 class WebGraphicsContext3D;
59 // Abstract interface to allow basic context eviction management
60 class PLATFORM_EXPORT ContextEvictionManager : public RefCounted<ContextEvictionManager> {
62 virtual ~ContextEvictionManager() {};
64 virtual void forciblyLoseOldestContext(const String& reason) = 0;
65 virtual IntSize oldestContextSize() = 0;
68 // Manages a rendering target (framebuffer + attachment) for a canvas. Can publish its rendering
69 // results to a blink::WebLayer for compositing.
70 class PLATFORM_EXPORT DrawingBuffer : public RefCounted<DrawingBuffer>, public blink::WebExternalTextureLayerClient {
71 struct MailboxInfo : public RefCounted<MailboxInfo> {
72 blink::WebExternalTextureMailbox mailbox;
77 enum PreserveDrawingBuffer {
82 static PassRefPtr<DrawingBuffer> create(blink::WebGraphicsContext3D*, const IntSize&, PreserveDrawingBuffer, PassRefPtr<ContextEvictionManager>);
84 virtual ~DrawingBuffer();
86 // Clear all resources from this object, as well as context. Called when context is destroyed
87 // to prevent invalid accesses to the resources.
88 void releaseResources();
90 // Issues a glClear() on all framebuffers associated with this DrawingBuffer. The caller is responsible for
91 // making the context current and setting the clear values and masks. Modifies the framebuffer binding.
92 void clearFramebuffers(GLbitfield clearMask);
94 // Given the desired buffer size, provides the largest dimensions that will fit in the pixel budget.
95 IntSize adjustSize(const IntSize&);
96 void reset(const IntSize&);
98 IntSize size() const { return m_size; }
99 bool isZeroSized() const { return m_size.isEmpty(); }
101 // Copies the multisample color buffer to the normal color buffer and leaves m_fbo bound.
102 void commit(long x = 0, long y = 0, long width = -1, long height = -1);
104 // commit should copy the full multisample buffer, and not respect the
105 // current scissor bounds. Track the state of the scissor test so that it
106 // can be disabled during calls to commit.
107 void setScissorEnabled(bool scissorEnabled) { m_scissorEnabled = scissorEnabled; }
109 // The DrawingBuffer needs to track the texture bound to texture unit 0.
110 // The bound texture is tracked to avoid costly queries during rendering.
111 void setTexture2DBinding(Platform3DObject texture) { m_texture2DBinding = texture; }
113 // The DrawingBuffer needs to track the currently bound framebuffer so it
114 // restore the binding when needed.
115 void setFramebufferBinding(Platform3DObject fbo) { m_framebufferBinding = fbo; }
117 // Track the currently active texture unit. Texture unit 0 is used as host for a scratch
119 void setActiveTextureUnit(GLint textureUnit) { m_activeTextureUnit = textureUnit; }
121 bool multisample() const;
123 Platform3DObject framebuffer() const;
125 void markContentsChanged();
126 void markLayerComposited();
127 bool layerComposited() const;
129 blink::WebLayer* platformLayer();
130 void paintCompositedResultsToCanvas(ImageBuffer*);
132 // WebExternalTextureLayerClient implementation.
133 virtual blink::WebGraphicsContext3D* context() OVERRIDE;
134 virtual bool prepareMailbox(blink::WebExternalTextureMailbox*, blink::WebExternalBitmap*) OVERRIDE;
135 virtual void mailboxReleased(const blink::WebExternalTextureMailbox&) OVERRIDE;
137 bool copyToPlatformTexture(blink::WebGraphicsContext3D*, Platform3DObject texture, GLenum internalFormat,
138 GLenum destType, GLint level, bool premultiplyAlpha, bool flipY);
140 void setPackAlignment(GLint param);
142 void paintRenderingResultsToCanvas(ImageBuffer*);
143 PassRefPtr<Uint8ClampedArray> paintRenderingResultsToImageData(int&, int&);
146 DrawingBuffer(blink::WebGraphicsContext3D*, const IntSize&, bool multisampleExtensionSupported,
147 bool packedDepthStencilExtensionSupported, PreserveDrawingBuffer, PassRefPtr<ContextEvictionManager>);
149 void initialize(const IntSize&);
151 unsigned createColorTexture(const IntSize& size = IntSize());
152 // Create the depth/stencil and multisample buffers, if needed.
153 void createSecondaryBuffers();
154 bool resizeFramebuffer(const IntSize&);
155 bool resizeMultisampleFramebuffer(const IntSize&);
156 void resizeDepthStencil(const IntSize&);
158 // Bind to the m_framebufferBinding if it's not 0.
159 void restoreFramebufferBinding();
161 void clearPlatformLayer();
163 PassRefPtr<MailboxInfo> recycledMailbox();
164 PassRefPtr<MailboxInfo> createNewMailbox(unsigned);
166 // Updates the current size of the buffer, ensuring that s_currentResourceUsePixels is updated.
167 void setSize(const IntSize& size);
169 // Calculates the difference in pixels between the current buffer size and the proposed size.
170 int pixelDelta(const IntSize& size);
172 // Given the desired buffer size, provides the largest dimensions that will fit in the pixel budget
173 // Returns true if the buffer will only fit if the oldest WebGL context is forcibly lost
174 IntSize adjustSizeWithContextEviction(const IntSize&, bool& evictContext);
176 void paintFramebufferToCanvas(int framebuffer, int width, int height, bool premultiplyAlpha, ImageBuffer*);
178 // This is the order of bytes to use when doing a readback.
184 // Helper function which does a readback from the currently-bound
185 // framebuffer into a buffer of a certain size with 4-byte pixels.
186 void readBackFramebuffer(unsigned char* pixels, int width, int height, ReadbackOrder, WebGLImageConversion::AlphaOp);
188 // Helper function to flip a bitmap vertically.
189 void flipVertically(uint8_t* data, int width, int height);
191 // Helper to texImage2D with pixel==0 case: pixels are initialized to 0.
192 // By default, alignment is 4, the OpenGL default setting.
193 void texImage2DResourceSafe(GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, GLint alignment = 4);
195 PreserveDrawingBuffer m_preserveDrawingBuffer;
196 bool m_scissorEnabled;
197 Platform3DObject m_texture2DBinding;
198 Platform3DObject m_framebufferBinding;
199 GLenum m_activeTextureUnit;
201 blink::WebGraphicsContext3D* m_context;
203 bool m_multisampleExtensionSupported;
204 bool m_packedDepthStencilExtensionSupported;
205 Platform3DObject m_fbo;
206 // DrawingBuffer's output is double-buffered. m_colorBuffer is the back buffer.
207 Platform3DObject m_colorBuffer;
208 Platform3DObject m_frontColorBuffer;
210 // This is used when we have OES_packed_depth_stencil.
211 Platform3DObject m_depthStencilBuffer;
213 // These are used when we don't.
214 Platform3DObject m_depthBuffer;
215 Platform3DObject m_stencilBuffer;
217 // For multisampling.
218 Platform3DObject m_multisampleFBO;
219 Platform3DObject m_multisampleColorBuffer;
221 // True if our contents have been modified since the last presentation of this buffer.
222 bool m_contentsChanged;
224 // True if commit() has been called since the last time markContentsChanged() had been called.
225 bool m_contentsChangeCommitted;
226 bool m_layerComposited;
228 blink::WebGraphicsContext3D::Attributes m_attributes;
229 unsigned m_internalColorFormat;
230 unsigned m_colorFormat;
231 unsigned m_internalRenderbufferFormat;
232 int m_maxTextureSize;
236 OwnPtr<blink::WebExternalTextureLayer> m_layer;
238 // All of the mailboxes that this DrawingBuffer has ever created.
239 Vector<RefPtr<MailboxInfo> > m_textureMailboxes;
240 // Mailboxes that were released by the compositor and can be used again by this DrawingBuffer.
241 Vector<RefPtr<MailboxInfo> > m_recycledMailboxes;
243 RefPtr<ContextEvictionManager> m_contextEvictionManager;
245 // If the width and height of the Canvas's backing store don't
246 // match those that we were given in the most recent call to
247 // reshape(), then we need an intermediate bitmap to read back the
248 // frame buffer into. This seems to happen when CSS styles are
249 // used to resize the Canvas.
250 SkBitmap m_resizingBitmap;
252 // Used to flip a bitmap vertically.
253 Vector<uint8_t> m_scanline;
256 } // namespace WebCore
258 #endif // DrawingBuffer_h