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 325e470
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 95 deletions.
47 changes: 34 additions & 13 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 @@ -1239,10 +1244,32 @@ package feathers.controls.text
}
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 +1639,11 @@ 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;
//we're going to update the texture in render() because
//there's a chance that it will be updated more than once per
//frame if we do it here.
this._needsTextureUpdate = true;
this.setRequiresRedraw();
}

this.doPendingActions();
Expand Down
40 changes: 20 additions & 20 deletions source/feathers/controls/text/TextBlockTextRenderer.as
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ package feathers.controls.text
/**
* @private
*/
protected var _needsUpdateSnapshot:Boolean = false;
protected var _needsTextureUpdate:Boolean = false;

/**
* @private
Expand Down Expand Up @@ -1356,32 +1356,32 @@ package feathers.controls.text
*/
override public function render(painter:Painter):void
{
if(this._needsUpdateSnapshot)
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.invalidate(INVALIDATION_FLAG_SIZE);
this.validate();
}
}
if(this._needsTextureUpdate)
{
this._needsUpdateSnapshot = false;
this._needsTextureUpdate = false;
if(this._content !== null)
{
this.refreshSnapshot();
}
}
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 Expand Up @@ -1729,7 +1729,7 @@ package feathers.controls.text
//we're going to update the texture in render() because
//there's a chance that it will be updated more than once per
//frame if we do it here.
this._needsUpdateSnapshot = true;
this._needsTextureUpdate = true;
this.setRequiresRedraw();
}
}
Expand Down
57 changes: 34 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,32 @@ 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.invalidate(INVALIDATION_FLAG_SIZE);
this.validate();
}
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 +2094,11 @@ 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();
}
//we're going to update the texture in render() because
//there's a chance that it will be updated more than once per
//frame if we do it here.
this._needsTextureUpdate = true;
this.setRequiresRedraw();
}
this.doPendingActions();
}
Expand Down
81 changes: 42 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,41 @@ 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.invalidate(INVALIDATION_FLAG_SIZE);
this.validate();
}
}
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 +1304,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 +1653,11 @@ 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;
}
}
//we're going to update the texture in render() because
//there's a chance that it will be updated more than once per
//frame if we do it here.
this._needsTextureUpdate = true;
this.setRequiresRedraw();
}
}

Expand Down

0 comments on commit 325e470

Please sign in to comment.