Add a WebCore logging channel for images
[WebKit.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 "IntPoint.h"
33 #include "IntSize.h"
34 #include "Logging.h"
35 #include "SharedBuffer.h"
36 #include <wtf/NeverDestroyed.h>
37
38 #if !PLATFORM(IOS)
39 #include <ApplicationServices/ApplicationServices.h>
40 #else
41 #include "CoreGraphicsSPI.h"
42 #include <ImageIO/ImageIO.h>
43 #endif
44
45 #if USE(APPLE_INTERNAL_SDK)
46 #import <ImageIO/CGImageSourcePrivate.h>
47 #else
48 const CFStringRef kCGImageSourceSubsampleFactor = CFSTR("kCGImageSourceSubsampleFactor");
49 #endif
50
51 namespace WebCore {
52
53 const CFStringRef WebCoreCGImagePropertyAPNGUnclampedDelayTime = CFSTR("UnclampedDelayTime");
54 const CFStringRef WebCoreCGImagePropertyAPNGDelayTime = CFSTR("DelayTime");
55 const CFStringRef WebCoreCGImagePropertyAPNGLoopCount = CFSTR("LoopCount");
56
57 const CFStringRef kCGImageSourceShouldPreferRGB32 = CFSTR("kCGImageSourceShouldPreferRGB32");
58 const CFStringRef kCGImageSourceSkipMetadata = CFSTR("kCGImageSourceSkipMetadata");
59
60 static RetainPtr<CFDictionaryRef> createImageSourceOptions(SubsamplingLevel subsamplingLevel)
61 {
62     if (!subsamplingLevel) {
63         const unsigned numOptions = 3;
64         const void* keys[numOptions] = { kCGImageSourceShouldCache, kCGImageSourceShouldPreferRGB32, kCGImageSourceSkipMetadata };
65         const void* values[numOptions] = { kCFBooleanTrue, kCFBooleanTrue, kCFBooleanTrue };
66         return CFDictionaryCreate(nullptr, keys, values, numOptions, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
67     }
68     
69     short constrainedSubsamplingLevel = std::min<short>(3, std::max<short>(0, subsamplingLevel));
70     int subsampleInt = 1 << constrainedSubsamplingLevel; // [0..3] => [1, 2, 4, 8]
71     
72     RetainPtr<CFNumberRef> subsampleNumber = adoptCF(CFNumberCreate(nullptr,  kCFNumberIntType,  &subsampleInt));
73     const CFIndex numOptions = 4;
74     const void* keys[numOptions] = { kCGImageSourceShouldCache, kCGImageSourceShouldPreferRGB32, kCGImageSourceSkipMetadata, kCGImageSourceSubsampleFactor };
75     const void* values[numOptions] = { kCFBooleanTrue, kCFBooleanTrue, kCFBooleanTrue, subsampleNumber.get() };
76     return adoptCF(CFDictionaryCreate(nullptr, keys, values, numOptions, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
77 }
78
79 static RetainPtr<CFDictionaryRef> imageSourceOptions(SubsamplingLevel subsamplingLevel = 0)
80 {
81     if (subsamplingLevel)
82         return createImageSourceOptions(subsamplingLevel);
83     
84     static NeverDestroyed<RetainPtr<CFDictionaryRef>> options = createImageSourceOptions(0);
85     return options;
86 }
87
88 static ImageOrientation orientationFromProperties(CFDictionaryRef imageProperties)
89 {
90     ASSERT(imageProperties);
91     CFNumberRef orientationProperty = (CFNumberRef)CFDictionaryGetValue(imageProperties, kCGImagePropertyOrientation);
92     if (!orientationProperty)
93         return ImageOrientation();
94     
95     int exifValue;
96     CFNumberGetValue(orientationProperty, kCFNumberIntType, &exifValue);
97     return ImageOrientation::fromEXIFValue(exifValue);
98 }
99
100 #if !PLATFORM(COCOA)
101 size_t sharedBufferGetBytesAtPosition(void* info, void* buffer, off_t position, size_t count)
102 {
103     SharedBuffer* sharedBuffer = static_cast<SharedBuffer*>(info);
104     size_t sourceSize = sharedBuffer->size();
105     if (position >= sourceSize)
106         return 0;
107     
108     const char* source = sharedBuffer->data() + position;
109     size_t amount = std::min<size_t>(count, sourceSize - position);
110     memcpy(buffer, source, amount);
111     return amount;
112 }
113
114 void sharedBufferRelease(void* info)
115 {
116     SharedBuffer* sharedBuffer = static_cast<SharedBuffer*>(info);
117     sharedBuffer->deref();
118 }
119 #endif
120
121 ImageDecoder::ImageDecoder()
122 {
123     m_nativeDecoder = adoptCF(CGImageSourceCreateIncremental(nullptr));
124 }
125
126 size_t ImageDecoder::bytesDecodedToDetermineProperties()
127 {
128     // Measured by tracing malloc/calloc calls on Mac OS 10.6.6, x86_64.
129     // A non-zero value ensures cached images with no decoded frames still enter
130     // the live decoded resources list when the CGImageSource decodes image
131     // properties, allowing the cache to prune the partially decoded image.
132     // This value is likely to be inaccurate on other platforms, but the overall
133     // behavior is unchanged.
134     return 13088;
135 }
136
137 String ImageDecoder::filenameExtension() const
138 {
139     CFStringRef imageSourceType = CGImageSourceGetType(m_nativeDecoder.get());
140     return WebCore::preferredExtensionForImageSourceType(imageSourceType);
141 }
142
143 bool ImageDecoder::isSizeAvailable() const
144 {
145     // Ragnaros yells: TOO SOON! You have awakened me TOO SOON, Executus!
146     if (CGImageSourceGetStatus(m_nativeDecoder.get()) < kCGImageStatusIncomplete)
147         return false;
148     
149     RetainPtr<CFDictionaryRef> image0Properties = adoptCF(CGImageSourceCopyPropertiesAtIndex(m_nativeDecoder.get(), 0, imageSourceOptions().get()));
150     if (!image0Properties)
151         return false;
152     
153     return CFDictionaryContainsKey(image0Properties.get(), kCGImagePropertyPixelWidth)
154     && CFDictionaryContainsKey(image0Properties.get(), kCGImagePropertyPixelHeight);
155 }
156
157 IntSize ImageDecoder::size() const
158 {
159     if (m_size.isEmpty())
160         m_size = frameSizeAtIndex(0, 0);
161     return m_size;
162 }
163
164 size_t ImageDecoder::frameCount() const
165 {
166     return CGImageSourceGetCount(m_nativeDecoder.get());
167 }
168
169 int ImageDecoder::repetitionCount() const
170 {
171     RetainPtr<CFDictionaryRef> properties = adoptCF(CGImageSourceCopyProperties(m_nativeDecoder.get(), imageSourceOptions().get()));
172     if (!properties)
173         return cAnimationLoopOnce;
174     
175     CFDictionaryRef gifProperties = (CFDictionaryRef)CFDictionaryGetValue(properties.get(), kCGImagePropertyGIFDictionary);
176     if (gifProperties) {
177         CFNumberRef num = (CFNumberRef)CFDictionaryGetValue(gifProperties, kCGImagePropertyGIFLoopCount);
178         
179         // No property means loop once.
180         if (!num)
181             return cAnimationLoopOnce;
182         
183         int loopCount;
184         CFNumberGetValue(num, kCFNumberIntType, &loopCount);
185         
186         // A property with value 0 means loop forever.
187         return loopCount ? loopCount : cAnimationLoopInfinite;
188     }
189     
190     CFDictionaryRef pngProperties = (CFDictionaryRef)CFDictionaryGetValue(properties.get(), kCGImagePropertyPNGDictionary);
191     if (pngProperties) {
192         CFNumberRef num = (CFNumberRef)CFDictionaryGetValue(pngProperties, WebCoreCGImagePropertyAPNGLoopCount);
193         if (!num)
194             return cAnimationLoopOnce;
195         
196         int loopCount;
197         CFNumberGetValue(num, kCFNumberIntType, &loopCount);
198         return loopCount ? loopCount : cAnimationLoopInfinite;
199     }
200     
201     // Turns out we're not an animated image after all, so we don't animate.
202     return cAnimationNone;
203 }
204
205 Optional<IntPoint> ImageDecoder::hotSpot() const
206 {
207     RetainPtr<CFDictionaryRef> properties = adoptCF(CGImageSourceCopyPropertiesAtIndex(m_nativeDecoder.get(), 0, imageSourceOptions().get()));
208     if (!properties)
209         return Nullopt;
210     
211     int x = -1, y = -1;
212     CFNumberRef num = (CFNumberRef)CFDictionaryGetValue(properties.get(), CFSTR("hotspotX"));
213     if (!num || !CFNumberGetValue(num, kCFNumberIntType, &x))
214         return Nullopt;
215     
216     num = (CFNumberRef)CFDictionaryGetValue(properties.get(), CFSTR("hotspotY"));
217     if (!num || !CFNumberGetValue(num, kCFNumberIntType, &y))
218         return Nullopt;
219     
220     if (x < 0 || y < 0)
221         return Nullopt;
222     
223     return IntPoint(x, y);
224 }
225
226 IntSize ImageDecoder::frameSizeAtIndex(size_t index, SubsamplingLevel subsamplingLevel) const
227 {
228     RetainPtr<CFDictionaryRef> properties = adoptCF(CGImageSourceCopyPropertiesAtIndex(m_nativeDecoder.get(), index, imageSourceOptions(subsamplingLevel).get()));
229     
230     if (!properties)
231         return { };
232     
233     int width = 0;
234     int height = 0;
235     CFNumberRef num = (CFNumberRef)CFDictionaryGetValue(properties.get(), kCGImagePropertyPixelWidth);
236     if (num)
237         CFNumberGetValue(num, kCFNumberIntType, &width);
238     
239     num = (CFNumberRef)CFDictionaryGetValue(properties.get(), kCGImagePropertyPixelHeight);
240     if (num)
241         CFNumberGetValue(num, kCFNumberIntType, &height);
242     
243     return IntSize(width, height);
244 }
245
246 bool ImageDecoder::frameIsCompleteAtIndex(size_t index) const
247 {
248     ASSERT(frameCount());
249     return CGImageSourceGetStatusAtIndex(m_nativeDecoder.get(), index) == kCGImageStatusComplete;
250 }
251
252 ImageOrientation ImageDecoder::orientationAtIndex(size_t index) const
253 {
254     RetainPtr<CFDictionaryRef> properties = adoptCF(CGImageSourceCopyPropertiesAtIndex(m_nativeDecoder.get(), index, imageSourceOptions().get()));
255     if (!properties)
256         return ImageOrientation();
257     
258     return orientationFromProperties(properties.get());
259 }
260
261 float ImageDecoder::frameDurationAtIndex(size_t index) const
262 {
263     float duration = 0;
264     RetainPtr<CFDictionaryRef> properties = adoptCF(CGImageSourceCopyPropertiesAtIndex(m_nativeDecoder.get(), index, imageSourceOptions().get()));
265     if (properties) {
266         CFDictionaryRef gifProperties = (CFDictionaryRef)CFDictionaryGetValue(properties.get(), kCGImagePropertyGIFDictionary);
267         if (gifProperties) {
268             if (CFNumberRef num = (CFNumberRef)CFDictionaryGetValue(gifProperties, kCGImagePropertyGIFUnclampedDelayTime)) {
269                 // Use the unclamped frame delay if it exists.
270                 CFNumberGetValue(num, kCFNumberFloatType, &duration);
271             } else if (CFNumberRef num = (CFNumberRef)CFDictionaryGetValue(gifProperties, kCGImagePropertyGIFDelayTime)) {
272                 // Fall back to the clamped frame delay if the unclamped frame delay does not exist.
273                 CFNumberGetValue(num, kCFNumberFloatType, &duration);
274             }
275         }
276         
277         CFDictionaryRef pngProperties = (CFDictionaryRef)CFDictionaryGetValue(properties.get(), kCGImagePropertyPNGDictionary);
278         if (pngProperties) {
279             if (CFNumberRef num = (CFNumberRef)CFDictionaryGetValue(pngProperties, WebCoreCGImagePropertyAPNGUnclampedDelayTime))
280                 CFNumberGetValue(num, kCFNumberFloatType, &duration);
281             else if (CFNumberRef num = (CFNumberRef)CFDictionaryGetValue(pngProperties, WebCoreCGImagePropertyAPNGDelayTime))
282                 CFNumberGetValue(num, kCFNumberFloatType, &duration);
283         }
284     }
285     
286     // Many annoying ads specify a 0 duration to make an image flash as quickly as possible.
287     // We follow Firefox's behavior and use a duration of 100 ms for any frames that specify
288     // a duration of <= 10 ms. See <rdar://problem/7689300> and <http://webkit.org/b/36082>
289     // for more information.
290     if (duration < 0.011f)
291         return 0.1f;
292     return duration;
293 }
294
295 bool ImageDecoder::allowSubsamplingOfFrameAtIndex(size_t) const
296 {
297     RetainPtr<CFDictionaryRef> properties = adoptCF(CGImageSourceCopyPropertiesAtIndex(m_nativeDecoder.get(), 0, imageSourceOptions().get()));
298     if (!properties)
299         return false;
300     
301     CFDictionaryRef jfifProperties = static_cast<CFDictionaryRef>(CFDictionaryGetValue(properties.get(), kCGImagePropertyJFIFDictionary));
302     if (jfifProperties) {
303         CFBooleanRef isProgCFBool = static_cast<CFBooleanRef>(CFDictionaryGetValue(jfifProperties, kCGImagePropertyJFIFIsProgressive));
304         if (isProgCFBool) {
305             bool isProgressive = CFBooleanGetValue(isProgCFBool);
306             // Workaround for <rdar://problem/5184655> - Hang rendering very large progressive JPEG. Decoding progressive
307             // images hangs for a very long time right now. Until this is fixed, don't sub-sample progressive images. This
308             // will cause them to fail our large image check and they won't be decoded.
309             // FIXME: Remove once underlying issue is fixed (<rdar://problem/5191418>)
310             return !isProgressive;
311         }
312     }
313     
314     return true;
315 }
316
317 bool ImageDecoder::frameHasAlphaAtIndex(size_t index) const
318 {
319     if (!frameIsCompleteAtIndex(index))
320         return true;
321     
322     CFStringRef imageType = CGImageSourceGetType(m_nativeDecoder.get());
323     
324     // Return false if there is no image type or the image type is JPEG, because
325     // JPEG does not support alpha transparency.
326     if (!imageType || CFEqual(imageType, CFSTR("public.jpeg")))
327         return false;
328     
329     // FIXME: Could return false for other non-transparent image formats.
330     // FIXME: Could maybe return false for a GIF Frame if we have enough info in the GIF properties dictionary
331     // to determine whether or not a transparent color was defined.
332     return true;
333 }
334
335 unsigned ImageDecoder::frameBytesAtIndex(size_t index, SubsamplingLevel subsamplingLevel) const
336 {
337     IntSize frameSize = frameSizeAtIndex(index, subsamplingLevel);
338     return frameSize.area() * 4;
339 }
340
341 NativeImagePtr ImageDecoder::createFrameImageAtIndex(size_t index, SubsamplingLevel subsamplingLevel) const
342 {
343     LOG(Images, "ImageDecoder %p createFrameImageAtIndex %lu", this, index);
344
345     RetainPtr<CGImageRef> image = adoptCF(CGImageSourceCreateImageAtIndex(m_nativeDecoder.get(), index, imageSourceOptions(subsamplingLevel).get()));
346     
347 #if PLATFORM(IOS)
348     // <rdar://problem/7371198> - CoreGraphics changed the default caching behaviour in iOS 4.0 to kCGImageCachingTransient
349     // which caused a performance regression for us since the images had to be resampled/recreated every time we called
350     // CGContextDrawImage. We now tell CG to cache the drawn images. See also <rdar://problem/14366755> -
351     // CoreGraphics needs to un-deprecate kCGImageCachingTemporary since it's still not the default.
352 #if COMPILER(CLANG)
353 #pragma clang diagnostic push
354 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
355 #endif
356     CGImageSetCachingFlags(image.get(), kCGImageCachingTemporary);
357 #if COMPILER(CLANG)
358 #pragma clang diagnostic pop
359 #endif
360 #endif // PLATFORM(IOS)
361     
362     CFStringRef imageUTI = CGImageSourceGetType(m_nativeDecoder.get());
363     static const CFStringRef xbmUTI = CFSTR("public.xbitmap-image");
364     
365     if (!imageUTI)
366         return image;
367     
368     if (!CFEqual(imageUTI, xbmUTI))
369         return image;
370     
371     // If it is an xbm image, mask out all the white areas to render them transparent.
372     const CGFloat maskingColors[6] = {255, 255,  255, 255, 255, 255};
373     RetainPtr<CGImageRef> maskedImage = adoptCF(CGImageCreateWithMaskingColors(image.get(), maskingColors));
374     return maskedImage ? maskedImage : image;
375 }
376
377 void ImageDecoder::setData(CFDataRef data, bool allDataReceived)
378 {
379     CGImageSourceUpdateData(m_nativeDecoder.get(), data, allDataReceived);
380 }
381
382 void ImageDecoder::setData(SharedBuffer& data, bool allDataReceived)
383 {
384 #if PLATFORM(COCOA)
385     // On Mac the NSData inside the SharedBuffer can be secretly appended to without the SharedBuffer's knowledge.
386     // We use SharedBuffer's ability to wrap itself inside CFData to get around this, ensuring that ImageIO is
387     // really looking at the SharedBuffer.
388     setData(data.createCFData().get(), allDataReceived);
389 #else
390     // Create a CGDataProvider to wrap the SharedBuffer.
391     data.ref();
392     // We use the GetBytesAtPosition callback rather than the GetBytePointer one because SharedBuffer
393     // does not provide a way to lock down the byte pointer and guarantee that it won't move, which
394     // is a requirement for using the GetBytePointer callback.
395     CGDataProviderDirectCallbacks providerCallbacks = { 0, 0, 0, sharedBufferGetBytesAtPosition, sharedBufferRelease };
396     RetainPtr<CGDataProviderRef> dataProvider = adoptCF(CGDataProviderCreateDirect(&data, data.size(), &providerCallbacks));
397     CGImageSourceUpdateDataProvider(m_nativeDecoder.get(), dataProvider.get(), allDataReceived);
398 #endif
399 }
400
401 }
402
403 #endif // USE(CG)