Use SkLazyPtr in SkPDFGraphicState.cpp

Motivation: currently we rely on mutexes for guarding static
initialization.  These mutexes will go away.

Review URL: https://codereview.chromium.org/815223005
This commit is contained in:
halcanary 2015-01-21 13:13:22 -08:00 committed by Commit bot
parent 28b44e8f27
commit e6ea244717
2 changed files with 47 additions and 35 deletions

View File

@ -6,6 +6,7 @@
*/
#include "SkData.h"
#include "SkLazyPtr.h"
#include "SkPDFCanon.h"
#include "SkPDFFormXObject.h"
#include "SkPDFGraphicState.h"
@ -144,12 +145,8 @@ SkPDFGraphicState* SkPDFGraphicState::GetGraphicStateForPaint(
return pdfGraphicState;
}
// static
SkPDFObject* SkPDFGraphicState::GetInvertFunction() {
// This assumes that canonicalPaintsMutex is held.
SkPDFCanon::GetPaintMutex().assertHeld();
static SkPDFStream* invertFunction = NULL;
if (!invertFunction) {
namespace {
SkPDFObject* create_invert_function() {
// Acrobat crashes if we use a type 0 function, kpdf crashes if we use
// a type 2 function, so we use a type 4 function.
SkAutoTUnref<SkPDFArray> domainAndRange(new SkPDFArray);
@ -162,20 +159,30 @@ SkPDFObject* SkPDFGraphicState::GetInvertFunction() {
SkAutoTUnref<SkData> psInvertStream(
SkData::NewWithoutCopy(psInvert, strlen(psInvert)));
invertFunction = new SkPDFStream(psInvertStream.get());
SkPDFStream* invertFunction = SkNEW_ARGS(
SkPDFStream, (psInvertStream.get()));
invertFunction->insertInt("FunctionType", 4);
invertFunction->insert("Domain", domainAndRange.get());
invertFunction->insert("Range", domainAndRange.get());
}
return invertFunction;
}
template <typename T> void unref(T* ptr) { ptr->unref(); }
} // namespace
SK_DECLARE_STATIC_LAZY_PTR(SkPDFObject, invertFunction,
create_invert_function, unref<SkPDFObject>);
// static
SkPDFObject* SkPDFGraphicState::GetInvertFunction() {
return invertFunction.get();
}
// static
SkPDFGraphicState* SkPDFGraphicState::GetSMaskGraphicState(
SkPDFFormXObject* sMask, bool invert, SkPDFSMaskMode sMaskMode) {
// The practical chances of using the same mask more than once are unlikely
// enough that it's not worth canonicalizing.
SkAutoMutexAcquire lock(SkPDFCanon::GetPaintMutex());
SkAutoTUnref<SkPDFDict> sMaskDict(new SkPDFDict("Mask"));
if (sMaskMode == kAlpha_SMaskMode) {
sMaskDict->insertName("S", "Alpha");
@ -202,21 +209,24 @@ SkPDFGraphicState* SkPDFGraphicState::GetSMaskGraphicState(
return result;
}
// static
SkPDFGraphicState* SkPDFGraphicState::GetNoSMaskGraphicState() {
SkAutoMutexAcquire lock(SkPDFCanon::GetPaintMutex());
static SkPDFGraphicState* noSMaskGS = NULL;
if (!noSMaskGS) {
noSMaskGS = new SkPDFGraphicState;
SkPDFGraphicState* SkPDFGraphicState::CreateNoSMaskGraphicState() {
SkPDFGraphicState* noSMaskGS = SkNEW(SkPDFGraphicState);
noSMaskGS->fPopulated = true;
noSMaskGS->fSMask = true;
noSMaskGS->insertName("Type", "ExtGState");
noSMaskGS->insertName("SMask", "None");
}
noSMaskGS->ref();
return noSMaskGS;
}
SK_DECLARE_STATIC_LAZY_PTR(
SkPDFGraphicState, noSMaskGraphicState,
SkPDFGraphicState::CreateNoSMaskGraphicState, unref<SkPDFGraphicState>);
// static
SkPDFGraphicState* SkPDFGraphicState::GetNoSMaskGraphicState() {
return SkRef(noSMaskGraphicState.get());
}
SkPDFGraphicState::SkPDFGraphicState()
: fPopulated(false),
fSMask(false) {
@ -270,4 +280,3 @@ void SkPDFGraphicState::populateDict() {
insertName("BM", as_blend_mode(xfermode));
}
}

View File

@ -73,6 +73,9 @@ public:
bool equals(const SkPaint&) const;
// Only public for SK_DECLARE_STATIC_LAZY_PTR
static SkPDFGraphicState* CreateNoSMaskGraphicState();
private:
const SkPaint fPaint;
SkTDArray<SkPDFObject*> fResources;