f67c5274fda8d39a30d511d8739f78a41f795756
[WebKit-https.git] / Source / WebCore / platform / graphics / cg / ImageDecoderCG.cpp
1 /*
2  * Copyright (C) 2016 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "ImageDecoderCG.h"
28
29 #if USE(CG)
30
31 #include "ImageOrientation.h"
32 #include "ImageSourceCG.h"
33 #include "IntPoint.h"
34 #include "IntSize.h"
35 #include "Logging.h"
36 #include "MIMETypeRegistry.h"
37 #include "SharedBuffer.h"
38 #include "UTIRegistry.h"
39 #include <pal/spi/cg/ImageIOSPI.h>
40 #include <ImageIO/ImageIO.h>
41 #include <pal/spi/cg/CoreGraphicsSPI.h>
42
43 namespace WebCore {
44
45 const CFStringRef WebCoreCGImagePropertyAPNGUnclampedDelayTime = CFSTR("UnclampedDelayTime");
46 const CFStringRef WebCoreCGImagePropertyAPNGDelayTime = CFSTR("DelayTime");
47 const CFStringRef WebCoreCGImagePropertyAPNGLoopCount = CFSTR("LoopCount");
48
49 #if PLATFORM(WIN)
50 const CFStringRef kCGImageSourceShouldPreferRGB32 = CFSTR("kCGImageSourceShouldPreferRGB32");
51 const CFStringRef kCGImageSourceSkipMetadata = CFSTR("kCGImageSourceSkipMetadata");
52 const CFStringRef kCGImageSourceSubsampleFactor = CFSTR("kCGImageSourceSubsampleFactor");
53 const CFStringRef kCGImageSourceShouldCacheImmediately = CFSTR("kCGImageSourceShouldCacheImmediately");
54 #endif
55
56 static RetainPtr<CFMutableDictionaryRef> createImageSourceOptions()
57 {
58     RetainPtr<CFMutableDictionaryRef> options = adoptCF(CFDictionaryCreateMutable(nullptr, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
59     CFDictionarySetValue(options.get(), kCGImageSourceShouldCache, kCFBooleanTrue);
60     CFDictionarySetValue(options.get(), kCGImageSourceShouldPreferRGB32, kCFBooleanTrue);
61     CFDictionarySetValue(options.get(), kCGImageSourceSkipMetadata, kCFBooleanTrue);
62     return options;
63 }
64     
65 static RetainPtr<CFMutableDictionaryRef> createImageSourceAsyncOptions()
66 {
67     RetainPtr<CFMutableDictionaryRef> options = createImageSourceOptions();
68     CFDictionarySetValue(options.get(), kCGImageSourceShouldCacheImmediately, kCFBooleanTrue);
69     CFDictionarySetValue(options.get(), kCGImageSourceCreateThumbnailFromImageAlways, kCFBooleanTrue);
70     return options;
71 }
72
73 static RetainPtr<CFMutableDictionaryRef> appendImageSourceOption(RetainPtr<CFMutableDictionaryRef>&& options, SubsamplingLevel subsamplingLevel)
74 {
75     subsamplingLevel = std::min(SubsamplingLevel::Last, std::max(SubsamplingLevel::First, subsamplingLevel));
76     int subsampleInt = 1 << static_cast<int>(subsamplingLevel); // [0..3] => [1, 2, 4, 8]
77     auto subsampleNumber = adoptCF(CFNumberCreate(nullptr,  kCFNumberIntType,  &subsampleInt));
78     CFDictionarySetValue(options.get(), kCGImageSourceSubsampleFactor, subsampleNumber.get());
79     return WTFMove(options);
80 }
81
82 static RetainPtr<CFMutableDictionaryRef> appendImageSourceOption(RetainPtr<CFMutableDictionaryRef>&& options, const IntSize& sizeForDrawing)
83 {
84     unsigned maxDimension = DecodingOptions::maxDimension(sizeForDrawing);
85     RetainPtr<CFNumberRef> maxDimensionNumber = adoptCF(CFNumberCreate(nullptr, kCFNumberIntType, &maxDimension));
86     CFDictionarySetValue(options.get(), kCGImageSourceThumbnailMaxPixelSize, maxDimensionNumber.get());
87     return WTFMove(options);
88 }
89     
90 static RetainPtr<CFMutableDictionaryRef> appendImageSourceOptions(RetainPtr<CFMutableDictionaryRef>&& options, SubsamplingLevel subsamplingLevel, const IntSize& sizeForDrawing)
91 {
92     if (subsamplingLevel != SubsamplingLevel::Default)
93         options = appendImageSourceOption(WTFMove(options), subsamplingLevel);
94     
95     options = appendImageSourceOption(WTFMove(options), sizeForDrawing);
96     return WTFMove(options);
97 }
98     
99 static RetainPtr<CFDictionaryRef> imageSourceOptions(SubsamplingLevel subsamplingLevel = SubsamplingLevel::Default)
100 {
101     static const auto options = createImageSourceOptions().leakRef();
102     if (subsamplingLevel == SubsamplingLevel::Default)
103         return options;
104     return appendImageSourceOption(adoptCF(CFDictionaryCreateMutableCopy(nullptr, 0, options)), subsamplingLevel);
105 }
106
107 static RetainPtr<CFDictionaryRef> imageSourceAsyncOptions(SubsamplingLevel subsamplingLevel, const IntSize& sizeForDrawing)
108 {
109     static const auto options = createImageSourceAsyncOptions().leakRef();
110     return appendImageSourceOptions(adoptCF(CFDictionaryCreateMutableCopy(nullptr, 0, options)), subsamplingLevel, sizeForDrawing);
111 }
112     
113 static ImageOrientation orientationFromProperties(CFDictionaryRef imageProperties)
114 {
115     ASSERT(imageProperties);
116     CFNumberRef orientationProperty = (CFNumberRef)CFDictionaryGetValue(imageProperties, kCGImagePropertyOrientation);
117     if (!orientationProperty)
118         return ImageOrientation();
119     
120     int exifValue;
121     CFNumberGetValue(orientationProperty, kCFNumberIntType, &exifValue);
122     return ImageOrientation::fromEXIFValue(exifValue);
123 }
124
125 #if !PLATFORM(COCOA)
126 size_t sharedBufferGetBytesAtPosition(void* info, void* buffer, off_t position, size_t count)
127 {
128     SharedBuffer* sharedBuffer = static_cast<SharedBuffer*>(info);
129     size_t sourceSize = sharedBuffer->size();
130     if (position >= sourceSize)
131         return 0;
132     
133     const char* source = sharedBuffer->data() + position;
134     size_t amount = std::min<size_t>(count, sourceSize - position);
135     memcpy(buffer, source, amount);
136     return amount;
137 }
138
139 void sharedBufferRelease(void* info)
140 {
141     SharedBuffer* sharedBuffer = static_cast<SharedBuffer*>(info);
142     sharedBuffer->deref();
143 }
144 #endif
145
146 ImageDecoderCG::ImageDecoderCG(SharedBuffer& data, AlphaOption, GammaAndColorProfileOption)
147 {
148     RetainPtr<CFStringRef> utiHint;
149     if (data.size() >= 32)
150         utiHint = adoptCF(CGImageSourceGetTypeWithData(data.createCFData().get(), nullptr, nullptr));
151     
152     if (utiHint) {
153         const void* key = kCGImageSourceTypeIdentifierHint;
154         const void* value = utiHint.get();
155         auto options = adoptCF(CFDictionaryCreate(kCFAllocatorDefault, &key, &value, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
156         m_nativeDecoder = adoptCF(CGImageSourceCreateIncremental(options.get()));
157     } else
158         m_nativeDecoder = adoptCF(CGImageSourceCreateIncremental(nullptr));
159 }
160
161 size_t ImageDecoderCG::bytesDecodedToDetermineProperties() const
162 {
163     // Measured by tracing malloc/calloc calls on Mac OS 10.6.6, x86_64.
164     // A non-zero value ensures cached images with no decoded frames still enter
165     // the live decoded resources list when the CGImageSource decodes image
166     // properties, allowing the cache to prune the partially decoded image.
167     // This value is likely to be inaccurate on other platforms, but the overall
168     // behavior is unchanged.
169     return 13088;
170 }
171     
172 String ImageDecoderCG::uti() const
173 {
174     return CGImageSourceGetType(m_nativeDecoder.get());
175 }
176
177 String ImageDecoderCG::filenameExtension() const
178 {
179     return WebCore::preferredExtensionForImageType(uti());
180 }
181
182 EncodedDataStatus ImageDecoderCG::encodedDataStatus() const
183 {
184     String uti = this->uti();
185     if (uti.isEmpty())
186         return EncodedDataStatus::Unknown;
187
188     switch (CGImageSourceGetStatus(m_nativeDecoder.get())) {
189     case kCGImageStatusUnknownType:
190         return EncodedDataStatus::Error;
191
192     case kCGImageStatusUnexpectedEOF:
193     case kCGImageStatusInvalidData:
194     case kCGImageStatusReadingHeader:
195         // Ragnaros yells: TOO SOON! You have awakened me TOO SOON, Executus!
196         if (!m_isAllDataReceived)
197             return EncodedDataStatus::Unknown;
198
199         return EncodedDataStatus::Error;
200
201     case kCGImageStatusIncomplete: {
202         if (!isSupportedImageType(uti))
203             return EncodedDataStatus::Error;
204
205         RetainPtr<CFDictionaryRef> image0Properties = adoptCF(CGImageSourceCopyPropertiesAtIndex(m_nativeDecoder.get(), 0, imageSourceOptions().get()));
206         if (!image0Properties)
207             return EncodedDataStatus::TypeAvailable;
208         
209         if (!CFDictionaryContainsKey(image0Properties.get(), kCGImagePropertyPixelWidth) || !CFDictionaryContainsKey(image0Properties.get(), kCGImagePropertyPixelHeight))
210             return EncodedDataStatus::TypeAvailable;
211         
212         return EncodedDataStatus::SizeAvailable;
213     }
214
215     case kCGImageStatusComplete:
216         if (!isSupportedImageType(uti))
217             return EncodedDataStatus::Error;
218
219         return EncodedDataStatus::Complete;
220     }
221
222     ASSERT_NOT_REACHED();
223     return EncodedDataStatus::Unknown;
224 }
225
226 size_t ImageDecoderCG::frameCount() const
227 {
228     return CGImageSourceGetCount(m_nativeDecoder.get());
229 }
230
231 RepetitionCount ImageDecoderCG::repetitionCount() const
232 {
233     RetainPtr<CFDictionaryRef> properties = adoptCF(CGImageSourceCopyProperties(m_nativeDecoder.get(), imageSourceOptions().get()));
234     if (!properties)
235         return RepetitionCountOnce;
236     
237     CFDictionaryRef gifProperties = (CFDictionaryRef)CFDictionaryGetValue(properties.get(), kCGImagePropertyGIFDictionary);
238     if (gifProperties) {
239         CFNumberRef num = (CFNumberRef)CFDictionaryGetValue(gifProperties, kCGImagePropertyGIFLoopCount);
240         
241         // No property means loop once.
242         if (!num)
243             return RepetitionCountOnce;
244         
245         RepetitionCount loopCount;
246         CFNumberGetValue(num, kCFNumberIntType, &loopCount);
247         
248         // A property with value 0 means loop forever.
249         // For loopCount > 0, the specs is not clear about it. But it looks the meaning
250         // is: play once + loop loopCount which is equivalent to play loopCount + 1.
251         return loopCount ? loopCount + 1 : RepetitionCountInfinite;
252     }
253     
254     CFDictionaryRef pngProperties = (CFDictionaryRef)CFDictionaryGetValue(properties.get(), kCGImagePropertyPNGDictionary);
255     if (pngProperties) {
256         CFNumberRef num = (CFNumberRef)CFDictionaryGetValue(pngProperties, WebCoreCGImagePropertyAPNGLoopCount);
257         if (!num)
258             return RepetitionCountOnce;
259         
260         RepetitionCount loopCount;
261         CFNumberGetValue(num, kCFNumberIntType, &loopCount);
262         return loopCount ? loopCount : RepetitionCountInfinite;
263     }
264     
265     // Turns out we're not an animated image after all, so we don't animate.
266     return RepetitionCountNone;
267 }
268
269 Optional<IntPoint> ImageDecoderCG::hotSpot() const
270 {
271     RetainPtr<CFDictionaryRef> properties = adoptCF(CGImageSourceCopyPropertiesAtIndex(m_nativeDecoder.get(), 0, imageSourceOptions().get()));
272     if (!properties)
273         return WTF::nullopt;
274     
275     int x = -1, y = -1;
276     CFNumberRef num = (CFNumberRef)CFDictionaryGetValue(properties.get(), CFSTR("hotspotX"));
277     if (!num || !CFNumberGetValue(num, kCFNumberIntType, &x))
278         return WTF::nullopt;
279     
280     num = (CFNumberRef)CFDictionaryGetValue(properties.get(), CFSTR("hotspotY"));
281     if (!num || !CFNumberGetValue(num, kCFNumberIntType, &y))
282         return WTF::nullopt;
283     
284     if (x < 0 || y < 0)
285         return WTF::nullopt;
286     
287     return IntPoint(x, y);
288 }
289
290 IntSize ImageDecoderCG::frameSizeAtIndex(size_t index, SubsamplingLevel subsamplingLevel) const
291 {
292     RetainPtr<CFDictionaryRef> properties = adoptCF(CGImageSourceCopyPropertiesAtIndex(m_nativeDecoder.get(), index, imageSourceOptions(subsamplingLevel).get()));
293     
294     if (!properties)
295         return { };
296     
297     int width = 0;
298     int height = 0;
299     CFNumberRef num = (CFNumberRef)CFDictionaryGetValue(properties.get(), kCGImagePropertyPixelWidth);
300     if (num)
301         CFNumberGetValue(num, kCFNumberIntType, &width);
302     
303     num = (CFNumberRef)CFDictionaryGetValue(properties.get(), kCGImagePropertyPixelHeight);
304     if (num)
305         CFNumberGetValue(num, kCFNumberIntType, &height);
306     
307     return IntSize(width, height);
308 }
309
310 bool ImageDecoderCG::frameIsCompleteAtIndex(size_t index) const
311 {
312     ASSERT(frameCount());
313     // CGImageSourceGetStatusAtIndex() changes the return status value from kCGImageStatusIncomplete
314     // to kCGImageStatusComplete only if (index > 1 && index < frameCount() - 1). To get an accurate
315     // result for the last frame (or the single frame of the static image) use CGImageSourceGetStatus()
316     // instead for this frame.
317     if (index == frameCount() - 1)
318         return CGImageSourceGetStatus(m_nativeDecoder.get()) == kCGImageStatusComplete;
319     return CGImageSourceGetStatusAtIndex(m_nativeDecoder.get(), index) == kCGImageStatusComplete;
320 }
321
322 ImageOrientation ImageDecoderCG::frameOrientationAtIndex(size_t index) const
323 {
324     RetainPtr<CFDictionaryRef> properties = adoptCF(CGImageSourceCopyPropertiesAtIndex(m_nativeDecoder.get(), index, imageSourceOptions().get()));
325     if (!properties)
326         return ImageOrientation();
327     
328     return orientationFromProperties(properties.get());
329 }
330
331 Seconds ImageDecoderCG::frameDurationAtIndex(size_t index) const
332 {
333     float value = 0;
334     RetainPtr<CFDictionaryRef> properties = adoptCF(CGImageSourceCopyPropertiesAtIndex(m_nativeDecoder.get(), index, imageSourceOptions().get()));
335     if (properties) {
336         CFDictionaryRef gifProperties = (CFDictionaryRef)CFDictionaryGetValue(properties.get(), kCGImagePropertyGIFDictionary);
337         if (gifProperties) {
338             if (CFNumberRef num = (CFNumberRef)CFDictionaryGetValue(gifProperties, kCGImagePropertyGIFUnclampedDelayTime)) {
339                 // Use the unclamped frame delay if it exists.
340                 CFNumberGetValue(num, kCFNumberFloatType, &value);
341             } else if (CFNumberRef num = (CFNumberRef)CFDictionaryGetValue(gifProperties, kCGImagePropertyGIFDelayTime)) {
342                 // Fall back to the clamped frame delay if the unclamped frame delay does not exist.
343                 CFNumberGetValue(num, kCFNumberFloatType, &value);
344             }
345         }
346         
347         CFDictionaryRef pngProperties = (CFDictionaryRef)CFDictionaryGetValue(properties.get(), kCGImagePropertyPNGDictionary);
348         if (pngProperties) {
349             if (CFNumberRef num = (CFNumberRef)CFDictionaryGetValue(pngProperties, WebCoreCGImagePropertyAPNGUnclampedDelayTime))
350                 CFNumberGetValue(num, kCFNumberFloatType, &value);
351             else if (CFNumberRef num = (CFNumberRef)CFDictionaryGetValue(pngProperties, WebCoreCGImagePropertyAPNGDelayTime))
352                 CFNumberGetValue(num, kCFNumberFloatType, &value);
353         }
354     }
355
356     Seconds duration(value);
357
358     // Many annoying ads specify a 0 duration to make an image flash as quickly as possible.
359     // We follow Firefox's behavior and use a duration of 100 ms for any frames that specify
360     // a duration of <= 10 ms. See <rdar://problem/7689300> and <http://webkit.org/b/36082>
361     // for more information.
362     if (duration < 11_ms)
363         return 100_ms;
364     return duration;
365 }
366
367 bool ImageDecoderCG::frameAllowSubsamplingAtIndex(size_t) const
368 {
369     return true;
370 }
371
372 bool ImageDecoderCG::frameHasAlphaAtIndex(size_t index) const
373 {
374     if (!frameIsCompleteAtIndex(index))
375         return true;
376     
377     String uti = this->uti();
378     
379     // Return false if there is no image type or the image type is JPEG, because
380     // JPEG does not support alpha transparency.
381     if (uti.isEmpty() || uti == "public.jpeg")
382         return false;
383     
384     // FIXME: Could return false for other non-transparent image formats.
385     // FIXME: Could maybe return false for a GIF Frame if we have enough info in the GIF properties dictionary
386     // to determine whether or not a transparent color was defined.
387     return true;
388 }
389
390 unsigned ImageDecoderCG::frameBytesAtIndex(size_t index, SubsamplingLevel subsamplingLevel) const
391 {
392     IntSize frameSize = frameSizeAtIndex(index, subsamplingLevel);
393     return (frameSize.area() * 4).unsafeGet();
394 }
395
396 NativeImagePtr ImageDecoderCG::createFrameImageAtIndex(size_t index, SubsamplingLevel subsamplingLevel, const DecodingOptions& decodingOptions)
397 {
398     LOG(Images, "ImageDecoder %p createFrameImageAtIndex %lu", this, index);
399     RetainPtr<CFDictionaryRef> options;
400     RetainPtr<CGImageRef> image;
401
402     if (!decodingOptions.isSynchronous()) {
403         // Don't consider the subsamplingLevel when comparing the image native size with sizeForDrawing.
404         IntSize size = frameSizeAtIndex(index, SubsamplingLevel::Default);
405         
406         if (decodingOptions.hasSizeForDrawing()) {
407             // See which size is smaller: the image native size or the sizeForDrawing.
408             Optional<IntSize> sizeForDrawing = decodingOptions.sizeForDrawing();
409             if (sizeForDrawing.value().unclampedArea() < size.unclampedArea())
410                 size = sizeForDrawing.value();
411         }
412         
413         options = imageSourceAsyncOptions(subsamplingLevel, size);
414         image = adoptCF(CGImageSourceCreateThumbnailAtIndex(m_nativeDecoder.get(), index, options.get()));
415     } else {
416         // Decode an image synchronously for its native size.
417         options = imageSourceOptions(subsamplingLevel);
418         image = adoptCF(CGImageSourceCreateImageAtIndex(m_nativeDecoder.get(), index, options.get()));
419     }
420     
421 #if PLATFORM(IOS_FAMILY)
422     // <rdar://problem/7371198> - CoreGraphics changed the default caching behaviour in iOS 4.0 to kCGImageCachingTransient
423     // which caused a performance regression for us since the images had to be resampled/recreated every time we called
424     // CGContextDrawImage. We now tell CG to cache the drawn images. See also <rdar://problem/14366755> -
425     // CoreGraphics needs to un-deprecate kCGImageCachingTemporary since it's still not the default.
426     ALLOW_DEPRECATED_DECLARATIONS_BEGIN
427     CGImageSetCachingFlags(image.get(), kCGImageCachingTemporary);
428     ALLOW_DEPRECATED_DECLARATIONS_END
429 #endif // PLATFORM(IOS_FAMILY)
430     
431     String uti = this->uti();
432     if (uti.isEmpty() || uti != "public.xbitmap-image")
433         return image;
434     
435     // If it is an xbm image, mask out all the white areas to render them transparent.
436     const CGFloat maskingColors[6] = {255, 255,  255, 255, 255, 255};
437     RetainPtr<CGImageRef> maskedImage = adoptCF(CGImageCreateWithMaskingColors(image.get(), maskingColors));
438     return maskedImage ? maskedImage : image;
439 }
440
441 void ImageDecoderCG::setData(SharedBuffer& data, bool allDataReceived)
442 {
443     m_isAllDataReceived = allDataReceived;
444
445 #if PLATFORM(COCOA)
446     // On Mac the NSData inside the SharedBuffer can be secretly appended to without the SharedBuffer's knowledge.
447     // We use SharedBuffer's ability to wrap itself inside CFData to get around this, ensuring that ImageIO is
448     // really looking at the SharedBuffer.
449     CGImageSourceUpdateData(m_nativeDecoder.get(), data.createCFData().get(), allDataReceived);
450 #else
451     // Create a CGDataProvider to wrap the SharedBuffer.
452     data.ref();
453     // We use the GetBytesAtPosition callback rather than the GetBytePointer one because SharedBuffer
454     // does not provide a way to lock down the byte pointer and guarantee that it won't move, which
455     // is a requirement for using the GetBytePointer callback.
456     CGDataProviderDirectCallbacks providerCallbacks = { 0, 0, 0, sharedBufferGetBytesAtPosition, sharedBufferRelease };
457     RetainPtr<CGDataProviderRef> dataProvider = adoptCF(CGDataProviderCreateDirect(&data, data.size(), &providerCallbacks));
458     CGImageSourceUpdateDataProvider(m_nativeDecoder.get(), dataProvider.get(), allDataReceived);
459 #endif
460 }
461
462 bool ImageDecoderCG::canDecodeType(const String& mimeType)
463 {
464     return MIMETypeRegistry::isSupportedImageMIMEType(mimeType);
465 }
466
467 }
468
469 #endif // USE(CG)