• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006 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 SkFlattenable_DEFINED
18 #define SkFlattenable_DEFINED
19 
20 #include "SkRefCnt.h"
21 #include "SkBitmap.h"
22 #include "SkReader32.h"
23 #include "SkTDArray.h"
24 #include "SkWriter32.h"
25 
26 class SkFlattenableReadBuffer;
27 class SkFlattenableWriteBuffer;
28 class SkString;
29 
30 /** \class SkFlattenable
31 
32  SkFlattenable is the base class for objects that need to be flattened
33  into a data stream for either transport or as part of the key to the
34  font cache.
35  */
36 class SkFlattenable : public SkRefCnt {
37 public:
38     typedef SkFlattenable* (*Factory)(SkFlattenableReadBuffer&);
39 
SkFlattenable()40     SkFlattenable() {}
41 
42     /** Implement this to return a factory function pointer that can be called
43      to recreate your class given a buffer (previously written to by your
44      override of flatten().
45      */
46     virtual Factory getFactory() = 0;
47     /** Override this to write data specific to your subclass into the buffer,
48      being sure to call your super-class' version first. This data will later
49      be passed to your Factory function, returned by getFactory().
50      */
51     virtual void flatten(SkFlattenableWriteBuffer&);
52 
53     /** Set the string to describe the sublass and return true. If this is not
54         overridden, ignore the string param and return false.
55      */
56     virtual bool toDumpString(SkString*) const;
57 
58     static Factory NameToFactory(const char name[]);
59     static const char* FactoryToName(Factory);
60     static void Register(const char name[], Factory);
61 
62     class Registrar {
63     public:
Registrar(const char name[],Factory factory)64         Registrar(const char name[], Factory factory) {
65             SkFlattenable::Register(name, factory);
66         }
67     };
68 
69 protected:
SkFlattenable(SkFlattenableReadBuffer &)70     SkFlattenable(SkFlattenableReadBuffer&) {}
71 };
72 
73 ///////////////////////////////////////////////////////////////////////////////
74 ///////////////////////////////////////////////////////////////////////////////
75 
76 class SkTypeface;
77 
78 class SkFlattenableReadBuffer : public SkReader32 {
79 public:
80     SkFlattenableReadBuffer();
81     explicit SkFlattenableReadBuffer(const void* data);
82     SkFlattenableReadBuffer(const void* data, size_t size);
83 
setRefCntArray(SkRefCnt * array[],int count)84     void setRefCntArray(SkRefCnt* array[], int count) {
85         fRCArray = array;
86         fRCCount = count;
87     }
88 
setTypefaceArray(SkTypeface * array[],int count)89     void setTypefaceArray(SkTypeface* array[], int count) {
90         fTFArray = array;
91         fTFCount = count;
92     }
93 
setFactoryPlayback(SkFlattenable::Factory array[],int count)94     void setFactoryPlayback(SkFlattenable::Factory array[], int count) {
95         fFactoryArray = array;
96         fFactoryCount = count;
97     }
98 
99     SkTypeface* readTypeface();
100     SkRefCnt* readRefCnt();
101     void* readFunctionPtr();
102     SkFlattenable* readFlattenable();
103 
104 private:
105     SkRefCnt** fRCArray;
106     int        fRCCount;
107 
108     SkTypeface** fTFArray;
109     int        fTFCount;
110 
111     SkFlattenable::Factory* fFactoryArray;
112     int                     fFactoryCount;
113 
114     typedef SkReader32 INHERITED;
115 };
116 
117 ///////////////////////////////////////////////////////////////////////////////
118 
119 #include "SkPtrRecorder.h"
120 
121 class SkRefCntRecorder : public SkPtrRecorder {
122 public:
123     virtual ~SkRefCntRecorder();
124 
125     /** Add a refcnt object to the set and ref it if not already present,
126         or if it is already present, do nothing. Either way, returns 0 if obj
127         is null, or a base-1 index if obj is not null.
128     */
record(SkRefCnt * ref)129     uint32_t record(SkRefCnt* ref) {
130         return this->recordPtr(ref);
131     }
132 
133     // This does not change the owner counts on the objects
get(SkRefCnt * array[])134     void get(SkRefCnt* array[]) const {
135         this->getPtrs((void**)array);
136     }
137 
138 protected:
139     // overrides
140     virtual void incPtr(void*);
141     virtual void decPtr(void*);
142 
143 private:
144     typedef SkPtrRecorder INHERITED;
145 };
146 
147 class SkFactoryRecorder : public SkPtrRecorder {
148 public:
149     /** Add a factory to the set. If it is null return 0, otherwise return a
150         base-1 index for the factory.
151     */
record(SkFlattenable::Factory fact)152     uint32_t record(SkFlattenable::Factory fact) {
153         return this->recordPtr((void*)fact);
154     }
155 
get(SkFlattenable::Factory array[])156     void get(SkFlattenable::Factory array[]) const {
157         this->getPtrs((void**)array);
158     }
159 
160 private:
161     typedef SkPtrRecorder INHERITED;
162 };
163 
164 class SkFlattenableWriteBuffer : public SkWriter32 {
165 public:
166     SkFlattenableWriteBuffer(size_t minSize);
167     virtual ~SkFlattenableWriteBuffer();
168 
169     void writeTypeface(SkTypeface*);
170     void writeRefCnt(SkRefCnt*);
171     void writeFunctionPtr(void*);
172     void writeFlattenable(SkFlattenable* flattenable);
173 
getTypefaceRecorder()174     SkRefCntRecorder* getTypefaceRecorder() const { return fTFRecorder; }
175     SkRefCntRecorder* setTypefaceRecorder(SkRefCntRecorder*);
176 
getRefCntRecorder()177     SkRefCntRecorder* getRefCntRecorder() const { return fRCRecorder; }
178     SkRefCntRecorder* setRefCntRecorder(SkRefCntRecorder*);
179 
getFactoryRecorder()180     SkFactoryRecorder* getFactoryRecorder() const { return fFactoryRecorder; }
181     SkFactoryRecorder* setFactoryRecorder(SkFactoryRecorder*);
182 
183     enum Flags {
184         kCrossProcess_Flag      = 0x01
185     };
getFlags()186     Flags getFlags() const { return fFlags; }
setFlags(Flags flags)187     void setFlags(Flags flags) { fFlags = flags; }
188 
isCrossProcess()189     bool isCrossProcess() const { return (fFlags & kCrossProcess_Flag) != 0; }
190 
persistBitmapPixels()191     bool persistBitmapPixels() const {
192         return (fFlags & kCrossProcess_Flag) != 0;
193     }
194 
persistTypeface()195     bool persistTypeface() const { return (fFlags & kCrossProcess_Flag) != 0; }
196 
197 private:
198     Flags               fFlags;
199     SkRefCntRecorder*   fTFRecorder;
200     SkRefCntRecorder*   fRCRecorder;
201     SkFactoryRecorder*  fFactoryRecorder;
202 
203     typedef SkWriter32 INHERITED;
204 };
205 
206 #endif
207 
208