Skip to content

Commit

Permalink
Cache extents of containers
Browse files Browse the repository at this point in the history
  • Loading branch information
hughsando committed Mar 30, 2013
1 parent 77376eb commit 5b0655a
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 33 deletions.
14 changes: 10 additions & 4 deletions project/common/CachedExtent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
namespace nme
{

static int sgCachedExtentID = 1;
int gCachedExtentID = 1;

// --- CachedExtent --------------------------------------

Extent2DF CachedExtent::Get(const Transform &inTransform)
{
mID = sgCachedExtentID++;
mID = gCachedExtentID++;
if (!mExtent.Valid())
return Extent2DF();
/*
Expand Down Expand Up @@ -48,12 +48,17 @@ bool CachedExtentRenderer::GetExtent(const Transform &inTransform,Extent2DF &ioE
for(int i=0;i<3;i++)
{
CachedExtent &cache = mExtentCache[i];
if (cache.mExtent.Valid() && test==cache.mTestMatrix &&
if (cache.mIsSet && test==cache.mTestMatrix &&
*inTransform.mScale9==cache.mScale9 && cache.mIncludeStroke==inIncludeStroke)
{
ioExtent.Add(cache.Get(inTransform));
// Maybe set but not valid - ie, 0 size
if (cache.mExtent.Valid())
ioExtent.Add(cache.Get(inTransform));
return true;
}
if (cache.mID<gCachedExtentID)
cache.mID = gCachedExtentID;

if (cache.mID<smallest)
{
smallest = cache.mID;
Expand All @@ -71,6 +76,7 @@ bool CachedExtentRenderer::GetExtent(const Transform &inTransform,Extent2DF &ioE
cache.mTransform.mMatrix3D = &cache.mMatrix;
cache.mTransform.mScale9 = &cache.mScale9;
cache.mIncludeStroke = inIncludeStroke;
cache.mIsSet = true;
GetExtent(cache);

cache.Get(inTransform);
Expand Down
66 changes: 57 additions & 9 deletions project/common/Display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Graphics &DisplayObject::GetGraphics()
{
if (!mGfx)
{
mGfx = new Graphics(true);
mGfx = new Graphics(this,true);
}
return *mGfx;
}
Expand All @@ -78,7 +78,6 @@ void DisplayObject::ClearCacheDirty()
}



void DisplayObject::SetParent(DisplayObjectContainer *inParent)
{
IncRef();
Expand Down Expand Up @@ -263,10 +262,13 @@ void DisplayObject::DirtyUp(uint32 inFlags)

void DisplayObject::DirtyCache(bool inParentOnly)
{
if (!inParentOnly)
mDirtyFlags |= dirtCache;
else if (mParent)
mParent->DirtyCache(false);
if (!(mDirtyFlags & dirtCache))
{
if (!inParentOnly)
mDirtyFlags |= dirtCache;
if (mParent)
mParent->DirtyCache(false);
}
}

Matrix DisplayObject::GetFullMatrix(bool inStageScaling)
Expand Down Expand Up @@ -865,6 +867,14 @@ void DisplayObjectContainer::addChild(DisplayObject *inChild)
DecRef();
}

void DisplayObjectContainer::DirtyCache(bool inParentOnly)
{
DisplayObject::DirtyCache(inParentOnly);
mExtentCache[0].mIsSet =
mExtentCache[1].mIsSet =
mExtentCache[2].mIsSet = false;
}

void DisplayObjectContainer::DirtyUp(uint32 inFlags)
{
mDirtyFlags |= inFlags;
Expand Down Expand Up @@ -1265,7 +1275,42 @@ void DisplayObjectContainer::Render( const RenderTarget &inTarget, const RenderS

void DisplayObjectContainer::GetExtent(const Transform &inTrans, Extent2DF &outExt,bool inForScreen,bool inIncludeStroke)
{
DisplayObject::GetExtent(inTrans,outExt,inForScreen,inIncludeStroke);
int smallest = mExtentCache[0].mID;
int slot = 0;
for(int i=0;i<3;i++)
{
CachedExtent &cache = mExtentCache[i];
if (cache.mIsSet && *inTrans.mMatrix==cache.mMatrix &&
*inTrans.mScale9==cache.mScale9 && cache.mIncludeStroke==inIncludeStroke &&
cache.mForScreen==inForScreen)
{
// Maybe set but not valid - ie, 0 size
if (cache.mExtent.Valid())
outExt.Add(cache.mExtent);
return;
}
if (cache.mID<gCachedExtentID)
cache.mID = gCachedExtentID;

if (cache.mID<smallest)
{
smallest = cache.mID;
slot = i;
}
}

// Need to recalculate the extent...
CachedExtent &cache = mExtentCache[slot];
cache.mExtent = Extent2DF();
cache.mIsSet = true;
cache.mMatrix = *inTrans.mMatrix;
cache.mScale9 = *inTrans.mScale9;
// todo:Matrix3d?
cache.mForScreen = inForScreen;
cache.mIncludeStroke = inIncludeStroke;


DisplayObject::GetExtent(inTrans,cache.mExtent,inForScreen,inIncludeStroke);

Matrix full;
Transform trans(inTrans);
Expand All @@ -1282,13 +1327,16 @@ void DisplayObjectContainer::GetExtent(const Transform &inTrans, Extent2DF &outE
{
double x = (corner & 1) ? obj->scrollRect.w : 0;
double y = (corner & 2) ? obj->scrollRect.h : 0;
outExt.Add( full.Apply(x,y) );
cache.mExtent.Add( full.Apply(x,y) );
}
}
else
// Seems scroll rects are ignored when calculating extent...
obj->GetExtent(trans,outExt,inForScreen,inIncludeStroke);
obj->GetExtent(trans,cache.mExtent,inForScreen,inIncludeStroke);
}

if (cache.mExtent.Valid())
outExt.Add(cache.mExtent);
}


Expand Down
2 changes: 1 addition & 1 deletion project/common/ExternalInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3410,7 +3410,7 @@ value nme_render_surface_to_surface(value* arg, int nargs)
state.mRoundSizeToPOW2 = false;
state.mPhase = rpRender;

Graphics *gfx = new Graphics(true);
Graphics *gfx = new Graphics(0,true);
gfx->beginBitmapFill(src,Matrix(),false,val_bool(arg[aSmooth]));
gfx->moveTo(0,0);
gfx->lineTo(src->Width(),0);
Expand Down
36 changes: 24 additions & 12 deletions project/common/Graphics.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
#include <Graphics.h>
#include <Surface.h>
#include <Display.h>

namespace nme
{

void Graphics::OnChanged()
{
mVersion++;
if (mOwner && !(mOwner->mDirtyFlags & dirtCache))
mOwner->DirtyCache(false);
}



// TODO: invlidate/cache extents (do for whole lot at once)

Graphics::Graphics(bool inInitRef) : Object(inInitRef)
Graphics::Graphics(DisplayObject *inOwner,bool inInitRef) : Object(inInitRef)
{
mRotation0 = 0;
mCursor = UserPoint(0,0);
Expand All @@ -16,6 +26,7 @@ Graphics::Graphics(bool inInitRef) : Object(inInitRef)
mTileJob.mIsTileJob = true;
mMeasuredJobs = 0;
mVersion = 0;
mOwner = inOwner;
}


Expand Down Expand Up @@ -49,7 +60,7 @@ void Graphics::clear()
mBuiltHardware = 0;
mMeasuredJobs = 0;
mCursor = UserPoint(0,0);
mVersion++;
OnChanged();
}

int Graphics::Version() const
Expand Down Expand Up @@ -87,7 +98,7 @@ void Graphics::drawEllipse(float x, float y, float width, float height)
mPathData->curveTo(x+w, y-ch_, x+w, y);

Flush();
mVersion++;
OnChanged();
}

void Graphics::drawRoundRect(float x,float y,float width,float height,float rx,float ry)
Expand Down Expand Up @@ -124,7 +135,7 @@ void Graphics::drawRoundRect(float x,float y,float width,float height,float
mPathData->lineTo(x+w, y+lh);

Flush();
mVersion++;
OnChanged();
}

void Graphics::drawPath(const QuickVec<uint8> &inCommands, const QuickVec<float> &inData,
Expand Down Expand Up @@ -168,7 +179,7 @@ void Graphics::drawPath(const QuickVec<uint8> &inCommands, const QuickVec<float>
point += 2;
}
}
mVersion++;
OnChanged();
}


Expand Down Expand Up @@ -231,14 +242,14 @@ void Graphics::drawGraphicsDatum(IGraphicsData *inData)
break;

}
mVersion++;
OnChanged();
}

void Graphics::drawGraphicsData(IGraphicsData **graphicsData,int inN)
{
for(int i=0;i<inN;i++)
drawGraphicsDatum(graphicsData[i]);
mVersion++;
OnChanged();
}

void Graphics::beginFill(unsigned int color, float alpha)
Expand Down Expand Up @@ -282,6 +293,8 @@ void Graphics::endTiles()
{
mTileJob.mFill->DecRef();
mTileJob.mFill = 0;

OnChanged();
}
}

Expand Down Expand Up @@ -330,14 +343,14 @@ void Graphics::lineTo(float x, float y)

mPathData->lineTo(x,y);
mCursor = UserPoint(x,y);
mVersion++;
OnChanged();
}

void Graphics::moveTo(float x, float y)
{
mPathData->moveTo(x,y);
mCursor = UserPoint(x,y);
mVersion++;
OnChanged();
}

void Graphics::curveTo(float cx, float cy, float x, float y)
Expand All @@ -354,7 +367,7 @@ void Graphics::curveTo(float cx, float cy, float x, float y)
else
mPathData->curveTo(cx,cy,x,y);
mCursor = UserPoint(x,y);
mVersion++;
OnChanged();
}

void Graphics::arcTo(float cx, float cy, float x, float y)
Expand All @@ -365,13 +378,12 @@ void Graphics::arcTo(float cx, float cy, float x, float y)

mPathData->arcTo(cx,cy,x,y);
mCursor = UserPoint(x,y);
mVersion++;
OnChanged();
}

void Graphics::tile(float x, float y, const Rect &inTileRect,float *inTrans,float *inRGBA)
{
mPathData->tile(x,y,inTileRect,inTrans,inRGBA);
mVersion++;
}


Expand Down
4 changes: 2 additions & 2 deletions project/common/TextField.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1150,7 +1150,7 @@ void TextField::BuildBackground()
{
ImagePoint scroll = GetScrollPos();
if (!mHighlightGfx)
mHighlightGfx = new Graphics(true);
mHighlightGfx = new Graphics(this,true);

int l0 = LineFromChar(mSelectMin);
int l1 = LineFromChar(mSelectMax-1);
Expand Down Expand Up @@ -1271,7 +1271,7 @@ void TextField::Render( const RenderTarget &inTarget, const RenderState &inState
if (caret)
{
if (!mCaretGfx)
mCaretGfx = new Graphics(true);
mCaretGfx = new Graphics(this,true);
int line = LineFromChar(caretIndex);
if (line>=0)
{
Expand Down
6 changes: 4 additions & 2 deletions project/include/CachedExtent.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
namespace nme
{


extern int gCachedExtentID;
struct CachedExtent
{
CachedExtent() : mID(0), mIncludeStroke(false) {}
CachedExtent() : mID(0), mIncludeStroke(false), mIsSet(false), mForScreen(false) {}
Extent2DF Get(const Transform &inTransform);

Transform mTransform;
Expand All @@ -19,6 +19,8 @@ struct CachedExtent
Scale9 mScale9;
Extent2DF mExtent;
bool mIncludeStroke;
bool mIsSet;
bool mForScreen;
int mID;
};

Expand Down
7 changes: 6 additions & 1 deletion project/include/Display.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <Utils.h>
#include <Geom.h>
#include <Graphics.h>
#include <CachedExtent.h>
#include <string>
#include <Filters.h>

Expand Down Expand Up @@ -283,6 +284,8 @@ class DisplayObject : public Object
const ColorTransform *inObjTrans,
ColorTransform *inBuf);

uint32 mDirtyFlags;

protected:
void UpdateDecomp();
void UpdateLocalMatrix();
Expand All @@ -299,7 +302,6 @@ class DisplayObject : public Object
int mIsMaskCount;

// Matrix stuff
uint32 mDirtyFlags;
Matrix mLocalMatrix;
// Decomp
double x;
Expand Down Expand Up @@ -333,6 +335,8 @@ class DisplayObjectContainer : public DisplayObject
bool NonNormalBlendChild();
virtual void GetExtent(const Transform &inTrans, Extent2DF &outExt,bool inForBitmap,bool inIncludeStroke);

void DirtyCache(bool inParentOnly = false);

void hackAddChild(DisplayObject *inObj) { mChildren.push_back(inObj); }
void hackRemoveChildren() { mChildren.resize(0); }

Expand All @@ -341,6 +345,7 @@ class DisplayObjectContainer : public DisplayObject


bool mouseChildren;
CachedExtent mExtentCache[3];
protected:
~DisplayObjectContainer();
QuickVec<DisplayObject *> mChildren;
Expand Down
Loading

0 comments on commit 5b0655a

Please sign in to comment.