FLAnimatedImage.m 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. //
  2. // FLAnimatedImage.m
  3. // Flipboard
  4. //
  5. // Created by Raphael Schaad on 7/8/13.
  6. // Copyright (c) 2013-2015 Flipboard. All rights reserved.
  7. //
  8. #import "FLAnimatedImage.h"
  9. #import <ImageIO/ImageIO.h>
  10. #import <MobileCoreServices/MobileCoreServices.h>
  11. // From vm_param.h, define for iOS 8.0 or higher to build on device.
  12. #ifndef BYTE_SIZE
  13. #define BYTE_SIZE 8 // byte size in bits
  14. #endif
  15. #define MEGABYTE (1024 * 1024)
  16. #if FLLumberjackIntegrationEnabled && defined(FLLumberjackAvailable)
  17. #if defined(DEBUG) && DEBUG
  18. #if defined(LOG_LEVEL_DEBUG) // CocoaLumberjack 1.x
  19. int flAnimatedImageLogLevel = LOG_LEVEL_DEBUG;
  20. #else // CocoaLumberjack 2.x
  21. int flAnimatedImageLogLevel = DDLogFlagDebug;
  22. #endif
  23. #else
  24. #if defined(LOG_LEVEL_WARN) // CocoaLumberjack 1.x
  25. int flAnimatedImageLogLevel = LOG_LEVEL_WARN;
  26. #else // CocoaLumberjack 2.x
  27. int flAnimatedImageLogLevel = DDLogFlagWarning;
  28. #endif
  29. #endif
  30. #endif
  31. // An individual animated image's ideal memory footprint, used when calculating the number of frames to preload
  32. // Note this is per-instance and not across all FLAnimatedImages, it would probably be best if the 10MB memory footprint was shared across all instances
  33. // (Meaning if you had 100 FLAnimatedImages the sum of their memory footprints / preloaded frames should not exceed 10MB)
  34. static const CGFloat kFLAnimatedImageIdealMemoryFootprint = 10.0;
  35. typedef NS_ENUM(NSUInteger, FLAnimatedImageFrameCacheSize) {
  36. FLAnimatedImageFrameCacheSizeNoLimit = 0, // 0 means no specific limit
  37. FLAnimatedImageFrameCacheSizeLowMemory = 1, // The minimum frame cache size; this will produce frames on-demand.
  38. FLAnimatedImageFrameCacheSizeGrowAfterMemoryWarning = 2, // If we can produce the frames faster than we consume, one frame ahead will already result in a stutter-free playback.
  39. FLAnimatedImageFrameCacheSizeDefault = 5 // Build up a comfy buffer window to cope with CPU hiccups etc.
  40. };
  41. @interface FLAnimatedImage ()
  42. @property (nonatomic, assign, readonly) NSUInteger frameCacheSizeOptimal; // The optimal number of frames to cache based on image size & number of frames; never changes
  43. @property (nonatomic, assign) NSUInteger frameCacheSizeMaxInternal; // Allow to cap the cache size e.g. when memory warnings occur; 0 means no specific limit (default)
  44. @property (nonatomic, assign) NSUInteger requestedFrameIndex; // Most recently requested frame index
  45. @property (nonatomic, assign, readonly) NSUInteger posterImageFrameIndex; // Index of non-purgable poster image; never changes
  46. @property (nonatomic, strong, readonly) NSMutableDictionary *cachedFramesForIndexes;
  47. @property (nonatomic, strong, readonly) NSMutableIndexSet *cachedFrameIndexes; // Indexes of cached frames
  48. @property (nonatomic, strong, readonly) NSMutableIndexSet *requestedFrameIndexes; // Indexes of frames that are currently produced in the background
  49. @property (nonatomic, strong, readonly) NSIndexSet *allFramesIndexSet; // Default index set with the full range of indexes; never changes
  50. @property (nonatomic, assign) NSUInteger memoryWarningCount;
  51. @property (nonatomic, strong, readonly) dispatch_queue_t serialQueue;
  52. @property (nonatomic, strong, readonly) __attribute__((NSObject)) CGImageSourceRef imageSource;
  53. // The weak proxy is used to break retain cycles with delayed actions from memory warnings.
  54. // We are lying about the actual type here to gain static type checking and eliminate casts.
  55. // The actual type of the object is `FLWeakProxy`.
  56. @property (nonatomic, strong, readonly) FLAnimatedImage *weakProxy;
  57. @end
  58. // For custom dispatching of memory warnings to avoid deallocation races since NSNotificationCenter doesn't retain objects it is notifying.
  59. static NSHashTable *allAnimatedImagesWeak;
  60. @implementation FLAnimatedImage
  61. #pragma mark - Accessors
  62. #pragma mark Public
  63. // This is the definite value the frame cache needs to size itself to.
  64. - (NSUInteger)frameCacheSizeCurrent
  65. {
  66. NSUInteger frameCacheSizeCurrent = self.frameCacheSizeOptimal;
  67. // If set, respect the caps.
  68. if (self.frameCacheSizeMax > FLAnimatedImageFrameCacheSizeNoLimit) {
  69. frameCacheSizeCurrent = MIN(frameCacheSizeCurrent, self.frameCacheSizeMax);
  70. }
  71. if (self.frameCacheSizeMaxInternal > FLAnimatedImageFrameCacheSizeNoLimit) {
  72. frameCacheSizeCurrent = MIN(frameCacheSizeCurrent, self.frameCacheSizeMaxInternal);
  73. }
  74. return frameCacheSizeCurrent;
  75. }
  76. - (void)setFrameCacheSizeMax:(NSUInteger)frameCacheSizeMax
  77. {
  78. if (_frameCacheSizeMax != frameCacheSizeMax) {
  79. // Remember whether the new cap will cause the current cache size to shrink; then we'll make sure to purge from the cache if needed.
  80. BOOL willFrameCacheSizeShrink = (frameCacheSizeMax < self.frameCacheSizeCurrent);
  81. // Update the value
  82. _frameCacheSizeMax = frameCacheSizeMax;
  83. if (willFrameCacheSizeShrink) {
  84. [self purgeFrameCacheIfNeeded];
  85. }
  86. }
  87. }
  88. #pragma mark Private
  89. - (void)setFrameCacheSizeMaxInternal:(NSUInteger)frameCacheSizeMaxInternal
  90. {
  91. if (_frameCacheSizeMaxInternal != frameCacheSizeMaxInternal) {
  92. // Remember whether the new cap will cause the current cache size to shrink; then we'll make sure to purge from the cache if needed.
  93. BOOL willFrameCacheSizeShrink = (frameCacheSizeMaxInternal < self.frameCacheSizeCurrent);
  94. // Update the value
  95. _frameCacheSizeMaxInternal = frameCacheSizeMaxInternal;
  96. if (willFrameCacheSizeShrink) {
  97. [self purgeFrameCacheIfNeeded];
  98. }
  99. }
  100. }
  101. #pragma mark - Life Cycle
  102. + (void)initialize
  103. {
  104. if (self == [FLAnimatedImage class]) {
  105. // UIKit memory warning notification handler shared by all of the instances
  106. allAnimatedImagesWeak = [NSHashTable weakObjectsHashTable];
  107. [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidReceiveMemoryWarningNotification object:nil queue:nil usingBlock:^(NSNotification *note) {
  108. // UIKit notifications are posted on the main thread. didReceiveMemoryWarning: is expecting the main run loop, and we don't lock on allAnimatedImagesWeak
  109. NSAssert([NSThread isMainThread], @"Received memory warning on non-main thread");
  110. // Get a strong reference to all of the images. If an instance is returned in this array, it is still live and has not entered dealloc.
  111. // Note that FLAnimatedImages can be created on any thread, so the hash table must be locked.
  112. NSArray *images = nil;
  113. @synchronized(allAnimatedImagesWeak) {
  114. images = [[allAnimatedImagesWeak allObjects] copy];
  115. }
  116. // Now issue notifications to all of the images while holding a strong reference to them
  117. [images makeObjectsPerformSelector:@selector(didReceiveMemoryWarning:) withObject:note];
  118. }];
  119. }
  120. }
  121. - (instancetype)init
  122. {
  123. FLAnimatedImage *animatedImage = [self initWithAnimatedGIFData:nil];
  124. if (!animatedImage) {
  125. FLLogError(@"Use `-initWithAnimatedGIFData:` and supply the animated GIF data as an argument to initialize an object of type `FLAnimatedImage`.");
  126. }
  127. return animatedImage;
  128. }
  129. - (instancetype)initWithAnimatedGIFData:(NSData *)data
  130. {
  131. // Early return if no data supplied!
  132. BOOL hasData = ([data length] > 0);
  133. if (!hasData) {
  134. FLLogError(@"No animated GIF data supplied.");
  135. return nil;
  136. }
  137. self = [super init];
  138. if (self) {
  139. // Do one-time initializations of `readonly` properties directly to ivar to prevent implicit actions and avoid need for private `readwrite` property overrides.
  140. // Keep a strong reference to `data` and expose it read-only publicly.
  141. // However, we will use the `_imageSource` as handler to the image data throughout our life cycle.
  142. _data = data;
  143. // Initialize internal data structures
  144. _cachedFramesForIndexes = [[NSMutableDictionary alloc] init];
  145. _cachedFrameIndexes = [[NSMutableIndexSet alloc] init];
  146. _requestedFrameIndexes = [[NSMutableIndexSet alloc] init];
  147. // Note: We could leverage `CGImageSourceCreateWithURL` too to add a second initializer `-initWithAnimatedGIFContentsOfURL:`.
  148. _imageSource = CGImageSourceCreateWithData((__bridge CFDataRef)data,
  149. (__bridge CFDictionaryRef)@{(NSString *)kCGImageSourceShouldCache: @NO});
  150. // Early return on failure!
  151. if (!_imageSource) {
  152. FLLogError(@"Failed to `CGImageSourceCreateWithData` for animated GIF data %@", data);
  153. return nil;
  154. }
  155. // Early return if not GIF!
  156. CFStringRef imageSourceContainerType = CGImageSourceGetType(_imageSource);
  157. BOOL isGIFData = UTTypeConformsTo(imageSourceContainerType, kUTTypeGIF);
  158. if (!isGIFData) {
  159. FLLogError(@"Supplied data is of type %@ and doesn't seem to be GIF data %@", imageSourceContainerType, data);
  160. return nil;
  161. }
  162. // Get `LoopCount`
  163. // Note: 0 means repeating the animation indefinitely.
  164. // Image properties example:
  165. // {
  166. // FileSize = 314446;
  167. // "{GIF}" = {
  168. // HasGlobalColorMap = 1;
  169. // LoopCount = 0;
  170. // };
  171. // }
  172. NSDictionary *imageProperties = (__bridge_transfer NSDictionary *)CGImageSourceCopyProperties(_imageSource, NULL);
  173. _loopCount = [[(NSDictionary *)[imageProperties objectForKey:(id)kCGImagePropertyGIFDictionary] objectForKey:(id)kCGImagePropertyGIFLoopCount] unsignedIntegerValue];
  174. // Iterate through frame images
  175. size_t imageCount = CGImageSourceGetCount(_imageSource);
  176. NSUInteger skippedFrameCount = 0;
  177. NSMutableDictionary *delayTimesForIndexesMutable = [NSMutableDictionary dictionaryWithCapacity:imageCount];
  178. for (size_t i = 0; i < imageCount; i++) {
  179. CGImageRef frameImageRef = CGImageSourceCreateImageAtIndex(_imageSource, i, NULL);
  180. if (frameImageRef) {
  181. UIImage *frameImage = [UIImage imageWithCGImage:frameImageRef];
  182. // Check for valid `frameImage` before parsing its properties as frames can be corrupted (and `frameImage` even `nil` when `frameImageRef` was valid).
  183. if (frameImage) {
  184. // Set poster image
  185. if (!self.posterImage) {
  186. _posterImage = frameImage;
  187. // Set its size to proxy our size.
  188. _size = _posterImage.size;
  189. // Remember index of poster image so we never purge it; also add it to the cache.
  190. _posterImageFrameIndex = i;
  191. [self.cachedFramesForIndexes setObject:self.posterImage forKey:@(self.posterImageFrameIndex)];
  192. [self.cachedFrameIndexes addIndex:self.posterImageFrameIndex];
  193. }
  194. // Get `DelayTime`
  195. // Note: It's not in (1/100) of a second like still falsely described in the documentation as per iOS 8 (rdar://19507384) but in seconds stored as `kCFNumberFloat32Type`.
  196. // Frame properties example:
  197. // {
  198. // ColorModel = RGB;
  199. // Depth = 8;
  200. // PixelHeight = 960;
  201. // PixelWidth = 640;
  202. // "{GIF}" = {
  203. // DelayTime = "0.4";
  204. // UnclampedDelayTime = "0.4";
  205. // };
  206. // }
  207. NSDictionary *frameProperties = (__bridge_transfer NSDictionary *)CGImageSourceCopyPropertiesAtIndex(_imageSource, i, NULL);
  208. NSDictionary *framePropertiesGIF = [frameProperties objectForKey:(id)kCGImagePropertyGIFDictionary];
  209. // Try to use the unclamped delay time; fall back to the normal delay time.
  210. NSNumber *delayTime = [framePropertiesGIF objectForKey:(id)kCGImagePropertyGIFUnclampedDelayTime];
  211. if (!delayTime) {
  212. delayTime = [framePropertiesGIF objectForKey:(id)kCGImagePropertyGIFDelayTime];
  213. }
  214. // If we don't get a delay time from the properties, fall back to `kDelayTimeIntervalDefault` or carry over the preceding frame's value.
  215. const NSTimeInterval kDelayTimeIntervalDefault = 0.1;
  216. if (!delayTime) {
  217. if (i == 0) {
  218. FLLogInfo(@"Falling back to default delay time for first frame %@ because none found in GIF properties %@", frameImage, frameProperties);
  219. delayTime = @(kDelayTimeIntervalDefault);
  220. } else {
  221. FLLogInfo(@"Falling back to preceding delay time for frame %zu %@ because none found in GIF properties %@", i, frameImage, frameProperties);
  222. delayTime = delayTimesForIndexesMutable[@(i - 1)];
  223. }
  224. }
  225. // Support frame delays as low as `kDelayTimeIntervalMinimum`, with anything below being rounded up to `kDelayTimeIntervalDefault` for legacy compatibility.
  226. // This is how the fastest browsers do it as per 2012: http://nullsleep.tumblr.com/post/16524517190/animated-gif-minimum-frame-delay-browser-compatibility
  227. const NSTimeInterval kDelayTimeIntervalMinimum = 0.02;
  228. // To support the minimum even when rounding errors occur, use an epsilon when comparing. We downcast to float because that's what we get for delayTime from ImageIO.
  229. if ([delayTime floatValue] < ((float)kDelayTimeIntervalMinimum - FLT_EPSILON)) {
  230. FLLogInfo(@"Rounding frame %zu's `delayTime` from %f up to default %f (minimum supported: %f).", i, [delayTime floatValue], kDelayTimeIntervalDefault, kDelayTimeIntervalMinimum);
  231. delayTime = @(kDelayTimeIntervalDefault);
  232. }
  233. delayTimesForIndexesMutable[@(i)] = delayTime;
  234. } else {
  235. skippedFrameCount++;
  236. FLLogInfo(@"Dropping frame %zu because valid `CGImageRef` %@ did result in `nil`-`UIImage`.", i, frameImageRef);
  237. }
  238. CFRelease(frameImageRef);
  239. } else {
  240. skippedFrameCount++;
  241. FLLogInfo(@"Dropping frame %zu because failed to `CGImageSourceCreateImageAtIndex` with image source %@", i, _imageSource);
  242. }
  243. }
  244. _delayTimesForIndexes = [delayTimesForIndexesMutable copy];
  245. _frameCount = imageCount;
  246. if (self.frameCount == 0) {
  247. FLLogInfo(@"Failed to create any valid frames for GIF with properties %@", imageProperties);
  248. return nil;
  249. } else if (self.frameCount == 1) {
  250. // Warn when we only have a single frame but return a valid GIF.
  251. FLLogInfo(@"Created valid GIF but with only a single frame. Image properties: %@", imageProperties);
  252. } else {
  253. // We have multiple frames, rock on!
  254. }
  255. // Calculate the optimal frame cache size by fitting the most number of frames possible into the quota specified by kFLAnimatedImageIdealMemoryFootprint
  256. CGFloat animatedImageFrameSize = CGImageGetBytesPerRow(self.posterImage.CGImage) * self.size.height / MEGABYTE;
  257. _frameCacheSizeOptimal = floor(kFLAnimatedImageIdealMemoryFootprint / animatedImageFrameSize);
  258. // Cap the optimal cache size. "5 frames ought to be enough for anybody" http://bit.ly/1M1gCvs.
  259. _frameCacheSizeOptimal = MIN(_frameCacheSizeOptimal, FLAnimatedImageFrameCacheSizeDefault);
  260. // There also must be at least one.
  261. _frameCacheSizeOptimal = MAX(_frameCacheSizeOptimal, 1);
  262. // Convenience/minor performance optimization; keep an index set handy with the full range to return in `-frameIndexesToCache`.
  263. _allFramesIndexSet = [[NSIndexSet alloc] initWithIndexesInRange:NSMakeRange(0, self.frameCount)];
  264. // See the property declarations for descriptions.
  265. _weakProxy = (id)[FLWeakProxy weakProxyForObject:self];
  266. // Register this instance in the weak table for memory notifications. The NSHashTable will clean up after itself when we're gone.
  267. // Note that FLAnimatedImages can be created on any thread, so the hash table must be locked.
  268. @synchronized(allAnimatedImagesWeak) {
  269. [allAnimatedImagesWeak addObject:self];
  270. }
  271. }
  272. return self;
  273. }
  274. + (instancetype)animatedImageWithGIFData:(NSData *)data
  275. {
  276. FLAnimatedImage *animatedImage = [[FLAnimatedImage alloc] initWithAnimatedGIFData:data];
  277. return animatedImage;
  278. }
  279. - (void)dealloc
  280. {
  281. if (_weakProxy) {
  282. [NSObject cancelPreviousPerformRequestsWithTarget:_weakProxy];
  283. }
  284. if (_imageSource) {
  285. CFRelease(_imageSource);
  286. }
  287. }
  288. #pragma mark - Public Methods
  289. // See header for more details.
  290. // Note: both consumer and producer are throttled: consumer by frame timings and producer by the available memory (max buffer window size).
  291. - (UIImage *)imageLazilyCachedAtIndex:(NSUInteger)index
  292. {
  293. // Early return if the requested index is beyond bounds.
  294. // Note: We're comparing an index with a count and need to bail on greater than or equal to.
  295. if (index >= self.frameCount) {
  296. FLLogWarn(@"Skipping requested frame %lu beyond bounds (total frame count: %lu) for animated image: %@", (unsigned long)index, (unsigned long)self.frameCount, self);
  297. return nil;
  298. }
  299. // Remember requested frame index, this influences what we should cache next.
  300. self.requestedFrameIndex = index;
  301. #if defined(DEBUG) && DEBUG
  302. if ([self.debug_delegate respondsToSelector:@selector(debug_animatedImage:didRequestCachedFrame:)]) {
  303. [self.debug_delegate debug_animatedImage:self didRequestCachedFrame:index];
  304. }
  305. #endif
  306. // Quick check to avoid doing any work if we already have all possible frames cached, a common case.
  307. if ([self.cachedFrameIndexes count] < self.frameCount) {
  308. // If we have frames that should be cached but aren't and aren't requested yet, request them.
  309. // Exclude existing cached frames, frames already requested, and specially cached poster image.
  310. NSMutableIndexSet *frameIndexesToAddToCacheMutable = [[self frameIndexesToCache] mutableCopy];
  311. [frameIndexesToAddToCacheMutable removeIndexes:self.cachedFrameIndexes];
  312. [frameIndexesToAddToCacheMutable removeIndexes:self.requestedFrameIndexes];
  313. [frameIndexesToAddToCacheMutable removeIndex:self.posterImageFrameIndex];
  314. NSIndexSet *frameIndexesToAddToCache = [frameIndexesToAddToCacheMutable copy];
  315. // Asynchronously add frames to our cache.
  316. if ([frameIndexesToAddToCache count] > 0) {
  317. [self addFrameIndexesToCache:frameIndexesToAddToCache];
  318. }
  319. }
  320. // Get the specified image.
  321. UIImage *image = self.cachedFramesForIndexes[@(index)];
  322. // Purge if needed based on the current playhead position.
  323. [self purgeFrameCacheIfNeeded];
  324. return image;
  325. }
  326. // Only called once from `-imageLazilyCachedAtIndex` but factored into its own method for logical grouping.
  327. - (void)addFrameIndexesToCache:(NSIndexSet *)frameIndexesToAddToCache
  328. {
  329. // Order matters. First, iterate over the indexes starting from the requested frame index.
  330. // Then, if there are any indexes before the requested frame index, do those.
  331. NSRange firstRange = NSMakeRange(self.requestedFrameIndex, self.frameCount - self.requestedFrameIndex);
  332. NSRange secondRange = NSMakeRange(0, self.requestedFrameIndex);
  333. if (firstRange.length + secondRange.length != self.frameCount) {
  334. FLLogWarn(@"Two-part frame cache range doesn't equal full range.");
  335. }
  336. // Add to the requested list before we actually kick them off, so they don't get into the queue twice.
  337. [self.requestedFrameIndexes addIndexes:frameIndexesToAddToCache];
  338. // Lazily create dedicated isolation queue.
  339. if (!self.serialQueue) {
  340. _serialQueue = dispatch_queue_create("com.flipboard.framecachingqueue", DISPATCH_QUEUE_SERIAL);
  341. }
  342. // Start streaming requested frames in the background into the cache.
  343. // Avoid capturing self in the block as there's no reason to keep doing work if the animated image went away.
  344. FLAnimatedImage * __weak weakSelf = self;
  345. dispatch_async(self.serialQueue, ^{
  346. // Produce and cache next needed frame.
  347. void (^frameRangeBlock)(NSRange, BOOL *) = ^(NSRange range, BOOL *stop) {
  348. // Iterate through contiguous indexes; can be faster than `enumerateIndexesInRange:options:usingBlock:`.
  349. for (NSUInteger i = range.location; i < NSMaxRange(range); i++) {
  350. #if defined(DEBUG) && DEBUG
  351. CFTimeInterval predrawBeginTime = CACurrentMediaTime();
  352. #endif
  353. UIImage *image = [weakSelf predrawnImageAtIndex:i];
  354. #if defined(DEBUG) && DEBUG
  355. CFTimeInterval predrawDuration = CACurrentMediaTime() - predrawBeginTime;
  356. CFTimeInterval slowdownDuration = 0.0;
  357. if ([self.debug_delegate respondsToSelector:@selector(debug_animatedImagePredrawingSlowdownFactor:)]) {
  358. CGFloat predrawingSlowdownFactor = [self.debug_delegate debug_animatedImagePredrawingSlowdownFactor:self];
  359. slowdownDuration = predrawDuration * predrawingSlowdownFactor - predrawDuration;
  360. [NSThread sleepForTimeInterval:slowdownDuration];
  361. }
  362. FLLogVerbose(@"Predrew frame %lu in %f ms for animated image: %@", (unsigned long)i, (predrawDuration + slowdownDuration) * 1000, self);
  363. #endif
  364. // The results get returned one by one as soon as they're ready (and not in batch).
  365. // The benefits of having the first frames as quick as possible outweigh building up a buffer to cope with potential hiccups when the CPU suddenly gets busy.
  366. if (image && weakSelf) {
  367. dispatch_async(dispatch_get_main_queue(), ^{
  368. weakSelf.cachedFramesForIndexes[@(i)] = image;
  369. [weakSelf.cachedFrameIndexes addIndex:i];
  370. [weakSelf.requestedFrameIndexes removeIndex:i];
  371. #if defined(DEBUG) && DEBUG
  372. if ([weakSelf.debug_delegate respondsToSelector:@selector(debug_animatedImage:didUpdateCachedFrames:)]) {
  373. [weakSelf.debug_delegate debug_animatedImage:weakSelf didUpdateCachedFrames:weakSelf.cachedFrameIndexes];
  374. }
  375. #endif
  376. });
  377. }
  378. }
  379. };
  380. [frameIndexesToAddToCache enumerateRangesInRange:firstRange options:0 usingBlock:frameRangeBlock];
  381. [frameIndexesToAddToCache enumerateRangesInRange:secondRange options:0 usingBlock:frameRangeBlock];
  382. });
  383. }
  384. + (CGSize)sizeForImage:(id)image
  385. {
  386. CGSize imageSize = CGSizeZero;
  387. // Early return for nil
  388. if (!image) {
  389. return imageSize;
  390. }
  391. if ([image isKindOfClass:[UIImage class]]) {
  392. UIImage *uiImage = (UIImage *)image;
  393. imageSize = uiImage.size;
  394. } else if ([image isKindOfClass:[FLAnimatedImage class]]) {
  395. FLAnimatedImage *animatedImage = (FLAnimatedImage *)image;
  396. imageSize = animatedImage.size;
  397. } else {
  398. // Bear trap to capture bad images; we have seen crashers cropping up on iOS 7.
  399. FLLogError(@"`image` isn't of expected types `UIImage` or `FLAnimatedImage`: %@", image);
  400. }
  401. return imageSize;
  402. }
  403. #pragma mark - Private Methods
  404. #pragma mark Frame Loading
  405. - (UIImage *)predrawnImageAtIndex:(NSUInteger)index
  406. {
  407. // It's very important to use the cached `_imageSource` since the random access to a frame with `CGImageSourceCreateImageAtIndex` turns from an O(1) into an O(n) operation when re-initializing the image source every time.
  408. CGImageRef imageRef = CGImageSourceCreateImageAtIndex(_imageSource, index, NULL);
  409. UIImage *image = [UIImage imageWithCGImage:imageRef];
  410. CFRelease(imageRef);
  411. // Loading in the image object is only half the work, the displaying image view would still have to synchronosly wait and decode the image, so we go ahead and do that here on the background thread.
  412. image = [[self class] predrawnImageFromImage:image];
  413. return image;
  414. }
  415. #pragma mark Frame Caching
  416. - (NSIndexSet *)frameIndexesToCache
  417. {
  418. NSIndexSet *indexesToCache = nil;
  419. // Quick check to avoid building the index set if the number of frames to cache equals the total frame count.
  420. if (self.frameCacheSizeCurrent == self.frameCount) {
  421. indexesToCache = self.allFramesIndexSet;
  422. } else {
  423. NSMutableIndexSet *indexesToCacheMutable = [[NSMutableIndexSet alloc] init];
  424. // Add indexes to the set in two separate blocks- the first starting from the requested frame index, up to the limit or the end.
  425. // The second, if needed, the remaining number of frames beginning at index zero.
  426. NSUInteger firstLength = MIN(self.frameCacheSizeCurrent, self.frameCount - self.requestedFrameIndex);
  427. NSRange firstRange = NSMakeRange(self.requestedFrameIndex, firstLength);
  428. [indexesToCacheMutable addIndexesInRange:firstRange];
  429. NSUInteger secondLength = self.frameCacheSizeCurrent - firstLength;
  430. if (secondLength > 0) {
  431. NSRange secondRange = NSMakeRange(0, secondLength);
  432. [indexesToCacheMutable addIndexesInRange:secondRange];
  433. }
  434. // Double check our math, before we add the poster image index which may increase it by one.
  435. if ([indexesToCacheMutable count] != self.frameCacheSizeCurrent) {
  436. FLLogWarn(@"Number of frames to cache doesn't equal expected cache size.");
  437. }
  438. [indexesToCacheMutable addIndex:self.posterImageFrameIndex];
  439. indexesToCache = [indexesToCacheMutable copy];
  440. }
  441. return indexesToCache;
  442. }
  443. - (void)purgeFrameCacheIfNeeded
  444. {
  445. // Purge frames that are currently cached but don't need to be.
  446. // But not if we're still under the number of frames to cache.
  447. // This way, if all frames are allowed to be cached (the common case), we can skip all the `NSIndexSet` math below.
  448. if ([self.cachedFrameIndexes count] > self.frameCacheSizeCurrent) {
  449. NSMutableIndexSet *indexesToPurge = [self.cachedFrameIndexes mutableCopy];
  450. [indexesToPurge removeIndexes:[self frameIndexesToCache]];
  451. [indexesToPurge enumerateRangesUsingBlock:^(NSRange range, BOOL *stop) {
  452. // Iterate through contiguous indexes; can be faster than `enumerateIndexesInRange:options:usingBlock:`.
  453. for (NSUInteger i = range.location; i < NSMaxRange(range); i++) {
  454. [self.cachedFrameIndexes removeIndex:i];
  455. [self.cachedFramesForIndexes removeObjectForKey:@(i)];
  456. // Note: Don't `CGImageSourceRemoveCacheAtIndex` on the image source for frames that we don't want cached any longer to maintain O(1) time access.
  457. #if defined(DEBUG) && DEBUG
  458. if ([self.debug_delegate respondsToSelector:@selector(debug_animatedImage:didUpdateCachedFrames:)]) {
  459. dispatch_async(dispatch_get_main_queue(), ^{
  460. [self.debug_delegate debug_animatedImage:self didUpdateCachedFrames:self.cachedFrameIndexes];
  461. });
  462. }
  463. #endif
  464. }
  465. }];
  466. }
  467. }
  468. - (void)growFrameCacheSizeAfterMemoryWarning:(NSNumber *)frameCacheSize
  469. {
  470. self.frameCacheSizeMaxInternal = [frameCacheSize unsignedIntegerValue];
  471. FLLogDebug(@"Grew frame cache size max to %lu after memory warning for animated image: %@", (unsigned long)self.frameCacheSizeMaxInternal, self);
  472. // Schedule resetting the frame cache size max completely after a while.
  473. const NSTimeInterval kResetDelay = 3.0;
  474. [self.weakProxy performSelector:@selector(resetFrameCacheSizeMaxInternal) withObject:nil afterDelay:kResetDelay];
  475. }
  476. - (void)resetFrameCacheSizeMaxInternal
  477. {
  478. self.frameCacheSizeMaxInternal = FLAnimatedImageFrameCacheSizeNoLimit;
  479. FLLogDebug(@"Reset frame cache size max (current frame cache size: %lu) for animated image: %@", (unsigned long)self.frameCacheSizeCurrent, self);
  480. }
  481. #pragma mark System Memory Warnings Notification Handler
  482. - (void)didReceiveMemoryWarning:(NSNotification *)notification
  483. {
  484. self.memoryWarningCount++;
  485. // If we were about to grow larger, but got rapped on our knuckles by the system again, cancel.
  486. [NSObject cancelPreviousPerformRequestsWithTarget:self.weakProxy selector:@selector(growFrameCacheSizeAfterMemoryWarning:) object:@(FLAnimatedImageFrameCacheSizeGrowAfterMemoryWarning)];
  487. [NSObject cancelPreviousPerformRequestsWithTarget:self.weakProxy selector:@selector(resetFrameCacheSizeMaxInternal) object:nil];
  488. // Go down to the minimum and by that implicitly immediately purge from the cache if needed to not get jettisoned by the system and start producing frames on-demand.
  489. FLLogDebug(@"Attempt setting frame cache size max to %lu (previous was %lu) after memory warning #%lu for animated image: %@", (unsigned long)FLAnimatedImageFrameCacheSizeLowMemory, (unsigned long)self.frameCacheSizeMaxInternal, (unsigned long)self.memoryWarningCount, self);
  490. self.frameCacheSizeMaxInternal = FLAnimatedImageFrameCacheSizeLowMemory;
  491. // Schedule growing larger again after a while, but cap our attempts to prevent a periodic sawtooth wave (ramps upward and then sharply drops) of memory usage.
  492. //
  493. // [mem]^ (2) (5) (6) 1) Loading frames for the first time
  494. // (*)| , , , 2) Mem warning #1; purge cache
  495. // | /| (4)/| /| 3) Grow cache size a bit after a while, if no mem warning occurs
  496. // | / | _/ | _/ | 4) Try to grow cache size back to optimum after a while, if no mem warning occurs
  497. // |(1)/ |_/ |/ |__(7) 5) Mem warning #2; purge cache
  498. // |__/ (3) 6) After repetition of (3) and (4), mem warning #3; purge cache
  499. // +----------------------> 7) After 3 mem warnings, stay at minimum cache size
  500. // [t]
  501. // *) The mem high water mark before we get warned might change for every cycle.
  502. //
  503. const NSUInteger kGrowAttemptsMax = 2;
  504. const NSTimeInterval kGrowDelay = 2.0;
  505. if ((self.memoryWarningCount - 1) <= kGrowAttemptsMax) {
  506. [self.weakProxy performSelector:@selector(growFrameCacheSizeAfterMemoryWarning:) withObject:@(FLAnimatedImageFrameCacheSizeGrowAfterMemoryWarning) afterDelay:kGrowDelay];
  507. }
  508. // Note: It's not possible to get the level of a memory warning with a public API: http://stackoverflow.com/questions/2915247/iphone-os-memory-warnings-what-do-the-different-levels-mean/2915477#2915477
  509. }
  510. #pragma mark Image Decoding
  511. // Decodes the image's data and draws it off-screen fully in memory; it's thread-safe and hence can be called on a background thread.
  512. // On success, the returned object is a new `UIImage` instance with the same content as the one passed in.
  513. // On failure, the returned object is the unchanged passed in one; the data will not be predrawn in memory though and an error will be logged.
  514. // First inspired by & good Karma to: https://gist.github.com/steipete/1144242
  515. + (UIImage *)predrawnImageFromImage:(UIImage *)imageToPredraw
  516. {
  517. // Always use a device RGB color space for simplicity and predictability what will be going on.
  518. CGColorSpaceRef colorSpaceDeviceRGBRef = CGColorSpaceCreateDeviceRGB();
  519. // Early return on failure!
  520. if (!colorSpaceDeviceRGBRef) {
  521. FLLogError(@"Failed to `CGColorSpaceCreateDeviceRGB` for image %@", imageToPredraw);
  522. return imageToPredraw;
  523. }
  524. // Even when the image doesn't have transparency, we have to add the extra channel because Quartz doesn't support other pixel formats than 32 bpp/8 bpc for RGB:
  525. // kCGImageAlphaNoneSkipFirst, kCGImageAlphaNoneSkipLast, kCGImageAlphaPremultipliedFirst, kCGImageAlphaPremultipliedLast
  526. // (source: docs "Quartz 2D Programming Guide > Graphics Contexts > Table 2-1 Pixel formats supported for bitmap graphics contexts")
  527. size_t numberOfComponents = CGColorSpaceGetNumberOfComponents(colorSpaceDeviceRGBRef) + 1; // 4: RGB + A
  528. // "In iOS 4.0 and later, and OS X v10.6 and later, you can pass NULL if you want Quartz to allocate memory for the bitmap." (source: docs)
  529. void *data = NULL;
  530. size_t width = imageToPredraw.size.width;
  531. size_t height = imageToPredraw.size.height;
  532. size_t bitsPerComponent = CHAR_BIT;
  533. size_t bitsPerPixel = (bitsPerComponent * numberOfComponents);
  534. size_t bytesPerPixel = (bitsPerPixel / BYTE_SIZE);
  535. size_t bytesPerRow = (bytesPerPixel * width);
  536. CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
  537. CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageToPredraw.CGImage);
  538. // If the alpha info doesn't match to one of the supported formats (see above), pick a reasonable supported one.
  539. // "For bitmaps created in iOS 3.2 and later, the drawing environment uses the premultiplied ARGB format to store the bitmap data." (source: docs)
  540. if (alphaInfo == kCGImageAlphaNone || alphaInfo == kCGImageAlphaOnly) {
  541. alphaInfo = kCGImageAlphaNoneSkipFirst;
  542. } else if (alphaInfo == kCGImageAlphaFirst) {
  543. alphaInfo = kCGImageAlphaPremultipliedFirst;
  544. } else if (alphaInfo == kCGImageAlphaLast) {
  545. alphaInfo = kCGImageAlphaPremultipliedLast;
  546. }
  547. // "The constants for specifying the alpha channel information are declared with the `CGImageAlphaInfo` type but can be passed to this parameter safely." (source: docs)
  548. bitmapInfo |= alphaInfo;
  549. // Create our own graphics context to draw to; `UIGraphicsGetCurrentContext`/`UIGraphicsBeginImageContextWithOptions` doesn't create a new context but returns the current one which isn't thread-safe (e.g. main thread could use it at the same time).
  550. // Note: It's not worth caching the bitmap context for multiple frames ("unique key" would be `width`, `height` and `hasAlpha`), it's ~50% slower. Time spent in libRIP's `CGSBlendBGRA8888toARGB8888` suddenly shoots up -- not sure why.
  551. CGContextRef bitmapContextRef = CGBitmapContextCreate(data, width, height, bitsPerComponent, bytesPerRow, colorSpaceDeviceRGBRef, bitmapInfo);
  552. CGColorSpaceRelease(colorSpaceDeviceRGBRef);
  553. // Early return on failure!
  554. if (!bitmapContextRef) {
  555. FLLogError(@"Failed to `CGBitmapContextCreate` with color space %@ and parameters (width: %zu height: %zu bitsPerComponent: %zu bytesPerRow: %zu) for image %@", colorSpaceDeviceRGBRef, width, height, bitsPerComponent, bytesPerRow, imageToPredraw);
  556. return imageToPredraw;
  557. }
  558. // Draw image in bitmap context and create image by preserving receiver's properties.
  559. CGContextDrawImage(bitmapContextRef, CGRectMake(0.0, 0.0, imageToPredraw.size.width, imageToPredraw.size.height), imageToPredraw.CGImage);
  560. CGImageRef predrawnImageRef = CGBitmapContextCreateImage(bitmapContextRef);
  561. UIImage *predrawnImage = [UIImage imageWithCGImage:predrawnImageRef scale:imageToPredraw.scale orientation:imageToPredraw.imageOrientation];
  562. CGImageRelease(predrawnImageRef);
  563. CGContextRelease(bitmapContextRef);
  564. // Early return on failure!
  565. if (!predrawnImage) {
  566. FLLogError(@"Failed to `imageWithCGImage:scale:orientation:` with image ref %@ created with color space %@ and bitmap context %@ and properties and properties (scale: %f orientation: %ld) for image %@", predrawnImageRef, colorSpaceDeviceRGBRef, bitmapContextRef, imageToPredraw.scale, (long)imageToPredraw.imageOrientation, imageToPredraw);
  567. return imageToPredraw;
  568. }
  569. return predrawnImage;
  570. }
  571. #pragma mark - Description
  572. - (NSString *)description
  573. {
  574. NSString *description = [super description];
  575. description = [description stringByAppendingFormat:@" size=%@", NSStringFromCGSize(self.size)];
  576. description = [description stringByAppendingFormat:@" frameCount=%lu", (unsigned long)self.frameCount];
  577. return description;
  578. }
  579. @end
  580. #pragma mark - FLWeakProxy
  581. @interface FLWeakProxy ()
  582. @property (nonatomic, weak) id target;
  583. @end
  584. @implementation FLWeakProxy
  585. #pragma mark Life Cycle
  586. // This is the designated creation method of an `FLWeakProxy` and
  587. // as a subclass of `NSProxy` it doesn't respond to or need `-init`.
  588. + (instancetype)weakProxyForObject:(id)targetObject
  589. {
  590. FLWeakProxy *weakProxy = [FLWeakProxy alloc];
  591. weakProxy.target = targetObject;
  592. return weakProxy;
  593. }
  594. #pragma mark Forwarding Messages
  595. - (id)forwardingTargetForSelector:(SEL)selector
  596. {
  597. // Keep it lightweight: access the ivar directly
  598. return _target;
  599. }
  600. #pragma mark - NSWeakProxy Method Overrides
  601. #pragma mark Handling Unimplemented Methods
  602. - (void)forwardInvocation:(NSInvocation *)invocation
  603. {
  604. // Fallback for when target is nil. Don't do anything, just return 0/NULL/nil.
  605. // The method signature we've received to get here is just a dummy to keep `doesNotRecognizeSelector:` from firing.
  606. // We can't really handle struct return types here because we don't know the length.
  607. void *nullPointer = NULL;
  608. [invocation setReturnValue:&nullPointer];
  609. }
  610. - (NSMethodSignature *)methodSignatureForSelector:(SEL)selector
  611. {
  612. // We only get here if `forwardingTargetForSelector:` returns nil.
  613. // In that case, our weak target has been reclaimed. Return a dummy method signature to keep `doesNotRecognizeSelector:` from firing.
  614. // We'll emulate the Obj-c messaging nil behavior by setting the return value to nil in `forwardInvocation:`, but we'll assume that the return value is `sizeof(void *)`.
  615. // Other libraries handle this situation by making use of a global method signature cache, but that seems heavier than necessary and has issues as well.
  616. // See https://www.mikeash.com/pyblog/friday-qa-2010-02-26-futures.html and https://github.com/steipete/PSTDelegateProxy/issues/1 for examples of using a method signature cache.
  617. return [NSObject instanceMethodSignatureForSelector:@selector(init)];
  618. }
  619. @end