halcanary | fb62b3d | 2015-01-21 09:59:14 -0800 | [diff] [blame] | 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 | #ifndef SkPDFCanon_DEFINED |
| 8 | #define SkPDFCanon_DEFINED |
| 9 | |
halcanary | be27a11 | 2015-04-01 13:31:19 -0700 | [diff] [blame] | 10 | #include "SkPDFGraphicState.h" |
halcanary | fb62b3d | 2015-01-21 09:59:14 -0800 | [diff] [blame] | 11 | #include "SkPDFShader.h" |
halcanary | fb62b3d | 2015-01-21 09:59:14 -0800 | [diff] [blame] | 12 | #include "SkTDArray.h" |
halcanary | be27a11 | 2015-04-01 13:31:19 -0700 | [diff] [blame] | 13 | #include "SkTHash.h" |
halcanary | fb62b3d | 2015-01-21 09:59:14 -0800 | [diff] [blame] | 14 | |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 15 | class SkBitmap; |
halcanary | fb62b3d | 2015-01-21 09:59:14 -0800 | [diff] [blame] | 16 | class SkPDFFont; |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 17 | class SkPDFBitmap; |
halcanary | fb62b3d | 2015-01-21 09:59:14 -0800 | [diff] [blame] | 18 | class SkPaint; |
halcanary | fb62b3d | 2015-01-21 09:59:14 -0800 | [diff] [blame] | 19 | |
halcanary | 792c80f | 2015-02-20 07:21:05 -0800 | [diff] [blame] | 20 | /** |
| 21 | * The SkPDFCanon canonicalizes objects across PDF pages(SkPDFDevices). |
| 22 | * |
| 23 | * The PDF backend works correctly if: |
| 24 | * - There is no more than one SkPDFCanon for each thread. |
| 25 | * - Every SkPDFDevice is given a pointer to a SkPDFCanon on creation. |
| 26 | * - All SkPDFDevices in a document share the same SkPDFCanon. |
| 27 | * The SkDocument_PDF class makes this happen by owning a single |
| 28 | * SkPDFCanon. |
| 29 | * |
halcanary | 2e3f9d8 | 2015-02-27 12:41:03 -0800 | [diff] [blame] | 30 | * The addFoo() methods will ref the Foo; the canon's destructor will |
| 31 | * call foo->unref() on all of these objects. |
| 32 | * |
| 33 | * The findFoo() methods do not change the ref count of the Foo |
| 34 | * objects. |
halcanary | 792c80f | 2015-02-20 07:21:05 -0800 | [diff] [blame] | 35 | */ |
halcanary | fb62b3d | 2015-01-21 09:59:14 -0800 | [diff] [blame] | 36 | class SkPDFCanon : SkNoncopyable { |
| 37 | public: |
halcanary | 2e3f9d8 | 2015-02-27 12:41:03 -0800 | [diff] [blame] | 38 | ~SkPDFCanon() { this->reset(); } |
| 39 | |
| 40 | // reset to original setting, unrefs all objects. |
| 41 | void reset(); |
halcanary | fb62b3d | 2015-01-21 09:59:14 -0800 | [diff] [blame] | 42 | |
halcanary | fb62b3d | 2015-01-21 09:59:14 -0800 | [diff] [blame] | 43 | // Returns exact match if there is one. If not, it returns NULL. |
| 44 | // If there is no exact match, but there is a related font, we |
| 45 | // still return NULL, but also set *relatedFont. |
| 46 | SkPDFFont* findFont(uint32_t fontID, |
| 47 | uint16_t glyphID, |
| 48 | SkPDFFont** relatedFont) const; |
| 49 | void addFont(SkPDFFont* font, uint32_t fontID, uint16_t fGlyphID); |
halcanary | fb62b3d | 2015-01-21 09:59:14 -0800 | [diff] [blame] | 50 | |
halcanary | 530ea8e | 2015-01-23 06:17:35 -0800 | [diff] [blame] | 51 | SkPDFFunctionShader* findFunctionShader(const SkPDFShader::State&) const; |
| 52 | void addFunctionShader(SkPDFFunctionShader*); |
halcanary | 530ea8e | 2015-01-23 06:17:35 -0800 | [diff] [blame] | 53 | |
| 54 | SkPDFAlphaFunctionShader* findAlphaShader(const SkPDFShader::State&) const; |
| 55 | void addAlphaShader(SkPDFAlphaFunctionShader*); |
halcanary | 530ea8e | 2015-01-23 06:17:35 -0800 | [diff] [blame] | 56 | |
| 57 | SkPDFImageShader* findImageShader(const SkPDFShader::State&) const; |
| 58 | void addImageShader(SkPDFImageShader*); |
halcanary | fb62b3d | 2015-01-21 09:59:14 -0800 | [diff] [blame] | 59 | |
halcanary | be27a11 | 2015-04-01 13:31:19 -0700 | [diff] [blame] | 60 | const SkPDFGraphicState* findGraphicState(const SkPDFGraphicState&) const; |
| 61 | void addGraphicState(const SkPDFGraphicState*); |
halcanary | fb62b3d | 2015-01-21 09:59:14 -0800 | [diff] [blame] | 62 | |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 63 | SkPDFBitmap* findBitmap(const SkBitmap&) const; |
| 64 | void addBitmap(SkPDFBitmap*); |
halcanary | a1f1ee9 | 2015-02-20 06:17:26 -0800 | [diff] [blame] | 65 | |
halcanary | fb62b3d | 2015-01-21 09:59:14 -0800 | [diff] [blame] | 66 | private: |
| 67 | struct FontRec { |
| 68 | SkPDFFont* fFont; |
| 69 | uint32_t fFontID; |
| 70 | uint16_t fGlyphID; |
| 71 | }; |
| 72 | SkTDArray<FontRec> fFontRecords; |
| 73 | |
halcanary | 530ea8e | 2015-01-23 06:17:35 -0800 | [diff] [blame] | 74 | SkTDArray<SkPDFFunctionShader*> fFunctionShaderRecords; |
| 75 | |
| 76 | SkTDArray<SkPDFAlphaFunctionShader*> fAlphaShaderRecords; |
| 77 | |
| 78 | SkTDArray<SkPDFImageShader*> fImageShaderRecords; |
halcanary | fb62b3d | 2015-01-21 09:59:14 -0800 | [diff] [blame] | 79 | |
halcanary | be27a11 | 2015-04-01 13:31:19 -0700 | [diff] [blame] | 80 | struct WrapGS { |
| 81 | explicit WrapGS(const SkPDFGraphicState* ptr = NULL) : fPtr(ptr) {} |
| 82 | const SkPDFGraphicState* fPtr; |
| 83 | bool operator==(const WrapGS& rhs) const { |
| 84 | SkASSERT(fPtr); |
| 85 | SkASSERT(rhs.fPtr); |
| 86 | return *fPtr == *rhs.fPtr; |
| 87 | } |
| 88 | static uint32_t Hash(const WrapGS& w) { |
| 89 | SkASSERT(w.fPtr); |
| 90 | return w.fPtr->hash(); |
| 91 | } |
| 92 | }; |
| 93 | SkTHashSet<WrapGS, WrapGS::Hash> fGraphicStateRecords; |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 94 | |
| 95 | SkTDArray<SkPDFBitmap*> fBitmapRecords; |
halcanary | fb62b3d | 2015-01-21 09:59:14 -0800 | [diff] [blame] | 96 | }; |
| 97 | #endif // SkPDFCanon_DEFINED |