OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 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 "PictureRenderer.h" | 8 #include "PictureRenderer.h" |
9 #include "picture_utils.h" | 9 #include "picture_utils.h" |
10 #include "SamplePipeControllers.h" | 10 #include "SamplePipeControllers.h" |
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 #endif | 316 #endif |
317 } | 317 } |
318 | 318 |
319 uint32_t PictureRenderer::recordFlags() { | 319 uint32_t PictureRenderer::recordFlags() { |
320 return (kNone_BBoxHierarchyType == fBBoxHierarchyType) | 320 return (kNone_BBoxHierarchyType == fBBoxHierarchyType) |
321 ? 0 | 321 ? 0 |
322 : SkPicture::kUsePathBoundsForClip_RecordingFlag; | 322 : SkPicture::kUsePathBoundsForClip_RecordingFlag; |
323 } | 323 } |
324 | 324 |
325 /** | 325 /** |
326 * Write the canvas to an image file and/or JSON summary. | 326 * Write the canvas to the specified path. |
327 * | 327 * |
328 * @param canvas Must be non-null. Canvas to be written to a file. | 328 * @param canvas Must be non-null. Canvas to be written to a file. |
329 * @param outputDir If nonempty, write the binary image to a file within this di
rectory; | 329 * @param outputDir If nonempty, write the binary image to a file within this di
rectory. |
330 * if empty, don't write out the image at all. | |
331 * @param inputFilename If we are writing out a binary image, use this to build
its filename. | 330 * @param inputFilename If we are writing out a binary image, use this to build
its filename. |
332 * @param jsonSummaryPtr If not null, add image results (checksum) to this summa
ry. | 331 * @param jsonSummaryPtr If not null, add image results to this summary. |
333 * @param useChecksumBasedFilenames If true, use checksum-based filenames when w
riting to disk. | 332 * @param useChecksumBasedFilenames If true, use checksum-based filenames when w
riting to disk. |
334 * @param tileNumberPtr If not null, which tile number this image contains. | 333 * @param tileNumberPtr If not null, which tile number this image contains. |
| 334 * @return bool True if the Canvas is written to a file. |
335 * | 335 * |
336 * @return bool True if the operation completed successfully. | 336 * TODO(epoger): Right now, all canvases must pass through this function in orde
r to be appended |
| 337 * to the ImageResultsSummary. We need some way to add bitmaps to the ImageResu
ltsSummary |
| 338 * even if --writePath has not been specified (and thus this function is not cal
led). |
| 339 * |
| 340 * One fix would be to pass in these path elements separately, and allow this fu
nction to be |
| 341 * called even if --writePath was not specified... |
| 342 * const char *outputDir // NULL if we don't want to write image files to dis
k |
| 343 * const char *filename // name we use within JSON summary, and as the filen
ame within outputDir |
| 344 * |
| 345 * UPDATE: Now that outputDir and inputFilename are passed separately, we should
be able to do that. |
337 */ | 346 */ |
338 static bool write(SkCanvas* canvas, const SkString& outputDir, const SkString& i
nputFilename, | 347 static bool write(SkCanvas* canvas, const SkString& outputDir, const SkString& i
nputFilename, |
339 ImageResultsSummary *jsonSummaryPtr, bool useChecksumBasedFile
names, | 348 ImageResultsSummary *jsonSummaryPtr, bool useChecksumBasedFile
names, |
340 const int* tileNumberPtr=NULL) { | 349 const int* tileNumberPtr=NULL) { |
341 SkASSERT(canvas != NULL); | 350 SkASSERT(canvas != NULL); |
342 if (NULL == canvas) { | 351 if (NULL == canvas) { |
343 return false; | 352 return false; |
344 } | 353 } |
345 | 354 |
346 SkBitmap bitmap; | 355 SkBitmap bitmap; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
391 outputRelativePath.append("/"); // always use "/", even on Windows | 400 outputRelativePath.append("/"); // always use "/", even on Windows |
392 outputRelativePath.append(outputFilename); | 401 outputRelativePath.append(outputFilename); |
393 } else { | 402 } else { |
394 outputRelativePath.set(outputFilename); | 403 outputRelativePath.set(outputFilename); |
395 } | 404 } |
396 | 405 |
397 jsonSummaryPtr->add(inputFilename.c_str(), outputRelativePath.c_str(), | 406 jsonSummaryPtr->add(inputFilename.c_str(), outputRelativePath.c_str(), |
398 hash, tileNumberPtr); | 407 hash, tileNumberPtr); |
399 } | 408 } |
400 | 409 |
401 if (outputDir.isEmpty()) { | 410 SkASSERT(!outputDir.isEmpty()); // TODO(epoger): we want to remove this cons
traint, |
402 return true; | 411 // as noted above |
403 } | |
404 | |
405 SkString dirPath; | 412 SkString dirPath; |
406 if (outputSubdirPtr) { | 413 if (outputSubdirPtr) { |
407 dirPath = SkOSPath::SkPathJoin(outputDir.c_str(), outputSubdirPtr); | 414 dirPath = SkOSPath::SkPathJoin(outputDir.c_str(), outputSubdirPtr); |
408 sk_mkdir(dirPath.c_str()); | 415 sk_mkdir(dirPath.c_str()); |
409 } else { | 416 } else { |
410 dirPath.set(outputDir); | 417 dirPath.set(outputDir); |
411 } | 418 } |
412 SkString fullPath = SkOSPath::SkPathJoin(dirPath.c_str(), outputFilename.c_s
tr()); | 419 SkString fullPath = SkOSPath::SkPathJoin(dirPath.c_str(), outputFilename.c_s
tr()); |
413 return SkImageEncoder::EncodeFile(fullPath.c_str(), bitmap, SkImageEncoder::
kPNG_Type, 100); | 420 return SkImageEncoder::EncodeFile(fullPath.c_str(), bitmap, SkImageEncoder::
kPNG_Type, 100); |
414 } | 421 } |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
456 if (NULL == fCanvas.get() || NULL == fPicture) { | 463 if (NULL == fCanvas.get() || NULL == fPicture) { |
457 return false; | 464 return false; |
458 } | 465 } |
459 | 466 |
460 PipeController pipeController(fCanvas.get()); | 467 PipeController pipeController(fCanvas.get()); |
461 SkGPipeWriter writer; | 468 SkGPipeWriter writer; |
462 SkCanvas* pipeCanvas = writer.startRecording(&pipeController); | 469 SkCanvas* pipeCanvas = writer.startRecording(&pipeController); |
463 pipeCanvas->drawPicture(*fPicture); | 470 pipeCanvas->drawPicture(*fPicture); |
464 writer.endRecording(); | 471 writer.endRecording(); |
465 fCanvas->flush(); | 472 fCanvas->flush(); |
| 473 if (!fOutputDir.isEmpty()) { |
| 474 return write(fCanvas, fOutputDir, fInputFilename, fJsonSummaryPtr, |
| 475 fUseChecksumBasedFilenames); |
| 476 } |
466 if (NULL != out) { | 477 if (NULL != out) { |
467 *out = SkNEW(SkBitmap); | 478 *out = SkNEW(SkBitmap); |
468 setup_bitmap(*out, fPicture->width(), fPicture->height()); | 479 setup_bitmap(*out, fPicture->width(), fPicture->height()); |
469 fCanvas->readPixels(*out, 0, 0); | 480 fCanvas->readPixels(*out, 0, 0); |
470 } | 481 } |
471 return write(fCanvas, fOutputDir, fInputFilename, fJsonSummaryPtr, | 482 return true; |
472 fUseChecksumBasedFilenames); | |
473 } | 483 } |
474 | 484 |
475 SkString PipePictureRenderer::getConfigNameInternal() { | 485 SkString PipePictureRenderer::getConfigNameInternal() { |
476 return SkString("pipe"); | 486 return SkString("pipe"); |
477 } | 487 } |
478 | 488 |
479 ////////////////////////////////////////////////////////////////////////////////
/////////////// | 489 ////////////////////////////////////////////////////////////////////////////////
/////////////// |
480 | 490 |
481 void SimplePictureRenderer::init(SkPicture* picture, const SkString* outputDir, | 491 void SimplePictureRenderer::init(SkPicture* picture, const SkString* outputDir, |
482 const SkString* inputFilename, bool useChecksum
BasedFilenames) { | 492 const SkString* inputFilename, bool useChecksum
BasedFilenames) { |
483 INHERITED::init(picture, outputDir, inputFilename, useChecksumBasedFilenames
); | 493 INHERITED::init(picture, outputDir, inputFilename, useChecksumBasedFilenames
); |
484 this->buildBBoxHierarchy(); | 494 this->buildBBoxHierarchy(); |
485 } | 495 } |
486 | 496 |
487 bool SimplePictureRenderer::render(SkBitmap** out) { | 497 bool SimplePictureRenderer::render(SkBitmap** out) { |
488 SkASSERT(fCanvas.get() != NULL); | 498 SkASSERT(fCanvas.get() != NULL); |
489 SkASSERT(NULL != fPicture); | 499 SkASSERT(NULL != fPicture); |
490 if (NULL == fCanvas.get() || NULL == fPicture) { | 500 if (NULL == fCanvas.get() || NULL == fPicture) { |
491 return false; | 501 return false; |
492 } | 502 } |
493 | 503 |
494 fCanvas->drawPicture(*fPicture); | 504 fCanvas->drawPicture(*fPicture); |
495 fCanvas->flush(); | 505 fCanvas->flush(); |
| 506 if (!fOutputDir.isEmpty()) { |
| 507 return write(fCanvas, fOutputDir, fInputFilename, fJsonSummaryPtr, |
| 508 fUseChecksumBasedFilenames); |
| 509 } |
| 510 |
496 if (NULL != out) { | 511 if (NULL != out) { |
497 *out = SkNEW(SkBitmap); | 512 *out = SkNEW(SkBitmap); |
498 setup_bitmap(*out, fPicture->width(), fPicture->height()); | 513 setup_bitmap(*out, fPicture->width(), fPicture->height()); |
499 fCanvas->readPixels(*out, 0, 0); | 514 fCanvas->readPixels(*out, 0, 0); |
500 } | 515 } |
501 return write(fCanvas, fOutputDir, fInputFilename, fJsonSummaryPtr, | 516 |
502 fUseChecksumBasedFilenames); | 517 return true; |
503 } | 518 } |
504 | 519 |
505 SkString SimplePictureRenderer::getConfigNameInternal() { | 520 SkString SimplePictureRenderer::getConfigNameInternal() { |
506 return SkString("simple"); | 521 return SkString("simple"); |
507 } | 522 } |
508 | 523 |
509 ////////////////////////////////////////////////////////////////////////////////
/////////////// | 524 ////////////////////////////////////////////////////////////////////////////////
/////////////// |
510 | 525 |
511 TiledPictureRenderer::TiledPictureRenderer() | 526 TiledPictureRenderer::TiledPictureRenderer() |
512 : fTileWidth(kDefaultTileWidth) | 527 : fTileWidth(kDefaultTileWidth) |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
700 | 715 |
701 SkBitmap bitmap; | 716 SkBitmap bitmap; |
702 if (out){ | 717 if (out){ |
703 *out = SkNEW(SkBitmap); | 718 *out = SkNEW(SkBitmap); |
704 setup_bitmap(*out, fPicture->width(), fPicture->height()); | 719 setup_bitmap(*out, fPicture->width(), fPicture->height()); |
705 setup_bitmap(&bitmap, fTileWidth, fTileHeight); | 720 setup_bitmap(&bitmap, fTileWidth, fTileHeight); |
706 } | 721 } |
707 bool success = true; | 722 bool success = true; |
708 for (int i = 0; i < fTileRects.count(); ++i) { | 723 for (int i = 0; i < fTileRects.count(); ++i) { |
709 draw_tile_to_canvas(fCanvas, fTileRects[i], fPicture); | 724 draw_tile_to_canvas(fCanvas, fTileRects[i], fPicture); |
710 success &= write(fCanvas, fOutputDir, fInputFilename, fJsonSummaryPtr, | 725 if (!fOutputDir.isEmpty()) { |
711 fUseChecksumBasedFilenames, &i); | 726 success &= write(fCanvas, fOutputDir, fInputFilename, fJsonSummaryPt
r, |
| 727 fUseChecksumBasedFilenames, &i); |
| 728 } |
712 if (NULL != out) { | 729 if (NULL != out) { |
713 if (fCanvas->readPixels(&bitmap, 0, 0)) { | 730 if (fCanvas->readPixels(&bitmap, 0, 0)) { |
714 // Add this tile to the entire bitmap. | 731 // Add this tile to the entire bitmap. |
715 bitmapCopyAtOffset(bitmap, *out, SkScalarFloorToInt(fTileRects[i
].left()), | 732 bitmapCopyAtOffset(bitmap, *out, SkScalarFloorToInt(fTileRects[i
].left()), |
716 SkScalarFloorToInt(fTileRects[i].top())); | 733 SkScalarFloorToInt(fTileRects[i].top())); |
717 } else { | 734 } else { |
718 success = false; | 735 success = false; |
719 } | 736 } |
720 } | 737 } |
721 } | 738 } |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
783 SkGraphics::SetTLSFontCacheLimit(1024 * 1024); | 800 SkGraphics::SetTLSFontCacheLimit(1024 * 1024); |
784 | 801 |
785 SkBitmap bitmap; | 802 SkBitmap bitmap; |
786 if (fBitmap != NULL) { | 803 if (fBitmap != NULL) { |
787 // All tiles are the same size. | 804 // All tiles are the same size. |
788 setup_bitmap(&bitmap, SkScalarFloorToInt(fRects[0].width()), SkScala
rFloorToInt(fRects[0].height())); | 805 setup_bitmap(&bitmap, SkScalarFloorToInt(fRects[0].width()), SkScala
rFloorToInt(fRects[0].height())); |
789 } | 806 } |
790 | 807 |
791 for (int i = fStart; i < fEnd; i++) { | 808 for (int i = fStart; i < fEnd; i++) { |
792 draw_tile_to_canvas(fCanvas, fRects[i], fClone); | 809 draw_tile_to_canvas(fCanvas, fRects[i], fClone); |
793 if (!write(fCanvas, fOutputDir, fInputFilename, fJsonSummaryPtr, | 810 if ((!fOutputDir.isEmpty()) |
794 fUseChecksumBasedFilenames, &i) | 811 && !write(fCanvas, fOutputDir, fInputFilename, fJsonSummaryPtr, |
| 812 fUseChecksumBasedFilenames, &i) |
795 && fSuccess != NULL) { | 813 && fSuccess != NULL) { |
796 *fSuccess = false; | 814 *fSuccess = false; |
797 // If one tile fails to write to a file, do not continue drawing
the rest. | 815 // If one tile fails to write to a file, do not continue drawing
the rest. |
798 break; | 816 break; |
799 } | 817 } |
800 if (fBitmap != NULL) { | 818 if (fBitmap != NULL) { |
801 if (fCanvas->readPixels(&bitmap, 0, 0)) { | 819 if (fCanvas->readPixels(&bitmap, 0, 0)) { |
802 SkAutoLockPixels alp(*fBitmap); | 820 SkAutoLockPixels alp(*fBitmap); |
803 bitmapCopyAtOffset(bitmap, fBitmap, SkScalarFloorToInt(fRect
s[i].left()), | 821 bitmapCopyAtOffset(bitmap, fBitmap, SkScalarFloorToInt(fRect
s[i].left()), |
804 SkScalarFloorToInt(fRects[i].top())); | 822 SkScalarFloorToInt(fRects[i].top())); |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1012 virtual SkString getConfigNameInternal() SK_OVERRIDE { | 1030 virtual SkString getConfigNameInternal() SK_OVERRIDE { |
1013 return SkString("picture_clone"); | 1031 return SkString("picture_clone"); |
1014 } | 1032 } |
1015 }; | 1033 }; |
1016 | 1034 |
1017 PictureRenderer* CreatePictureCloneRenderer() { | 1035 PictureRenderer* CreatePictureCloneRenderer() { |
1018 return SkNEW(PictureCloneRenderer); | 1036 return SkNEW(PictureCloneRenderer); |
1019 } | 1037 } |
1020 | 1038 |
1021 } // namespace sk_tools | 1039 } // namespace sk_tools |
OLD | NEW |