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 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
|
/* -*- 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/. */
#include "FilterNodeWebgl.h"
#include <limits>
#include "DrawTargetWebglInternal.h"
#include "SourceSurfaceWebgl.h"
#include "mozilla/gfx/Blur.h"
#include "mozilla/gfx/DrawTargetSkia.h"
#include "mozilla/gfx/FilterNodeSoftware.h"
#include "mozilla/gfx/Helpers.h"
#include "mozilla/gfx/Logging.h"
namespace mozilla::gfx {
FilterNodeWebgl::FilterNodeWebgl(FilterType aType)
: mType(aType),
mSoftwareFilter(
FilterNodeSoftware::Create(aType).downcast<FilterNodeSoftware>()) {}
FilterNodeWebgl::~FilterNodeWebgl() = default;
already_AddRefed<FilterNodeWebgl> FilterNodeWebgl::Create(FilterType aType) {
RefPtr<FilterNodeWebgl> filter;
switch (aType) {
case FilterType::CROP:
filter = new FilterNodeCropWebgl;
break;
case FilterType::TRANSFORM:
filter = new FilterNodeTransformWebgl;
break;
case FilterType::GAUSSIAN_BLUR:
filter = new FilterNodeGaussianBlurWebgl;
break;
default:
filter = new FilterNodeWebgl(aType);
break;
}
return filter.forget();
}
int32_t FilterNodeWebgl::InputIndex(uint32_t aInputEnumIndex) const {
if (mSoftwareFilter) {
return mSoftwareFilter->InputIndex(aInputEnumIndex);
}
return -1;
}
bool FilterNodeWebgl::ReserveInputIndex(uint32_t aIndex) {
size_t inputIndex = aIndex;
if (std::numeric_limits<size_t>::max() - inputIndex < 1) {
return false;
}
if (mInputSurfaces.size() <= inputIndex) {
mInputSurfaces.resize(inputIndex + 1);
}
if (mInputFilters.size() <= inputIndex) {
mInputFilters.resize(inputIndex + 1);
}
return true;
}
bool FilterNodeWebgl::SetInputAccel(uint32_t aIndex, SourceSurface* aSurface) {
if (ReserveInputIndex(aIndex)) {
mInputSurfaces[aIndex] = aSurface;
mInputFilters[aIndex] = nullptr;
return true;
}
return false;
}
bool FilterNodeWebgl::SetInputSoftware(uint32_t aIndex,
SourceSurface* aSurface) {
if (mSoftwareFilter) {
mSoftwareFilter->SetInput(aIndex, aSurface);
}
mInputMask |= (1 << aIndex);
return true;
}
void FilterNodeWebgl::SetInput(uint32_t aIndex, SourceSurface* aSurface) {
int32_t inputIndex = InputIndex(aIndex);
if (inputIndex < 0 || !SetInputAccel(inputIndex, aSurface) ||
!SetInputSoftware(inputIndex, aSurface)) {
gfxDevCrash(LogReason::FilterInputSet) << "Invalid set " << inputIndex;
return;
}
}
void FilterNodeWebgl::SetInput(uint32_t aIndex, FilterNode* aFilter) {
if (aFilter && aFilter->GetBackendType() != FILTER_BACKEND_WEBGL) {
MOZ_ASSERT(false, "FilterNodeWebgl required as input");
return;
}
int32_t inputIndex = InputIndex(aIndex);
if (inputIndex < 0 || !ReserveInputIndex(inputIndex)) {
gfxDevCrash(LogReason::FilterInputSet) << "Invalid set " << inputIndex;
return;
}
auto* webglFilter = static_cast<FilterNodeWebgl*>(aFilter);
mInputFilters[inputIndex] = webglFilter;
mInputSurfaces[inputIndex] = nullptr;
if (mSoftwareFilter) {
MOZ_ASSERT(!webglFilter || webglFilter->mSoftwareFilter);
mSoftwareFilter->SetInput(
aIndex, webglFilter ? webglFilter->mSoftwareFilter.get() : nullptr);
}
}
void FilterNodeWebgl::SetAttribute(uint32_t aIndex, bool aValue) {
if (mSoftwareFilter) {
mSoftwareFilter->SetAttribute(aIndex, aValue);
}
}
void FilterNodeWebgl::SetAttribute(uint32_t aIndex, uint32_t aValue) {
if (mSoftwareFilter) {
mSoftwareFilter->SetAttribute(aIndex, aValue);
}
}
void FilterNodeWebgl::SetAttribute(uint32_t aIndex, Float aValue) {
if (mSoftwareFilter) {
mSoftwareFilter->SetAttribute(aIndex, aValue);
}
}
void FilterNodeWebgl::SetAttribute(uint32_t aIndex, const Size& aValue) {
if (mSoftwareFilter) {
mSoftwareFilter->SetAttribute(aIndex, aValue);
}
}
void FilterNodeWebgl::SetAttribute(uint32_t aIndex, const IntSize& aValue) {
if (mSoftwareFilter) {
mSoftwareFilter->SetAttribute(aIndex, aValue);
}
}
void FilterNodeWebgl::SetAttribute(uint32_t aIndex, const IntPoint& aValue) {
if (mSoftwareFilter) {
mSoftwareFilter->SetAttribute(aIndex, aValue);
}
}
void FilterNodeWebgl::SetAttribute(uint32_t aIndex, const Rect& aValue) {
if (mSoftwareFilter) {
mSoftwareFilter->SetAttribute(aIndex, aValue);
}
}
void FilterNodeWebgl::SetAttribute(uint32_t aIndex, const IntRect& aValue) {
if (mSoftwareFilter) {
mSoftwareFilter->SetAttribute(aIndex, aValue);
}
}
void FilterNodeWebgl::SetAttribute(uint32_t aIndex, const Point& aValue) {
if (mSoftwareFilter) {
mSoftwareFilter->SetAttribute(aIndex, aValue);
}
}
void FilterNodeWebgl::SetAttribute(uint32_t aIndex, const Matrix& aValue) {
if (mSoftwareFilter) {
mSoftwareFilter->SetAttribute(aIndex, aValue);
}
}
void FilterNodeWebgl::SetAttribute(uint32_t aIndex, const Matrix5x4& aValue) {
if (mSoftwareFilter) {
mSoftwareFilter->SetAttribute(aIndex, aValue);
}
}
void FilterNodeWebgl::SetAttribute(uint32_t aIndex, const Point3D& aValue) {
if (mSoftwareFilter) {
mSoftwareFilter->SetAttribute(aIndex, aValue);
}
}
void FilterNodeWebgl::SetAttribute(uint32_t aIndex, const DeviceColor& aValue) {
if (mSoftwareFilter) {
mSoftwareFilter->SetAttribute(aIndex, aValue);
}
}
void FilterNodeWebgl::SetAttribute(uint32_t aIndex, const Float* aValues,
uint32_t aSize) {
if (mSoftwareFilter) {
mSoftwareFilter->SetAttribute(aIndex, aValues, aSize);
}
}
IntRect FilterNodeWebgl::MapRectToSource(const IntRect& aRect,
const IntRect& aMax,
FilterNode* aSourceNode) {
if (mSoftwareFilter) {
if (aSourceNode && aSourceNode->GetBackendType() == FILTER_BACKEND_WEBGL) {
aSourceNode = static_cast<FilterNodeWebgl*>(aSourceNode)->mSoftwareFilter;
}
return mSoftwareFilter->MapRectToSource(aRect, aMax, aSourceNode);
}
return aMax;
}
void FilterNodeWebgl::Draw(DrawTargetWebgl* aDT, const Rect& aSourceRect,
const Point& aDestPoint,
const DrawOptions& aOptions) {
ResolveAllInputs(aDT);
MOZ_ASSERT(mSoftwareFilter);
aDT->DrawFilterFallback(mSoftwareFilter, aSourceRect, aDestPoint, aOptions);
}
already_AddRefed<SourceSurface> FilterNodeWebgl::DrawChild(
DrawTargetWebgl* aDT, const Rect& aSourceRect, Point& aSurfaceOffset) {
ResolveAllInputs(aDT);
MOZ_ASSERT(mSoftwareFilter);
RefPtr<DrawTarget> swDT = aDT->mSkia->CreateSimilarDrawTarget(
IntSize::Ceil(aSourceRect.Size()), aDT->GetFormat());
if (!swDT) {
return nullptr;
}
swDT->DrawFilter(mSoftwareFilter, aSourceRect, Point(0, 0));
aSurfaceOffset = aSourceRect.TopLeft();
return swDT->Snapshot();
}
IntRect FilterNodeWebgl::MapInputRectToSource(uint32_t aInputEnumIndex,
const IntRect& aRect,
const IntRect& aMax,
FilterNode* aSourceNode) {
int32_t inputIndex = InputIndex(aInputEnumIndex);
if (inputIndex < 0) {
gfxDevCrash(LogReason::FilterInputError)
<< "Invalid input " << inputIndex << " vs. " << NumberOfSetInputs();
return aMax;
}
if ((uint32_t)inputIndex < NumberOfSetInputs()) {
if (RefPtr<FilterNodeWebgl> filter = mInputFilters[inputIndex]) {
return filter->MapRectToSource(aRect, aMax, aSourceNode);
}
}
if (this == aSourceNode) {
return aRect;
}
return IntRect();
}
void FilterNodeWebgl::ResolveAllInputs(DrawTargetWebgl* aDT) {
for (const auto& filter : mInputFilters) {
if (filter) {
filter->ResolveInputs(aDT, false);
filter->ResolveAllInputs(aDT);
}
}
}
int32_t FilterNodeCropWebgl::InputIndex(uint32_t aInputEnumIndex) const {
switch (aInputEnumIndex) {
case IN_CROP_IN:
return 0;
default:
return -1;
}
}
void FilterNodeCropWebgl::SetAttribute(uint32_t aIndex, const Rect& aValue) {
MOZ_ASSERT(aIndex == ATT_CROP_RECT);
Rect srcRect = aValue;
srcRect.Round();
if (!srcRect.ToIntRect(&mCropRect)) {
mCropRect = IntRect();
}
FilterNodeWebgl::SetAttribute(aIndex, aValue);
}
IntRect FilterNodeCropWebgl::MapRectToSource(const IntRect& aRect,
const IntRect& aMax,
FilterNode* aSourceNode) {
return MapInputRectToSource(IN_CROP_IN, aRect.Intersect(mCropRect), aMax,
aSourceNode);
}
void FilterNodeCropWebgl::Draw(DrawTargetWebgl* aDT, const Rect& aSourceRect,
const Point& aDestPoint,
const DrawOptions& aOptions) {
ResolveInputs(aDT, true);
uint32_t inputIdx = InputIndex(IN_CROP_IN);
if (inputIdx < NumberOfSetInputs()) {
Rect croppedSource = aSourceRect.Intersect(Rect(mCropRect));
if (RefPtr<FilterNodeWebgl> filter = mInputFilters[inputIdx]) {
filter->Draw(aDT, croppedSource,
aDestPoint + croppedSource.TopLeft() - aSourceRect.TopLeft(),
aOptions);
} else if (RefPtr<SourceSurface> surface = mInputSurfaces[inputIdx]) {
aDT->DrawSurface(surface,
croppedSource - aSourceRect.TopLeft() + aDestPoint,
croppedSource, DrawSurfaceOptions(), aOptions);
}
}
}
int32_t FilterNodeTransformWebgl::InputIndex(uint32_t aInputEnumIndex) const {
switch (aInputEnumIndex) {
case IN_TRANSFORM_IN:
return 0;
default:
return -1;
}
}
void FilterNodeTransformWebgl::SetAttribute(uint32_t aIndex, uint32_t aValue) {
MOZ_ASSERT(aIndex == ATT_TRANSFORM_FILTER);
mSamplingFilter = static_cast<SamplingFilter>(aValue);
FilterNodeWebgl::SetAttribute(aIndex, aValue);
}
void FilterNodeTransformWebgl::SetAttribute(uint32_t aIndex,
const Matrix& aValue) {
MOZ_ASSERT(aIndex == ATT_TRANSFORM_MATRIX);
mMatrix = aValue;
FilterNodeWebgl::SetAttribute(aIndex, aValue);
}
IntRect FilterNodeTransformWebgl::MapRectToSource(const IntRect& aRect,
const IntRect& aMax,
FilterNode* aSourceNode) {
if (aRect.IsEmpty()) {
return IntRect();
}
Matrix inv(mMatrix);
if (!inv.Invert()) {
return aMax;
}
Rect rect = inv.TransformBounds(Rect(aRect));
rect.RoundOut();
IntRect intRect;
if (!rect.ToIntRect(&intRect)) {
return aMax;
}
return MapInputRectToSource(IN_TRANSFORM_IN, intRect, aMax, aSourceNode);
}
already_AddRefed<SourceSurface> FilterNodeTransformWebgl::DrawChild(
DrawTargetWebgl* aDT, const Rect& aSourceRect, Point& aSurfaceOffset) {
ResolveInputs(aDT, true);
uint32_t inputIdx = InputIndex(IN_TRANSFORM_IN);
if (inputIdx < NumberOfSetInputs()) {
if (mMatrix.IsIntegerTranslation()) {
if (RefPtr<SourceSurface> surface = mInputSurfaces[inputIdx]) {
aSurfaceOffset = mMatrix.GetTranslation().Round();
return surface.forget();
}
}
return FilterNodeWebgl::DrawChild(aDT, aSourceRect, aSurfaceOffset);
}
return nullptr;
}
FilterNodeDeferInputWebgl::FilterNodeDeferInputWebgl(
RefPtr<Path> aPath, const Pattern& aPattern, const IntRect& aSourceRect,
const Matrix& aDestTransform, const DrawOptions& aOptions,
const StrokeOptions* aStrokeOptions)
: mPath(std::move(aPath)),
mSourceRect(aSourceRect),
mDestTransform(aDestTransform),
mOptions(aOptions) {
mPattern.Init(aPattern);
if (aStrokeOptions) {
mStrokeOptions = Some(*aStrokeOptions);
if (aStrokeOptions->mDashLength > 0) {
mDashPatternStorage.reset(new Float[aStrokeOptions->mDashLength]);
PodCopy(mDashPatternStorage.get(), aStrokeOptions->mDashPattern,
aStrokeOptions->mDashLength);
mStrokeOptions->mDashPattern = mDashPatternStorage.get();
}
}
SetAttribute(ATT_TRANSFORM_MATRIX,
Matrix::Translation(mSourceRect.TopLeft()));
}
void FilterNodeDeferInputWebgl::ResolveInputs(DrawTargetWebgl* aDT,
bool aAccel) {
uint32_t inputIdx = InputIndex(IN_TRANSFORM_IN);
bool hasAccel = false;
if (inputIdx < NumberOfSetInputs() && mInputSurfaces[inputIdx]) {
if (aAccel || (mInputMask & (1 << inputIdx))) {
return;
}
hasAccel = true;
}
RefPtr<SourceSurface> surface;
SurfaceFormat format = SurfaceFormat::B8G8R8A8;
static const ColorPattern maskPattern(DeviceColor(1, 1, 1, 1));
const Pattern* pattern = mPattern.GetPattern();
if (aAccel) {
// If using acceleration on a color pattern, attempt to blur solely on the
// alpha values to significantly reduce data churn, as the color will only
// vary linearly with alpha over the input surface. The color will be
// incorporated on the final mask draw.
if (mPattern.GetPattern()->GetType() == PatternType::COLOR) {
format = SurfaceFormat::A8;
pattern = &maskPattern;
}
surface = aDT->ResolveFilterInputAccel(
mPath, *pattern, mSourceRect, mDestTransform, mOptions,
mStrokeOptions.ptrOr(nullptr), format);
}
if (!surface) {
surface = aDT->mSkia->ResolveFilterInput(
mPath, *pattern, mSourceRect, mDestTransform, mOptions,
mStrokeOptions.ptrOr(nullptr), format);
}
if (hasAccel) {
SetInputSoftware(inputIdx, surface);
} else if (surface && surface->GetFormat() == SurfaceFormat::A8) {
SetInputAccel(inputIdx, surface);
} else {
SetInput(inputIdx, surface);
}
}
DeviceColor FilterNodeDeferInputWebgl::GetColor() const {
return mPattern.GetPattern()->GetType() == PatternType::COLOR
? static_cast<const ColorPattern*>(mPattern.GetPattern())->mColor
: DeviceColor(1, 1, 1, 1);
}
int32_t FilterNodeGaussianBlurWebgl::InputIndex(
uint32_t aInputEnumIndex) const {
switch (aInputEnumIndex) {
case IN_GAUSSIAN_BLUR_IN:
return 0;
default:
return -1;
}
}
void FilterNodeGaussianBlurWebgl::SetAttribute(uint32_t aIndex, float aValue) {
MOZ_ASSERT(aIndex == ATT_GAUSSIAN_BLUR_STD_DEVIATION);
// Match the FilterNodeSoftware blur limit.
mStdDeviation = std::clamp(aValue, 0.0f, 100.0f);
FilterNodeWebgl::SetAttribute(aIndex, aValue);
}
IntRect FilterNodeGaussianBlurWebgl::MapRectToSource(const IntRect& aRect,
const IntRect& aMax,
FilterNode* aSourceNode) {
return MapInputRectToSource(IN_GAUSSIAN_BLUR_IN, aRect, aMax, aSourceNode);
}
void FilterNodeGaussianBlurWebgl::Draw(DrawTargetWebgl* aDT,
const Rect& aSourceRect,
const Point& aDestPoint,
const DrawOptions& aOptions) {
ResolveInputs(aDT, true);
uint32_t inputIdx = InputIndex(IN_GAUSSIAN_BLUR_IN);
if (inputIdx < NumberOfSetInputs()) {
bool success = false;
Point surfaceOffset;
if (RefPtr<SourceSurface> surface =
mInputFilters[inputIdx] ? mInputFilters[inputIdx]->DrawChild(
aDT, aSourceRect, surfaceOffset)
: mInputSurfaces[inputIdx]) {
DeviceColor color =
surface->GetFormat() == SurfaceFormat::A8 && mInputFilters[inputIdx]
? mInputFilters[inputIdx]->GetColor()
: DeviceColor(1, 1, 1, 1);
aDT->PushClipRect(Rect(aDestPoint, aSourceRect.Size()));
IntRect surfRect = RoundedOut(
Rect(surface->GetRect()).Intersect(aSourceRect - surfaceOffset));
Point destOffset =
Point(surfRect.TopLeft()) + surfaceOffset - aSourceRect.TopLeft();
success = surfRect.IsEmpty() ||
aDT->BlurSurface(mStdDeviation, surface, surfRect,
aDestPoint + destOffset, aOptions, color);
aDT->PopClip();
}
if (!success) {
FilterNodeWebgl::Draw(aDT, aSourceRect, aDestPoint, aOptions);
}
}
}
} // namespace mozilla::gfx
|