epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 1 | |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 3 | * Copyright 2010 The Android Open Source Project |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 9 | |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 10 | #ifndef SkPDFGraphicState_DEFINED |
| 11 | #define SkPDFGraphicState_DEFINED |
| 12 | |
| 13 | #include "SkPaint.h" |
| 14 | #include "SkPDFTypes.h" |
| 15 | #include "SkTemplates.h" |
| 16 | #include "SkThread.h" |
| 17 | |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 18 | class SkPDFFormXObject; |
| 19 | |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 20 | /** \class SkPDFGraphicState |
| 21 | SkPaint objects roughly correspond to graphic state dictionaries that can |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 22 | be installed. So that a given dictionary is only output to the pdf file |
| 23 | once, we want to canonicalize them. Static methods in this class manage |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 24 | a weakly referenced set of SkPDFGraphicState objects: when the last |
| 25 | reference to a SkPDFGraphicState is removed, it removes itself from the |
| 26 | static set of objects. |
| 27 | |
| 28 | */ |
| 29 | class SkPDFGraphicState : public SkPDFDict { |
| 30 | public: |
| 31 | virtual ~SkPDFGraphicState(); |
| 32 | |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 33 | virtual void getResources(SkTDArray<SkPDFObject*>* resourceList); |
| 34 | |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 35 | // Override emitObject and getOutputSize so that we can populate |
| 36 | // the dictionary on demand. |
| 37 | virtual void emitObject(SkWStream* stream, SkPDFCatalog* catalog, |
| 38 | bool indirect); |
| 39 | virtual size_t getOutputSize(SkPDFCatalog* catalog, bool indirect); |
| 40 | |
| 41 | /** Get the graphic state for the passed SkPaint. The reference count of |
| 42 | * the object is incremented and it is the caller's responsibility to |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 43 | * unreference it when done. This is needed to accommodate the weak |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 44 | * reference pattern used when the returned object is new and has no |
| 45 | * other references. |
| 46 | * @param paint The SkPaint to emulate. |
| 47 | */ |
reed@google.com | f6c3ebd | 2011-07-20 17:20:28 +0000 | [diff] [blame] | 48 | static SkPDFGraphicState* GetGraphicStateForPaint(const SkPaint& paint); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 49 | |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 50 | /** Make a graphic state that only sets the passed soft mask. The |
| 51 | * reference count of the object is incremented and it is the caller's |
| 52 | * responsibility to unreference it when done. |
| 53 | * @param sMask The form xobject to use as a soft mask. |
| 54 | * @param invert Indicates if the alpha of the sMask should be inverted. |
| 55 | */ |
reed@google.com | f6c3ebd | 2011-07-20 17:20:28 +0000 | [diff] [blame] | 56 | static SkPDFGraphicState* GetSMaskGraphicState(SkPDFFormXObject* sMask, |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 57 | bool invert); |
| 58 | |
| 59 | /** Get a graphic state that only unsets the soft mask. The reference |
| 60 | * count of the object is incremented and it is the caller's responsibility |
| 61 | * to unreference it when done. This is needed to accommodate the weak |
| 62 | * reference pattern used when the returned object is new and has no |
| 63 | * other references. |
| 64 | */ |
reed@google.com | f6c3ebd | 2011-07-20 17:20:28 +0000 | [diff] [blame] | 65 | static SkPDFGraphicState* GetNoSMaskGraphicState(); |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 66 | |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 67 | private: |
| 68 | const SkPaint fPaint; |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 69 | SkTDArray<SkPDFObject*> fResources; |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 70 | bool fPopulated; |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 71 | bool fSMask; |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 72 | |
| 73 | class GSCanonicalEntry { |
| 74 | public: |
| 75 | SkPDFGraphicState* fGraphicState; |
| 76 | const SkPaint* fPaint; |
| 77 | |
| 78 | bool operator==(const GSCanonicalEntry& b) const; |
| 79 | explicit GSCanonicalEntry(SkPDFGraphicState* gs) |
| 80 | : fGraphicState(gs), |
| 81 | fPaint(&gs->fPaint) {} |
vandebo@chromium.org | 7332207 | 2011-06-21 21:19:41 +0000 | [diff] [blame] | 82 | explicit GSCanonicalEntry(const SkPaint* paint) |
| 83 | : fGraphicState(NULL), |
| 84 | fPaint(paint) {} |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | // This should be made a hash table if performance is a problem. |
reed@google.com | f6c3ebd | 2011-07-20 17:20:28 +0000 | [diff] [blame] | 88 | static SkTDArray<GSCanonicalEntry>& CanonicalPaints(); |
| 89 | static SkMutex& CanonicalPaintsMutex(); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 90 | |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 91 | SkPDFGraphicState(); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 92 | explicit SkPDFGraphicState(const SkPaint& paint); |
| 93 | |
| 94 | void populateDict(); |
| 95 | |
vandebo@chromium.org | 19e3c1e | 2011-05-25 00:41:30 +0000 | [diff] [blame] | 96 | static SkPDFObject* GetInvertFunction(); |
| 97 | |
reed@google.com | f6c3ebd | 2011-07-20 17:20:28 +0000 | [diff] [blame] | 98 | static int Find(const SkPaint& paint); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | #endif |