杨锴
2024-08-14 909e20941e45f8712c012db602034b47da0bfdb0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* This file is part of the SDWebImage package.
* (c) Olivier Poitrey <rs@dailymotion.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
 
#import "SDImageFramePool.h"
#import "SDInternalMacros.h"
#import "objc/runtime.h"
 
@interface SDImageFramePool ()
 
@property (class, readonly) NSMapTable *providerFramePoolMap;
 
@property (weak) id<SDAnimatedImageProvider> provider;
@property (atomic) NSUInteger registerCount;
 
@property (nonatomic, strong) NSMutableDictionary<NSNumber *, UIImage *> *frameBuffer;
@property (nonatomic, strong) NSOperationQueue *fetchQueue;
 
@end
 
// Lock to ensure atomic behavior
SD_LOCK_DECLARE_STATIC(_providerFramePoolMapLock);
 
@implementation SDImageFramePool
 
+ (NSMapTable *)providerFramePoolMap {
    static NSMapTable *providerFramePoolMap;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        providerFramePoolMap = [NSMapTable mapTableWithKeyOptions:NSPointerFunctionsStrongMemory | NSPointerFunctionsObjectPointerPersonality valueOptions:NSPointerFunctionsStrongMemory | NSPointerFunctionsObjectPointerPersonality];
    });
    return providerFramePoolMap;
}
 
#pragma mark - Life Cycle
- (instancetype)init {
    self = [super init];
    if (self) {
        _frameBuffer = [NSMutableDictionary dictionary];
        _fetchQueue = [[NSOperationQueue alloc] init];
        _fetchQueue.maxConcurrentOperationCount = 1;
        _fetchQueue.name = @"com.hackemist.SDImageFramePool.fetchQueue";
#if SD_UIKIT
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveMemoryWarning:) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
#endif
    }
    return self;
}
 
- (void)dealloc {
#if SD_UIKIT
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
#endif
}
 
- (void)didReceiveMemoryWarning:(NSNotification *)notification {
    [self removeAllFrames];
}
 
+ (void)initialize {
    // Lock to ensure atomic behavior
    SD_LOCK_INIT(_providerFramePoolMapLock);
}
 
+ (instancetype)registerProvider:(id<SDAnimatedImageProvider>)provider {
    // Lock to ensure atomic behavior
    SD_LOCK(_providerFramePoolMapLock);
    SDImageFramePool *framePool = [self.providerFramePoolMap objectForKey:provider];
    if (!framePool) {
        framePool = [[SDImageFramePool alloc] init];
        framePool.provider = provider;
        [self.providerFramePoolMap setObject:framePool forKey:provider];
    }
    framePool.registerCount += 1;
    SD_UNLOCK(_providerFramePoolMapLock);
    return framePool;
}
 
+ (void)unregisterProvider:(id<SDAnimatedImageProvider>)provider {
    // Lock to ensure atomic behavior
    SD_LOCK(_providerFramePoolMapLock);
    SDImageFramePool *framePool = [self.providerFramePoolMap objectForKey:provider];
    if (!framePool) {
        SD_UNLOCK(_providerFramePoolMapLock);
        return;
    }
    framePool.registerCount -= 1;
    if (framePool.registerCount == 0) {
        [self.providerFramePoolMap removeObjectForKey:provider];
    }
    SD_UNLOCK(_providerFramePoolMapLock);
}
 
- (void)prefetchFrameAtIndex:(NSUInteger)index {
    @synchronized (self) {
        NSUInteger frameCount = self.frameBuffer.count;
        if (frameCount > self.maxBufferCount) {
            // Remove the frame buffer if need
            // TODO, use LRU or better algorithm to detect which frames to clear
            self.frameBuffer[@(index - 1)] = nil;
            self.frameBuffer[@(index + 1)] = nil;
        }
    }
    
    if (self.fetchQueue.operationCount == 0) {
        // Prefetch next frame in background queue
        id<SDAnimatedImageProvider> animatedProvider = self.provider;
        @weakify(self);
        NSOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
            @strongify(self);
            if (!self) {
                return;
            }
            UIImage *frame = [animatedProvider animatedImageFrameAtIndex:index];
            
            [self setFrame:frame atIndex:index];
        }];
        [self.fetchQueue addOperation:operation];
    }
}
 
- (void)setMaxConcurrentCount:(NSUInteger)maxConcurrentCount {
    self.fetchQueue.maxConcurrentOperationCount = maxConcurrentCount;
}
 
- (NSUInteger)currentFrameCount {
    NSUInteger frameCount = 0;
    @synchronized (self) {
        frameCount = self.frameBuffer.count;
    }
    return frameCount;
}
 
- (void)setFrame:(UIImage *)frame atIndex:(NSUInteger)index {
    @synchronized (self) {
        self.frameBuffer[@(index)] = frame;
    }
}
 
- (UIImage *)frameAtIndex:(NSUInteger)index {
    UIImage *frame;
    @synchronized (self) {
        frame = self.frameBuffer[@(index)];
    }
    return frame;
}
 
- (void)removeFrameAtIndex:(NSUInteger)index {
    @synchronized (self) {
        self.frameBuffer[@(index)] = nil;
    }
}
 
- (void)removeAllFrames {
    @synchronized (self) {
        [self.frameBuffer removeAllObjects];
    }
}
 
@end