SkPDF: PDFShader code modernized.
Motivation: reduce code complexity.
SkCanon stores SkPDFShader::State next to SkDFObject, not inside.
many places use sk_sp<T> rather than T* to represent ownership.
SkPDFShader::State no longer holds bitmap.
SkPDFShader::State gets move constructor, no longer heap-allocated.
Classes removed:
SkPDFFunctionShader
SkPDFAlphaFunctionShader
SkPDFImageShader
BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2193973002
Review-Url: https://codereview.chromium.org/2193973002
diff --git a/src/pdf/SkPDFCanon.cpp b/src/pdf/SkPDFCanon.cpp
index 11bfb89..3dcf4e9 100644
--- a/src/pdf/SkPDFCanon.cpp
+++ b/src/pdf/SkPDFCanon.cpp
@@ -17,12 +17,13 @@
fFontRecords[i].fFont->unref();
}
fFontRecords.reset();
- fFunctionShaderRecords.unrefAll();
+
fFunctionShaderRecords.reset();
- fAlphaShaderRecords.unrefAll();
fAlphaShaderRecords.reset();
- fImageShaderRecords.unrefAll();
fImageShaderRecords.reset();
+
+ // TODO(halcanary): make SkTHashSet work nicely with sk_sp<>,
+ // or use std::unordered_set<>
fGraphicStateRecords.foreach ([](WrapGS w) { w.fPtr->unref(); });
fGraphicStateRecords.reset();
@@ -32,21 +33,6 @@
////////////////////////////////////////////////////////////////////////////////
-template <class T> T* assert_ptr(T* p) { SkASSERT(p); return p; }
-
-// requires `bool T::equals(const U&) const`
-template <typename T, typename U>
-T* find_item(const SkTDArray<T*>& ptrArray, const U& object) {
- for (int i = 0; i < ptrArray.count(); ++i) {
- if (ptrArray[i]->equals(object)) {
- return ptrArray[i];
- }
- }
- return nullptr;
-}
-
-////////////////////////////////////////////////////////////////////////////////
-
SkPDFFont* SkPDFCanon::findFont(uint32_t fontID,
uint16_t glyphID,
SkPDFFont** relatedFontPtr) const {
@@ -76,33 +62,43 @@
////////////////////////////////////////////////////////////////////////////////
-SkPDFFunctionShader* SkPDFCanon::findFunctionShader(
+template <typename T>
+sk_sp<SkPDFObject> find_shader(const SkTArray<T>& records,
+ const SkPDFShader::State& state) {
+ for (const T& record : records) {
+ if (record.fShaderState == state) {
+ return record.fShaderObject;
+ }
+ }
+ return nullptr;
+}
+
+sk_sp<SkPDFObject> SkPDFCanon::findFunctionShader(
const SkPDFShader::State& state) const {
- return find_item(fFunctionShaderRecords, state);
+ return find_shader(fFunctionShaderRecords, state);
}
-void SkPDFCanon::addFunctionShader(SkPDFFunctionShader* pdfShader) {
- fFunctionShaderRecords.push(SkRef(pdfShader));
+void SkPDFCanon::addFunctionShader(sk_sp<SkPDFObject> pdfShader,
+ SkPDFShader::State state) {
+ fFunctionShaderRecords.emplace_back(std::move(state), std::move(pdfShader));
}
-////////////////////////////////////////////////////////////////////////////////
-
-SkPDFAlphaFunctionShader* SkPDFCanon::findAlphaShader(
+sk_sp<SkPDFObject> SkPDFCanon::findAlphaShader(
const SkPDFShader::State& state) const {
- return find_item(fAlphaShaderRecords, state);
+ return find_shader(fAlphaShaderRecords, state);
}
-void SkPDFCanon::addAlphaShader(SkPDFAlphaFunctionShader* pdfShader) {
- fAlphaShaderRecords.push(SkRef(pdfShader));
+void SkPDFCanon::addAlphaShader(sk_sp<SkPDFObject> pdfShader,
+ SkPDFShader::State state) {
+ fAlphaShaderRecords.emplace_back(std::move(state), std::move(pdfShader));
}
-////////////////////////////////////////////////////////////////////////////////
-
-SkPDFImageShader* SkPDFCanon::findImageShader(
+sk_sp<SkPDFObject> SkPDFCanon::findImageShader(
const SkPDFShader::State& state) const {
- return find_item(fImageShaderRecords, state);
+ return find_shader(fImageShaderRecords, state);
}
-void SkPDFCanon::addImageShader(SkPDFImageShader* pdfShader) {
- fImageShaderRecords.push(SkRef(pdfShader));
+void SkPDFCanon::addImageShader(sk_sp<SkPDFObject> pdfShader,
+ SkPDFShader::State state) {
+ fImageShaderRecords.emplace_back(std::move(state), std::move(pdfShader));
}
////////////////////////////////////////////////////////////////////////////////