OLD | NEW |
| (Empty) |
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 "core/platform/graphics/GraphicsContext3D.h" | |
35 #include "platform/geometry/IntSize.h" | |
36 #include "platform/graphics/GraphicsTypes3D.h" | |
37 | |
38 #include "public/platform/WebExternalTextureLayerClient.h" | |
39 #include "public/platform/WebExternalTextureMailbox.h" | |
40 #include "wtf/Noncopyable.h" | |
41 #include "wtf/OwnPtr.h" | |
42 #include "wtf/PassOwnPtr.h" | |
43 | |
44 namespace blink { | |
45 class WebExternalBitmap; | |
46 class WebExternalTextureLayer; | |
47 class WebGraphicsContext3D; | |
48 class WebLayer; | |
49 } | |
50 | |
51 namespace WebCore { | |
52 class GraphicsContext3D; | |
53 class ImageData; | |
54 | |
55 // Abstract interface to allow basic context eviction management | |
56 class ContextEvictionManager : public RefCounted<ContextEvictionManager> { | |
57 public: | |
58 virtual ~ContextEvictionManager() {}; | |
59 | |
60 virtual void forciblyLoseOldestContext(const String& reason) = 0; | |
61 virtual IntSize oldestContextSize() = 0; | |
62 }; | |
63 | |
64 // Manages a rendering target (framebuffer + attachment) for a canvas. Can publ
ish its rendering | |
65 // results to a blink::WebLayer for compositing. | |
66 class DrawingBuffer : public RefCounted<DrawingBuffer>, public blink::WebExterna
lTextureLayerClient { | |
67 struct MailboxInfo : public RefCounted<MailboxInfo> { | |
68 blink::WebExternalTextureMailbox mailbox; | |
69 unsigned textureId; | |
70 IntSize size; | |
71 }; | |
72 public: | |
73 enum PreserveDrawingBuffer { | |
74 Preserve, | |
75 Discard | |
76 }; | |
77 | |
78 static PassRefPtr<DrawingBuffer> create(GraphicsContext3D*, const IntSize&,
PreserveDrawingBuffer, PassRefPtr<ContextEvictionManager>); | |
79 | |
80 ~DrawingBuffer(); | |
81 | |
82 // Clear all resources from this object, as well as context. Called when con
text is destroyed | |
83 // to prevent invalid accesses to the resources. | |
84 void releaseResources(); | |
85 | |
86 // Issues a glClear() on all framebuffers associated with this DrawingBuffer
. The caller is responsible for | |
87 // making the context current and setting the clear values and masks. Modifi
es the framebuffer binding. | |
88 void clearFramebuffers(GC3Dbitfield clearMask); | |
89 | |
90 // Given the desired buffer size, provides the largest dimensions that will
fit in the pixel budget. | |
91 IntSize adjustSize(const IntSize&); | |
92 void reset(const IntSize&); | |
93 void bind(); | |
94 IntSize size() const { return m_size; } | |
95 bool isZeroSized() const { return m_size.isEmpty(); } | |
96 | |
97 // Copies the multisample color buffer to the normal color buffer and leaves
m_fbo bound. | |
98 void commit(long x = 0, long y = 0, long width = -1, long height = -1); | |
99 | |
100 // commit should copy the full multisample buffer, and not respect the | |
101 // current scissor bounds. Track the state of the scissor test so that it | |
102 // can be disabled during calls to commit. | |
103 void setScissorEnabled(bool scissorEnabled) { m_scissorEnabled = scissorEnab
led; } | |
104 | |
105 // The DrawingBuffer needs to track the texture bound to texture unit 0. | |
106 // The bound texture is tracked to avoid costly queries during rendering. | |
107 void setTexture2DBinding(Platform3DObject texture) { m_texture2DBinding = te
xture; } | |
108 | |
109 // The DrawingBuffer needs to track the currently bound framebuffer so it | |
110 // restore the binding when needed. | |
111 void setFramebufferBinding(Platform3DObject fbo) { m_framebufferBinding = fb
o; } | |
112 | |
113 // Track the currently active texture unit. Texture unit 0 is used as host f
or a scratch | |
114 // texture. | |
115 void setActiveTextureUnit(GC3Dint textureUnit) { m_activeTextureUnit = textu
reUnit; } | |
116 | |
117 bool multisample() const; | |
118 | |
119 Platform3DObject framebuffer() const; | |
120 | |
121 void markContentsChanged(); | |
122 | |
123 blink::WebLayer* platformLayer(); | |
124 void paintCompositedResultsToCanvas(ImageBuffer*); | |
125 | |
126 // WebExternalTextureLayerClient implementation. | |
127 virtual blink::WebGraphicsContext3D* context() OVERRIDE; | |
128 virtual bool prepareMailbox(blink::WebExternalTextureMailbox*, blink::WebExt
ernalBitmap*) OVERRIDE; | |
129 virtual void mailboxReleased(const blink::WebExternalTextureMailbox&) OVERRI
DE; | |
130 | |
131 bool copyToPlatformTexture(GraphicsContext3D&, Platform3DObject texture, GC3
Denum internalFormat, | |
132 GC3Denum destType, GC3Dint level, bool premultiplyAlpha, bool flipY); | |
133 | |
134 private: | |
135 DrawingBuffer(GraphicsContext3D*, const IntSize&, bool multisampleExtensionS
upported, | |
136 bool packedDepthStencilExtensionSupported, PreserveDrawingBuff
er, PassRefPtr<ContextEvictionManager>); | |
137 | |
138 void initialize(const IntSize&); | |
139 | |
140 Platform3DObject frontColorBuffer() const; | |
141 Platform3DObject colorBuffer() const { return m_colorBuffer; } | |
142 | |
143 unsigned createColorTexture(const IntSize& size = IntSize()); | |
144 // Create the depth/stencil and multisample buffers, if needed. | |
145 void createSecondaryBuffers(); | |
146 bool resizeFramebuffer(const IntSize&); | |
147 bool resizeMultisampleFramebuffer(const IntSize&); | |
148 void resizeDepthStencil(const IntSize&, int sampleCount); | |
149 | |
150 // Bind to the m_framebufferBinding if it's not 0. | |
151 void restoreFramebufferBinding(); | |
152 | |
153 void clearPlatformLayer(); | |
154 | |
155 PassRefPtr<MailboxInfo> recycledMailbox(); | |
156 PassRefPtr<MailboxInfo> createNewMailbox(unsigned); | |
157 | |
158 // Updates the current size of the buffer, ensuring that s_currentResourceUs
ePixels is updated. | |
159 void setSize(const IntSize& size); | |
160 | |
161 // Calculates the difference in pixels between the current buffer size and t
he proposed size. | |
162 int pixelDelta(const IntSize& size); | |
163 | |
164 // Given the desired buffer size, provides the largest dimensions that will
fit in the pixel budget | |
165 // Returns true if the buffer will only fit if the oldest WebGL context is f
orcibly lost | |
166 IntSize adjustSizeWithContextEviction(const IntSize&, bool& evictContext); | |
167 | |
168 PreserveDrawingBuffer m_preserveDrawingBuffer; | |
169 bool m_scissorEnabled; | |
170 Platform3DObject m_texture2DBinding; | |
171 Platform3DObject m_framebufferBinding; | |
172 GC3Denum m_activeTextureUnit; | |
173 | |
174 RefPtr<GraphicsContext3D> m_context; | |
175 IntSize m_size; | |
176 bool m_multisampleExtensionSupported; | |
177 bool m_packedDepthStencilExtensionSupported; | |
178 Platform3DObject m_fbo; | |
179 Platform3DObject m_colorBuffer; | |
180 Platform3DObject m_frontColorBuffer; | |
181 | |
182 // This is used when we have OES_packed_depth_stencil. | |
183 Platform3DObject m_depthStencilBuffer; | |
184 | |
185 // These are used when we don't. | |
186 Platform3DObject m_depthBuffer; | |
187 Platform3DObject m_stencilBuffer; | |
188 | |
189 // For multisampling. | |
190 Platform3DObject m_multisampleFBO; | |
191 Platform3DObject m_multisampleColorBuffer; | |
192 | |
193 // True if our contents have been modified since the last presentation of th
is buffer. | |
194 bool m_contentsChanged; | |
195 | |
196 // True if commit() has been called since the last time markContentsChanged(
) had been called. | |
197 bool m_contentsChangeCommitted; | |
198 | |
199 GraphicsContext3D::Attributes m_attributes; | |
200 unsigned m_internalColorFormat; | |
201 unsigned m_colorFormat; | |
202 unsigned m_internalRenderbufferFormat; | |
203 int m_maxTextureSize; | |
204 | |
205 OwnPtr<blink::WebExternalTextureLayer> m_layer; | |
206 | |
207 // All of the mailboxes that this DrawingBuffer has ever created. | |
208 Vector<RefPtr<MailboxInfo> > m_textureMailboxes; | |
209 // Mailboxes that were released by the compositor and can be used again by t
his DrawingBuffer. | |
210 Vector<RefPtr<MailboxInfo> > m_recycledMailboxes; | |
211 RefPtr<MailboxInfo> m_lastColorBuffer; | |
212 | |
213 RefPtr<ContextEvictionManager> m_contextEvictionManager; | |
214 }; | |
215 | |
216 } // namespace WebCore | |
217 | |
218 #endif // DrawingBuffer_h | |
OLD | NEW |