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" |
| 11 | #include "SkPicturePlayback.h" |
| 12 | #include "SkPictureRecord.h" |
| 13 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 14 | #include "SkBitmapDevice.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | #include "SkCanvas.h" |
| 16 | #include "SkChunkAlloc.h" |
| 17 | #include "SkPicture.h" |
| 18 | #include "SkRegion.h" |
| 19 | #include "SkStream.h" |
| 20 | #include "SkTDArray.h" |
| 21 | #include "SkTSearch.h" |
| 22 | #include "SkTime.h" |
| 23 | |
| 24 | #include "SkReader32.h" |
| 25 | #include "SkWriter32.h" |
rileya@google.com | 8515e79 | 2012-09-13 21:41:51 +0000 | [diff] [blame] | 26 | #include "SkRTree.h" |
| 27 | #include "SkBBoxHierarchyRecord.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 28 | |
| 29 | #define DUMP_BUFFER_SIZE 65536 |
| 30 | |
| 31 | //#define ENABLE_TIME_DRAW // dumps milliseconds for each draw |
| 32 | |
| 33 | |
| 34 | #ifdef SK_DEBUG |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 35 | // enable SK_DEBUG_TRACE to trace DrawType elements when |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 36 | // recorded and played back |
| 37 | // #define SK_DEBUG_TRACE |
| 38 | // enable SK_DEBUG_SIZE to see the size of picture components |
| 39 | // #define SK_DEBUG_SIZE |
| 40 | // enable SK_DEBUG_DUMP to see the contents of recorded elements |
| 41 | // #define SK_DEBUG_DUMP |
| 42 | // enable SK_DEBUG_VALIDATE to check internal structures for consistency |
| 43 | // #define SK_DEBUG_VALIDATE |
| 44 | #endif |
| 45 | |
| 46 | #if defined SK_DEBUG_TRACE || defined SK_DEBUG_DUMP |
| 47 | const char* DrawTypeToString(DrawType drawType) { |
| 48 | switch (drawType) { |
| 49 | case UNUSED: SkDebugf("DrawType UNUSED\n"); SkASSERT(0); break; |
| 50 | case CLIP_PATH: return "CLIP_PATH"; |
| 51 | case CLIP_REGION: return "CLIP_REGION"; |
| 52 | case CLIP_RECT: return "CLIP_RECT"; |
robertphillips@google.com | e4ce5b8 | 2013-02-15 17:19:15 +0000 | [diff] [blame] | 53 | case CLIP_RRECT: return "CLIP_RRECT"; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 54 | case CONCAT: return "CONCAT"; |
| 55 | case DRAW_BITMAP: return "DRAW_BITMAP"; |
| 56 | case DRAW_BITMAP_MATRIX: return "DRAW_BITMAP_MATRIX"; |
robertphillips@google.com | e4ce5b8 | 2013-02-15 17:19:15 +0000 | [diff] [blame] | 57 | case DRAW_BITMAP_NINE: return "DRAW_BITMAP_NINE"; |
| 58 | case DRAW_BITMAP_RECT_TO_RECT: return "DRAW_BITMAP_RECT_TO_RECT"; |
| 59 | case DRAW_CLEAR: return "DRAW_CLEAR"; |
| 60 | case DRAW_DATA: return "DRAW_DATA"; |
| 61 | case DRAW_OVAL: return "DRAW_OVAL"; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 62 | case DRAW_PAINT: return "DRAW_PAINT"; |
| 63 | case DRAW_PATH: return "DRAW_PATH"; |
| 64 | case DRAW_PICTURE: return "DRAW_PICTURE"; |
| 65 | case DRAW_POINTS: return "DRAW_POINTS"; |
| 66 | case DRAW_POS_TEXT: return "DRAW_POS_TEXT"; |
robertphillips@google.com | e4ce5b8 | 2013-02-15 17:19:15 +0000 | [diff] [blame] | 67 | case DRAW_POS_TEXT_TOP_BOTTOM: return "DRAW_POS_TEXT_TOP_BOTTOM"; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 68 | case DRAW_POS_TEXT_H: return "DRAW_POS_TEXT_H"; |
robertphillips@google.com | e4ce5b8 | 2013-02-15 17:19:15 +0000 | [diff] [blame] | 69 | case DRAW_POS_TEXT_H_TOP_BOTTOM: return "DRAW_POS_TEXT_H_TOP_BOTTOM"; |
| 70 | case DRAW_RECT: return "DRAW_RECT"; |
| 71 | case DRAW_RRECT: return "DRAW_RRECT"; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 72 | case DRAW_SPRITE: return "DRAW_SPRITE"; |
| 73 | case DRAW_TEXT: return "DRAW_TEXT"; |
| 74 | case DRAW_TEXT_ON_PATH: return "DRAW_TEXT_ON_PATH"; |
robertphillips@google.com | e4ce5b8 | 2013-02-15 17:19:15 +0000 | [diff] [blame] | 75 | case DRAW_TEXT_TOP_BOTTOM: return "DRAW_TEXT_TOP_BOTTOM"; |
| 76 | case DRAW_VERTICES: return "DRAW_VERTICES"; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 77 | case RESTORE: return "RESTORE"; |
| 78 | case ROTATE: return "ROTATE"; |
| 79 | case SAVE: return "SAVE"; |
| 80 | case SAVE_LAYER: return "SAVE_LAYER"; |
| 81 | case SCALE: return "SCALE"; |
robertphillips@google.com | e4ce5b8 | 2013-02-15 17:19:15 +0000 | [diff] [blame] | 82 | case SET_MATRIX: return "SET_MATRIX"; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 83 | case SKEW: return "SKEW"; |
| 84 | case TRANSLATE: return "TRANSLATE"; |
robertphillips@google.com | e4ce5b8 | 2013-02-15 17:19:15 +0000 | [diff] [blame] | 85 | case NOOP: return "NOOP"; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 86 | default: |
| 87 | SkDebugf("DrawType error 0x%08x\n", drawType); |
| 88 | SkASSERT(0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 89 | break; |
| 90 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 91 | SkASSERT(0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 92 | return NULL; |
| 93 | } |
| 94 | #endif |
| 95 | |
| 96 | #ifdef SK_DEBUG_VALIDATE |
| 97 | static void validateMatrix(const SkMatrix* matrix) { |
| 98 | SkScalar scaleX = matrix->getScaleX(); |
| 99 | SkScalar scaleY = matrix->getScaleY(); |
| 100 | SkScalar skewX = matrix->getSkewX(); |
| 101 | SkScalar skewY = matrix->getSkewY(); |
| 102 | SkScalar perspX = matrix->getPerspX(); |
| 103 | SkScalar perspY = matrix->getPerspY(); |
| 104 | if (scaleX != 0 && skewX != 0) |
| 105 | SkDebugf("scaleX != 0 && skewX != 0\n"); |
| 106 | SkASSERT(scaleX == 0 || skewX == 0); |
| 107 | SkASSERT(scaleY == 0 || skewY == 0); |
| 108 | SkASSERT(perspX == 0); |
| 109 | SkASSERT(perspY == 0); |
| 110 | } |
| 111 | #endif |
| 112 | |
| 113 | |
| 114 | /////////////////////////////////////////////////////////////////////////////// |
| 115 | |
| 116 | SkPicture::SkPicture() { |
| 117 | fRecord = NULL; |
| 118 | fPlayback = NULL; |
| 119 | fWidth = fHeight = 0; |
| 120 | } |
| 121 | |
commit-bot@chromium.org | faa5ae4 | 2013-07-23 11:13:56 +0000 | [diff] [blame] | 122 | SkPicture::SkPicture(const SkPicture& src) : INHERITED() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 123 | fWidth = src.fWidth; |
| 124 | fHeight = src.fHeight; |
| 125 | fRecord = NULL; |
| 126 | |
| 127 | /* We want to copy the src's playback. However, if that hasn't been built |
| 128 | yet, we need to fake a call to endRecording() without actually calling |
| 129 | it (since it is destructive, and we don't want to change src). |
| 130 | */ |
| 131 | if (src.fPlayback) { |
| 132 | fPlayback = SkNEW_ARGS(SkPicturePlayback, (*src.fPlayback)); |
| 133 | } else if (src.fRecord) { |
| 134 | // here we do a fake src.endRecording() |
| 135 | fPlayback = SkNEW_ARGS(SkPicturePlayback, (*src.fRecord)); |
| 136 | } else { |
| 137 | fPlayback = NULL; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | SkPicture::~SkPicture() { |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 142 | SkSafeUnref(fRecord); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 143 | SkDELETE(fPlayback); |
| 144 | } |
| 145 | |
commit-bot@chromium.org | e494dbd | 2014-03-04 19:08:57 +0000 | [diff] [blame] | 146 | void SkPicture::internalOnly_EnableOpts(bool enableOpts) { |
| 147 | if (NULL != fRecord) { |
| 148 | fRecord->internalOnly_EnableOpts(enableOpts); |
| 149 | } |
| 150 | } |
| 151 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 152 | void SkPicture::swap(SkPicture& other) { |
| 153 | SkTSwap(fRecord, other.fRecord); |
| 154 | SkTSwap(fPlayback, other.fPlayback); |
| 155 | SkTSwap(fWidth, other.fWidth); |
| 156 | SkTSwap(fHeight, other.fHeight); |
| 157 | } |
| 158 | |
djsollen@google.com | c9ab987 | 2012-08-29 18:52:07 +0000 | [diff] [blame] | 159 | SkPicture* SkPicture::clone() const { |
| 160 | SkPicture* clonedPicture = SkNEW(SkPicture); |
| 161 | clone(clonedPicture, 1); |
| 162 | return clonedPicture; |
| 163 | } |
| 164 | |
| 165 | void SkPicture::clone(SkPicture* pictures, int count) const { |
| 166 | SkPictCopyInfo copyInfo; |
| 167 | |
| 168 | for (int i = 0; i < count; i++) { |
| 169 | SkPicture* clone = &pictures[i]; |
| 170 | |
| 171 | clone->fWidth = fWidth; |
| 172 | clone->fHeight = fHeight; |
commit-bot@chromium.org | 550db50 | 2014-02-14 17:27:10 +0000 | [diff] [blame] | 173 | SkSafeSetNull(clone->fRecord); |
scroggo@google.com | de1390d | 2012-11-02 20:51:19 +0000 | [diff] [blame] | 174 | SkDELETE(clone->fPlayback); |
| 175 | |
djsollen@google.com | c9ab987 | 2012-08-29 18:52:07 +0000 | [diff] [blame] | 176 | /* We want to copy the src's playback. However, if that hasn't been built |
| 177 | yet, we need to fake a call to endRecording() without actually calling |
| 178 | it (since it is destructive, and we don't want to change src). |
| 179 | */ |
| 180 | if (fPlayback) { |
| 181 | clone->fPlayback = SkNEW_ARGS(SkPicturePlayback, (*fPlayback, ©Info)); |
| 182 | } else if (fRecord) { |
| 183 | // here we do a fake src.endRecording() |
| 184 | clone->fPlayback = SkNEW_ARGS(SkPicturePlayback, (*fRecord, true)); |
| 185 | } else { |
| 186 | clone->fPlayback = NULL; |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 191 | /////////////////////////////////////////////////////////////////////////////// |
| 192 | |
reed@android.com | ae814c8 | 2009-02-13 14:56:09 +0000 | [diff] [blame] | 193 | SkCanvas* SkPicture::beginRecording(int width, int height, |
| 194 | uint32_t recordingFlags) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 195 | if (fPlayback) { |
| 196 | SkDELETE(fPlayback); |
| 197 | fPlayback = NULL; |
| 198 | } |
| 199 | |
commit-bot@chromium.org | 550db50 | 2014-02-14 17:27:10 +0000 | [diff] [blame] | 200 | SkSafeSetNull(fRecord); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 201 | |
junov@chromium.org | 35ac048 | 2012-11-01 17:10:32 +0000 | [diff] [blame] | 202 | // Must be set before calling createBBoxHierarchy |
| 203 | fWidth = width; |
| 204 | fHeight = height; |
| 205 | |
commit-bot@chromium.org | 19fafef | 2014-02-17 15:28:00 +0000 | [diff] [blame] | 206 | const SkISize size = SkISize::Make(width, height); |
| 207 | |
rileya@google.com | 8515e79 | 2012-09-13 21:41:51 +0000 | [diff] [blame] | 208 | if (recordingFlags & kOptimizeForClippedPlayback_RecordingFlag) { |
junov@chromium.org | 35ac048 | 2012-11-01 17:10:32 +0000 | [diff] [blame] | 209 | SkBBoxHierarchy* tree = this->createBBoxHierarchy(); |
rileya@google.com | 8515e79 | 2012-09-13 21:41:51 +0000 | [diff] [blame] | 210 | SkASSERT(NULL != tree); |
commit-bot@chromium.org | 19fafef | 2014-02-17 15:28:00 +0000 | [diff] [blame] | 211 | fRecord = SkNEW_ARGS(SkBBoxHierarchyRecord, (size, recordingFlags, tree)); |
rileya@google.com | 8515e79 | 2012-09-13 21:41:51 +0000 | [diff] [blame] | 212 | tree->unref(); |
| 213 | } else { |
commit-bot@chromium.org | 19fafef | 2014-02-17 15:28:00 +0000 | [diff] [blame] | 214 | fRecord = SkNEW_ARGS(SkPictureRecord, (size, recordingFlags)); |
rileya@google.com | 8515e79 | 2012-09-13 21:41:51 +0000 | [diff] [blame] | 215 | } |
reed@google.com | d86e7ab | 2012-09-27 20:31:31 +0000 | [diff] [blame] | 216 | fRecord->beginRecording(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 217 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 218 | return fRecord; |
| 219 | } |
| 220 | |
junov@chromium.org | 35ac048 | 2012-11-01 17:10:32 +0000 | [diff] [blame] | 221 | SkBBoxHierarchy* SkPicture::createBBoxHierarchy() const { |
skia.committer@gmail.com | 760f2d9 | 2012-11-02 02:01:24 +0000 | [diff] [blame] | 222 | // These values were empirically determined to produce reasonable |
junov@chromium.org | 35ac048 | 2012-11-01 17:10:32 +0000 | [diff] [blame] | 223 | // performance in most cases. |
| 224 | static const int kRTreeMinChildren = 6; |
| 225 | static const int kRTreeMaxChildren = 11; |
| 226 | |
| 227 | SkScalar aspectRatio = SkScalarDiv(SkIntToScalar(fWidth), |
| 228 | SkIntToScalar(fHeight)); |
sglez@google.com | 8c90212 | 2013-08-30 17:27:47 +0000 | [diff] [blame] | 229 | bool sortDraws = false; // Do not sort draw calls when bulk loading. |
| 230 | |
junov@chromium.org | 35ac048 | 2012-11-01 17:10:32 +0000 | [diff] [blame] | 231 | return SkRTree::Create(kRTreeMinChildren, kRTreeMaxChildren, |
sglez@google.com | 8c90212 | 2013-08-30 17:27:47 +0000 | [diff] [blame] | 232 | aspectRatio, sortDraws); |
junov@chromium.org | 35ac048 | 2012-11-01 17:10:32 +0000 | [diff] [blame] | 233 | } |
| 234 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 235 | SkCanvas* SkPicture::getRecordingCanvas() const { |
| 236 | // will be null if we are not recording |
| 237 | return fRecord; |
| 238 | } |
| 239 | |
| 240 | void SkPicture::endRecording() { |
| 241 | if (NULL == fPlayback) { |
| 242 | if (NULL != fRecord) { |
junov@chromium.org | a6c9e0e | 2012-07-12 17:47:34 +0000 | [diff] [blame] | 243 | fRecord->endRecording(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 244 | fPlayback = SkNEW_ARGS(SkPicturePlayback, (*fRecord)); |
commit-bot@chromium.org | 550db50 | 2014-02-14 17:27:10 +0000 | [diff] [blame] | 245 | SkSafeSetNull(fRecord); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | SkASSERT(NULL == fRecord); |
| 249 | } |
| 250 | |
reed@google.com | 74babdf | 2013-05-20 17:02:41 +0000 | [diff] [blame] | 251 | void SkPicture::draw(SkCanvas* surface, SkDrawPictureCallback* callback) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 252 | this->endRecording(); |
| 253 | if (fPlayback) { |
reed@google.com | 74babdf | 2013-05-20 17:02:41 +0000 | [diff] [blame] | 254 | fPlayback->draw(*surface, callback); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
| 258 | /////////////////////////////////////////////////////////////////////////////// |
| 259 | |
| 260 | #include "SkStream.h" |
| 261 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 262 | 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^] | 263 | |
| 264 | bool SkPicture::IsValidPictInfo(const SkPictInfo& info) { |
| 265 | if (0 != memcmp(info.fMagic, kMagic, sizeof(kMagic))) { |
| 266 | return false; |
| 267 | } |
| 268 | |
| 269 | if (info.fVersion < MIN_PICTURE_VERSION || |
| 270 | info.fVersion > CURRENT_PICTURE_VERSION) { |
| 271 | return false; |
| 272 | } |
| 273 | |
| 274 | return true; |
| 275 | } |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 276 | |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 277 | bool SkPicture::InternalOnly_StreamIsSKP(SkStream* stream, SkPictInfo* pInfo) { |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 278 | if (NULL == stream) { |
| 279 | return false; |
borenet@google.com | 66bcbd1 | 2012-09-17 18:26:06 +0000 | [diff] [blame] | 280 | } |
reed@google.com | 6756209 | 2012-06-22 15:38:39 +0000 | [diff] [blame] | 281 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 282 | // Check magic bytes. |
reed@google.com | 6756209 | 2012-06-22 15:38:39 +0000 | [diff] [blame] | 283 | SkPictInfo info; |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame^] | 284 | SkASSERT(sizeof(kMagic) == sizeof(info.fMagic)); |
| 285 | if (!stream->read(&info, sizeof(info)) || !IsValidPictInfo(info)) { |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 286 | return false; |
reed@google.com | 6756209 | 2012-06-22 15:38:39 +0000 | [diff] [blame] | 287 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 288 | |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 289 | if (pInfo != NULL) { |
| 290 | *pInfo = info; |
| 291 | } |
| 292 | return true; |
| 293 | } |
| 294 | |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 295 | bool SkPicture::InternalOnly_BufferIsSKP(SkReadBuffer& buffer, SkPictInfo* pInfo) { |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 296 | // Check magic bytes. |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 297 | SkPictInfo info; |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame^] | 298 | SkASSERT(sizeof(kMagic) == sizeof(info.fMagic)); |
| 299 | if (!buffer.readByteArray(&info, sizeof(info)) || !IsValidPictInfo(info)) { |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 300 | return false; |
| 301 | } |
| 302 | |
| 303 | if (pInfo != NULL) { |
| 304 | *pInfo = info; |
| 305 | } |
| 306 | return true; |
| 307 | } |
| 308 | |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 309 | SkPicture::SkPicture(SkPicturePlayback* playback, int width, int height) |
| 310 | : fPlayback(playback) |
| 311 | , fRecord(NULL) |
| 312 | , fWidth(width) |
| 313 | , fHeight(height) {} |
| 314 | |
| 315 | SkPicture* SkPicture::CreateFromStream(SkStream* stream, InstallPixelRefProc proc) { |
| 316 | SkPictInfo info; |
| 317 | |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 318 | if (!InternalOnly_StreamIsSKP(stream, &info)) { |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 319 | return NULL; |
| 320 | } |
| 321 | |
| 322 | SkPicturePlayback* playback; |
| 323 | // Check to see if there is a playback to recreate. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 324 | if (stream->readBool()) { |
scroggo@google.com | 1270532 | 2013-10-01 15:30:46 +0000 | [diff] [blame] | 325 | playback = SkPicturePlayback::CreateFromStream(stream, info, proc); |
| 326 | if (NULL == playback) { |
| 327 | return NULL; |
| 328 | } |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 329 | } else { |
| 330 | playback = NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 331 | } |
reed@google.com | 6756209 | 2012-06-22 15:38:39 +0000 | [diff] [blame] | 332 | |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 333 | return SkNEW_ARGS(SkPicture, (playback, info.fWidth, info.fHeight)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 334 | } |
| 335 | |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 336 | SkPicture* SkPicture::CreateFromBuffer(SkReadBuffer& buffer) { |
| 337 | SkPictInfo info; |
| 338 | |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 339 | if (!InternalOnly_BufferIsSKP(buffer, &info)) { |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 340 | return NULL; |
| 341 | } |
| 342 | |
| 343 | SkPicturePlayback* playback; |
| 344 | // Check to see if there is a playback to recreate. |
| 345 | if (buffer.readBool()) { |
| 346 | playback = SkPicturePlayback::CreateFromBuffer(buffer); |
| 347 | if (NULL == playback) { |
| 348 | return NULL; |
| 349 | } |
| 350 | } else { |
| 351 | playback = NULL; |
| 352 | } |
| 353 | |
| 354 | return SkNEW_ARGS(SkPicture, (playback, info.fWidth, info.fHeight)); |
| 355 | } |
| 356 | |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame^] | 357 | void SkPicture::createHeader(SkPictInfo* info) const { |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 358 | // Copy magic bytes at the beginning of the header |
| 359 | SkASSERT(sizeof(kMagic) == 8); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame^] | 360 | SkASSERT(sizeof(kMagic) == sizeof(info->fMagic)); |
| 361 | memcpy(info->fMagic, kMagic, sizeof(kMagic)); |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 362 | |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 363 | // Set picture info after magic bytes in the header |
commit-bot@chromium.org | e8d9614 | 2014-02-25 02:16:10 +0000 | [diff] [blame] | 364 | info->fVersion = CURRENT_PICTURE_VERSION; |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 365 | info->fWidth = fWidth; |
| 366 | info->fHeight = fHeight; |
| 367 | info->fFlags = SkPictInfo::kCrossProcess_Flag; |
| 368 | // TODO: remove this flag, since we're always float (now) |
| 369 | info->fFlags |= SkPictInfo::kScalarIsFloat_Flag; |
| 370 | |
| 371 | if (8 == sizeof(void*)) { |
| 372 | info->fFlags |= SkPictInfo::kPtrIs64Bit_Flag; |
| 373 | } |
| 374 | } |
| 375 | |
scroggo@google.com | 32ef131 | 2013-02-22 22:04:19 +0000 | [diff] [blame] | 376 | void SkPicture::serialize(SkWStream* stream, EncodeBitmap encoder) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 377 | SkPicturePlayback* playback = fPlayback; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 378 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 379 | if (NULL == playback && fRecord) { |
| 380 | playback = SkNEW_ARGS(SkPicturePlayback, (*fRecord)); |
| 381 | } |
| 382 | |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame^] | 383 | SkPictInfo header; |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 384 | this->createHeader(&header); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame^] | 385 | stream->write(&header, sizeof(header)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 386 | if (playback) { |
| 387 | stream->writeBool(true); |
scroggo@google.com | 5a7c6be | 2012-10-04 21:46:08 +0000 | [diff] [blame] | 388 | playback->serialize(stream, encoder); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 389 | // delete playback if it is a local version (i.e. cons'd up just now) |
| 390 | if (playback != fPlayback) { |
| 391 | SkDELETE(playback); |
| 392 | } |
| 393 | } else { |
| 394 | stream->writeBool(false); |
| 395 | } |
| 396 | } |
| 397 | |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 398 | void SkPicture::flatten(SkWriteBuffer& buffer) const { |
| 399 | SkPicturePlayback* playback = fPlayback; |
| 400 | |
| 401 | if (NULL == playback && fRecord) { |
| 402 | playback = SkNEW_ARGS(SkPicturePlayback, (*fRecord)); |
| 403 | } |
| 404 | |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame^] | 405 | SkPictInfo header; |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 406 | this->createHeader(&header); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame^] | 407 | buffer.writeByteArray(&header, sizeof(header)); |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 408 | if (playback) { |
| 409 | buffer.writeBool(true); |
| 410 | playback->flatten(buffer); |
| 411 | // delete playback if it is a local version (i.e. cons'd up just now) |
| 412 | if (playback != fPlayback) { |
| 413 | SkDELETE(playback); |
| 414 | } |
| 415 | } else { |
| 416 | buffer.writeBool(false); |
| 417 | } |
| 418 | } |
| 419 | |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 420 | bool SkPicture::willPlayBackBitmaps() const { |
| 421 | if (!fPlayback) return false; |
| 422 | return fPlayback->containsBitmaps(); |
| 423 | } |
| 424 | |
djsollen@google.com | d9b0f48 | 2013-02-01 16:18:09 +0000 | [diff] [blame] | 425 | #ifdef SK_BUILD_FOR_ANDROID |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 426 | void SkPicture::abortPlayback() { |
| 427 | if (NULL == fPlayback) { |
| 428 | return; |
| 429 | } |
| 430 | fPlayback->abort(); |
| 431 | } |
djsollen@google.com | d9b0f48 | 2013-02-01 16:18:09 +0000 | [diff] [blame] | 432 | #endif |