diff options
author | halcanary <halcanary@google.com> | 2015-01-21 09:59:14 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-01-21 09:59:14 -0800 |
commit | fb62b3d423fa34c672df42f47017dbef087d19e9 (patch) | |
tree | 014428ea2ca480f62b55d3d63db5115df577bff6 /src/pdf/SkPDFCanon.cpp | |
parent | 1c60dfe7ca0db010fa3118a1a2c7ff4c09136ab0 (diff) |
SkPDFCanon
SkPDFCanon's fields and methods will eventually become part of
SkPDFDocument/SkDocument_PDF. For now, it exists as a singleton to
preflight that transition. This replaces three global arrays in
SkPDFFont, SkPDFShader, and SkPDFGraphicsContext. This code is still
thread-unsafe (http://skbug.com/2683), but moving this functionality
into SkPDFDocument will fix that.
This CL does not change pdf output from either GMs or SKPs.
This change also simplifies some code around the SkPDFCanon methods.
BUG=skia:2683
Review URL: https://codereview.chromium.org/842253003
Diffstat (limited to 'src/pdf/SkPDFCanon.cpp')
-rw-r--r-- | src/pdf/SkPDFCanon.cpp | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/src/pdf/SkPDFCanon.cpp b/src/pdf/SkPDFCanon.cpp new file mode 100644 index 0000000000..31212f8c91 --- /dev/null +++ b/src/pdf/SkPDFCanon.cpp @@ -0,0 +1,136 @@ +/* + * Copyright 2015 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "SkLazyPtr.h" +#include "SkPDFCanon.h" +#include "SkPDFFont.h" +#include "SkPDFGraphicState.h" +#include "SkPDFShader.h" + +//////////////////////////////////////////////////////////////////////////////// + +SK_DECLARE_STATIC_MUTEX(gSkPDFCanonFontMutex); +SK_DECLARE_STATIC_MUTEX(gSkPDFCanonShaderMutex); +SK_DECLARE_STATIC_MUTEX(gSkPDFCanonPaintMutex); + +SkBaseMutex& SkPDFCanon::GetFontMutex() { return gSkPDFCanonFontMutex; } +SkBaseMutex& SkPDFCanon::GetShaderMutex() { return gSkPDFCanonShaderMutex; } +SkBaseMutex& SkPDFCanon::GetPaintMutex() { return gSkPDFCanonPaintMutex; } + +SkPDFCanon::SkPDFCanon() {} + +SkPDFCanon::~SkPDFCanon() {} + +SK_DECLARE_STATIC_LAZY_PTR(SkPDFCanon, singleton); + +SkPDFCanon& SkPDFCanon::GetCanon() { return *singleton.get(); } + +//////////////////////////////////////////////////////////////////////////////// + +static void assert_mutex_held(const SkPDFCanon* canon, SkBaseMutex& mutex) { + if (canon == singleton.get()) { + mutex.assertHeld(); + } +} + +SkPDFFont* SkPDFCanon::findFont(uint32_t fontID, + uint16_t glyphID, + SkPDFFont** relatedFontPtr) const { + assert_mutex_held(this, gSkPDFCanonFontMutex); + SkASSERT(relatedFontPtr); + + SkPDFFont* relatedFont = NULL; + for (int i = 0; i < fFontRecords.count(); ++i) { + SkPDFFont::Match match = SkPDFFont::IsMatch( + fFontRecords[i].fFont, fFontRecords[i].fFontID, + fFontRecords[i].fGlyphID, fontID, glyphID); + if (SkPDFFont::kExact_Match == match) { + return fFontRecords[i].fFont; + } else if (!relatedFont && SkPDFFont::kRelated_Match == match) { + relatedFont = fFontRecords[i].fFont; + } + } + *relatedFontPtr = relatedFont; // May still be NULL. + return NULL; +} + +void SkPDFCanon::addFont(SkPDFFont* font, uint32_t fontID, uint16_t fGlyphID) { + assert_mutex_held(this, gSkPDFCanonFontMutex); + SkPDFCanon::FontRec* rec = fFontRecords.push(); + rec->fFont = font; + rec->fFontID = fontID; + rec->fGlyphID = fGlyphID; +} + +void SkPDFCanon::removeFont(SkPDFFont* pdfFont) { + assert_mutex_held(this, gSkPDFCanonFontMutex); + for (int i = 0; i < fFontRecords.count(); i++) { + if (fFontRecords[i].fFont == pdfFont) { + fFontRecords.removeShuffle(i); + return; + } + } + // Not all SkPDFFonts are added to the Canon. +} + +//////////////////////////////////////////////////////////////////////////////// + +SkPDFShader* SkPDFCanon::findShader(const SkPDFShader::State& state) const { + assert_mutex_held(this, gSkPDFCanonShaderMutex); + for (int i = 0; i < fShaderRecords.count(); ++i) { + if (fShaderRecords[i]->equals(state)) { + return fShaderRecords[i]; + } + } + return NULL; +} + +void SkPDFCanon::addShader(SkPDFShader* shader) { + assert_mutex_held(this, gSkPDFCanonShaderMutex); + SkASSERT(shader); + fShaderRecords.push(shader); +} + +void SkPDFCanon::removeShader(SkPDFShader* pdfShader) { + assert_mutex_held(this, gSkPDFCanonShaderMutex); + for (int i = 0; i < fShaderRecords.count(); ++i) { + if (fShaderRecords[i] == pdfShader) { + fShaderRecords.removeShuffle(i); + return; + } + } + SkDEBUGFAIL("pdfShader not found"); +} + +//////////////////////////////////////////////////////////////////////////////// + +SkPDFGraphicState* SkPDFCanon::findGraphicState(const SkPaint& paint) const { + assert_mutex_held(this, gSkPDFCanonPaintMutex); + for (int i = 0; i < fGraphicStateRecords.count(); ++i) { + if (fGraphicStateRecords[i]->equals(paint)) { + return fGraphicStateRecords[i]; + } + } + return NULL; +} + +void SkPDFCanon::addGraphicState(SkPDFGraphicState* state) { + assert_mutex_held(this, gSkPDFCanonPaintMutex); + SkASSERT(state); + fGraphicStateRecords.push(state); +} + +void SkPDFCanon::removeGraphicState(SkPDFGraphicState* pdfGraphicState) { + assert_mutex_held(this, gSkPDFCanonPaintMutex); + for (int i = 0; i < fGraphicStateRecords.count(); ++i) { + if (fGraphicStateRecords[i] == pdfGraphicState) { + fGraphicStateRecords.removeShuffle(i); + return; + } + } + SkDEBUGFAIL("pdfGraphicState not found"); +} |