1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef MOZILLA_GFX_FILTERNODEWEBGL_H_
#define MOZILLA_GFX_FILTERNODEWEBGL_H_
#include <vector>
#include "mozilla/gfx/Filters.h"
#include "mozilla/gfx/PatternHelpers.h"
namespace mozilla::gfx {
class DrawTargetWebgl;
class FilterNodeSoftware;
/**
* FilterNodeWebgl wraps a FilterNodeSoftware for most operations that
* are not yet accelerated. To provide acceleration, this must be subclassed
* to override an optimized implementation for particular operations.
*/
class FilterNodeWebgl : public FilterNode {
public:
~FilterNodeWebgl() override;
FilterBackend GetBackendType() override { return FILTER_BACKEND_WEBGL; }
FilterType GetType() const { return mType; }
bool ReserveInputIndex(uint32_t aIndex);
bool SetInputAccel(uint32_t aIndex, SourceSurface* aSurface);
bool SetInputSoftware(uint32_t aIndex, SourceSurface* aSurface);
void SetInput(uint32_t aIndex, SourceSurface* aSurface) override;
void SetInput(uint32_t aIndex, FilterNode* aFilter) override;
void SetAttribute(uint32_t aIndex, bool) override;
void SetAttribute(uint32_t aIndex, uint32_t) override;
void SetAttribute(uint32_t aIndex, Float) override;
void SetAttribute(uint32_t aIndex, const Size&) override;
void SetAttribute(uint32_t aIndex, const IntSize&) override;
void SetAttribute(uint32_t aIndex, const IntPoint&) override;
void SetAttribute(uint32_t aIndex, const Rect&) override;
void SetAttribute(uint32_t aIndex, const IntRect&) override;
void SetAttribute(uint32_t aIndex, const Point&) override;
void SetAttribute(uint32_t aIndex, const Matrix&) override;
void SetAttribute(uint32_t aIndex, const Matrix5x4&) override;
void SetAttribute(uint32_t aIndex, const Point3D&) override;
void SetAttribute(uint32_t aIndex, const DeviceColor&) override;
void SetAttribute(uint32_t aIndex, const Float* aValues,
uint32_t aSize) override;
IntRect MapRectToSource(const IntRect& aRect, const IntRect& aMax,
FilterNode* aSourceNode) override;
// Draw the root of a filter chain. Any filter drawing originates here.
virtual void Draw(DrawTargetWebgl* aDT, const Rect& aSourceRect,
const Point& aDestPoint, const DrawOptions& aOptions,
FilterNodeWebgl* aParent = nullptr);
// Speculatively draw part of a filter chain, only if it can be accelerated.
// On success, it return true. Instead of falling back, nothing is drawn and
// it returns false.
virtual bool DrawAccel(DrawTargetWebgl* aDT, const Rect& aSourceRect,
const Point& aDestPoint, const DrawOptions& aOptions,
FilterNodeWebgl* aParent = nullptr) {
return false;
}
// Draw a child filter and return a surface that can be processed by a parent
// filter. This will try to accelerate the filter if possible, but implements
// a fallback if not.
virtual already_AddRefed<SourceSurface> DrawChild(
FilterNodeWebgl* aParent, DrawTargetWebgl* aDT, const Rect& aSourceRect,
const DrawOptions& aOptions, Point& aSurfaceOffset, DeviceColor& aColor);
// This handles deferred filter inputs that should not be resolved to surfaces
// until right before drawing. Accelerated filters must ensure they call this
// to resolve these inputs before using them for drawing.
virtual void ResolveInputs(DrawTargetWebgl* aDT, bool aAccel,
FilterNodeWebgl* aParent = nullptr) {}
// Recursively resolve all inputs in the filter chain.
void ResolveAllInputs(DrawTargetWebgl* aDT, FilterNodeWebgl* aParent);
protected:
std::vector<RefPtr<FilterNodeWebgl>> mInputFilters;
std::vector<RefPtr<SourceSurface>> mInputSurfaces;
uint32_t mInputMask = 0;
FilterType mType;
RefPtr<FilterNodeSoftware> mSoftwareFilter;
friend class DrawTargetWebgl;
explicit FilterNodeWebgl(FilterType aType);
static already_AddRefed<FilterNodeWebgl> Create(FilterType aType);
size_t NumberOfSetInputs() const {
return std::max(mInputSurfaces.size(), mInputFilters.size());
}
virtual int32_t InputIndex(uint32_t aInputEnumIndex) const;
IntRect MapInputRectToSource(uint32_t aInputEnumIndex, const IntRect& aRect,
const IntRect& aMax, FilterNode* aSourceNode);
};
class FilterNodeCropWebgl : public FilterNodeWebgl {
public:
FilterNodeCropWebgl() : FilterNodeWebgl(FilterType::CROP) {}
void SetAttribute(uint32_t aIndex, const Rect& aValue) override;
IntRect MapRectToSource(const IntRect& aRect, const IntRect& aMax,
FilterNode* aSourceNode) override;
void Draw(DrawTargetWebgl* aDT, const Rect& aSourceRect,
const Point& aDestPoint, const DrawOptions& aOptions,
FilterNodeWebgl* aParent) override;
bool DrawAccel(DrawTargetWebgl* aDT, const Rect& aSourceRect,
const Point& aDestPoint, const DrawOptions& aOptions,
FilterNodeWebgl* aParent) override;
already_AddRefed<SourceSurface> DrawChild(FilterNodeWebgl* aParent,
DrawTargetWebgl* aDT,
const Rect& aSourceRect,
const DrawOptions& aOptions,
Point& aSurfaceOffset,
DeviceColor& aColor) override;
private:
IntRect mCropRect;
int32_t InputIndex(uint32_t aInputEnumIndex) const override;
};
class FilterNodeTransformWebgl : public FilterNodeWebgl {
public:
FilterNodeTransformWebgl() : FilterNodeWebgl(FilterType::TRANSFORM) {}
void SetAttribute(uint32_t aIndex, uint32_t aValue) override;
void SetAttribute(uint32_t aIndex, const Matrix& aValue) override;
IntRect MapRectToSource(const IntRect& aRect, const IntRect& aMax,
FilterNode* aSourceNode) override;
void Draw(DrawTargetWebgl* aDT, const Rect& aSourceRect,
const Point& aDestPoint, const DrawOptions& aOptions,
FilterNodeWebgl* aParent) override;
already_AddRefed<SourceSurface> DrawChild(FilterNodeWebgl* aParent,
DrawTargetWebgl* aDT,
const Rect& aSourceRect,
const DrawOptions& aOptions,
Point& aSurfaceOffset,
DeviceColor& aColor) override;
protected:
Matrix mMatrix;
SamplingFilter mSamplingFilter = SamplingFilter::GOOD;
int32_t InputIndex(uint32_t aInputEnumIndex) const override;
};
class FilterNodeDeferInputWebgl : public FilterNodeTransformWebgl {
public:
FilterNodeDeferInputWebgl(RefPtr<Path> aPath, const Pattern& aPattern,
const IntRect& aSourceRect,
const Matrix& aDestTransform,
const DrawOptions& aOptions,
const StrokeOptions* aStrokeOptions);
void ResolveInputs(DrawTargetWebgl* aDT, bool aAccel,
FilterNodeWebgl* aParent) override;
void Draw(DrawTargetWebgl* aDT, const Rect& aSourceRect,
const Point& aDestPoint, const DrawOptions& aOptions,
FilterNodeWebgl* aParent) override;
already_AddRefed<SourceSurface> DrawChild(FilterNodeWebgl* aParent,
DrawTargetWebgl* aDT,
const Rect& aSourceRect,
const DrawOptions& aOptions,
Point& aSurfaceOffset,
DeviceColor& aColor) override;
private:
RefPtr<Path> mPath;
GeneralPattern mPattern;
IntRect mSourceRect;
Matrix mDestTransform;
DrawOptions mOptions;
Maybe<StrokeOptions> mStrokeOptions;
UniquePtr<Float[]> mDashPatternStorage;
};
class FilterNodeGaussianBlurWebgl : public FilterNodeWebgl {
public:
FilterNodeGaussianBlurWebgl() : FilterNodeWebgl(FilterType::GAUSSIAN_BLUR) {}
void SetAttribute(uint32_t aIndex, float aValue) override;
IntRect MapRectToSource(const IntRect& aRect, const IntRect& aMax,
FilterNode* aSourceNode) override;
void Draw(DrawTargetWebgl* aDT, const Rect& aSourceRect,
const Point& aDestPoint, const DrawOptions& aOptions,
FilterNodeWebgl* aParent) override;
private:
float mStdDeviation = 0;
int32_t InputIndex(uint32_t aInputEnumIndex) const override;
};
class FilterNodeColorMatrixWebgl : public FilterNodeWebgl {
public:
FilterNodeColorMatrixWebgl() : FilterNodeWebgl(FilterType::COLOR_MATRIX) {}
void SetAttribute(uint32_t aIndex, const Matrix5x4& aValue) override;
void SetAttribute(uint32_t aIndex, uint32_t aValue) override;
bool DrawAccel(DrawTargetWebgl* aDT, const Rect& aSourceRect,
const Point& aDestPoint, const DrawOptions& aOptions,
FilterNodeWebgl* aParent) override;
protected:
Matrix5x4 mMatrix;
AlphaMode mAlphaMode;
int32_t InputIndex(uint32_t aInputEnumIndex) const override;
};
class FilterNodeComponentTransferWebgl : public FilterNodeWebgl {
public:
explicit FilterNodeComponentTransferWebgl(FilterType aType)
: FilterNodeWebgl(aType),
mDisableR(true),
mDisableG(true),
mDisableB(true),
mDisableA(true) {}
void SetAttribute(uint32_t aIndex, bool aValue) override;
bool DrawAccel(DrawTargetWebgl* aDT, const Rect& aSourceRect,
const Point& aDestPoint, const DrawOptions& aOptions,
FilterNodeWebgl* aParent) override;
virtual bool ToColorMatrix(Matrix5x4& aMatrix) const { return false; }
protected:
bool mDisableR : 1;
bool mDisableG : 1;
bool mDisableB : 1;
bool mDisableA : 1;
int32_t InputIndex(uint32_t aInputEnumIndex) const override;
};
class FilterNodeLinearTransferWebgl : public FilterNodeComponentTransferWebgl {
public:
FilterNodeLinearTransferWebgl()
: FilterNodeComponentTransferWebgl(FilterType::LINEAR_TRANSFER) {}
using FilterNodeComponentTransferWebgl::SetAttribute;
void SetAttribute(uint32_t aIndex, Float aValue) override;
bool ToColorMatrix(Matrix5x4& aMatrix) const override;
protected:
DeviceColor mSlope;
DeviceColor mIntercept;
};
class FilterNodeTableTransferWebgl : public FilterNodeComponentTransferWebgl {
public:
FilterNodeTableTransferWebgl()
: FilterNodeComponentTransferWebgl(FilterType::TABLE_TRANSFER) {}
using FilterNodeComponentTransferWebgl::SetAttribute;
void SetAttribute(uint32_t aIndex, const Float* aValues,
uint32_t aSize) override;
bool ToColorMatrix(Matrix5x4& aMatrix) const override;
protected:
std::vector<Float> mTableR;
std::vector<Float> mTableG;
std::vector<Float> mTableB;
std::vector<Float> mTableA;
};
class FilterNodePremultiplyWebgl : public FilterNodeWebgl {
public:
FilterNodePremultiplyWebgl() : FilterNodeWebgl(FilterType::PREMULTIPLY) {}
void Draw(DrawTargetWebgl* aDT, const Rect& aSourceRect,
const Point& aDestPoint, const DrawOptions& aOptions,
FilterNodeWebgl* aParent) override;
protected:
int32_t InputIndex(uint32_t aInputEnumIndex) const override;
};
class FilterNodeUnpremultiplyWebgl : public FilterNodeWebgl {
public:
FilterNodeUnpremultiplyWebgl() : FilterNodeWebgl(FilterType::UNPREMULTIPLY) {}
already_AddRefed<SourceSurface> DrawChild(FilterNodeWebgl* aParent,
DrawTargetWebgl* aDT,
const Rect& aSourceRect,
const DrawOptions& aOptions,
Point& aSurfaceOffset,
DeviceColor& aColor) override;
protected:
int32_t InputIndex(uint32_t aInputEnumIndex) const override;
};
class FilterNodeOpacityWebgl : public FilterNodeWebgl {
public:
FilterNodeOpacityWebgl() : FilterNodeWebgl(FilterType::OPACITY) {}
void SetAttribute(uint32_t aIndex, Float aValue) override;
void Draw(DrawTargetWebgl* aDT, const Rect& aSourceRect,
const Point& aDestPoint, const DrawOptions& aOptions,
FilterNodeWebgl* aParent) override;
already_AddRefed<SourceSurface> DrawChild(FilterNodeWebgl* aParent,
DrawTargetWebgl* aDT,
const Rect& aSourceRect,
const DrawOptions& aOptions,
Point& aSurfaceOffset,
DeviceColor& aColor) override;
protected:
Float mValue = 1.0f;
int32_t InputIndex(uint32_t aInputEnumIndex) const override;
};
} // namespace mozilla::gfx
#endif /* MOZILLA_GFX_FILTERNODEWEBGL_H_ */
|