| 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 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 } | 82 } |
| 83 | 83 |
| 84 void PictureRenderer::init(SkPicture* pict, const SkString* outputDir, | 84 void PictureRenderer::init(SkPicture* pict, const SkString* outputDir, |
| 85 const SkString* inputFilename, bool useChecksumBasedF
ilenames) { | 85 const SkString* inputFilename, bool useChecksumBasedF
ilenames) { |
| 86 this->CopyString(&fOutputDir, outputDir); | 86 this->CopyString(&fOutputDir, outputDir); |
| 87 this->CopyString(&fInputFilename, inputFilename); | 87 this->CopyString(&fInputFilename, inputFilename); |
| 88 fUseChecksumBasedFilenames = useChecksumBasedFilenames; | 88 fUseChecksumBasedFilenames = useChecksumBasedFilenames; |
| 89 | 89 |
| 90 SkASSERT(NULL == fPicture); | 90 SkASSERT(NULL == fPicture); |
| 91 SkASSERT(NULL == fCanvas.get()); | 91 SkASSERT(NULL == fCanvas.get()); |
| 92 if (fPicture != NULL || NULL != fCanvas.get()) { | 92 if (NULL != fPicture || NULL != fCanvas.get()) { |
| 93 return; | 93 return; |
| 94 } | 94 } |
| 95 | 95 |
| 96 SkASSERT(pict != NULL); | 96 SkASSERT(pict != NULL); |
| 97 if (NULL == pict) { | 97 if (NULL == pict) { |
| 98 return; | 98 return; |
| 99 } | 99 } |
| 100 | 100 |
| 101 fPicture = pict; | 101 fPicture.reset(pict)->ref(); |
| 102 fPicture->ref(); | |
| 103 fCanvas.reset(this->setupCanvas()); | 102 fCanvas.reset(this->setupCanvas()); |
| 104 } | 103 } |
| 105 | 104 |
| 106 void PictureRenderer::CopyString(SkString* dest, const SkString* src) { | 105 void PictureRenderer::CopyString(SkString* dest, const SkString* src) { |
| 107 if (NULL != src) { | 106 if (NULL != src) { |
| 108 dest->set(*src); | 107 dest->set(*src); |
| 109 } else { | 108 } else { |
| 110 dest->reset(); | 109 dest->reset(); |
| 111 } | 110 } |
| 112 } | 111 } |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 | 207 |
| 209 void PictureRenderer::scaleToScaleFactor(SkCanvas* canvas) { | 208 void PictureRenderer::scaleToScaleFactor(SkCanvas* canvas) { |
| 210 SkASSERT(canvas != NULL); | 209 SkASSERT(canvas != NULL); |
| 211 if (fScaleFactor != SK_Scalar1) { | 210 if (fScaleFactor != SK_Scalar1) { |
| 212 canvas->scale(fScaleFactor, fScaleFactor); | 211 canvas->scale(fScaleFactor, fScaleFactor); |
| 213 } | 212 } |
| 214 } | 213 } |
| 215 | 214 |
| 216 void PictureRenderer::end() { | 215 void PictureRenderer::end() { |
| 217 this->resetState(true); | 216 this->resetState(true); |
| 218 SkSafeUnref(fPicture); | 217 fPicture.reset(NULL); |
| 219 fPicture = NULL; | |
| 220 fCanvas.reset(NULL); | 218 fCanvas.reset(NULL); |
| 221 } | 219 } |
| 222 | 220 |
| 223 int PictureRenderer::getViewWidth() { | 221 int PictureRenderer::getViewWidth() { |
| 224 SkASSERT(fPicture != NULL); | 222 SkASSERT(fPicture != NULL); |
| 225 int width = SkScalarCeilToInt(fPicture->width() * fScaleFactor); | 223 int width = SkScalarCeilToInt(fPicture->width() * fScaleFactor); |
| 226 if (fViewport.width() > 0) { | 224 if (fViewport.width() > 0) { |
| 227 width = SkMin32(width, fViewport.width()); | 225 width = SkMin32(width, fViewport.width()); |
| 228 } | 226 } |
| 229 return width; | 227 return width; |
| 230 } | 228 } |
| 231 | 229 |
| 232 int PictureRenderer::getViewHeight() { | 230 int PictureRenderer::getViewHeight() { |
| 233 SkASSERT(fPicture != NULL); | 231 SkASSERT(fPicture != NULL); |
| 234 int height = SkScalarCeilToInt(fPicture->height() * fScaleFactor); | 232 int height = SkScalarCeilToInt(fPicture->height() * fScaleFactor); |
| 235 if (fViewport.height() > 0) { | 233 if (fViewport.height() > 0) { |
| 236 height = SkMin32(height, fViewport.height()); | 234 height = SkMin32(height, fViewport.height()); |
| 237 } | 235 } |
| 238 return height; | 236 return height; |
| 239 } | 237 } |
| 240 | 238 |
| 241 /** Converts fPicture to a picture that uses a BBoxHierarchy. | 239 /** Converts fPicture to a picture that uses a BBoxHierarchy. |
| 242 * PictureRenderer subclasses that are used to test picture playback | 240 * PictureRenderer subclasses that are used to test picture playback |
| 243 * should call this method during init. | 241 * should call this method during init. |
| 244 */ | 242 */ |
| 245 void PictureRenderer::buildBBoxHierarchy() { | 243 void PictureRenderer::buildBBoxHierarchy() { |
| 246 SkASSERT(NULL != fPicture); | 244 SkASSERT(NULL != fPicture); |
| 247 if (kNone_BBoxHierarchyType != fBBoxHierarchyType && NULL != fPicture) { | 245 if (kNone_BBoxHierarchyType != fBBoxHierarchyType && NULL != fPicture) { |
| 248 SkPicture* newPicture = this->createPicture(); | 246 SkAutoTUnref<SkPictureFactory> factory(this->getFactory()); |
| 249 SkCanvas* recorder = newPicture->beginRecording(fPicture->width(), fPict
ure->height(), | 247 SkPictureRecorder recorder(factory); |
| 250 this->recordFlags()); | 248 SkCanvas* canvas = recorder.beginRecording(fPicture->width(), fPicture->
height(), |
| 251 fPicture->draw(recorder); | 249 this->recordFlags()); |
| 252 newPicture->endRecording(); | 250 fPicture->draw(canvas); |
| 253 fPicture->unref(); | 251 fPicture.reset(recorder.endRecording()); |
| 254 fPicture = newPicture; | |
| 255 } | 252 } |
| 256 } | 253 } |
| 257 | 254 |
| 258 void PictureRenderer::resetState(bool callFinish) { | 255 void PictureRenderer::resetState(bool callFinish) { |
| 259 #if SK_SUPPORT_GPU | 256 #if SK_SUPPORT_GPU |
| 260 SkGLContextHelper* glContext = this->getGLContext(); | 257 SkGLContextHelper* glContext = this->getGLContext(); |
| 261 if (NULL == glContext) { | 258 if (NULL == glContext) { |
| 262 SkASSERT(kBitmap_DeviceType == fDeviceType); | 259 SkASSERT(kBitmap_DeviceType == fDeviceType); |
| 263 return; | 260 return; |
| 264 } | 261 } |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 379 // defer the canvas setup until the render step | 376 // defer the canvas setup until the render step |
| 380 return NULL; | 377 return NULL; |
| 381 } | 378 } |
| 382 | 379 |
| 383 // the size_t* parameter is deprecated, so we ignore it | 380 // the size_t* parameter is deprecated, so we ignore it |
| 384 static SkData* encode_bitmap_to_data(size_t*, const SkBitmap& bm) { | 381 static SkData* encode_bitmap_to_data(size_t*, const SkBitmap& bm) { |
| 385 return SkImageEncoder::EncodeData(bm, SkImageEncoder::kPNG_Type, 100); | 382 return SkImageEncoder::EncodeData(bm, SkImageEncoder::kPNG_Type, 100); |
| 386 } | 383 } |
| 387 | 384 |
| 388 bool RecordPictureRenderer::render(SkBitmap** out) { | 385 bool RecordPictureRenderer::render(SkBitmap** out) { |
| 389 SkAutoTUnref<SkPicture> replayer(this->createPicture()); | 386 SkAutoTUnref<SkPictureFactory> factory(this->getFactory()); |
| 390 SkCanvas* recorder = replayer->beginRecording(this->getViewWidth(), this->ge
tViewHeight(), | 387 SkPictureRecorder recorder(factory); |
| 391 this->recordFlags()); | 388 SkCanvas* canvas = recorder.beginRecording(this->getViewWidth(), this->getVi
ewHeight(), |
| 392 this->scaleToScaleFactor(recorder); | 389 this->recordFlags()); |
| 393 fPicture->draw(recorder); | 390 this->scaleToScaleFactor(canvas); |
| 394 replayer->endRecording(); | 391 fPicture->draw(canvas); |
| 392 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 395 if (!fOutputDir.isEmpty()) { | 393 if (!fOutputDir.isEmpty()) { |
| 396 // Record the new picture as a new SKP with PNG encoded bitmaps. | 394 // Record the new picture as a new SKP with PNG encoded bitmaps. |
| 397 SkString skpPath; | 395 SkString skpPath; |
| 398 make_filepath(&skpPath, fOutputDir, fInputFilename); | 396 make_filepath(&skpPath, fOutputDir, fInputFilename); |
| 399 SkFILEWStream stream(skpPath.c_str()); | 397 SkFILEWStream stream(skpPath.c_str()); |
| 400 replayer->serialize(&stream, &encode_bitmap_to_data); | 398 picture->serialize(&stream, &encode_bitmap_to_data); |
| 401 return true; | 399 return true; |
| 402 } | 400 } |
| 403 return false; | 401 return false; |
| 404 } | 402 } |
| 405 | 403 |
| 406 SkString RecordPictureRenderer::getConfigNameInternal() { | 404 SkString RecordPictureRenderer::getConfigNameInternal() { |
| 407 return SkString("record"); | 405 return SkString("record"); |
| 408 } | 406 } |
| 409 | 407 |
| 410 ////////////////////////////////////////////////////////////////////////////////
/////////////// | 408 ////////////////////////////////////////////////////////////////////////////////
/////////////// |
| (...skipping 30 matching lines...) Expand all Loading... |
| 441 ////////////////////////////////////////////////////////////////////////////////
/////////////// | 439 ////////////////////////////////////////////////////////////////////////////////
/////////////// |
| 442 | 440 |
| 443 void SimplePictureRenderer::init(SkPicture* picture, const SkString* outputDir, | 441 void SimplePictureRenderer::init(SkPicture* picture, const SkString* outputDir, |
| 444 const SkString* inputFilename, bool useChecksum
BasedFilenames) { | 442 const SkString* inputFilename, bool useChecksum
BasedFilenames) { |
| 445 INHERITED::init(picture, outputDir, inputFilename, useChecksumBasedFilenames
); | 443 INHERITED::init(picture, outputDir, inputFilename, useChecksumBasedFilenames
); |
| 446 this->buildBBoxHierarchy(); | 444 this->buildBBoxHierarchy(); |
| 447 } | 445 } |
| 448 | 446 |
| 449 bool SimplePictureRenderer::render(SkBitmap** out) { | 447 bool SimplePictureRenderer::render(SkBitmap** out) { |
| 450 SkASSERT(fCanvas.get() != NULL); | 448 SkASSERT(fCanvas.get() != NULL); |
| 451 SkASSERT(fPicture != NULL); | 449 SkASSERT(NULL != fPicture); |
| 452 if (NULL == fCanvas.get() || NULL == fPicture) { | 450 if (NULL == fCanvas.get() || NULL == fPicture) { |
| 453 return false; | 451 return false; |
| 454 } | 452 } |
| 455 | 453 |
| 456 fCanvas->drawPicture(*fPicture); | 454 fCanvas->drawPicture(*fPicture); |
| 457 fCanvas->flush(); | 455 fCanvas->flush(); |
| 458 if (!fOutputDir.isEmpty()) { | 456 if (!fOutputDir.isEmpty()) { |
| 459 return write(fCanvas, fOutputDir, fInputFilename, fJsonSummaryPtr, | 457 return write(fCanvas, fOutputDir, fInputFilename, fJsonSummaryPtr, |
| 460 fUseChecksumBasedFilenames); | 458 fUseChecksumBasedFilenames); |
| 461 } | 459 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 480 , fTileHeight(kDefaultTileHeight) | 478 , fTileHeight(kDefaultTileHeight) |
| 481 , fTileWidthPercentage(0.0) | 479 , fTileWidthPercentage(0.0) |
| 482 , fTileHeightPercentage(0.0) | 480 , fTileHeightPercentage(0.0) |
| 483 , fTileMinPowerOf2Width(0) | 481 , fTileMinPowerOf2Width(0) |
| 484 , fCurrentTileOffset(-1) | 482 , fCurrentTileOffset(-1) |
| 485 , fTilesX(0) | 483 , fTilesX(0) |
| 486 , fTilesY(0) { } | 484 , fTilesY(0) { } |
| 487 | 485 |
| 488 void TiledPictureRenderer::init(SkPicture* pict, const SkString* outputDir, | 486 void TiledPictureRenderer::init(SkPicture* pict, const SkString* outputDir, |
| 489 const SkString* inputFilename, bool useChecksumB
asedFilenames) { | 487 const SkString* inputFilename, bool useChecksumB
asedFilenames) { |
| 490 SkASSERT(pict != NULL); | 488 SkASSERT(NULL != pict); |
| 491 SkASSERT(0 == fTileRects.count()); | 489 SkASSERT(0 == fTileRects.count()); |
| 492 if (NULL == pict || fTileRects.count() != 0) { | 490 if (NULL == pict || fTileRects.count() != 0) { |
| 493 return; | 491 return; |
| 494 } | 492 } |
| 495 | 493 |
| 496 // Do not call INHERITED::init(), which would create a (potentially large) c
anvas which is not | 494 // Do not call INHERITED::init(), which would create a (potentially large) c
anvas which is not |
| 497 // used by bench_pictures. | 495 // used by bench_pictures. |
| 498 fPicture = pict; | 496 fPicture.reset(pict)->ref(); |
| 499 this->CopyString(&fOutputDir, outputDir); | 497 this->CopyString(&fOutputDir, outputDir); |
| 500 this->CopyString(&fInputFilename, inputFilename); | 498 this->CopyString(&fInputFilename, inputFilename); |
| 501 fUseChecksumBasedFilenames = useChecksumBasedFilenames; | 499 fUseChecksumBasedFilenames = useChecksumBasedFilenames; |
| 502 fPicture->ref(); | |
| 503 this->buildBBoxHierarchy(); | 500 this->buildBBoxHierarchy(); |
| 504 | 501 |
| 505 if (fTileWidthPercentage > 0) { | 502 if (fTileWidthPercentage > 0) { |
| 506 fTileWidth = sk_float_ceil2int(float(fTileWidthPercentage * fPicture->wi
dth() / 100)); | 503 fTileWidth = sk_float_ceil2int(float(fTileWidthPercentage * fPicture->wi
dth() / 100)); |
| 507 } | 504 } |
| 508 if (fTileHeightPercentage > 0) { | 505 if (fTileHeightPercentage > 0) { |
| 509 fTileHeight = sk_float_ceil2int(float(fTileHeightPercentage * fPicture->
height() / 100)); | 506 fTileHeight = sk_float_ceil2int(float(fTileHeightPercentage * fPicture->
height() / 100)); |
| 510 } | 507 } |
| 511 | 508 |
| 512 if (fTileMinPowerOf2Width > 0) { | 509 if (fTileMinPowerOf2Width > 0) { |
| (...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 899 | 896 |
| 900 SkString MultiCorePictureRenderer::getConfigNameInternal() { | 897 SkString MultiCorePictureRenderer::getConfigNameInternal() { |
| 901 SkString name = this->INHERITED::getConfigNameInternal(); | 898 SkString name = this->INHERITED::getConfigNameInternal(); |
| 902 name.appendf("_multi_%i_threads", fNumThreads); | 899 name.appendf("_multi_%i_threads", fNumThreads); |
| 903 return name; | 900 return name; |
| 904 } | 901 } |
| 905 | 902 |
| 906 ////////////////////////////////////////////////////////////////////////////////
/////////////// | 903 ////////////////////////////////////////////////////////////////////////////////
/////////////// |
| 907 | 904 |
| 908 void PlaybackCreationRenderer::setup() { | 905 void PlaybackCreationRenderer::setup() { |
| 909 fReplayer.reset(this->createPicture()); | 906 SkAutoTUnref<SkPictureFactory> factory(this->getFactory()); |
| 910 SkCanvas* recorder = fReplayer->beginRecording(this->getViewWidth(), this->g
etViewHeight(), | 907 fRecorder.reset(SkNEW_ARGS(SkPictureRecorder, (factory))); |
| 911 this->recordFlags()); | 908 SkCanvas* canvas = fRecorder->beginRecording(this->getViewWidth(), this->get
ViewHeight(), |
| 912 this->scaleToScaleFactor(recorder); | 909 this->recordFlags()); |
| 913 fPicture->draw(recorder); | 910 this->scaleToScaleFactor(canvas); |
| 911 fPicture->draw(canvas); |
| 914 } | 912 } |
| 915 | 913 |
| 916 bool PlaybackCreationRenderer::render(SkBitmap** out) { | 914 bool PlaybackCreationRenderer::render(SkBitmap** out) { |
| 917 fReplayer->endRecording(); | 915 fPicture.reset(fRecorder->endRecording()); |
| 918 // Since this class does not actually render, return false. | 916 // Since this class does not actually render, return false. |
| 919 return false; | 917 return false; |
| 920 } | 918 } |
| 921 | 919 |
| 922 SkString PlaybackCreationRenderer::getConfigNameInternal() { | 920 SkString PlaybackCreationRenderer::getConfigNameInternal() { |
| 923 return SkString("playback_creation"); | 921 return SkString("playback_creation"); |
| 924 } | 922 } |
| 925 | 923 |
| 926 ////////////////////////////////////////////////////////////////////////////////
/////////////// | 924 ////////////////////////////////////////////////////////////////////////////////
/////////////// |
| 927 // SkPicture variants for each BBoxHierarchy type | 925 // SkPicture variants for each BBoxHierarchy type |
| 928 | 926 |
| 929 class RTreePicture : public SkPicture { | 927 class RTreePicture : public SkPicture { |
| 930 public: | 928 public: |
| 931 virtual SkBBoxHierarchy* createBBoxHierarchy() const SK_OVERRIDE{ | 929 virtual SkBBoxHierarchy* createBBoxHierarchy() const SK_OVERRIDE { |
| 932 static const int kRTreeMinChildren = 6; | 930 static const int kRTreeMinChildren = 6; |
| 933 static const int kRTreeMaxChildren = 11; | 931 static const int kRTreeMaxChildren = 11; |
| 934 SkScalar aspectRatio = SkScalarDiv(SkIntToScalar(fWidth), | 932 SkScalar aspectRatio = SkScalarDiv(SkIntToScalar(fWidth), |
| 935 SkIntToScalar(fHeight)); | 933 SkIntToScalar(fHeight)); |
| 936 bool sortDraws = false; | 934 bool sortDraws = false; |
| 937 return SkRTree::Create(kRTreeMinChildren, kRTreeMaxChildren, | 935 return SkRTree::Create(kRTreeMinChildren, kRTreeMaxChildren, |
| 938 aspectRatio, sortDraws); | 936 aspectRatio, sortDraws); |
| 939 } | 937 } |
| 940 }; | 938 }; |
| 941 | 939 |
| 942 SkPicture* PictureRenderer::createPicture() { | 940 class SkRTreePictureFactory : public SkPictureFactory { |
| 941 private: |
| 942 virtual SkPicture* create(int width, int height) SK_OVERRIDE { |
| 943 return SkNEW(RTreePicture); |
| 944 } |
| 945 |
| 946 private: |
| 947 typedef SkPictureFactory INHERITED; |
| 948 }; |
| 949 |
| 950 SkPictureFactory* PictureRenderer::getFactory() { |
| 943 switch (fBBoxHierarchyType) { | 951 switch (fBBoxHierarchyType) { |
| 944 case kNone_BBoxHierarchyType: | 952 case kNone_BBoxHierarchyType: |
| 945 return SkNEW(SkPicture); | 953 return NULL; |
| 946 case kQuadTree_BBoxHierarchyType: | 954 case kQuadTree_BBoxHierarchyType: |
| 947 return SkNEW_ARGS(SkQuadTreePicture, (SkIRect::MakeWH(fPicture->widt
h(), | 955 return SkNEW(SkQuadTreePictureFactory); |
| 948 fPicture->height()))); | |
| 949 case kRTree_BBoxHierarchyType: | 956 case kRTree_BBoxHierarchyType: |
| 950 return SkNEW(RTreePicture); | 957 return SkNEW(SkRTreePictureFactory); |
| 951 case kTileGrid_BBoxHierarchyType: | 958 case kTileGrid_BBoxHierarchyType: |
| 952 return SkNEW_ARGS(SkTileGridPicture, (fPicture->width(), | 959 return new SkTileGridPictureFactory(fGridInfo); |
| 953 fPicture->height(), fGridInfo)); | |
| 954 } | 960 } |
| 955 SkASSERT(0); // invalid bbhType | 961 SkASSERT(0); // invalid bbhType |
| 956 return NULL; | 962 return NULL; |
| 957 } | 963 } |
| 958 | 964 |
| 959 /////////////////////////////////////////////////////////////////////////////// | 965 /////////////////////////////////////////////////////////////////////////////// |
| 960 | 966 |
| 961 class GatherRenderer : public PictureRenderer { | 967 class GatherRenderer : public PictureRenderer { |
| 962 public: | 968 public: |
| 963 virtual bool render(SkBitmap** out = NULL) | 969 virtual bool render(SkBitmap** out = NULL) |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 998 virtual SkString getConfigNameInternal() SK_OVERRIDE { | 1004 virtual SkString getConfigNameInternal() SK_OVERRIDE { |
| 999 return SkString("picture_clone"); | 1005 return SkString("picture_clone"); |
| 1000 } | 1006 } |
| 1001 }; | 1007 }; |
| 1002 | 1008 |
| 1003 PictureRenderer* CreatePictureCloneRenderer() { | 1009 PictureRenderer* CreatePictureCloneRenderer() { |
| 1004 return SkNEW(PictureCloneRenderer); | 1010 return SkNEW(PictureCloneRenderer); |
| 1005 } | 1011 } |
| 1006 | 1012 |
| 1007 } // namespace sk_tools | 1013 } // namespace sk_tools |
| OLD | NEW |