Skip to content
379 changes: 336 additions & 43 deletions src/openfl/display/BitmapData.hx

Large diffs are not rendered by default.

144 changes: 85 additions & 59 deletions src/openfl/display/DisplayObjectRenderer.hx
Original file line number Diff line number Diff line change
Expand Up @@ -216,26 +216,27 @@ class DisplayObjectRenderer extends EventDispatcher
{
if (displayObject == null) return false;
var renderer = this;
var allowFramebuffer = renderer.__type == OPENGL;

switch (displayObject.__drawableType)
{
case BITMAP:
var bitmap:Bitmap = cast displayObject;
// TODO: Handle filters without an intermediate draw
if (bitmap.__bitmapData == null
|| (bitmap.__filters == null #if lime && renderer.__type == OPENGL #end && bitmap.__cacheBitmap == null)) return false;
|| (bitmap.__filters == null #if lime && allowFramebuffer #end && bitmap.__cacheBitmap == null)) return false;
force = (bitmap.__bitmapData.image != null && bitmap.__bitmapData.image.version != bitmap.__imageVersion);

case TEXT_FIELD:
var textField:TextField = cast displayObject;
if (textField.__filters == null #if lime && renderer.__type == OPENGL #end && textField.__cacheBitmap == null
if (textField.__filters == null #if lime && allowFramebuffer #end && textField.__cacheBitmap == null
&& !textField.__domRender) return false;
if (force) textField.__renderDirty = true;
force = force || textField.__dirty;

case TILEMAP:
var tilemap:Tilemap = cast displayObject;
if (tilemap.__filters == null #if lime && renderer.__type == OPENGL #end && tilemap.__cacheBitmap == null) return false;
if (tilemap.__filters == null #if lime && allowFramebuffer #end && tilemap.__cacheBitmap == null) return false;

default:
}
Expand All @@ -252,9 +253,8 @@ class DisplayObjectRenderer extends EventDispatcher
var updated = false;

if (displayObject.cacheAsBitmap
|| (renderer.__type != OPENGL
&& !colorTransform.__isDefault(true) #if (openfl_legacy_scale9grid && openfl_force_gl_cacheasbitmap_for_scale9grid)
|| (renderer.__type == OPENGL && displayObject.scale9Grid != null) #end))
|| (!allowFramebuffer && !colorTransform.__isDefault(true) #if (openfl_legacy_scale9grid && openfl_force_gl_cacheasbitmap_for_scale9grid)
|| (allowFramebuffer && displayObject.scale9Grid != null) #end))
{
var rect:Rectangle = null;

Expand All @@ -271,7 +271,7 @@ class DisplayObjectRenderer extends EventDispatcher
if (softwareDirty || hardwareDirty)
{
#if !openfl_force_gl_cacheasbitmap
if (renderType == OPENGL)
if (allowFramebuffer)
{
if (#if !openfl_disable_gl_cacheasbitmap __shouldCacheHardware(displayObject, null) == false #else true #end)
{
Expand All @@ -280,12 +280,22 @@ class DisplayObjectRenderer extends EventDispatcher
#else
renderType = CAIRO;
#end
allowFramebuffer = false;
}
}
#end

if (softwareDirty && (renderType == CANVAS || renderType == CAIRO)) needRender = true;
if (hardwareDirty && renderType == OPENGL) needRender = true;
if (!allowFramebuffer)
{
if (softwareDirty)
{
needRender = true;
}
}
else if (hardwareDirty)
{
needRender = true;
}
}

var updateTransform = (needRender || !displayObject.__cacheBitmap.__worldTransform.equals(displayObject.__worldTransform));
Expand Down Expand Up @@ -336,7 +346,7 @@ class DisplayObjectRenderer extends EventDispatcher
}

if (!needRender
&& renderer.__type != OPENGL
&& !allowFramebuffer
&& displayObject.__cacheBitmapData != null
&& displayObject.__cacheBitmapData.image != null
&& displayObject.__cacheBitmapData.image.version < displayObject.__cacheBitmapData.__textureVersion)
Expand Down Expand Up @@ -382,12 +392,19 @@ class DisplayObjectRenderer extends EventDispatcher

displayObject.__getFilterBounds(rect, displayObject.__cacheBitmapMatrix);

filterWidth = rect.width > 0 ? Math.ceil(rect.width * pixelRatio) : 0;
filterHeight = rect.height > 0 ? Math.ceil(rect.height * pixelRatio) : 0;
filterWidth = rect.width > 0 ? Math.floor(rect.width * pixelRatio) : 0;
filterHeight = rect.height > 0 ? Math.floor(rect.height * pixelRatio) : 0;

offsetX = rect.x > 0 ? Math.ceil(rect.x) : Math.floor(rect.x);
offsetY = rect.y > 0 ? Math.ceil(rect.y) : Math.floor(rect.y);

#if lime
bitmapWidth = filterWidth;
bitmapHeight = filterHeight;

if (!needRender) needRender = displayObject.__cacheBitmapData == null ||
displayObject.__cacheBitmapData.width != filterWidth || displayObject.__cacheBitmapData.height != filterHeight;
#else
if (displayObject.__cacheBitmapData != null)
{
if (filterWidth > displayObject.__cacheBitmapData.width || filterHeight > displayObject.__cacheBitmapData.height)
Expand All @@ -407,6 +424,7 @@ class DisplayObjectRenderer extends EventDispatcher
bitmapWidth = filterWidth;
bitmapHeight = filterHeight;
}
#end
}

if (needRender)
Expand All @@ -416,39 +434,57 @@ class DisplayObjectRenderer extends EventDispatcher

if (filterWidth >= 0.5 && filterHeight >= 0.5)
{
var needsFill = (displayObject.opaqueBackground != null
&& (bitmapWidth != filterWidth || bitmapHeight != filterHeight));
var fillColor = displayObject.opaqueBackground != null ? (0xFF << 24) | displayObject.opaqueBackground : 0;
var bitmapColor = needsFill ? 0 : fillColor;
var allowFramebuffer = (renderer.__type == OPENGL);

if (displayObject.__cacheBitmapData == null
|| bitmapWidth > displayObject.__cacheBitmapData.width
|| bitmapHeight > displayObject.__cacheBitmapData.height)
{
displayObject.__cacheBitmapData = new BitmapData(bitmapWidth, bitmapHeight, true, bitmapColor);
var fillColor = displayObject.opaqueBackground != null ? 0xFF000000 | displayObject.opaqueBackground : 0;

#if lime
if (allowFramebuffer)
{
if (displayObject.__cacheBitmap == null) displayObject.__cacheBitmap = new Bitmap();
displayObject.__cacheBitmap.__bitmapData = displayObject.__cacheBitmapData;
displayObject.__cacheBitmapRenderer = null;
if (displayObject.__cacheBitmapData == null)
{
displayObject.__cacheBitmapData = BitmapData.fromContext(cast(renderer, OpenGLRenderer).__context3D,
bitmapWidth, bitmapHeight);

displayObject.__cacheBitmap.__bitmapData = displayObject.__cacheBitmapData;
//displayObject.__cacheBitmapRenderer = null;
}
else if (bitmapWidth != displayObject.__cacheBitmapData.width || bitmapHeight != displayObject.__cacheBitmapData.height)
{
displayObject.__cacheBitmapData.resize(bitmapWidth, bitmapHeight);

displayObject.__cacheBitmap.__bitmapData = displayObject.__cacheBitmapData;
//displayObject.__cacheBitmapRenderer = null;
}

displayObject.__cacheBitmapData.__fillRect(displayObject.__cacheBitmapData.rect, fillColor, true);
}
else
#end
{
displayObject.__cacheBitmapData.__fillRect(displayObject.__cacheBitmapData.rect, bitmapColor, allowFramebuffer);
}
if (displayObject.__cacheBitmapData == null
|| bitmapWidth > displayObject.__cacheBitmapData.width
|| bitmapHeight > displayObject.__cacheBitmapData.height)
{
displayObject.__cacheBitmapData = new BitmapData(bitmapWidth, bitmapHeight, true, fillColor);

if (renderer.__type == OPENGL
&& displayObject.__cacheBitmapData.__texture != null
&& __hasMaskedDescendant(displayObject)
&& __isOnMouseOverPath(displayObject))
{
displayObject.__cacheBitmapData.__texture = null;
}
if (displayObject.__cacheBitmap == null) displayObject.__cacheBitmap = new Bitmap();
displayObject.__cacheBitmap.__bitmapData = displayObject.__cacheBitmapData;
displayObject.__cacheBitmapRenderer = null;
}
else
{
rect.setTo(0, 0, filterWidth, filterHeight);
displayObject.__cacheBitmapData.__fillRect(rect, fillColor, allowFramebuffer);
}

if (needsFill)
{
rect.setTo(0, 0, filterWidth, filterHeight);
displayObject.__cacheBitmapData.__fillRect(rect, fillColor, allowFramebuffer);
if (renderer.__type == OPENGL
&& displayObject.__cacheBitmapData.__texture != null
&& __hasMaskedDescendant(displayObject)
&& __isOnMouseOverPath(displayObject))
{
displayObject.__cacheBitmapData.__texture.dispose();
displayObject.__cacheBitmapData.__texture = null;
}
}
}
else
Expand Down Expand Up @@ -536,7 +572,7 @@ class DisplayObjectRenderer extends EventDispatcher
#if lime
if (displayObject.__cacheBitmapRenderer == null || renderType != displayObject.__cacheBitmapRenderer.__type)
{
if (renderType == OPENGL)
if (allowFramebuffer)
{
displayObject.__cacheBitmapRenderer = new OpenGLRenderer(cast(renderer, OpenGLRenderer).__context3D, displayObject.__cacheBitmapData);
}
Expand Down Expand Up @@ -634,20 +670,15 @@ class DisplayObjectRenderer extends EventDispatcher
var bitmap3:BitmapData = null;

// if (needSecondBitmapData) {
if (displayObject.__cacheBitmapData2 == null
|| bitmapWidth > displayObject.__cacheBitmapData2.width
|| bitmapHeight > displayObject.__cacheBitmapData2.height)
if (displayObject.__cacheBitmapData2 == null)
{
displayObject.__cacheBitmapData2 = new BitmapData(bitmapWidth, bitmapHeight, true, 0);
displayObject.__cacheBitmapData2 = BitmapData.fromContext(context, bitmapWidth, bitmapHeight);
}
else
else if (bitmapWidth != displayObject.__cacheBitmapData2.width || bitmapHeight != displayObject.__cacheBitmapData2.height)
{
displayObject.__cacheBitmapData2.fillRect(displayObject.__cacheBitmapData2.rect, 0);
if (displayObject.__cacheBitmapData2.image != null)
{
displayObject.__cacheBitmapData2.__textureVersion = displayObject.__cacheBitmapData2.image.version + 1;
}
displayObject.__cacheBitmapData2.resize(bitmapWidth, bitmapHeight);
}
displayObject.__cacheBitmapData2.__fillRect(displayObject.__cacheBitmapData2.rect, 0, true);
displayObject.__cacheBitmapData2.__setUVRect(context, 0, 0, filterWidth, filterHeight);
bitmap2 = displayObject.__cacheBitmapData2;
// } else {
Expand All @@ -656,20 +687,15 @@ class DisplayObjectRenderer extends EventDispatcher

if (needCopyOfOriginal)
{
if (displayObject.__cacheBitmapData3 == null
|| bitmapWidth > displayObject.__cacheBitmapData3.width
|| bitmapHeight > displayObject.__cacheBitmapData3.height)
if (displayObject.__cacheBitmapData3 == null)
{
displayObject.__cacheBitmapData3 = new BitmapData(bitmapWidth, bitmapHeight, true, 0);
displayObject.__cacheBitmapData3 = BitmapData.fromContext(context, bitmapWidth, bitmapHeight);
}
else
else if (bitmapWidth != displayObject.__cacheBitmapData3.width || bitmapHeight != displayObject.__cacheBitmapData3.height)
{
displayObject.__cacheBitmapData3.fillRect(displayObject.__cacheBitmapData3.rect, 0);
if (displayObject.__cacheBitmapData3.image != null)
{
displayObject.__cacheBitmapData3.__textureVersion = displayObject.__cacheBitmapData3.image.version + 1;
}
displayObject.__cacheBitmapData3.resize(bitmapWidth, bitmapHeight);
}
displayObject.__cacheBitmapData3.__fillRect(displayObject.__cacheBitmapData3.rect, 0, true);
displayObject.__cacheBitmapData3.__setUVRect(context, 0, 0, filterWidth, filterHeight);
bitmap3 = displayObject.__cacheBitmapData3;
}
Expand Down
Loading
Loading