2 * Copyright (C) 2009 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #ifndef WebGLFramebuffer_h
27 #define WebGLFramebuffer_h
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"
37 class WebGLRenderbuffer;
40 class WebGLFramebuffer FINAL : public WebGLContextObject, public ScriptWrappable {
42 class WebGLAttachment : public RefCounted<WebGLAttachment> {
44 virtual ~WebGLAttachment();
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;
65 virtual ~WebGLFramebuffer();
67 static PassRefPtr<WebGLFramebuffer> create(WebGLRenderingContext*);
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;
77 GLenum colorBufferFormat() const;
78 GLsizei colorBufferWidth() const;
79 GLsizei colorBufferHeight() const;
81 // This should always be called before drawArray, drawElements, clear,
82 // readPixels, copyTexImage2D, copyTexSubImage2D if this framebuffer is
84 // Return false if the framebuffer is incomplete.
85 bool onAccess(blink::WebGraphicsContext3D*, const char** reason);
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;
93 bool hasEverBeenBound() const { return object() && m_hasEverBeenBound; }
95 void setHasEverBeenBound() { m_hasEverBeenBound = true; }
97 bool hasStencilBuffer() const;
99 // Wrapper for drawBuffersEXT/drawBuffersARB to work around a driver bug.
100 void drawBuffers(const Vector<GLenum>& bufs);
102 GLenum getDrawBuffer(GLenum);
105 WebGLFramebuffer(WebGLRenderingContext*);
107 virtual void deleteObjectImpl(blink::WebGraphicsContext3D*, Platform3DObject) OVERRIDE;
110 WebGLAttachment* getAttachment(GLenum) const;
111 bool isAttachmentComplete(WebGLAttachment* attachedObject, GLenum attachment, const char** reason) const;
113 // Check if the framebuffer is currently bound.
114 bool isBound() const;
116 // attach 'attachment' at 'attachmentPoint'.
117 void attach(GLenum attachment, GLenum attachmentPoint);
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);
122 typedef WTF::HashMap<GLenum, RefPtr<WebGLAttachment> > AttachmentMap;
124 AttachmentMap m_attachments;
126 bool m_hasEverBeenBound;
128 Vector<GLenum> m_drawBuffers;
129 Vector<GLenum> m_filteredDrawBuffers;
132 } // namespace WebCore
134 #endif // WebGLFramebuffer_h