1/* 2 * Copyright (C) 2006 Nikolas Zimmermann <zimmermann@kde.org> 3 * Copyright (C) 2007, 2008, 2009 Apple Inc. All rights reserved. 4 * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved. 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 * 15 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY 16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR 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#ifndef ImageBuffer_h 29#define ImageBuffer_h 30 31#include "AffineTransform.h" 32#include "ColorSpace.h" 33#include "GraphicsTypes.h" 34#include "GraphicsTypes3D.h" 35#include "IntSize.h" 36#include "ImageBufferData.h" 37#include "PlatformLayer.h" 38#include <runtime/Uint8ClampedArray.h> 39#include <wtf/Forward.h> 40#include <wtf/OwnPtr.h> 41#include <wtf/PassRefPtr.h> 42#include <wtf/Vector.h> 43 44namespace WebCore { 45 46class FloatRect; 47class GraphicsContext; 48class GraphicsContext3D; 49class Image; 50class ImageData; 51class IntPoint; 52class IntRect; 53 54enum Multiply { 55 Premultiplied, 56 Unmultiplied 57}; 58 59enum RenderingMode { 60 Unaccelerated, 61 UnacceleratedNonPlatformBuffer, // Use plain memory allocation rather than platform API to allocate backing store. 62 Accelerated 63}; 64 65enum BackingStoreCopy { 66 CopyBackingStore, // Guarantee subsequent draws don't affect the copy. 67 DontCopyBackingStore // Subsequent draws may affect the copy. 68}; 69 70enum ScaleBehavior { 71 Scaled, 72 Unscaled 73}; 74 75class ImageBuffer { 76 WTF_MAKE_NONCOPYABLE(ImageBuffer); WTF_MAKE_FAST_ALLOCATED; 77public: 78 // Will return a null pointer on allocation failure. 79 static std::unique_ptr<ImageBuffer> create(const FloatSize& size, float resolutionScale = 1, ColorSpace colorSpace = ColorSpaceDeviceRGB, RenderingMode renderingMode = Unaccelerated) 80 { 81 bool success = false; 82 std::unique_ptr<ImageBuffer> buffer(new ImageBuffer(size, resolutionScale, colorSpace, renderingMode, success)); 83 if (!success) 84 return nullptr; 85 return buffer; 86 } 87 88 static std::unique_ptr<ImageBuffer> createCompatibleBuffer(const FloatSize&, float resolutionScale, ColorSpace, const GraphicsContext*, bool hasAlpha); 89 90 ~ImageBuffer(); 91 92 // The actual resolution of the backing store 93 const IntSize& internalSize() const { return m_size; } 94 const IntSize& logicalSize() const { return m_logicalSize; } 95 96 GraphicsContext* context() const; 97 98 PassRefPtr<Image> copyImage(BackingStoreCopy = CopyBackingStore, ScaleBehavior = Scaled) const; 99 // Give hints on the faster copyImage Mode, return DontCopyBackingStore if it supports the DontCopyBackingStore behavior 100 // or return CopyBackingStore if it doesn't. 101 static BackingStoreCopy fastCopyImageMode(); 102 103 enum CoordinateSystem { LogicalCoordinateSystem, BackingStoreCoordinateSystem }; 104 105 PassRefPtr<Uint8ClampedArray> getUnmultipliedImageData(const IntRect&, CoordinateSystem = LogicalCoordinateSystem) const; 106 PassRefPtr<Uint8ClampedArray> getPremultipliedImageData(const IntRect&, CoordinateSystem = LogicalCoordinateSystem) const; 107 108 void putByteArray(Multiply multiplied, Uint8ClampedArray*, const IntSize& sourceSize, const IntRect& sourceRect, const IntPoint& destPoint, CoordinateSystem = LogicalCoordinateSystem); 109 110 void convertToLuminanceMask(); 111 112 String toDataURL(const String& mimeType, const double* quality = 0, CoordinateSystem = LogicalCoordinateSystem) const; 113#if !USE(CG) 114 AffineTransform baseTransform() const { return AffineTransform(); } 115 void transformColorSpace(ColorSpace srcColorSpace, ColorSpace dstColorSpace); 116 void platformTransformColorSpace(const Vector<int>&); 117#else 118 AffineTransform baseTransform() const { return AffineTransform(1, 0, 0, -1, 0, m_data.m_backingStoreSize.height()); } 119#endif 120 PlatformLayer* platformLayer() const; 121 122 // FIXME: current implementations of this method have the restriction that they only work 123 // with textures that are RGB or RGBA format, and UNSIGNED_BYTE type. 124 bool copyToPlatformTexture(GraphicsContext3D&, Platform3DObject, GC3Denum, bool, bool); 125 126 FloatSize spaceSize() const { return m_space; } 127 void setSpaceSize(const FloatSize& space) 128 { 129 m_space = space; 130 } 131 132private: 133#if USE(CG) 134 // The returned image might be larger than the internalSize(). If you want the smaller 135 // image, crop the result. 136 RetainPtr<CGImageRef> copyNativeImage(BackingStoreCopy = CopyBackingStore) const; 137 void flushContext() const; 138#endif 139 void clip(GraphicsContext*, const FloatRect&) const; 140 141 void draw(GraphicsContext*, ColorSpace, const FloatRect& destRect, const FloatRect& srcRect = FloatRect(0, 0, -1, -1), CompositeOperator = CompositeSourceOver, BlendMode = BlendModeNormal, bool useLowQualityScale = false); 142 void drawPattern(GraphicsContext*, const FloatRect& srcRect, const AffineTransform& patternTransform, const FloatPoint& phase, ColorSpace styleColorSpace, CompositeOperator, const FloatRect& destRect, BlendMode = BlendModeNormal); 143 144 inline void genericConvertToLuminanceMask(); 145 146 friend class GraphicsContext; 147 friend class GeneratedImage; 148 friend class CrossfadeGeneratedImage; 149 friend class GradientImage; 150 151private: 152 ImageBufferData m_data; 153 IntSize m_size; 154 IntSize m_logicalSize; 155 float m_resolutionScale; 156 OwnPtr<GraphicsContext> m_context; 157 FloatSize m_space; 158 159 // This constructor will place its success into the given out-variable 160 // so that create() knows when it should return failure. 161 ImageBuffer(const FloatSize&, float resolutionScale, ColorSpace, RenderingMode, bool& success); 162}; 163 164#if USE(CG) 165String ImageDataToDataURL(const ImageData&, const String& mimeType, const double* quality); 166#endif 167 168} // namespace WebCore 169 170#endif // ImageBuffer_h 171