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