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/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(WebGLRenderingContextBase*);
     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 
     79     // This should always be called before drawArray, drawElements, clear,
     80     // readPixels, copyTexImage2D, copyTexSubImage2D if this framebuffer is
     81     // currently bound.
     82     // Return false if the framebuffer is incomplete.
     83     bool onAccess(blink::WebGraphicsContext3D*, const char** reason);
     84 
     85     // Software version of glCheckFramebufferStatus(), except that when
     86     // FRAMEBUFFER_COMPLETE is returned, it is still possible for
     87     // glCheckFramebufferStatus() to return FRAMEBUFFER_UNSUPPORTED,
     88     // depending on hardware implementation.
     89     GLenum checkStatus(const char** reason) const;
     90 
     91     bool hasEverBeenBound() const { return object() && m_hasEverBeenBound; }
     92 
     93     void setHasEverBeenBound() { m_hasEverBeenBound = true; }
     94 
     95     bool hasStencilBuffer() const;
     96 
     97     // Wrapper for drawBuffersEXT/drawBuffersARB to work around a driver bug.
     98     void drawBuffers(const Vector<GLenum>& bufs);
     99 
    100     GLenum getDrawBuffer(GLenum);
    101 
    102 protected:
    103     WebGLFramebuffer(WebGLRenderingContextBase*);
    104 
    105     virtual void deleteObjectImpl(blink::WebGraphicsContext3D*, Platform3DObject) OVERRIDE;
    106 
    107 private:
    108     WebGLAttachment* getAttachment(GLenum) const;
    109     bool isAttachmentComplete(WebGLAttachment* attachedObject, GLenum attachment, const char** reason) const;
    110 
    111     // Check if the framebuffer is currently bound.
    112     bool isBound() const;
    113 
    114     // attach 'attachment' at 'attachmentPoint'.
    115     void attach(GLenum attachment, GLenum attachmentPoint);
    116 
    117     // Check if a new drawBuffers call should be issued. This is called when we add or remove an attachment.
    118     void drawBuffersIfNecessary(bool force);
    119 
    120     typedef WTF::HashMap<GLenum, RefPtr<WebGLAttachment> > AttachmentMap;
    121 
    122     AttachmentMap m_attachments;
    123 
    124     bool m_hasEverBeenBound;
    125 
    126     Vector<GLenum> m_drawBuffers;
    127     Vector<GLenum> m_filteredDrawBuffers;
    128 };
    129 
    130 } // namespace WebCore
    131 
    132 #endif // WebGLFramebuffer_h
    133