Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / html / canvas / WebGLFramebuffer.h
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 "bindings/v8/ScriptWrappable.h"
30 #include "core/html/canvas/WebGLContextObject.h"
31 #include "core/html/canvas/WebGLSharedObject.h"
32 #include "wtf/PassRefPtr.h"
33 #include "wtf/RefCounted.h"
34
35 namespace WebCore {
36
37 class WebGLRenderbuffer;
38 class WebGLTexture;
39
40 class WebGLFramebuffer FINAL : public WebGLContextObject, public ScriptWrappable {
41 public:
42     class WebGLAttachment : public RefCounted<WebGLAttachment> {
43     public:
44         virtual ~WebGLAttachment();
45
46         virtual GLsizei width() const = 0;
47         virtual GLsizei height() const = 0;
48         virtual GLenum format() const = 0;
49         // For texture attachment, type() returns the type of the attached texture.
50         // For renderbuffer attachment, the type of the renderbuffer may vary with GL implementation.
51         // To avoid confusion, it would be better to not implement type() for renderbuffer attachment and
52         // we should always use the internalformat of the renderbuffer and avoid using type() API.
53         virtual GLenum type() const = 0;
54         virtual WebGLSharedObject* object() const = 0;
55         virtual bool isSharedObject(WebGLSharedObject*) const = 0;
56         virtual bool valid() const = 0;
57         virtual void onDetached(blink::WebGraphicsContext3D*) = 0;
58         virtual void attach(blink::WebGraphicsContext3D*, GLenum attachment) = 0;
59         virtual void unattach(blink::WebGraphicsContext3D*, GLenum attachment) = 0;
60
61     protected:
62         WebGLAttachment();
63     };
64
65     virtual ~WebGLFramebuffer();
66
67     static PassRefPtr<WebGLFramebuffer> create(WebGLRenderingContext*);
68
69     void setAttachmentForBoundFramebuffer(GLenum attachment, GLenum texTarget, WebGLTexture*, GLint level);
70     void setAttachmentForBoundFramebuffer(GLenum attachment, WebGLRenderbuffer*);
71     // If an object is attached to the currently bound framebuffer, remove it.
72     void removeAttachmentFromBoundFramebuffer(WebGLSharedObject*);
73     // If a given attachment point for the currently bound framebuffer is not null, remove the attached object.
74     void removeAttachmentFromBoundFramebuffer(GLenum);
75     WebGLSharedObject* getAttachmentObject(GLenum) const;
76
77     GLenum colorBufferFormat() const;
78     GLsizei colorBufferWidth() const;
79     GLsizei colorBufferHeight() const;
80
81     // This should always be called before drawArray, drawElements, clear,
82     // readPixels, copyTexImage2D, copyTexSubImage2D if this framebuffer is
83     // currently bound.
84     // Return false if the framebuffer is incomplete.
85     bool onAccess(blink::WebGraphicsContext3D*, const char** reason);
86
87     // Software version of glCheckFramebufferStatus(), except that when
88     // FRAMEBUFFER_COMPLETE is returned, it is still possible for
89     // glCheckFramebufferStatus() to return FRAMEBUFFER_UNSUPPORTED,
90     // depending on hardware implementation.
91     GLenum checkStatus(const char** reason) const;
92
93     bool hasEverBeenBound() const { return object() && m_hasEverBeenBound; }
94
95     void setHasEverBeenBound() { m_hasEverBeenBound = true; }
96
97     bool hasStencilBuffer() const;
98
99     // Wrapper for drawBuffersEXT/drawBuffersARB to work around a driver bug.
100     void drawBuffers(const Vector<GLenum>& bufs);
101
102     GLenum getDrawBuffer(GLenum);
103
104 protected:
105     WebGLFramebuffer(WebGLRenderingContext*);
106
107     virtual void deleteObjectImpl(blink::WebGraphicsContext3D*, Platform3DObject) OVERRIDE;
108
109 private:
110     WebGLAttachment* getAttachment(GLenum) const;
111     bool isAttachmentComplete(WebGLAttachment* attachedObject, GLenum attachment, const char** reason) const;
112
113     // Check if the framebuffer is currently bound.
114     bool isBound() const;
115
116     // attach 'attachment' at 'attachmentPoint'.
117     void attach(GLenum attachment, GLenum attachmentPoint);
118
119     // Check if a new drawBuffers call should be issued. This is called when we add or remove an attachment.
120     void drawBuffersIfNecessary(bool force);
121
122     typedef WTF::HashMap<GLenum, RefPtr<WebGLAttachment> > AttachmentMap;
123
124     AttachmentMap m_attachments;
125
126     bool m_hasEverBeenBound;
127
128     Vector<GLenum> m_drawBuffers;
129     Vector<GLenum> m_filteredDrawBuffers;
130 };
131
132 } // namespace WebCore
133
134 #endif // WebGLFramebuffer_h