Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
4 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
Source/WebCore/platform/graphics/filters/FilterTargetSwitcher.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
63
Source/WebCore/platform/graphics/filters/FilterTargetSwitcher.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |