#ifndef FilterOperation_h
#define FilterOperation_h
#if ENABLE(CSS_FILTERS)
#include "Color.h"
#include "FilterEffect.h"
#include "LayoutSize.h"
#include "Length.h"
#include <wtf/OwnPtr.h>
#include <wtf/PassOwnPtr.h>
#include <wtf/RefCounted.h>
#include <wtf/text/WTFString.h>
#ifdef PASSTHROUGH
#undef PASSTHROUGH
#endif
namespace WebCore {
class CachedSVGDocumentReference;
class FilterOperation : public RefCounted<FilterOperation> {
public:
enum OperationType {
REFERENCE, GRAYSCALE,
SEPIA,
SATURATE,
HUE_ROTATE,
INVERT,
OPACITY,
BRIGHTNESS,
CONTRAST,
BLUR,
DROP_SHADOW,
PASSTHROUGH,
DEFAULT,
NONE
};
virtual ~FilterOperation() { }
virtual PassRefPtr<FilterOperation> clone() const = 0;
virtual bool operator==(const FilterOperation&) const = 0;
bool operator!=(const FilterOperation& o) const { return !(*this == o); }
virtual PassRefPtr<FilterOperation> blend(const FilterOperation* , double , bool = false)
{
ASSERT(!blendingNeedsRendererSize());
return 0;
}
virtual PassRefPtr<FilterOperation> blend(const FilterOperation* , double , const LayoutSize&, bool = false)
{
ASSERT(blendingNeedsRendererSize());
return 0;
}
OperationType type() const { return m_type; }
bool isBasicColorMatrixFilterOperation() const
{
return m_type == GRAYSCALE || m_type == SEPIA || m_type == SATURATE || m_type == HUE_ROTATE;
}
bool isBasicComponentTransferFilterOperation() const
{
return m_type == INVERT || m_type == BRIGHTNESS || m_type == CONTRAST || m_type == OPACITY;
}
bool isSameType(const FilterOperation& o) const { return o.type() == m_type; }
virtual bool affectsOpacity() const { return false; }
virtual bool movesPixels() const { return false; }
virtual bool blendingNeedsRendererSize() const { return false; }
protected:
FilterOperation(OperationType type)
: m_type(type)
{
}
OperationType m_type;
};
#define FILTEROPERATION_TYPE_CASTS(ToValueTypeName, predicate) \
TYPE_CASTS_BASE(ToValueTypeName, WebCore::FilterOperation, value, value->predicate, value.predicate)
class DefaultFilterOperation : public FilterOperation {
public:
static PassRefPtr<DefaultFilterOperation> create(OperationType representedType)
{
return adoptRef(new DefaultFilterOperation(representedType));
}
virtual PassRefPtr<FilterOperation> clone() const override
{
return adoptRef(new DefaultFilterOperation(representedType()));
}
OperationType representedType() const { return m_representedType; }
private:
virtual bool operator==(const FilterOperation&) const override;
DefaultFilterOperation(OperationType representedType)
: FilterOperation(DEFAULT)
, m_representedType(representedType)
{
}
OperationType m_representedType;
};
FILTEROPERATION_TYPE_CASTS(DefaultFilterOperation, type() == FilterOperation::DEFAULT);
class PassthroughFilterOperation : public FilterOperation {
public:
static PassRefPtr<PassthroughFilterOperation> create()
{
return adoptRef(new PassthroughFilterOperation());
}
virtual PassRefPtr<FilterOperation> clone() const override
{
return adoptRef(new PassthroughFilterOperation());
}
private:
virtual bool operator==(const FilterOperation& o) const override
{
return isSameType(o);
}
PassthroughFilterOperation()
: FilterOperation(PASSTHROUGH)
{
}
};
FILTEROPERATION_TYPE_CASTS(PassthroughFilterOperation, type() == FilterOperation::PASSTHROUGH);
class ReferenceFilterOperation : public FilterOperation {
public:
static PassRefPtr<ReferenceFilterOperation> create(const String& url, const String& fragment)
{
return adoptRef(new ReferenceFilterOperation(url, fragment));
}
virtual ~ReferenceFilterOperation();
virtual PassRefPtr<FilterOperation> clone() const override
{
ASSERT_NOT_REACHED();
return nullptr;
}
virtual bool affectsOpacity() const override { return true; }
virtual bool movesPixels() const override { return true; }
const String& url() const { return m_url; }
const String& fragment() const { return m_fragment; }
CachedSVGDocumentReference* cachedSVGDocumentReference() const { return m_cachedSVGDocumentReference.get(); }
CachedSVGDocumentReference* getOrCreateCachedSVGDocumentReference();
FilterEffect* filterEffect() const { return m_filterEffect.get(); }
void setFilterEffect(PassRefPtr<FilterEffect> filterEffect) { m_filterEffect = filterEffect; }
private:
ReferenceFilterOperation(const String& url, const String& fragment);
virtual bool operator==(const FilterOperation&) const override;
String m_url;
String m_fragment;
std::unique_ptr<CachedSVGDocumentReference> m_cachedSVGDocumentReference;
RefPtr<FilterEffect> m_filterEffect;
};
FILTEROPERATION_TYPE_CASTS(ReferenceFilterOperation, type() == FilterOperation::REFERENCE);
class BasicColorMatrixFilterOperation : public FilterOperation {
public:
static PassRefPtr<BasicColorMatrixFilterOperation> create(double amount, OperationType type)
{
return adoptRef(new BasicColorMatrixFilterOperation(amount, type));
}
virtual PassRefPtr<FilterOperation> clone() const override
{
return adoptRef(new BasicColorMatrixFilterOperation(amount(), type()));
}
double amount() const { return m_amount; }
virtual PassRefPtr<FilterOperation> blend(const FilterOperation* from, double progress, bool blendToPassthrough = false) override;
private:
virtual bool operator==(const FilterOperation&) const override;
double passthroughAmount() const;
BasicColorMatrixFilterOperation(double amount, OperationType type)
: FilterOperation(type)
, m_amount(amount)
{
}
double m_amount;
};
FILTEROPERATION_TYPE_CASTS(BasicColorMatrixFilterOperation, isBasicColorMatrixFilterOperation());
class BasicComponentTransferFilterOperation : public FilterOperation {
public:
static PassRefPtr<BasicComponentTransferFilterOperation> create(double amount, OperationType type)
{
return adoptRef(new BasicComponentTransferFilterOperation(amount, type));
}
virtual PassRefPtr<FilterOperation> clone() const override
{
return adoptRef(new BasicComponentTransferFilterOperation(amount(), type()));
}
double amount() const { return m_amount; }
virtual bool affectsOpacity() const override { return m_type == OPACITY; }
virtual PassRefPtr<FilterOperation> blend(const FilterOperation* from, double progress, bool blendToPassthrough = false) override;
private:
virtual bool operator==(const FilterOperation&) const override;
double passthroughAmount() const;
BasicComponentTransferFilterOperation(double amount, OperationType type)
: FilterOperation(type)
, m_amount(amount)
{
}
double m_amount;
};
FILTEROPERATION_TYPE_CASTS(BasicComponentTransferFilterOperation, isBasicComponentTransferFilterOperation());
class BlurFilterOperation : public FilterOperation {
public:
static PassRefPtr<BlurFilterOperation> create(Length stdDeviation)
{
return adoptRef(new BlurFilterOperation(WTF::move(stdDeviation)));
}
virtual PassRefPtr<FilterOperation> clone() const override
{
return adoptRef(new BlurFilterOperation(stdDeviation()));
}
const Length& stdDeviation() const { return m_stdDeviation; }
virtual bool affectsOpacity() const override { return true; }
virtual bool movesPixels() const override { return true; }
virtual PassRefPtr<FilterOperation> blend(const FilterOperation* from, double progress, bool blendToPassthrough = false) override;
private:
virtual bool operator==(const FilterOperation&) const override;
BlurFilterOperation(Length stdDeviation)
: FilterOperation(BLUR)
, m_stdDeviation(WTF::move(stdDeviation))
{
}
Length m_stdDeviation;
};
FILTEROPERATION_TYPE_CASTS(BlurFilterOperation, type() == FilterOperation::BLUR);
class DropShadowFilterOperation : public FilterOperation {
public:
static PassRefPtr<DropShadowFilterOperation> create(const IntPoint& location, int stdDeviation, Color color)
{
return adoptRef(new DropShadowFilterOperation(location, stdDeviation, color));
}
virtual PassRefPtr<FilterOperation> clone() const override
{
return adoptRef(new DropShadowFilterOperation(location(), stdDeviation(), color()));
}
int x() const { return m_location.x(); }
int y() const { return m_location.y(); }
IntPoint location() const { return m_location; }
int stdDeviation() const { return m_stdDeviation; }
Color color() const { return m_color; }
virtual bool affectsOpacity() const override { return true; }
virtual bool movesPixels() const override { return true; }
virtual PassRefPtr<FilterOperation> blend(const FilterOperation* from, double progress, bool blendToPassthrough = false) override;
private:
virtual bool operator==(const FilterOperation&) const override;
DropShadowFilterOperation(const IntPoint& location, int stdDeviation, Color color)
: FilterOperation(DROP_SHADOW)
, m_location(location)
, m_stdDeviation(stdDeviation)
, m_color(color)
{
}
IntPoint m_location; int m_stdDeviation;
Color m_color;
};
FILTEROPERATION_TYPE_CASTS(DropShadowFilterOperation, type() == FilterOperation::DROP_SHADOW);
}
#endif // ENABLE(CSS_FILTERS)
#endif // FilterOperation_h