Home | History | Annotate | Download | only in rendering
      1 /*
      2  * Copyright (C) 2011 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 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 FilterEffectRenderer_h
     27 #define FilterEffectRenderer_h
     28 
     29 #include "core/svg/graphics/filters/SVGFilterBuilder.h"
     30 #include "platform/geometry/FloatRect.h"
     31 #include "platform/geometry/IntRectExtent.h"
     32 #include "platform/geometry/LayoutRect.h"
     33 #include "platform/graphics/GraphicsContext.h"
     34 #include "platform/graphics/ImageBuffer.h"
     35 #include "platform/graphics/filters/Filter.h"
     36 #include "platform/graphics/filters/FilterEffect.h"
     37 #include "platform/graphics/filters/FilterOperations.h"
     38 #include "platform/graphics/filters/SourceGraphic.h"
     39 #include "wtf/PassRefPtr.h"
     40 #include "wtf/RefCounted.h"
     41 #include "wtf/RefPtr.h"
     42 
     43 namespace WebCore {
     44 
     45 class ShaderResource;
     46 class CustomFilterProgram;
     47 class Document;
     48 class GraphicsContext;
     49 class RenderLayer;
     50 class RenderObject;
     51 
     52 class FilterEffectRendererHelper {
     53 public:
     54     FilterEffectRendererHelper(bool haveFilterEffect)
     55         : m_savedGraphicsContext(0)
     56         , m_renderLayer(0)
     57         , m_haveFilterEffect(haveFilterEffect)
     58     {
     59     }
     60 
     61     bool haveFilterEffect() const { return m_haveFilterEffect; }
     62     bool hasStartedFilterEffect() const { return m_savedGraphicsContext; }
     63 
     64     bool prepareFilterEffect(RenderLayer*, const LayoutRect& filterBoxRect, const LayoutRect& dirtyRect, const LayoutRect& layerRepaintRect);
     65     GraphicsContext* beginFilterEffect(GraphicsContext* oldContext);
     66     GraphicsContext* applyFilterEffect();
     67 
     68     const LayoutRect& repaintRect() const { return m_repaintRect; }
     69 private:
     70     GraphicsContext* m_savedGraphicsContext;
     71     RenderLayer* m_renderLayer;
     72 
     73     LayoutRect m_repaintRect;
     74     bool m_haveFilterEffect;
     75 };
     76 
     77 class FilterEffectRenderer : public Filter
     78 {
     79     WTF_MAKE_FAST_ALLOCATED;
     80 public:
     81     static PassRefPtr<FilterEffectRenderer> create()
     82     {
     83         return adoptRef(new FilterEffectRenderer());
     84     }
     85 
     86     void setSourceImageRect(const FloatRect& sourceImageRect)
     87     {
     88         m_sourceDrawingRegion = sourceImageRect;
     89         m_graphicsBufferAttached = false;
     90     }
     91     virtual FloatRect sourceImageRect() const { return m_sourceDrawingRegion; }
     92 
     93     GraphicsContext* inputContext();
     94     ImageBuffer* output() const { return lastEffect()->asImageBuffer(); }
     95 
     96     bool build(RenderObject* renderer, const FilterOperations&);
     97     bool updateBackingStoreRect(const FloatRect& filterRect);
     98     void allocateBackingStoreIfNeeded();
     99     void clearIntermediateResults();
    100     void apply();
    101 
    102     IntRect outputRect() const { return lastEffect()->hasResult() ? lastEffect()->absolutePaintRect() : IntRect(); }
    103 
    104     bool hasFilterThatMovesPixels() const { return m_hasFilterThatMovesPixels; }
    105     LayoutRect computeSourceImageRectForDirtyRect(const LayoutRect& filterBoxRect, const LayoutRect& dirtyRect);
    106 
    107     bool hasCustomShaderFilter() const { return m_hasCustomShaderFilter; }
    108     PassRefPtr<FilterEffect> lastEffect() const
    109     {
    110         return m_lastEffect;
    111     }
    112 private:
    113 
    114     FilterEffectRenderer();
    115     virtual ~FilterEffectRenderer();
    116 
    117     FloatRect m_sourceDrawingRegion;
    118 
    119     RefPtr<SourceGraphic> m_sourceGraphic;
    120     RefPtr<FilterEffect> m_lastEffect;
    121 
    122     IntRectExtent m_outsets;
    123 
    124     bool m_graphicsBufferAttached;
    125     bool m_hasFilterThatMovesPixels;
    126     bool m_hasCustomShaderFilter;
    127 };
    128 
    129 } // namespace WebCore
    130 
    131 
    132 #endif // FilterEffectRenderer_h
    133