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