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 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 } | 113 } |
114 | 114 |
115 void PictureRenderer::init(SkPicture* pict, const SkString* outputDir, | 115 void PictureRenderer::init(SkPicture* pict, const SkString* outputDir, |
116 const SkString* inputFilename, bool useChecksumBasedF
ilenames) { | 116 const SkString* inputFilename, bool useChecksumBasedF
ilenames) { |
117 this->CopyString(&fOutputDir, outputDir); | 117 this->CopyString(&fOutputDir, outputDir); |
118 this->CopyString(&fInputFilename, inputFilename); | 118 this->CopyString(&fInputFilename, inputFilename); |
119 fUseChecksumBasedFilenames = useChecksumBasedFilenames; | 119 fUseChecksumBasedFilenames = useChecksumBasedFilenames; |
120 | 120 |
121 SkASSERT(NULL == fPicture); | 121 SkASSERT(NULL == fPicture); |
122 SkASSERT(NULL == fCanvas.get()); | 122 SkASSERT(NULL == fCanvas.get()); |
123 if (fPicture != NULL || NULL != fCanvas.get()) { | 123 if (NULL != fPicture || NULL != fCanvas.get()) { |
124 return; | 124 return; |
125 } | 125 } |
126 | 126 |
127 SkASSERT(pict != NULL); | 127 SkASSERT(pict != NULL); |
128 if (NULL == pict) { | 128 if (NULL == pict) { |
129 return; | 129 return; |
130 } | 130 } |
131 | 131 |
132 fPicture = pict; | 132 fPicture.reset(pict)->ref(); |
133 fPicture->ref(); | |
134 fCanvas.reset(this->setupCanvas()); | 133 fCanvas.reset(this->setupCanvas()); |
135 } | 134 } |
136 | 135 |
137 void PictureRenderer::CopyString(SkString* dest, const SkString* src) { | 136 void PictureRenderer::CopyString(SkString* dest, const SkString* src) { |
138 if (NULL != src) { | 137 if (NULL != src) { |
139 dest->set(*src); | 138 dest->set(*src); |
140 } else { | 139 } else { |
141 dest->reset(); | 140 dest->reset(); |
142 } | 141 } |
143 } | 142 } |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 | 238 |
240 void PictureRenderer::scaleToScaleFactor(SkCanvas* canvas) { | 239 void PictureRenderer::scaleToScaleFactor(SkCanvas* canvas) { |
241 SkASSERT(canvas != NULL); | 240 SkASSERT(canvas != NULL); |
242 if (fScaleFactor != SK_Scalar1) { | 241 if (fScaleFactor != SK_Scalar1) { |
243 canvas->scale(fScaleFactor, fScaleFactor); | 242 canvas->scale(fScaleFactor, fScaleFactor); |
244 } | 243 } |
245 } | 244 } |
246 | 245 |
247 void PictureRenderer::end() { | 246 void PictureRenderer::end() { |
248 this->resetState(true); | 247 this->resetState(true); |
249 SkSafeUnref(fPicture); | 248 fPicture.reset(NULL); |
250 fPicture = NULL; | |
251 fCanvas.reset(NULL); | 249 fCanvas.reset(NULL); |
252 } | 250 } |
253 | 251 |
254 int PictureRenderer::getViewWidth() { | 252 int PictureRenderer::getViewWidth() { |
255 SkASSERT(fPicture != NULL); | 253 SkASSERT(fPicture != NULL); |
256 int width = SkScalarCeilToInt(fPicture->width() * fScaleFactor); | 254 int width = SkScalarCeilToInt(fPicture->width() * fScaleFactor); |
257 if (fViewport.width() > 0) { | 255 if (fViewport.width() > 0) { |
258 width = SkMin32(width, fViewport.width()); | 256 width = SkMin32(width, fViewport.width()); |
259 } | 257 } |
260 return width; | 258 return width; |
261 } | 259 } |
262 | 260 |
263 int PictureRenderer::getViewHeight() { | 261 int PictureRenderer::getViewHeight() { |
264 SkASSERT(fPicture != NULL); | 262 SkASSERT(fPicture != NULL); |
265 int height = SkScalarCeilToInt(fPicture->height() * fScaleFactor); | 263 int height = SkScalarCeilToInt(fPicture->height() * fScaleFactor); |
266 if (fViewport.height() > 0) { | 264 if (fViewport.height() > 0) { |
267 height = SkMin32(height, fViewport.height()); | 265 height = SkMin32(height, fViewport.height()); |
268 } | 266 } |
269 return height; | 267 return height; |
270 } | 268 } |
271 | 269 |
272 /** Converts fPicture to a picture that uses a BBoxHierarchy. | 270 /** Converts fPicture to a picture that uses a BBoxHierarchy. |
273 * PictureRenderer subclasses that are used to test picture playback | 271 * PictureRenderer subclasses that are used to test picture playback |
274 * should call this method during init. | 272 * should call this method during init. |
275 */ | 273 */ |
276 void PictureRenderer::buildBBoxHierarchy() { | 274 void PictureRenderer::buildBBoxHierarchy() { |
277 SkASSERT(NULL != fPicture); | 275 SkASSERT(NULL != fPicture); |
278 if (kNone_BBoxHierarchyType != fBBoxHierarchyType && NULL != fPicture) { | 276 if (kNone_BBoxHierarchyType != fBBoxHierarchyType && NULL != fPicture) { |
279 SkPicture* newPicture = this->createPicture(); | 277 SkAutoTUnref<SkPictureFactory> factory(this->getFactory()); |
280 SkCanvas* recorder = newPicture->beginRecording(fPicture->width(), fPict
ure->height(), | 278 SkPictureRecorder recorder(factory); |
281 this->recordFlags()); | 279 SkCanvas* canvas = recorder.beginRecording(fPicture->width(), fPicture->
height(), |
282 fPicture->draw(recorder); | 280 this->recordFlags()); |
283 newPicture->endRecording(); | 281 fPicture->draw(canvas); |
284 fPicture->unref(); | 282 fPicture.reset(recorder.endRecording()); |
285 fPicture = newPicture; | |
286 } | 283 } |
287 } | 284 } |
288 | 285 |
289 void PictureRenderer::resetState(bool callFinish) { | 286 void PictureRenderer::resetState(bool callFinish) { |
290 #if SK_SUPPORT_GPU | 287 #if SK_SUPPORT_GPU |
291 SkGLContextHelper* glContext = this->getGLContext(); | 288 SkGLContextHelper* glContext = this->getGLContext(); |
292 if (NULL == glContext) { | 289 if (NULL == glContext) { |
293 SkASSERT(kBitmap_DeviceType == fDeviceType); | 290 SkASSERT(kBitmap_DeviceType == fDeviceType); |
294 return; | 291 return; |
295 } | 292 } |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
431 // defer the canvas setup until the render step | 428 // defer the canvas setup until the render step |
432 return NULL; | 429 return NULL; |
433 } | 430 } |
434 | 431 |
435 // the size_t* parameter is deprecated, so we ignore it | 432 // the size_t* parameter is deprecated, so we ignore it |
436 static SkData* encode_bitmap_to_data(size_t*, const SkBitmap& bm) { | 433 static SkData* encode_bitmap_to_data(size_t*, const SkBitmap& bm) { |
437 return SkImageEncoder::EncodeData(bm, SkImageEncoder::kPNG_Type, 100); | 434 return SkImageEncoder::EncodeData(bm, SkImageEncoder::kPNG_Type, 100); |
438 } | 435 } |
439 | 436 |
440 bool RecordPictureRenderer::render(SkBitmap** out) { | 437 bool RecordPictureRenderer::render(SkBitmap** out) { |
441 SkAutoTUnref<SkPicture> replayer(this->createPicture()); | 438 SkAutoTUnref<SkPictureFactory> factory(this->getFactory()); |
442 SkCanvas* recorder = replayer->beginRecording(this->getViewWidth(), this->ge
tViewHeight(), | 439 SkPictureRecorder recorder(factory); |
443 this->recordFlags()); | 440 SkCanvas* canvas = recorder.beginRecording(this->getViewWidth(), this->getVi
ewHeight(), |
444 this->scaleToScaleFactor(recorder); | 441 this->recordFlags()); |
445 fPicture->draw(recorder); | 442 this->scaleToScaleFactor(canvas); |
446 replayer->endRecording(); | 443 fPicture->draw(canvas); |
| 444 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
447 if (!fOutputDir.isEmpty()) { | 445 if (!fOutputDir.isEmpty()) { |
448 // Record the new picture as a new SKP with PNG encoded bitmaps. | 446 // Record the new picture as a new SKP with PNG encoded bitmaps. |
449 SkString skpPath = SkOSPath::SkPathJoin(fOutputDir.c_str(), fInputFilena
me.c_str()); | 447 SkString skpPath = SkOSPath::SkPathJoin(fOutputDir.c_str(), fInputFilena
me.c_str()); |
450 SkFILEWStream stream(skpPath.c_str()); | 448 SkFILEWStream stream(skpPath.c_str()); |
451 replayer->serialize(&stream, &encode_bitmap_to_data); | 449 picture->serialize(&stream, &encode_bitmap_to_data); |
452 return true; | 450 return true; |
453 } | 451 } |
454 return false; | 452 return false; |
455 } | 453 } |
456 | 454 |
457 SkString RecordPictureRenderer::getConfigNameInternal() { | 455 SkString RecordPictureRenderer::getConfigNameInternal() { |
458 return SkString("record"); | 456 return SkString("record"); |
459 } | 457 } |
460 | 458 |
461 ////////////////////////////////////////////////////////////////////////////////
/////////////// | 459 ////////////////////////////////////////////////////////////////////////////////
/////////////// |
(...skipping 30 matching lines...) Expand all Loading... |
492 ////////////////////////////////////////////////////////////////////////////////
/////////////// | 490 ////////////////////////////////////////////////////////////////////////////////
/////////////// |
493 | 491 |
494 void SimplePictureRenderer::init(SkPicture* picture, const SkString* outputDir, | 492 void SimplePictureRenderer::init(SkPicture* picture, const SkString* outputDir, |
495 const SkString* inputFilename, bool useChecksum
BasedFilenames) { | 493 const SkString* inputFilename, bool useChecksum
BasedFilenames) { |
496 INHERITED::init(picture, outputDir, inputFilename, useChecksumBasedFilenames
); | 494 INHERITED::init(picture, outputDir, inputFilename, useChecksumBasedFilenames
); |
497 this->buildBBoxHierarchy(); | 495 this->buildBBoxHierarchy(); |
498 } | 496 } |
499 | 497 |
500 bool SimplePictureRenderer::render(SkBitmap** out) { | 498 bool SimplePictureRenderer::render(SkBitmap** out) { |
501 SkASSERT(fCanvas.get() != NULL); | 499 SkASSERT(fCanvas.get() != NULL); |
502 SkASSERT(fPicture != NULL); | 500 SkASSERT(NULL != fPicture); |
503 if (NULL == fCanvas.get() || NULL == fPicture) { | 501 if (NULL == fCanvas.get() || NULL == fPicture) { |
504 return false; | 502 return false; |
505 } | 503 } |
506 | 504 |
507 fCanvas->drawPicture(*fPicture); | 505 fCanvas->drawPicture(*fPicture); |
508 fCanvas->flush(); | 506 fCanvas->flush(); |
509 if (!fOutputDir.isEmpty()) { | 507 if (!fOutputDir.isEmpty()) { |
510 return write(fCanvas, fOutputDir, fInputFilename, fJsonSummaryPtr, | 508 return write(fCanvas, fOutputDir, fInputFilename, fJsonSummaryPtr, |
511 fUseChecksumBasedFilenames); | 509 fUseChecksumBasedFilenames); |
512 } | 510 } |
(...skipping 18 matching lines...) Expand all Loading... |
531 , fTileHeight(kDefaultTileHeight) | 529 , fTileHeight(kDefaultTileHeight) |
532 , fTileWidthPercentage(0.0) | 530 , fTileWidthPercentage(0.0) |
533 , fTileHeightPercentage(0.0) | 531 , fTileHeightPercentage(0.0) |
534 , fTileMinPowerOf2Width(0) | 532 , fTileMinPowerOf2Width(0) |
535 , fCurrentTileOffset(-1) | 533 , fCurrentTileOffset(-1) |
536 , fTilesX(0) | 534 , fTilesX(0) |
537 , fTilesY(0) { } | 535 , fTilesY(0) { } |
538 | 536 |
539 void TiledPictureRenderer::init(SkPicture* pict, const SkString* outputDir, | 537 void TiledPictureRenderer::init(SkPicture* pict, const SkString* outputDir, |
540 const SkString* inputFilename, bool useChecksumB
asedFilenames) { | 538 const SkString* inputFilename, bool useChecksumB
asedFilenames) { |
541 SkASSERT(pict != NULL); | 539 SkASSERT(NULL != pict); |
542 SkASSERT(0 == fTileRects.count()); | 540 SkASSERT(0 == fTileRects.count()); |
543 if (NULL == pict || fTileRects.count() != 0) { | 541 if (NULL == pict || fTileRects.count() != 0) { |
544 return; | 542 return; |
545 } | 543 } |
546 | 544 |
547 // Do not call INHERITED::init(), which would create a (potentially large) c
anvas which is not | 545 // Do not call INHERITED::init(), which would create a (potentially large) c
anvas which is not |
548 // used by bench_pictures. | 546 // used by bench_pictures. |
549 fPicture = SkRef(pict); | 547 fPicture.reset(pict)->ref(); |
550 this->CopyString(&fOutputDir, outputDir); | 548 this->CopyString(&fOutputDir, outputDir); |
551 this->CopyString(&fInputFilename, inputFilename); | 549 this->CopyString(&fInputFilename, inputFilename); |
552 fUseChecksumBasedFilenames = useChecksumBasedFilenames; | 550 fUseChecksumBasedFilenames = useChecksumBasedFilenames; |
553 this->buildBBoxHierarchy(); | 551 this->buildBBoxHierarchy(); |
554 | 552 |
555 if (fTileWidthPercentage > 0) { | 553 if (fTileWidthPercentage > 0) { |
556 fTileWidth = sk_float_ceil2int(float(fTileWidthPercentage * fPicture->wi
dth() / 100)); | 554 fTileWidth = sk_float_ceil2int(float(fTileWidthPercentage * fPicture->wi
dth() / 100)); |
557 } | 555 } |
558 if (fTileHeightPercentage > 0) { | 556 if (fTileHeightPercentage > 0) { |
559 fTileHeight = sk_float_ceil2int(float(fTileHeightPercentage * fPicture->
height() / 100)); | 557 fTileHeight = sk_float_ceil2int(float(fTileHeightPercentage * fPicture->
height() / 100)); |
(...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
949 | 947 |
950 SkString MultiCorePictureRenderer::getConfigNameInternal() { | 948 SkString MultiCorePictureRenderer::getConfigNameInternal() { |
951 SkString name = this->INHERITED::getConfigNameInternal(); | 949 SkString name = this->INHERITED::getConfigNameInternal(); |
952 name.appendf("_multi_%i_threads", fNumThreads); | 950 name.appendf("_multi_%i_threads", fNumThreads); |
953 return name; | 951 return name; |
954 } | 952 } |
955 | 953 |
956 ////////////////////////////////////////////////////////////////////////////////
/////////////// | 954 ////////////////////////////////////////////////////////////////////////////////
/////////////// |
957 | 955 |
958 void PlaybackCreationRenderer::setup() { | 956 void PlaybackCreationRenderer::setup() { |
959 fReplayer.reset(this->createPicture()); | 957 SkAutoTUnref<SkPictureFactory> factory(this->getFactory()); |
960 SkCanvas* recorder = fReplayer->beginRecording(this->getViewWidth(), this->g
etViewHeight(), | 958 fRecorder.reset(SkNEW_ARGS(SkPictureRecorder, (factory))); |
961 this->recordFlags()); | 959 SkCanvas* canvas = fRecorder->beginRecording(this->getViewWidth(), this->get
ViewHeight(), |
962 this->scaleToScaleFactor(recorder); | 960 this->recordFlags()); |
963 recorder->drawPicture(*fPicture); | 961 this->scaleToScaleFactor(canvas); |
| 962 canvas->drawPicture(*fPicture); |
964 } | 963 } |
965 | 964 |
966 bool PlaybackCreationRenderer::render(SkBitmap** out) { | 965 bool PlaybackCreationRenderer::render(SkBitmap** out) { |
967 fReplayer->endRecording(); | 966 fPicture.reset(fRecorder->endRecording()); |
968 // Since this class does not actually render, return false. | 967 // Since this class does not actually render, return false. |
969 return false; | 968 return false; |
970 } | 969 } |
971 | 970 |
972 SkString PlaybackCreationRenderer::getConfigNameInternal() { | 971 SkString PlaybackCreationRenderer::getConfigNameInternal() { |
973 return SkString("playback_creation"); | 972 return SkString("playback_creation"); |
974 } | 973 } |
975 | 974 |
976 ////////////////////////////////////////////////////////////////////////////////
/////////////// | 975 ////////////////////////////////////////////////////////////////////////////////
/////////////// |
977 // SkPicture variants for each BBoxHierarchy type | 976 // SkPicture variants for each BBoxHierarchy type |
978 | 977 |
979 class RTreePicture : public SkPicture { | 978 class RTreePicture : public SkPicture { |
980 public: | 979 public: |
981 virtual SkBBoxHierarchy* createBBoxHierarchy() const SK_OVERRIDE{ | 980 virtual SkBBoxHierarchy* createBBoxHierarchy() const SK_OVERRIDE { |
982 static const int kRTreeMinChildren = 6; | 981 static const int kRTreeMinChildren = 6; |
983 static const int kRTreeMaxChildren = 11; | 982 static const int kRTreeMaxChildren = 11; |
984 SkScalar aspectRatio = SkScalarDiv(SkIntToScalar(fWidth), | 983 SkScalar aspectRatio = SkScalarDiv(SkIntToScalar(fWidth), |
985 SkIntToScalar(fHeight)); | 984 SkIntToScalar(fHeight)); |
986 bool sortDraws = false; | 985 bool sortDraws = false; |
987 return SkRTree::Create(kRTreeMinChildren, kRTreeMaxChildren, | 986 return SkRTree::Create(kRTreeMinChildren, kRTreeMaxChildren, |
988 aspectRatio, sortDraws); | 987 aspectRatio, sortDraws); |
989 } | 988 } |
990 }; | 989 }; |
991 | 990 |
992 SkPicture* PictureRenderer::createPicture() { | 991 class SkRTreePictureFactory : public SkPictureFactory { |
| 992 private: |
| 993 virtual SkPicture* create(int width, int height) SK_OVERRIDE { |
| 994 return SkNEW(RTreePicture); |
| 995 } |
| 996 |
| 997 private: |
| 998 typedef SkPictureFactory INHERITED; |
| 999 }; |
| 1000 |
| 1001 SkPictureFactory* PictureRenderer::getFactory() { |
993 switch (fBBoxHierarchyType) { | 1002 switch (fBBoxHierarchyType) { |
994 case kNone_BBoxHierarchyType: | 1003 case kNone_BBoxHierarchyType: |
995 return SkNEW(SkPicture); | 1004 return NULL; |
996 case kQuadTree_BBoxHierarchyType: | 1005 case kQuadTree_BBoxHierarchyType: |
997 return SkNEW_ARGS(SkQuadTreePicture, (SkIRect::MakeWH(fPicture->widt
h(), | 1006 return SkNEW(SkQuadTreePictureFactory); |
998 fPicture->heig
ht()))); | |
999 case kRTree_BBoxHierarchyType: | 1007 case kRTree_BBoxHierarchyType: |
1000 return SkNEW(RTreePicture); | 1008 return SkNEW(SkRTreePictureFactory); |
1001 case kTileGrid_BBoxHierarchyType: | 1009 case kTileGrid_BBoxHierarchyType: |
1002 return SkNEW_ARGS(SkTileGridPicture, (fPicture->width(), | 1010 return new SkTileGridPictureFactory(fGridInfo); |
1003 fPicture->height(), fGridInfo)
); | |
1004 } | 1011 } |
1005 SkASSERT(0); // invalid bbhType | 1012 SkASSERT(0); // invalid bbhType |
1006 return NULL; | 1013 return NULL; |
1007 } | 1014 } |
1008 | 1015 |
1009 /////////////////////////////////////////////////////////////////////////////// | 1016 /////////////////////////////////////////////////////////////////////////////// |
1010 | 1017 |
1011 class GatherRenderer : public PictureRenderer { | 1018 class GatherRenderer : public PictureRenderer { |
1012 public: | 1019 public: |
1013 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE { | 1020 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1046 virtual SkString getConfigNameInternal() SK_OVERRIDE { | 1053 virtual SkString getConfigNameInternal() SK_OVERRIDE { |
1047 return SkString("picture_clone"); | 1054 return SkString("picture_clone"); |
1048 } | 1055 } |
1049 }; | 1056 }; |
1050 | 1057 |
1051 PictureRenderer* CreatePictureCloneRenderer() { | 1058 PictureRenderer* CreatePictureCloneRenderer() { |
1052 return SkNEW(PictureCloneRenderer); | 1059 return SkNEW(PictureCloneRenderer); |
1053 } | 1060 } |
1054 | 1061 |
1055 } // namespace sk_tools | 1062 } // namespace sk_tools |
OLD | NEW |