krit@webkit.org | 8479247 | 2009-08-08 19:11:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 Dirk Schulze <krit@webkit.org> |
krit@webkit.org | 0995b2d | 2011-07-09 06:05:14 +0000 | [diff] [blame] | 3 | * Copyright (C) Research In Motion Limited 2011. All rights reserved. |
said@apple.com | 8eabfa2 | 2022-02-10 05:18:02 +0000 | [diff] [blame] | 4 | * Copyright (C) 2016-2022 Apple Inc. All rights reserved. |
krit@webkit.org | 8479247 | 2009-08-08 19:11:35 +0000 | [diff] [blame] | 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * |
mjs@apple.com | 9204733 | 2014-03-15 04:08:27 +0000 | [diff] [blame] | 15 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
krit@webkit.org | 8479247 | 2009-08-08 19:11:35 +0000 | [diff] [blame] | 16 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
mjs@apple.com | 9204733 | 2014-03-15 04:08:27 +0000 | [diff] [blame] | 18 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
krit@webkit.org | 8479247 | 2009-08-08 19:11:35 +0000 | [diff] [blame] | 19 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 20 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 22 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 23 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include "config.h" |
| 29 | #include "ImageBuffer.h" |
| 30 | |
commit-queue@webkit.org | f3676f5 | 2022-06-07 13:43:49 +0000 | [diff] [blame] | 31 | #include "BitmapImage.h" |
simon.fraser@apple.com | 6ce84df | 2014-04-18 18:35:20 +0000 | [diff] [blame] | 32 | #include "GraphicsContext.h" |
commit-queue@webkit.org | 1107a9b | 2020-02-25 18:49:00 +0000 | [diff] [blame] | 33 | #include "HostWindow.h" |
commit-queue@webkit.org | aa9a420 | 2020-02-19 01:12:47 +0000 | [diff] [blame] | 34 | #include "PlatformImageBuffer.h" |
krit@webkit.org | 8479247 | 2009-08-08 19:11:35 +0000 | [diff] [blame] | 35 | |
| 36 | namespace WebCore { |
| 37 | |
commit-queue@webkit.org | ef9df18 | 2015-05-07 22:46:43 +0000 | [diff] [blame] | 38 | static const float MaxClampedLength = 4096; |
| 39 | static const float MaxClampedArea = MaxClampedLength * MaxClampedLength; |
| 40 | |
simon.fraser@apple.com | 4d62676 | 2022-04-06 15:22:41 +0000 | [diff] [blame] | 41 | RefPtr<ImageBuffer> ImageBuffer::create(const FloatSize& size, RenderingPurpose purpose, float resolutionScale, const DestinationColorSpace& colorSpace, PixelFormat pixelFormat, OptionSet<ImageBufferOptions> options, const CreationContext& creationContext) |
wenson_hsieh@apple.com | 6937974 | 2020-02-29 03:00:22 +0000 | [diff] [blame] | 42 | { |
said@apple.com | 604a4f6 | 2020-11-03 19:38:41 +0000 | [diff] [blame] | 43 | RefPtr<ImageBuffer> imageBuffer; |
said@apple.com | 2135593 | 2021-02-24 17:58:32 +0000 | [diff] [blame] | 44 | |
simon.fraser@apple.com | 4d62676 | 2022-04-06 15:22:41 +0000 | [diff] [blame] | 45 | // Give UseDisplayList a higher precedence since it is a debug option. |
| 46 | if (options.contains(ImageBufferOptions::UseDisplayList)) { |
| 47 | if (options.contains(ImageBufferOptions::Accelerated)) |
simon.fraser@apple.com | 71d9495 | 2022-04-07 20:13:53 +0000 | [diff] [blame] | 48 | imageBuffer = DisplayListAcceleratedImageBuffer::create(size, resolutionScale, colorSpace, pixelFormat, purpose, creationContext); |
said@apple.com | e4d5737 | 2020-10-28 01:36:51 +0000 | [diff] [blame] | 49 | |
| 50 | if (!imageBuffer) |
simon.fraser@apple.com | 71d9495 | 2022-04-07 20:13:53 +0000 | [diff] [blame] | 51 | imageBuffer = DisplayListUnacceleratedImageBuffer::create(size, resolutionScale, colorSpace, pixelFormat, purpose, creationContext); |
wenson_hsieh@apple.com | 6937974 | 2020-02-29 03:00:22 +0000 | [diff] [blame] | 52 | } |
said@apple.com | 2135593 | 2021-02-24 17:58:32 +0000 | [diff] [blame] | 53 | |
simon.fraser@apple.com | 4d62676 | 2022-04-06 15:22:41 +0000 | [diff] [blame] | 54 | if (creationContext.hostWindow && !imageBuffer) { |
| 55 | auto renderingMode = options.contains(ImageBufferOptions::Accelerated) ? RenderingMode::Accelerated : RenderingMode::Unaccelerated; |
simon.fraser@apple.com | ce6dd1f | 2022-04-03 05:24:35 +0000 | [diff] [blame] | 56 | imageBuffer = creationContext.hostWindow->createImageBuffer(size, renderingMode, purpose, resolutionScale, colorSpace, pixelFormat); |
simon.fraser@apple.com | 4d62676 | 2022-04-06 15:22:41 +0000 | [diff] [blame] | 57 | } |
wenson_hsieh@apple.com | 6937974 | 2020-02-29 03:00:22 +0000 | [diff] [blame] | 58 | |
simon.fraser@apple.com | 4d62676 | 2022-04-06 15:22:41 +0000 | [diff] [blame] | 59 | if (imageBuffer) |
| 60 | return imageBuffer; |
said@apple.com | e4d5737 | 2020-10-28 01:36:51 +0000 | [diff] [blame] | 61 | |
simon.fraser@apple.com | 4d62676 | 2022-04-06 15:22:41 +0000 | [diff] [blame] | 62 | if (options.contains(ImageBufferOptions::Accelerated)) |
simon.fraser@apple.com | 71d9495 | 2022-04-07 20:13:53 +0000 | [diff] [blame] | 63 | imageBuffer = AcceleratedImageBuffer::create(size, resolutionScale, colorSpace, pixelFormat, purpose, creationContext); |
said@apple.com | e4d5737 | 2020-10-28 01:36:51 +0000 | [diff] [blame] | 64 | |
| 65 | if (!imageBuffer) |
simon.fraser@apple.com | 71d9495 | 2022-04-07 20:13:53 +0000 | [diff] [blame] | 66 | imageBuffer = UnacceleratedImageBuffer::create(size, resolutionScale, colorSpace, pixelFormat, purpose, creationContext); |
commit-queue@webkit.org | aa9a420 | 2020-02-19 01:12:47 +0000 | [diff] [blame] | 67 | |
| 68 | return imageBuffer; |
bfulgham@apple.com | 60d08fe | 2016-10-14 22:42:14 +0000 | [diff] [blame] | 69 | } |
| 70 | |
said@apple.com | 8eabfa2 | 2022-02-10 05:18:02 +0000 | [diff] [blame] | 71 | RefPtr<ImageBuffer> ImageBuffer::clone() const |
commit-queue@webkit.org | aa9a420 | 2020-02-19 01:12:47 +0000 | [diff] [blame] | 72 | { |
said@apple.com | a22fc22 | 2022-03-01 23:16:43 +0000 | [diff] [blame] | 73 | auto clone = context().createAlignedImageBuffer(logicalSize(), colorSpace()); |
said@apple.com | 8eabfa2 | 2022-02-10 05:18:02 +0000 | [diff] [blame] | 74 | if (!clone) |
commit-queue@webkit.org | aa9a420 | 2020-02-19 01:12:47 +0000 | [diff] [blame] | 75 | return nullptr; |
| 76 | |
said@apple.com | 8eabfa2 | 2022-02-10 05:18:02 +0000 | [diff] [blame] | 77 | clone->context().drawImageBuffer(const_cast<ImageBuffer&>(*this), FloatPoint()); |
| 78 | return clone; |
commit-queue@webkit.org | aa9a420 | 2020-02-19 01:12:47 +0000 | [diff] [blame] | 79 | } |
bfulgham@apple.com | 60d08fe | 2016-10-14 22:42:14 +0000 | [diff] [blame] | 80 | |
said@apple.com | b5a71a9 | 2015-05-07 23:32:45 +0000 | [diff] [blame] | 81 | bool ImageBuffer::sizeNeedsClamping(const FloatSize& size) |
commit-queue@webkit.org | ef9df18 | 2015-05-07 22:46:43 +0000 | [diff] [blame] | 82 | { |
said@apple.com | b5a71a9 | 2015-05-07 23:32:45 +0000 | [diff] [blame] | 83 | if (size.isEmpty()) |
commit-queue@webkit.org | ef9df18 | 2015-05-07 22:46:43 +0000 | [diff] [blame] | 84 | return false; |
| 85 | |
said@apple.com | b5a71a9 | 2015-05-07 23:32:45 +0000 | [diff] [blame] | 86 | return floorf(size.height()) * floorf(size.width()) > MaxClampedArea; |
commit-queue@webkit.org | ef9df18 | 2015-05-07 22:46:43 +0000 | [diff] [blame] | 87 | } |
| 88 | |
said@apple.com | b5a71a9 | 2015-05-07 23:32:45 +0000 | [diff] [blame] | 89 | bool ImageBuffer::sizeNeedsClamping(const FloatSize& size, FloatSize& scale) |
commit-queue@webkit.org | ef9df18 | 2015-05-07 22:46:43 +0000 | [diff] [blame] | 90 | { |
| 91 | FloatSize scaledSize(size); |
| 92 | scaledSize.scale(scale.width(), scale.height()); |
| 93 | |
said@apple.com | b5a71a9 | 2015-05-07 23:32:45 +0000 | [diff] [blame] | 94 | if (!sizeNeedsClamping(scaledSize)) |
| 95 | return false; |
commit-queue@webkit.org | ef9df18 | 2015-05-07 22:46:43 +0000 | [diff] [blame] | 96 | |
| 97 | // The area of scaled size is bigger than the upper limit, adjust the scale to fit. |
| 98 | scale.scale(sqrtf(MaxClampedArea / (scaledSize.width() * scaledSize.height()))); |
said@apple.com | b5a71a9 | 2015-05-07 23:32:45 +0000 | [diff] [blame] | 99 | ASSERT(!sizeNeedsClamping(size, scale)); |
| 100 | return true; |
commit-queue@webkit.org | ef9df18 | 2015-05-07 22:46:43 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | FloatSize ImageBuffer::clampedSize(const FloatSize& size) |
| 104 | { |
| 105 | return size.shrunkTo(FloatSize(MaxClampedLength, MaxClampedLength)); |
| 106 | } |
| 107 | |
| 108 | FloatSize ImageBuffer::clampedSize(const FloatSize& size, FloatSize& scale) |
| 109 | { |
| 110 | if (size.isEmpty()) |
| 111 | return size; |
| 112 | |
| 113 | FloatSize clampedSize = ImageBuffer::clampedSize(size); |
simon.fraser@apple.com | 726bd78 | 2017-09-20 06:13:54 +0000 | [diff] [blame] | 114 | scale = clampedSize / size; |
said@apple.com | b5a71a9 | 2015-05-07 23:32:45 +0000 | [diff] [blame] | 115 | ASSERT(!sizeNeedsClamping(clampedSize)); |
| 116 | ASSERT(!sizeNeedsClamping(size, scale)); |
commit-queue@webkit.org | ef9df18 | 2015-05-07 22:46:43 +0000 | [diff] [blame] | 117 | return clampedSize; |
| 118 | } |
| 119 | |
| 120 | FloatRect ImageBuffer::clampedRect(const FloatRect& rect) |
| 121 | { |
| 122 | return FloatRect(rect.location(), clampedSize(rect.size())); |
| 123 | } |
| 124 | |
commit-queue@webkit.org | f3676f5 | 2022-06-07 13:43:49 +0000 | [diff] [blame] | 125 | RefPtr<NativeImage> ImageBuffer::sinkIntoNativeImage(RefPtr<ImageBuffer> source) |
commit-queue@webkit.org | 0f9edcc | 2016-06-03 00:39:58 +0000 | [diff] [blame] | 126 | { |
commit-queue@webkit.org | f3676f5 | 2022-06-07 13:43:49 +0000 | [diff] [blame] | 127 | if (!source) |
| 128 | return nullptr; |
| 129 | return source->sinkIntoNativeImage(); |
commit-queue@webkit.org | 0f9edcc | 2016-06-03 00:39:58 +0000 | [diff] [blame] | 130 | } |
| 131 | |
commit-queue@webkit.org | f3676f5 | 2022-06-07 13:43:49 +0000 | [diff] [blame] | 132 | RefPtr<Image> ImageBuffer::copyImage(BackingStoreCopy copyBehavior, PreserveResolution preserveResolution) const |
junov@google.com | f50296b | 2012-12-05 21:40:26 +0000 | [diff] [blame] | 133 | { |
commit-queue@webkit.org | f3676f5 | 2022-06-07 13:43:49 +0000 | [diff] [blame] | 134 | RefPtr<NativeImage> image; |
| 135 | if (resolutionScale() == 1 || preserveResolution == PreserveResolution::Yes) |
| 136 | image = copyNativeImage(copyBehavior); |
| 137 | else { |
| 138 | auto copyBuffer = context().createImageBuffer(logicalSize(), 1.f, colorSpace()); |
| 139 | if (!copyBuffer) |
| 140 | return nullptr; |
| 141 | copyBuffer->context().drawImageBuffer(const_cast<ImageBuffer&>(*this), FloatPoint { }, CompositeOperator::Copy); |
| 142 | image = ImageBuffer::sinkIntoNativeImage(WTFMove(copyBuffer)); |
| 143 | } |
| 144 | if (!image) |
| 145 | return nullptr; |
| 146 | return BitmapImage::create(image.releaseNonNull()); |
| 147 | } |
| 148 | |
| 149 | RefPtr<Image> ImageBuffer::sinkIntoImage(RefPtr<ImageBuffer> source, PreserveResolution preserveResolution) |
| 150 | { |
| 151 | if (!source) |
| 152 | return nullptr; |
| 153 | RefPtr<NativeImage> image; |
| 154 | if (source->resolutionScale() == 1 || preserveResolution == PreserveResolution::Yes) |
| 155 | image = sinkIntoNativeImage(WTFMove(source)); |
| 156 | else { |
| 157 | auto copySize = source->logicalSize(); |
| 158 | auto copyBuffer = source->context().createImageBuffer(copySize, 1.f, source->colorSpace()); |
| 159 | if (!copyBuffer) |
| 160 | return nullptr; |
| 161 | drawConsuming(WTFMove(source), copyBuffer->context(), FloatRect { { }, copySize }, FloatRect { 0, 0, -1, -1 }, CompositeOperator::Copy); |
| 162 | image = ImageBuffer::sinkIntoNativeImage(WTFMove(copyBuffer)); |
| 163 | } |
| 164 | if (!image) |
| 165 | return nullptr; |
| 166 | return BitmapImage::create(image.releaseNonNull()); |
junov@google.com | f50296b | 2012-12-05 21:40:26 +0000 | [diff] [blame] | 167 | } |
commit-queue@webkit.org | 8a3af35 | 2012-06-22 20:45:52 +0000 | [diff] [blame] | 168 | |
said@apple.com | 604a4f6 | 2020-11-03 19:38:41 +0000 | [diff] [blame] | 169 | void ImageBuffer::drawConsuming(RefPtr<ImageBuffer> imageBuffer, GraphicsContext& context, const FloatRect& destRect, const FloatRect& srcRect, const ImagePaintingOptions& options) |
commit-queue@webkit.org | 0f9edcc | 2016-06-03 00:39:58 +0000 | [diff] [blame] | 170 | { |
commit-queue@webkit.org | aa9a420 | 2020-02-19 01:12:47 +0000 | [diff] [blame] | 171 | imageBuffer->drawConsuming(context, destRect, srcRect, options); |
commit-queue@webkit.org | 0f9edcc | 2016-06-03 00:39:58 +0000 | [diff] [blame] | 172 | } |
| 173 | |
commit-queue@webkit.org | aa9a420 | 2020-02-19 01:12:47 +0000 | [diff] [blame] | 174 | } // namespace WebCore |