Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Instance Methods | Protected Attributes | List of all members
FlutterBackBufferCache Class Reference

#include <FlutterSurfaceManager.h>

Inheritance diagram for FlutterBackBufferCache:

Instance Methods

(nullable FlutterSurface *) - removeSurfaceForSize:
 
(void) - returnSurfaces:
 
(NSUInteger) - count
 
(instancetype) - init [implementation]
 
(int- ageForSurface: [implementation]
 
(void) - setAge:forSurface: [implementation]
 
(void) - onIdle [implementation]
 
(void) - reschedule [implementation]
 
(void) - dealloc [implementation]
 

Protected Attributes

NSMutableArray< FlutterSurface * > * _surfaces
 
NSMapTable< FlutterSurface *, NSNumber * > * _surfaceAge
 

Detailed Description

Cache of back buffers to prevent unnecessary IOSurface allocations.

Definition at line 81 of file FlutterSurfaceManager.h.

Method Documentation

◆ ageForSurface:

- (int) ageForSurface: (FlutterSurface*)  surface
implementation

Definition at line 277 of file FlutterSurfaceManager.mm.

293 NSNumber* age = [_surfaceAge objectForKey:surface];
294 return age != nil ? age.intValue : 0;
295}
VkSurfaceKHR surface
Definition main.cc:49

◆ count

- (NSUInteger) count

Returns number of surfaces currently in cache. Used for tests.

Definition at line 277 of file FlutterSurfaceManager.mm.

352 {
353 @synchronized(self) {
354 return _surfaces.count;
355 }
356}
NSMutableArray< FlutterSurface * > * _surfaces

◆ dealloc

- (void) dealloc
implementation

Definition at line 277 of file FlutterSurfaceManager.mm.

369 {
370 [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(onIdle) object:nil];
371}

◆ init

- (instancetype) init
implementation

Definition at line 277 of file FlutterSurfaceManager.mm.

284 {
285 if (self = [super init]) {
286 self->_surfaces = [[NSMutableArray alloc] init];
287 self->_surfaceAge = [NSMapTable weakToStrongObjectsMapTable];
288 }
289 return self;
290}

◆ onIdle

- (void) onIdle
implementation

Definition at line 277 of file FlutterSurfaceManager.mm.

358 {
359 @synchronized(self) {
360 [_surfaces removeAllObjects];
361 }
362}

◆ removeSurfaceForSize:

- (nullable FlutterSurface *) removeSurfaceForSize: (CGSize)  size

Removes surface with given size from cache (if available) and returns it.

Definition at line 277 of file FlutterSurfaceManager.mm.

301 :(CGSize)size {
302 @synchronized(self) {
303 // Purge all cached surfaces if the size has changed.
304 if (_surfaces.firstObject != nil && !CGSizeEqualToSize(_surfaces.firstObject.size, size)) {
305 [_surfaces removeAllObjects];
306 }
307
308 FlutterSurface* res;
309
310 // Returns youngest surface that is not in use. Returning youngest surface ensures
311 // that the cache doesn't keep more surfaces than it needs to, as the unused surfaces
312 // kept in cache will have their age kept increasing until purged (inside [returnSurfaces:]).
313 for (FlutterSurface* surface in _surfaces) {
314 if (!surface.isInUse &&
315 (res == nil || [self ageForSurface:res] > [self ageForSurface:surface])) {
316 res = surface;
317 }
318 }
319 if (res != nil) {
320 [_surfaces removeObject:res];
321 }
322 return res;
323 }
324}

◆ reschedule

- (void) reschedule
implementation

Definition at line 277 of file FlutterSurfaceManager.mm.

364 {
365 [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(onIdle) object:nil];
366 [self performSelector:@selector(onIdle) withObject:nil afterDelay:kIdleDelay];
367}

◆ returnSurfaces:

- (void) returnSurfaces: (nonnull NSArray<FlutterSurface*>*)  surfaces

Removes all cached surfaces replacing them with new ones.

Definition at line 277 of file FlutterSurfaceManager.mm.

326 :(nonnull NSArray<FlutterSurface*>*)returnedSurfaces {
327 @synchronized(self) {
328 for (FlutterSurface* surface in returnedSurfaces) {
329 [self setAge:0 forSurface:surface];
330 }
331 for (FlutterSurface* surface in _surfaces) {
332 [self setAge:[self ageForSurface:surface] + 1 forSurface:surface];
333 }
334
335 [_surfaces addObjectsFromArray:returnedSurfaces];
336
337 // Purge all surface with age = kSurfaceEvictionAge. Reaching this age can mean two things:
338 // - Surface is still in use and we can't return it. This can happen in some edge
339 // cases where the compositor holds on to the surface for much longer than expected.
340 // - Surface is not in use but it hasn't been requested from the cache for a while.
341 // This means there are too many surfaces in the cache.
342 [_surfaces filterUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(FlutterSurface* surface,
343 NSDictionary* bindings) {
344 return [self ageForSurface:surface] < kSurfaceEvictionAge;
345 }]];
346 }
347
348 // performSelector:withObject:afterDelay needs to be performed on RunLoop thread
349 [self performSelectorOnMainThread:@selector(reschedule) withObject:nil waitUntilDone:NO];
350}

◆ setAge:forSurface:

- (void) setAge: (int age
forSurface: (FlutterSurface*)  surface 
implementation

Definition at line 277 of file FlutterSurfaceManager.mm.

297 :(int)age forSurface:(FlutterSurface*)surface {
298 [_surfaceAge setObject:@(age) forKey:surface];
299}
Type::kYUV Type::kRGBA() int(0.7 *637)
NSMapTable< FlutterSurface *, NSNumber * > * _surfaceAge

Member Data Documentation

◆ _surfaceAge

- (NSMapTable<FlutterSurface*, NSNumber*>*) _surfaceAge
protected

Provided by category FlutterBackBufferCache().

Definition at line 277 of file FlutterSurfaceManager.mm.

◆ _surfaces

- (NSMutableArray<FlutterSurface*>*) _surfaces
protected

Provided by category FlutterBackBufferCache().

Definition at line 276 of file FlutterSurfaceManager.mm.


The documentation for this class was generated from the following files: