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 "GraphicsLayer.h"
36#include "IntSize.h"
37
38#include <wtf/Noncopyable.h>
39#include <wtf/OwnPtr.h>
40#include <wtf/PassOwnPtr.h>
41#if PLATFORM(MAC)
42#include <wtf/RetainPtr.h>
43#endif
44
45#if ENABLE(SKIA_GPU)
46class GrContext;
47struct GrPlatformSurfaceDesc;
48#endif
49
50namespace WebCore {
51
52#if PLATFORM(CHROMIUM)
53struct DrawingBufferInternal;
54#endif
55
56// Manages a rendering target (framebuffer + attachment) for a canvas.  Can publish its rendering
57// results to a PlatformLayer for compositing.
58class DrawingBuffer : public RefCounted<DrawingBuffer> {
59public:
60    friend class GraphicsContext3D;
61
62    ~DrawingBuffer();
63
64    void clearFramebuffer();
65
66    // Returns true if the buffer was successfully resized.
67    bool reset(const IntSize&);
68    void bind();
69    IntSize size() const { return m_size; }
70    Platform3DObject colorBuffer() const { return m_colorBuffer; }
71
72    // Clear all resources from this object, as well as context. Called when context is destroyed
73    // to prevent invalid accesses to the resources.
74    void clear();
75
76    // Create the depth/stencil and multisample buffers, if needed.
77    void createSecondaryBuffers();
78
79    void resizeDepthStencil(int sampleCount);
80
81    // Copies the multisample color buffer to the normal color buffer and leaves m_fbo bound
82    void commit(long x = 0, long y = 0, long width = -1, long height = -1);
83
84    bool multisample() const { return m_context && m_context->getContextAttributes().antialias && m_multisampleExtensionSupported; }
85
86    Platform3DObject platformColorBuffer() const;
87
88#if USE(ACCELERATED_COMPOSITING)
89    PlatformLayer* platformLayer();
90    void publishToPlatformLayer();
91#endif
92
93#if PLATFORM(CHROMIUM)
94    class WillPublishCallback {
95        WTF_MAKE_NONCOPYABLE(WillPublishCallback);
96    public:
97        WillPublishCallback() { }
98        virtual ~WillPublishCallback() { }
99
100        virtual void willPublish() = 0;
101    };
102
103    void setWillPublishCallback(PassOwnPtr<WillPublishCallback> callback) { m_callback = callback; }
104#endif
105
106#if ENABLE(SKIA_GPU)
107    void setGrContext(GrContext* ctx);
108    void getGrPlatformSurfaceDesc(GrPlatformSurfaceDesc*);
109#endif
110
111    PassRefPtr<GraphicsContext3D> graphicsContext3D() const { return m_context; }
112
113private:
114    static PassRefPtr<DrawingBuffer> create(GraphicsContext3D*, const IntSize&);
115
116    DrawingBuffer(GraphicsContext3D*, const IntSize&, bool multisampleExtensionSupported, bool packedDepthStencilExtensionSupported);
117
118    // Platform specific function called after reset() so each platform can do extra work if needed
119    void didReset();
120
121    RefPtr<GraphicsContext3D> m_context;
122    IntSize m_size;
123    bool m_multisampleExtensionSupported;
124    bool m_packedDepthStencilExtensionSupported;
125    Platform3DObject m_fbo;
126    Platform3DObject m_colorBuffer;
127
128    // This is used when we have OES_packed_depth_stencil.
129    Platform3DObject m_depthStencilBuffer;
130
131    // These are used when we don't.
132    Platform3DObject m_depthBuffer;
133    Platform3DObject m_stencilBuffer;
134
135    // For multisampling
136    Platform3DObject m_multisampleFBO;
137    Platform3DObject m_multisampleColorBuffer;
138
139#if PLATFORM(CHROMIUM)
140    OwnPtr<WillPublishCallback> m_callback;
141    OwnPtr<DrawingBufferInternal> m_internal;
142#endif
143
144#if PLATFORM(MAC)
145    RetainPtr<WebGLLayer> m_platformLayer;
146#endif
147
148#if ENABLE(SKIA_GPU)
149    GrContext* m_grContext;
150#endif
151};
152
153} // namespace WebCore
154
155#endif // DrawingBuffer_h
156