Home | History | Annotate | Download | only in html
      1 /*
      2  * Copyright (C) 2004, 2006, 2009, 2010 Apple Inc. All rights reserved.
      3  * Copyright (C) 2007 Alp Toker <alp (at) atoker.com>
      4  * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     16  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     18  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     22  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     23  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #ifndef HTMLCanvasElement_h
     29 #define HTMLCanvasElement_h
     30 
     31 #include "core/dom/Document.h"
     32 #include "core/html/HTMLElement.h"
     33 #include "core/html/canvas/CanvasImageSource.h"
     34 #include "platform/geometry/FloatRect.h"
     35 #include "platform/geometry/IntSize.h"
     36 #include "platform/graphics/Canvas2DLayerBridge.h"
     37 #include "platform/graphics/GraphicsTypes.h"
     38 #include "platform/graphics/ImageBufferClient.h"
     39 #include "platform/heap/Handle.h"
     40 #include "wtf/Forward.h"
     41 
     42 #define CanvasDefaultInterpolationQuality InterpolationLow
     43 
     44 namespace WebCore {
     45 
     46 class AffineTransform;
     47 class CanvasContextAttributes;
     48 class CanvasRenderingContext;
     49 class GraphicsContext;
     50 class GraphicsContextStateSaver;
     51 class HTMLCanvasElement;
     52 class Image;
     53 class ImageData;
     54 class ImageBuffer;
     55 class ImageBufferSurface;
     56 class IntSize;
     57 
     58 class CanvasObserver : public WillBeGarbageCollectedMixin {
     59     DECLARE_EMPTY_VIRTUAL_DESTRUCTOR_WILL_BE_REMOVED(CanvasObserver);
     60 public:
     61     virtual void canvasChanged(HTMLCanvasElement*, const FloatRect& changedRect) = 0;
     62     virtual void canvasResized(HTMLCanvasElement*) = 0;
     63 #if !ENABLE(OILPAN)
     64     virtual void canvasDestroyed(HTMLCanvasElement*) = 0;
     65 #endif
     66 
     67     virtual void trace(Visitor*) { }
     68 };
     69 
     70 class HTMLCanvasElement FINAL : public HTMLElement, public DocumentVisibilityObserver, public CanvasImageSource, public ImageBufferClient {
     71     WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(HTMLCanvasElement);
     72 public:
     73     DECLARE_NODE_FACTORY(HTMLCanvasElement);
     74     virtual ~HTMLCanvasElement();
     75 
     76     void addObserver(CanvasObserver*);
     77     void removeObserver(CanvasObserver*);
     78 
     79     // Attributes and functions exposed to script
     80     int width() const { return size().width(); }
     81     int height() const { return size().height(); }
     82 
     83     const IntSize& size() const { return m_size; }
     84 
     85     void setWidth(int);
     86     void setHeight(int);
     87     void setAccelerationDisabled(bool accelerationDisabled) { m_accelerationDisabled = accelerationDisabled; }
     88     bool accelerationDisabled() const { return m_accelerationDisabled; }
     89 
     90     void setSize(const IntSize& newSize)
     91     {
     92         if (newSize == size())
     93             return;
     94         m_ignoreReset = true;
     95         setWidth(newSize.width());
     96         setHeight(newSize.height());
     97         m_ignoreReset = false;
     98         reset();
     99     }
    100 
    101     CanvasRenderingContext* getContext(const String&, CanvasContextAttributes* attributes = 0);
    102 
    103     static String toEncodingMimeType(const String& mimeType);
    104     String toDataURL(const String& mimeType, const double* quality, ExceptionState&) const;
    105     String toDataURL(const String& mimeType, ExceptionState& exceptionState) const { return toDataURL(mimeType, 0, exceptionState); }
    106 
    107     // Used for rendering
    108     void didDraw(const FloatRect&);
    109     void notifyObserversCanvasChanged(const FloatRect&);
    110 
    111     void paint(GraphicsContext*, const LayoutRect&);
    112 
    113     GraphicsContext* drawingContext() const;
    114     GraphicsContext* existingDrawingContext() const;
    115 
    116     CanvasRenderingContext* renderingContext() const { return m_context.get(); }
    117 
    118     void ensureUnacceleratedImageBuffer();
    119     ImageBuffer* buffer() const;
    120     Image* copiedImage() const;
    121     void clearCopiedImage();
    122     PassRefPtrWillBeRawPtr<ImageData> getImageData() const;
    123     void makePresentationCopy();
    124     void clearPresentationCopy();
    125 
    126     SecurityOrigin* securityOrigin() const;
    127     bool originClean() const { return m_originClean; }
    128     void setOriginTainted() { m_originClean = false; }
    129 
    130     AffineTransform baseTransform() const;
    131 
    132     bool is3D() const;
    133 
    134     bool hasImageBuffer() const { return m_imageBuffer; }
    135     bool hasValidImageBuffer() const;
    136     void discardImageBuffer();
    137 
    138     bool shouldAccelerate(const IntSize&) const;
    139 
    140     virtual const AtomicString imageSourceURL() const OVERRIDE;
    141 
    142     virtual InsertionNotificationRequest insertedInto(ContainerNode*) OVERRIDE;
    143 
    144     // DocumentVisibilityObserver implementation
    145     virtual void didChangeVisibilityState(PageVisibilityState) OVERRIDE;
    146 
    147     // CanvasImageSource implementation
    148     virtual PassRefPtr<Image> getSourceImageForCanvas(SourceImageMode, SourceImageStatus*) const OVERRIDE;
    149     virtual bool wouldTaintOrigin(SecurityOrigin*) const OVERRIDE;
    150     virtual FloatSize sourceSize() const OVERRIDE;
    151 
    152     // ImageBufferClient implementation
    153     virtual void notifySurfaceInvalid() OVERRIDE;
    154 
    155     virtual void trace(Visitor*) OVERRIDE;
    156 
    157 protected:
    158     virtual void didMoveToNewDocument(Document& oldDocument) OVERRIDE;
    159 
    160 private:
    161     explicit HTMLCanvasElement(Document&);
    162 
    163     virtual void parseAttribute(const QualifiedName&, const AtomicString&) OVERRIDE;
    164     virtual RenderObject* createRenderer(RenderStyle*) OVERRIDE;
    165     virtual bool areAuthorShadowsAllowed() const OVERRIDE { return false; }
    166 
    167     void reset();
    168 
    169     PassOwnPtr<ImageBufferSurface> createImageBufferSurface(const IntSize& deviceSize, int* msaaSampleCount);
    170     void createImageBuffer();
    171     void createImageBufferInternal();
    172     void clearImageBuffer();
    173 
    174     void setSurfaceSize(const IntSize&);
    175 
    176     bool paintsIntoCanvasBuffer() const;
    177 
    178     void updateExternallyAllocatedMemory() const;
    179 
    180     String toDataURLInternal(const String& mimeType, const double* quality, bool isSaving = false) const;
    181 
    182     WillBeHeapHashSet<RawPtrWillBeWeakMember<CanvasObserver> > m_observers;
    183 
    184     IntSize m_size;
    185 
    186     OwnPtrWillBeMember<CanvasRenderingContext> m_context;
    187 
    188     bool m_ignoreReset;
    189     bool m_accelerationDisabled;
    190     FloatRect m_dirtyRect;
    191 
    192     mutable intptr_t m_externallyAllocatedMemory;
    193 
    194     bool m_originClean;
    195 
    196     // It prevents HTMLCanvasElement::buffer() from continuously re-attempting to allocate an imageBuffer
    197     // after the first attempt failed.
    198     mutable bool m_didFailToCreateImageBuffer;
    199     mutable bool m_didClearImageBuffer;
    200     OwnPtr<ImageBuffer> m_imageBuffer;
    201     mutable OwnPtr<GraphicsContextStateSaver> m_contextStateSaver;
    202 
    203     mutable RefPtr<Image> m_presentedImage;
    204     mutable RefPtr<Image> m_copiedImage; // FIXME: This is temporary for platforms that have to copy the image buffer to render (and for CSSCanvasValue).
    205 };
    206 
    207 } //namespace
    208 
    209 #endif
    210