@@ -19,8 +19,7 @@ import 'util.dart';
19
19
20
20
// Only supported in profile/release mode. Allows Flutter to use MSAA but
21
21
// removes the ability for disabling AA on Paint objects.
22
- const bool _kUsingMSAA =
23
- bool .fromEnvironment ('flutter.canvaskit.msaa' );
22
+ const bool _kUsingMSAA = bool .fromEnvironment ('flutter.canvaskit.msaa' );
24
23
25
24
typedef SubmitCallback = bool Function (SurfaceFrame , CkCanvas );
26
25
@@ -146,53 +145,60 @@ class Surface {
146
145
throw CanvasKitError ('Cannot create surfaces of empty size.' );
147
146
}
148
147
149
- // Check if the window is the same size as before, and if so, don't allocate
150
- // a new canvas as the previous canvas is big enough to fit everything.
151
- final ui.Size ? previousSurfaceSize = _currentSurfaceSize;
152
- if (! _forceNewContext &&
153
- previousSurfaceSize != null &&
154
- size.width == previousSurfaceSize.width &&
155
- size.height == previousSurfaceSize.height) {
156
- // The existing surface is still reusable.
157
- if (window.devicePixelRatio != _currentDevicePixelRatio) {
158
- _updateLogicalHtmlCanvasSize ();
159
- _translateCanvas ();
148
+ if (! _forceNewContext) {
149
+ // Check if the window is the same size as before, and if so, don't allocate
150
+ // a new canvas as the previous canvas is big enough to fit everything.
151
+ final ui.Size ? previousSurfaceSize = _currentSurfaceSize;
152
+ if (previousSurfaceSize != null &&
153
+ size.width == previousSurfaceSize.width &&
154
+ size.height == previousSurfaceSize.height) {
155
+ // The existing surface is still reusable.
156
+ if (window.devicePixelRatio != _currentDevicePixelRatio) {
157
+ _updateLogicalHtmlCanvasSize ();
158
+ _translateCanvas ();
159
+ }
160
+ return _surface! ;
160
161
}
161
- return _surface! ;
162
- }
163
162
164
- // If the current canvas size is smaller than the requested size then create
165
- // a new, larger, canvas. Then update the GR context so we can create a new
166
- // SkSurface.
167
- final ui.Size ? previousCanvasSize = _currentCanvasPhysicalSize;
168
- if (_forceNewContext ||
169
- previousCanvasSize == null ||
170
- size.width > previousCanvasSize.width ||
171
- size.height > previousCanvasSize.height) {
163
+ final ui.Size ? previousCanvasSize = _currentCanvasPhysicalSize;
172
164
// Initialize a new, larger, canvas. If the size is growing, then make the
173
165
// new canvas larger than required to avoid many canvas creations.
174
- final ui.Size newSize = previousCanvasSize == null ? size : size * 1.4 ;
166
+ if (previousCanvasSize != null &&
167
+ (size.width > previousCanvasSize.width ||
168
+ size.height > previousCanvasSize.height)) {
169
+ final ui.Size newSize = size * 1.4 ;
170
+ _surface? .dispose ();
171
+ _surface = null ;
172
+ htmlCanvas! .width = newSize.width;
173
+ htmlCanvas! .height = newSize.height;
174
+ _currentCanvasPhysicalSize = newSize;
175
+ _pixelWidth = newSize.width.ceil ();
176
+ _pixelHeight = newSize.height.ceil ();
177
+ _updateLogicalHtmlCanvasSize ();
178
+ }
179
+ }
175
180
176
- // If we have a surface, send a dummy command to its canvas to make its context
177
- // current or else disposing the context could fail below.
178
- _surface? .getCanvas ().clear (const ui.Color (0x00000000 ));
181
+ // Either a new context is being forced or we've never had one.
182
+ if (_forceNewContext || _currentCanvasPhysicalSize == null ) {
179
183
_surface? .dispose ();
180
184
_surface = null ;
181
185
_addedToScene = false ;
182
186
_grContext? .releaseResourcesAndAbandonContext ();
183
187
_grContext? .delete ();
184
188
_grContext = null ;
185
189
186
- _createNewCanvas (newSize );
187
- _currentCanvasPhysicalSize = newSize ;
190
+ _createNewCanvas (size );
191
+ _currentCanvasPhysicalSize = size ;
188
192
} else if (window.devicePixelRatio != _currentDevicePixelRatio) {
189
193
_updateLogicalHtmlCanvasSize ();
190
194
}
191
195
192
196
_currentDevicePixelRatio = window.devicePixelRatio;
193
197
_currentSurfaceSize = size;
194
198
_translateCanvas ();
195
- return _surface = _createNewSurface (size);
199
+ _surface? .dispose ();
200
+ _surface = _createNewSurface (size);
201
+ return _surface! ;
196
202
}
197
203
198
204
/// Sets the CSS size of the canvas so that canvas pixels are 1:1 with device
0 commit comments