Use "= default" to denote default constructor or destructor
[WebKit-https.git] / Source / WebCore / platform / graphics / cg / ImageBufferCG.cpp
1 /*
2  * Copyright (C) 2006 Nikolas Zimmermann <zimmermann@kde.org>
3  * Copyright (C) 2008-2017 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 #include "config.h"
29 #include "ImageBuffer.h"
30
31 #if USE(CG)
32
33 #include "BitmapImage.h"
34 #include "GraphicsContext.h"
35 #include "GraphicsContextCG.h"
36 #include "ImageData.h"
37 #include "IntRect.h"
38 #include "MIMETypeRegistry.h"
39 #include <math.h>
40 #include <CoreGraphics/CoreGraphics.h>
41 #include <ImageIO/ImageIO.h>
42 #include <wtf/Assertions.h>
43 #include <wtf/CheckedArithmetic.h>
44 #include <wtf/MainThread.h>
45 #include <wtf/RetainPtr.h>
46 #include <wtf/text/Base64.h>
47 #include <wtf/text/WTFString.h>
48
49 #if PLATFORM(COCOA)
50 #include "UTIUtilities.h"
51 #endif
52
53 #if USE(IOSURFACE_CANVAS_BACKING_STORE)
54 #include "IOSurface.h"
55 #include <pal/spi/cocoa/IOSurfaceSPI.h>
56 #endif
57
58 // CA uses ARGB32 for textures and ARGB32 -> ARGB32 resampling is optimized.
59 #define USE_ARGB32 PLATFORM(IOS)
60
61 namespace WebCore {
62
63 static FloatSize scaleSizeToUserSpace(const FloatSize& logicalSize, const IntSize& backingStoreSize, const IntSize& internalSize)
64 {
65     float xMagnification = static_cast<float>(backingStoreSize.width()) / internalSize.width();
66     float yMagnification = static_cast<float>(backingStoreSize.height()) / internalSize.height();
67     return FloatSize(logicalSize.width() * xMagnification, logicalSize.height() * yMagnification);
68 }
69
70 std::unique_ptr<ImageBuffer> ImageBuffer::createCompatibleBuffer(const FloatSize& size, const GraphicsContext& context)
71 {
72     if (size.isEmpty())
73         return nullptr;
74
75     RetainPtr<CGColorSpaceRef> colorSpace;
76 #if PLATFORM(COCOA)
77     CGContextRef cgContext = context.platformContext();
78     switch (CGContextGetType(cgContext)) {
79     case kCGContextTypeBitmap:
80         colorSpace = CGBitmapContextGetColorSpace(cgContext);
81         break;
82 #if USE(IOSURFACE)
83     case kCGContextTypeIOSurface:
84         colorSpace = CGIOSurfaceContextGetColorSpace(cgContext);
85         break;
86 #endif
87     default:
88         colorSpace = adoptCF(CGContextCopyDeviceColorSpace(cgContext));
89     }
90
91     if (!colorSpace)
92         colorSpace = sRGBColorSpaceRef();
93 #else
94     colorSpace = sRGBColorSpaceRef();
95 #endif
96     RenderingMode renderingMode = context.renderingMode();
97     IntSize scaledSize = ImageBuffer::compatibleBufferSize(size, context);
98     bool success = false;
99     std::unique_ptr<ImageBuffer> buffer(new ImageBuffer(scaledSize, 1, colorSpace.get(), renderingMode, success));
100
101     if (!success)
102         return nullptr;
103
104     // Set up a corresponding scale factor on the graphics context.
105     buffer->context().scale(scaledSize / size);
106     return buffer;
107 }
108
109 ImageBuffer::ImageBuffer(const FloatSize& size, float resolutionScale, CGColorSpaceRef colorSpace, RenderingMode renderingMode, bool& success)
110     : m_logicalSize(size)
111     , m_resolutionScale(resolutionScale)
112 {
113     success = false; // Make early return mean failure.
114     float scaledWidth = ceilf(resolutionScale * size.width());
115     float scaledHeight = ceilf(resolutionScale * size.height());
116
117     // FIXME: Should we automatically use a lower resolution?
118     if (!FloatSize(scaledWidth, scaledHeight).isExpressibleAsIntSize())
119         return;
120
121     m_size = IntSize(scaledWidth, scaledHeight);
122     m_data.backingStoreSize = m_size;
123
124     bool accelerateRendering = renderingMode == Accelerated;
125     if (m_size.width() <= 0 || m_size.height() <= 0)
126         return;
127
128 #if USE(IOSURFACE_CANVAS_BACKING_STORE)
129     Checked<int, RecordOverflow> width = m_size.width();
130     Checked<int, RecordOverflow> height = m_size.height();
131 #endif
132
133     // Prevent integer overflows
134     m_data.bytesPerRow = 4 * Checked<unsigned, RecordOverflow>(m_data.backingStoreSize.width());
135     Checked<size_t, RecordOverflow> numBytes = Checked<unsigned, RecordOverflow>(m_data.backingStoreSize.height()) * m_data.bytesPerRow;
136     if (numBytes.hasOverflowed())
137         return;
138
139 #if USE(IOSURFACE_CANVAS_BACKING_STORE)
140     IntSize maxSize = IOSurface::maximumSize();
141     if (width.unsafeGet() > maxSize.width() || height.unsafeGet() > maxSize.height())
142         accelerateRendering = false;
143 #else
144     ASSERT(renderingMode == Unaccelerated);
145 #endif
146
147     m_data.colorSpace = colorSpace;
148
149     RetainPtr<CGContextRef> cgContext;
150     if (accelerateRendering) {
151 #if USE(IOSURFACE_CANVAS_BACKING_STORE)
152         FloatSize userBounds = sizeForDestinationSize(FloatSize(width.unsafeGet(), height.unsafeGet()));
153         m_data.surface = IOSurface::create(m_data.backingStoreSize, IntSize(userBounds), colorSpace);
154         if (m_data.surface) {
155             cgContext = m_data.surface->ensurePlatformContext();
156             if (cgContext)
157                 CGContextClearRect(cgContext.get(), FloatRect(FloatPoint(), userBounds));
158             else
159                 m_data.surface = nullptr;
160         }
161 #endif
162         if (!cgContext)
163             accelerateRendering = false; // If allocation fails, fall back to non-accelerated path.
164     }
165
166     if (!accelerateRendering) {
167         if (!tryFastCalloc(m_data.backingStoreSize.height(), m_data.bytesPerRow.unsafeGet()).getValue(m_data.data))
168             return;
169         ASSERT(!(reinterpret_cast<intptr_t>(m_data.data) & 3));
170
171 #if USE_ARGB32
172         m_data.bitmapInfo = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host;
173 #else
174         m_data.bitmapInfo = kCGImageAlphaPremultipliedLast;
175 #endif
176         cgContext = adoptCF(CGBitmapContextCreate(m_data.data, m_data.backingStoreSize.width(), m_data.backingStoreSize.height(), 8, m_data.bytesPerRow.unsafeGet(), m_data.colorSpace, m_data.bitmapInfo));
177         const auto releaseImageData = [] (void*, const void* data, size_t) {
178             fastFree(const_cast<void*>(data));
179         };
180         // Create a live image that wraps the data.
181         m_data.dataProvider = adoptCF(CGDataProviderCreateWithData(0, m_data.data, numBytes.unsafeGet(), releaseImageData));
182
183         if (!cgContext)
184             return;
185
186         m_data.context = std::make_unique<GraphicsContext>(cgContext.get());
187     }
188
189     context().scale(FloatSize(1, -1));
190     context().translate(0, -m_data.backingStoreSize.height());
191     context().applyDeviceScaleFactor(m_resolutionScale);
192
193     success = true;
194 }
195
196 ImageBuffer::ImageBuffer(const FloatSize& size, float resolutionScale, ColorSpace imageColorSpace, RenderingMode renderingMode, bool& success)
197     : ImageBuffer(size, resolutionScale, cachedCGColorSpace(imageColorSpace), renderingMode, success)
198 {
199 }
200
201 ImageBuffer::~ImageBuffer() = default;
202
203 FloatSize ImageBuffer::sizeForDestinationSize(FloatSize destinationSize) const
204 {
205     return scaleSizeToUserSpace(destinationSize, m_data.backingStoreSize, internalSize());
206 }
207
208 #if USE(IOSURFACE_CANVAS_BACKING_STORE)
209 size_t ImageBuffer::memoryCost() const
210 {
211     // memoryCost() may be invoked concurrently from a GC thread, and we need to be careful about what data we access here and how.
212     // It's safe to access internalSize() because it doesn't do any pointer chasing.
213     // It's safe to access m_data.surface because the surface can only be assigned during construction of this ImageBuffer.
214     // It's safe to access m_data.surface->totalBytes() because totalBytes() doesn't chase pointers.
215     if (m_data.surface)
216         return m_data.surface->totalBytes();
217     return 4 * internalSize().width() * internalSize().height();
218 }
219
220 size_t ImageBuffer::externalMemoryCost() const
221 {
222     // externalMemoryCost() may be invoked concurrently from a GC thread, and we need to be careful about what data we access here and how.
223     // It's safe to access m_data.surface because the surface can only be assigned during construction of this ImageBuffer.
224     // It's safe to access m_data.surface->totalBytes() because totalBytes() doesn't chase pointers.
225     if (m_data.surface)
226         return m_data.surface->totalBytes();
227     return 0;
228 }
229 #endif
230
231 GraphicsContext& ImageBuffer::context() const
232 {
233 #if USE(IOSURFACE_CANVAS_BACKING_STORE)
234     if (m_data.surface)
235         return m_data.surface->ensureGraphicsContext();
236 #endif
237     return *m_data.context;
238 }
239
240 void ImageBuffer::flushContext() const
241 {
242     CGContextFlush(context().platformContext());
243 }
244
245 static RetainPtr<CGImageRef> createCroppedImageIfNecessary(CGImageRef image, const IntSize& bounds)
246 {
247     if (image && (CGImageGetWidth(image) != static_cast<size_t>(bounds.width()) || CGImageGetHeight(image) != static_cast<size_t>(bounds.height())))
248         return adoptCF(CGImageCreateWithImageInRect(image, CGRectMake(0, 0, bounds.width(), bounds.height())));
249     return image;
250 }
251
252 static RefPtr<Image> createBitmapImageAfterScalingIfNeeded(RetainPtr<CGImageRef>&& image, IntSize internalSize, IntSize logicalSize, IntSize backingStoreSize, float resolutionScale, ScaleBehavior scaleBehavior)
253 {
254     if (resolutionScale == 1 || scaleBehavior == Unscaled)
255         image = createCroppedImageIfNecessary(image.get(), internalSize);
256     else {
257         auto context = adoptCF(CGBitmapContextCreate(0, logicalSize.width(), logicalSize.height(), 8, 4 * logicalSize.width(), sRGBColorSpaceRef(), kCGImageAlphaPremultipliedLast));
258         CGContextSetBlendMode(context.get(), kCGBlendModeCopy);
259         CGContextClipToRect(context.get(), FloatRect(FloatPoint::zero(), logicalSize));
260         FloatSize imageSizeInUserSpace = scaleSizeToUserSpace(logicalSize, backingStoreSize, internalSize);
261         CGContextDrawImage(context.get(), FloatRect(FloatPoint::zero(), imageSizeInUserSpace), image.get());
262         image = adoptCF(CGBitmapContextCreateImage(context.get()));
263     }
264
265     if (!image)
266         return nullptr;
267
268     return BitmapImage::create(WTFMove(image));
269 }
270
271 RefPtr<Image> ImageBuffer::copyImage(BackingStoreCopy copyBehavior, ScaleBehavior scaleBehavior) const
272 {
273     RetainPtr<CGImageRef> image;
274     if (m_resolutionScale == 1 || scaleBehavior == Unscaled)
275         image = copyNativeImage(copyBehavior);
276     else
277         image = copyNativeImage(DontCopyBackingStore);
278
279     return createBitmapImageAfterScalingIfNeeded(WTFMove(image), internalSize(), logicalSize(), m_data.backingStoreSize, m_resolutionScale, scaleBehavior);
280 }
281
282 RefPtr<Image> ImageBuffer::sinkIntoImage(std::unique_ptr<ImageBuffer> imageBuffer, ScaleBehavior scaleBehavior)
283 {
284     IntSize internalSize = imageBuffer->internalSize();
285     IntSize logicalSize = imageBuffer->logicalSize();
286     IntSize backingStoreSize = imageBuffer->m_data.backingStoreSize;
287     float resolutionScale = imageBuffer->m_resolutionScale;
288
289     return createBitmapImageAfterScalingIfNeeded(sinkIntoNativeImage(WTFMove(imageBuffer)), internalSize, logicalSize, backingStoreSize, resolutionScale, scaleBehavior);
290 }
291
292 BackingStoreCopy ImageBuffer::fastCopyImageMode()
293 {
294     return DontCopyBackingStore;
295 }
296
297 RetainPtr<CGImageRef> ImageBuffer::sinkIntoNativeImage(std::unique_ptr<ImageBuffer> imageBuffer)
298 {
299 #if USE(IOSURFACE_CANVAS_BACKING_STORE)
300     if (!imageBuffer->m_data.surface)
301         return imageBuffer->copyNativeImage(DontCopyBackingStore);
302
303     return IOSurface::sinkIntoImage(IOSurface::createFromImageBuffer(WTFMove(imageBuffer)));
304 #else
305     return imageBuffer->copyNativeImage(DontCopyBackingStore);
306 #endif
307 }
308
309 RetainPtr<CGImageRef> ImageBuffer::copyNativeImage(BackingStoreCopy copyBehavior) const
310 {
311     RetainPtr<CGImageRef> image;
312     if (!context().isAcceleratedContext()) {
313         switch (copyBehavior) {
314         case DontCopyBackingStore:
315             image = adoptCF(CGImageCreate(m_data.backingStoreSize.width(), m_data.backingStoreSize.height(), 8, 32, m_data.bytesPerRow.unsafeGet(), m_data.colorSpace, m_data.bitmapInfo, m_data.dataProvider.get(), 0, true, kCGRenderingIntentDefault));
316             break;
317         case CopyBackingStore:
318             image = adoptCF(CGBitmapContextCreateImage(context().platformContext()));
319             break;
320         default:
321             ASSERT_NOT_REACHED();
322             break;
323         }
324     }
325 #if USE(IOSURFACE_CANVAS_BACKING_STORE)
326     else
327         image = m_data.surface->createImage();
328 #endif
329
330     return image;
331 }
332
333 void ImageBuffer::drawConsuming(std::unique_ptr<ImageBuffer> imageBuffer, GraphicsContext& destContext, const FloatRect& destRect, const FloatRect& srcRect, CompositeOperator op, BlendMode blendMode)
334 {
335 #if USE(IOSURFACE_CANVAS_BACKING_STORE)
336     if (!imageBuffer->m_data.surface) {
337         imageBuffer->draw(destContext, destRect, srcRect, op, blendMode);
338         return;
339     }
340     
341     ASSERT(destContext.isAcceleratedContext());
342     
343     float resolutionScale = imageBuffer->m_resolutionScale;
344     IntSize backingStoreSize = imageBuffer->m_data.backingStoreSize;
345
346     RetainPtr<CGImageRef> image = IOSurface::sinkIntoImage(IOSurface::createFromImageBuffer(WTFMove(imageBuffer)));
347     
348     FloatRect adjustedSrcRect = srcRect;
349     adjustedSrcRect.scale(resolutionScale);
350     destContext.drawNativeImage(image.get(), backingStoreSize, destRect, adjustedSrcRect, op, blendMode);
351 #else
352     imageBuffer->draw(destContext, destRect, srcRect, op, blendMode);
353 #endif
354 }
355
356 void ImageBuffer::draw(GraphicsContext& destContext, const FloatRect& destRect, const FloatRect& srcRect, CompositeOperator op, BlendMode blendMode)
357 {
358     RetainPtr<CGImageRef> image;
359     if (&destContext == &context() || destContext.isAcceleratedContext())
360         image = copyNativeImage(CopyBackingStore); // Drawing into our own buffer, need to deep copy.
361     else
362         image = copyNativeImage(DontCopyBackingStore);
363
364     FloatRect adjustedSrcRect = srcRect;
365     adjustedSrcRect.scale(m_resolutionScale);
366     destContext.drawNativeImage(image.get(), m_data.backingStoreSize, destRect, adjustedSrcRect, op, blendMode);
367 }
368
369 void ImageBuffer::drawPattern(GraphicsContext& destContext, const FloatRect& destRect, const FloatRect& srcRect, const AffineTransform& patternTransform, const FloatPoint& phase, const FloatSize& spacing, CompositeOperator op, BlendMode blendMode)
370 {
371     FloatRect adjustedSrcRect = srcRect;
372     adjustedSrcRect.scale(m_resolutionScale);
373
374     if (!context().isAcceleratedContext()) {
375         if (&destContext == &context() || destContext.isAcceleratedContext()) {
376             if (RefPtr<Image> copy = copyImage(CopyBackingStore)) // Drawing into our own buffer, need to deep copy.
377                 copy->drawPattern(destContext, destRect, adjustedSrcRect, patternTransform, phase, spacing, op, blendMode);
378         } else {
379             if (RefPtr<Image> imageForRendering = copyImage(DontCopyBackingStore))
380                 imageForRendering->drawPattern(destContext, destRect, adjustedSrcRect, patternTransform, phase, spacing, op, blendMode);
381         }
382     } else {
383         if (RefPtr<Image> copy = copyImage(CopyBackingStore))
384             copy->drawPattern(destContext, destRect, adjustedSrcRect, patternTransform, phase, spacing, op, blendMode);
385     }
386 }
387
388 RefPtr<Uint8ClampedArray> ImageBuffer::getUnmultipliedImageData(const IntRect& rect, IntSize* pixelArrayDimensions, CoordinateSystem coordinateSystem) const
389 {
390     if (context().isAcceleratedContext())
391         flushContext();
392
393     IntRect srcRect = rect;
394     if (coordinateSystem == LogicalCoordinateSystem)
395         srcRect.scale(m_resolutionScale);
396
397     if (pixelArrayDimensions)
398         *pixelArrayDimensions = srcRect.size();
399
400     return m_data.getData(srcRect, internalSize(), context().isAcceleratedContext(), true, 1);
401 }
402
403 RefPtr<Uint8ClampedArray> ImageBuffer::getPremultipliedImageData(const IntRect& rect, IntSize* pixelArrayDimensions, CoordinateSystem coordinateSystem) const
404 {
405     if (context().isAcceleratedContext())
406         flushContext();
407
408     IntRect srcRect = rect;
409     if (coordinateSystem == LogicalCoordinateSystem)
410         srcRect.scale(m_resolutionScale);
411
412     if (pixelArrayDimensions)
413         *pixelArrayDimensions = srcRect.size();
414
415     return m_data.getData(srcRect, internalSize(), context().isAcceleratedContext(), false, 1);
416 }
417
418 void ImageBuffer::putByteArray(Multiply multiplied, Uint8ClampedArray* source, const IntSize& sourceSize, const IntRect& sourceRect, const IntPoint& destPoint, CoordinateSystem coordinateSystem)
419 {
420     if (context().isAcceleratedContext())
421         flushContext();
422
423     IntRect scaledSourceRect = sourceRect;
424     IntSize scaledSourceSize = sourceSize;
425     if (coordinateSystem == LogicalCoordinateSystem) {
426         scaledSourceRect.scale(m_resolutionScale);
427         scaledSourceSize.scale(m_resolutionScale);
428     }
429
430     m_data.putData(source, scaledSourceSize, scaledSourceRect, destPoint, internalSize(), context().isAcceleratedContext(), multiplied == Unmultiplied, 1);
431     
432     // Force recreating the IOSurface cached image if it is requested through CGIOSurfaceContextCreateImage().
433     // See https://bugs.webkit.org/show_bug.cgi?id=157966 for explaining why this is necessary.
434     if (context().isAcceleratedContext())
435         context().fillRect(FloatRect(1, 1, 0, 0));
436 }
437
438 static inline CFStringRef jpegUTI()
439 {
440 #if PLATFORM(IOS) || PLATFORM(WIN)
441     static const CFStringRef kUTTypeJPEG = CFSTR("public.jpeg");
442 #endif
443     return kUTTypeJPEG;
444 }
445     
446 static RetainPtr<CFStringRef> utiFromImageBufferMIMEType(const String& mimeType)
447 {
448     // FIXME: Why doesn't iOS use the CoreServices version?
449 #if PLATFORM(MAC)
450     return UTIFromMIMEType(mimeType).createCFString();
451 #else
452     ASSERT(isMainThread()); // It is unclear if CFSTR is threadsafe.
453
454     // FIXME: Add Windows support for all the supported UTIs when a way to convert from MIMEType to UTI reliably is found.
455     // For now, only support PNG, JPEG, and GIF. See <rdar://problem/6095286>.
456     static const CFStringRef kUTTypePNG = CFSTR("public.png");
457     static const CFStringRef kUTTypeGIF = CFSTR("com.compuserve.gif");
458
459     if (equalLettersIgnoringASCIICase(mimeType, "image/png"))
460         return kUTTypePNG;
461     if (equalLettersIgnoringASCIICase(mimeType, "image/jpeg"))
462         return jpegUTI();
463     if (equalLettersIgnoringASCIICase(mimeType, "image/gif"))
464         return kUTTypeGIF;
465
466     ASSERT_NOT_REACHED();
467     return kUTTypePNG;
468 #endif
469 }
470
471 static bool encodeImage(CGImageRef image, CFStringRef uti, std::optional<double> quality, CFMutableDataRef data)
472 {
473     if (!image || !uti || !data)
474         return false;
475
476     auto destination = adoptCF(CGImageDestinationCreateWithData(data, uti, 1, 0));
477     if (!destination)
478         return false;
479
480     RetainPtr<CFDictionaryRef> imageProperties;
481     if (CFEqual(uti, jpegUTI()) && quality && *quality >= 0.0 && *quality <= 1.0) {
482         // Apply the compression quality to the JPEG image destination.
483         auto compressionQuality = adoptCF(CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &*quality));
484         const void* key = kCGImageDestinationLossyCompressionQuality;
485         const void* value = compressionQuality.get();
486         imageProperties = adoptCF(CFDictionaryCreate(0, &key, &value, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
487     }
488
489     // Setting kCGImageDestinationBackgroundColor to black for JPEG images in imageProperties would save some math
490     // in the calling functions, but it doesn't seem to work.
491
492     CGImageDestinationAddImage(destination.get(), image, imageProperties.get());
493     return CGImageDestinationFinalize(destination.get());
494 }
495
496 static String dataURL(CFDataRef data, const String& mimeType)
497 {
498     Vector<char> base64Data;
499     base64Encode(CFDataGetBytePtr(data), CFDataGetLength(data), base64Data);
500
501     return "data:" + mimeType + ";base64," + base64Data;
502 }
503
504 static Vector<uint8_t> dataVector(CFDataRef cfData)
505 {
506     Vector<uint8_t> data;
507     data.append(CFDataGetBytePtr(cfData), CFDataGetLength(cfData));
508     return data;
509 }
510
511 String ImageBuffer::toDataURL(const String& mimeType, std::optional<double> quality, CoordinateSystem) const
512 {
513     if (auto data = toCFData(mimeType, quality))
514         return dataURL(data.get(), mimeType);
515     return ASCIILiteral("data:,");
516 }
517
518 Vector<uint8_t> ImageBuffer::toData(const String& mimeType, std::optional<double> quality) const
519 {
520     if (auto data = toCFData(mimeType, quality))
521         return dataVector(data.get());
522     return { };
523 }
524
525 RetainPtr<CFDataRef> ImageBuffer::toCFData(const String& mimeType, std::optional<double> quality) const
526 {
527     ASSERT(MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(mimeType));
528
529     if (context().isAcceleratedContext())
530         flushContext();
531
532     auto uti = utiFromImageBufferMIMEType(mimeType);
533     ASSERT(uti);
534
535     RetainPtr<CGImageRef> image;
536     RefPtr<Uint8ClampedArray> premultipliedData;
537
538     if (CFEqual(uti.get(), jpegUTI())) {
539         // JPEGs don't have an alpha channel, so we have to manually composite on top of black.
540         premultipliedData = getPremultipliedImageData(IntRect(IntPoint(), logicalSize()));
541         if (!premultipliedData)
542             return nullptr;
543
544         size_t dataSize = 4 * logicalSize().width() * logicalSize().height();
545         auto dataProvider = adoptCF(CGDataProviderCreateWithData(nullptr, premultipliedData->data(), dataSize, nullptr));
546         if (!dataProvider)
547             return nullptr;
548
549         image = adoptCF(CGImageCreate(logicalSize().width(), logicalSize().height(), 8, 32, 4 * logicalSize().width(), sRGBColorSpaceRef(), kCGBitmapByteOrderDefault | kCGImageAlphaNoneSkipLast, dataProvider.get(), 0, false, kCGRenderingIntentDefault));
550     } else if (m_resolutionScale == 1) {
551         image = copyNativeImage(CopyBackingStore);
552         image = createCroppedImageIfNecessary(image.get(), internalSize());
553     } else {
554         image = copyNativeImage(DontCopyBackingStore);
555         auto context = adoptCF(CGBitmapContextCreate(0, logicalSize().width(), logicalSize().height(), 8, 4 * logicalSize().width(), sRGBColorSpaceRef(), kCGImageAlphaPremultipliedLast));
556         CGContextSetBlendMode(context.get(), kCGBlendModeCopy);
557         CGContextClipToRect(context.get(), CGRectMake(0, 0, logicalSize().width(), logicalSize().height()));
558         FloatSize imageSizeInUserSpace = sizeForDestinationSize(logicalSize());
559         CGContextDrawImage(context.get(), CGRectMake(0, 0, imageSizeInUserSpace.width(), imageSizeInUserSpace.height()), image.get());
560         image = adoptCF(CGBitmapContextCreateImage(context.get()));
561     }
562
563     auto cfData = adoptCF(CFDataCreateMutable(kCFAllocatorDefault, 0));
564     if (!encodeImage(image.get(), uti.get(), quality, cfData.get()))
565         return nullptr;
566
567     return WTFMove(cfData);
568 }
569
570 static RetainPtr<CFDataRef> cfData(const ImageData& source, const String& mimeType, std::optional<double> quality)
571 {
572     ASSERT(MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(mimeType));
573
574     auto uti = utiFromImageBufferMIMEType(mimeType);
575     ASSERT(uti);
576
577     CGImageAlphaInfo dataAlphaInfo = kCGImageAlphaLast;
578     unsigned char* data = source.data()->data();
579     Vector<uint8_t> premultipliedData;
580
581     if (CFEqual(uti.get(), jpegUTI())) {
582         // JPEGs don't have an alpha channel, so we have to manually composite on top of black.
583         size_t size = 4 * source.width() * source.height();
584         if (!premultipliedData.tryReserveCapacity(size))
585             return nullptr;
586
587         premultipliedData.grow(size);
588         unsigned char *buffer = premultipliedData.data();
589         for (size_t i = 0; i < size; i += 4) {
590             unsigned alpha = data[i + 3];
591             if (alpha != 255) {
592                 buffer[i + 0] = data[i + 0] * alpha / 255;
593                 buffer[i + 1] = data[i + 1] * alpha / 255;
594                 buffer[i + 2] = data[i + 2] * alpha / 255;
595             } else {
596                 buffer[i + 0] = data[i + 0];
597                 buffer[i + 1] = data[i + 1];
598                 buffer[i + 2] = data[i + 2];
599             }
600         }
601
602         dataAlphaInfo = kCGImageAlphaNoneSkipLast; // Ignore the alpha channel.
603         data = premultipliedData.data();
604     }
605
606     auto dataProvider = adoptCF(CGDataProviderCreateWithData(0, data, 4 * source.width() * source.height(), 0));
607     if (!dataProvider)
608         return nullptr;
609
610     auto image = adoptCF(CGImageCreate(source.width(), source.height(), 8, 32, 4 * source.width(), sRGBColorSpaceRef(), kCGBitmapByteOrderDefault | dataAlphaInfo, dataProvider.get(), 0, false, kCGRenderingIntentDefault));
611
612     auto cfData = adoptCF(CFDataCreateMutable(kCFAllocatorDefault, 0));
613     if (!encodeImage(image.get(), uti.get(), quality, cfData.get()))
614         return nullptr;
615
616     return WTFMove(cfData);
617 }
618
619 String dataURL(const ImageData& source, const String& mimeType, std::optional<double> quality)
620 {
621     if (auto data = cfData(source, mimeType, quality))
622         return dataURL(data.get(), mimeType);
623     return ASCIILiteral("data:,");
624 }
625
626 Vector<uint8_t> data(const ImageData& source, const String& mimeType, std::optional<double> quality)
627 {
628     if (auto data = cfData(source, mimeType, quality))
629         return dataVector(data.get());
630     return { };
631 }
632
633 } // namespace WebCore
634
635 #endif