epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2007 The Android Open Source Project |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
scroggo@google.com | 2983ddd | 2013-05-07 14:45:40 +0000 | [diff] [blame] | 9 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #include "SkPictureFlat.h" |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 11 | #include "SkPictureData.h" |
robertphillips | ce4dd3d | 2014-07-07 13:46:35 -0700 | [diff] [blame] | 12 | #include "SkPicturePlayback.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 13 | #include "SkPictureRecord.h" |
mtklein | 7373456 | 2014-06-24 12:28:34 -0700 | [diff] [blame] | 14 | #include "SkPictureRecorder.h" |
robertphillips | 3afef1f | 2014-07-08 06:12:22 -0700 | [diff] [blame] | 15 | #include "SkPictureStateTree.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 16 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 17 | #include "SkBitmapDevice.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 18 | #include "SkCanvas.h" |
| 19 | #include "SkChunkAlloc.h" |
Mike Klein | c11530e | 2014-06-24 11:29:06 -0400 | [diff] [blame] | 20 | #include "SkDrawPictureCallback.h" |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 21 | #include "SkPaintPriv.h" |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 22 | #include "SkPathEffect.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | #include "SkPicture.h" |
| 24 | #include "SkRegion.h" |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 25 | #include "SkShader.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 26 | #include "SkStream.h" |
| 27 | #include "SkTDArray.h" |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 28 | #include "SkTLogic.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 29 | #include "SkTSearch.h" |
| 30 | #include "SkTime.h" |
| 31 | |
| 32 | #include "SkReader32.h" |
| 33 | #include "SkWriter32.h" |
rileya@google.com | 8515e79 | 2012-09-13 21:41:51 +0000 | [diff] [blame] | 34 | #include "SkRTree.h" |
| 35 | #include "SkBBoxHierarchyRecord.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 36 | |
commit-bot@chromium.org | eb9547c | 2014-03-19 21:24:25 +0000 | [diff] [blame] | 37 | #if SK_SUPPORT_GPU |
| 38 | #include "GrContext.h" |
| 39 | #endif |
| 40 | |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 41 | #include "SkRecord.h" |
| 42 | #include "SkRecordDraw.h" |
Mike Klein | c11530e | 2014-06-24 11:29:06 -0400 | [diff] [blame] | 43 | #include "SkRecorder.h" |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 44 | |
commit-bot@chromium.org | 8f831f2 | 2014-04-23 22:35:42 +0000 | [diff] [blame] | 45 | template <typename T> int SafeCount(const T* obj) { |
| 46 | return obj ? obj->count() : 0; |
| 47 | } |
| 48 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 49 | /////////////////////////////////////////////////////////////////////////////// |
| 50 | |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 51 | namespace { |
| 52 | |
| 53 | // Some commands have a paint, some have an optional paint. Either way, get back a pointer. |
| 54 | static const SkPaint* AsPtr(const SkPaint& p) { return &p; } |
| 55 | static const SkPaint* AsPtr(const SkRecords::Optional<SkPaint>& p) { return p; } |
| 56 | |
| 57 | /** SkRecords visitor to determine whether an instance may require an |
| 58 | "external" bitmap to rasterize. May return false positives. |
| 59 | Does not return true for bitmap text. |
| 60 | |
| 61 | Expected use is to determine whether images need to be decoded before |
| 62 | rasterizing a particular SkRecord. |
| 63 | */ |
| 64 | struct BitmapTester { |
| 65 | // Helpers. These create HasMember_bitmap and HasMember_paint. |
| 66 | SK_CREATE_MEMBER_DETECTOR(bitmap); |
| 67 | SK_CREATE_MEMBER_DETECTOR(paint); |
| 68 | |
| 69 | |
| 70 | // Main entry for visitor: |
mtklein | 53fecfb | 2014-08-21 09:11:37 -0700 | [diff] [blame] | 71 | // If the command is a DrawPicture, recurse. |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 72 | // If the command has a bitmap directly, return true. |
| 73 | // If the command has a paint and the paint has a bitmap, return true. |
| 74 | // Otherwise, return false. |
mtklein | 53fecfb | 2014-08-21 09:11:37 -0700 | [diff] [blame] | 75 | bool operator()(const SkRecords::DrawPicture& op) { return op.picture->willPlayBackBitmaps(); } |
| 76 | |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 77 | template <typename T> |
| 78 | bool operator()(const T& r) { return CheckBitmap(r); } |
| 79 | |
| 80 | |
| 81 | // If the command has a bitmap, of course we're going to play back bitmaps. |
| 82 | template <typename T> |
| 83 | static SK_WHEN(HasMember_bitmap<T>, bool) CheckBitmap(const T&) { return true; } |
| 84 | |
| 85 | // If not, look for one in its paint (if it has a paint). |
| 86 | template <typename T> |
| 87 | static SK_WHEN(!HasMember_bitmap<T>, bool) CheckBitmap(const T& r) { return CheckPaint(r); } |
| 88 | |
| 89 | // If we have a paint, dig down into the effects looking for a bitmap. |
| 90 | template <typename T> |
| 91 | static SK_WHEN(HasMember_paint<T>, bool) CheckPaint(const T& r) { |
| 92 | const SkPaint* paint = AsPtr(r.paint); |
| 93 | if (paint) { |
| 94 | const SkShader* shader = paint->getShader(); |
| 95 | if (shader && |
| 96 | shader->asABitmap(NULL, NULL, NULL) == SkShader::kDefault_BitmapType) { |
| 97 | return true; |
| 98 | } |
| 99 | } |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | // If we don't have a paint, that non-paint has no bitmap. |
| 104 | template <typename T> |
| 105 | static SK_WHEN(!HasMember_paint<T>, bool) CheckPaint(const T&) { return false; } |
| 106 | }; |
| 107 | |
| 108 | bool WillPlaybackBitmaps(const SkRecord& record) { |
| 109 | BitmapTester tester; |
| 110 | for (unsigned i = 0; i < record.count(); i++) { |
| 111 | if (record.visit<bool>(i, tester)) { |
| 112 | return true; |
| 113 | } |
| 114 | } |
| 115 | return false; |
| 116 | } |
| 117 | |
mtklein | 53fecfb | 2014-08-21 09:11:37 -0700 | [diff] [blame] | 118 | // SkRecord visitor to find recorded text. |
| 119 | struct TextHunter { |
| 120 | // All ops with text have that text as a char array member named "text". |
| 121 | SK_CREATE_MEMBER_DETECTOR(text); |
| 122 | bool operator()(const SkRecords::DrawPicture& op) { return op.picture->hasText(); } |
| 123 | template <typename T> SK_WHEN(HasMember_text<T>, bool) operator()(const T&) { return true; } |
| 124 | template <typename T> SK_WHEN(!HasMember_text<T>, bool) operator()(const T&) { return false; } |
| 125 | }; |
| 126 | |
| 127 | } // namespace |
| 128 | |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 129 | /** SkRecords visitor to determine heuristically whether or not a SkPicture |
| 130 | will be performant when rasterized on the GPU. |
| 131 | */ |
mtklein | 53fecfb | 2014-08-21 09:11:37 -0700 | [diff] [blame] | 132 | struct SkPicture::PathCounter { |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 133 | SK_CREATE_MEMBER_DETECTOR(paint); |
| 134 | |
| 135 | PathCounter() |
| 136 | : numPaintWithPathEffectUses (0) |
| 137 | , numFastPathDashEffects (0) |
| 138 | , numAAConcavePaths (0) |
| 139 | , numAAHairlineConcavePaths (0) { |
| 140 | } |
| 141 | |
mtklein | 53fecfb | 2014-08-21 09:11:37 -0700 | [diff] [blame] | 142 | // Recurse into nested pictures. |
| 143 | void operator()(const SkRecords::DrawPicture& op) { |
| 144 | // If you're not also SkRecord-backed, tough luck. Get on the bandwagon. |
| 145 | if (op.picture->fRecord.get() == NULL) { |
| 146 | return; |
| 147 | } |
| 148 | const SkRecord& nested = *op.picture->fRecord; |
| 149 | for (unsigned i = 0; i < nested.count(); i++) { |
| 150 | nested.visit<void>(i, *this); |
| 151 | } |
| 152 | } |
| 153 | |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 154 | void checkPaint(const SkPaint* paint) { |
| 155 | if (paint && paint->getPathEffect()) { |
| 156 | numPaintWithPathEffectUses++; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | void operator()(const SkRecords::DrawPoints& op) { |
| 161 | this->checkPaint(&op.paint); |
| 162 | const SkPathEffect* effect = op.paint.getPathEffect(); |
| 163 | if (effect) { |
| 164 | SkPathEffect::DashInfo info; |
| 165 | SkPathEffect::DashType dashType = effect->asADash(&info); |
| 166 | if (2 == op.count && SkPaint::kRound_Cap != op.paint.getStrokeCap() && |
| 167 | SkPathEffect::kDash_DashType == dashType && 2 == info.fCount) { |
| 168 | numFastPathDashEffects++; |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | void operator()(const SkRecords::DrawPath& op) { |
| 174 | this->checkPaint(&op.paint); |
| 175 | if (op.paint.isAntiAlias() && !op.path.isConvex()) { |
| 176 | numAAConcavePaths++; |
| 177 | |
| 178 | if (SkPaint::kStroke_Style == op.paint.getStyle() && |
| 179 | 0 == op.paint.getStrokeWidth()) { |
| 180 | numAAHairlineConcavePaths++; |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | template <typename T> |
| 186 | SK_WHEN(HasMember_paint<T>, void) operator()(const T& op) { |
| 187 | this->checkPaint(AsPtr(op.paint)); |
| 188 | } |
| 189 | |
| 190 | template <typename T> |
| 191 | SK_WHEN(!HasMember_paint<T>, void) operator()(const T& op) { /* do nothing */ } |
| 192 | |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 193 | int numPaintWithPathEffectUses; |
| 194 | int numFastPathDashEffects; |
| 195 | int numAAConcavePaths; |
| 196 | int numAAHairlineConcavePaths; |
| 197 | }; |
| 198 | |
mtklein | c551d9f | 2014-08-20 08:09:46 -0700 | [diff] [blame] | 199 | SkPicture::Analysis::Analysis(const SkRecord& record) { |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 200 | fWillPlaybackBitmaps = WillPlaybackBitmaps(record); |
| 201 | |
| 202 | PathCounter counter; |
| 203 | for (unsigned i = 0; i < record.count(); i++) { |
| 204 | record.visit<void>(i, counter); |
| 205 | } |
| 206 | fNumPaintWithPathEffectUses = counter.numPaintWithPathEffectUses; |
mtklein | c551d9f | 2014-08-20 08:09:46 -0700 | [diff] [blame] | 207 | fNumFastPathDashEffects = counter.numFastPathDashEffects; |
| 208 | fNumAAConcavePaths = counter.numAAConcavePaths; |
| 209 | fNumAAHairlineConcavePaths = counter.numAAHairlineConcavePaths; |
| 210 | |
| 211 | fHasText = false; |
| 212 | TextHunter text; |
| 213 | for (unsigned i = 0; i < record.count(); i++) { |
| 214 | if (record.visit<bool>(i, text)) { |
| 215 | fHasText = true; |
| 216 | break; |
| 217 | } |
| 218 | } |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | bool SkPicture::Analysis::suitableForGpuRasterization(const char** reason, |
| 222 | int sampleCount) const { |
| 223 | // TODO: the heuristic used here needs to be refined |
| 224 | static const int kNumPaintWithPathEffectsUsesTol = 1; |
| 225 | static const int kNumAAConcavePathsTol = 5; |
| 226 | |
| 227 | int numNonDashedPathEffects = fNumPaintWithPathEffectUses - |
| 228 | fNumFastPathDashEffects; |
| 229 | bool suitableForDash = (0 == fNumPaintWithPathEffectUses) || |
| 230 | (numNonDashedPathEffects < kNumPaintWithPathEffectsUsesTol |
| 231 | && 0 == sampleCount); |
| 232 | |
| 233 | bool ret = suitableForDash && |
| 234 | (fNumAAConcavePaths - fNumAAHairlineConcavePaths) |
| 235 | < kNumAAConcavePathsTol; |
| 236 | |
| 237 | if (!ret && NULL != reason) { |
| 238 | if (!suitableForDash) { |
| 239 | if (0 != sampleCount) { |
| 240 | *reason = "Can't use multisample on dash effect."; |
| 241 | } else { |
| 242 | *reason = "Too many non dashed path effects."; |
| 243 | } |
| 244 | } else if ((fNumAAConcavePaths - fNumAAHairlineConcavePaths) |
| 245 | >= kNumAAConcavePathsTol) |
| 246 | *reason = "Too many anti-aliased concave paths."; |
| 247 | else |
| 248 | *reason = "Unknown reason for GPU unsuitability."; |
| 249 | } |
| 250 | return ret; |
| 251 | } |
| 252 | |
| 253 | /////////////////////////////////////////////////////////////////////////////// |
| 254 | |
Robert Phillips | cfaeec4 | 2014-07-13 12:00:50 -0400 | [diff] [blame] | 255 | #ifdef SK_SUPPORT_LEGACY_DEFAULT_PICTURE_CTOR |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 256 | // fRecord OK |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 257 | SkPicture::SkPicture() |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 258 | : fWidth(0) |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 259 | , fHeight(0) { |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 260 | this->needsNewGenID(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 261 | } |
Robert Phillips | cfaeec4 | 2014-07-13 12:00:50 -0400 | [diff] [blame] | 262 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 263 | |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 264 | // fRecord OK |
robertphillips | 0bdbea7 | 2014-06-11 11:37:55 -0700 | [diff] [blame] | 265 | SkPicture::SkPicture(int width, int height, |
robertphillips | e26e65e | 2014-06-12 05:51:22 -0700 | [diff] [blame] | 266 | const SkPictureRecord& record, |
robertphillips | 0bdbea7 | 2014-06-11 11:37:55 -0700 | [diff] [blame] | 267 | bool deepCopyOps) |
| 268 | : fWidth(width) |
Mike Klein | 27dc17c | 2014-08-18 18:35:16 -0400 | [diff] [blame] | 269 | , fHeight(height) |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 270 | , fAnalysis() { |
robertphillips | 0bdbea7 | 2014-06-11 11:37:55 -0700 | [diff] [blame] | 271 | this->needsNewGenID(); |
robertphillips | 9058d60 | 2014-06-10 11:45:46 -0700 | [diff] [blame] | 272 | |
robertphillips | 0bdbea7 | 2014-06-11 11:37:55 -0700 | [diff] [blame] | 273 | SkPictInfo info; |
| 274 | this->createHeader(&info); |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 275 | fData.reset(SkNEW_ARGS(SkPictureData, (record, info, deepCopyOps))); |
commit-bot@chromium.org | 2ee3c2c | 2014-05-19 12:36:15 +0000 | [diff] [blame] | 276 | } |
| 277 | |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 278 | // Create an SkPictureData-backed SkPicture from an SkRecord. |
mtklein | 7373456 | 2014-06-24 12:28:34 -0700 | [diff] [blame] | 279 | // This for compatibility with serialization code only. This is not cheap. |
| 280 | static SkPicture* backport(const SkRecord& src, int width, int height) { |
| 281 | SkPictureRecorder recorder; |
mtklein | c92e550 | 2014-08-21 13:07:27 -0700 | [diff] [blame^] | 282 | SkRecordDraw(src, |
| 283 | recorder.DEPRECATED_beginRecording(width, height), NULL/*bbh*/, NULL/*callback*/); |
mtklein | 7373456 | 2014-06-24 12:28:34 -0700 | [diff] [blame] | 284 | return recorder.endRecording(); |
| 285 | } |
| 286 | |
Mike Klein | c11530e | 2014-06-24 11:29:06 -0400 | [diff] [blame] | 287 | // fRecord OK |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 288 | SkPicture::~SkPicture() { |
| 289 | this->callDeletionListeners(); |
| 290 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 291 | |
mtklein | 7b705bb | 2014-08-20 14:22:58 -0700 | [diff] [blame] | 292 | // fRecord OK |
mtklein | d3e474e | 2014-06-27 12:34:44 -0700 | [diff] [blame] | 293 | #ifdef SK_SUPPORT_LEGACY_PICTURE_CLONE |
djsollen@google.com | c9ab987 | 2012-08-29 18:52:07 +0000 | [diff] [blame] | 294 | SkPicture* SkPicture::clone() const { |
mtklein | 3ba15ae | 2014-08-06 11:57:00 -0700 | [diff] [blame] | 295 | return SkRef(const_cast<SkPicture*>(this)); |
djsollen@google.com | c9ab987 | 2012-08-29 18:52:07 +0000 | [diff] [blame] | 296 | } |
mtklein | d3e474e | 2014-06-27 12:34:44 -0700 | [diff] [blame] | 297 | #endif//SK_SUPPORT_LEGACY_PICTURE_CLONE |
djsollen@google.com | c9ab987 | 2012-08-29 18:52:07 +0000 | [diff] [blame] | 298 | |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 299 | // fRecord OK |
| 300 | void SkPicture::EXPERIMENTAL_addAccelData(const SkPicture::AccelData* data) const { |
| 301 | fAccelData.reset(SkRef(data)); |
| 302 | } |
| 303 | |
| 304 | // fRecord OK |
| 305 | const SkPicture::AccelData* SkPicture::EXPERIMENTAL_getAccelData( |
| 306 | SkPicture::AccelData::Key key) const { |
| 307 | if (NULL != fAccelData.get() && fAccelData->getKey() == key) { |
| 308 | return fAccelData.get(); |
| 309 | } |
| 310 | return NULL; |
| 311 | } |
| 312 | |
| 313 | // fRecord OK |
commit-bot@chromium.org | 145d1c0 | 2014-03-16 19:46:36 +0000 | [diff] [blame] | 314 | SkPicture::AccelData::Domain SkPicture::AccelData::GenerateDomain() { |
| 315 | static int32_t gNextID = 0; |
| 316 | |
| 317 | int32_t id = sk_atomic_inc(&gNextID); |
| 318 | if (id >= 1 << (8 * sizeof(Domain))) { |
| 319 | SK_CRASH(); |
| 320 | } |
| 321 | |
| 322 | return static_cast<Domain>(id); |
| 323 | } |
| 324 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 325 | /////////////////////////////////////////////////////////////////////////////// |
| 326 | |
robertphillips | 3afef1f | 2014-07-08 06:12:22 -0700 | [diff] [blame] | 327 | uint32_t SkPicture::OperationList::offset(int index) const { |
| 328 | SkASSERT(index < fOps.count()); |
| 329 | return ((SkPictureStateTree::Draw*)fOps[index])->fOffset; |
| 330 | } |
| 331 | |
| 332 | const SkMatrix& SkPicture::OperationList::matrix(int index) const { |
| 333 | SkASSERT(index < fOps.count()); |
| 334 | return *((SkPictureStateTree::Draw*)fOps[index])->fMatrix; |
| 335 | } |
| 336 | |
mtklein | 7b705bb | 2014-08-20 14:22:58 -0700 | [diff] [blame] | 337 | // fRecord TODO(robert) / kind of OK in a non-optimal sense |
robertphillips | ce4dd3d | 2014-07-07 13:46:35 -0700 | [diff] [blame] | 338 | const SkPicture::OperationList* SkPicture::EXPERIMENTAL_getActiveOps(const SkIRect& queryRect) const { |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 339 | SkASSERT(NULL != fData.get()); |
| 340 | if (NULL != fData.get()) { |
| 341 | return fData->getActiveOps(queryRect); |
commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 342 | } |
robertphillips | ce4dd3d | 2014-07-07 13:46:35 -0700 | [diff] [blame] | 343 | return NULL; |
commit-bot@chromium.org | 75cf29b | 2014-03-24 19:40:49 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 346 | // fRecord OK |
| 347 | void SkPicture::draw(SkCanvas* canvas, SkDrawPictureCallback* callback) const { |
| 348 | SkASSERT(NULL != canvas); |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 349 | SkASSERT(NULL != fData.get() || NULL != fRecord.get()); |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 350 | |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 351 | // If the query contains the whole picture, don't bother with the BBH. |
| 352 | SkRect clipBounds = { 0, 0, 0, 0 }; |
| 353 | (void)canvas->getClipBounds(&clipBounds); |
| 354 | const bool useBBH = !clipBounds.contains(SkRect::MakeWH(this->width(), this->height())); |
| 355 | |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 356 | if (NULL != fData.get()) { |
robertphillips | ce4dd3d | 2014-07-07 13:46:35 -0700 | [diff] [blame] | 357 | SkPicturePlayback playback(this); |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 358 | playback.setUseBBH(useBBH); |
robertphillips | ce4dd3d | 2014-07-07 13:46:35 -0700 | [diff] [blame] | 359 | playback.draw(canvas, callback); |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 360 | } |
| 361 | if (NULL != fRecord.get()) { |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 362 | SkRecordDraw(*fRecord, canvas, useBBH ? fBBH.get() : NULL, callback); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | |
| 366 | /////////////////////////////////////////////////////////////////////////////// |
| 367 | |
| 368 | #include "SkStream.h" |
| 369 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 370 | static const char kMagic[] = { 's', 'k', 'i', 'a', 'p', 'i', 'c', 't' }; |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 371 | |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 372 | // fRecord OK |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 373 | bool SkPicture::IsValidPictInfo(const SkPictInfo& info) { |
| 374 | if (0 != memcmp(info.fMagic, kMagic, sizeof(kMagic))) { |
| 375 | return false; |
| 376 | } |
| 377 | |
| 378 | if (info.fVersion < MIN_PICTURE_VERSION || |
| 379 | info.fVersion > CURRENT_PICTURE_VERSION) { |
| 380 | return false; |
| 381 | } |
| 382 | |
| 383 | return true; |
| 384 | } |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 385 | |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 386 | // fRecord OK |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 387 | bool SkPicture::InternalOnly_StreamIsSKP(SkStream* stream, SkPictInfo* pInfo) { |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 388 | if (NULL == stream) { |
| 389 | return false; |
borenet@google.com | 66bcbd1 | 2012-09-17 18:26:06 +0000 | [diff] [blame] | 390 | } |
reed@google.com | 6756209 | 2012-06-22 15:38:39 +0000 | [diff] [blame] | 391 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 392 | // Check magic bytes. |
reed@google.com | 6756209 | 2012-06-22 15:38:39 +0000 | [diff] [blame] | 393 | SkPictInfo info; |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 394 | SkASSERT(sizeof(kMagic) == sizeof(info.fMagic)); |
| 395 | if (!stream->read(&info, sizeof(info)) || !IsValidPictInfo(info)) { |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 396 | return false; |
reed@google.com | 6756209 | 2012-06-22 15:38:39 +0000 | [diff] [blame] | 397 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 398 | |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 399 | if (pInfo != NULL) { |
| 400 | *pInfo = info; |
| 401 | } |
| 402 | return true; |
| 403 | } |
| 404 | |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 405 | // fRecord OK |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 406 | bool SkPicture::InternalOnly_BufferIsSKP(SkReadBuffer& buffer, SkPictInfo* pInfo) { |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 407 | // Check magic bytes. |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 408 | SkPictInfo info; |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 409 | SkASSERT(sizeof(kMagic) == sizeof(info.fMagic)); |
| 410 | if (!buffer.readByteArray(&info, sizeof(info)) || !IsValidPictInfo(info)) { |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 411 | return false; |
| 412 | } |
| 413 | |
| 414 | if (pInfo != NULL) { |
| 415 | *pInfo = info; |
| 416 | } |
| 417 | return true; |
| 418 | } |
| 419 | |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 420 | // fRecord OK |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 421 | SkPicture::SkPicture(SkPictureData* data, int width, int height) |
| 422 | : fData(data) |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 423 | , fWidth(width) |
Mike Klein | 27dc17c | 2014-08-18 18:35:16 -0400 | [diff] [blame] | 424 | , fHeight(height) |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 425 | , fAnalysis() { |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 426 | this->needsNewGenID(); |
| 427 | } |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 428 | |
mtklein | 7b705bb | 2014-08-20 14:22:58 -0700 | [diff] [blame] | 429 | SkPicture* SkPicture::Forwardport(const SkPicture& src) { |
| 430 | SkAutoTDelete<SkRecord> record(SkNEW(SkRecord)); |
| 431 | SkRecorder canvas(record.get(), src.width(), src.height()); |
| 432 | src.draw(&canvas); |
| 433 | return SkNEW_ARGS(SkPicture, (src.width(), src.height(), record.detach(), NULL/*bbh*/)); |
| 434 | } |
| 435 | |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 436 | // fRecord OK |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 437 | SkPicture* SkPicture::CreateFromStream(SkStream* stream, InstallPixelRefProc proc) { |
| 438 | SkPictInfo info; |
| 439 | |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 440 | if (!InternalOnly_StreamIsSKP(stream, &info)) { |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 441 | return NULL; |
| 442 | } |
| 443 | |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 444 | // Check to see if there is a playback to recreate. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 445 | if (stream->readBool()) { |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 446 | SkPictureData* data = SkPictureData::CreateFromStream(stream, info, proc); |
| 447 | if (NULL == data) { |
scroggo@google.com | 1270532 | 2013-10-01 15:30:46 +0000 | [diff] [blame] | 448 | return NULL; |
| 449 | } |
mtklein | 4082d29 | 2014-08-20 16:18:25 -0700 | [diff] [blame] | 450 | #if 0 |
mtklein | 1b523ba | 2014-08-20 15:50:45 -0700 | [diff] [blame] | 451 | const SkPicture src(data, info.fWidth, info.fHeight); |
| 452 | return Forwardport(src); |
mtklein | 4082d29 | 2014-08-20 16:18:25 -0700 | [diff] [blame] | 453 | #else |
| 454 | return SkNEW_ARGS(SkPicture, (data, info.fWidth, info.fHeight)); |
| 455 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 456 | } |
reed@google.com | 6756209 | 2012-06-22 15:38:39 +0000 | [diff] [blame] | 457 | |
robertphillips | e26e65e | 2014-06-12 05:51:22 -0700 | [diff] [blame] | 458 | return NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 461 | // fRecord OK |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 462 | SkPicture* SkPicture::CreateFromBuffer(SkReadBuffer& buffer) { |
| 463 | SkPictInfo info; |
| 464 | |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 465 | if (!InternalOnly_BufferIsSKP(buffer, &info)) { |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 466 | return NULL; |
| 467 | } |
| 468 | |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 469 | // Check to see if there is a playback to recreate. |
| 470 | if (buffer.readBool()) { |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 471 | SkPictureData* data = SkPictureData::CreateFromBuffer(buffer, info); |
| 472 | if (NULL == data) { |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 473 | return NULL; |
| 474 | } |
mtklein | 4082d29 | 2014-08-20 16:18:25 -0700 | [diff] [blame] | 475 | #if 0 |
mtklein | 1b523ba | 2014-08-20 15:50:45 -0700 | [diff] [blame] | 476 | const SkPicture src(data, info.fWidth, info.fHeight); |
| 477 | return Forwardport(src); |
mtklein | 4082d29 | 2014-08-20 16:18:25 -0700 | [diff] [blame] | 478 | #else |
| 479 | return SkNEW_ARGS(SkPicture, (data, info.fWidth, info.fHeight)); |
| 480 | #endif |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 481 | } |
| 482 | |
robertphillips | e26e65e | 2014-06-12 05:51:22 -0700 | [diff] [blame] | 483 | return NULL; |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 484 | } |
| 485 | |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 486 | // fRecord OK |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 487 | void SkPicture::createHeader(SkPictInfo* info) const { |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 488 | // Copy magic bytes at the beginning of the header |
| 489 | SkASSERT(sizeof(kMagic) == 8); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 490 | SkASSERT(sizeof(kMagic) == sizeof(info->fMagic)); |
| 491 | memcpy(info->fMagic, kMagic, sizeof(kMagic)); |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 492 | |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 493 | // Set picture info after magic bytes in the header |
commit-bot@chromium.org | e8d9614 | 2014-02-25 02:16:10 +0000 | [diff] [blame] | 494 | info->fVersion = CURRENT_PICTURE_VERSION; |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 495 | info->fWidth = fWidth; |
| 496 | info->fHeight = fHeight; |
| 497 | info->fFlags = SkPictInfo::kCrossProcess_Flag; |
| 498 | // TODO: remove this flag, since we're always float (now) |
| 499 | info->fFlags |= SkPictInfo::kScalarIsFloat_Flag; |
| 500 | |
| 501 | if (8 == sizeof(void*)) { |
| 502 | info->fFlags |= SkPictInfo::kPtrIs64Bit_Flag; |
| 503 | } |
| 504 | } |
| 505 | |
mtklein | 7373456 | 2014-06-24 12:28:34 -0700 | [diff] [blame] | 506 | // fRecord OK |
scroggo@google.com | 32ef131 | 2013-02-22 22:04:19 +0000 | [diff] [blame] | 507 | void SkPicture::serialize(SkWStream* stream, EncodeBitmap encoder) const { |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 508 | const SkPictureData* data = fData.get(); |
mtklein | 7373456 | 2014-06-24 12:28:34 -0700 | [diff] [blame] | 509 | |
| 510 | // If we're a new-format picture, backport to old format for serialization. |
| 511 | SkAutoTDelete<SkPicture> oldFormat; |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 512 | if (NULL == data && NULL != fRecord.get()) { |
mtklein | 7373456 | 2014-06-24 12:28:34 -0700 | [diff] [blame] | 513 | oldFormat.reset(backport(*fRecord, fWidth, fHeight)); |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 514 | data = oldFormat->fData.get(); |
| 515 | SkASSERT(NULL != data); |
mtklein | 7373456 | 2014-06-24 12:28:34 -0700 | [diff] [blame] | 516 | } |
| 517 | |
commit-bot@chromium.org | 2ee3c2c | 2014-05-19 12:36:15 +0000 | [diff] [blame] | 518 | SkPictInfo info; |
| 519 | this->createHeader(&info); |
commit-bot@chromium.org | 0943f5f | 2014-03-28 18:05:47 +0000 | [diff] [blame] | 520 | stream->write(&info, sizeof(info)); |
mtklein | 40684ba | 2014-06-24 10:12:39 -0700 | [diff] [blame] | 521 | |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 522 | if (NULL != data) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 523 | stream->writeBool(true); |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 524 | data->serialize(stream, encoder); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 525 | } else { |
| 526 | stream->writeBool(false); |
| 527 | } |
| 528 | } |
| 529 | |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 530 | // fRecord OK |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 531 | void SkPicture::flatten(SkWriteBuffer& buffer) const { |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 532 | const SkPictureData* data = fData.get(); |
mtklein | 7373456 | 2014-06-24 12:28:34 -0700 | [diff] [blame] | 533 | |
| 534 | // If we're a new-format picture, backport to old format for serialization. |
| 535 | SkAutoTDelete<SkPicture> oldFormat; |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 536 | if (NULL == data && NULL != fRecord.get()) { |
mtklein | 7373456 | 2014-06-24 12:28:34 -0700 | [diff] [blame] | 537 | oldFormat.reset(backport(*fRecord, fWidth, fHeight)); |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 538 | data = oldFormat->fData.get(); |
| 539 | SkASSERT(NULL != data); |
mtklein | 7373456 | 2014-06-24 12:28:34 -0700 | [diff] [blame] | 540 | } |
| 541 | |
commit-bot@chromium.org | 2ee3c2c | 2014-05-19 12:36:15 +0000 | [diff] [blame] | 542 | SkPictInfo info; |
| 543 | this->createHeader(&info); |
commit-bot@chromium.org | 0943f5f | 2014-03-28 18:05:47 +0000 | [diff] [blame] | 544 | buffer.writeByteArray(&info, sizeof(info)); |
mtklein | 40684ba | 2014-06-24 10:12:39 -0700 | [diff] [blame] | 545 | |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 546 | if (NULL != data) { |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 547 | buffer.writeBool(true); |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 548 | data->flatten(buffer); |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 549 | } else { |
| 550 | buffer.writeBool(false); |
| 551 | } |
| 552 | } |
| 553 | |
commit-bot@chromium.org | 1c30818 | 2014-03-19 22:54:40 +0000 | [diff] [blame] | 554 | #if SK_SUPPORT_GPU |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 555 | // fRecord OK |
commit-bot@chromium.org | a1ff26a | 2014-05-30 21:52:52 +0000 | [diff] [blame] | 556 | bool SkPicture::suitableForGpuRasterization(GrContext* context, const char **reason) const { |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 557 | if (fRecord.get()) { |
| 558 | return fAnalysis.suitableForGpuRasterization(reason, 0); |
| 559 | } |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 560 | if (NULL == fData.get()) { |
robertphillips | 0bdbea7 | 2014-06-11 11:37:55 -0700 | [diff] [blame] | 561 | if (NULL != reason) { |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 562 | *reason = "Missing internal data."; |
robertphillips | 0bdbea7 | 2014-06-11 11:37:55 -0700 | [diff] [blame] | 563 | } |
| 564 | return false; |
commit-bot@chromium.org | a1ff26a | 2014-05-30 21:52:52 +0000 | [diff] [blame] | 565 | } |
robertphillips | 0bdbea7 | 2014-06-11 11:37:55 -0700 | [diff] [blame] | 566 | |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 567 | return fData->suitableForGpuRasterization(context, reason); |
commit-bot@chromium.org | eb9547c | 2014-03-19 21:24:25 +0000 | [diff] [blame] | 568 | } |
commit-bot@chromium.org | 1c30818 | 2014-03-19 22:54:40 +0000 | [diff] [blame] | 569 | #endif |
commit-bot@chromium.org | eb9547c | 2014-03-19 21:24:25 +0000 | [diff] [blame] | 570 | |
mtklein | c551d9f | 2014-08-20 08:09:46 -0700 | [diff] [blame] | 571 | // fRecord OK |
ajuma | 750ae26 | 2014-08-18 12:59:55 -0700 | [diff] [blame] | 572 | bool SkPicture::hasText() const { |
mtklein | c551d9f | 2014-08-20 08:09:46 -0700 | [diff] [blame] | 573 | if (fRecord.get()) { |
| 574 | return fAnalysis.fHasText; |
| 575 | } |
| 576 | if (fData.get()) { |
| 577 | return fData->hasText(); |
| 578 | } |
| 579 | SkFAIL("Unreachable"); |
| 580 | return false; |
ajuma | 750ae26 | 2014-08-18 12:59:55 -0700 | [diff] [blame] | 581 | } |
| 582 | |
tomhudson | d930511 | 2014-07-05 13:37:53 -0700 | [diff] [blame] | 583 | // fRecord OK |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 584 | bool SkPicture::willPlayBackBitmaps() const { |
tomhudson | d930511 | 2014-07-05 13:37:53 -0700 | [diff] [blame] | 585 | if (fRecord.get()) { |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 586 | return fAnalysis.fWillPlaybackBitmaps; |
tomhudson | d930511 | 2014-07-05 13:37:53 -0700 | [diff] [blame] | 587 | } |
mtklein | c551d9f | 2014-08-20 08:09:46 -0700 | [diff] [blame] | 588 | if (fData.get()) { |
| 589 | return fData->containsBitmaps(); |
commit-bot@chromium.org | 145d1c0 | 2014-03-16 19:46:36 +0000 | [diff] [blame] | 590 | } |
mtklein | c551d9f | 2014-08-20 08:09:46 -0700 | [diff] [blame] | 591 | SkFAIL("Unreachable"); |
| 592 | return false; |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 595 | // fRecord OK |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 596 | static int32_t next_picture_generation_id() { |
| 597 | static int32_t gPictureGenerationID = 0; |
| 598 | // do a loop in case our global wraps around, as we never want to |
| 599 | // return a 0 |
| 600 | int32_t genID; |
| 601 | do { |
| 602 | genID = sk_atomic_inc(&gPictureGenerationID) + 1; |
commit-bot@chromium.org | 2b4e370 | 2014-04-07 18:26:22 +0000 | [diff] [blame] | 603 | } while (SK_InvalidGenID == genID); |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 604 | return genID; |
| 605 | } |
| 606 | |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 607 | // fRecord OK |
commit-bot@chromium.org | 2b4e370 | 2014-04-07 18:26:22 +0000 | [diff] [blame] | 608 | uint32_t SkPicture::uniqueID() const { |
commit-bot@chromium.org | 2b4e370 | 2014-04-07 18:26:22 +0000 | [diff] [blame] | 609 | if (SK_InvalidGenID == fUniqueID) { |
| 610 | fUniqueID = next_picture_generation_id(); |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 611 | } |
commit-bot@chromium.org | 2b4e370 | 2014-04-07 18:26:22 +0000 | [diff] [blame] | 612 | return fUniqueID; |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 613 | } |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 614 | |
| 615 | // fRecord OK |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 616 | SkPicture::SkPicture(int width, int height, SkRecord* record, SkBBoxHierarchy* bbh) |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 617 | : fWidth(width) |
| 618 | , fHeight(height) |
tomhudson | d930511 | 2014-07-05 13:37:53 -0700 | [diff] [blame] | 619 | , fRecord(record) |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 620 | , fBBH(SkSafeRef(bbh)) |
mtklein | c551d9f | 2014-08-20 08:09:46 -0700 | [diff] [blame] | 621 | , fAnalysis(*record) { |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 622 | // TODO: delay as much of this work until just before first playback? |
| 623 | if (fBBH.get()) { |
| 624 | SkRecordFillBounds(*record, fBBH.get()); |
| 625 | } |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 626 | this->needsNewGenID(); |
| 627 | } |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 628 | |
| 629 | // Note that we are assuming that this entry point will only be called from |
mtklein | 3ba15ae | 2014-08-06 11:57:00 -0700 | [diff] [blame] | 630 | // one thread. Currently the only client of this method is |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 631 | // SkGpuDevice::EXPERIMENTAL_optimize which should be only called from a single |
| 632 | // thread. |
| 633 | void SkPicture::addDeletionListener(DeletionListener* listener) const { |
| 634 | SkASSERT(NULL != listener); |
| 635 | |
| 636 | *fDeletionListeners.append() = SkRef(listener); |
| 637 | } |
| 638 | |
| 639 | void SkPicture::callDeletionListeners() { |
| 640 | for (int i = 0; i < fDeletionListeners.count(); ++i) { |
| 641 | fDeletionListeners[i]->onDeletion(this->uniqueID()); |
| 642 | } |
| 643 | |
| 644 | fDeletionListeners.unrefAll(); |
| 645 | } |
mtklein | 5a246bb | 2014-08-14 19:17:18 -0700 | [diff] [blame] | 646 | |
| 647 | // fRecord OK |
| 648 | int SkPicture::approximateOpCount() const { |
| 649 | SkASSERT(fRecord.get() || fData.get()); |
| 650 | if (fRecord.get()) { |
| 651 | return fRecord->count(); |
| 652 | } |
| 653 | if (fData.get()) { |
| 654 | return fData->opCount(); |
| 655 | } |
| 656 | return 0; |
| 657 | } |