Flutter macOS Embedder
FlutterBackBufferCache Class Reference

#import <FlutterSurfaceManager.h>

Inheritance diagram for FlutterBackBufferCache:

Instance Methods

(nullable FlutterSurface *) - removeSurfaceForSize:
 
(void) - returnSurfaces:
 
(NSUInteger) - count
 

Detailed Description

Cache of back buffers to prevent unnecessary IOSurface allocations.

Definition at line 83 of file FlutterSurfaceManager.h.

Method Documentation

◆ count

- (NSUInteger) count

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

Definition at line 271 of file FlutterSurfaceManager.mm.

346  {
347  @synchronized(self) {
348  return _surfaces.count;
349  }
350 }

Referenced by flutter::testing::TEST().

◆ removeSurfaceForSize:

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

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

Definition at line 271 of file FlutterSurfaceManager.mm.

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

◆ returnSurfaces:

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

Removes all cached surfaces replacing them with new ones.

Definition at line 271 of file FlutterSurfaceManager.mm.

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

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