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 | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 282 | SkRecordDraw(src, recorder.beginRecording(width, height), NULL/*bbh*/, NULL/*callback*/); |
mtklein | 7373456 | 2014-06-24 12:28:34 -0700 | [diff] [blame] | 283 | return recorder.endRecording(); |
| 284 | } |
| 285 | |
Mike Klein | c11530e | 2014-06-24 11:29:06 -0400 | [diff] [blame] | 286 | // fRecord OK |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 287 | SkPicture::~SkPicture() { |
| 288 | this->callDeletionListeners(); |
| 289 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 290 | |
mtklein | 7b705bb | 2014-08-20 14:22:58 -0700 | [diff] [blame] | 291 | // fRecord OK |
mtklein | d3e474e | 2014-06-27 12:34:44 -0700 | [diff] [blame] | 292 | #ifdef SK_SUPPORT_LEGACY_PICTURE_CLONE |
djsollen@google.com | c9ab987 | 2012-08-29 18:52:07 +0000 | [diff] [blame] | 293 | SkPicture* SkPicture::clone() const { |
mtklein | 3ba15ae | 2014-08-06 11:57:00 -0700 | [diff] [blame] | 294 | return SkRef(const_cast<SkPicture*>(this)); |
djsollen@google.com | c9ab987 | 2012-08-29 18:52:07 +0000 | [diff] [blame] | 295 | } |
mtklein | d3e474e | 2014-06-27 12:34:44 -0700 | [diff] [blame] | 296 | #endif//SK_SUPPORT_LEGACY_PICTURE_CLONE |
djsollen@google.com | c9ab987 | 2012-08-29 18:52:07 +0000 | [diff] [blame] | 297 | |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 298 | // fRecord OK |
| 299 | void SkPicture::EXPERIMENTAL_addAccelData(const SkPicture::AccelData* data) const { |
| 300 | fAccelData.reset(SkRef(data)); |
| 301 | } |
| 302 | |
| 303 | // fRecord OK |
| 304 | const SkPicture::AccelData* SkPicture::EXPERIMENTAL_getAccelData( |
| 305 | SkPicture::AccelData::Key key) const { |
| 306 | if (NULL != fAccelData.get() && fAccelData->getKey() == key) { |
| 307 | return fAccelData.get(); |
| 308 | } |
| 309 | return NULL; |
| 310 | } |
| 311 | |
| 312 | // fRecord OK |
commit-bot@chromium.org | 145d1c0 | 2014-03-16 19:46:36 +0000 | [diff] [blame] | 313 | SkPicture::AccelData::Domain SkPicture::AccelData::GenerateDomain() { |
| 314 | static int32_t gNextID = 0; |
| 315 | |
| 316 | int32_t id = sk_atomic_inc(&gNextID); |
| 317 | if (id >= 1 << (8 * sizeof(Domain))) { |
| 318 | SK_CRASH(); |
| 319 | } |
| 320 | |
| 321 | return static_cast<Domain>(id); |
| 322 | } |
| 323 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 324 | /////////////////////////////////////////////////////////////////////////////// |
| 325 | |
robertphillips | 3afef1f | 2014-07-08 06:12:22 -0700 | [diff] [blame] | 326 | uint32_t SkPicture::OperationList::offset(int index) const { |
| 327 | SkASSERT(index < fOps.count()); |
| 328 | return ((SkPictureStateTree::Draw*)fOps[index])->fOffset; |
| 329 | } |
| 330 | |
| 331 | const SkMatrix& SkPicture::OperationList::matrix(int index) const { |
| 332 | SkASSERT(index < fOps.count()); |
| 333 | return *((SkPictureStateTree::Draw*)fOps[index])->fMatrix; |
| 334 | } |
| 335 | |
mtklein | 7b705bb | 2014-08-20 14:22:58 -0700 | [diff] [blame] | 336 | // fRecord TODO(robert) / kind of OK in a non-optimal sense |
robertphillips | ce4dd3d | 2014-07-07 13:46:35 -0700 | [diff] [blame] | 337 | const SkPicture::OperationList* SkPicture::EXPERIMENTAL_getActiveOps(const SkIRect& queryRect) const { |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 338 | SkASSERT(NULL != fData.get()); |
| 339 | if (NULL != fData.get()) { |
| 340 | return fData->getActiveOps(queryRect); |
commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 341 | } |
robertphillips | ce4dd3d | 2014-07-07 13:46:35 -0700 | [diff] [blame] | 342 | return NULL; |
commit-bot@chromium.org | 75cf29b | 2014-03-24 19:40:49 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 345 | // fRecord OK |
| 346 | void SkPicture::draw(SkCanvas* canvas, SkDrawPictureCallback* callback) const { |
| 347 | SkASSERT(NULL != canvas); |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 348 | SkASSERT(NULL != fData.get() || NULL != fRecord.get()); |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 349 | |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 350 | // If the query contains the whole picture, don't bother with the BBH. |
| 351 | SkRect clipBounds = { 0, 0, 0, 0 }; |
| 352 | (void)canvas->getClipBounds(&clipBounds); |
| 353 | const bool useBBH = !clipBounds.contains(SkRect::MakeWH(this->width(), this->height())); |
| 354 | |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 355 | if (NULL != fData.get()) { |
robertphillips | ce4dd3d | 2014-07-07 13:46:35 -0700 | [diff] [blame] | 356 | SkPicturePlayback playback(this); |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 357 | playback.setUseBBH(useBBH); |
robertphillips | ce4dd3d | 2014-07-07 13:46:35 -0700 | [diff] [blame] | 358 | playback.draw(canvas, callback); |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 359 | } |
| 360 | if (NULL != fRecord.get()) { |
mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 361 | SkRecordDraw(*fRecord, canvas, useBBH ? fBBH.get() : NULL, callback); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
| 365 | /////////////////////////////////////////////////////////////////////////////// |
| 366 | |
| 367 | #include "SkStream.h" |
| 368 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 369 | 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] | 370 | |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 371 | // fRecord OK |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 372 | bool SkPicture::IsValidPictInfo(const SkPictInfo& info) { |
| 373 | if (0 != memcmp(info.fMagic, kMagic, sizeof(kMagic))) { |
| 374 | return false; |
| 375 | } |
| 376 | |
| 377 | if (info.fVersion < MIN_PICTURE_VERSION || |
| 378 | info.fVersion > CURRENT_PICTURE_VERSION) { |
| 379 | return false; |
| 380 | } |
| 381 | |
| 382 | return true; |
| 383 | } |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 384 | |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 385 | // fRecord OK |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 386 | bool SkPicture::InternalOnly_StreamIsSKP(SkStream* stream, SkPictInfo* pInfo) { |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 387 | if (NULL == stream) { |
| 388 | return false; |
borenet@google.com | 66bcbd1 | 2012-09-17 18:26:06 +0000 | [diff] [blame] | 389 | } |
reed@google.com | 6756209 | 2012-06-22 15:38:39 +0000 | [diff] [blame] | 390 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 391 | // Check magic bytes. |
reed@google.com | 6756209 | 2012-06-22 15:38:39 +0000 | [diff] [blame] | 392 | SkPictInfo info; |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 393 | SkASSERT(sizeof(kMagic) == sizeof(info.fMagic)); |
| 394 | if (!stream->read(&info, sizeof(info)) || !IsValidPictInfo(info)) { |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 395 | return false; |
reed@google.com | 6756209 | 2012-06-22 15:38:39 +0000 | [diff] [blame] | 396 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 397 | |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 398 | if (pInfo != NULL) { |
| 399 | *pInfo = info; |
| 400 | } |
| 401 | return true; |
| 402 | } |
| 403 | |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 404 | // fRecord OK |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 405 | bool SkPicture::InternalOnly_BufferIsSKP(SkReadBuffer& buffer, SkPictInfo* pInfo) { |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 406 | // Check magic bytes. |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 407 | SkPictInfo info; |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 408 | SkASSERT(sizeof(kMagic) == sizeof(info.fMagic)); |
| 409 | if (!buffer.readByteArray(&info, sizeof(info)) || !IsValidPictInfo(info)) { |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 410 | return false; |
| 411 | } |
| 412 | |
| 413 | if (pInfo != NULL) { |
| 414 | *pInfo = info; |
| 415 | } |
| 416 | return true; |
| 417 | } |
| 418 | |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 419 | // fRecord OK |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 420 | SkPicture::SkPicture(SkPictureData* data, int width, int height) |
| 421 | : fData(data) |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 422 | , fWidth(width) |
Mike Klein | 27dc17c | 2014-08-18 18:35:16 -0400 | [diff] [blame] | 423 | , fHeight(height) |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 424 | , fAnalysis() { |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 425 | this->needsNewGenID(); |
| 426 | } |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 427 | |
mtklein | 7b705bb | 2014-08-20 14:22:58 -0700 | [diff] [blame] | 428 | SkPicture* SkPicture::Forwardport(const SkPicture& src) { |
| 429 | SkAutoTDelete<SkRecord> record(SkNEW(SkRecord)); |
| 430 | SkRecorder canvas(record.get(), src.width(), src.height()); |
| 431 | src.draw(&canvas); |
| 432 | return SkNEW_ARGS(SkPicture, (src.width(), src.height(), record.detach(), NULL/*bbh*/)); |
| 433 | } |
| 434 | |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 435 | // fRecord OK |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 436 | SkPicture* SkPicture::CreateFromStream(SkStream* stream, InstallPixelRefProc proc) { |
| 437 | SkPictInfo info; |
| 438 | |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 439 | if (!InternalOnly_StreamIsSKP(stream, &info)) { |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 440 | return NULL; |
| 441 | } |
| 442 | |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 443 | // Check to see if there is a playback to recreate. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 444 | if (stream->readBool()) { |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 445 | SkPictureData* data = SkPictureData::CreateFromStream(stream, info, proc); |
| 446 | if (NULL == data) { |
scroggo@google.com | 1270532 | 2013-10-01 15:30:46 +0000 | [diff] [blame] | 447 | return NULL; |
| 448 | } |
mtklein | 4082d29 | 2014-08-20 16:18:25 -0700 | [diff] [blame] | 449 | #if 0 |
mtklein | 1b523ba | 2014-08-20 15:50:45 -0700 | [diff] [blame] | 450 | const SkPicture src(data, info.fWidth, info.fHeight); |
| 451 | return Forwardport(src); |
mtklein | 4082d29 | 2014-08-20 16:18:25 -0700 | [diff] [blame] | 452 | #else |
| 453 | return SkNEW_ARGS(SkPicture, (data, info.fWidth, info.fHeight)); |
| 454 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 455 | } |
reed@google.com | 6756209 | 2012-06-22 15:38:39 +0000 | [diff] [blame] | 456 | |
robertphillips | e26e65e | 2014-06-12 05:51:22 -0700 | [diff] [blame] | 457 | return NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 460 | // fRecord OK |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 461 | SkPicture* SkPicture::CreateFromBuffer(SkReadBuffer& buffer) { |
| 462 | SkPictInfo info; |
| 463 | |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 464 | if (!InternalOnly_BufferIsSKP(buffer, &info)) { |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 465 | return NULL; |
| 466 | } |
| 467 | |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 468 | // Check to see if there is a playback to recreate. |
| 469 | if (buffer.readBool()) { |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 470 | SkPictureData* data = SkPictureData::CreateFromBuffer(buffer, info); |
| 471 | if (NULL == data) { |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 472 | return NULL; |
| 473 | } |
mtklein | 4082d29 | 2014-08-20 16:18:25 -0700 | [diff] [blame] | 474 | #if 0 |
mtklein | 1b523ba | 2014-08-20 15:50:45 -0700 | [diff] [blame] | 475 | const SkPicture src(data, info.fWidth, info.fHeight); |
| 476 | return Forwardport(src); |
mtklein | 4082d29 | 2014-08-20 16:18:25 -0700 | [diff] [blame] | 477 | #else |
| 478 | return SkNEW_ARGS(SkPicture, (data, info.fWidth, info.fHeight)); |
| 479 | #endif |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 480 | } |
| 481 | |
robertphillips | e26e65e | 2014-06-12 05:51:22 -0700 | [diff] [blame] | 482 | return NULL; |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 485 | // fRecord OK |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 486 | void SkPicture::createHeader(SkPictInfo* info) const { |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 487 | // Copy magic bytes at the beginning of the header |
| 488 | SkASSERT(sizeof(kMagic) == 8); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 489 | SkASSERT(sizeof(kMagic) == sizeof(info->fMagic)); |
| 490 | memcpy(info->fMagic, kMagic, sizeof(kMagic)); |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 491 | |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 492 | // Set picture info after magic bytes in the header |
commit-bot@chromium.org | e8d9614 | 2014-02-25 02:16:10 +0000 | [diff] [blame] | 493 | info->fVersion = CURRENT_PICTURE_VERSION; |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 494 | info->fWidth = fWidth; |
| 495 | info->fHeight = fHeight; |
| 496 | info->fFlags = SkPictInfo::kCrossProcess_Flag; |
| 497 | // TODO: remove this flag, since we're always float (now) |
| 498 | info->fFlags |= SkPictInfo::kScalarIsFloat_Flag; |
| 499 | |
| 500 | if (8 == sizeof(void*)) { |
| 501 | info->fFlags |= SkPictInfo::kPtrIs64Bit_Flag; |
| 502 | } |
| 503 | } |
| 504 | |
mtklein | 7373456 | 2014-06-24 12:28:34 -0700 | [diff] [blame] | 505 | // fRecord OK |
scroggo@google.com | 32ef131 | 2013-02-22 22:04:19 +0000 | [diff] [blame] | 506 | void SkPicture::serialize(SkWStream* stream, EncodeBitmap encoder) const { |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 507 | const SkPictureData* data = fData.get(); |
mtklein | 7373456 | 2014-06-24 12:28:34 -0700 | [diff] [blame] | 508 | |
| 509 | // If we're a new-format picture, backport to old format for serialization. |
| 510 | SkAutoTDelete<SkPicture> oldFormat; |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 511 | if (NULL == data && NULL != fRecord.get()) { |
mtklein | 7373456 | 2014-06-24 12:28:34 -0700 | [diff] [blame] | 512 | oldFormat.reset(backport(*fRecord, fWidth, fHeight)); |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 513 | data = oldFormat->fData.get(); |
| 514 | SkASSERT(NULL != data); |
mtklein | 7373456 | 2014-06-24 12:28:34 -0700 | [diff] [blame] | 515 | } |
| 516 | |
commit-bot@chromium.org | 2ee3c2c | 2014-05-19 12:36:15 +0000 | [diff] [blame] | 517 | SkPictInfo info; |
| 518 | this->createHeader(&info); |
commit-bot@chromium.org | 0943f5f | 2014-03-28 18:05:47 +0000 | [diff] [blame] | 519 | stream->write(&info, sizeof(info)); |
mtklein | 40684ba | 2014-06-24 10:12:39 -0700 | [diff] [blame] | 520 | |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 521 | if (NULL != data) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 522 | stream->writeBool(true); |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 523 | data->serialize(stream, encoder); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 524 | } else { |
| 525 | stream->writeBool(false); |
| 526 | } |
| 527 | } |
| 528 | |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 529 | // fRecord OK |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 530 | void SkPicture::flatten(SkWriteBuffer& buffer) const { |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 531 | const SkPictureData* data = fData.get(); |
mtklein | 7373456 | 2014-06-24 12:28:34 -0700 | [diff] [blame] | 532 | |
| 533 | // If we're a new-format picture, backport to old format for serialization. |
| 534 | SkAutoTDelete<SkPicture> oldFormat; |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 535 | if (NULL == data && NULL != fRecord.get()) { |
mtklein | 7373456 | 2014-06-24 12:28:34 -0700 | [diff] [blame] | 536 | oldFormat.reset(backport(*fRecord, fWidth, fHeight)); |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 537 | data = oldFormat->fData.get(); |
| 538 | SkASSERT(NULL != data); |
mtklein | 7373456 | 2014-06-24 12:28:34 -0700 | [diff] [blame] | 539 | } |
| 540 | |
commit-bot@chromium.org | 2ee3c2c | 2014-05-19 12:36:15 +0000 | [diff] [blame] | 541 | SkPictInfo info; |
| 542 | this->createHeader(&info); |
commit-bot@chromium.org | 0943f5f | 2014-03-28 18:05:47 +0000 | [diff] [blame] | 543 | buffer.writeByteArray(&info, sizeof(info)); |
mtklein | 40684ba | 2014-06-24 10:12:39 -0700 | [diff] [blame] | 544 | |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 545 | if (NULL != data) { |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 546 | buffer.writeBool(true); |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 547 | data->flatten(buffer); |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 548 | } else { |
| 549 | buffer.writeBool(false); |
| 550 | } |
| 551 | } |
| 552 | |
commit-bot@chromium.org | 1c30818 | 2014-03-19 22:54:40 +0000 | [diff] [blame] | 553 | #if SK_SUPPORT_GPU |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 554 | // fRecord OK |
commit-bot@chromium.org | a1ff26a | 2014-05-30 21:52:52 +0000 | [diff] [blame] | 555 | bool SkPicture::suitableForGpuRasterization(GrContext* context, const char **reason) const { |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 556 | if (fRecord.get()) { |
| 557 | return fAnalysis.suitableForGpuRasterization(reason, 0); |
| 558 | } |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 559 | if (NULL == fData.get()) { |
robertphillips | 0bdbea7 | 2014-06-11 11:37:55 -0700 | [diff] [blame] | 560 | if (NULL != reason) { |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 561 | *reason = "Missing internal data."; |
robertphillips | 0bdbea7 | 2014-06-11 11:37:55 -0700 | [diff] [blame] | 562 | } |
| 563 | return false; |
commit-bot@chromium.org | a1ff26a | 2014-05-30 21:52:52 +0000 | [diff] [blame] | 564 | } |
robertphillips | 0bdbea7 | 2014-06-11 11:37:55 -0700 | [diff] [blame] | 565 | |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 566 | return fData->suitableForGpuRasterization(context, reason); |
commit-bot@chromium.org | eb9547c | 2014-03-19 21:24:25 +0000 | [diff] [blame] | 567 | } |
commit-bot@chromium.org | 1c30818 | 2014-03-19 22:54:40 +0000 | [diff] [blame] | 568 | #endif |
commit-bot@chromium.org | eb9547c | 2014-03-19 21:24:25 +0000 | [diff] [blame] | 569 | |
mtklein | c551d9f | 2014-08-20 08:09:46 -0700 | [diff] [blame] | 570 | // fRecord OK |
ajuma | 750ae26 | 2014-08-18 12:59:55 -0700 | [diff] [blame] | 571 | bool SkPicture::hasText() const { |
mtklein | c551d9f | 2014-08-20 08:09:46 -0700 | [diff] [blame] | 572 | if (fRecord.get()) { |
| 573 | return fAnalysis.fHasText; |
| 574 | } |
| 575 | if (fData.get()) { |
| 576 | return fData->hasText(); |
| 577 | } |
| 578 | SkFAIL("Unreachable"); |
| 579 | return false; |
ajuma | 750ae26 | 2014-08-18 12:59:55 -0700 | [diff] [blame] | 580 | } |
| 581 | |
tomhudson | d930511 | 2014-07-05 13:37:53 -0700 | [diff] [blame] | 582 | // fRecord OK |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 583 | bool SkPicture::willPlayBackBitmaps() const { |
tomhudson | d930511 | 2014-07-05 13:37:53 -0700 | [diff] [blame] | 584 | if (fRecord.get()) { |
tomhudson | 3a0f279 | 2014-08-20 05:29:41 -0700 | [diff] [blame] | 585 | return fAnalysis.fWillPlaybackBitmaps; |
tomhudson | d930511 | 2014-07-05 13:37:53 -0700 | [diff] [blame] | 586 | } |
mtklein | c551d9f | 2014-08-20 08:09:46 -0700 | [diff] [blame] | 587 | if (fData.get()) { |
| 588 | return fData->containsBitmaps(); |
commit-bot@chromium.org | 145d1c0 | 2014-03-16 19:46:36 +0000 | [diff] [blame] | 589 | } |
mtklein | c551d9f | 2014-08-20 08:09:46 -0700 | [diff] [blame] | 590 | SkFAIL("Unreachable"); |
| 591 | return false; |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 594 | // fRecord OK |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 595 | static int32_t next_picture_generation_id() { |
| 596 | static int32_t gPictureGenerationID = 0; |
| 597 | // do a loop in case our global wraps around, as we never want to |
| 598 | // return a 0 |
| 599 | int32_t genID; |
| 600 | do { |
| 601 | genID = sk_atomic_inc(&gPictureGenerationID) + 1; |
commit-bot@chromium.org | 2b4e370 | 2014-04-07 18:26:22 +0000 | [diff] [blame] | 602 | } while (SK_InvalidGenID == genID); |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 603 | return genID; |
| 604 | } |
| 605 | |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 606 | // fRecord OK |
commit-bot@chromium.org | 2b4e370 | 2014-04-07 18:26:22 +0000 | [diff] [blame] | 607 | uint32_t SkPicture::uniqueID() const { |
commit-bot@chromium.org | 2b4e370 | 2014-04-07 18:26:22 +0000 | [diff] [blame] | 608 | if (SK_InvalidGenID == fUniqueID) { |
| 609 | fUniqueID = next_picture_generation_id(); |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 610 | } |
commit-bot@chromium.org | 2b4e370 | 2014-04-07 18:26:22 +0000 | [diff] [blame] | 611 | return fUniqueID; |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 612 | } |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 613 | |
| 614 | // fRecord OK |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 615 | SkPicture::SkPicture(int width, int height, SkRecord* record, SkBBoxHierarchy* bbh) |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 616 | : fWidth(width) |
| 617 | , fHeight(height) |
tomhudson | d930511 | 2014-07-05 13:37:53 -0700 | [diff] [blame] | 618 | , fRecord(record) |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 619 | , fBBH(SkSafeRef(bbh)) |
mtklein | c551d9f | 2014-08-20 08:09:46 -0700 | [diff] [blame] | 620 | , fAnalysis(*record) { |
mtklein | 5ad6ee1 | 2014-08-11 08:08:43 -0700 | [diff] [blame] | 621 | // TODO: delay as much of this work until just before first playback? |
| 622 | if (fBBH.get()) { |
| 623 | SkRecordFillBounds(*record, fBBH.get()); |
| 624 | } |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 625 | this->needsNewGenID(); |
| 626 | } |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 627 | |
| 628 | // 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] | 629 | // one thread. Currently the only client of this method is |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 630 | // SkGpuDevice::EXPERIMENTAL_optimize which should be only called from a single |
| 631 | // thread. |
| 632 | void SkPicture::addDeletionListener(DeletionListener* listener) const { |
| 633 | SkASSERT(NULL != listener); |
| 634 | |
| 635 | *fDeletionListeners.append() = SkRef(listener); |
| 636 | } |
| 637 | |
| 638 | void SkPicture::callDeletionListeners() { |
| 639 | for (int i = 0; i < fDeletionListeners.count(); ++i) { |
| 640 | fDeletionListeners[i]->onDeletion(this->uniqueID()); |
| 641 | } |
| 642 | |
| 643 | fDeletionListeners.unrefAll(); |
| 644 | } |
mtklein | 5a246bb | 2014-08-14 19:17:18 -0700 | [diff] [blame] | 645 | |
| 646 | // fRecord OK |
| 647 | int SkPicture::approximateOpCount() const { |
| 648 | SkASSERT(fRecord.get() || fData.get()); |
| 649 | if (fRecord.get()) { |
| 650 | return fRecord->count(); |
| 651 | } |
| 652 | if (fData.get()) { |
| 653 | return fData->opCount(); |
| 654 | } |
| 655 | return 0; |
| 656 | } |