Skip to content

Commit

Permalink
Add the class FilterTargetSwitcher
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=246822

Reviewed by Simon Fraser.

This class will manage applying a filter to a certain area of drawings with the
minimum changes in the existing code. All it needs is a set of callbacks in the
existing caller class. And the caller adds a line like this to the drawing
function:

	FilterTargetSwitcher targetSwitcher(*this, bounds);

The constructor of FilterTargetSwitcher will ask the client if there a filter to
apply or not. If there is a filter, it will switch the drawing to a temporary
ImageBuffer. The client has to make sure all the drawing code from now on will
use the context of this ImageBuffer. The destructor will apply the filter to the
temporary ImageBuffer and then composite filtered ImageBuffer back to the client
drawing context.

* Source/WebCore/Sources.txt:
* Source/WebCore/WebCore.xcodeproj/project.pbxproj:
* Source/WebCore/platform/graphics/filters/FilterTargetSwitcher.cpp: Added.
(WebCore::FilterTargetSwitcher::FilterTargetSwitcher):
(WebCore::FilterTargetSwitcher::~FilterTargetSwitcher):
(WebCore::FilterTargetSwitcher::drawingContext const):
(WebCore::FilterTargetSwitcher::outsets const):
* Source/WebCore/platform/graphics/filters/FilterTargetSwitcher.h: Added.

Canonical link: https://commits.webkit.org/255802@main
  • Loading branch information
shallawa authored and Said Abou-Hallawa committed Oct 20, 2022
1 parent 0c33f22 commit 5a02a23
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 0 deletions.
1 change: 1 addition & 0 deletions Source/WebCore/Sources.txt
Expand Up @@ -2227,6 +2227,7 @@ platform/graphics/filters/FilterImage.cpp
platform/graphics/filters/FilterOperation.cpp
platform/graphics/filters/FilterOperations.cpp
platform/graphics/filters/FilterResults.cpp
platform/graphics/filters/FilterTargetSwitcher.cpp
platform/graphics/filters/PointLightSource.cpp
platform/graphics/filters/SourceAlpha.cpp
platform/graphics/filters/SourceGraphic.cpp
Expand Down
4 changes: 4 additions & 0 deletions Source/WebCore/WebCore.xcodeproj/project.pbxproj
Expand Up @@ -11747,6 +11747,8 @@
724DCF1B284853650026ACF4 /* ImageBufferAllocator.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = ImageBufferAllocator.cpp; sourceTree = "<group>"; };
724DCF1C284853650026ACF4 /* ImageBufferAllocator.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ImageBufferAllocator.h; sourceTree = "<group>"; };
724DCF1D28485C330026ACF4 /* FilterResults.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = FilterResults.cpp; sourceTree = "<group>"; };
724E09062901CD27000AF3EB /* FilterTargetSwitcher.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = FilterTargetSwitcher.cpp; sourceTree = "<group>"; };
724E09072901CD27000AF3EB /* FilterTargetSwitcher.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FilterTargetSwitcher.h; sourceTree = "<group>"; };
724ED3291A3A7E5400F5F13C /* EXTBlendMinMax.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = EXTBlendMinMax.cpp; sourceTree = "<group>"; };
724ED32A1A3A7E5400F5F13C /* EXTBlendMinMax.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EXTBlendMinMax.h; sourceTree = "<group>"; };
724ED32B1A3A7E5400F5F13C /* EXTBlendMinMax.idl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = EXTBlendMinMax.idl; sourceTree = "<group>"; };
Expand Down Expand Up @@ -27866,6 +27868,8 @@
49ECEB661499790D00CDD3A4 /* FilterOperations.h */,
724DCF1D28485C330026ACF4 /* FilterResults.cpp */,
7211B5D6276536820076FEF8 /* FilterResults.h */,
724E09062901CD27000AF3EB /* FilterTargetSwitcher.cpp */,
724E09072901CD27000AF3EB /* FilterTargetSwitcher.h */,
84730D741248F0B300D3A9C9 /* LightSource.h */,
A1E1154513015C4E0054AC8C /* PointLightSource.cpp */,
84730D751248F0B300D3A9C9 /* PointLightSource.h */,
Expand Down
104 changes: 104 additions & 0 deletions Source/WebCore/platform/graphics/filters/FilterTargetSwitcher.cpp
@@ -0,0 +1,104 @@
/*
* Copyright (C) 2022 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "config.h"
#include "FilterTargetSwitcher.h"

#include "Filter.h"
#include "FilterResults.h"
#include "GraphicsContext.h"
#include "ImageBuffer.h"

namespace WebCore {

FilterTargetSwitcher::FilterTargetSwitcher(Client& client, const DestinationColorSpace& colorSpace, const std::function<FloatRect()>& boundsProvider)
: m_client(client)
{
auto* context = m_client.drawingContext();
if (!context)
return;

m_filter = m_client.createFilter([&]() {
m_bounds = boundsProvider();
return m_bounds;
});

if (!m_filter)
return;

ASSERT(!m_bounds.isEmpty());

m_imageBuffer = context->createScaledImageBuffer(m_bounds, { 1, 1 }, colorSpace);
if (!m_imageBuffer) {
m_filter = nullptr;
return;
}

auto state = context->state();
m_imageBuffer->context().mergeAllChanges(state);
m_client.setTargetSwitcher(this);
}

FilterTargetSwitcher::FilterTargetSwitcher(Client& client, const DestinationColorSpace& colorSpace, const FloatRect& bounds)
: FilterTargetSwitcher(client, colorSpace, [&]() {
return bounds;
})
{
}

FilterTargetSwitcher::~FilterTargetSwitcher()
{
if (!m_filter)
return;

ASSERT(m_imageBuffer);
ASSERT(!m_bounds.isEmpty());

m_client.setTargetSwitcher(nullptr);

auto* context = m_client.drawingContext();
FilterResults results;
context->drawFilteredImageBuffer(m_imageBuffer.get(), m_bounds, *m_filter, results);
}

GraphicsContext* FilterTargetSwitcher::drawingContext() const
{
if (m_imageBuffer)
return &m_imageBuffer->context();
return m_client.drawingContext();
}

FloatBoxExtent FilterTargetSwitcher::outsets() const
{
if (!m_filter)
return { };

ASSERT(!m_bounds.isEmpty());

auto outsets = m_client.calculateFilterOutsets(m_bounds);
return { outsets.top(), outsets.right(), outsets.bottom(), outsets.left() };
}

} // namespace WebCore
63 changes: 63 additions & 0 deletions Source/WebCore/platform/graphics/filters/FilterTargetSwitcher.h
@@ -0,0 +1,63 @@
/*
* Copyright (C) 2022 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#pragma once

#include "DestinationColorSpace.h"
#include "FloatRect.h"

namespace WebCore {

class Filter;
class GraphicsContext;
class ImageBuffer;

class FilterTargetSwitcher {
public:
class Client {
public:
virtual ~Client() = default;
virtual RefPtr<Filter> createFilter(const std::function<FloatRect()>& boundsProvider) const = 0;
virtual IntOutsets calculateFilterOutsets(const FloatRect& bounds) const = 0;
virtual GraphicsContext* drawingContext() const = 0;
virtual void setTargetSwitcher(FilterTargetSwitcher*) = 0;
protected:
Client() = default;
};

FilterTargetSwitcher(Client&, const DestinationColorSpace&, const std::function<FloatRect()>& boundsProvider);
FilterTargetSwitcher(Client&, const DestinationColorSpace&, const FloatRect& bounds);
~FilterTargetSwitcher();
GraphicsContext* drawingContext() const;
FloatBoxExtent outsets() const;

private:
Client& m_client;
RefPtr<Filter> m_filter;
RefPtr<ImageBuffer> m_imageBuffer;
FloatRect m_bounds;
};

} // namespace WebCore

0 comments on commit 5a02a23

Please sign in to comment.