• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "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/Deque.h"
45 #include "wtf/Noncopyable.h"
46 #include "wtf/OwnPtr.h"
47 #include "wtf/PassOwnPtr.h"
48 
49 namespace blink {
50 class WebExternalBitmap;
51 class WebExternalTextureLayer;
52 class WebGraphicsContext3D;
53 class WebLayer;
54 }
55 
56 namespace WebCore {
57 class Extensions3DUtil;
58 class ImageData;
59 class ImageBuffer;
60 
61 // Abstract interface to allow basic context eviction management
62 class PLATFORM_EXPORT ContextEvictionManager : public RefCounted<ContextEvictionManager> {
63 public:
~ContextEvictionManager()64     virtual ~ContextEvictionManager() {};
65 
66     virtual void forciblyLoseOldestContext(const String& reason) = 0;
67     virtual IntSize oldestContextSize() = 0;
68 };
69 
70 // Manages a rendering target (framebuffer + attachment) for a canvas.  Can publish its rendering
71 // results to a blink::WebLayer for compositing.
72 class PLATFORM_EXPORT DrawingBuffer : public RefCounted<DrawingBuffer>, public blink::WebExternalTextureLayerClient  {
73     // If we used CHROMIUM_image as the backing storage for our buffers,
74     // we need to know the mapping from texture id to image.
75     struct TextureInfo {
76         Platform3DObject textureId;
77         blink::WGC3Duint imageId;
78 
TextureInfoTextureInfo79         TextureInfo()
80             : textureId(0)
81             , imageId(0)
82         {
83         }
84     };
85 
86     struct MailboxInfo : public RefCounted<MailboxInfo> {
87         blink::WebExternalTextureMailbox mailbox;
88         TextureInfo textureInfo;
89         IntSize size;
90         // This keeps the parent drawing buffer alive as long as the compositor is
91         // referring to one of the mailboxes DrawingBuffer produced. The parent drawing buffer is
92         // cleared when the compositor returns the mailbox. See mailboxReleased().
93         RefPtr<DrawingBuffer> m_parentDrawingBuffer;
94     };
95 public:
96     enum PreserveDrawingBuffer {
97         Preserve,
98         Discard
99     };
100 
101     static PassRefPtr<DrawingBuffer> create(PassOwnPtr<blink::WebGraphicsContext3D>, const IntSize&, PreserveDrawingBuffer, blink::WebGraphicsContext3D::Attributes requestedAttributes, PassRefPtr<ContextEvictionManager>);
102 
103     virtual ~DrawingBuffer();
104 
105     // Destruction will be completed after all mailboxes are released.
106     void beginDestruction();
107 
108     // Issues a glClear() on all framebuffers associated with this DrawingBuffer. The caller is responsible for
109     // making the context current and setting the clear values and masks. Modifies the framebuffer binding.
110     void clearFramebuffers(GLbitfield clearMask);
111 
112     // Given the desired buffer size, provides the largest dimensions that will fit in the pixel budget.
113     static IntSize adjustSize(const IntSize& desiredSize, const IntSize& curSize, int maxTextureSize);
114     bool reset(const IntSize&);
115     void bind();
size()116     IntSize size() const { return m_size; }
117 
118     // Copies the multisample color buffer to the normal color buffer and leaves m_fbo bound.
119     void commit(long x = 0, long y = 0, long width = -1, long height = -1);
120 
121     // commit should copy the full multisample buffer, and not respect the
122     // current scissor bounds. Track the state of the scissor test so that it
123     // can be disabled during calls to commit.
setScissorEnabled(bool scissorEnabled)124     void setScissorEnabled(bool scissorEnabled) { m_scissorEnabled = scissorEnabled; }
125 
126     // The DrawingBuffer needs to track the texture bound to texture unit 0.
127     // The bound texture is tracked to avoid costly queries during rendering.
setTexture2DBinding(Platform3DObject texture)128     void setTexture2DBinding(Platform3DObject texture) { m_texture2DBinding = texture; }
129 
130     // The DrawingBuffer needs to track the currently bound framebuffer so it
131     // restore the binding when needed.
setFramebufferBinding(Platform3DObject fbo)132     void setFramebufferBinding(Platform3DObject fbo) { m_framebufferBinding = fbo; }
133 
134     // Track the currently active texture unit. Texture unit 0 is used as host for a scratch
135     // texture.
setActiveTextureUnit(GLint textureUnit)136     void setActiveTextureUnit(GLint textureUnit) { m_activeTextureUnit = textureUnit; }
137 
138     bool multisample() const;
139 
140     Platform3DObject framebuffer() const;
141 
142     void markContentsChanged();
143     void markLayerComposited();
144     bool layerComposited() const;
145 
146     blink::WebLayer* platformLayer();
147     void paintCompositedResultsToCanvas(ImageBuffer*);
148 
149     blink::WebGraphicsContext3D* context();
150 
151     // Returns the actual context attributes for this drawing buffer which may differ from the
152     // requested context attributes due to implementation limits.
getActualAttributes()153     blink::WebGraphicsContext3D::Attributes getActualAttributes() const { return m_actualAttributes; }
154 
155     // WebExternalTextureLayerClient implementation.
156     virtual bool prepareMailbox(blink::WebExternalTextureMailbox*, blink::WebExternalBitmap*) OVERRIDE;
157     virtual void mailboxReleased(const blink::WebExternalTextureMailbox&) OVERRIDE;
158 
159     // Destroys the TEXTURE_2D binding for the owned context
160     bool copyToPlatformTexture(blink::WebGraphicsContext3D*, Platform3DObject texture, GLenum internalFormat,
161         GLenum destType, GLint level, bool premultiplyAlpha, bool flipY, bool fromFrontBuffer = false);
162 
163     void setPackAlignment(GLint param);
164 
165     void paintRenderingResultsToCanvas(ImageBuffer*);
166     PassRefPtr<Uint8ClampedArray> paintRenderingResultsToImageData(int&, int&);
167 
168 protected: // For unittests
169     DrawingBuffer(
170         PassOwnPtr<blink::WebGraphicsContext3D>,
171         PassOwnPtr<Extensions3DUtil>,
172         bool multisampleExtensionSupported,
173         bool packedDepthStencilExtensionSupported,
174         PreserveDrawingBuffer,
175         blink::WebGraphicsContext3D::Attributes requestedAttributes,
176         PassRefPtr<ContextEvictionManager>);
177 
178     bool initialize(const IntSize&);
179 
180 private:
181     void mailboxReleasedWhileDestructionInProgress(const blink::WebExternalTextureMailbox&);
182 
183     unsigned createColorTexture();
184     // Create the depth/stencil and multisample buffers, if needed.
185     void createSecondaryBuffers();
186     bool resizeFramebuffer(const IntSize&);
187     bool resizeMultisampleFramebuffer(const IntSize&);
188     void resizeDepthStencil(const IntSize&);
189 
190     // Bind to the m_framebufferBinding if it's not 0.
191     void restoreFramebufferBinding();
192 
193     void clearPlatformLayer();
194 
195     PassRefPtr<MailboxInfo> recycledMailbox();
196     PassRefPtr<MailboxInfo> createNewMailbox(const TextureInfo&);
197     void deleteMailbox(const blink::WebExternalTextureMailbox&);
198 
199     // Updates the current size of the buffer, ensuring that s_currentResourceUsePixels is updated.
200     void setSize(const IntSize& size);
201 
202     // Calculates the difference in pixels between the current buffer size and the proposed size.
203     static int pixelDelta(const IntSize& newSize, const IntSize& curSize);
204 
205     // Given the desired buffer size, provides the largest dimensions that will fit in the pixel budget
206     // Returns true if the buffer will only fit if the oldest WebGL context is forcibly lost
207     IntSize adjustSizeWithContextEviction(const IntSize&, bool& evictContext);
208 
209     void paintFramebufferToCanvas(int framebuffer, int width, int height, bool premultiplyAlpha, ImageBuffer*);
210 
211     // This is the order of bytes to use when doing a readback.
212     enum ReadbackOrder {
213         ReadbackRGBA,
214         ReadbackSkia
215     };
216 
217     // Helper function which does a readback from the currently-bound
218     // framebuffer into a buffer of a certain size with 4-byte pixels.
219     void readBackFramebuffer(unsigned char* pixels, int width, int height, ReadbackOrder, WebGLImageConversion::AlphaOp);
220 
221     // Helper function to flip a bitmap vertically.
222     void flipVertically(uint8_t* data, int width, int height);
223 
224     // Helper to texImage2D with pixel==0 case: pixels are initialized to 0.
225     // By default, alignment is 4, the OpenGL default setting.
226     void texImage2DResourceSafe(GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, GLint alignment = 4);
227     // Allocate buffer storage to be sent to compositor using either texImage2D or CHROMIUM_image based on available support.
228     void allocateTextureMemory(TextureInfo*, const IntSize&);
229     void deleteChromiumImageForTexture(TextureInfo*);
230 
231     PreserveDrawingBuffer m_preserveDrawingBuffer;
232     bool m_scissorEnabled;
233     Platform3DObject m_texture2DBinding;
234     Platform3DObject m_framebufferBinding;
235     GLenum m_activeTextureUnit;
236 
237     OwnPtr<blink::WebGraphicsContext3D> m_context;
238     OwnPtr<Extensions3DUtil> m_extensionsUtil;
239     IntSize m_size;
240     blink::WebGraphicsContext3D::Attributes m_requestedAttributes;
241     bool m_multisampleExtensionSupported;
242     bool m_packedDepthStencilExtensionSupported;
243     Platform3DObject m_fbo;
244     // DrawingBuffer's output is double-buffered. m_colorBuffer is the back buffer.
245     TextureInfo m_colorBuffer;
246     TextureInfo m_frontColorBuffer;
247 
248     // This is used when we have OES_packed_depth_stencil.
249     Platform3DObject m_depthStencilBuffer;
250 
251     // These are used when we don't.
252     Platform3DObject m_depthBuffer;
253     Platform3DObject m_stencilBuffer;
254 
255     // For multisampling.
256     Platform3DObject m_multisampleFBO;
257     Platform3DObject m_multisampleColorBuffer;
258 
259     // True if our contents have been modified since the last presentation of this buffer.
260     bool m_contentsChanged;
261 
262     // True if commit() has been called since the last time markContentsChanged() had been called.
263     bool m_contentsChangeCommitted;
264     bool m_layerComposited;
265 
266     enum MultisampleMode {
267         None,
268         ImplicitResolve,
269         ExplicitResolve,
270     };
271 
272     MultisampleMode m_multisampleMode;
273 
274     blink::WebGraphicsContext3D::Attributes m_actualAttributes;
275     unsigned m_internalColorFormat;
276     unsigned m_colorFormat;
277     unsigned m_internalRenderbufferFormat;
278     int m_maxTextureSize;
279     int m_sampleCount;
280     int m_packAlignment;
281     bool m_destructionInProgress;
282 
283     OwnPtr<blink::WebExternalTextureLayer> m_layer;
284 
285     // All of the mailboxes that this DrawingBuffer has ever created.
286     Vector<RefPtr<MailboxInfo> > m_textureMailboxes;
287     // Mailboxes that were released by the compositor can be used again by this DrawingBuffer.
288     Deque<blink::WebExternalTextureMailbox> m_recycledMailboxQueue;
289 
290     RefPtr<ContextEvictionManager> m_contextEvictionManager;
291 
292     // If the width and height of the Canvas's backing store don't
293     // match those that we were given in the most recent call to
294     // reshape(), then we need an intermediate bitmap to read back the
295     // frame buffer into. This seems to happen when CSS styles are
296     // used to resize the Canvas.
297     SkBitmap m_resizingBitmap;
298 
299     // Used to flip a bitmap vertically.
300     Vector<uint8_t> m_scanline;
301 };
302 
303 } // namespace WebCore
304 
305 #endif // DrawingBuffer_h
306