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
|
/*
* Copyright (C) 2009 Dirk Schulze <krit@webkit.org>
* Copyright (C) Research In Motion Limited 2011. All rights reserved.
* Copyright (C) 2016-2017 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 "ImageBuffer.h"
#include "GraphicsContext.h"
#include "IntRect.h"
#include <wtf/MathExtras.h>
namespace WebCore {
static const float MaxClampedLength = 4096;
static const float MaxClampedArea = MaxClampedLength * MaxClampedLength;
std::unique_ptr<ImageBuffer> ImageBuffer::create(const FloatSize& size, RenderingMode renderingMode, float resolutionScale, ColorSpace colorSpace)
{
bool success = false;
std::unique_ptr<ImageBuffer> buffer(new ImageBuffer(size, resolutionScale, colorSpace, renderingMode, success));
if (!success)
return nullptr;
return buffer;
}
#if USE(DIRECT2D)
std::unique_ptr<ImageBuffer> ImageBuffer::create(const FloatSize& size, RenderingMode renderingMode, const GraphicsContext* targetContext, float resolutionScale, ColorSpace colorSpace)
{
bool success = false;
std::unique_ptr<ImageBuffer> buffer(new ImageBuffer(size, resolutionScale, colorSpace, renderingMode, targetContext, success));
if (!success)
return nullptr;
return buffer;
}
#endif
bool ImageBuffer::sizeNeedsClamping(const FloatSize& size)
{
if (size.isEmpty())
return false;
return floorf(size.height()) * floorf(size.width()) > MaxClampedArea;
}
bool ImageBuffer::sizeNeedsClamping(const FloatSize& size, FloatSize& scale)
{
FloatSize scaledSize(size);
scaledSize.scale(scale.width(), scale.height());
if (!sizeNeedsClamping(scaledSize))
return false;
// The area of scaled size is bigger than the upper limit, adjust the scale to fit.
scale.scale(sqrtf(MaxClampedArea / (scaledSize.width() * scaledSize.height())));
ASSERT(!sizeNeedsClamping(size, scale));
return true;
}
FloatSize ImageBuffer::clampedSize(const FloatSize& size)
{
return size.shrunkTo(FloatSize(MaxClampedLength, MaxClampedLength));
}
FloatSize ImageBuffer::clampedSize(const FloatSize& size, FloatSize& scale)
{
if (size.isEmpty())
return size;
FloatSize clampedSize = ImageBuffer::clampedSize(size);
scale = FloatSize(clampedSize.width() / size.width(), clampedSize.height() / size.height());
ASSERT(!sizeNeedsClamping(clampedSize));
ASSERT(!sizeNeedsClamping(size, scale));
return clampedSize;
}
FloatRect ImageBuffer::clampedRect(const FloatRect& rect)
{
return FloatRect(rect.location(), clampedSize(rect.size()));
}
Vector<uint8_t> ImageBuffer::toBGRAData() const
{
#if USE(CG)
if (context().isAcceleratedContext())
flushContext();
return m_data.toBGRAData(context().isAcceleratedContext(), m_size.width(), m_size.height());
#else
// FIXME: Implement this for other backends.
return { };
#endif
}
#if !(USE(CG) || USE(DIRECT2D))
FloatSize ImageBuffer::sizeForDestinationSize(FloatSize size) const
{
return size;
}
void ImageBuffer::transformColorSpace(ColorSpace srcColorSpace, ColorSpace dstColorSpace)
{
if (srcColorSpace == dstColorSpace)
return;
// only sRGB <-> linearRGB are supported at the moment
if ((srcColorSpace != ColorSpaceLinearRGB && srcColorSpace != ColorSpaceDeviceRGB)
|| (dstColorSpace != ColorSpaceLinearRGB && dstColorSpace != ColorSpaceDeviceRGB))
return;
if (dstColorSpace == ColorSpaceLinearRGB) {
static const std::array<uint8_t, 256> linearRgbLUT = [] {
std::array<uint8_t, 256> array;
for (unsigned i = 0; i < 256; i++) {
float color = i / 255.0f;
color = (color <= 0.04045f ? color / 12.92f : pow((color + 0.055f) / 1.055f, 2.4f));
color = std::max(0.0f, color);
color = std::min(1.0f, color);
array[i] = static_cast<uint8_t>(round(color * 255));
}
return array;
}();
platformTransformColorSpace(linearRgbLUT);
} else if (dstColorSpace == ColorSpaceDeviceRGB) {
static const std::array<uint8_t, 256> deviceRgbLUT= [] {
std::array<uint8_t, 256> array;
for (unsigned i = 0; i < 256; i++) {
float color = i / 255.0f;
color = (powf(color, 1.0f / 2.4f) * 1.055f) - 0.055f;
color = std::max(0.0f, color);
color = std::min(1.0f, color);
array[i] = static_cast<uint8_t>(round(color * 255));
}
return array;
}();
platformTransformColorSpace(deviceRgbLUT);
}
}
#endif // USE(CG)
inline void ImageBuffer::genericConvertToLuminanceMask()
{
IntRect luminanceRect(IntPoint(), internalSize());
RefPtr<Uint8ClampedArray> srcPixelArray = getUnmultipliedImageData(luminanceRect);
unsigned pixelArrayLength = srcPixelArray->length();
for (unsigned pixelOffset = 0; pixelOffset < pixelArrayLength; pixelOffset += 4) {
unsigned char a = srcPixelArray->item(pixelOffset + 3);
if (!a)
continue;
unsigned char r = srcPixelArray->item(pixelOffset);
unsigned char g = srcPixelArray->item(pixelOffset + 1);
unsigned char b = srcPixelArray->item(pixelOffset + 2);
double luma = (r * 0.2125 + g * 0.7154 + b * 0.0721) * ((double)a / 255.0);
srcPixelArray->set(pixelOffset + 3, luma);
}
putByteArray(Unmultiplied, srcPixelArray.get(), luminanceRect.size(), luminanceRect, IntPoint());
}
void ImageBuffer::convertToLuminanceMask()
{
// Add platform specific functions with platformConvertToLuminanceMask here later.
genericConvertToLuminanceMask();
}
#if !USE(CAIRO)
PlatformLayer* ImageBuffer::platformLayer() const
{
return 0;
}
bool ImageBuffer::copyToPlatformTexture(GraphicsContext3D&, GC3Denum, Platform3DObject, GC3Denum, bool, bool)
{
return false;
}
#endif
std::unique_ptr<ImageBuffer> ImageBuffer::createCompatibleBuffer(const FloatSize& size, ColorSpace colorSpace, const GraphicsContext& context)
{
if (size.isEmpty())
return nullptr;
IntSize scaledSize = ImageBuffer::compatibleBufferSize(size, context);
auto buffer = ImageBuffer::createCompatibleBuffer(scaledSize, 1, colorSpace, context);
if (!buffer)
return nullptr;
// Set up a corresponding scale factor on the graphics context.
buffer->context().scale(FloatSize(scaledSize.width() / size.width(), scaledSize.height() / size.height()));
return buffer;
}
std::unique_ptr<ImageBuffer> ImageBuffer::createCompatibleBuffer(const FloatSize& size, float resolutionScale, ColorSpace colorSpace, const GraphicsContext& context)
{
return create(size, context.renderingMode(), resolutionScale, colorSpace);
}
IntSize ImageBuffer::compatibleBufferSize(const FloatSize& size, const GraphicsContext& context)
{
// Enlarge the buffer size if the context's transform is scaling it so we need a higher
// resolution than one pixel per unit.
return expandedIntSize(size * context.scaleFactor());
}
bool ImageBuffer::isCompatibleWithContext(const GraphicsContext& context) const
{
return areEssentiallyEqual(context.scaleFactor(), this->context().scaleFactor());
}
#if !USE(IOSURFACE_CANVAS_BACKING_STORE)
size_t ImageBuffer::memoryCost() const
{
// memoryCost() may be invoked concurrently from a GC thread, and we need to be careful about what data we access here and how.
// It's safe to access internalSize() because it doesn't do any pointer chasing.
return 4 * internalSize().width() * internalSize().height();
}
size_t ImageBuffer::externalMemoryCost() const
{
return 0;
}
#endif
}
|