Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Cache extents of containers

  • Loading branch information...
commit 5b0655a52798f3bd6d12b491a47180e568ba5ff5 1 parent 77376eb
@hughsando hughsando authored
View
14 project/common/CachedExtent.cpp
@@ -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();
/*
@@ -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;
@@ -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);
View
66 project/common/Display.cpp
@@ -59,7 +59,7 @@ Graphics &DisplayObject::GetGraphics()
{
if (!mGfx)
{
- mGfx = new Graphics(true);
+ mGfx = new Graphics(this,true);
}
return *mGfx;
}
@@ -78,7 +78,6 @@ void DisplayObject::ClearCacheDirty()
}
-
void DisplayObject::SetParent(DisplayObjectContainer *inParent)
{
IncRef();
@@ -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)
@@ -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;
@@ -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);
@@ -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);
}
View
2  project/common/ExternalInterface.cpp
@@ -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);
View
36 project/common/Graphics.cpp
@@ -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);
@@ -16,6 +26,7 @@ Graphics::Graphics(bool inInitRef) : Object(inInitRef)
mTileJob.mIsTileJob = true;
mMeasuredJobs = 0;
mVersion = 0;
+ mOwner = inOwner;
}
@@ -49,7 +60,7 @@ void Graphics::clear()
mBuiltHardware = 0;
mMeasuredJobs = 0;
mCursor = UserPoint(0,0);
- mVersion++;
+ OnChanged();
}
int Graphics::Version() const
@@ -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)
@@ -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,
@@ -168,7 +179,7 @@ void Graphics::drawPath(const QuickVec<uint8> &inCommands, const QuickVec<float>
point += 2;
}
}
- mVersion++;
+ OnChanged();
}
@@ -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)
@@ -282,6 +293,8 @@ void Graphics::endTiles()
{
mTileJob.mFill->DecRef();
mTileJob.mFill = 0;
+
+ OnChanged();
}
}
@@ -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)
@@ -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)
@@ -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++;
}
View
4 project/common/TextField.cpp
@@ -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);
@@ -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)
{
View
6 project/include/CachedExtent.h
@@ -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;
@@ -19,6 +19,8 @@ struct CachedExtent
Scale9 mScale9;
Extent2DF mExtent;
bool mIncludeStroke;
+ bool mIsSet;
+ bool mForScreen;
int mID;
};
View
7 project/include/Display.h
@@ -5,6 +5,7 @@
#include <Utils.h>
#include <Geom.h>
#include <Graphics.h>
+#include <CachedExtent.h>
#include <string>
#include <Filters.h>
@@ -283,6 +284,8 @@ class DisplayObject : public Object
const ColorTransform *inObjTrans,
ColorTransform *inBuf);
+ uint32 mDirtyFlags;
+
protected:
void UpdateDecomp();
void UpdateLocalMatrix();
@@ -299,7 +302,6 @@ class DisplayObject : public Object
int mIsMaskCount;
// Matrix stuff
- uint32 mDirtyFlags;
Matrix mLocalMatrix;
// Decomp
double x;
@@ -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); }
@@ -341,6 +345,7 @@ class DisplayObjectContainer : public DisplayObject
bool mouseChildren;
+ CachedExtent mExtentCache[3];
protected:
~DisplayObjectContainer();
QuickVec<DisplayObject *> mChildren;
View
4 project/include/Graphics.h
@@ -669,7 +669,7 @@ typedef QuickVec<GraphicsJob> GraphicsJobs;
class Graphics : public Object
{
public:
- Graphics(bool inInitRef = false);
+ Graphics(DisplayObject *inOwner, bool inInitRef = false);
~Graphics();
void clear();
@@ -729,8 +729,10 @@ class Graphics : public Object
protected:
void BuildHardware();
void Flush(bool inLine=true,bool inFill=true,bool inTile=true);
+ inline void OnChanged();
private:
+ DisplayObject *mOwner;
GraphicsJobs mJobs;
int mVersion;
View
18 templates/default/flash/haxe/ApplicationMain.hx
@@ -93,6 +93,22 @@ class ApplicationMain {
mPreloader = null;
begin();
}
+
+ public static function getAsset(inName:String) : Dynamic
+ {
+ var types = Assets.type;
+ if (types.exists(inName))
+ switch(types.get(inName))
+ {
+ case BINARY, TEXT: return Assets.getBytes(inName);
+ case FONT: return Assets.getFont(inName);
+ case IMAGE: return Assets.getBitmapData(inName,false);
+ case MUSIC, SOUND: return Assets.getSound(inName);
+ }
+
+ throw "Asset does not exist: " + inName;
+ return null;
+ }
}
#else
@@ -158,4 +174,4 @@ class DocumentClass {
}
}
-#end
+#end
Please sign in to comment.
Something went wrong with that request. Please try again.