Skip to content

Commit

Permalink
Text Renderers/Editors: if they make a texture snapshot, it only happ…
Browse files Browse the repository at this point in the history
…ens 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)
  • Loading branch information
joshtynjala committed Apr 14, 2017
1 parent af4c441 commit f008306
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 93 deletions.
46 changes: 31 additions & 15 deletions source/feathers/controls/text/StageTextTextEditor.as
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,11 @@ package feathers.controls.text
*/
protected var _needsNewTexture:Boolean = false;

/**
* @private
*/
protected var _needsTextureUpdate:Boolean = false;

/**
* @private
*/
Expand Down Expand Up @@ -1225,7 +1230,7 @@ package feathers.controls.text
{
painter.excludeFromCache(this);
}
if(this.textSnapshot && this._updateSnapshotOnScaleChange)
if(this.textSnapshot !== null && this._updateSnapshotOnScaleChange)
{
var matrix:Matrix = Pool.getMatrix();
this.getTransformationMatrix(this.stage, matrix);
Expand All @@ -1234,15 +1239,36 @@ package feathers.controls.text
{
//the snapshot needs to be updated because the scale has
//changed since the last snapshot was taken.
this.invalidate(INVALIDATION_FLAG_SIZE);
this.validate();
this._needsTextureUpdate = true;
}
Pool.putMatrix(matrix);
}
if(this._needsTextureUpdate)
{
this._needsTextureUpdate = false;
var hasText:Boolean = this._text.length > 0;
if(hasText)
{
this.refreshSnapshot();
}
if(this.textSnapshot)
{
this.textSnapshot.visible = !this._stageTextHasFocus;
this.textSnapshot.alpha = hasText ? 1 : 0;
}
if(!this._stageTextHasFocus)
{
//hide the StageText after the snapshot is created
//native controls don't necessarily render at the same time
//as starling, and we don't want to see the text disappear
//for a moment
this.stageText.visible = false;
}
}

//we'll skip this if the text field isn't visible to avoid running
//that code every frame.
if(this.stageText && this.stageText.visible)
if(this.stageText !== null && this.stageText.visible)
{
this.refreshViewPortAndFontSize();
}
Expand Down Expand Up @@ -1612,17 +1638,7 @@ package feathers.controls.text

if(!this._stageTextHasFocus && (stateInvalid || stylesInvalid || dataInvalid || sizeInvalid || this._needsNewTexture))
{
var hasText:Boolean = this._text.length > 0;
if(hasText)
{
this.refreshSnapshot();
}
if(this.textSnapshot)
{
this.textSnapshot.visible = !this._stageTextHasFocus;
this.textSnapshot.alpha = hasText ? 1 : 0;
}
this.stageText.visible = false;
this._needsTextureUpdate = true;
}

this.doPendingActions();
Expand Down
31 changes: 15 additions & 16 deletions source/feathers/controls/text/TextBlockTextRenderer.as
Original file line number Diff line number Diff line change
Expand Up @@ -1356,6 +1356,21 @@ package feathers.controls.text
*/
override public function render(painter:Painter):void
{
var starling:Starling = this.stage !== null ? this.stage.starling : Starling.current;
if(this.textSnapshot !== null && this._updateSnapshotOnScaleChange)
{
this.getTransformationMatrix(this.stage, HELPER_MATRIX);
var globalScaleX:Number = matrixToScaleX(HELPER_MATRIX);
var globalScaleY:Number = matrixToScaleY(HELPER_MATRIX);
if(globalScaleX != this._lastGlobalScaleX ||
globalScaleY != this._lastGlobalScaleY ||
starling.contentScaleFactor != this._lastGlobalContentScaleFactor)
{
//the snapshot needs to be updated because the scale has
//changed since the last snapshot was taken.
this._needsNewTexture = true;
}
}
if(this._needsUpdateSnapshot)
{
this._needsUpdateSnapshot = false;
Expand All @@ -1366,22 +1381,6 @@ package feathers.controls.text
}
if(this.textSnapshot !== null)
{
var starling:Starling = this.stage !== null ? this.stage.starling : Starling.current;
if(this._updateSnapshotOnScaleChange)
{
this.getTransformationMatrix(this.stage, HELPER_MATRIX);
var globalScaleX:Number = matrixToScaleX(HELPER_MATRIX);
var globalScaleY:Number = matrixToScaleY(HELPER_MATRIX);
if(globalScaleX != this._lastGlobalScaleX ||
globalScaleY != this._lastGlobalScaleY ||
starling.contentScaleFactor != this._lastGlobalContentScaleFactor)
{
//the snapshot needs to be updated because the scale has
//changed since the last snapshot was taken.
this.invalidate(INVALIDATION_FLAG_SIZE);
this.validate();
}
}
var scaleFactor:Number = starling.contentScaleFactor;
if(!this._nativeFilters || this._nativeFilters.length === 0)
{
Expand Down
52 changes: 29 additions & 23 deletions source/feathers/controls/text/TextFieldTextEditor.as
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,11 @@ package feathers.controls.text
*/
protected var _lastGlobalScaleY:Number = 0;

/**
* @private
*/
protected var _needsTextureUpdate:Boolean = false;

/**
* @private
*/
Expand Down Expand Up @@ -1403,21 +1408,31 @@ package feathers.controls.text
*/
override public function render(painter:Painter):void
{
if(this.textSnapshot)
if(this.textSnapshot !== null && this._updateSnapshotOnScaleChange)
{
if(this._updateSnapshotOnScaleChange)
var matrix:Matrix = Pool.getMatrix();
this.getTransformationMatrix(this.stage, matrix);
if(matrixToScaleX(matrix) !== this._lastGlobalScaleX ||
matrixToScaleY(matrix) !== this._lastGlobalScaleY)
{
var matrix:Matrix = Pool.getMatrix();
this.getTransformationMatrix(this.stage, matrix);
if(matrixToScaleX(matrix) !== this._lastGlobalScaleX ||
matrixToScaleY(matrix) !== this._lastGlobalScaleY)
{
//the snapshot needs to be updated because the scale has
//changed since the last snapshot was taken.
this.invalidate(INVALIDATION_FLAG_SIZE);
this.validate();
}
Pool.putMatrix(matrix);
//the snapshot needs to be updated because the scale has
//changed since the last snapshot was taken.
this._needsTextureUpdate = true;
}
Pool.putMatrix(matrix);
}
if(this._needsTextureUpdate)
{
this._needsTextureUpdate = false;
if(this._useSnapshotDelayWorkaround)
{
//sometimes, we need to wait a frame for flash.text.TextField
//to render properly when drawing to BitmapData.
this.addEventListener(Event.ENTER_FRAME, refreshSnapshot_enterFrameHandler);
}
else
{
this.refreshSnapshot();
}
this.positionSnapshot();
}
Expand Down Expand Up @@ -2078,16 +2093,7 @@ package feathers.controls.text

if(!this._textFieldHasFocus && (sizeInvalid || stylesInvalid || dataInvalid || stateInvalid || this._needsNewTexture))
{
if(this._useSnapshotDelayWorkaround)
{
//sometimes, we need to wait a frame for flash.text.TextField
//to render properly when drawing to BitmapData.
this.addEventListener(Event.ENTER_FRAME, refreshSnapshot_enterFrameHandler);
}
else
{
this.refreshSnapshot();
}
this._needsTextureUpdate = true;
}
this.doPendingActions();
}
Expand Down
76 changes: 37 additions & 39 deletions source/feathers/controls/text/TextFieldTextRenderer.as
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ package feathers.controls.text
*/
protected var _snapshotVisibleHeight:int = 0;

/**
* @private
*/
protected var _needsTextureUpdate:Boolean = false;

/**
* @private
*/
Expand Down Expand Up @@ -1229,24 +1234,40 @@ package feathers.controls.text
*/
override public function render(painter:Painter):void
{
if(this.textSnapshot !== null)
var starling:Starling = this.stage !== null ? this.stage.starling : Starling.current;
if(this.textSnapshot !== null && this._updateSnapshotOnScaleChange)
{
var starling:Starling = this.stage !== null ? this.stage.starling : Starling.current;
if(this._updateSnapshotOnScaleChange)
this.getTransformationMatrix(this.stage, HELPER_MATRIX);
var globalScaleX:Number = matrixToScaleX(HELPER_MATRIX);
var globalScaleY:Number = matrixToScaleY(HELPER_MATRIX);
if(globalScaleX != this._lastGlobalScaleX ||
globalScaleY != this._lastGlobalScaleY ||
starling.contentScaleFactor != this._lastContentScaleFactor)
{
this.getTransformationMatrix(this.stage, HELPER_MATRIX);
var globalScaleX:Number = matrixToScaleX(HELPER_MATRIX);
var globalScaleY:Number = matrixToScaleY(HELPER_MATRIX);
if(globalScaleX != this._lastGlobalScaleX ||
globalScaleY != this._lastGlobalScaleY ||
starling.contentScaleFactor != this._lastContentScaleFactor)
//the snapshot needs to be updated because the scale has
//changed since the last snapshot was taken.
this._needsTextureUpdate = true;
}
}
if(this._needsTextureUpdate)
{
this._needsTextureUpdate = false;
if(this._text.length > 0)
{
if(this._useSnapshotDelayWorkaround)
{
//the snapshot needs to be updated because the scale has
//changed since the last snapshot was taken.
this.invalidate(INVALIDATION_FLAG_SIZE);
this.validate();
//we need to wait a frame for the TextField to render
//properly. sometimes two, and this is a known issue.
this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
else
{
this.refreshSnapshot();
}
}
}
if(this.textSnapshot !== null)
{
var scaleFactor:Number = starling.contentScaleFactor;
if(!this._nativeFilters || this._nativeFilters.length === 0)
{
Expand Down Expand Up @@ -1282,11 +1303,13 @@ package feathers.controls.text
if(snapshotIndex < 0)
{
var snapshot:Image = this.textSnapshot;
snapshot.visible = this._text.length > 0 && this._snapshotWidth > 0 && this._snapshotHeight > 0;
}
else
{
snapshot = this.textSnapshots[snapshotIndex];
}
snapshot.pixelSnapping = this._pixelSnapping;
snapshot.x = xPosition / scaleFactor;
snapshot.y = yPosition / scaleFactor;
snapshotIndex++;
Expand Down Expand Up @@ -1629,32 +1652,7 @@ package feathers.controls.text
{
this._previousActualWidth = this.actualWidth;
this._previousActualHeight = this.actualHeight;
var hasText:Boolean = this._text.length > 0;
if(hasText)
{
if(this._useSnapshotDelayWorkaround)
{
//we need to wait a frame for the TextField to render
//properly. sometimes two, and this is a known issue.
this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
else
{
this.refreshSnapshot();
}
}
if(this.textSnapshot)
{
this.textSnapshot.visible = hasText && this._snapshotWidth > 0 && this._snapshotHeight > 0;
this.textSnapshot.pixelSnapping = this._pixelSnapping;
}
if(this.textSnapshots)
{
for each(var snapshot:Image in this.textSnapshots)
{
snapshot.pixelSnapping = this._pixelSnapping;
}
}
this._needsTextureUpdate = true;
}
}

Expand Down

0 comments on commit f008306

Please sign in to comment.