FLAnimatedImage.m 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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. // This is how the fastest browsers do it as per 2012: http://nullsleep.tumblr.com/post/16524517190/animated-gif-minimum-frame-delay-browser-compatibility
  17. const NSTimeInterval kFLAnimatedImageDelayTimeIntervalMinimum = 0.02;
  18. // An animated image's data size (dimensions * frameCount) category; its value is the max allowed memory (in MB).
  19. // E.g.: A 100x200px GIF with 30 frames is ~2.3MB in our pixel format and would fall into the `FLAnimatedImageDataSizeCategoryAll` category.
  20. typedef NS_ENUM(NSUInteger, FLAnimatedImageDataSizeCategory) {
  21. FLAnimatedImageDataSizeCategoryAll = 10, // All frames permanently in memory (be nice to the CPU)
  22. FLAnimatedImageDataSizeCategoryDefault = 75, // A frame cache of default size in memory (usually real-time performance and keeping low memory profile)
  23. FLAnimatedImageDataSizeCategoryOnDemand = 250, // Only keep one frame at the time in memory (easier on memory, slowest performance)
  24. FLAnimatedImageDataSizeCategoryUnsupported // Even for one frame too large, computer says no.
  25. };
  26. typedef NS_ENUM(NSUInteger, FLAnimatedImageFrameCacheSize) {
  27. FLAnimatedImageFrameCacheSizeNoLimit = 0, // 0 means no specific limit
  28. FLAnimatedImageFrameCacheSizeLowMemory = 1, // The minimum frame cache size; this will produce frames on-demand.
  29. FLAnimatedImageFrameCacheSizeGrowAfterMemoryWarning = 2, // If we can produce the frames faster than we consume, one frame ahead will already result in a stutter-free playback.
  30. FLAnimatedImageFrameCacheSizeDefault = 5 // Build up a comfy buffer window to cope with CPU hiccups etc.
  31. };
  32. #if defined(DEBUG) && DEBUG
  33. @protocol FLAnimatedImageDebugDelegate <NSObject>
  34. @optional
  35. - (void)debug_animatedImage:(FLAnimatedImage *)animatedImage didUpdateCachedFrames:(NSIndexSet *)indexesOfFramesInCache;
  36. - (void)debug_animatedImage:(FLAnimatedImage *)animatedImage didRequestCachedFrame:(NSUInteger)index;
  37. - (CGFloat)debug_animatedImagePredrawingSlowdownFactor:(FLAnimatedImage *)animatedImage;
  38. @end
  39. #endif
  40. @interface FLAnimatedImage ()
  41. @property (nonatomic, assign, readonly) NSUInteger frameCacheSizeOptimal; // The optimal number of frames to cache based on image size & number of frames; never changes
  42. @property (nonatomic, assign, readonly, getter=isPredrawingEnabled) BOOL predrawingEnabled; // Enables predrawing of images to improve performance.
  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. #if defined(DEBUG) && DEBUG
  58. @property (nonatomic, weak) id<FLAnimatedImageDebugDelegate> debug_delegate;
  59. #endif
  60. @end
  61. // For custom dispatching of memory warnings to avoid deallocation races since NSNotificationCenter doesn't retain objects it is notifying.
  62. static NSHashTable *allAnimatedImagesWeak;
  63. @implementation FLAnimatedImage
  64. #pragma mark - Accessors
  65. #pragma mark Public
  66. // This is the definite value the frame cache needs to size itself to.
  67. - (NSUInteger)frameCacheSizeCurrent
  68. {
  69. NSUInteger frameCacheSizeCurrent = self.frameCacheSizeOptimal;
  70. // If set, respect the caps.
  71. if (self.frameCacheSizeMax > FLAnimatedImageFrameCacheSizeNoLimit) {
  72. frameCacheSizeCurrent = MIN(frameCacheSizeCurrent, self.frameCacheSizeMax);
  73. }
  74. if (self.frameCacheSizeMaxInternal > FLAnimatedImageFrameCacheSizeNoLimit) {
  75. frameCacheSizeCurrent = MIN(frameCacheSizeCurrent, self.frameCacheSizeMaxInternal);
  76. }
  77. return frameCacheSizeCurrent;
  78. }
  79. - (void)setFrameCacheSizeMax:(NSUInteger)frameCacheSizeMax
  80. {
  81. if (_frameCacheSizeMax != frameCacheSizeMax) {
  82. // 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.
  83. BOOL willFrameCacheSizeShrink = (frameCacheSizeMax < self.frameCacheSizeCurrent);
  84. // Update the value
  85. _frameCacheSizeMax = frameCacheSizeMax;
  86. if (willFrameCacheSizeShrink) {
  87. [self purgeFrameCacheIfNeeded];
  88. }
  89. }
  90. }
  91. #pragma mark Private
  92. - (void)setFrameCacheSizeMaxInternal:(NSUInteger)frameCacheSizeMaxInternal
  93. {
  94. if (_frameCacheSizeMaxInternal != frameCacheSizeMaxInternal) {
  95. // 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.
  96. BOOL willFrameCacheSizeShrink = (frameCacheSizeMaxInternal < self.frameCacheSizeCurrent);
  97. // Update the value
  98. _frameCacheSizeMaxInternal = frameCacheSizeMaxInternal;
  99. if (willFrameCacheSizeShrink) {
  100. [self purgeFrameCacheIfNeeded];
  101. }
  102. }
  103. }
  104. #pragma mark - Life Cycle
  105. + (void)initialize
  106. {
  107. if (self == [FLAnimatedImage class]) {
  108. // UIKit memory warning notification handler shared by all of the instances
  109. allAnimatedImagesWeak = [NSHashTable weakObjectsHashTable];
  110. [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidReceiveMemoryWarningNotification object:nil queue:nil usingBlock:^(NSNotification *note) {
  111. // UIKit notifications are posted on the main thread. didReceiveMemoryWarning: is expecting the main run loop, and we don't lock on allAnimatedImagesWeak
  112. NSAssert([NSThread isMainThread], @"Received memory warning on non-main thread");
  113. // 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.
  114. // Note that FLAnimatedImages can be created on any thread, so the hash table must be locked.
  115. NSArray *images = nil;
  116. @synchronized(allAnimatedImagesWeak) {
  117. images = [[allAnimatedImagesWeak allObjects] copy];
  118. }
  119. // Now issue notifications to all of the images while holding a strong reference to them
  120. [images makeObjectsPerformSelector:@selector(didReceiveMemoryWarning:) withObject:note];
  121. }];
  122. }
  123. }
  124. - (instancetype)init
  125. {
  126. FLAnimatedImage *animatedImage = [self initWithAnimatedGIFData:nil];
  127. if (!animatedImage) {
  128. FLLog(FLLogLevelError, @"Use `-initWithAnimatedGIFData:` and supply the animated GIF data as an argument to initialize an object of type `FLAnimatedImage`.");
  129. }
  130. return animatedImage;
  131. }
  132. - (instancetype)initWithAnimatedGIFData:(NSData *)data
  133. {
  134. return [self initWithAnimatedGIFData:data optimalFrameCacheSize:0 predrawingEnabled:YES];
  135. }
  136. - (instancetype)initWithAnimatedGIFData:(NSData *)data optimalFrameCacheSize:(NSUInteger)optimalFrameCacheSize predrawingEnabled:(BOOL)isPredrawingEnabled
  137. {
  138. // Early return if no data supplied!
  139. BOOL hasData = ([data length] > 0);
  140. if (!hasData) {
  141. FLLog(FLLogLevelError, @"No animated GIF data supplied.");
  142. return nil;
  143. }
  144. self = [super init];
  145. if (self) {
  146. // Do one-time initializations of `readonly` properties directly to ivar to prevent implicit actions and avoid need for private `readwrite` property overrides.
  147. // Keep a strong reference to `data` and expose it read-only publicly.
  148. // However, we will use the `_imageSource` as handler to the image data throughout our life cycle.
  149. _data = data;
  150. _predrawingEnabled = isPredrawingEnabled;
  151. // Initialize internal data structures
  152. _cachedFramesForIndexes = [[NSMutableDictionary alloc] init];
  153. _cachedFrameIndexes = [[NSMutableIndexSet alloc] init];
  154. _requestedFrameIndexes = [[NSMutableIndexSet alloc] init];
  155. // Note: We could leverage `CGImageSourceCreateWithURL` too to add a second initializer `-initWithAnimatedGIFContentsOfURL:`.
  156. _imageSource = CGImageSourceCreateWithData((__bridge CFDataRef)data,
  157. (__bridge CFDictionaryRef)@{(NSString *)kCGImageSourceShouldCache: @YES});
  158. // Early return on failure!
  159. if (!_imageSource) {
  160. FLLog(FLLogLevelError, @"Failed to `CGImageSourceCreateWithData` for animated GIF data %@", data);
  161. return nil;
  162. }
  163. // Early return if not GIF!
  164. CFStringRef imageSourceContainerType = CGImageSourceGetType(_imageSource);
  165. BOOL isGIFData = UTTypeConformsTo(imageSourceContainerType, kUTTypeGIF);
  166. if (!isGIFData) {
  167. FLLog(FLLogLevelError, @"Supplied data is of type %@ and doesn't seem to be GIF data %@", imageSourceContainerType, data);
  168. return nil;
  169. }
  170. // Get `LoopCount`
  171. // Note: 0 means repeating the animation indefinitely.
  172. // Image properties example:
  173. // {
  174. // FileSize = 314446;
  175. // "{GIF}" = {
  176. // HasGlobalColorMap = 1;
  177. // LoopCount = 0;
  178. // };
  179. // }
  180. NSDictionary *imageProperties = (__bridge_transfer NSDictionary *)CGImageSourceCopyProperties(_imageSource, NULL);
  181. _loopCount = [[[imageProperties objectForKey:(id)kCGImagePropertyGIFDictionary] objectForKey:(id)kCGImagePropertyGIFLoopCount] unsignedIntegerValue];
  182. // Iterate through frame images
  183. size_t imageCount = CGImageSourceGetCount(_imageSource);
  184. NSUInteger skippedFrameCount = 0;
  185. NSMutableDictionary *delayTimesForIndexesMutable = [NSMutableDictionary dictionaryWithCapacity:imageCount];
  186. for (size_t i = 0; i < imageCount; i++) {
  187. @autoreleasepool {
  188. CGImageRef frameImageRef = CGImageSourceCreateImageAtIndex(_imageSource, i, NULL);
  189. if (frameImageRef) {
  190. UIImage *frameImage = [UIImage imageWithCGImage:frameImageRef];
  191. // Check for valid `frameImage` before parsing its properties as frames can be corrupted (and `frameImage` even `nil` when `frameImageRef` was valid).
  192. if (frameImage) {
  193. // Set poster image
  194. if (!self.posterImage) {
  195. _posterImage = frameImage;
  196. // Set its size to proxy our size.
  197. _size = _posterImage.size;
  198. // Remember index of poster image so we never purge it; also add it to the cache.
  199. _posterImageFrameIndex = i;
  200. [self.cachedFramesForIndexes setObject:self.posterImage forKey:@(self.posterImageFrameIndex)];
  201. [self.cachedFrameIndexes addIndex:self.posterImageFrameIndex];
  202. }
  203. // Get `DelayTime`
  204. // 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`.
  205. // Frame properties example:
  206. // {
  207. // ColorModel = RGB;
  208. // Depth = 8;
  209. // PixelHeight = 960;
  210. // PixelWidth = 640;
  211. // "{GIF}" = {
  212. // DelayTime = "0.4";
  213. // UnclampedDelayTime = "0.4";
  214. // };
  215. // }
  216. NSDictionary *frameProperties = (__bridge_transfer NSDictionary *)CGImageSourceCopyPropertiesAtIndex(_imageSource, i, NULL);
  217. NSDictionary *framePropertiesGIF = [frameProperties objectForKey:(id)kCGImagePropertyGIFDictionary];
  218. // Try to use the unclamped delay time; fall back to the normal delay time.
  219. NSNumber *delayTime = [framePropertiesGIF objectForKey:(id)kCGImagePropertyGIFUnclampedDelayTime];
  220. if (!delayTime) {
  221. delayTime = [framePropertiesGIF objectForKey:(id)kCGImagePropertyGIFDelayTime];
  222. }
  223. // If we don't get a delay time from the properties, fall back to `kDelayTimeIntervalDefault` or carry over the preceding frame's value.
  224. const NSTimeInterval kDelayTimeIntervalDefault = 0.1;
  225. if (!delayTime) {
  226. if (i == 0) {
  227. FLLog(FLLogLevelInfo, @"Falling back to default delay time for first frame %@ because none found in GIF properties %@", frameImage, frameProperties);
  228. delayTime = @(kDelayTimeIntervalDefault);
  229. } else {
  230. FLLog(FLLogLevelInfo, @"Falling back to preceding delay time for frame %zu %@ because none found in GIF properties %@", i, frameImage, frameProperties);
  231. delayTime = delayTimesForIndexesMutable[@(i - 1)];
  232. }
  233. }
  234. // Support frame delays as low as `kFLAnimatedImageDelayTimeIntervalMinimum`, with anything below being rounded up to `kDelayTimeIntervalDefault` for legacy compatibility.
  235. // 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.
  236. if ([delayTime floatValue] < ((float)kFLAnimatedImageDelayTimeIntervalMinimum - FLT_EPSILON)) {
  237. FLLog(FLLogLevelInfo, @"Rounding frame %zu's `delayTime` from %f up to default %f (minimum supported: %f).", i, [delayTime floatValue], kDelayTimeIntervalDefault, kFLAnimatedImageDelayTimeIntervalMinimum);
  238. delayTime = @(kDelayTimeIntervalDefault);
  239. }
  240. delayTimesForIndexesMutable[@(i)] = delayTime;
  241. } else {
  242. skippedFrameCount++;
  243. FLLog(FLLogLevelInfo, @"Dropping frame %zu because valid `CGImageRef` %@ did result in `nil`-`UIImage`.", i, frameImageRef);
  244. }
  245. CFRelease(frameImageRef);
  246. } else {
  247. skippedFrameCount++;
  248. FLLog(FLLogLevelInfo, @"Dropping frame %zu because failed to `CGImageSourceCreateImageAtIndex` with image source %@", i, _imageSource);
  249. }
  250. }
  251. }
  252. _delayTimesForIndexes = [delayTimesForIndexesMutable copy];
  253. _frameCount = imageCount;
  254. if (self.frameCount == 0) {
  255. FLLog(FLLogLevelInfo, @"Failed to create any valid frames for GIF with properties %@", imageProperties);
  256. return nil;
  257. } else if (self.frameCount == 1) {
  258. // Warn when we only have a single frame but return a valid GIF.
  259. FLLog(FLLogLevelInfo, @"Created valid GIF but with only a single frame. Image properties: %@", imageProperties);
  260. } else {
  261. // We have multiple frames, rock on!
  262. }
  263. // If no value is provided, select a default based on the GIF.
  264. if (optimalFrameCacheSize == 0) {
  265. // Calculate the optimal frame cache size: try choosing a larger buffer window depending on the predicted image size.
  266. // It's only dependent on the image size & number of frames and never changes.
  267. CGFloat animatedImageDataSize = CGImageGetBytesPerRow(self.posterImage.CGImage) * self.size.height * (self.frameCount - skippedFrameCount) / MEGABYTE;
  268. if (animatedImageDataSize <= FLAnimatedImageDataSizeCategoryAll) {
  269. _frameCacheSizeOptimal = self.frameCount;
  270. } else if (animatedImageDataSize <= FLAnimatedImageDataSizeCategoryDefault) {
  271. // This value doesn't depend on device memory much because if we're not keeping all frames in memory we will always be decoding 1 frame up ahead per 1 frame that gets played and at this point we might as well just keep a small buffer just large enough to keep from running out of frames.
  272. _frameCacheSizeOptimal = FLAnimatedImageFrameCacheSizeDefault;
  273. } else {
  274. // The predicted size exceeds the limits to build up a cache and we go into low memory mode from the beginning.
  275. _frameCacheSizeOptimal = FLAnimatedImageFrameCacheSizeLowMemory;
  276. }
  277. } else {
  278. // Use the provided value.
  279. _frameCacheSizeOptimal = optimalFrameCacheSize;
  280. }
  281. // In any case, cap the optimal cache size at the frame count.
  282. _frameCacheSizeOptimal = MIN(_frameCacheSizeOptimal, self.frameCount);
  283. // Convenience/minor performance optimization; keep an index set handy with the full range to return in `-frameIndexesToCache`.
  284. _allFramesIndexSet = [[NSIndexSet alloc] initWithIndexesInRange:NSMakeRange(0, self.frameCount)];
  285. // See the property declarations for descriptions.
  286. _weakProxy = (id)[FLWeakProxy weakProxyForObject:self];
  287. // Register this instance in the weak table for memory notifications. The NSHashTable will clean up after itself when we're gone.
  288. // Note that FLAnimatedImages can be created on any thread, so the hash table must be locked.
  289. @synchronized(allAnimatedImagesWeak) {
  290. [allAnimatedImagesWeak addObject:self];
  291. }
  292. }
  293. return self;
  294. }
  295. + (instancetype)animatedImageWithGIFData:(NSData *)data
  296. {
  297. FLAnimatedImage *animatedImage = [[FLAnimatedImage alloc] initWithAnimatedGIFData:data];
  298. return animatedImage;
  299. }
  300. - (void)dealloc
  301. {
  302. if (_weakProxy) {
  303. [NSObject cancelPreviousPerformRequestsWithTarget:_weakProxy];
  304. }
  305. if (_imageSource) {
  306. CFRelease(_imageSource);
  307. }
  308. }
  309. #pragma mark - Public Methods
  310. // See header for more details.
  311. // Note: both consumer and producer are throttled: consumer by frame timings and producer by the available memory (max buffer window size).
  312. - (UIImage *)imageLazilyCachedAtIndex:(NSUInteger)index
  313. {
  314. // Early return if the requested index is beyond bounds.
  315. // Note: We're comparing an index with a count and need to bail on greater than or equal to.
  316. if (index >= self.frameCount) {
  317. FLLog(FLLogLevelWarn, @"Skipping requested frame %lu beyond bounds (total frame count: %lu) for animated image: %@", (unsigned long)index, (unsigned long)self.frameCount, self);
  318. return nil;
  319. }
  320. // Remember requested frame index, this influences what we should cache next.
  321. self.requestedFrameIndex = index;
  322. #if defined(DEBUG) && DEBUG
  323. if ([self.debug_delegate respondsToSelector:@selector(debug_animatedImage:didRequestCachedFrame:)]) {
  324. [self.debug_delegate debug_animatedImage:self didRequestCachedFrame:index];
  325. }
  326. #endif
  327. // Quick check to avoid doing any work if we already have all possible frames cached, a common case.
  328. if ([self.cachedFrameIndexes count] < self.frameCount) {
  329. // If we have frames that should be cached but aren't and aren't requested yet, request them.
  330. // Exclude existing cached frames, frames already requested, and specially cached poster image.
  331. NSMutableIndexSet *frameIndexesToAddToCacheMutable = [self frameIndexesToCache];
  332. [frameIndexesToAddToCacheMutable removeIndexes:self.cachedFrameIndexes];
  333. [frameIndexesToAddToCacheMutable removeIndexes:self.requestedFrameIndexes];
  334. [frameIndexesToAddToCacheMutable removeIndex:self.posterImageFrameIndex];
  335. NSIndexSet *frameIndexesToAddToCache = [frameIndexesToAddToCacheMutable copy];
  336. // Asynchronously add frames to our cache.
  337. if ([frameIndexesToAddToCache count] > 0) {
  338. [self addFrameIndexesToCache:frameIndexesToAddToCache];
  339. }
  340. }
  341. // Get the specified image.
  342. UIImage *image = self.cachedFramesForIndexes[@(index)];
  343. // Purge if needed based on the current playhead position.
  344. [self purgeFrameCacheIfNeeded];
  345. return image;
  346. }
  347. // Only called once from `-imageLazilyCachedAtIndex` but factored into its own method for logical grouping.
  348. - (void)addFrameIndexesToCache:(NSIndexSet *)frameIndexesToAddToCache
  349. {
  350. // Order matters. First, iterate over the indexes starting from the requested frame index.
  351. // Then, if there are any indexes before the requested frame index, do those.
  352. NSRange firstRange = NSMakeRange(self.requestedFrameIndex, self.frameCount - self.requestedFrameIndex);
  353. NSRange secondRange = NSMakeRange(0, self.requestedFrameIndex);
  354. if (firstRange.length + secondRange.length != self.frameCount) {
  355. FLLog(FLLogLevelWarn, @"Two-part frame cache range doesn't equal full range.");
  356. }
  357. // Add to the requested list before we actually kick them off, so they don't get into the queue twice.
  358. [self.requestedFrameIndexes addIndexes:frameIndexesToAddToCache];
  359. // Lazily create dedicated isolation queue.
  360. if (!self.serialQueue) {
  361. _serialQueue = dispatch_queue_create("com.flipboard.framecachingqueue", DISPATCH_QUEUE_SERIAL);
  362. }
  363. // Start streaming requested frames in the background into the cache.
  364. // Avoid capturing self in the block as there's no reason to keep doing work if the animated image went away.
  365. FLAnimatedImage * __weak weakSelf = self;
  366. dispatch_async(self.serialQueue, ^{
  367. // Produce and cache next needed frame.
  368. void (^frameRangeBlock)(NSRange, BOOL *) = ^(NSRange range, BOOL *stop) {
  369. // Iterate through contiguous indexes; can be faster than `enumerateIndexesInRange:options:usingBlock:`.
  370. for (NSUInteger i = range.location; i < NSMaxRange(range); i++) {
  371. #if defined(DEBUG) && DEBUG
  372. CFTimeInterval predrawBeginTime = CACurrentMediaTime();
  373. #endif
  374. UIImage *image = [weakSelf imageAtIndex:i];
  375. #if defined(DEBUG) && DEBUG
  376. CFTimeInterval predrawDuration = CACurrentMediaTime() - predrawBeginTime;
  377. CFTimeInterval slowdownDuration = 0.0;
  378. if ([self.debug_delegate respondsToSelector:@selector(debug_animatedImagePredrawingSlowdownFactor:)]) {
  379. CGFloat predrawingSlowdownFactor = [self.debug_delegate debug_animatedImagePredrawingSlowdownFactor:self];
  380. slowdownDuration = predrawDuration * predrawingSlowdownFactor - predrawDuration;
  381. [NSThread sleepForTimeInterval:slowdownDuration];
  382. }
  383. FLLog(FLLogLevelVerbose, @"Predrew frame %lu in %f ms for animated image: %@", (unsigned long)i, (predrawDuration + slowdownDuration) * 1000, self);
  384. #endif
  385. // The results get returned one by one as soon as they're ready (and not in batch).
  386. // 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.
  387. if (image && weakSelf) {
  388. dispatch_async(dispatch_get_main_queue(), ^{
  389. weakSelf.cachedFramesForIndexes[@(i)] = image;
  390. [weakSelf.cachedFrameIndexes addIndex:i];
  391. [weakSelf.requestedFrameIndexes removeIndex:i];
  392. #if defined(DEBUG) && DEBUG
  393. if ([weakSelf.debug_delegate respondsToSelector:@selector(debug_animatedImage:didUpdateCachedFrames:)]) {
  394. [weakSelf.debug_delegate debug_animatedImage:weakSelf didUpdateCachedFrames:weakSelf.cachedFrameIndexes];
  395. }
  396. #endif
  397. });
  398. }
  399. }
  400. };
  401. [frameIndexesToAddToCache enumerateRangesInRange:firstRange options:0 usingBlock:frameRangeBlock];
  402. [frameIndexesToAddToCache enumerateRangesInRange:secondRange options:0 usingBlock:frameRangeBlock];
  403. });
  404. }
  405. + (CGSize)sizeForImage:(id)image
  406. {
  407. CGSize imageSize = CGSizeZero;
  408. // Early return for nil
  409. if (!image) {
  410. return imageSize;
  411. }
  412. if ([image isKindOfClass:[UIImage class]]) {
  413. UIImage *uiImage = (UIImage *)image;
  414. imageSize = uiImage.size;
  415. } else if ([image isKindOfClass:[FLAnimatedImage class]]) {
  416. FLAnimatedImage *animatedImage = (FLAnimatedImage *)image;
  417. imageSize = animatedImage.size;
  418. } else {
  419. // Bear trap to capture bad images; we have seen crashers cropping up on iOS 7.
  420. FLLog(FLLogLevelError, @"`image` isn't of expected types `UIImage` or `FLAnimatedImage`: %@", image);
  421. }
  422. return imageSize;
  423. }
  424. #pragma mark - Private Methods
  425. #pragma mark Frame Loading
  426. - (UIImage *)imageAtIndex:(NSUInteger)index
  427. {
  428. // 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.
  429. CGImageRef imageRef = CGImageSourceCreateImageAtIndex(_imageSource, index, NULL);
  430. // Early return for nil
  431. if (!imageRef) {
  432. return nil;
  433. }
  434. UIImage *image = [UIImage imageWithCGImage:imageRef];
  435. CFRelease(imageRef);
  436. // 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.
  437. if (self.isPredrawingEnabled) {
  438. image = [[self class] predrawnImageFromImage:image];
  439. }
  440. return image;
  441. }
  442. #pragma mark Frame Caching
  443. - (NSMutableIndexSet *)frameIndexesToCache
  444. {
  445. NSMutableIndexSet *indexesToCache = nil;
  446. // Quick check to avoid building the index set if the number of frames to cache equals the total frame count.
  447. if (self.frameCacheSizeCurrent == self.frameCount) {
  448. indexesToCache = [self.allFramesIndexSet mutableCopy];
  449. } else {
  450. indexesToCache = [[NSMutableIndexSet alloc] init];
  451. // Add indexes to the set in two separate blocks- the first starting from the requested frame index, up to the limit or the end.
  452. // The second, if needed, the remaining number of frames beginning at index zero.
  453. NSUInteger firstLength = MIN(self.frameCacheSizeCurrent, self.frameCount - self.requestedFrameIndex);
  454. NSRange firstRange = NSMakeRange(self.requestedFrameIndex, firstLength);
  455. [indexesToCache addIndexesInRange:firstRange];
  456. NSUInteger secondLength = self.frameCacheSizeCurrent - firstLength;
  457. if (secondLength > 0) {
  458. NSRange secondRange = NSMakeRange(0, secondLength);
  459. [indexesToCache addIndexesInRange:secondRange];
  460. }
  461. // Double check our math, before we add the poster image index which may increase it by one.
  462. if ([indexesToCache count] != self.frameCacheSizeCurrent) {
  463. FLLog(FLLogLevelWarn, @"Number of frames to cache doesn't equal expected cache size.");
  464. }
  465. [indexesToCache addIndex:self.posterImageFrameIndex];
  466. }
  467. return indexesToCache;
  468. }
  469. - (void)purgeFrameCacheIfNeeded
  470. {
  471. // Purge frames that are currently cached but don't need to be.
  472. // But not if we're still under the number of frames to cache.
  473. // This way, if all frames are allowed to be cached (the common case), we can skip all the `NSIndexSet` math below.
  474. if ([self.cachedFrameIndexes count] > self.frameCacheSizeCurrent) {
  475. NSMutableIndexSet *indexesToPurge = [self.cachedFrameIndexes mutableCopy];
  476. [indexesToPurge removeIndexes:[self frameIndexesToCache]];
  477. [indexesToPurge enumerateRangesUsingBlock:^(NSRange range, BOOL *stop) {
  478. // Iterate through contiguous indexes; can be faster than `enumerateIndexesInRange:options:usingBlock:`.
  479. for (NSUInteger i = range.location; i < NSMaxRange(range); i++) {
  480. [self.cachedFrameIndexes removeIndex:i];
  481. [self.cachedFramesForIndexes removeObjectForKey:@(i)];
  482. // Note: Don't `CGImageSourceRemoveCacheAtIndex` on the image source for frames that we don't want cached any longer to maintain O(1) time access.
  483. #if defined(DEBUG) && DEBUG
  484. if ([self.debug_delegate respondsToSelector:@selector(debug_animatedImage:didUpdateCachedFrames:)]) {
  485. dispatch_async(dispatch_get_main_queue(), ^{
  486. [self.debug_delegate debug_animatedImage:self didUpdateCachedFrames:self.cachedFrameIndexes];
  487. });
  488. }
  489. #endif
  490. }
  491. }];
  492. }
  493. }
  494. - (void)growFrameCacheSizeAfterMemoryWarning:(NSNumber *)frameCacheSize
  495. {
  496. self.frameCacheSizeMaxInternal = [frameCacheSize unsignedIntegerValue];
  497. FLLog(FLLogLevelDebug, @"Grew frame cache size max to %lu after memory warning for animated image: %@", (unsigned long)self.frameCacheSizeMaxInternal, self);
  498. // Schedule resetting the frame cache size max completely after a while.
  499. const NSTimeInterval kResetDelay = 3.0;
  500. [self.weakProxy performSelector:@selector(resetFrameCacheSizeMaxInternal) withObject:nil afterDelay:kResetDelay];
  501. }
  502. - (void)resetFrameCacheSizeMaxInternal
  503. {
  504. self.frameCacheSizeMaxInternal = FLAnimatedImageFrameCacheSizeNoLimit;
  505. FLLog(FLLogLevelDebug, @"Reset frame cache size max (current frame cache size: %lu) for animated image: %@", (unsigned long)self.frameCacheSizeCurrent, self);
  506. }
  507. #pragma mark System Memory Warnings Notification Handler
  508. - (void)didReceiveMemoryWarning:(NSNotification *)notification
  509. {
  510. self.memoryWarningCount++;
  511. // If we were about to grow larger, but got rapped on our knuckles by the system again, cancel.
  512. [NSObject cancelPreviousPerformRequestsWithTarget:self.weakProxy selector:@selector(growFrameCacheSizeAfterMemoryWarning:) object:@(FLAnimatedImageFrameCacheSizeGrowAfterMemoryWarning)];
  513. [NSObject cancelPreviousPerformRequestsWithTarget:self.weakProxy selector:@selector(resetFrameCacheSizeMaxInternal) object:nil];
  514. // 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.
  515. FLLog(FLLogLevelDebug, @"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);
  516. self.frameCacheSizeMaxInternal = FLAnimatedImageFrameCacheSizeLowMemory;
  517. // 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.
  518. //
  519. // [mem]^ (2) (5) (6) 1) Loading frames for the first time
  520. // (*)| , , , 2) Mem warning #1; purge cache
  521. // | /| (4)/| /| 3) Grow cache size a bit after a while, if no mem warning occurs
  522. // | / | _/ | _/ | 4) Try to grow cache size back to optimum after a while, if no mem warning occurs
  523. // |(1)/ |_/ |/ |__(7) 5) Mem warning #2; purge cache
  524. // |__/ (3) 6) After repetition of (3) and (4), mem warning #3; purge cache
  525. // +----------------------> 7) After 3 mem warnings, stay at minimum cache size
  526. // [t]
  527. // *) The mem high water mark before we get warned might change for every cycle.
  528. //
  529. const NSUInteger kGrowAttemptsMax = 2;
  530. const NSTimeInterval kGrowDelay = 2.0;
  531. if ((self.memoryWarningCount - 1) <= kGrowAttemptsMax) {
  532. [self.weakProxy performSelector:@selector(growFrameCacheSizeAfterMemoryWarning:) withObject:@(FLAnimatedImageFrameCacheSizeGrowAfterMemoryWarning) afterDelay:kGrowDelay];
  533. }
  534. // 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
  535. }
  536. #pragma mark Image Decoding
  537. // 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.
  538. // On success, the returned object is a new `UIImage` instance with the same content as the one passed in.
  539. // 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.
  540. // First inspired by & good Karma to: https://gist.github.com/steipete/1144242
  541. + (UIImage *)predrawnImageFromImage:(UIImage *)imageToPredraw
  542. {
  543. // Always use a device RGB color space for simplicity and predictability what will be going on.
  544. CGColorSpaceRef colorSpaceDeviceRGBRef = CGColorSpaceCreateDeviceRGB();
  545. // Early return on failure!
  546. if (!colorSpaceDeviceRGBRef) {
  547. FLLog(FLLogLevelError, @"Failed to `CGColorSpaceCreateDeviceRGB` for image %@", imageToPredraw);
  548. return imageToPredraw;
  549. }
  550. // 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:
  551. // kCGImageAlphaNoneSkipFirst, kCGImageAlphaNoneSkipLast, kCGImageAlphaPremultipliedFirst, kCGImageAlphaPremultipliedLast
  552. // (source: docs "Quartz 2D Programming Guide > Graphics Contexts > Table 2-1 Pixel formats supported for bitmap graphics contexts")
  553. size_t numberOfComponents = CGColorSpaceGetNumberOfComponents(colorSpaceDeviceRGBRef) + 1; // 4: RGB + A
  554. // "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)
  555. void *data = NULL;
  556. size_t width = imageToPredraw.size.width;
  557. size_t height = imageToPredraw.size.height;
  558. size_t bitsPerComponent = CHAR_BIT;
  559. size_t bitsPerPixel = (bitsPerComponent * numberOfComponents);
  560. size_t bytesPerPixel = (bitsPerPixel / BYTE_SIZE);
  561. size_t bytesPerRow = (bytesPerPixel * width);
  562. CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
  563. CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageToPredraw.CGImage);
  564. // If the alpha info doesn't match to one of the supported formats (see above), pick a reasonable supported one.
  565. // "For bitmaps created in iOS 3.2 and later, the drawing environment uses the premultiplied ARGB format to store the bitmap data." (source: docs)
  566. if (alphaInfo == kCGImageAlphaNone || alphaInfo == kCGImageAlphaOnly) {
  567. alphaInfo = kCGImageAlphaNoneSkipFirst;
  568. } else if (alphaInfo == kCGImageAlphaFirst) {
  569. alphaInfo = kCGImageAlphaPremultipliedFirst;
  570. } else if (alphaInfo == kCGImageAlphaLast) {
  571. alphaInfo = kCGImageAlphaPremultipliedLast;
  572. }
  573. // "The constants for specifying the alpha channel information are declared with the `CGImageAlphaInfo` type but can be passed to this parameter safely." (source: docs)
  574. bitmapInfo |= alphaInfo;
  575. // 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).
  576. // 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.
  577. CGContextRef bitmapContextRef = CGBitmapContextCreate(data, width, height, bitsPerComponent, bytesPerRow, colorSpaceDeviceRGBRef, bitmapInfo);
  578. CGColorSpaceRelease(colorSpaceDeviceRGBRef);
  579. // Early return on failure!
  580. if (!bitmapContextRef) {
  581. FLLog(FLLogLevelError, @"Failed to `CGBitmapContextCreate` with color space %@ and parameters (width: %zu height: %zu bitsPerComponent: %zu bytesPerRow: %zu) for image %@", colorSpaceDeviceRGBRef, width, height, bitsPerComponent, bytesPerRow, imageToPredraw);
  582. return imageToPredraw;
  583. }
  584. // Draw image in bitmap context and create image by preserving receiver's properties.
  585. CGContextDrawImage(bitmapContextRef, CGRectMake(0.0, 0.0, imageToPredraw.size.width, imageToPredraw.size.height), imageToPredraw.CGImage);
  586. CGImageRef predrawnImageRef = CGBitmapContextCreateImage(bitmapContextRef);
  587. UIImage *predrawnImage = [UIImage imageWithCGImage:predrawnImageRef scale:imageToPredraw.scale orientation:imageToPredraw.imageOrientation];
  588. CGImageRelease(predrawnImageRef);
  589. CGContextRelease(bitmapContextRef);
  590. // Early return on failure!
  591. if (!predrawnImage) {
  592. FLLog(FLLogLevelError, @"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);
  593. return imageToPredraw;
  594. }
  595. return predrawnImage;
  596. }
  597. #pragma mark - Description
  598. - (NSString *)description
  599. {
  600. NSString *description = [super description];
  601. description = [description stringByAppendingFormat:@" size=%@", NSStringFromCGSize(self.size)];
  602. description = [description stringByAppendingFormat:@" frameCount=%lu", (unsigned long)self.frameCount];
  603. return description;
  604. }
  605. @end
  606. #pragma mark - Logging
  607. @implementation FLAnimatedImage (Logging)
  608. static void (^_logBlock)(NSString *logString, FLLogLevel logLevel) = nil;
  609. static FLLogLevel _logLevel;
  610. + (void)setLogBlock:(void (^)(NSString *logString, FLLogLevel logLevel))logBlock logLevel:(FLLogLevel)logLevel
  611. {
  612. _logBlock = logBlock;
  613. _logLevel = logLevel;
  614. }
  615. + (void)logStringFromBlock:(NSString *(^)(void))stringBlock withLevel:(FLLogLevel)level
  616. {
  617. if (level <= _logLevel && _logBlock && stringBlock) {
  618. _logBlock(stringBlock(), level);
  619. }
  620. }
  621. @end
  622. #pragma mark - FLWeakProxy
  623. @interface FLWeakProxy ()
  624. @property (nonatomic, weak) id target;
  625. @end
  626. @implementation FLWeakProxy
  627. #pragma mark Life Cycle
  628. // This is the designated creation method of an `FLWeakProxy` and
  629. // as a subclass of `NSProxy` it doesn't respond to or need `-init`.
  630. + (instancetype)weakProxyForObject:(id)targetObject
  631. {
  632. FLWeakProxy *weakProxy = [FLWeakProxy alloc];
  633. weakProxy.target = targetObject;
  634. return weakProxy;
  635. }
  636. #pragma mark Forwarding Messages
  637. - (id)forwardingTargetForSelector:(SEL)selector
  638. {
  639. // Keep it lightweight: access the ivar directly
  640. return _target;
  641. }
  642. #pragma mark - NSWeakProxy Method Overrides
  643. #pragma mark Handling Unimplemented Methods
  644. - (void)forwardInvocation:(NSInvocation *)invocation
  645. {
  646. // Fallback for when target is nil. Don't do anything, just return 0/NULL/nil.
  647. // The method signature we've received to get here is just a dummy to keep `doesNotRecognizeSelector:` from firing.
  648. // We can't really handle struct return types here because we don't know the length.
  649. void *nullPointer = NULL;
  650. [invocation setReturnValue:&nullPointer];
  651. }
  652. - (NSMethodSignature *)methodSignatureForSelector:(SEL)selector
  653. {
  654. // We only get here if `forwardingTargetForSelector:` returns nil.
  655. // In that case, our weak target has been reclaimed. Return a dummy method signature to keep `doesNotRecognizeSelector:` from firing.
  656. // 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 *)`.
  657. // 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.
  658. // 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.
  659. return [NSObject instanceMethodSignatureForSelector:@selector(init)];
  660. }
  661. @end