OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 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 "DMSrcSink.h" | 8 #include "DMSrcSink.h" |
9 #include "SamplePipeControllers.h" | 9 #include "SamplePipeControllers.h" |
10 #include "SkCodec.h" | 10 #include "SkCodec.h" |
11 #include "SkCommonFlags.h" | 11 #include "SkCommonFlags.h" |
12 #include "SkData.h" | 12 #include "SkData.h" |
13 #include "SkDeferredCanvas.h" | 13 #include "SkDeferredCanvas.h" |
14 #include "SkDocument.h" | 14 #include "SkDocument.h" |
15 #include "SkError.h" | 15 #include "SkError.h" |
16 #include "SkImageGenerator.h" | 16 #include "SkImageGenerator.h" |
17 #include "SkMultiPictureDraw.h" | 17 #include "SkMultiPictureDraw.h" |
18 #include "SkNullCanvas.h" | 18 #include "SkNullCanvas.h" |
19 #include "SkOSFile.h" | 19 #include "SkOSFile.h" |
20 #include "SkPictureData.h" | 20 #include "SkPictureData.h" |
21 #include "SkPictureRecorder.h" | 21 #include "SkPictureRecorder.h" |
22 #include "SkRandom.h" | 22 #include "SkRandom.h" |
| 23 #include "SkRecordDraw.h" |
| 24 #include "SkRecorder.h" |
23 #include "SkSVGCanvas.h" | 25 #include "SkSVGCanvas.h" |
24 #include "SkScanlineDecoder.h" | 26 #include "SkScanlineDecoder.h" |
25 #include "SkStream.h" | 27 #include "SkStream.h" |
26 #include "SkXMLWriter.h" | 28 #include "SkXMLWriter.h" |
27 | 29 |
28 DEFINE_bool(multiPage, false, "For document-type backends, render the source" | 30 DEFINE_bool(multiPage, false, "For document-type backends, render the source" |
29 " into multiple pages"); | 31 " into multiple pages"); |
30 | 32 |
31 static bool lazy_decode_bitmap(const void* src, size_t size, SkBitmap* dst) { | 33 static bool lazy_decode_bitmap(const void* src, size_t size, SkBitmap* dst) { |
32 SkAutoTUnref<SkData> encoded(SkData::NewWithCopy(src, size)); | 34 SkAutoTUnref<SkData> encoded(SkData::NewWithCopy(src, size)); |
(...skipping 684 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
717 if (!err.isEmpty()) { | 719 if (!err.isEmpty()) { |
718 return err; | 720 return err; |
719 } | 721 } |
720 pic.reset(recorder.endRecordingAsPicture()); | 722 pic.reset(recorder.endRecordingAsPicture()); |
721 } | 723 } |
722 canvas->drawPicture(pic); | 724 canvas->drawPicture(pic); |
723 return ""; | 725 return ""; |
724 }); | 726 }); |
725 } | 727 } |
726 | 728 |
| 729 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~*/ |
| 730 |
| 731 // This is like SkRecords::Draw, in that it plays back SkRecords ops into a Canv
as. |
| 732 // Unlike SkRecords::Draw, it builds a single-op sub-picture out of each Draw-ty
pe op. |
| 733 // This is an only-slightly-exaggerated simluation of Blink's Slimming Paint pic
tures. |
| 734 struct DrawsAsSingletonPictures { |
| 735 SkCanvas* fCanvas; |
| 736 |
| 737 SK_CREATE_MEMBER_DETECTOR(paint); |
| 738 |
| 739 template <typename T> |
| 740 void draw(const T& op, SkCanvas* canvas) { |
| 741 // We must pass SkMatrix::I() as our initial matrix. |
| 742 // By default SkRecords::Draw() uses the canvas' matrix as its initial m
atrix, |
| 743 // which would have the funky effect of applying transforms over and ove
r. |
| 744 SkRecords::Draw(canvas, nullptr, nullptr, 0, &SkMatrix::I())(op); |
| 745 } |
| 746 |
| 747 // Most things that have paints are Draw-type ops. Create sub-pictures for
each. |
| 748 template <typename T> |
| 749 SK_WHEN(HasMember_paint<T>, void) operator()(const T& op) { |
| 750 SkPictureRecorder rec; |
| 751 this->draw(op, rec.beginRecording(SkRect::MakeLargest())); |
| 752 SkAutoTUnref<SkPicture> pic(rec.endRecordingAsPicture()); |
| 753 fCanvas->drawPicture(pic); |
| 754 } |
| 755 |
| 756 // If you don't have a paint or are a SaveLayer, you're not a Draw-type op. |
| 757 // We cannot make subpictures out of these because they affect state. Draw
them directly. |
| 758 template <typename T> |
| 759 SK_WHEN(!HasMember_paint<T>, void) operator()(const T& op) { this->draw(op,
fCanvas); } |
| 760 void operator()(const SkRecords::SaveLayer& op) { this->draw(op,
fCanvas); } |
| 761 }; |
| 762 |
| 763 ViaSingletonPictures::ViaSingletonPictures(Sink* sink) : fSink(sink) {} |
| 764 |
| 765 // Record Src into a picture, then record it into a macro picture with a sub-pic
ture for each draw. |
| 766 // Then play back that macro picture into our wrapped sink. |
| 767 Error ViaSingletonPictures::draw( |
| 768 const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) cons
t { |
| 769 auto size = src.size(); |
| 770 return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas
) -> Error { |
| 771 // Use low-level (Skia-private) recording APIs so we can read the SkReco
rd. |
| 772 SkRecord skr; |
| 773 SkRecorder recorder(&skr, size.width(), size.height()); |
| 774 Error err = src.draw(&recorder); |
| 775 if (!err.isEmpty()) { |
| 776 return err; |
| 777 } |
| 778 |
| 779 // Record our macro-picture, with each draw op as its own sub-picture. |
| 780 SkPictureRecorder macroRec; |
| 781 SkCanvas* macroCanvas = macroRec.beginRecording(SkIntToScalar(size.width
()), |
| 782 SkIntToScalar(size.heigh
t())); |
| 783 DrawsAsSingletonPictures drawsAsSingletonPictures = { macroCanvas }; |
| 784 for (unsigned i = 0; i < skr.count(); i++) { |
| 785 skr.visit<void>(i, drawsAsSingletonPictures); |
| 786 } |
| 787 SkAutoTUnref<SkPicture> macroPic(macroRec.endRecordingAsPicture()); |
| 788 |
| 789 canvas->drawPicture(macroPic); |
| 790 return ""; |
| 791 }); |
| 792 } |
| 793 |
727 } // namespace DM | 794 } // namespace DM |
OLD | NEW |