Skip to content

Commit f008306

Browse files
committed
Text Renderers/Editors: if they make a texture snapshot, it only happens in render() to avoid multiple texture uploads in the same frame (which should slightly improve performance when text is changing frequently, such as scrolling a list)
1 parent af4c441 commit f008306

File tree

4 files changed

+112
-93
lines changed

4 files changed

+112
-93
lines changed

source/feathers/controls/text/StageTextTextEditor.as

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,11 @@ package feathers.controls.text
316316
*/
317317
protected var _needsNewTexture:Boolean = false;
318318

319+
/**
320+
* @private
321+
*/
322+
protected var _needsTextureUpdate:Boolean = false;
323+
319324
/**
320325
* @private
321326
*/
@@ -1225,7 +1230,7 @@ package feathers.controls.text
12251230
{
12261231
painter.excludeFromCache(this);
12271232
}
1228-
if(this.textSnapshot && this._updateSnapshotOnScaleChange)
1233+
if(this.textSnapshot !== null && this._updateSnapshotOnScaleChange)
12291234
{
12301235
var matrix:Matrix = Pool.getMatrix();
12311236
this.getTransformationMatrix(this.stage, matrix);
@@ -1234,15 +1239,36 @@ package feathers.controls.text
12341239
{
12351240
//the snapshot needs to be updated because the scale has
12361241
//changed since the last snapshot was taken.
1237-
this.invalidate(INVALIDATION_FLAG_SIZE);
1238-
this.validate();
1242+
this._needsTextureUpdate = true;
12391243
}
12401244
Pool.putMatrix(matrix);
12411245
}
1246+
if(this._needsTextureUpdate)
1247+
{
1248+
this._needsTextureUpdate = false;
1249+
var hasText:Boolean = this._text.length > 0;
1250+
if(hasText)
1251+
{
1252+
this.refreshSnapshot();
1253+
}
1254+
if(this.textSnapshot)
1255+
{
1256+
this.textSnapshot.visible = !this._stageTextHasFocus;
1257+
this.textSnapshot.alpha = hasText ? 1 : 0;
1258+
}
1259+
if(!this._stageTextHasFocus)
1260+
{
1261+
//hide the StageText after the snapshot is created
1262+
//native controls don't necessarily render at the same time
1263+
//as starling, and we don't want to see the text disappear
1264+
//for a moment
1265+
this.stageText.visible = false;
1266+
}
1267+
}
12421268

12431269
//we'll skip this if the text field isn't visible to avoid running
12441270
//that code every frame.
1245-
if(this.stageText && this.stageText.visible)
1271+
if(this.stageText !== null && this.stageText.visible)
12461272
{
12471273
this.refreshViewPortAndFontSize();
12481274
}
@@ -1612,17 +1638,7 @@ package feathers.controls.text
16121638

16131639
if(!this._stageTextHasFocus && (stateInvalid || stylesInvalid || dataInvalid || sizeInvalid || this._needsNewTexture))
16141640
{
1615-
var hasText:Boolean = this._text.length > 0;
1616-
if(hasText)
1617-
{
1618-
this.refreshSnapshot();
1619-
}
1620-
if(this.textSnapshot)
1621-
{
1622-
this.textSnapshot.visible = !this._stageTextHasFocus;
1623-
this.textSnapshot.alpha = hasText ? 1 : 0;
1624-
}
1625-
this.stageText.visible = false;
1641+
this._needsTextureUpdate = true;
16261642
}
16271643

16281644
this.doPendingActions();

source/feathers/controls/text/TextBlockTextRenderer.as

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,6 +1356,21 @@ package feathers.controls.text
13561356
*/
13571357
override public function render(painter:Painter):void
13581358
{
1359+
var starling:Starling = this.stage !== null ? this.stage.starling : Starling.current;
1360+
if(this.textSnapshot !== null && this._updateSnapshotOnScaleChange)
1361+
{
1362+
this.getTransformationMatrix(this.stage, HELPER_MATRIX);
1363+
var globalScaleX:Number = matrixToScaleX(HELPER_MATRIX);
1364+
var globalScaleY:Number = matrixToScaleY(HELPER_MATRIX);
1365+
if(globalScaleX != this._lastGlobalScaleX ||
1366+
globalScaleY != this._lastGlobalScaleY ||
1367+
starling.contentScaleFactor != this._lastGlobalContentScaleFactor)
1368+
{
1369+
//the snapshot needs to be updated because the scale has
1370+
//changed since the last snapshot was taken.
1371+
this._needsNewTexture = true;
1372+
}
1373+
}
13591374
if(this._needsUpdateSnapshot)
13601375
{
13611376
this._needsUpdateSnapshot = false;
@@ -1366,22 +1381,6 @@ package feathers.controls.text
13661381
}
13671382
if(this.textSnapshot !== null)
13681383
{
1369-
var starling:Starling = this.stage !== null ? this.stage.starling : Starling.current;
1370-
if(this._updateSnapshotOnScaleChange)
1371-
{
1372-
this.getTransformationMatrix(this.stage, HELPER_MATRIX);
1373-
var globalScaleX:Number = matrixToScaleX(HELPER_MATRIX);
1374-
var globalScaleY:Number = matrixToScaleY(HELPER_MATRIX);
1375-
if(globalScaleX != this._lastGlobalScaleX ||
1376-
globalScaleY != this._lastGlobalScaleY ||
1377-
starling.contentScaleFactor != this._lastGlobalContentScaleFactor)
1378-
{
1379-
//the snapshot needs to be updated because the scale has
1380-
//changed since the last snapshot was taken.
1381-
this.invalidate(INVALIDATION_FLAG_SIZE);
1382-
this.validate();
1383-
}
1384-
}
13851384
var scaleFactor:Number = starling.contentScaleFactor;
13861385
if(!this._nativeFilters || this._nativeFilters.length === 0)
13871386
{

source/feathers/controls/text/TextFieldTextEditor.as

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,11 @@ package feathers.controls.text
332332
*/
333333
protected var _lastGlobalScaleY:Number = 0;
334334

335+
/**
336+
* @private
337+
*/
338+
protected var _needsTextureUpdate:Boolean = false;
339+
335340
/**
336341
* @private
337342
*/
@@ -1403,21 +1408,31 @@ package feathers.controls.text
14031408
*/
14041409
override public function render(painter:Painter):void
14051410
{
1406-
if(this.textSnapshot)
1411+
if(this.textSnapshot !== null && this._updateSnapshotOnScaleChange)
14071412
{
1408-
if(this._updateSnapshotOnScaleChange)
1413+
var matrix:Matrix = Pool.getMatrix();
1414+
this.getTransformationMatrix(this.stage, matrix);
1415+
if(matrixToScaleX(matrix) !== this._lastGlobalScaleX ||
1416+
matrixToScaleY(matrix) !== this._lastGlobalScaleY)
14091417
{
1410-
var matrix:Matrix = Pool.getMatrix();
1411-
this.getTransformationMatrix(this.stage, matrix);
1412-
if(matrixToScaleX(matrix) !== this._lastGlobalScaleX ||
1413-
matrixToScaleY(matrix) !== this._lastGlobalScaleY)
1414-
{
1415-
//the snapshot needs to be updated because the scale has
1416-
//changed since the last snapshot was taken.
1417-
this.invalidate(INVALIDATION_FLAG_SIZE);
1418-
this.validate();
1419-
}
1420-
Pool.putMatrix(matrix);
1418+
//the snapshot needs to be updated because the scale has
1419+
//changed since the last snapshot was taken.
1420+
this._needsTextureUpdate = true;
1421+
}
1422+
Pool.putMatrix(matrix);
1423+
}
1424+
if(this._needsTextureUpdate)
1425+
{
1426+
this._needsTextureUpdate = false;
1427+
if(this._useSnapshotDelayWorkaround)
1428+
{
1429+
//sometimes, we need to wait a frame for flash.text.TextField
1430+
//to render properly when drawing to BitmapData.
1431+
this.addEventListener(Event.ENTER_FRAME, refreshSnapshot_enterFrameHandler);
1432+
}
1433+
else
1434+
{
1435+
this.refreshSnapshot();
14211436
}
14221437
this.positionSnapshot();
14231438
}
@@ -2078,16 +2093,7 @@ package feathers.controls.text
20782093

20792094
if(!this._textFieldHasFocus && (sizeInvalid || stylesInvalid || dataInvalid || stateInvalid || this._needsNewTexture))
20802095
{
2081-
if(this._useSnapshotDelayWorkaround)
2082-
{
2083-
//sometimes, we need to wait a frame for flash.text.TextField
2084-
//to render properly when drawing to BitmapData.
2085-
this.addEventListener(Event.ENTER_FRAME, refreshSnapshot_enterFrameHandler);
2086-
}
2087-
else
2088-
{
2089-
this.refreshSnapshot();
2090-
}
2096+
this._needsTextureUpdate = true;
20912097
}
20922098
this.doPendingActions();
20932099
}

source/feathers/controls/text/TextFieldTextRenderer.as

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ package feathers.controls.text
170170
*/
171171
protected var _snapshotVisibleHeight:int = 0;
172172

173+
/**
174+
* @private
175+
*/
176+
protected var _needsTextureUpdate:Boolean = false;
177+
173178
/**
174179
* @private
175180
*/
@@ -1229,24 +1234,40 @@ package feathers.controls.text
12291234
*/
12301235
override public function render(painter:Painter):void
12311236
{
1232-
if(this.textSnapshot !== null)
1237+
var starling:Starling = this.stage !== null ? this.stage.starling : Starling.current;
1238+
if(this.textSnapshot !== null && this._updateSnapshotOnScaleChange)
12331239
{
1234-
var starling:Starling = this.stage !== null ? this.stage.starling : Starling.current;
1235-
if(this._updateSnapshotOnScaleChange)
1240+
this.getTransformationMatrix(this.stage, HELPER_MATRIX);
1241+
var globalScaleX:Number = matrixToScaleX(HELPER_MATRIX);
1242+
var globalScaleY:Number = matrixToScaleY(HELPER_MATRIX);
1243+
if(globalScaleX != this._lastGlobalScaleX ||
1244+
globalScaleY != this._lastGlobalScaleY ||
1245+
starling.contentScaleFactor != this._lastContentScaleFactor)
12361246
{
1237-
this.getTransformationMatrix(this.stage, HELPER_MATRIX);
1238-
var globalScaleX:Number = matrixToScaleX(HELPER_MATRIX);
1239-
var globalScaleY:Number = matrixToScaleY(HELPER_MATRIX);
1240-
if(globalScaleX != this._lastGlobalScaleX ||
1241-
globalScaleY != this._lastGlobalScaleY ||
1242-
starling.contentScaleFactor != this._lastContentScaleFactor)
1247+
//the snapshot needs to be updated because the scale has
1248+
//changed since the last snapshot was taken.
1249+
this._needsTextureUpdate = true;
1250+
}
1251+
}
1252+
if(this._needsTextureUpdate)
1253+
{
1254+
this._needsTextureUpdate = false;
1255+
if(this._text.length > 0)
1256+
{
1257+
if(this._useSnapshotDelayWorkaround)
12431258
{
1244-
//the snapshot needs to be updated because the scale has
1245-
//changed since the last snapshot was taken.
1246-
this.invalidate(INVALIDATION_FLAG_SIZE);
1247-
this.validate();
1259+
//we need to wait a frame for the TextField to render
1260+
//properly. sometimes two, and this is a known issue.
1261+
this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
1262+
}
1263+
else
1264+
{
1265+
this.refreshSnapshot();
12481266
}
12491267
}
1268+
}
1269+
if(this.textSnapshot !== null)
1270+
{
12501271
var scaleFactor:Number = starling.contentScaleFactor;
12511272
if(!this._nativeFilters || this._nativeFilters.length === 0)
12521273
{
@@ -1282,11 +1303,13 @@ package feathers.controls.text
12821303
if(snapshotIndex < 0)
12831304
{
12841305
var snapshot:Image = this.textSnapshot;
1306+
snapshot.visible = this._text.length > 0 && this._snapshotWidth > 0 && this._snapshotHeight > 0;
12851307
}
12861308
else
12871309
{
12881310
snapshot = this.textSnapshots[snapshotIndex];
12891311
}
1312+
snapshot.pixelSnapping = this._pixelSnapping;
12901313
snapshot.x = xPosition / scaleFactor;
12911314
snapshot.y = yPosition / scaleFactor;
12921315
snapshotIndex++;
@@ -1629,32 +1652,7 @@ package feathers.controls.text
16291652
{
16301653
this._previousActualWidth = this.actualWidth;
16311654
this._previousActualHeight = this.actualHeight;
1632-
var hasText:Boolean = this._text.length > 0;
1633-
if(hasText)
1634-
{
1635-
if(this._useSnapshotDelayWorkaround)
1636-
{
1637-
//we need to wait a frame for the TextField to render
1638-
//properly. sometimes two, and this is a known issue.
1639-
this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
1640-
}
1641-
else
1642-
{
1643-
this.refreshSnapshot();
1644-
}
1645-
}
1646-
if(this.textSnapshot)
1647-
{
1648-
this.textSnapshot.visible = hasText && this._snapshotWidth > 0 && this._snapshotHeight > 0;
1649-
this.textSnapshot.pixelSnapping = this._pixelSnapping;
1650-
}
1651-
if(this.textSnapshots)
1652-
{
1653-
for each(var snapshot:Image in this.textSnapshots)
1654-
{
1655-
snapshot.pixelSnapping = this._pixelSnapping;
1656-
}
1657-
}
1655+
this._needsTextureUpdate = true;
16581656
}
16591657
}
16601658

0 commit comments

Comments
 (0)