Clean up image subsampling code, make it less iOS-specific
[WebKit-https.git] / Source / WebCore / platform / graphics / cg / ImageSourceCG.cpp
1 /*
2  * Copyright (C) 2004, 2005, 2006, 2008 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 "ImageSource.h"
28
29 #if USE(CG)
30 #include "ImageSourceCG.h"
31
32 #include "ImageOrientation.h"
33 #include "IntPoint.h"
34 #include "IntSize.h"
35 #include "MIMETypeRegistry.h"
36 #include "SharedBuffer.h"
37 #if !PLATFORM(IOS)
38 #include <ApplicationServices/ApplicationServices.h>
39 #else
40 #include <CoreGraphics/CGImagePrivate.h>
41 #include <ImageIO/ImageIO.h>
42 #include <wtf/NeverDestroyed.h>
43 #include <wtf/RetainPtr.h>
44 #endif
45
46 #if __has_include(<ImageIO/CGImageSourcePrivate.h>)
47 #import <ImageIO/CGImageSourcePrivate.h>
48 #else
49 const CFStringRef kCGImageSourceSubsampleFactor = CFSTR("kCGImageSourceSubsampleFactor");
50 #endif
51
52 namespace WebCore {
53
54 const CFStringRef WebCoreCGImagePropertyAPNGUnclampedDelayTime = CFSTR("UnclampedDelayTime");
55 const CFStringRef WebCoreCGImagePropertyAPNGDelayTime = CFSTR("DelayTime");
56 const CFStringRef WebCoreCGImagePropertyAPNGLoopCount = CFSTR("LoopCount");
57
58 const CFStringRef kCGImageSourceShouldPreferRGB32 = CFSTR("kCGImageSourceShouldPreferRGB32");
59 const CFStringRef kCGImageSourceSkipMetadata = CFSTR("kCGImageSourceSkipMetadata");
60
61 #if !PLATFORM(COCOA)
62 size_t sharedBufferGetBytesAtPosition(void* info, void* buffer, off_t position, size_t count)
63 {
64     SharedBuffer* sharedBuffer = static_cast<SharedBuffer*>(info);
65     size_t sourceSize = sharedBuffer->size();
66     if (position >= sourceSize)
67         return 0;
68
69     const char* source = sharedBuffer->data() + position;
70     size_t amount = std::min<size_t>(count, sourceSize - position);
71     memcpy(buffer, source, amount);
72     return amount;
73 }
74
75 void sharedBufferRelease(void* info)
76 {
77     SharedBuffer* sharedBuffer = static_cast<SharedBuffer*>(info);
78     sharedBuffer->deref();
79 }
80 #endif
81
82 ImageSource::ImageSource(ImageSource::AlphaOption, ImageSource::GammaAndColorProfileOption)
83     : m_decoder(0)
84 {
85     // FIXME: AlphaOption and GammaAndColorProfileOption are ignored.
86 }
87
88 ImageSource::~ImageSource()
89 {
90     clear(true);
91 }
92
93 void ImageSource::clear(bool destroyAllFrames, size_t, SharedBuffer* data, bool allDataReceived)
94 {
95     // Recent versions of ImageIO discard previously decoded image frames if the client
96     // application no longer holds references to them, so there's no need to throw away
97     // the decoder unless we're explicitly asked to destroy all of the frames.
98     if (!destroyAllFrames)
99         return;
100
101     if (m_decoder) {
102         CFRelease(m_decoder);
103         m_decoder = 0;
104     }
105     if (data)
106         setData(data, allDataReceived);
107 }
108
109 static CFDictionaryRef createImageSourceOptions(ImageSource::ShouldSkipMetadata skipMetaData, SubsamplingLevel subsamplingLevel)
110 {
111     const CFBooleanRef imageSourceSkipMetadata = (skipMetaData == ImageSource::SkipMetadata) ? kCFBooleanTrue : kCFBooleanFalse;
112     
113     if (!subsamplingLevel) {
114         const unsigned numOptions = 3;
115         const void* keys[numOptions] = { kCGImageSourceShouldCache, kCGImageSourceShouldPreferRGB32, kCGImageSourceSkipMetadata };
116         const void* values[numOptions] = { kCFBooleanTrue, kCFBooleanTrue, imageSourceSkipMetadata };
117         return CFDictionaryCreate(nullptr, keys, values, numOptions, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
118     }
119
120     short constrainedSubsamplingLevel = std::min<short>(3, std::max<short>(0, subsamplingLevel));
121     int subsampleInt = 1 << constrainedSubsamplingLevel; // [0..3] => [1, 2, 4, 8]
122
123     RetainPtr<CFNumberRef> subsampleNumber = adoptCF(CFNumberCreate(nullptr,  kCFNumberIntType,  &subsampleInt));
124     const CFIndex numOptions = 4;
125     const void* keys[numOptions] = { kCGImageSourceShouldCache, kCGImageSourceShouldPreferRGB32, kCGImageSourceSkipMetadata, kCGImageSourceSubsampleFactor };
126     const void* values[numOptions] = { kCFBooleanTrue, kCFBooleanTrue, imageSourceSkipMetadata, subsampleNumber.get() };
127     return CFDictionaryCreate(nullptr, keys, values, numOptions, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
128 }
129
130 static CFDictionaryRef imageSourceOptions(ImageSource::ShouldSkipMetadata skipMetadata = ImageSource::SkipMetadata, SubsamplingLevel subsamplingLevel = 0)
131 {
132     if (subsamplingLevel)
133         return createImageSourceOptions(skipMetadata, subsamplingLevel);
134
135     static CFDictionaryRef options = createImageSourceOptions(skipMetadata, 0);
136     return options;
137 }
138
139 bool ImageSource::initialized() const
140 {
141     return m_decoder;
142 }
143
144 void ImageSource::setData(SharedBuffer* data, bool allDataReceived)
145 {
146 #if PLATFORM(COCOA)
147     if (!m_decoder)
148         m_decoder = CGImageSourceCreateIncremental(0);
149     // On Mac the NSData inside the SharedBuffer can be secretly appended to without the SharedBuffer's knowledge.  We use SharedBuffer's ability
150     // to wrap itself inside CFData to get around this, ensuring that ImageIO is really looking at the SharedBuffer.
151     CGImageSourceUpdateData(m_decoder, data->createCFData().get(), allDataReceived);
152 #else
153     if (!m_decoder) {
154         m_decoder = CGImageSourceCreateIncremental(0);
155     } else if (allDataReceived) {
156 #if !PLATFORM(WIN)
157         // 10.6 bug workaround: image sources with final=false fail to draw into PDF contexts, so re-create image source
158         // when data is complete. <rdar://problem/7874035> (<http://openradar.appspot.com/7874035>)
159         CFRelease(m_decoder);
160         m_decoder = CGImageSourceCreateIncremental(0);
161 #endif
162     }
163     // Create a CGDataProvider to wrap the SharedBuffer.
164     data->ref();
165     // We use the GetBytesAtPosition callback rather than the GetBytePointer one because SharedBuffer
166     // does not provide a way to lock down the byte pointer and guarantee that it won't move, which
167     // is a requirement for using the GetBytePointer callback.
168     CGDataProviderDirectCallbacks providerCallbacks = { 0, 0, 0, sharedBufferGetBytesAtPosition, sharedBufferRelease };
169     RetainPtr<CGDataProviderRef> dataProvider = adoptCF(CGDataProviderCreateDirect(data, data->size(), &providerCallbacks));
170     CGImageSourceUpdateDataProvider(m_decoder, dataProvider.get(), allDataReceived);
171 #endif
172 }
173
174 String ImageSource::filenameExtension() const
175 {
176     if (!m_decoder)
177         return String();
178     CFStringRef imageSourceType = CGImageSourceGetType(m_decoder);
179     return WebCore::preferredExtensionForImageSourceType(imageSourceType);
180 }
181
182 SubsamplingLevel ImageSource::subsamplingLevelForScale(float scale) const
183 {
184     // There are four subsampling levels: 0 = 1x, 1 = 0.5x, 2 = 0.25x, 3 = 0.125x.
185     float clampedScale = std::max<float>(0.125, std::min<float>(1, scale));
186     int result = ceilf(log2f(1 / clampedScale));
187     ASSERT(result >=0 && result <= 3);
188     return result;
189 }
190
191 bool ImageSource::isSizeAvailable()
192 {
193     bool result = false;
194     CGImageSourceStatus imageSourceStatus = CGImageSourceGetStatus(m_decoder);
195
196     // Ragnaros yells: TOO SOON! You have awakened me TOO SOON, Executus!
197     if (imageSourceStatus >= kCGImageStatusIncomplete) {
198         RetainPtr<CFDictionaryRef> image0Properties = adoptCF(CGImageSourceCopyPropertiesAtIndex(m_decoder, 0, imageSourceOptions()));
199         if (image0Properties) {
200             CFNumberRef widthNumber = (CFNumberRef)CFDictionaryGetValue(image0Properties.get(), kCGImagePropertyPixelWidth);
201             CFNumberRef heightNumber = (CFNumberRef)CFDictionaryGetValue(image0Properties.get(), kCGImagePropertyPixelHeight);
202             result = widthNumber && heightNumber;
203         }
204     }
205     
206     return result;
207 }
208
209 static ImageOrientation orientationFromProperties(CFDictionaryRef imageProperties)
210 {
211     ASSERT(imageProperties);
212     CFNumberRef orientationProperty = (CFNumberRef)CFDictionaryGetValue(imageProperties, kCGImagePropertyOrientation);
213     if (!orientationProperty)
214         return DefaultImageOrientation;
215
216     int exifValue;
217     CFNumberGetValue(orientationProperty, kCFNumberIntType, &exifValue);
218     return ImageOrientation::fromEXIFValue(exifValue);
219 }
220
221 bool ImageSource::allowSubsamplingOfFrameAtIndex(size_t) const
222 {
223     RetainPtr<CFDictionaryRef> properties = adoptCF(CGImageSourceCopyPropertiesAtIndex(m_decoder, 0, imageSourceOptions()));
224     if (!properties)
225         return false;
226
227     CFDictionaryRef jfifProperties = static_cast<CFDictionaryRef>(CFDictionaryGetValue(properties.get(), kCGImagePropertyJFIFDictionary));
228     if (jfifProperties) {
229         CFBooleanRef isProgCFBool = static_cast<CFBooleanRef>(CFDictionaryGetValue(jfifProperties, kCGImagePropertyJFIFIsProgressive));
230         if (isProgCFBool) {
231             bool isProgressive = CFBooleanGetValue(isProgCFBool);
232             // Workaround for <rdar://problem/5184655> - Hang rendering very large progressive JPEG. Decoding progressive
233             // images hangs for a very long time right now. Until this is fixed, don't sub-sample progressive images. This
234             // will cause them to fail our large image check and they won't be decoded.
235             // FIXME: Remove once underlying issue is fixed (<rdar://problem/5191418>)
236             return !isProgressive;
237         }
238     }
239
240     return true;
241 }
242
243 IntSize ImageSource::frameSizeAtIndex(size_t index, SubsamplingLevel subsamplingLevel, ImageOrientationDescription description) const
244 {
245     RetainPtr<CFDictionaryRef> properties = adoptCF(CGImageSourceCopyPropertiesAtIndex(m_decoder, index, imageSourceOptions(SkipMetadata, subsamplingLevel)));
246
247     if (!properties)
248         return IntSize();
249
250     int width = 0;
251     int height = 0;
252     CFNumberRef num = (CFNumberRef)CFDictionaryGetValue(properties.get(), kCGImagePropertyPixelWidth);
253     if (num)
254         CFNumberGetValue(num, kCFNumberIntType, &width);
255     num = (CFNumberRef)CFDictionaryGetValue(properties.get(), kCGImagePropertyPixelHeight);
256     if (num)
257         CFNumberGetValue(num, kCFNumberIntType, &height);
258
259     if ((description.respectImageOrientation() == RespectImageOrientation) && orientationFromProperties(properties.get()).usesWidthAsHeight())
260         return IntSize(height, width);
261
262     return IntSize(width, height);
263 }
264
265 ImageOrientation ImageSource::orientationAtIndex(size_t index) const
266 {
267     RetainPtr<CFDictionaryRef> properties = adoptCF(CGImageSourceCopyPropertiesAtIndex(m_decoder, index, imageSourceOptions()));
268     if (!properties)
269         return DefaultImageOrientation;
270
271     return orientationFromProperties(properties.get());
272 }
273
274 IntSize ImageSource::size(ImageOrientationDescription description) const
275 {
276     return frameSizeAtIndex(0, 0, description);
277 }
278
279 bool ImageSource::getHotSpot(IntPoint& hotSpot) const
280 {
281     RetainPtr<CFDictionaryRef> properties = adoptCF(CGImageSourceCopyPropertiesAtIndex(m_decoder, 0, imageSourceOptions()));
282     if (!properties)
283         return false;
284
285     int x = -1, y = -1;
286     CFNumberRef num = (CFNumberRef)CFDictionaryGetValue(properties.get(), CFSTR("hotspotX"));
287     if (!num || !CFNumberGetValue(num, kCFNumberIntType, &x))
288         return false;
289
290     num = (CFNumberRef)CFDictionaryGetValue(properties.get(), CFSTR("hotspotY"));
291     if (!num || !CFNumberGetValue(num, kCFNumberIntType, &y))
292         return false;
293
294     if (x < 0 || y < 0)
295         return false;
296
297     hotSpot = IntPoint(x, y);
298     return true;
299 }
300
301 size_t ImageSource::bytesDecodedToDetermineProperties() const
302 {
303     // Measured by tracing malloc/calloc calls on Mac OS 10.6.6, x86_64.
304     // A non-zero value ensures cached images with no decoded frames still enter
305     // the live decoded resources list when the CGImageSource decodes image
306     // properties, allowing the cache to prune the partially decoded image.
307     // This value is likely to be inaccurate on other platforms, but the overall
308     // behavior is unchanged.
309     return 13088;
310 }
311     
312 int ImageSource::repetitionCount()
313 {
314     if (!initialized())
315         return cAnimationLoopOnce;
316
317     RetainPtr<CFDictionaryRef> properties = adoptCF(CGImageSourceCopyProperties(m_decoder, imageSourceOptions()));
318     if (!properties)
319         return cAnimationLoopOnce;
320
321     CFDictionaryRef gifProperties = (CFDictionaryRef)CFDictionaryGetValue(properties.get(), kCGImagePropertyGIFDictionary);
322     if (gifProperties) {
323         CFNumberRef num = (CFNumberRef)CFDictionaryGetValue(gifProperties, kCGImagePropertyGIFLoopCount);
324
325         // No property means loop once.
326         if (!num)
327             return cAnimationLoopOnce;
328
329         int loopCount;
330         CFNumberGetValue(num, kCFNumberIntType, &loopCount);
331
332         // A property with value 0 means loop forever.
333         return loopCount ? loopCount : cAnimationLoopInfinite;
334     }
335
336     CFDictionaryRef pngProperties = (CFDictionaryRef)CFDictionaryGetValue(properties.get(), kCGImagePropertyPNGDictionary);
337     if (pngProperties) {
338         CFNumberRef num = (CFNumberRef)CFDictionaryGetValue(pngProperties, WebCoreCGImagePropertyAPNGLoopCount);
339         if (!num)
340             return cAnimationLoopOnce;
341
342         int loopCount;
343         CFNumberGetValue(num, kCFNumberIntType, &loopCount);
344         return loopCount ? loopCount : cAnimationLoopInfinite;
345     }
346
347     // Turns out we're not an animated image after all, so we don't animate.
348     return cAnimationNone;
349 }
350
351 size_t ImageSource::frameCount() const
352 {
353     return m_decoder ? CGImageSourceGetCount(m_decoder) : 0;
354 }
355
356 CGImageRef ImageSource::createFrameAtIndex(size_t index, SubsamplingLevel subsamplingLevel)
357 {
358     if (!initialized())
359         return 0;
360
361     RetainPtr<CGImageRef> image = adoptCF(CGImageSourceCreateImageAtIndex(m_decoder, index, imageSourceOptions(SkipMetadata, subsamplingLevel)));
362
363 #if PLATFORM(IOS)
364     // <rdar://problem/7371198> - CoreGraphics changed the default caching behaviour in iOS 4.0 to kCGImageCachingTransient
365     // which caused a performance regression for us since the images had to be resampled/recreated every time we called
366     // CGContextDrawImage. We now tell CG to cache the drawn images. See also <rdar://problem/14366755> -
367     // CoreGraphics needs to un-deprecate kCGImageCachingTemporary since it's still not the default.
368 #if COMPILER(CLANG)
369 #pragma clang diagnostic push
370 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
371 #endif
372     CGImageSetCachingFlags(image.get(), kCGImageCachingTemporary);
373 #if COMPILER(CLANG)
374 #pragma clang diagnostic pop
375 #endif
376 #endif // !PLATFORM(IOS)
377
378     CFStringRef imageUTI = CGImageSourceGetType(m_decoder);
379     static const CFStringRef xbmUTI = CFSTR("public.xbitmap-image");
380     if (!imageUTI || !CFEqual(imageUTI, xbmUTI))
381         return image.leakRef();
382     
383     // If it is an xbm image, mask out all the white areas to render them transparent.
384     const CGFloat maskingColors[6] = {255, 255,  255, 255, 255, 255};
385     RetainPtr<CGImageRef> maskedImage = adoptCF(CGImageCreateWithMaskingColors(image.get(), maskingColors));
386     if (!maskedImage)
387         return image.leakRef();
388
389     return maskedImage.leakRef();
390 }
391
392 bool ImageSource::frameIsCompleteAtIndex(size_t index)
393 {
394     ASSERT(frameCount());
395
396     // CGImageSourceGetStatusAtIndex claims that all frames of a multi-frame image are incomplete
397     // when we've not yet received the complete data for an image that is using an incremental data
398     // source (<rdar://problem/7679174>). We work around this by special-casing all frames except the
399     // last in an image and treating them as complete if they are present and reported as being
400     // incomplete. We do this on the assumption that loading new data can only modify the existing last
401     // frame or append new frames. The last frame is only treated as being complete if the image source
402     // reports it as such. This ensures that it is truly the last frame of the image rather than just
403     // the last that we currently have data for.
404
405     CGImageSourceStatus frameStatus = CGImageSourceGetStatusAtIndex(m_decoder, index);
406     if (index < frameCount() - 1)
407         return frameStatus >= kCGImageStatusIncomplete;
408
409     return frameStatus == kCGImageStatusComplete;
410 }
411
412 float ImageSource::frameDurationAtIndex(size_t index)
413 {
414     if (!initialized())
415         return 0;
416
417     float duration = 0;
418     RetainPtr<CFDictionaryRef> properties = adoptCF(CGImageSourceCopyPropertiesAtIndex(m_decoder, index, imageSourceOptions(SkipMetadata)));
419     if (properties) {
420         CFDictionaryRef gifProperties = (CFDictionaryRef)CFDictionaryGetValue(properties.get(), kCGImagePropertyGIFDictionary);
421         if (gifProperties) {
422             if (CFNumberRef num = (CFNumberRef)CFDictionaryGetValue(gifProperties, kCGImagePropertyGIFUnclampedDelayTime)) {
423                 // Use the unclamped frame delay if it exists.
424                 CFNumberGetValue(num, kCFNumberFloatType, &duration);
425             } else if (CFNumberRef num = (CFNumberRef)CFDictionaryGetValue(gifProperties, kCGImagePropertyGIFDelayTime)) {
426                 // Fall back to the clamped frame delay if the unclamped frame delay does not exist.
427                 CFNumberGetValue(num, kCFNumberFloatType, &duration);
428             }
429         }
430
431         CFDictionaryRef pngProperties = (CFDictionaryRef)CFDictionaryGetValue(properties.get(), kCGImagePropertyPNGDictionary);
432         if (pngProperties) {
433             if (CFNumberRef num = (CFNumberRef)CFDictionaryGetValue(pngProperties, WebCoreCGImagePropertyAPNGUnclampedDelayTime))
434                 CFNumberGetValue(num, kCFNumberFloatType, &duration);
435             else if (CFNumberRef num = (CFNumberRef)CFDictionaryGetValue(pngProperties, WebCoreCGImagePropertyAPNGDelayTime))
436                 CFNumberGetValue(num, kCFNumberFloatType, &duration);
437         }
438     }
439
440     // Many annoying ads specify a 0 duration to make an image flash as quickly as possible.
441     // We follow Firefox's behavior and use a duration of 100 ms for any frames that specify
442     // a duration of <= 10 ms. See <rdar://problem/7689300> and <http://webkit.org/b/36082>
443     // for more information.
444     if (duration < 0.011f)
445         return 0.1f;
446     return duration;
447 }
448
449 bool ImageSource::frameHasAlphaAtIndex(size_t index)
450 {
451     if (!m_decoder)
452         return false; // FIXME: why doesn't this return true?
453
454     if (!frameIsCompleteAtIndex(index))
455         return true;
456
457     CFStringRef imageType = CGImageSourceGetType(m_decoder);
458
459     // Return false if there is no image type or the image type is JPEG, because
460     // JPEG does not support alpha transparency.
461     if (!imageType || CFEqual(imageType, CFSTR("public.jpeg")))
462         return false;
463
464     // FIXME: Could return false for other non-transparent image formats.
465     // FIXME: Could maybe return false for a GIF Frame if we have enough info in the GIF properties dictionary
466     // to determine whether or not a transparent color was defined.
467     return true;
468 }
469
470 unsigned ImageSource::frameBytesAtIndex(size_t index, SubsamplingLevel subsamplingLevel) const
471 {
472     IntSize frameSize = frameSizeAtIndex(index, subsamplingLevel, ImageOrientationDescription(RespectImageOrientation));
473     return frameSize.width() * frameSize.height() * 4;
474 }
475
476 }
477
478 #endif // USE(CG)