vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef SkPDFGraphicState_DEFINED |
| 18 | #define SkPDFGraphicState_DEFINED |
| 19 | |
| 20 | #include "SkPaint.h" |
| 21 | #include "SkPDFTypes.h" |
| 22 | #include "SkTemplates.h" |
| 23 | #include "SkThread.h" |
| 24 | |
| 25 | /** \class SkPDFGraphicState |
| 26 | SkPaint objects roughly correspond to graphic state dictionaries that can |
| 27 | be installed. So that a given dictionary is only output to the pdf file |
| 28 | once, we want to canonicalize them. Static methods in this class manage |
| 29 | a weakly referenced set of SkPDFGraphicState objects: when the last |
| 30 | reference to a SkPDFGraphicState is removed, it removes itself from the |
| 31 | static set of objects. |
| 32 | |
| 33 | */ |
| 34 | class SkPDFGraphicState : public SkPDFDict { |
| 35 | public: |
| 36 | virtual ~SkPDFGraphicState(); |
| 37 | |
| 38 | // Override emitObject and getOutputSize so that we can populate |
| 39 | // the dictionary on demand. |
| 40 | virtual void emitObject(SkWStream* stream, SkPDFCatalog* catalog, |
| 41 | bool indirect); |
| 42 | virtual size_t getOutputSize(SkPDFCatalog* catalog, bool indirect); |
| 43 | |
| 44 | /** Get the graphic state for the passed SkPaint. The reference count of |
| 45 | * the object is incremented and it is the caller's responsibility to |
| 46 | * unreference it when done. This is needed to accommodate the weak |
| 47 | * reference pattern used when the returned object is new and has no |
| 48 | * other references. |
| 49 | * @param paint The SkPaint to emulate. |
| 50 | */ |
| 51 | static SkPDFGraphicState* getGraphicStateForPaint(const SkPaint& paint); |
| 52 | |
| 53 | private: |
| 54 | const SkPaint fPaint; |
| 55 | bool fPopulated; |
| 56 | |
| 57 | class GSCanonicalEntry { |
| 58 | public: |
| 59 | SkPDFGraphicState* fGraphicState; |
| 60 | const SkPaint* fPaint; |
| 61 | |
| 62 | bool operator==(const GSCanonicalEntry& b) const; |
| 63 | explicit GSCanonicalEntry(SkPDFGraphicState* gs) |
| 64 | : fGraphicState(gs), |
| 65 | fPaint(&gs->fPaint) {} |
| 66 | explicit GSCanonicalEntry(const SkPaint* paint) : fPaint(paint) {} |
| 67 | }; |
| 68 | |
| 69 | // This should be made a hash table if performance is a problem. |
| 70 | static SkTDArray<GSCanonicalEntry>& canonicalPaints(); |
| 71 | static SkMutex& canonicalPaintsMutex(); |
| 72 | |
| 73 | explicit SkPDFGraphicState(const SkPaint& paint); |
| 74 | |
| 75 | void populateDict(); |
| 76 | |
| 77 | static int find(const SkPaint& paint); |
| 78 | }; |
| 79 | |
| 80 | #endif |