|
7 | 7 | */
|
8 | 8 |
|
9 | 9 | #import "SDImageWebPCoder.h"
|
| 10 | +#import "SDWebImageWebPCoderDefine.h" |
| 11 | +#import <Accelerate/Accelerate.h> |
| 12 | +#import <os/lock.h> |
| 13 | +#import <libkern/OSAtomic.h> |
10 | 14 |
|
11 | 15 | #if __has_include("webp/decode.h") && __has_include("webp/encode.h") && __has_include("webp/demux.h") && __has_include("webp/mux.h")
|
12 | 16 | #import "webp/decode.h"
|
|
22 | 26 | @import libwebp;
|
23 | 27 | #endif
|
24 | 28 |
|
25 |
| -#import <Accelerate/Accelerate.h> |
26 |
| -#import "SDWebImageWebPCoderDefine.h" |
| 29 | +#define SD_USE_OS_UNFAIR_LOCK TARGET_OS_MACCATALYST ||\ |
| 30 | + (__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_10_0) ||\ |
| 31 | + (__MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_12) ||\ |
| 32 | + (__TV_OS_VERSION_MIN_REQUIRED >= __TVOS_10_0) ||\ |
| 33 | + (__WATCH_OS_VERSION_MIN_REQUIRED >= __WATCHOS_3_0) |
| 34 | + |
| 35 | +#ifndef SD_LOCK_DECLARE |
| 36 | +#if SD_USE_OS_UNFAIR_LOCK |
| 37 | +#define SD_LOCK_DECLARE(lock) os_unfair_lock lock |
| 38 | +#else |
| 39 | +#define SD_LOCK_DECLARE(lock) os_unfair_lock lock API_AVAILABLE(ios(10.0), tvos(10), watchos(3), macos(10.12)); \ |
| 40 | +OSSpinLock lock##_deprecated; |
| 41 | +#endif |
| 42 | +#endif |
| 43 | + |
| 44 | +#ifndef SD_LOCK_INIT |
| 45 | +#if SD_USE_OS_UNFAIR_LOCK |
| 46 | +#define SD_LOCK_INIT(lock) lock = OS_UNFAIR_LOCK_INIT |
| 47 | +#else |
| 48 | +#define SD_LOCK_INIT(lock) if (@available(iOS 10, tvOS 10, watchOS 3, macOS 10.12, *)) lock = OS_UNFAIR_LOCK_INIT; \ |
| 49 | +else lock##_deprecated = OS_SPINLOCK_INIT; |
| 50 | +#endif |
| 51 | +#endif |
| 52 | + |
| 53 | +#ifndef SD_LOCK |
| 54 | +#if SD_USE_OS_UNFAIR_LOCK |
| 55 | +#define SD_LOCK(lock) os_unfair_lock_lock(&lock) |
| 56 | +#else |
| 57 | +#define SD_LOCK(lock) if (@available(iOS 10, tvOS 10, watchOS 3, macOS 10.12, *)) os_unfair_lock_lock(&lock); \ |
| 58 | +else OSSpinLockLock(&lock##_deprecated); |
| 59 | +#endif |
| 60 | +#endif |
| 61 | + |
| 62 | +#ifndef SD_UNLOCK |
| 63 | +#if SD_USE_OS_UNFAIR_LOCK |
| 64 | +#define SD_UNLOCK(lock) os_unfair_lock_unlock(&lock) |
| 65 | +#else |
| 66 | +#define SD_UNLOCK(lock) if (@available(iOS 10, tvOS 10, watchOS 3, macOS 10.12, *)) os_unfair_lock_unlock(&lock); \ |
| 67 | +else OSSpinLockUnlock(&lock##_deprecated); |
| 68 | +#endif |
| 69 | +#endif |
27 | 70 |
|
28 | 71 | /// Calculate the actual thumnail pixel size
|
29 | 72 | static CGSize SDCalculateThumbnailSize(CGSize fullSize, BOOL preserveAspectRatio, CGSize thumbnailSize) {
|
@@ -99,7 +142,7 @@ @implementation SDImageWebPCoder {
|
99 | 142 | BOOL _finished;
|
100 | 143 | CGFloat _canvasWidth;
|
101 | 144 | CGFloat _canvasHeight;
|
102 |
| - dispatch_semaphore_t _lock; |
| 145 | + SD_LOCK_DECLARE(_lock); |
103 | 146 | NSUInteger _currentBlendIndex;
|
104 | 147 | BOOL _preserveAspectRatio;
|
105 | 148 | CGSize _thumbnailSize;
|
@@ -292,7 +335,7 @@ - (instancetype)initIncrementalWithOptions:(nullable SDImageCoderOptions *)optio
|
292 | 335 | }
|
293 | 336 | _preserveAspectRatio = preserveAspectRatio;
|
294 | 337 | _currentBlendIndex = NSNotFound;
|
295 |
| - _lock = dispatch_semaphore_create(1); |
| 338 | + SD_LOCK_INIT(_lock); |
296 | 339 | }
|
297 | 340 | return self;
|
298 | 341 | }
|
@@ -975,7 +1018,7 @@ - (instancetype)initWithAnimatedImageData:(NSData *)data options:(nullable SDIma
|
975 | 1018 | _demux = demuxer;
|
976 | 1019 | _imageData = data;
|
977 | 1020 | _currentBlendIndex = NSNotFound;
|
978 |
| - _lock = dispatch_semaphore_create(1); |
| 1021 | + SD_LOCK_INIT(_lock); |
979 | 1022 | }
|
980 | 1023 | return self;
|
981 | 1024 | }
|
|
0 commit comments