Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(388)

Side by Side Diff: src/pdf/SkPDFTypes.h

Issue 1790023003: SkPDF: add drop() virtual to release resources early. (Closed) Base URL: https://skia.googlesource.com/skia.git@skpdf-global
Patch Set: Record move fns Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2010 The Android Open Source Project 2 * Copyright 2010 The Android Open Source Project
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 8
9 #ifndef SkPDFTypes_DEFINED 9 #ifndef SkPDFTypes_DEFINED
10 #define SkPDFTypes_DEFINED 10 #define SkPDFTypes_DEFINED
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 const SkPDFSubstituteMap& substitutes) const = 0; 44 const SkPDFSubstituteMap& substitutes) const = 0;
45 45
46 /** 46 /**
47 * Adds all transitive dependencies of this object to the 47 * Adds all transitive dependencies of this object to the
48 * catalog. Implementations should respect the catalog's object 48 * catalog. Implementations should respect the catalog's object
49 * substitution map. 49 * substitution map.
50 */ 50 */
51 virtual void addResources(SkPDFObjNumMap* catalog, 51 virtual void addResources(SkPDFObjNumMap* catalog,
52 const SkPDFSubstituteMap& substitutes) const {} 52 const SkPDFSubstituteMap& substitutes) const {}
53 53
54 /**
55 * Release all resources associated with this SkPDFObject. It is
56 * an error to call emitObject() or addResources() after calling
57 * dump().
58 */
59 virtual void dump() {}
mtklein 2016/03/18 14:03:26 We usually use dump() to mean, print this to stdou
tomhudson 2016/03/18 14:11:54 Yes! I was concerned but didn't have a better idea
hal.canary 2016/03/18 21:18:07 done. I like drop better.
hal.canary 2016/03/18 21:18:07 Done.
60
54 private: 61 private:
55 typedef SkRefCnt INHERITED; 62 typedef SkRefCnt INHERITED;
56 }; 63 };
57 64
58 //////////////////////////////////////////////////////////////////////////////// 65 ////////////////////////////////////////////////////////////////////////////////
59 66
60 /** 67 /**
61 A SkPDFUnion is a non-virtualized implementation of the 68 A SkPDFUnion is a non-virtualized implementation of the
62 non-compound, non-specialized PDF Object types: Name, String, 69 non-compound, non-specialized PDF Object types: Name, String,
63 Number, Boolean. 70 Number, Boolean.
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
171 #endif // 0 178 #endif // 0
172 179
173 //////////////////////////////////////////////////////////////////////////////// 180 ////////////////////////////////////////////////////////////////////////////////
174 181
175 /** \class SkPDFArray 182 /** \class SkPDFArray
176 183
177 An array object in a PDF. 184 An array object in a PDF.
178 */ 185 */
179 class SkPDFArray final : public SkPDFObject { 186 class SkPDFArray final : public SkPDFObject {
180 public: 187 public:
181 static const int kMaxLen = 8191;
182
183 /** Create a PDF array. Maximum length is 8191. 188 /** Create a PDF array. Maximum length is 8191.
184 */ 189 */
185 SkPDFArray(); 190 SkPDFArray();
186 virtual ~SkPDFArray(); 191 virtual ~SkPDFArray();
187 192
188 // The SkPDFObject interface. 193 // The SkPDFObject interface.
189 void emitObject(SkWStream* stream, 194 void emitObject(SkWStream* stream,
190 const SkPDFObjNumMap& objNumMap, 195 const SkPDFObjNumMap& objNumMap,
191 const SkPDFSubstituteMap& substitutes) const override; 196 const SkPDFSubstituteMap& substitutes) const override;
192 void addResources(SkPDFObjNumMap*, 197 void addResources(SkPDFObjNumMap*,
193 const SkPDFSubstituteMap&) const override; 198 const SkPDFSubstituteMap&) const override;
199 void dump() override;
194 200
195 /** The size of the array. 201 /** The size of the array.
196 */ 202 */
197 int size() const; 203 int size() const;
198 204
199 /** Preallocate space for the given number of entries. 205 /** Preallocate space for the given number of entries.
200 * @param length The number of array slots to preallocate. 206 * @param length The number of array slots to preallocate.
201 */ 207 */
202 void reserve(int length); 208 void reserve(int length);
203 209
204 /** Appends a value to the end of the array. 210 /** Appends a value to the end of the array.
205 * @param value The value to add to the array. 211 * @param value The value to add to the array.
206 */ 212 */
207 void appendInt(int32_t); 213 void appendInt(int32_t);
208 void appendBool(bool); 214 void appendBool(bool);
209 void appendScalar(SkScalar); 215 void appendScalar(SkScalar);
210 void appendName(const char[]); 216 void appendName(const char[]);
211 void appendName(const SkString&); 217 void appendName(const SkString&);
212 void appendString(const char[]); 218 void appendString(const char[]);
213 void appendString(const SkString&); 219 void appendString(const SkString&);
214 void appendObject(sk_sp<SkPDFObject>); 220 void appendObject(sk_sp<SkPDFObject>);
215 void appendObjRef(sk_sp<SkPDFObject>); 221 void appendObjRef(sk_sp<SkPDFObject>);
216 222
217 private: 223 private:
218 SkTDArray<SkPDFUnion> fValues; 224 SkTArray<SkPDFUnion> fValues;
219 void append(SkPDFUnion&& value); 225 void append(SkPDFUnion&& value);
220 typedef SkPDFObject INHERITED; 226 SkDEBUGCODE(bool fDumped;)
221 }; 227 };
222 228
223 /** \class SkPDFDict 229 /** \class SkPDFDict
224 230
225 A dictionary object in a PDF. 231 A dictionary object in a PDF.
226 */ 232 */
227 class SkPDFDict : public SkPDFObject { 233 class SkPDFDict : public SkPDFObject {
228 public: 234 public:
229 /** Create a PDF dictionary. Maximum number of entries is 4095. 235 /** Create a PDF dictionary.
236 * @param type The value of the Type entry, nullptr for no type.
230 */ 237 */
231 SkPDFDict(); 238 explicit SkPDFDict(const char type[] = nullptr);
232
233 /** Create a PDF dictionary with a Type entry.
234 * @param type The value of the Type entry.
235 */
236 explicit SkPDFDict(const char type[]);
237 239
238 virtual ~SkPDFDict(); 240 virtual ~SkPDFDict();
239 241
240 // The SkPDFObject interface. 242 // The SkPDFObject interface.
241 void emitObject(SkWStream* stream, 243 void emitObject(SkWStream* stream,
242 const SkPDFObjNumMap& objNumMap, 244 const SkPDFObjNumMap& objNumMap,
243 const SkPDFSubstituteMap& substitutes) const override; 245 const SkPDFSubstituteMap& substitutes) const override;
244 void addResources(SkPDFObjNumMap*, 246 void addResources(SkPDFObjNumMap*,
245 const SkPDFSubstituteMap&) const override; 247 const SkPDFSubstituteMap&) const override;
248 void dump() override;
246 249
247 /** The size of the dictionary. 250 /** The size of the dictionary.
248 */ 251 */
249 int size() const; 252 int size() const;
250 253
251 /** Add the value to the dictionary with the given key. 254 /** Add the value to the dictionary with the given key.
252 * @param key The text of the key for this dictionary entry. 255 * @param key The text of the key for this dictionary entry.
253 * @param value The value for this dictionary entry. 256 * @param value The value for this dictionary entry.
254 */ 257 */
255 void insertObject(const char key[], sk_sp<SkPDFObject>); 258 void insertObject(const char key[], sk_sp<SkPDFObject>);
256 void insertObject(const SkString& key, sk_sp<SkPDFObject>); 259 void insertObject(const SkString& key, sk_sp<SkPDFObject>);
257 void insertObjRef(const char key[], sk_sp<SkPDFObject>); 260 void insertObjRef(const char key[], sk_sp<SkPDFObject>);
258 void insertObjRef(const SkString& key, sk_sp<SkPDFObject>); 261 void insertObjRef(const SkString& key, sk_sp<SkPDFObject>);
259 262
260 /** Add the value to the dictionary with the given key. 263 /** Add the value to the dictionary with the given key.
261 * @param key The text of the key for this dictionary entry. 264 * @param key The text of the key for this dictionary entry.
262 * @param value The value for this dictionary entry. 265 * @param value The value for this dictionary entry.
263 */ 266 */
264 void insertBool(const char key[], bool value); 267 void insertBool(const char key[], bool value);
265 void insertInt(const char key[], int32_t value); 268 void insertInt(const char key[], int32_t value);
266 void insertInt(const char key[], size_t value); 269 void insertInt(const char key[], size_t value);
267 void insertScalar(const char key[], SkScalar value); 270 void insertScalar(const char key[], SkScalar value);
268 void insertName(const char key[], const char nameValue[]); 271 void insertName(const char key[], const char nameValue[]);
269 void insertName(const char key[], const SkString& nameValue); 272 void insertName(const char key[], const SkString& nameValue);
270 void insertString(const char key[], const char value[]); 273 void insertString(const char key[], const char value[]);
271 void insertString(const char key[], const SkString& value); 274 void insertString(const char key[], const SkString& value);
272 275
273 /** Remove all entries from the dictionary.
274 */
275 void clear();
276
277 /** Emit the dictionary, without the "<<" and ">>". 276 /** Emit the dictionary, without the "<<" and ">>".
278 */ 277 */
279 void emitAll(SkWStream* stream, 278 void emitAll(SkWStream* stream,
280 const SkPDFObjNumMap& objNumMap, 279 const SkPDFObjNumMap& objNumMap,
281 const SkPDFSubstituteMap& substitutes) const; 280 const SkPDFSubstituteMap& substitutes) const;
282 281
283 private: 282 private:
284 struct Record { 283 struct Record {
285 SkPDFUnion fKey; 284 SkPDFUnion fKey;
286 SkPDFUnion fValue; 285 SkPDFUnion fValue;
286 Record(SkPDFUnion&&, SkPDFUnion&&);
287 Record(Record&&);
288 Record& operator=(Record&&);
289 Record(const Record&) = delete;
290 Record& operator=(const Record&) = delete;
287 }; 291 };
288 SkTDArray<Record> fRecords; 292 SkTArray<Record> fRecords;
289 static const int kMaxLen = 4095; 293 SkDEBUGCODE(bool fDumped;)
290
291 void set(SkPDFUnion&& name, SkPDFUnion&& value);
292
293 typedef SkPDFObject INHERITED;
294 }; 294 };
295 295
296 /** \class SkPDFSharedStream 296 /** \class SkPDFSharedStream
297 297
298 This class takes an asset and assumes that it is backed by 298 This class takes an asset and assumes that it is backed by
299 long-lived shared data (for example, an open file 299 long-lived shared data (for example, an open file
300 descriptor). That is: no memory savings can be made by holding on 300 descriptor). That is: no memory savings can be made by holding on
301 to a compressed version instead. 301 to a compressed version instead.
302 */ 302 */
303 class SkPDFSharedStream final : public SkPDFObject { 303 class SkPDFSharedStream final : public SkPDFObject {
304 public: 304 public:
305 // Takes ownership of asset. 305 // Takes ownership of asset.
306 SkPDFSharedStream(SkStreamAsset* data) 306 SkPDFSharedStream(SkStreamAsset* data);
307 : fAsset(data), fDict(new SkPDFDict) { SkASSERT(data); } 307 ~SkPDFSharedStream();
308 SkPDFDict* dict() { return fDict.get(); } 308 SkPDFDict* dict() { return fDict.get(); }
309 void emitObject(SkWStream*, 309 void emitObject(SkWStream*,
310 const SkPDFObjNumMap&, 310 const SkPDFObjNumMap&,
311 const SkPDFSubstituteMap&) const override; 311 const SkPDFSubstituteMap&) const override;
312 void addResources(SkPDFObjNumMap*, 312 void addResources(SkPDFObjNumMap*,
313 const SkPDFSubstituteMap&) const override; 313 const SkPDFSubstituteMap&) const override;
314 void dump() override;
314 315
315 private: 316 private:
316 SkAutoTDelete<SkStreamAsset> fAsset; 317 SkAutoTDelete<SkStreamAsset> fAsset;
317 sk_sp<SkPDFDict> fDict; 318 sk_sp<SkPDFDict> fDict;
319 SkDEBUGCODE(bool fDumped;)
318 typedef SkPDFObject INHERITED; 320 typedef SkPDFObject INHERITED;
319 }; 321 };
320 322
321 //////////////////////////////////////////////////////////////////////////////// 323 ////////////////////////////////////////////////////////////////////////////////
322 324
323 /** \class SkPDFObjNumMap 325 /** \class SkPDFObjNumMap
324 326
325 The PDF Object Number Map manages object numbers. It is used to 327 The PDF Object Number Map manages object numbers. It is used to
326 create the PDF cross reference table. 328 create the PDF cross reference table.
327 */ 329 */
328 class SkPDFObjNumMap : SkNoncopyable { 330 class SkPDFObjNumMap : SkNoncopyable {
329 public: 331 public:
330 /** Add the passed object to the catalog. 332 /** Add the passed object to the catalog.
331 * @param obj The object to add. 333 * @param obj The object to add.
332 * @return True iff the object was not already added to the catalog. 334 * @return True iff the object was not already added to the catalog.
333 */ 335 */
334 bool addObject(SkPDFObject* obj); 336 bool addObject(SkPDFObject* obj);
335 337
336 /** Add the passed object to the catalog, as well as all its dependencies. 338 /** Add the passed object to the catalog, as well as all its dependencies.
337 * @param obj The object to add. If nullptr, this is a noop. 339 * @param obj The object to add. If nullptr, this is a noop.
338 * @param subs Will be passed to obj->addResources(). 340 * @param subs Will be passed to obj->addResources().
339 */ 341 */
340 void addObjectRecursively(SkPDFObject* obj, const SkPDFSubstituteMap& subs); 342 void addObjectRecursively(SkPDFObject* obj, const SkPDFSubstituteMap& subs);
341 343
342 /** Get the object number for the passed object. 344 /** Get the object number for the passed object.
343 * @param obj The object of interest. 345 * @param obj The object of interest.
344 */ 346 */
345 int32_t getObjectNumber(SkPDFObject* obj) const; 347 int32_t getObjectNumber(SkPDFObject* obj) const;
346 348
347 const SkTDArray<SkPDFObject*>& objects() const { return fObjects; } 349 const SkTArray<sk_sp<SkPDFObject>>& objects() const { return fObjects; }
348 350
349 private: 351 private:
350 SkTDArray<SkPDFObject*> fObjects; 352 SkTArray<sk_sp<SkPDFObject>> fObjects;
351 SkTHashMap<SkPDFObject*, int32_t> fObjectNumbers; 353 SkTHashMap<SkPDFObject*, int32_t> fObjectNumbers;
352 }; 354 };
353 355
354 //////////////////////////////////////////////////////////////////////////////// 356 ////////////////////////////////////////////////////////////////////////////////
355 357
356 /** \class SkPDFSubstituteMap 358 /** \class SkPDFSubstituteMap
357 359
358 The PDF Substitute Map manages substitute objects and owns the 360 The PDF Substitute Map manages substitute objects and owns the
359 substitutes. 361 substitutes.
360 */ 362 */
(...skipping 19 matching lines...) Expand all
380 }; 382 };
381 383
382 #ifdef SK_PDF_IMAGE_STATS 384 #ifdef SK_PDF_IMAGE_STATS
383 extern SkAtomic<int> gDrawImageCalls; 385 extern SkAtomic<int> gDrawImageCalls;
384 extern SkAtomic<int> gJpegImageObjects; 386 extern SkAtomic<int> gJpegImageObjects;
385 extern SkAtomic<int> gRegularImageObjects; 387 extern SkAtomic<int> gRegularImageObjects;
386 extern void SkPDFImageDumpStats(); 388 extern void SkPDFImageDumpStats();
387 #endif // SK_PDF_IMAGE_STATS 389 #endif // SK_PDF_IMAGE_STATS
388 390
389 #endif 391 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698