oliver | 962ed808 | 2006-10-31 02:33:47 +0000 | [diff] [blame] | 1 | /* |
oliver | 7cf2d68 | 2006-10-31 04:17:43 +0000 | [diff] [blame] | 2 | * Copyright (C) 2006 Nikolas Zimmermann <zimmermann@kde.org> |
ddkilzer@apple.com | 3d46b2d | 2009-05-23 16:39:34 +0000 | [diff] [blame] | 3 | * Copyright (C) 2007, 2008, 2009 Apple Inc. All rights reserved. |
weinig@apple.com | c04cf94 | 2010-06-01 01:23:52 +0000 | [diff] [blame] | 4 | * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved. |
oliver | 962ed808 | 2006-10-31 02:33:47 +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 |
oliver | 962ed808 | 2006-10-31 02:33:47 +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 |
oliver | 962ed808 | 2006-10-31 02:33:47 +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 | |
eseidel | 8eddecf | 2007-01-16 00:49:43 +0000 | [diff] [blame] | 28 | #ifndef ImageBuffer_h |
| 29 | #define ImageBuffer_h |
darin | 6de7ece | 2006-11-06 06:05:51 +0000 | [diff] [blame] | 30 | |
krit@webkit.org | 2b95a9a | 2010-02-08 20:30:42 +0000 | [diff] [blame] | 31 | #include "AffineTransform.h" |
krit@webkit.org | 2f1d535 | 2010-10-20 13:09:50 +0000 | [diff] [blame] | 32 | #include "ColorSpace.h" |
tonyg@chromium.org | 9c38421 | 2011-01-19 23:13:26 +0000 | [diff] [blame] | 33 | #include "GraphicsTypes.h" |
commit-queue@webkit.org | 8a3af35 | 2012-06-22 20:45:52 +0000 | [diff] [blame] | 34 | #include "GraphicsTypes3D.h" |
oliver | 7cf2d68 | 2006-10-31 04:17:43 +0000 | [diff] [blame] | 35 | #include "IntSize.h" |
brettw@chromium.org | 6eee5b0 | 2008-10-28 23:10:42 +0000 | [diff] [blame] | 36 | #include "ImageBufferData.h" |
abucur@adobe.com | 46b0fd5 | 2014-01-30 10:22:36 +0000 | [diff] [blame] | 37 | #include "PlatformLayer.h" |
oliver@apple.com | df60608 | 2013-08-05 22:11:50 +0000 | [diff] [blame] | 38 | #include <runtime/Uint8ClampedArray.h> |
barraclough@apple.com | d218c2d | 2010-08-10 00:38:14 +0000 | [diff] [blame] | 39 | #include <wtf/Forward.h> |
oliver@apple.com | ae996a9 | 2008-02-23 01:16:16 +0000 | [diff] [blame] | 40 | #include <wtf/PassRefPtr.h> |
tonyg@chromium.org | 9c38421 | 2011-01-19 23:13:26 +0000 | [diff] [blame] | 41 | #include <wtf/Vector.h> |
oliver | 962ed808 | 2006-10-31 02:33:47 +0000 | [diff] [blame] | 42 | |
oliver | 962ed808 | 2006-10-31 02:33:47 +0000 | [diff] [blame] | 43 | namespace WebCore { |
| 44 | |
simon.fraser@apple.com | 6ce84df | 2014-04-18 18:35:20 +0000 | [diff] [blame] | 45 | class FloatRect; |
| 46 | class GraphicsContext; |
| 47 | class GraphicsContext3D; |
| 48 | class Image; |
| 49 | class ImageData; |
| 50 | class IntPoint; |
| 51 | class IntRect; |
krit@webkit.org | 8479247 | 2009-08-08 19:11:35 +0000 | [diff] [blame] | 52 | |
simon.fraser@apple.com | 6ce84df | 2014-04-18 18:35:20 +0000 | [diff] [blame] | 53 | enum Multiply { |
| 54 | Premultiplied, |
| 55 | Unmultiplied |
| 56 | }; |
krit@webkit.org | acfb1ce | 2009-08-12 10:11:03 +0000 | [diff] [blame] | 57 | |
simon.fraser@apple.com | 6ce84df | 2014-04-18 18:35:20 +0000 | [diff] [blame] | 58 | enum BackingStoreCopy { |
| 59 | CopyBackingStore, // Guarantee subsequent draws don't affect the copy. |
| 60 | DontCopyBackingStore // Subsequent draws may affect the copy. |
| 61 | }; |
| 62 | |
| 63 | enum ScaleBehavior { |
| 64 | Scaled, |
| 65 | Unscaled |
| 66 | }; |
| 67 | |
| 68 | class ImageBuffer { |
| 69 | WTF_MAKE_NONCOPYABLE(ImageBuffer); WTF_MAKE_FAST_ALLOCATED; |
timothy_horton@apple.com | 1b3fb69 | 2015-12-14 05:27:45 +0000 | [diff] [blame] | 70 | friend class IOSurface; |
simon.fraser@apple.com | 6ce84df | 2014-04-18 18:35:20 +0000 | [diff] [blame] | 71 | public: |
| 72 | // Will return a null pointer on allocation failure. |
simon.fraser@apple.com | f21302e | 2015-11-08 06:31:50 +0000 | [diff] [blame] | 73 | static std::unique_ptr<ImageBuffer> create(const FloatSize& size, RenderingMode renderingMode, float resolutionScale = 1, ColorSpace colorSpace = ColorSpaceSRGB) |
simon.fraser@apple.com | 6ce84df | 2014-04-18 18:35:20 +0000 | [diff] [blame] | 74 | { |
| 75 | bool success = false; |
| 76 | std::unique_ptr<ImageBuffer> buffer(new ImageBuffer(size, resolutionScale, colorSpace, renderingMode, success)); |
| 77 | if (!success) |
| 78 | return nullptr; |
| 79 | return buffer; |
| 80 | } |
| 81 | |
mmaxfield@apple.com | a93d7ef | 2015-08-29 06:15:28 +0000 | [diff] [blame] | 82 | static std::unique_ptr<ImageBuffer> createCompatibleBuffer(const FloatSize&, float resolutionScale, ColorSpace, const GraphicsContext&, bool hasAlpha); |
simon.fraser@apple.com | 6ce84df | 2014-04-18 18:35:20 +0000 | [diff] [blame] | 83 | |
commit-queue@webkit.org | 8cc7564 | 2014-08-22 18:25:37 +0000 | [diff] [blame] | 84 | WEBCORE_EXPORT ~ImageBuffer(); |
simon.fraser@apple.com | 6ce84df | 2014-04-18 18:35:20 +0000 | [diff] [blame] | 85 | |
| 86 | // The actual resolution of the backing store |
| 87 | const IntSize& internalSize() const { return m_size; } |
| 88 | const IntSize& logicalSize() const { return m_logicalSize; } |
| 89 | |
simon.fraser@apple.com | 32e0530 | 2016-01-06 04:12:47 +0000 | [diff] [blame] | 90 | FloatSize sizeForDestinationSize(FloatSize) const; |
| 91 | |
timothy_horton@apple.com | 07cddc6 | 2015-07-24 00:19:58 +0000 | [diff] [blame] | 92 | float resolutionScale() const { return m_resolutionScale; } |
| 93 | |
mmaxfield@apple.com | a93d7ef | 2015-08-29 06:15:28 +0000 | [diff] [blame] | 94 | WEBCORE_EXPORT GraphicsContext& context() const; |
simon.fraser@apple.com | 6ce84df | 2014-04-18 18:35:20 +0000 | [diff] [blame] | 95 | |
darin@apple.com | 3d1d5fc | 2015-04-23 05:20:23 +0000 | [diff] [blame] | 96 | WEBCORE_EXPORT RefPtr<Image> copyImage(BackingStoreCopy = CopyBackingStore, ScaleBehavior = Scaled) const; |
timothy_horton@apple.com | 1b3fb69 | 2015-12-14 05:27:45 +0000 | [diff] [blame] | 97 | WEBCORE_EXPORT static RefPtr<Image> sinkIntoImage(std::unique_ptr<ImageBuffer>, ScaleBehavior = Scaled); |
simon.fraser@apple.com | 6ce84df | 2014-04-18 18:35:20 +0000 | [diff] [blame] | 98 | // Give hints on the faster copyImage Mode, return DontCopyBackingStore if it supports the DontCopyBackingStore behavior |
| 99 | // or return CopyBackingStore if it doesn't. |
| 100 | static BackingStoreCopy fastCopyImageMode(); |
| 101 | |
| 102 | enum CoordinateSystem { LogicalCoordinateSystem, BackingStoreCoordinateSystem }; |
| 103 | |
| 104 | PassRefPtr<Uint8ClampedArray> getUnmultipliedImageData(const IntRect&, CoordinateSystem = LogicalCoordinateSystem) const; |
| 105 | PassRefPtr<Uint8ClampedArray> getPremultipliedImageData(const IntRect&, CoordinateSystem = LogicalCoordinateSystem) const; |
| 106 | |
| 107 | void putByteArray(Multiply multiplied, Uint8ClampedArray*, const IntSize& sourceSize, const IntRect& sourceRect, const IntPoint& destPoint, CoordinateSystem = LogicalCoordinateSystem); |
mdelaney@apple.com | 0e72db9b | 2011-07-21 20:49:38 +0000 | [diff] [blame] | 108 | |
simon.fraser@apple.com | 6ce84df | 2014-04-18 18:35:20 +0000 | [diff] [blame] | 109 | void convertToLuminanceMask(); |
| 110 | |
| 111 | String toDataURL(const String& mimeType, const double* quality = 0, CoordinateSystem = LogicalCoordinateSystem) const; |
paroga@webkit.org | f226d9f | 2011-04-17 10:04:48 +0000 | [diff] [blame] | 112 | #if !USE(CG) |
simon.fraser@apple.com | 6ce84df | 2014-04-18 18:35:20 +0000 | [diff] [blame] | 113 | AffineTransform baseTransform() const { return AffineTransform(); } |
| 114 | void transformColorSpace(ColorSpace srcColorSpace, ColorSpace dstColorSpace); |
| 115 | void platformTransformColorSpace(const Vector<int>&); |
krit@webkit.org | 76a0829 | 2008-12-07 07:48:53 +0000 | [diff] [blame] | 116 | #else |
cdumez@apple.com | ff36da6 | 2015-03-10 01:24:14 +0000 | [diff] [blame] | 117 | AffineTransform baseTransform() const { return AffineTransform(1, 0, 0, -1, 0, m_data.backingStoreSize.height()); } |
krit@webkit.org | 76a0829 | 2008-12-07 07:48:53 +0000 | [diff] [blame] | 118 | #endif |
simon.fraser@apple.com | 6ce84df | 2014-04-18 18:35:20 +0000 | [diff] [blame] | 119 | PlatformLayer* platformLayer() const; |
hyatt@apple.com | ffec3a4 | 2010-08-16 20:44:01 +0000 | [diff] [blame] | 120 | |
simon.fraser@apple.com | 32e0530 | 2016-01-06 04:12:47 +0000 | [diff] [blame] | 121 | #if USE(CAIRO) |
| 122 | NativeImagePtr nativeImage() const; |
| 123 | #endif |
| 124 | |
simon.fraser@apple.com | 6ce84df | 2014-04-18 18:35:20 +0000 | [diff] [blame] | 125 | // FIXME: current implementations of this method have the restriction that they only work |
| 126 | // with textures that are RGB or RGBA format, and UNSIGNED_BYTE type. |
commit-queue@webkit.org | f9aa844 | 2015-09-01 17:40:00 +0000 | [diff] [blame] | 127 | bool copyToPlatformTexture(GraphicsContext3D&, GC3Denum, Platform3DObject, GC3Denum, bool, bool); |
commit-queue@webkit.org | 8a3af35 | 2012-06-22 20:45:52 +0000 | [diff] [blame] | 128 | |
commit-queue@webkit.org | ef9df18 | 2015-05-07 22:46:43 +0000 | [diff] [blame] | 129 | // These functions are used when clamping the ImageBuffer which is created for filter, masker or clipper. |
said@apple.com | b5a71a9 | 2015-05-07 23:32:45 +0000 | [diff] [blame] | 130 | static bool sizeNeedsClamping(const FloatSize&); |
| 131 | static bool sizeNeedsClamping(const FloatSize&, FloatSize& scale); |
commit-queue@webkit.org | ef9df18 | 2015-05-07 22:46:43 +0000 | [diff] [blame] | 132 | static FloatSize clampedSize(const FloatSize&); |
| 133 | static FloatSize clampedSize(const FloatSize&, FloatSize& scale); |
| 134 | static FloatRect clampedRect(const FloatRect&); |
commit-queue@webkit.org | f760892 | 2013-08-30 12:58:10 +0000 | [diff] [blame] | 135 | |
simon.fraser@apple.com | 6ce84df | 2014-04-18 18:35:20 +0000 | [diff] [blame] | 136 | private: |
mdelaney@apple.com | 0e72db9b | 2011-07-21 20:49:38 +0000 | [diff] [blame] | 137 | #if USE(CG) |
simon.fraser@apple.com | 6ce84df | 2014-04-18 18:35:20 +0000 | [diff] [blame] | 138 | // The returned image might be larger than the internalSize(). If you want the smaller |
| 139 | // image, crop the result. |
| 140 | RetainPtr<CGImageRef> copyNativeImage(BackingStoreCopy = CopyBackingStore) const; |
timothy_horton@apple.com | 1b3fb69 | 2015-12-14 05:27:45 +0000 | [diff] [blame] | 141 | static RetainPtr<CGImageRef> sinkIntoNativeImage(std::unique_ptr<ImageBuffer>); |
simon.fraser@apple.com | 6ce84df | 2014-04-18 18:35:20 +0000 | [diff] [blame] | 142 | void flushContext() const; |
mdelaney@apple.com | 0e72db9b | 2011-07-21 20:49:38 +0000 | [diff] [blame] | 143 | #endif |
simon.fraser@apple.com | 32e0530 | 2016-01-06 04:12:47 +0000 | [diff] [blame] | 144 | |
simon.fraser@apple.com | 3efd555 | 2015-11-08 07:17:53 +0000 | [diff] [blame] | 145 | void draw(GraphicsContext&, const FloatRect& destRect, const FloatRect& srcRect = FloatRect(0, 0, -1, -1), CompositeOperator = CompositeSourceOver, BlendMode = BlendModeNormal, bool useLowQualityScale = false); |
| 146 | void drawPattern(GraphicsContext&, const FloatRect& srcRect, const AffineTransform& patternTransform, const FloatPoint& phase, const FloatSize& spacing, CompositeOperator, const FloatRect& destRect, BlendMode = BlendModeNormal); |
mdelaney@apple.com | 0e72db9b | 2011-07-21 20:49:38 +0000 | [diff] [blame] | 147 | |
timothy_horton@apple.com | 1b3fb69 | 2015-12-14 05:27:45 +0000 | [diff] [blame] | 148 | static void drawConsuming(std::unique_ptr<ImageBuffer>, GraphicsContext&, const FloatRect& destRect, const FloatRect& srcRect = FloatRect(0, 0, -1, -1), CompositeOperator = CompositeSourceOver, BlendMode = BlendModeNormal, bool useLowQualityScale = false); |
| 149 | |
simon.fraser@apple.com | 6ce84df | 2014-04-18 18:35:20 +0000 | [diff] [blame] | 150 | inline void genericConvertToLuminanceMask(); |
krit@webkit.org | 0995b2d | 2011-07-09 06:05:14 +0000 | [diff] [blame] | 151 | |
simon.fraser@apple.com | 6ce84df | 2014-04-18 18:35:20 +0000 | [diff] [blame] | 152 | friend class GraphicsContext; |
| 153 | friend class GeneratedImage; |
| 154 | friend class CrossfadeGeneratedImage; |
dino@apple.com | 5569282 | 2015-06-19 01:13:37 +0000 | [diff] [blame] | 155 | friend class NamedImageGeneratedImage; |
simon.fraser@apple.com | 6ce84df | 2014-04-18 18:35:20 +0000 | [diff] [blame] | 156 | friend class GradientImage; |
hyatt@apple.com | ffec3a4 | 2010-08-16 20:44:01 +0000 | [diff] [blame] | 157 | |
simon.fraser@apple.com | 6ce84df | 2014-04-18 18:35:20 +0000 | [diff] [blame] | 158 | private: |
| 159 | ImageBufferData m_data; |
| 160 | IntSize m_size; |
| 161 | IntSize m_logicalSize; |
| 162 | float m_resolutionScale; |
brettw@chromium.org | 6eee5b0 | 2008-10-28 23:10:42 +0000 | [diff] [blame] | 163 | |
simon.fraser@apple.com | 6ce84df | 2014-04-18 18:35:20 +0000 | [diff] [blame] | 164 | // This constructor will place its success into the given out-variable |
| 165 | // so that create() knows when it should return failure. |
commit-queue@webkit.org | 8cc7564 | 2014-08-22 18:25:37 +0000 | [diff] [blame] | 166 | WEBCORE_EXPORT ImageBuffer(const FloatSize&, float resolutionScale, ColorSpace, RenderingMode, bool& success); |
simon.fraser@apple.com | 6ce84df | 2014-04-18 18:35:20 +0000 | [diff] [blame] | 167 | }; |
oliver | 7cf2d68 | 2006-10-31 04:17:43 +0000 | [diff] [blame] | 168 | |
mrobinson@webkit.org | 3f23d7a | 2013-04-26 16:24:37 +0000 | [diff] [blame] | 169 | #if USE(CG) |
simon.fraser@apple.com | 6ce84df | 2014-04-18 18:35:20 +0000 | [diff] [blame] | 170 | String ImageDataToDataURL(const ImageData&, const String& mimeType, const double* quality); |
commit-queue@webkit.org | fdb70f4 | 2011-03-16 03:26:48 +0000 | [diff] [blame] | 171 | #endif |
| 172 | |
weinig@apple.com | 9879162 | 2008-03-08 03:08:48 +0000 | [diff] [blame] | 173 | } // namespace WebCore |
| 174 | |
| 175 | #endif // ImageBuffer_h |