blob: f419d327d91f98b50ad5be1d87a496b0fcf7d4c6 [file] [log] [blame]
oliver962ed8082006-10-31 02:33:47 +00001/*
oliver7cf2d682006-10-31 04:17:43 +00002 * Copyright (C) 2006 Nikolas Zimmermann <zimmermann@kde.org>
ddkilzer@apple.com3d46b2d2009-05-23 16:39:34 +00003 * Copyright (C) 2007, 2008, 2009 Apple Inc. All rights reserved.
weinig@apple.comc04cf942010-06-01 01:23:52 +00004 * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved.
oliver962ed8082006-10-31 02:33:47 +00005 *
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.com92047332014-03-15 04:08:27 +000015 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
oliver962ed8082006-10-31 02:33:47 +000016 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
mjs@apple.com92047332014-03-15 04:08:27 +000018 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
oliver962ed8082006-10-31 02:33:47 +000019 * 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
eseidel8eddecf2007-01-16 00:49:43 +000028#ifndef ImageBuffer_h
29#define ImageBuffer_h
darin6de7ece2006-11-06 06:05:51 +000030
krit@webkit.org2b95a9a2010-02-08 20:30:42 +000031#include "AffineTransform.h"
krit@webkit.org2f1d5352010-10-20 13:09:50 +000032#include "ColorSpace.h"
tonyg@chromium.org9c384212011-01-19 23:13:26 +000033#include "GraphicsTypes.h"
commit-queue@webkit.org8a3af352012-06-22 20:45:52 +000034#include "GraphicsTypes3D.h"
oliver7cf2d682006-10-31 04:17:43 +000035#include "IntSize.h"
brettw@chromium.org6eee5b02008-10-28 23:10:42 +000036#include "ImageBufferData.h"
abucur@adobe.com46b0fd52014-01-30 10:22:36 +000037#include "PlatformLayer.h"
oliver@apple.comdf606082013-08-05 22:11:50 +000038#include <runtime/Uint8ClampedArray.h>
barraclough@apple.comd218c2d2010-08-10 00:38:14 +000039#include <wtf/Forward.h>
oliver@apple.comae996a92008-02-23 01:16:16 +000040#include <wtf/PassRefPtr.h>
tonyg@chromium.org9c384212011-01-19 23:13:26 +000041#include <wtf/Vector.h>
oliver962ed8082006-10-31 02:33:47 +000042
oliver962ed8082006-10-31 02:33:47 +000043namespace WebCore {
44
simon.fraser@apple.com6ce84df2014-04-18 18:35:20 +000045class FloatRect;
46class GraphicsContext;
47class GraphicsContext3D;
48class Image;
49class ImageData;
50class IntPoint;
51class IntRect;
krit@webkit.org84792472009-08-08 19:11:35 +000052
simon.fraser@apple.com6ce84df2014-04-18 18:35:20 +000053enum Multiply {
54 Premultiplied,
55 Unmultiplied
56};
krit@webkit.orgacfb1ce2009-08-12 10:11:03 +000057
simon.fraser@apple.com6ce84df2014-04-18 18:35:20 +000058enum BackingStoreCopy {
59 CopyBackingStore, // Guarantee subsequent draws don't affect the copy.
60 DontCopyBackingStore // Subsequent draws may affect the copy.
61};
62
63enum ScaleBehavior {
64 Scaled,
65 Unscaled
66};
67
68class ImageBuffer {
69 WTF_MAKE_NONCOPYABLE(ImageBuffer); WTF_MAKE_FAST_ALLOCATED;
timothy_horton@apple.com1b3fb692015-12-14 05:27:45 +000070 friend class IOSurface;
simon.fraser@apple.com6ce84df2014-04-18 18:35:20 +000071public:
72 // Will return a null pointer on allocation failure.
simon.fraser@apple.comf21302e2015-11-08 06:31:50 +000073 static std::unique_ptr<ImageBuffer> create(const FloatSize& size, RenderingMode renderingMode, float resolutionScale = 1, ColorSpace colorSpace = ColorSpaceSRGB)
simon.fraser@apple.com6ce84df2014-04-18 18:35:20 +000074 {
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.coma93d7ef2015-08-29 06:15:28 +000082 static std::unique_ptr<ImageBuffer> createCompatibleBuffer(const FloatSize&, float resolutionScale, ColorSpace, const GraphicsContext&, bool hasAlpha);
simon.fraser@apple.com6ce84df2014-04-18 18:35:20 +000083
commit-queue@webkit.org8cc75642014-08-22 18:25:37 +000084 WEBCORE_EXPORT ~ImageBuffer();
simon.fraser@apple.com6ce84df2014-04-18 18:35:20 +000085
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.com32e05302016-01-06 04:12:47 +000090 FloatSize sizeForDestinationSize(FloatSize) const;
91
timothy_horton@apple.com07cddc62015-07-24 00:19:58 +000092 float resolutionScale() const { return m_resolutionScale; }
93
mmaxfield@apple.coma93d7ef2015-08-29 06:15:28 +000094 WEBCORE_EXPORT GraphicsContext& context() const;
simon.fraser@apple.com6ce84df2014-04-18 18:35:20 +000095
darin@apple.com3d1d5fc2015-04-23 05:20:23 +000096 WEBCORE_EXPORT RefPtr<Image> copyImage(BackingStoreCopy = CopyBackingStore, ScaleBehavior = Scaled) const;
timothy_horton@apple.com1b3fb692015-12-14 05:27:45 +000097 WEBCORE_EXPORT static RefPtr<Image> sinkIntoImage(std::unique_ptr<ImageBuffer>, ScaleBehavior = Scaled);
simon.fraser@apple.com6ce84df2014-04-18 18:35:20 +000098 // 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.com0e72db9b2011-07-21 20:49:38 +0000108
simon.fraser@apple.com6ce84df2014-04-18 18:35:20 +0000109 void convertToLuminanceMask();
110
111 String toDataURL(const String& mimeType, const double* quality = 0, CoordinateSystem = LogicalCoordinateSystem) const;
paroga@webkit.orgf226d9f2011-04-17 10:04:48 +0000112#if !USE(CG)
simon.fraser@apple.com6ce84df2014-04-18 18:35:20 +0000113 AffineTransform baseTransform() const { return AffineTransform(); }
114 void transformColorSpace(ColorSpace srcColorSpace, ColorSpace dstColorSpace);
115 void platformTransformColorSpace(const Vector<int>&);
krit@webkit.org76a08292008-12-07 07:48:53 +0000116#else
cdumez@apple.comff36da62015-03-10 01:24:14 +0000117 AffineTransform baseTransform() const { return AffineTransform(1, 0, 0, -1, 0, m_data.backingStoreSize.height()); }
krit@webkit.org76a08292008-12-07 07:48:53 +0000118#endif
simon.fraser@apple.com6ce84df2014-04-18 18:35:20 +0000119 PlatformLayer* platformLayer() const;
hyatt@apple.comffec3a42010-08-16 20:44:01 +0000120
simon.fraser@apple.com32e05302016-01-06 04:12:47 +0000121#if USE(CAIRO)
122 NativeImagePtr nativeImage() const;
123#endif
124
simon.fraser@apple.com6ce84df2014-04-18 18:35:20 +0000125 // 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.orgf9aa8442015-09-01 17:40:00 +0000127 bool copyToPlatformTexture(GraphicsContext3D&, GC3Denum, Platform3DObject, GC3Denum, bool, bool);
commit-queue@webkit.org8a3af352012-06-22 20:45:52 +0000128
commit-queue@webkit.orgef9df182015-05-07 22:46:43 +0000129 // These functions are used when clamping the ImageBuffer which is created for filter, masker or clipper.
said@apple.comb5a71a92015-05-07 23:32:45 +0000130 static bool sizeNeedsClamping(const FloatSize&);
131 static bool sizeNeedsClamping(const FloatSize&, FloatSize& scale);
commit-queue@webkit.orgef9df182015-05-07 22:46:43 +0000132 static FloatSize clampedSize(const FloatSize&);
133 static FloatSize clampedSize(const FloatSize&, FloatSize& scale);
134 static FloatRect clampedRect(const FloatRect&);
commit-queue@webkit.orgf7608922013-08-30 12:58:10 +0000135
simon.fraser@apple.com6ce84df2014-04-18 18:35:20 +0000136private:
mdelaney@apple.com0e72db9b2011-07-21 20:49:38 +0000137#if USE(CG)
simon.fraser@apple.com6ce84df2014-04-18 18:35:20 +0000138 // 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.com1b3fb692015-12-14 05:27:45 +0000141 static RetainPtr<CGImageRef> sinkIntoNativeImage(std::unique_ptr<ImageBuffer>);
simon.fraser@apple.com6ce84df2014-04-18 18:35:20 +0000142 void flushContext() const;
mdelaney@apple.com0e72db9b2011-07-21 20:49:38 +0000143#endif
simon.fraser@apple.com32e05302016-01-06 04:12:47 +0000144
simon.fraser@apple.com3efd5552015-11-08 07:17:53 +0000145 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.com0e72db9b2011-07-21 20:49:38 +0000147
timothy_horton@apple.com1b3fb692015-12-14 05:27:45 +0000148 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.com6ce84df2014-04-18 18:35:20 +0000150 inline void genericConvertToLuminanceMask();
krit@webkit.org0995b2d2011-07-09 06:05:14 +0000151
simon.fraser@apple.com6ce84df2014-04-18 18:35:20 +0000152 friend class GraphicsContext;
153 friend class GeneratedImage;
154 friend class CrossfadeGeneratedImage;
dino@apple.com55692822015-06-19 01:13:37 +0000155 friend class NamedImageGeneratedImage;
simon.fraser@apple.com6ce84df2014-04-18 18:35:20 +0000156 friend class GradientImage;
hyatt@apple.comffec3a42010-08-16 20:44:01 +0000157
simon.fraser@apple.com6ce84df2014-04-18 18:35:20 +0000158private:
159 ImageBufferData m_data;
160 IntSize m_size;
161 IntSize m_logicalSize;
162 float m_resolutionScale;
brettw@chromium.org6eee5b02008-10-28 23:10:42 +0000163
simon.fraser@apple.com6ce84df2014-04-18 18:35:20 +0000164 // This constructor will place its success into the given out-variable
165 // so that create() knows when it should return failure.
commit-queue@webkit.org8cc75642014-08-22 18:25:37 +0000166 WEBCORE_EXPORT ImageBuffer(const FloatSize&, float resolutionScale, ColorSpace, RenderingMode, bool& success);
simon.fraser@apple.com6ce84df2014-04-18 18:35:20 +0000167};
oliver7cf2d682006-10-31 04:17:43 +0000168
mrobinson@webkit.org3f23d7a2013-04-26 16:24:37 +0000169#if USE(CG)
simon.fraser@apple.com6ce84df2014-04-18 18:35:20 +0000170String ImageDataToDataURL(const ImageData&, const String& mimeType, const double* quality);
commit-queue@webkit.orgfdb70f42011-03-16 03:26:48 +0000171#endif
172
weinig@apple.com98791622008-03-08 03:08:48 +0000173} // namespace WebCore
174
175#endif // ImageBuffer_h