This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
[web] Reuse ImageElement(s) across frames #18437
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
71641a4
reuse images across frames
ferhatb 2775747
Merge remote-tracking branch 'upstream/master' into safariflicker
ferhatb 26c59d3
Merge remote-tracking branch 'upstream/master' into safariflicker
ferhatb af1ab48
Change implementation to CrossFrameCache at picture level
ferhatb 9406125
Add newline at end of test
ferhatb 4e9fc2c
Merge remote-tracking branch 'upstream/master' into safariflicker
ferhatb beebb44
Update licenses_flutter
ferhatb 8b37c34
remove unused local var
ferhatb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| // @dart = 2.6 | ||
| part of engine; | ||
|
|
||
| /// A monotonically increasing frame number being rendered. | ||
| /// | ||
| /// Used for debugging only. | ||
| int _debugFrameNumber = 1; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need this here? |
||
|
|
||
| List<FrameReference<dynamic>> _frameReferences = <FrameReference<dynamic>>[]; | ||
|
|
||
| /// A temporary reference to a value of type [V]. | ||
| /// | ||
| /// The value automatically gets set to null after the current frame is | ||
| /// rendered. | ||
| /// | ||
| /// It is useful to think of this as a weak reference that's scoped to a | ||
| /// single frame. | ||
| class FrameReference<V> { | ||
| /// Creates a frame reference to a value. | ||
| FrameReference([this.value]) { | ||
| _frameReferences.add(this); | ||
| } | ||
|
|
||
| /// The current value of this reference. | ||
| V value; | ||
| } | ||
|
|
||
| /// Cache where items cached before frame(N) is committed, can be reused in | ||
| /// frame(N+1) and are evicted if not. | ||
| /// | ||
| /// A typical use case is image elements. As images are created and added to | ||
| /// DOM when painting a scene they are cached and if possible reused on next | ||
| /// update. If the next update does not reuse the element, it is evicted. | ||
| /// | ||
| /// Maps are lazily allocated since many pictures don't contain cachable images | ||
| /// at all. | ||
| class CrossFrameCache<T> { | ||
| // Cached items in a scene. | ||
| Map<String, List<_CrossFrameCacheItem<T>>> _cache; | ||
|
|
||
| // Cached items that have been committed, ready for reuse on next frame. | ||
| Map<String, List<_CrossFrameCacheItem<T>>> _reusablePool; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would call this field |
||
|
|
||
| // Called when a scene or picture update is committed. | ||
| void commitFrame() { | ||
| // Evict unused items from prior frame. | ||
| if (_reusablePool != null) { | ||
| for (List<_CrossFrameCacheItem<T>> items in _reusablePool.values) { | ||
| for (_CrossFrameCacheItem<T> item in items) { | ||
| item.evict(); | ||
| } | ||
| } | ||
| } | ||
| // Move cached items to reusable pool. | ||
| _reusablePool = _cache; | ||
| _cache = null; | ||
| } | ||
|
|
||
| /// Caches an item for reuse on next update frame. | ||
| /// | ||
| /// Duplicate keys are allowed. For example the same image url may be used | ||
| /// to create multiple instances of [ImageElement] to be reused in the future. | ||
| void cache(String key, T value, [CrossFrameCacheEvictCallback<T> callback]) { | ||
| _addToCache(key, _CrossFrameCacheItem<T>(value, callback)); | ||
| } | ||
|
|
||
| void _addToCache(String key, _CrossFrameCacheItem<T> item) { | ||
| _cache ??= {}; | ||
| (_cache[key] ??= [])..add(item); | ||
| } | ||
|
|
||
| /// Given a key, consumes an item that has been cached in a prior frame. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should also talk about what it returns, and in particular when does it return |
||
| T reuse(String key) { | ||
| if (_reusablePool == null) { | ||
| return null; | ||
| } | ||
| List<_CrossFrameCacheItem<T>> items = _reusablePool[key]; | ||
| if (items == null || items.isEmpty) { | ||
| return null; | ||
| } | ||
| _CrossFrameCacheItem<T> item = items.removeAt(0); | ||
| _addToCache(key, item); | ||
| return item.value; | ||
| } | ||
| } | ||
|
|
||
| class _CrossFrameCacheItem<T> { | ||
| final T value; | ||
| final CrossFrameCacheEvictCallback<T> evictCallback; | ||
| _CrossFrameCacheItem(this.value, this.evictCallback); | ||
| void evict() { | ||
| if (evictCallback != null) { | ||
| evictCallback(value); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| typedef CrossFrameCacheEvictCallback<T> = void Function(T value); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| // @dart = 2.6 | ||
| import 'package:ui/src/engine.dart'; | ||
| import 'package:test/test.dart'; | ||
|
|
||
| void main() { | ||
| group('CrossFrameCache', () { | ||
| test('Reuse returns no object when cache empty', () { | ||
| final CrossFrameCache<TestItem> cache = CrossFrameCache(); | ||
| cache.commitFrame(); | ||
| TestItem requestedItem = cache.reuse('item1'); | ||
| expect(requestedItem, null); | ||
| }); | ||
|
|
||
| test('Reuses object across frames', () { | ||
| final CrossFrameCache<TestItem> cache = CrossFrameCache(); | ||
| final TestItem testItem1 = TestItem('item1'); | ||
| cache.cache(testItem1.label, testItem1); | ||
| cache.commitFrame(); | ||
| TestItem requestedItem = cache.reuse('item1'); | ||
| expect(requestedItem, testItem1); | ||
| requestedItem = cache.reuse('item1'); | ||
| expect(requestedItem, null); | ||
| }); | ||
|
|
||
| test('Reuses objects that have same key across frames', () { | ||
| final CrossFrameCache<TestItem> cache = CrossFrameCache(); | ||
| final TestItem testItem1 = TestItem('sameLabel'); | ||
| final TestItem testItem2 = TestItem('sameLabel'); | ||
| final TestItem testItemX = TestItem('X'); | ||
| cache.cache(testItem1.label, testItem1); | ||
| cache.cache(testItemX.label, testItemX); | ||
| cache.cache(testItem2.label, testItem2); | ||
| cache.commitFrame(); | ||
| TestItem requestedItem = cache.reuse('sameLabel'); | ||
| expect(requestedItem, testItem1); | ||
| requestedItem = cache.reuse('sameLabel'); | ||
| expect(requestedItem, testItem2); | ||
| requestedItem = cache.reuse('sameLabel'); | ||
| expect(requestedItem, null); | ||
| }); | ||
|
|
||
| test('Values don\'t survive beyond next frame', () { | ||
| final CrossFrameCache<TestItem> cache = CrossFrameCache(); | ||
| final TestItem testItem1 = TestItem('item1'); | ||
| cache.cache(testItem1.label, testItem1); | ||
| cache.commitFrame(); | ||
| cache.commitFrame(); | ||
| TestItem requestedItem = cache.reuse('item1'); | ||
| expect(requestedItem, null); | ||
| }); | ||
|
|
||
| test('Values are evicted when not reused', () { | ||
| final Set<TestItem> _evictedItems = {}; | ||
| final CrossFrameCache<TestItem> cache = CrossFrameCache(); | ||
| final TestItem testItem1 = TestItem('item1'); | ||
| final TestItem testItem2 = TestItem('item2'); | ||
| cache.cache(testItem1.label, testItem1, (TestItem item) {_evictedItems.add(item);}); | ||
| cache.cache(testItem2.label, testItem2, (TestItem item) {_evictedItems.add(item);}); | ||
| cache.commitFrame(); | ||
| expect(_evictedItems.length, 0); | ||
| cache.reuse('item2'); | ||
| cache.commitFrame(); | ||
| expect(_evictedItems.contains(testItem1), true); | ||
| expect(_evictedItems.contains(testItem2), false); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| class TestItem { | ||
| final String label; | ||
| TestItem(this.label); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: https://dart.dev/guides/language/effective-dart/design#dont-define-a-setter-without-a-corresponding-getter