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
|
/*
* 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 THE COPYRIGHT HOLDER "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 THE COPYRIGHT HOLDER 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 "StyleCrossfadeImage.h"
#include "AnimationUtilities.h"
#include "CSSCrossfadeValue.h"
#include "CSSValuePool.h"
#include "CachedImage.h"
#include "CachedResourceLoader.h"
#include "CrossfadeGeneratedImage.h"
#include "RenderElement.h"
#include <wtf/PointerComparison.h>
namespace WebCore {
StyleCrossfadeImage::StyleCrossfadeImage(RefPtr<StyleImage>&& from, RefPtr<StyleImage>&& to, double percentage, bool isPrefixed)
: StyleGeneratedImage { Type::CrossfadeImage, StyleCrossfadeImage::isFixedSize }
, m_from { WTFMove(from) }
, m_to { WTFMove(to) }
, m_percentage { percentage }
, m_isPrefixed { isPrefixed }
, m_inputImagesAreReady { false }
{
}
StyleCrossfadeImage::~StyleCrossfadeImage()
{
if (m_cachedFromImage)
m_cachedFromImage->removeClient(*this);
if (m_cachedToImage)
m_cachedToImage->removeClient(*this);
}
bool StyleCrossfadeImage::operator==(const StyleImage& other) const
{
auto* otherCrossfadeImage = dynamicDowncast<StyleCrossfadeImage>(other);
return otherCrossfadeImage && equals(*otherCrossfadeImage);
}
bool StyleCrossfadeImage::equals(const StyleCrossfadeImage& other) const
{
return equalInputImages(other) && m_percentage == other.m_percentage;
}
bool StyleCrossfadeImage::equalInputImages(const StyleCrossfadeImage& other) const
{
return arePointingToEqualData(m_from, other.m_from) && arePointingToEqualData(m_to, other.m_to);
}
RefPtr<StyleCrossfadeImage> StyleCrossfadeImage::blend(const StyleCrossfadeImage& from, const BlendingContext& context) const
{
ASSERT(equalInputImages(from));
if (!m_cachedToImage || !m_cachedFromImage)
return nullptr;
auto newPercentage = WebCore::blend(from.m_percentage, m_percentage, context);
return StyleCrossfadeImage::create(m_from, m_to, newPercentage, from.m_isPrefixed && m_isPrefixed);
}
Ref<CSSValue> StyleCrossfadeImage::computedStyleValue(const RenderStyle& style) const
{
auto fromComputedValue = m_from ? m_from->computedStyleValue(style) : static_reference_cast<CSSValue>(CSSPrimitiveValue::create(CSSValueNone));
auto toComputedValue = m_to ? m_to->computedStyleValue(style) : static_reference_cast<CSSValue>(CSSPrimitiveValue::create(CSSValueNone));
return CSSCrossfadeValue::create(WTFMove(fromComputedValue), WTFMove(toComputedValue), CSSPrimitiveValue::create(m_percentage), m_isPrefixed);
}
bool StyleCrossfadeImage::isPending() const
{
if (m_from && m_from->isPending())
return true;
if (m_to && m_to->isPending())
return true;
return false;
}
void StyleCrossfadeImage::load(CachedResourceLoader& loader, const ResourceLoaderOptions& options)
{
auto oldCachedFromImage = m_cachedFromImage;
auto oldCachedToImage = m_cachedToImage;
if (m_from) {
if (m_from->isPending())
m_from->load(loader, options);
m_cachedFromImage = m_from->cachedImage();
} else
m_cachedFromImage = nullptr;
if (m_to) {
if (m_to->isPending())
m_to->load(loader, options);
m_cachedToImage = m_to->cachedImage();
} else
m_cachedToImage = nullptr;
if (m_cachedFromImage != oldCachedFromImage) {
if (oldCachedFromImage)
oldCachedFromImage->removeClient(*this);
if (m_cachedFromImage)
m_cachedFromImage->addClient(*this);
}
if (m_cachedToImage != oldCachedToImage) {
if (oldCachedToImage)
oldCachedToImage->removeClient(*this);
if (m_cachedToImage)
m_cachedToImage->addClient(*this);
}
m_inputImagesAreReady = true;
}
RefPtr<Image> StyleCrossfadeImage::image(const RenderElement* renderer, const FloatSize& size, bool isForFirstLine) const
{
if (!renderer)
return &Image::nullImage();
if (size.isEmpty())
return nullptr;
if (!m_from || !m_to)
return &Image::nullImage();
auto fromImage = m_from->image(renderer, size, isForFirstLine);
auto toImage = m_to->image(renderer, size, isForFirstLine);
if (!fromImage || !toImage)
return &Image::nullImage();
return CrossfadeGeneratedImage::create(*fromImage, *toImage, m_percentage, fixedSize(*renderer), size);
}
bool StyleCrossfadeImage::knownToBeOpaque(const RenderElement& renderer) const
{
if (m_from && !m_from->knownToBeOpaque(renderer))
return false;
if (m_to && !m_to->knownToBeOpaque(renderer))
return false;
return true;
}
FloatSize StyleCrossfadeImage::fixedSize(const RenderElement& renderer) const
{
if (!m_from || !m_to)
return { };
auto fromImageSize = m_from->imageSize(&renderer, 1);
auto toImageSize = m_to->imageSize(&renderer, 1);
// Rounding issues can cause transitions between images of equal size to return
// a different fixed size; avoid performing the interpolation if the images are the same size.
if (fromImageSize == toImageSize)
return fromImageSize;
float percentage = m_percentage;
float inversePercentage = 1 - percentage;
return fromImageSize * inversePercentage + toImageSize * percentage;
}
void StyleCrossfadeImage::imageChanged(CachedImage*, const IntRect*)
{
if (!m_inputImagesAreReady)
return;
for (auto entry : clients()) {
auto& client = entry.key;
client.imageChanged(static_cast<WrappedImagePtr>(this));
}
}
} // namespace WebCore
|