OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 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 "SkBigPicture.h" | 8 #include "SkBigPicture.h" |
9 #include "SkData.h" | 9 #include "SkData.h" |
10 #include "SkDrawable.h" | 10 #include "SkDrawable.h" |
11 #include "SkImage.h" | |
11 #include "SkPictureRecorder.h" | 12 #include "SkPictureRecorder.h" |
12 #include "SkPictureUtils.h" | 13 #include "SkPictureUtils.h" |
13 #include "SkRecord.h" | 14 #include "SkRecord.h" |
14 #include "SkRecordDraw.h" | 15 #include "SkRecordDraw.h" |
15 #include "SkRecordOpts.h" | 16 #include "SkRecordOpts.h" |
16 #include "SkRecordedDrawable.h" | 17 #include "SkRecordedDrawable.h" |
17 #include "SkRecorder.h" | 18 #include "SkRecorder.h" |
18 #include "SkTypes.h" | 19 #include "SkTypes.h" |
20 #include "SkTLogic.h" | |
21 | |
22 namespace SkRecords { | |
23 | |
24 struct OptimizeFor { | |
25 GrContext* fCtx; | |
26 | |
27 // A few ops have a top-level SkImage: | |
28 void operator()(DrawAtlas* op) { this->make_texture(&op->atlas); } | |
29 void operator()(DrawImage* op) { this->make_texture(&op->image); } | |
30 void operator()(DrawImageNine* op) { this->make_texture(&op->image); } | |
31 void operator()(DrawImageRect* op) { this->make_texture(&op->image); } | |
32 void make_texture(sk_sp<const SkImage>* img) const { | |
33 *img = (*img)->makeTextureImage(fCtx); | |
34 } | |
35 | |
36 // Some ops have a paint, some have an optional paint. | |
37 // Either way, get back a pointer. | |
38 static SkPaint* AsPtr(SkPaint& p) { return &p; } | |
39 static SkPaint* AsPtr(SkRecords::Optional<SkPaint>& p) { return p; } | |
40 | |
41 // For all other types of ops, look for images inside the paint. | |
42 template <typename T> | |
43 SK_WHEN(T::kTags & kHasPaint_Tag, void) operator()(T* op) { | |
44 SkMatrix matrix; | |
45 SkShader::TileMode xy[2]; | |
46 | |
47 if (auto paint = AsPtr(op->paint)) | |
48 if (auto shader = paint->getShader()) | |
49 if (auto image = shader->isAImage(&matrix, xy)) { | |
50 paint->setShader(image->makeTextureImage(fCtx)->makeShader(xy[0] , xy[1], &matrix)); | |
51 } | |
52 | |
53 // TODO: re-build compose shaders | |
54 } | |
55 | |
56 // Control ops, etc. Nothing to do for these. | |
57 template <typename T> | |
58 SK_WHEN(!(T::kTags & kHasPaint_Tag), void) operator()(T*) {} | |
59 }; | |
60 | |
61 } // namespace SkRecords | |
62 | |
63 static void optimize_for(GrContext* ctx, SkRecord* record) { | |
64 for (int i = 0; ctx && i < record->count(); i++) { | |
djsollen
2016/08/02 12:37:56
don't you want to avoid this loop if ctx == nullpt
| |
65 record->mutate(i, SkRecords::OptimizeFor{ctx}); | |
66 } | |
67 } | |
19 | 68 |
20 SkPictureRecorder::SkPictureRecorder() { | 69 SkPictureRecorder::SkPictureRecorder() { |
21 fActivelyRecording = false; | 70 fActivelyRecording = false; |
22 fRecorder.reset(new SkRecorder(nullptr, SkRect::MakeWH(0, 0), &fMiniRecorder )); | 71 fRecorder.reset(new SkRecorder(nullptr, SkRect::MakeWH(0, 0), &fMiniRecorder )); |
23 } | 72 } |
24 | 73 |
25 SkPictureRecorder::~SkPictureRecorder() {} | 74 SkPictureRecorder::~SkPictureRecorder() {} |
26 | 75 |
27 SkCanvas* SkPictureRecorder::beginRecording(const SkRect& cullRect, | 76 SkCanvas* SkPictureRecorder::beginRecording(const SkRect& cullRect, |
28 SkBBHFactory* bbhFactory /* = nullpt r */, | 77 SkBBHFactory* bbhFactory /* = nullpt r */, |
(...skipping 27 matching lines...) Expand all Loading... | |
56 | 105 |
57 if (fRecord->count() == 0) { | 106 if (fRecord->count() == 0) { |
58 if (finishFlags & kReturnNullForEmpty_FinishFlag) { | 107 if (finishFlags & kReturnNullForEmpty_FinishFlag) { |
59 return nullptr; | 108 return nullptr; |
60 } | 109 } |
61 return fMiniRecorder.detachAsPicture(fCullRect); | 110 return fMiniRecorder.detachAsPicture(fCullRect); |
62 } | 111 } |
63 | 112 |
64 // TODO: delay as much of this work until just before first playback? | 113 // TODO: delay as much of this work until just before first playback? |
65 SkRecordOptimize(fRecord); | 114 SkRecordOptimize(fRecord); |
115 optimize_for(fGrContextToOptimizeFor, fRecord); | |
66 | 116 |
67 if (fRecord->count() == 0) { | 117 if (fRecord->count() == 0) { |
68 if (finishFlags & kReturnNullForEmpty_FinishFlag) { | 118 if (finishFlags & kReturnNullForEmpty_FinishFlag) { |
69 return nullptr; | 119 return nullptr; |
70 } | 120 } |
71 } | 121 } |
72 | 122 |
73 SkDrawableList* drawableList = fRecorder->getDrawableList(); | 123 SkDrawableList* drawableList = fRecorder->getDrawableList(); |
74 SkBigPicture::SnapshotArray* pictList = | 124 SkBigPicture::SnapshotArray* pictList = |
75 drawableList ? drawableList->newDrawableSnapshot() : nullptr; | 125 drawableList ? drawableList->newDrawableSnapshot() : nullptr; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
116 } | 166 } |
117 SkRecordDraw(*fRecord, canvas, nullptr, drawables, drawableCount, nullptr/*b bh*/, nullptr/*callback*/); | 167 SkRecordDraw(*fRecord, canvas, nullptr, drawables, drawableCount, nullptr/*b bh*/, nullptr/*callback*/); |
118 } | 168 } |
119 | 169 |
120 sk_sp<SkDrawable> SkPictureRecorder::finishRecordingAsDrawable(uint32_t finishFl ags) { | 170 sk_sp<SkDrawable> SkPictureRecorder::finishRecordingAsDrawable(uint32_t finishFl ags) { |
121 fActivelyRecording = false; | 171 fActivelyRecording = false; |
122 fRecorder->flushMiniRecorder(); | 172 fRecorder->flushMiniRecorder(); |
123 fRecorder->restoreToCount(1); // If we were missing any restores, add them now. | 173 fRecorder->restoreToCount(1); // If we were missing any restores, add them now. |
124 | 174 |
125 SkRecordOptimize(fRecord); | 175 SkRecordOptimize(fRecord); |
176 optimize_for(fGrContextToOptimizeFor, fRecord); | |
126 | 177 |
127 if (fRecord->count() == 0) { | 178 if (fRecord->count() == 0) { |
128 if (finishFlags & kReturnNullForEmpty_FinishFlag) { | 179 if (finishFlags & kReturnNullForEmpty_FinishFlag) { |
129 return nullptr; | 180 return nullptr; |
130 } | 181 } |
131 } | 182 } |
132 | 183 |
133 if (fBBH.get()) { | 184 if (fBBH.get()) { |
134 SkAutoTMalloc<SkRect> bounds(fRecord->count()); | 185 SkAutoTMalloc<SkRect> bounds(fRecord->count()); |
135 SkRecordFillBounds(fCullRect, *fRecord, bounds); | 186 SkRecordFillBounds(fCullRect, *fRecord, bounds); |
136 fBBH->insert(bounds, fRecord->count()); | 187 fBBH->insert(bounds, fRecord->count()); |
137 } | 188 } |
138 | 189 |
139 sk_sp<SkDrawable> drawable = | 190 sk_sp<SkDrawable> drawable = |
140 sk_make_sp<SkRecordedDrawable>(fRecord, fBBH, fRecorder->detachDrawable List(), fCullRect); | 191 sk_make_sp<SkRecordedDrawable>(fRecord, fBBH, fRecorder->detachDrawable List(), fCullRect); |
141 | 192 |
142 // release our refs now, so only the drawable will be the owner. | 193 // release our refs now, so only the drawable will be the owner. |
143 fRecord.reset(nullptr); | 194 fRecord.reset(nullptr); |
144 fBBH.reset(nullptr); | 195 fBBH.reset(nullptr); |
145 | 196 |
146 return drawable; | 197 return drawable; |
147 } | 198 } |
OLD | NEW |