OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright 2015 Google Inc. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license that can be | |
5 * found in the LICENSE file. | |
6 */ | |
7 | |
8 #include "SkLazyPtr.h" | |
9 #include "SkPDFCanon.h" | |
10 #include "SkPDFFont.h" | |
11 #include "SkPDFGraphicState.h" | |
12 #include "SkPDFShader.h" | |
13 | |
14 //////////////////////////////////////////////////////////////////////////////// | |
15 | |
16 SK_DECLARE_STATIC_MUTEX(gSkPDFCanonFontMutex); | |
17 SK_DECLARE_STATIC_MUTEX(gSkPDFCanonShaderMutex); | |
18 SK_DECLARE_STATIC_MUTEX(gSkPDFCanonPaintMutex); | |
19 | |
20 SkBaseMutex& SkPDFCanon::GetFontMutex() { return gSkPDFCanonFontMutex; } | |
21 SkBaseMutex& SkPDFCanon::GetShaderMutex() { return gSkPDFCanonShaderMutex; } | |
22 SkBaseMutex& SkPDFCanon::GetPaintMutex() { return gSkPDFCanonPaintMutex; } | |
23 | |
24 SkPDFCanon::SkPDFCanon() {} | |
25 | |
26 SkPDFCanon::~SkPDFCanon() {} | |
27 | |
28 SK_DECLARE_STATIC_LAZY_PTR(SkPDFCanon, singleton); | |
29 | |
30 SkPDFCanon& SkPDFCanon::GetCanon() { return *singleton.get(); } | |
31 | |
32 //////////////////////////////////////////////////////////////////////////////// | |
33 | |
34 static void assert_mutex_held(const SkPDFCanon* canon, SkBaseMutex& mutex) { | |
35 if (canon == singleton.get()) { | |
36 mutex.assertHeld(); | |
37 } | |
38 } | |
39 | |
40 SkPDFFont* SkPDFCanon::findFont(uint32_t fontID, | |
41 uint16_t glyphID, | |
42 SkPDFFont** relatedFontPtr) const { | |
43 assert_mutex_held(this, gSkPDFCanonFontMutex); | |
44 SkASSERT(relatedFontPtr); | |
45 | |
46 SkPDFFont* relatedFont = NULL; | |
47 for (int i = 0; i < fFontRecords.count(); ++i) { | |
48 SkPDFFont::Match match = SkPDFFont::IsMatch( | |
49 fFontRecords[i].fFont, fFontRecords[i].fFontID, | |
50 fFontRecords[i].fGlyphID, fontID, glyphID); | |
51 if (SkPDFFont::kExact_Match == match) { | |
52 return fFontRecords[i].fFont; | |
53 } else if (!relatedFont && SkPDFFont::kRelated_Match == match) { | |
54 relatedFont = fFontRecords[i].fFont; | |
55 } | |
56 } | |
57 *relatedFontPtr = relatedFont; // May still be NULL. | |
58 return NULL; | |
59 } | |
60 | |
61 void SkPDFCanon::addFont(SkPDFFont* font, uint32_t fontID, uint16_t fGlyphID) { | |
62 assert_mutex_held(this, gSkPDFCanonFontMutex); | |
63 SkPDFCanon::FontRec* rec = fFontRecords.push(); | |
64 rec->fFont = font; | |
65 rec->fFontID = fontID; | |
66 rec->fGlyphID = fGlyphID; | |
67 } | |
68 | |
69 void SkPDFCanon::removeFont(SkPDFFont* pdfFont) { | |
70 assert_mutex_held(this, gSkPDFCanonFontMutex); | |
71 for (int i = 0; i < fFontRecords.count(); i++) { | |
72 if (fFontRecords[i].fFont == pdfFont) { | |
73 fFontRecords.removeShuffle(i); | |
74 return; | |
75 } | |
76 } | |
77 } | |
78 | |
79 //////////////////////////////////////////////////////////////////////////////// | |
80 | |
81 SkPDFShader* SkPDFCanon::findShader(const SkPDFShader::State& state) const { | |
82 assert_mutex_held(this, gSkPDFCanonShaderMutex); | |
83 for (int i = 0; i < fShaderRecords.count(); ++i) { | |
84 if (fShaderRecords[i]->equals(state)) { | |
85 return fShaderRecords[i]; | |
86 } | |
87 } | |
88 return NULL; | |
89 } | |
90 | |
91 void SkPDFCanon::addShader(SkPDFShader* shader) { | |
92 assert_mutex_held(this, gSkPDFCanonShaderMutex); | |
93 SkASSERT(shader); | |
94 fShaderRecords.push(shader); | |
95 } | |
96 | |
97 void SkPDFCanon::removeShader(SkPDFShader* pdfShader) { | |
98 assert_mutex_held(this, gSkPDFCanonShaderMutex); | |
99 for (int i = 0; i < fShaderRecords.count(); ++i) { | |
100 if (fShaderRecords[i] == pdfShader) { | |
101 fShaderRecords.removeShuffle(i); | |
102 return; | |
103 } | |
104 } | |
105 SkDEBUGFAIL("pdfShader not found"); | |
mtklein
2015/01/21 17:24:38
leftover, or something we should add to removeFont
hal.canary
2015/01/21 17:45:30
I noted the reason why it was left out as a commen
| |
106 } | |
107 | |
108 //////////////////////////////////////////////////////////////////////////////// | |
109 | |
110 SkPDFGraphicState* SkPDFCanon::findGraphicState(const SkPaint& paint) const { | |
111 assert_mutex_held(this, gSkPDFCanonPaintMutex); | |
112 for (int i = 0; i < fGraphicStateRecords.count(); ++i) { | |
113 if (fGraphicStateRecords[i]->equals(paint)) { | |
114 return fGraphicStateRecords[i]; | |
115 } | |
116 } | |
117 return NULL; | |
118 } | |
119 | |
120 void SkPDFCanon::addGraphicState(SkPDFGraphicState* state) { | |
121 assert_mutex_held(this, gSkPDFCanonPaintMutex); | |
122 SkASSERT(state); | |
123 fGraphicStateRecords.push(state); | |
124 } | |
125 | |
126 void SkPDFCanon::removeGraphicState(SkPDFGraphicState* pdfGraphicState) { | |
127 assert_mutex_held(this, gSkPDFCanonPaintMutex); | |
128 for (int i = 0; i < fGraphicStateRecords.count(); ++i) { | |
129 if (fGraphicStateRecords[i] == pdfGraphicState) { | |
130 fGraphicStateRecords.removeShuffle(i); | |
131 return; | |
132 } | |
133 } | |
134 SkDEBUGFAIL("pdfGraphicState not found"); | |
135 } | |
OLD | NEW |