@@ -19,8 +19,7 @@ import 'util.dart';
1919
2020// Only supported in profile/release mode. Allows Flutter to use MSAA but
2121// 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' );
2423
2524typedef SubmitCallback = bool Function (SurfaceFrame , CkCanvas );
2625
@@ -146,53 +145,60 @@ class Surface {
146145 throw CanvasKitError ('Cannot create surfaces of empty size.' );
147146 }
148147
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! ;
160161 }
161- return _surface! ;
162- }
163162
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;
172164 // Initialize a new, larger, canvas. If the size is growing, then make the
173165 // 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+ }
175180
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 ) {
179183 _surface? .dispose ();
180184 _surface = null ;
181185 _addedToScene = false ;
182186 _grContext? .releaseResourcesAndAbandonContext ();
183187 _grContext? .delete ();
184188 _grContext = null ;
185189
186- _createNewCanvas (newSize );
187- _currentCanvasPhysicalSize = newSize ;
190+ _createNewCanvas (size );
191+ _currentCanvasPhysicalSize = size ;
188192 } else if (window.devicePixelRatio != _currentDevicePixelRatio) {
189193 _updateLogicalHtmlCanvasSize ();
190194 }
191195
192196 _currentDevicePixelRatio = window.devicePixelRatio;
193197 _currentSurfaceSize = size;
194198 _translateCanvas ();
195- return _surface = _createNewSurface (size);
199+ _surface? .dispose ();
200+ _surface = _createNewSurface (size);
201+ return _surface! ;
196202 }
197203
198204 /// Sets the CSS size of the canvas so that canvas pixels are 1:1 with device
0 commit comments