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

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

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
« src/pdf/SkPDFTypes.h ('K') | « src/pdf/SkPDFTypes.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2011 Google Inc. 2 * Copyright 2011 Google Inc.
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 #include "SkDeflate.h" 8 #include "SkDeflate.h"
9 #include "SkPDFTypes.h" 9 #include "SkPDFTypes.h"
10 #include "SkPDFUtils.h" 10 #include "SkPDFUtils.h"
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 fValue.emitObject(stream, objNumMap, substitutes); 256 fValue.emitObject(stream, objNumMap, substitutes);
257 } 257 }
258 void SkPDFAtom::addResources(SkPDFObjNumMap* map, 258 void SkPDFAtom::addResources(SkPDFObjNumMap* map,
259 const SkPDFSubstituteMap& substitutes) const { 259 const SkPDFSubstituteMap& substitutes) const {
260 fValue.addResources(map, substitutes); 260 fValue.addResources(map, substitutes);
261 } 261 }
262 #endif // 0 262 #endif // 0
263 263
264 //////////////////////////////////////////////////////////////////////////////// 264 ////////////////////////////////////////////////////////////////////////////////
265 265
266 SkPDFArray::SkPDFArray() {} 266 SkPDFArray::SkPDFArray() { SkDEBUGCODE(fDumped = false;) }
267 SkPDFArray::~SkPDFArray() { 267
268 for (SkPDFUnion& value : fValues) { 268 SkPDFArray::~SkPDFArray() { this->dump(); }
269 value.~SkPDFUnion(); 269
270 } 270 void SkPDFArray::dump() {
271 fValues.reset(); 271 fValues.reset();
272 SkDEBUGCODE(fDumped = true;)
272 } 273 }
273 274
274 int SkPDFArray::size() const { return fValues.count(); } 275 int SkPDFArray::size() const { return fValues.count(); }
275 276
276 void SkPDFArray::reserve(int length) { fValues.setReserve(length); } 277 void SkPDFArray::reserve(int length) {
278 // TODO: implement SkTArray<T>::reserve()
tomhudson 2016/03/18 13:59:38 TODO! If you intend to leave undone, is there anyt
hal.canary 2016/03/18 21:18:07 I put more info in the TODO. I haven't had time t
279 }
277 280
278 void SkPDFArray::emitObject(SkWStream* stream, 281 void SkPDFArray::emitObject(SkWStream* stream,
279 const SkPDFObjNumMap& objNumMap, 282 const SkPDFObjNumMap& objNumMap,
280 const SkPDFSubstituteMap& substitutes) const { 283 const SkPDFSubstituteMap& substitutes) const {
284 SkASSERT(!fDumped);
281 stream->writeText("["); 285 stream->writeText("[");
282 for (int i = 0; i < fValues.count(); i++) { 286 for (int i = 0; i < fValues.count(); i++) {
283 fValues[i].emitObject(stream, objNumMap, substitutes); 287 fValues[i].emitObject(stream, objNumMap, substitutes);
284 if (i + 1 < fValues.count()) { 288 if (i + 1 < fValues.count()) {
285 stream->writeText(" "); 289 stream->writeText(" ");
286 } 290 }
287 } 291 }
288 stream->writeText("]"); 292 stream->writeText("]");
289 } 293 }
290 294
291 void SkPDFArray::addResources(SkPDFObjNumMap* catalog, 295 void SkPDFArray::addResources(SkPDFObjNumMap* catalog,
292 const SkPDFSubstituteMap& substitutes) const { 296 const SkPDFSubstituteMap& substitutes) const {
297 SkASSERT(!fDumped);
293 for (const SkPDFUnion& value : fValues) { 298 for (const SkPDFUnion& value : fValues) {
294 value.addResources(catalog, substitutes); 299 value.addResources(catalog, substitutes);
295 } 300 }
296 } 301 }
297 302
298 void SkPDFArray::append(SkPDFUnion&& value) { 303 void SkPDFArray::append(SkPDFUnion&& value) {
299 new (fValues.append()) SkPDFUnion(std::move(value)); 304 fValues.emplace_back(std::move(value));
300 } 305 }
301 306
302 void SkPDFArray::appendInt(int32_t value) { 307 void SkPDFArray::appendInt(int32_t value) {
303 this->append(SkPDFUnion::Int(value)); 308 this->append(SkPDFUnion::Int(value));
304 } 309 }
305 310
306 void SkPDFArray::appendBool(bool value) { 311 void SkPDFArray::appendBool(bool value) {
307 this->append(SkPDFUnion::Bool(value)); 312 this->append(SkPDFUnion::Bool(value));
308 } 313 }
309 314
(...skipping 20 matching lines...) Expand all
330 void SkPDFArray::appendObject(sk_sp<SkPDFObject> objSp) { 335 void SkPDFArray::appendObject(sk_sp<SkPDFObject> objSp) {
331 this->append(SkPDFUnion::Object(std::move(objSp))); 336 this->append(SkPDFUnion::Object(std::move(objSp)));
332 } 337 }
333 338
334 void SkPDFArray::appendObjRef(sk_sp<SkPDFObject> objSp) { 339 void SkPDFArray::appendObjRef(sk_sp<SkPDFObject> objSp) {
335 this->append(SkPDFUnion::ObjRef(std::move(objSp))); 340 this->append(SkPDFUnion::ObjRef(std::move(objSp)));
336 } 341 }
337 342
338 /////////////////////////////////////////////////////////////////////////////// 343 ///////////////////////////////////////////////////////////////////////////////
339 344
340 SkPDFDict::SkPDFDict() {} 345 SkPDFDict::~SkPDFDict() { this->dump(); }
341 346
342 SkPDFDict::~SkPDFDict() { this->clear(); } 347 void SkPDFDict::dump() {
348 fRecords.reset();
349 SkDEBUGCODE(fDumped = true;)
350 }
343 351
344 SkPDFDict::SkPDFDict(const char type[]) { this->insertName("Type", type); } 352 SkPDFDict::SkPDFDict(const char type[]) {
353 SkDEBUGCODE(fDumped = false;)
354 if (type) {
355 this->insertName("Type", type);
356 }
357 }
345 358
346 void SkPDFDict::emitObject(SkWStream* stream, 359 void SkPDFDict::emitObject(SkWStream* stream,
347 const SkPDFObjNumMap& objNumMap, 360 const SkPDFObjNumMap& objNumMap,
348 const SkPDFSubstituteMap& substitutes) const { 361 const SkPDFSubstituteMap& substitutes) const {
349 stream->writeText("<<"); 362 stream->writeText("<<");
350 this->emitAll(stream, objNumMap, substitutes); 363 this->emitAll(stream, objNumMap, substitutes);
351 stream->writeText(">>"); 364 stream->writeText(">>");
352 } 365 }
353 366
354 void SkPDFDict::emitAll(SkWStream* stream, 367 void SkPDFDict::emitAll(SkWStream* stream,
355 const SkPDFObjNumMap& objNumMap, 368 const SkPDFObjNumMap& objNumMap,
356 const SkPDFSubstituteMap& substitutes) const { 369 const SkPDFSubstituteMap& substitutes) const {
370 SkASSERT(!fDumped);
357 for (int i = 0; i < fRecords.count(); i++) { 371 for (int i = 0; i < fRecords.count(); i++) {
358 fRecords[i].fKey.emitObject(stream, objNumMap, substitutes); 372 fRecords[i].fKey.emitObject(stream, objNumMap, substitutes);
359 stream->writeText(" "); 373 stream->writeText(" ");
360 fRecords[i].fValue.emitObject(stream, objNumMap, substitutes); 374 fRecords[i].fValue.emitObject(stream, objNumMap, substitutes);
361 if (i + 1 < fRecords.count()) { 375 if (i + 1 < fRecords.count()) {
362 stream->writeText("\n"); 376 stream->writeText("\n");
363 } 377 }
364 } 378 }
365 } 379 }
366 380
367 void SkPDFDict::addResources(SkPDFObjNumMap* catalog, 381 void SkPDFDict::addResources(SkPDFObjNumMap* catalog,
368 const SkPDFSubstituteMap& substitutes) const { 382 const SkPDFSubstituteMap& substitutes) const {
383 SkASSERT(!fDumped);
369 for (int i = 0; i < fRecords.count(); i++) { 384 for (int i = 0; i < fRecords.count(); i++) {
370 fRecords[i].fKey.addResources(catalog, substitutes); 385 fRecords[i].fKey.addResources(catalog, substitutes);
371 fRecords[i].fValue.addResources(catalog, substitutes); 386 fRecords[i].fValue.addResources(catalog, substitutes);
372 } 387 }
373 } 388 }
374 389
375 void SkPDFDict::set(SkPDFUnion&& name, SkPDFUnion&& value) { 390 SkPDFDict::Record::Record(SkPDFUnion&& k, SkPDFUnion&& v)
376 Record* rec = fRecords.append(); 391 : fKey(std::move(k)), fValue(std::move(v)) {}
377 SkASSERT(name.isName()); 392
378 new (&rec->fKey) SkPDFUnion(std::move(name)); 393 SkPDFDict::Record::Record(SkPDFDict::Record&& o)
379 new (&rec->fValue) SkPDFUnion(std::move(value)); 394 : fKey(std::move(o.fKey)), fValue(std::move(o.fValue)) {}
395
396 SkPDFDict::Record& SkPDFDict::Record::operator=(SkPDFDict::Record&& o) {
tomhudson 2016/03/18 13:59:38 You want move assignment but no non-destructive no
hal.canary 2016/03/18 21:18:07 Yep. I have not needed those yet, so I don't defi
tomhudson 2016/03/21 13:54:30 Acknowledged.
397 fKey = std::move(o.fKey);
398 fValue = std::move(o.fValue);
399 return *this;
380 } 400 }
381 401
382 int SkPDFDict::size() const { return fRecords.count(); } 402 int SkPDFDict::size() const { return fRecords.count(); }
383 403
384 void SkPDFDict::insertObjRef(const char key[], sk_sp<SkPDFObject> objSp) { 404 void SkPDFDict::insertObjRef(const char key[], sk_sp<SkPDFObject> objSp) {
385 this->set(SkPDFUnion::Name(key), SkPDFUnion::ObjRef(std::move(objSp))); 405 fRecords.emplace_back(SkPDFUnion::Name(key), SkPDFUnion::ObjRef(std::move(ob jSp)));
386 } 406 }
407
387 void SkPDFDict::insertObjRef(const SkString& key, sk_sp<SkPDFObject> objSp) { 408 void SkPDFDict::insertObjRef(const SkString& key, sk_sp<SkPDFObject> objSp) {
388 this->set(SkPDFUnion::Name(key), SkPDFUnion::ObjRef(std::move(objSp))); 409 fRecords.emplace_back(SkPDFUnion::Name(key), SkPDFUnion::ObjRef(std::move(ob jSp)));
389 } 410 }
390 411
391 void SkPDFDict::insertObject(const char key[], sk_sp<SkPDFObject> objSp) { 412 void SkPDFDict::insertObject(const char key[], sk_sp<SkPDFObject> objSp) {
392 this->set(SkPDFUnion::Name(key), SkPDFUnion::Object(std::move(objSp))); 413 fRecords.emplace_back(SkPDFUnion::Name(key), SkPDFUnion::Object(std::move(ob jSp)));
393 } 414 }
394 void SkPDFDict::insertObject(const SkString& key, sk_sp<SkPDFObject> objSp) { 415 void SkPDFDict::insertObject(const SkString& key, sk_sp<SkPDFObject> objSp) {
395 this->set(SkPDFUnion::Name(key), SkPDFUnion::Object(std::move(objSp))); 416 fRecords.emplace_back(SkPDFUnion::Name(key), SkPDFUnion::Object(std::move(ob jSp)));
396 } 417 }
397 418
398 void SkPDFDict::insertBool(const char key[], bool value) { 419 void SkPDFDict::insertBool(const char key[], bool value) {
399 this->set(SkPDFUnion::Name(key), SkPDFUnion::Bool(value)); 420 fRecords.emplace_back(SkPDFUnion::Name(key), SkPDFUnion::Bool(value));
400 } 421 }
401 422
402 void SkPDFDict::insertInt(const char key[], int32_t value) { 423 void SkPDFDict::insertInt(const char key[], int32_t value) {
403 this->set(SkPDFUnion::Name(key), SkPDFUnion::Int(value)); 424 fRecords.emplace_back(SkPDFUnion::Name(key), SkPDFUnion::Int(value));
404 } 425 }
405 426
406 void SkPDFDict::insertInt(const char key[], size_t value) { 427 void SkPDFDict::insertInt(const char key[], size_t value) {
407 this->insertInt(key, SkToS32(value)); 428 this->insertInt(key, SkToS32(value));
408 } 429 }
409 430
410 void SkPDFDict::insertScalar(const char key[], SkScalar value) { 431 void SkPDFDict::insertScalar(const char key[], SkScalar value) {
411 this->set(SkPDFUnion::Name(key), SkPDFUnion::Scalar(value)); 432 fRecords.emplace_back(SkPDFUnion::Name(key), SkPDFUnion::Scalar(value));
412 } 433 }
413 434
414 void SkPDFDict::insertName(const char key[], const char name[]) { 435 void SkPDFDict::insertName(const char key[], const char name[]) {
415 this->set(SkPDFUnion::Name(key), SkPDFUnion::Name(name)); 436 fRecords.emplace_back(SkPDFUnion::Name(key), SkPDFUnion::Name(name));
416 } 437 }
417 438
418 void SkPDFDict::insertName(const char key[], const SkString& name) { 439 void SkPDFDict::insertName(const char key[], const SkString& name) {
419 this->set(SkPDFUnion::Name(key), SkPDFUnion::Name(name)); 440 fRecords.emplace_back(SkPDFUnion::Name(key), SkPDFUnion::Name(name));
420 } 441 }
421 442
422 void SkPDFDict::insertString(const char key[], const char value[]) { 443 void SkPDFDict::insertString(const char key[], const char value[]) {
423 this->set(SkPDFUnion::Name(key), SkPDFUnion::String(value)); 444 fRecords.emplace_back(SkPDFUnion::Name(key), SkPDFUnion::String(value));
424 } 445 }
425 446
426 void SkPDFDict::insertString(const char key[], const SkString& value) { 447 void SkPDFDict::insertString(const char key[], const SkString& value) {
427 this->set(SkPDFUnion::Name(key), SkPDFUnion::String(value)); 448 fRecords.emplace_back(SkPDFUnion::Name(key), SkPDFUnion::String(value));
428 }
429
430 void SkPDFDict::clear() {
tomhudson 2016/03/18 13:59:38 What happened to clear()? Do we just not need to c
hal.canary 2016/03/18 21:18:07 clear() is superceded by drop(), which has the sam
tomhudson 2016/03/21 13:54:30 Acknowledged.
431 for (Record& rec : fRecords) {
432 rec.fKey.~SkPDFUnion();
433 rec.fValue.~SkPDFUnion();
434 }
435 fRecords.reset();
436 } 449 }
437 450
438 //////////////////////////////////////////////////////////////////////////////// 451 ////////////////////////////////////////////////////////////////////////////////
439 452
453 SkPDFSharedStream::SkPDFSharedStream(SkStreamAsset* data)
454 : fAsset(data), fDict(new SkPDFDict) {
455 SkDEBUGCODE(fDumped = false;)
456 SkASSERT(data);
457 }
458
459 SkPDFSharedStream::~SkPDFSharedStream() { this->dump(); }
460
461 void SkPDFSharedStream::dump() {
462 fAsset.free();
463 fDict.reset(nullptr);
464 SkDEBUGCODE(fDumped = true;)
465 }
466
440 void SkPDFSharedStream::emitObject( 467 void SkPDFSharedStream::emitObject(
441 SkWStream* stream, 468 SkWStream* stream,
442 const SkPDFObjNumMap& objNumMap, 469 const SkPDFObjNumMap& objNumMap,
443 const SkPDFSubstituteMap& substitutes) const { 470 const SkPDFSubstituteMap& substitutes) const {
471 SkASSERT(!fDumped);
444 SkDynamicMemoryWStream buffer; 472 SkDynamicMemoryWStream buffer;
445 SkDeflateWStream deflateWStream(&buffer); 473 SkDeflateWStream deflateWStream(&buffer);
446 // Since emitObject is const, this function doesn't change the dictionary. 474 // Since emitObject is const, this function doesn't change the dictionary.
447 SkAutoTDelete<SkStreamAsset> dup(fAsset->duplicate()); // Cheap copy 475 SkAutoTDelete<SkStreamAsset> dup(fAsset->duplicate()); // Cheap copy
448 SkASSERT(dup); 476 SkASSERT(dup);
449 SkStreamCopy(&deflateWStream, dup.get()); 477 SkStreamCopy(&deflateWStream, dup.get());
450 deflateWStream.finalize(); 478 deflateWStream.finalize();
451 size_t length = buffer.bytesWritten(); 479 size_t length = buffer.bytesWritten();
452 stream->writeText("<<"); 480 stream->writeText("<<");
453 fDict->emitAll(stream, objNumMap, substitutes); 481 fDict->emitAll(stream, objNumMap, substitutes);
454 stream->writeText("\n"); 482 stream->writeText("\n");
455 SkPDFUnion::Name("Length").emitObject(stream, objNumMap, substitutes); 483 SkPDFUnion::Name("Length").emitObject(stream, objNumMap, substitutes);
456 stream->writeText(" "); 484 stream->writeText(" ");
457 SkPDFUnion::Int(length).emitObject(stream, objNumMap, substitutes); 485 SkPDFUnion::Int(length).emitObject(stream, objNumMap, substitutes);
458 stream->writeText("\n"); 486 stream->writeText("\n");
459 SkPDFUnion::Name("Filter").emitObject(stream, objNumMap, substitutes); 487 SkPDFUnion::Name("Filter").emitObject(stream, objNumMap, substitutes);
460 stream->writeText(" "); 488 stream->writeText(" ");
461 SkPDFUnion::Name("FlateDecode").emitObject(stream, objNumMap, substitutes); 489 SkPDFUnion::Name("FlateDecode").emitObject(stream, objNumMap, substitutes);
462 stream->writeText(">>"); 490 stream->writeText(">>");
463 stream->writeText(" stream\n"); 491 stream->writeText(" stream\n");
464 buffer.writeToStream(stream); 492 buffer.writeToStream(stream);
465 stream->writeText("\nendstream"); 493 stream->writeText("\nendstream");
466 } 494 }
467 495
468 void SkPDFSharedStream::addResources( 496 void SkPDFSharedStream::addResources(
469 SkPDFObjNumMap* catalog, const SkPDFSubstituteMap& substitutes) const { 497 SkPDFObjNumMap* catalog, const SkPDFSubstituteMap& substitutes) const {
498 SkASSERT(!fDumped);
470 fDict->addResources(catalog, substitutes); 499 fDict->addResources(catalog, substitutes);
471 } 500 }
472 501
473 //////////////////////////////////////////////////////////////////////////////// 502 ////////////////////////////////////////////////////////////////////////////////
474 503
475 SkPDFSubstituteMap::~SkPDFSubstituteMap() { 504 SkPDFSubstituteMap::~SkPDFSubstituteMap() {
476 fSubstituteMap.foreach( 505 fSubstituteMap.foreach(
477 [](SkPDFObject*, SkPDFObject** v) { (*v)->unref(); }); 506 [](SkPDFObject*, SkPDFObject** v) { (*v)->unref(); });
478 } 507 }
479 508
480 void SkPDFSubstituteMap::setSubstitute(SkPDFObject* original, 509 void SkPDFSubstituteMap::setSubstitute(SkPDFObject* original,
481 SkPDFObject* substitute) { 510 SkPDFObject* substitute) {
482 SkASSERT(original != substitute); 511 SkASSERT(original != substitute);
483 SkASSERT(!fSubstituteMap.find(original)); 512 SkASSERT(!fSubstituteMap.find(original));
484 fSubstituteMap.set(original, SkRef(substitute)); 513 fSubstituteMap.set(original, SkRef(substitute));
485 } 514 }
486 515
487 SkPDFObject* SkPDFSubstituteMap::getSubstitute(SkPDFObject* object) const { 516 SkPDFObject* SkPDFSubstituteMap::getSubstitute(SkPDFObject* object) const {
488 SkPDFObject** found = fSubstituteMap.find(object); 517 SkPDFObject** found = fSubstituteMap.find(object);
489 return found ? *found : object; 518 return found ? *found : object;
490 } 519 }
491 520
492 //////////////////////////////////////////////////////////////////////////////// 521 ////////////////////////////////////////////////////////////////////////////////
493 522
494 bool SkPDFObjNumMap::addObject(SkPDFObject* obj) { 523 bool SkPDFObjNumMap::addObject(SkPDFObject* obj) {
495 if (fObjectNumbers.find(obj)) { 524 if (fObjectNumbers.find(obj)) {
496 return false; 525 return false;
497 } 526 }
498 fObjectNumbers.set(obj, fObjectNumbers.count() + 1); 527 fObjectNumbers.set(obj, fObjectNumbers.count() + 1);
499 fObjects.push(obj); 528 fObjects.emplace_back(sk_ref_sp(obj));
tomhudson 2016/03/18 13:59:38 You're ref'ing an sp, but input is a raw pointer?
hal.canary 2016/03/18 21:18:07 yes. this is awkward now and I will fix it later.
tomhudson 2016/03/21 13:54:30 Plz to file bug or TODO about laters.
500 return true; 529 return true;
501 } 530 }
502 531
503 void SkPDFObjNumMap::addObjectRecursively(SkPDFObject* obj, 532 void SkPDFObjNumMap::addObjectRecursively(SkPDFObject* obj,
504 const SkPDFSubstituteMap& subs) { 533 const SkPDFSubstituteMap& subs) {
505 if (obj && this->addObject(obj)) { 534 if (obj && this->addObject(obj)) {
506 obj->addResources(this, subs); 535 obj->addResources(this, subs);
507 } 536 }
508 } 537 }
509 538
(...skipping 10 matching lines...) Expand all
520 549
521 void SkPDFImageDumpStats() { 550 void SkPDFImageDumpStats() {
522 SkDebugf("\ntotal PDF drawImage/drawBitmap calls: %d\n" 551 SkDebugf("\ntotal PDF drawImage/drawBitmap calls: %d\n"
523 "total PDF jpeg images: %d\n" 552 "total PDF jpeg images: %d\n"
524 "total PDF regular images: %d\n", 553 "total PDF regular images: %d\n",
525 gDrawImageCalls.load(), 554 gDrawImageCalls.load(),
526 gJpegImageObjects.load(), 555 gJpegImageObjects.load(),
527 gRegularImageObjects.load()); 556 gRegularImageObjects.load());
528 } 557 }
529 #endif // SK_PDF_IMAGE_STATS 558 #endif // SK_PDF_IMAGE_STATS
OLDNEW
« src/pdf/SkPDFTypes.h ('K') | « src/pdf/SkPDFTypes.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698