Bug 790673. Add BorrowedCGContext. r=bas

This can be used to safely get at the underlying CGContext.
This commit is contained in:
Jeff Muizelaar 2013-07-09 18:55:02 -04:00
parent 096bfd6457
commit 273bf40ece
3 changed files with 71 additions and 0 deletions

View File

@ -40,6 +40,9 @@ struct IDWriteRenderingParams;
class GrContext;
struct GrGLInterface;
struct CGContext;
typedef struct CGContext *CGContextRef;
namespace mozilla {
namespace gfx {
@ -993,6 +996,46 @@ private:
static DrawEventRecorder *mRecorder;
};
#ifdef XP_MACOSX
/* This is a helper class that let's you borrow a CGContextRef from a
* DrawTargetCG. This is used for drawing themed widgets.
*
* Callers should check the cg member after constructing the object
* to see if it succeeded. The DrawTarget should not be used while
* the context is borrowed. */
class BorrowedCGContext
{
public:
BorrowedCGContext(DrawTarget *aDT) : mDT(aDT)
{
cg = BorrowCGContextFromDrawTarget(aDT);
}
// The caller needs to call Finish if cg is non-null when
// they are done with the context. This is currently explicit
// instead of happening implicitly in the destructor to make
// what's happening in the caller more clear. It also
// let's you resume using the DrawTarget in the same scope.
void Finish()
{
if (cg) {
ReturnCGContextToDrawTarget(mDT, cg);
cg = nullptr;
}
}
~BorrowedCGContext() {
MOZ_ASSERT(!cg);
}
CGContextRef cg;
private:
static CGContextRef BorrowCGContextFromDrawTarget(DrawTarget *aDT);
static void ReturnCGContextToDrawTarget(DrawTarget *aDT, CGContextRef cg);
DrawTarget *mDT;
};
#endif
}
}

View File

@ -1166,6 +1166,33 @@ DrawTargetCG::MarkChanged()
}
}
CGContextRef
BorrowedCGContext::BorrowCGContextFromDrawTarget(DrawTarget *aDT)
{
if (aDT->GetType() == BACKEND_COREGRAPHICS || aDT->GetType() == BACKEND_COREGRAPHICS_ACCELERATED) {
DrawTargetCG* cgDT = static_cast<DrawTargetCG*>(aDT);
cgDT->MarkChanged();
// swap out the context
CGContextRef cg = cgDT->mCg;
cgDT->mCg = nullptr;
// save the state to make it easier for callers to avoid mucking with things
CGContextSaveGState(cg);
return cg;
}
return nullptr;
}
void
BorrowedCGContext::ReturnCGContextToDrawTarget(DrawTarget *aDT, CGContextRef cg)
{
DrawTargetCG* cgDT = static_cast<DrawTargetCG*>(aDT);
CGContextRestoreGState(cg);
cgDT->mCg = cg;
}
}

View File

@ -85,6 +85,7 @@ SetStrokeOptions(CGContextRef cg, const StrokeOptions &aStrokeOptions)
class DrawTargetCG : public DrawTarget
{
public:
friend BorrowedCGContext;
DrawTargetCG();
virtual ~DrawTargetCG();