1 /*
2  * Copyright (C) 2009 Apple 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
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #ifndef WebGLFramebuffer_h
27 #define WebGLFramebuffer_h
28 
29 #include "WebGLObject.h"
30 
31 #include <wtf/PassRefPtr.h>
32 #include <wtf/RefCounted.h>
33 
34 namespace WebCore {
35 
36 class WebGLRenderbuffer;
37 class WebGLTexture;
38 
39 class WebGLFramebuffer : public WebGLObject {
40 public:
~WebGLFramebuffer()41     virtual ~WebGLFramebuffer() { deleteObject(); }
42 
43     static PassRefPtr<WebGLFramebuffer> create(WebGLRenderingContext*);
44 
45     void setAttachment(GC3Denum attachment, GC3Denum texTarget, WebGLTexture*, GC3Dint level);
46     void setAttachment(GC3Denum attachment, WebGLRenderbuffer*);
47     // If an object is attached to the framebuffer, remove it.
48     void removeAttachment(WebGLObject*);
49     WebGLObject* getAttachment(GC3Denum) const;
50 
51     GC3Denum getColorBufferFormat() const;
52     GC3Dsizei getWidth() const;
53     GC3Dsizei getHeight() const;
54 
55     // This should always be called before drawArray, drawElements, clear,
56     // readPixels, copyTexImage2D, copyTexSubImage2D if this framebuffer is
57     // currently bound.
58     // Return false if the framebuffer is incomplete; otherwise initialize
59     // the buffers if they haven't been initialized and
60     // needToInitializeRenderbuffers is true.
61     bool onAccess(bool needToInitializeRenderbuffers);
62 
63     // Return false does not mean COMPLETE, might still be INCOMPLETE.
64     bool isIncomplete(bool checkInternalFormat) const;
65 
hasEverBeenBound()66     bool hasEverBeenBound() const { return object() && m_hasEverBeenBound; }
67 
setHasEverBeenBound()68     void setHasEverBeenBound() { m_hasEverBeenBound = true; }
69 
70 protected:
71     WebGLFramebuffer(WebGLRenderingContext*);
72 
73     virtual void deleteObjectImpl(Platform3DObject);
74 
75 private:
isFramebuffer()76     virtual bool isFramebuffer() const { return true; }
77 
78     // Return false if framebuffer is incomplete.
79     bool initializeRenderbuffers();
80 
isColorAttached()81     bool isColorAttached() const { return (m_colorAttachment && m_colorAttachment->object()); }
isDepthAttached()82     bool isDepthAttached() const { return (m_depthAttachment && m_depthAttachment->object()); }
isStencilAttached()83     bool isStencilAttached() const { return (m_stencilAttachment && m_stencilAttachment->object()); }
isDepthStencilAttached()84     bool isDepthStencilAttached() const { return (m_depthStencilAttachment && m_depthStencilAttachment->object()); }
85 
86     RefPtr<WebGLObject> m_colorAttachment;
87     RefPtr<WebGLObject> m_depthAttachment;
88     RefPtr<WebGLObject> m_stencilAttachment;
89     RefPtr<WebGLObject> m_depthStencilAttachment;
90 
91     bool m_hasEverBeenBound;
92 
93     GC3Denum m_texTarget;
94     GC3Dint m_texLevel;
95 };
96 
97 } // namespace WebCore
98 
99 #endif // WebGLFramebuffer_h
100