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 | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 17 | #include "SkBBHFactory.h" |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 18 | #include "SkBitmapDevice.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 19 | #include "SkCanvas.h" |
| 20 | #include "SkChunkAlloc.h" |
Mike Klein | c11530e | 2014-06-24 11:29:06 -0400 | [diff] [blame] | 21 | #include "SkDrawPictureCallback.h" |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 22 | #include "SkPaintPriv.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | #include "SkPicture.h" |
tomhudson | d930511 | 2014-07-05 13:37:53 -0700 | [diff] [blame] | 24 | #include "SkRecordAnalysis.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 25 | #include "SkRegion.h" |
| 26 | #include "SkStream.h" |
| 27 | #include "SkTDArray.h" |
| 28 | #include "SkTSearch.h" |
| 29 | #include "SkTime.h" |
| 30 | |
| 31 | #include "SkReader32.h" |
| 32 | #include "SkWriter32.h" |
rileya@google.com | 8515e79 | 2012-09-13 21:41:51 +0000 | [diff] [blame] | 33 | #include "SkRTree.h" |
| 34 | #include "SkBBoxHierarchyRecord.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 35 | |
commit-bot@chromium.org | eb9547c | 2014-03-19 21:24:25 +0000 | [diff] [blame] | 36 | #if SK_SUPPORT_GPU |
| 37 | #include "GrContext.h" |
| 38 | #endif |
| 39 | |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 40 | #include "SkRecord.h" |
| 41 | #include "SkRecordDraw.h" |
Mike Klein | c11530e | 2014-06-24 11:29:06 -0400 | [diff] [blame] | 42 | #include "SkRecorder.h" |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 43 | |
commit-bot@chromium.org | 8f831f2 | 2014-04-23 22:35:42 +0000 | [diff] [blame] | 44 | template <typename T> int SafeCount(const T* obj) { |
| 45 | return obj ? obj->count() : 0; |
| 46 | } |
| 47 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 48 | #define DUMP_BUFFER_SIZE 65536 |
| 49 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 50 | #ifdef SK_DEBUG |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 51 | // enable SK_DEBUG_TRACE to trace DrawType elements when |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 52 | // recorded and played back |
| 53 | // #define SK_DEBUG_TRACE |
| 54 | // enable SK_DEBUG_SIZE to see the size of picture components |
| 55 | // #define SK_DEBUG_SIZE |
| 56 | // enable SK_DEBUG_DUMP to see the contents of recorded elements |
| 57 | // #define SK_DEBUG_DUMP |
| 58 | // enable SK_DEBUG_VALIDATE to check internal structures for consistency |
| 59 | // #define SK_DEBUG_VALIDATE |
| 60 | #endif |
| 61 | |
| 62 | #if defined SK_DEBUG_TRACE || defined SK_DEBUG_DUMP |
| 63 | const char* DrawTypeToString(DrawType drawType) { |
| 64 | switch (drawType) { |
| 65 | case UNUSED: SkDebugf("DrawType UNUSED\n"); SkASSERT(0); break; |
| 66 | case CLIP_PATH: return "CLIP_PATH"; |
| 67 | case CLIP_REGION: return "CLIP_REGION"; |
| 68 | case CLIP_RECT: return "CLIP_RECT"; |
robertphillips@google.com | e4ce5b8 | 2013-02-15 17:19:15 +0000 | [diff] [blame] | 69 | case CLIP_RRECT: return "CLIP_RRECT"; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 70 | case CONCAT: return "CONCAT"; |
| 71 | case DRAW_BITMAP: return "DRAW_BITMAP"; |
| 72 | case DRAW_BITMAP_MATRIX: return "DRAW_BITMAP_MATRIX"; |
robertphillips@google.com | e4ce5b8 | 2013-02-15 17:19:15 +0000 | [diff] [blame] | 73 | case DRAW_BITMAP_NINE: return "DRAW_BITMAP_NINE"; |
| 74 | case DRAW_BITMAP_RECT_TO_RECT: return "DRAW_BITMAP_RECT_TO_RECT"; |
| 75 | case DRAW_CLEAR: return "DRAW_CLEAR"; |
| 76 | case DRAW_DATA: return "DRAW_DATA"; |
| 77 | case DRAW_OVAL: return "DRAW_OVAL"; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 78 | case DRAW_PAINT: return "DRAW_PAINT"; |
| 79 | case DRAW_PATH: return "DRAW_PATH"; |
| 80 | case DRAW_PICTURE: return "DRAW_PICTURE"; |
| 81 | case DRAW_POINTS: return "DRAW_POINTS"; |
| 82 | case DRAW_POS_TEXT: return "DRAW_POS_TEXT"; |
robertphillips@google.com | e4ce5b8 | 2013-02-15 17:19:15 +0000 | [diff] [blame] | 83 | 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] | 84 | case DRAW_POS_TEXT_H: return "DRAW_POS_TEXT_H"; |
robertphillips@google.com | e4ce5b8 | 2013-02-15 17:19:15 +0000 | [diff] [blame] | 85 | case DRAW_POS_TEXT_H_TOP_BOTTOM: return "DRAW_POS_TEXT_H_TOP_BOTTOM"; |
| 86 | case DRAW_RECT: return "DRAW_RECT"; |
| 87 | case DRAW_RRECT: return "DRAW_RRECT"; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 88 | case DRAW_SPRITE: return "DRAW_SPRITE"; |
| 89 | case DRAW_TEXT: return "DRAW_TEXT"; |
| 90 | case DRAW_TEXT_ON_PATH: return "DRAW_TEXT_ON_PATH"; |
robertphillips@google.com | e4ce5b8 | 2013-02-15 17:19:15 +0000 | [diff] [blame] | 91 | case DRAW_TEXT_TOP_BOTTOM: return "DRAW_TEXT_TOP_BOTTOM"; |
| 92 | case DRAW_VERTICES: return "DRAW_VERTICES"; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 93 | case RESTORE: return "RESTORE"; |
| 94 | case ROTATE: return "ROTATE"; |
| 95 | case SAVE: return "SAVE"; |
| 96 | case SAVE_LAYER: return "SAVE_LAYER"; |
| 97 | case SCALE: return "SCALE"; |
robertphillips@google.com | e4ce5b8 | 2013-02-15 17:19:15 +0000 | [diff] [blame] | 98 | case SET_MATRIX: return "SET_MATRIX"; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 99 | case SKEW: return "SKEW"; |
| 100 | case TRANSLATE: return "TRANSLATE"; |
robertphillips@google.com | e4ce5b8 | 2013-02-15 17:19:15 +0000 | [diff] [blame] | 101 | case NOOP: return "NOOP"; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 102 | default: |
| 103 | SkDebugf("DrawType error 0x%08x\n", drawType); |
| 104 | SkASSERT(0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 105 | break; |
| 106 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 107 | SkASSERT(0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 108 | return NULL; |
| 109 | } |
| 110 | #endif |
| 111 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 112 | /////////////////////////////////////////////////////////////////////////////// |
| 113 | |
Robert Phillips | cfaeec4 | 2014-07-13 12:00:50 -0400 | [diff] [blame] | 114 | #ifdef SK_SUPPORT_LEGACY_DEFAULT_PICTURE_CTOR |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 115 | // fRecord OK |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 116 | SkPicture::SkPicture() |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 117 | : fWidth(0) |
tomhudson | d930511 | 2014-07-05 13:37:53 -0700 | [diff] [blame] | 118 | , fHeight(0) |
| 119 | , fRecordWillPlayBackBitmaps(false) { |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 120 | this->needsNewGenID(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 121 | } |
Robert Phillips | cfaeec4 | 2014-07-13 12:00:50 -0400 | [diff] [blame] | 122 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 123 | |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 124 | // fRecord OK |
robertphillips | 0bdbea7 | 2014-06-11 11:37:55 -0700 | [diff] [blame] | 125 | SkPicture::SkPicture(int width, int height, |
robertphillips | e26e65e | 2014-06-12 05:51:22 -0700 | [diff] [blame] | 126 | const SkPictureRecord& record, |
robertphillips | 0bdbea7 | 2014-06-11 11:37:55 -0700 | [diff] [blame] | 127 | bool deepCopyOps) |
| 128 | : fWidth(width) |
tomhudson | d930511 | 2014-07-05 13:37:53 -0700 | [diff] [blame] | 129 | , fHeight(height) |
| 130 | , fRecordWillPlayBackBitmaps(false) { |
robertphillips | 0bdbea7 | 2014-06-11 11:37:55 -0700 | [diff] [blame] | 131 | this->needsNewGenID(); |
robertphillips | 9058d60 | 2014-06-10 11:45:46 -0700 | [diff] [blame] | 132 | |
robertphillips | 0bdbea7 | 2014-06-11 11:37:55 -0700 | [diff] [blame] | 133 | SkPictInfo info; |
| 134 | this->createHeader(&info); |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 135 | fData.reset(SkNEW_ARGS(SkPictureData, (record, info, deepCopyOps))); |
commit-bot@chromium.org | 2ee3c2c | 2014-05-19 12:36:15 +0000 | [diff] [blame] | 136 | } |
| 137 | |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 138 | // Create an SkPictureData-backed SkPicture from an SkRecord. |
mtklein | 7373456 | 2014-06-24 12:28:34 -0700 | [diff] [blame] | 139 | // This for compatibility with serialization code only. This is not cheap. |
| 140 | static SkPicture* backport(const SkRecord& src, int width, int height) { |
| 141 | SkPictureRecorder recorder; |
| 142 | SkRecordDraw(src, recorder.beginRecording(width, height)); |
| 143 | return recorder.endRecording(); |
| 144 | } |
| 145 | |
Mike Klein | c11530e | 2014-06-24 11:29:06 -0400 | [diff] [blame] | 146 | // fRecord OK |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame^] | 147 | SkPicture::~SkPicture() { |
| 148 | this->callDeletionListeners(); |
| 149 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 150 | |
mtklein | d3e474e | 2014-06-27 12:34:44 -0700 | [diff] [blame] | 151 | #ifdef SK_SUPPORT_LEGACY_PICTURE_CLONE |
Mike Klein | c11530e | 2014-06-24 11:29:06 -0400 | [diff] [blame] | 152 | // fRecord TODO, fix by deleting this method |
djsollen@google.com | c9ab987 | 2012-08-29 18:52:07 +0000 | [diff] [blame] | 153 | SkPicture* SkPicture::clone() const { |
djsollen@google.com | c9ab987 | 2012-08-29 18:52:07 +0000 | [diff] [blame] | 154 | |
Robert Phillips | cfaeec4 | 2014-07-13 12:00:50 -0400 | [diff] [blame] | 155 | SkAutoTDelete<SkPictureData> newData; |
djsollen@google.com | c9ab987 | 2012-08-29 18:52:07 +0000 | [diff] [blame] | 156 | |
Robert Phillips | cfaeec4 | 2014-07-13 12:00:50 -0400 | [diff] [blame] | 157 | if (fData.get()) { |
| 158 | SkPictCopyInfo copyInfo; |
djsollen@google.com | c9ab987 | 2012-08-29 18:52:07 +0000 | [diff] [blame] | 159 | |
Robert Phillips | cfaeec4 | 2014-07-13 12:00:50 -0400 | [diff] [blame] | 160 | int paintCount = SafeCount(fData->fPaints); |
scroggo@google.com | de1390d | 2012-11-02 20:51:19 +0000 | [diff] [blame] | 161 | |
Robert Phillips | cfaeec4 | 2014-07-13 12:00:50 -0400 | [diff] [blame] | 162 | /* The alternative to doing this is to have a clone method on the paint and have it |
| 163 | * make the deep copy of its internal structures as needed. The holdup to doing |
| 164 | * that is at this point we would need to pass the SkBitmapHeap so that we don't |
| 165 | * unnecessarily flatten the pixels in a bitmap shader. |
djsollen@google.com | c9ab987 | 2012-08-29 18:52:07 +0000 | [diff] [blame] | 166 | */ |
Robert Phillips | cfaeec4 | 2014-07-13 12:00:50 -0400 | [diff] [blame] | 167 | copyInfo.paintData.setCount(paintCount); |
commit-bot@chromium.org | 66ec1e4 | 2014-04-29 17:22:54 +0000 | [diff] [blame] | 168 | |
Robert Phillips | cfaeec4 | 2014-07-13 12:00:50 -0400 | [diff] [blame] | 169 | /* Use an SkBitmapHeap to avoid flattening bitmaps in shaders. If there already is |
| 170 | * one, use it. If this SkPictureData was created from a stream, fBitmapHeap |
| 171 | * will be NULL, so create a new one. |
| 172 | */ |
| 173 | if (fData->fBitmapHeap.get() == NULL) { |
| 174 | // FIXME: Put this on the stack inside SkPicture::clone. |
| 175 | SkBitmapHeap* heap = SkNEW(SkBitmapHeap); |
| 176 | copyInfo.controller.setBitmapStorage(heap); |
| 177 | heap->unref(); |
| 178 | } else { |
| 179 | copyInfo.controller.setBitmapStorage(fData->fBitmapHeap); |
djsollen@google.com | c9ab987 | 2012-08-29 18:52:07 +0000 | [diff] [blame] | 180 | } |
Robert Phillips | cfaeec4 | 2014-07-13 12:00:50 -0400 | [diff] [blame] | 181 | |
| 182 | SkDEBUGCODE(int heapSize = SafeCount(fData->fBitmapHeap.get());) |
| 183 | for (int i = 0; i < paintCount; i++) { |
| 184 | if (NeedsDeepCopy(fData->fPaints->at(i))) { |
| 185 | copyInfo.paintData[i] = |
| 186 | SkFlatData::Create<SkPaint::FlatteningTraits>(©Info.controller, |
| 187 | fData->fPaints->at(i), 0); |
| 188 | |
| 189 | } else { |
| 190 | // this is our sentinel, which we use in the unflatten loop |
| 191 | copyInfo.paintData[i] = NULL; |
| 192 | } |
| 193 | } |
| 194 | SkASSERT(SafeCount(fData->fBitmapHeap.get()) == heapSize); |
| 195 | |
| 196 | // needed to create typeface playback |
| 197 | copyInfo.controller.setupPlaybacks(); |
| 198 | |
| 199 | newData.reset(SkNEW_ARGS(SkPictureData, (*fData, ©Info))); |
djsollen@google.com | c9ab987 | 2012-08-29 18:52:07 +0000 | [diff] [blame] | 200 | } |
Robert Phillips | cfaeec4 | 2014-07-13 12:00:50 -0400 | [diff] [blame] | 201 | |
| 202 | SkPicture* clone = SkNEW_ARGS(SkPicture, (newData.detach(), fWidth, fHeight)); |
| 203 | clone->fRecordWillPlayBackBitmaps = fRecordWillPlayBackBitmaps; |
| 204 | clone->fUniqueID = this->uniqueID(); // need to call method to ensure != 0 |
| 205 | |
| 206 | return clone; |
djsollen@google.com | c9ab987 | 2012-08-29 18:52:07 +0000 | [diff] [blame] | 207 | } |
mtklein | d3e474e | 2014-06-27 12:34:44 -0700 | [diff] [blame] | 208 | #endif//SK_SUPPORT_LEGACY_PICTURE_CLONE |
djsollen@google.com | c9ab987 | 2012-08-29 18:52:07 +0000 | [diff] [blame] | 209 | |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 210 | // fRecord OK |
| 211 | void SkPicture::EXPERIMENTAL_addAccelData(const SkPicture::AccelData* data) const { |
| 212 | fAccelData.reset(SkRef(data)); |
| 213 | } |
| 214 | |
| 215 | // fRecord OK |
| 216 | const SkPicture::AccelData* SkPicture::EXPERIMENTAL_getAccelData( |
| 217 | SkPicture::AccelData::Key key) const { |
| 218 | if (NULL != fAccelData.get() && fAccelData->getKey() == key) { |
| 219 | return fAccelData.get(); |
| 220 | } |
| 221 | return NULL; |
| 222 | } |
| 223 | |
| 224 | // fRecord OK |
commit-bot@chromium.org | 145d1c0 | 2014-03-16 19:46:36 +0000 | [diff] [blame] | 225 | SkPicture::AccelData::Domain SkPicture::AccelData::GenerateDomain() { |
| 226 | static int32_t gNextID = 0; |
| 227 | |
| 228 | int32_t id = sk_atomic_inc(&gNextID); |
| 229 | if (id >= 1 << (8 * sizeof(Domain))) { |
| 230 | SK_CRASH(); |
| 231 | } |
| 232 | |
| 233 | return static_cast<Domain>(id); |
| 234 | } |
| 235 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 236 | /////////////////////////////////////////////////////////////////////////////// |
| 237 | |
robertphillips | 3afef1f | 2014-07-08 06:12:22 -0700 | [diff] [blame] | 238 | uint32_t SkPicture::OperationList::offset(int index) const { |
| 239 | SkASSERT(index < fOps.count()); |
| 240 | return ((SkPictureStateTree::Draw*)fOps[index])->fOffset; |
| 241 | } |
| 242 | |
| 243 | const SkMatrix& SkPicture::OperationList::matrix(int index) const { |
| 244 | SkASSERT(index < fOps.count()); |
| 245 | return *((SkPictureStateTree::Draw*)fOps[index])->fMatrix; |
| 246 | } |
| 247 | |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 248 | // fRecord TODO |
robertphillips | ce4dd3d | 2014-07-07 13:46:35 -0700 | [diff] [blame] | 249 | const SkPicture::OperationList* SkPicture::EXPERIMENTAL_getActiveOps(const SkIRect& queryRect) const { |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 250 | SkASSERT(NULL != fData.get()); |
| 251 | if (NULL != fData.get()) { |
| 252 | return fData->getActiveOps(queryRect); |
commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 253 | } |
robertphillips | ce4dd3d | 2014-07-07 13:46:35 -0700 | [diff] [blame] | 254 | return NULL; |
commit-bot@chromium.org | 75cf29b | 2014-03-24 19:40:49 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 257 | // fRecord OK |
| 258 | void SkPicture::draw(SkCanvas* canvas, SkDrawPictureCallback* callback) const { |
| 259 | SkASSERT(NULL != canvas); |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 260 | SkASSERT(NULL != fData.get() || NULL != fRecord.get()); |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 261 | |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 262 | if (NULL != fData.get()) { |
robertphillips | ce4dd3d | 2014-07-07 13:46:35 -0700 | [diff] [blame] | 263 | SkPicturePlayback playback(this); |
| 264 | playback.draw(canvas, callback); |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 265 | } |
| 266 | if (NULL != fRecord.get()) { |
Mike Klein | c11530e | 2014-06-24 11:29:06 -0400 | [diff] [blame] | 267 | SkRecordDraw(*fRecord, canvas, callback); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
| 271 | /////////////////////////////////////////////////////////////////////////////// |
| 272 | |
| 273 | #include "SkStream.h" |
| 274 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 275 | 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] | 276 | |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 277 | // fRecord OK |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 278 | bool SkPicture::IsValidPictInfo(const SkPictInfo& info) { |
| 279 | if (0 != memcmp(info.fMagic, kMagic, sizeof(kMagic))) { |
| 280 | return false; |
| 281 | } |
| 282 | |
| 283 | if (info.fVersion < MIN_PICTURE_VERSION || |
| 284 | info.fVersion > CURRENT_PICTURE_VERSION) { |
| 285 | return false; |
| 286 | } |
| 287 | |
| 288 | return true; |
| 289 | } |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 290 | |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 291 | // fRecord OK |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 292 | bool SkPicture::InternalOnly_StreamIsSKP(SkStream* stream, SkPictInfo* pInfo) { |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 293 | if (NULL == stream) { |
| 294 | return false; |
borenet@google.com | 66bcbd1 | 2012-09-17 18:26:06 +0000 | [diff] [blame] | 295 | } |
reed@google.com | 6756209 | 2012-06-22 15:38:39 +0000 | [diff] [blame] | 296 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 297 | // Check magic bytes. |
reed@google.com | 6756209 | 2012-06-22 15:38:39 +0000 | [diff] [blame] | 298 | SkPictInfo info; |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 299 | SkASSERT(sizeof(kMagic) == sizeof(info.fMagic)); |
| 300 | if (!stream->read(&info, sizeof(info)) || !IsValidPictInfo(info)) { |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 301 | return false; |
reed@google.com | 6756209 | 2012-06-22 15:38:39 +0000 | [diff] [blame] | 302 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 303 | |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 304 | if (pInfo != NULL) { |
| 305 | *pInfo = info; |
| 306 | } |
| 307 | return true; |
| 308 | } |
| 309 | |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 310 | // fRecord OK |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 311 | bool SkPicture::InternalOnly_BufferIsSKP(SkReadBuffer& buffer, SkPictInfo* pInfo) { |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 312 | // Check magic bytes. |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 313 | SkPictInfo info; |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 314 | SkASSERT(sizeof(kMagic) == sizeof(info.fMagic)); |
| 315 | if (!buffer.readByteArray(&info, sizeof(info)) || !IsValidPictInfo(info)) { |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 316 | return false; |
| 317 | } |
| 318 | |
| 319 | if (pInfo != NULL) { |
| 320 | *pInfo = info; |
| 321 | } |
| 322 | return true; |
| 323 | } |
| 324 | |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 325 | // fRecord OK |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 326 | SkPicture::SkPicture(SkPictureData* data, int width, int height) |
| 327 | : fData(data) |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 328 | , fWidth(width) |
tomhudson | d930511 | 2014-07-05 13:37:53 -0700 | [diff] [blame] | 329 | , fHeight(height) |
| 330 | , fRecordWillPlayBackBitmaps(false) { |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 331 | this->needsNewGenID(); |
| 332 | } |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 333 | |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 334 | // fRecord OK |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 335 | SkPicture* SkPicture::CreateFromStream(SkStream* stream, InstallPixelRefProc proc) { |
| 336 | SkPictInfo info; |
| 337 | |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 338 | if (!InternalOnly_StreamIsSKP(stream, &info)) { |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 339 | return NULL; |
| 340 | } |
| 341 | |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 342 | // Check to see if there is a playback to recreate. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 343 | if (stream->readBool()) { |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 344 | SkPictureData* data = SkPictureData::CreateFromStream(stream, info, proc); |
| 345 | if (NULL == data) { |
scroggo@google.com | 1270532 | 2013-10-01 15:30:46 +0000 | [diff] [blame] | 346 | return NULL; |
| 347 | } |
robertphillips | e26e65e | 2014-06-12 05:51:22 -0700 | [diff] [blame] | 348 | |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 349 | return SkNEW_ARGS(SkPicture, (data, info.fWidth, info.fHeight)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 350 | } |
reed@google.com | 6756209 | 2012-06-22 15:38:39 +0000 | [diff] [blame] | 351 | |
robertphillips | e26e65e | 2014-06-12 05:51:22 -0700 | [diff] [blame] | 352 | return NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 355 | // fRecord OK |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 356 | SkPicture* SkPicture::CreateFromBuffer(SkReadBuffer& buffer) { |
| 357 | SkPictInfo info; |
| 358 | |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 359 | if (!InternalOnly_BufferIsSKP(buffer, &info)) { |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 360 | return NULL; |
| 361 | } |
| 362 | |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 363 | // Check to see if there is a playback to recreate. |
| 364 | if (buffer.readBool()) { |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 365 | SkPictureData* data = SkPictureData::CreateFromBuffer(buffer, info); |
| 366 | if (NULL == data) { |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 367 | return NULL; |
| 368 | } |
robertphillips | e26e65e | 2014-06-12 05:51:22 -0700 | [diff] [blame] | 369 | |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 370 | return SkNEW_ARGS(SkPicture, (data, info.fWidth, info.fHeight)); |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 371 | } |
| 372 | |
robertphillips | e26e65e | 2014-06-12 05:51:22 -0700 | [diff] [blame] | 373 | return NULL; |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 376 | // fRecord OK |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 377 | void SkPicture::createHeader(SkPictInfo* info) const { |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 378 | // Copy magic bytes at the beginning of the header |
| 379 | SkASSERT(sizeof(kMagic) == 8); |
commit-bot@chromium.org | 9e5f85e | 2014-03-12 14:46:41 +0000 | [diff] [blame] | 380 | SkASSERT(sizeof(kMagic) == sizeof(info->fMagic)); |
| 381 | memcpy(info->fMagic, kMagic, sizeof(kMagic)); |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 382 | |
commit-bot@chromium.org | 6f4fb0f | 2014-03-03 19:18:39 +0000 | [diff] [blame] | 383 | // Set picture info after magic bytes in the header |
commit-bot@chromium.org | e8d9614 | 2014-02-25 02:16:10 +0000 | [diff] [blame] | 384 | info->fVersion = CURRENT_PICTURE_VERSION; |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 385 | info->fWidth = fWidth; |
| 386 | info->fHeight = fHeight; |
| 387 | info->fFlags = SkPictInfo::kCrossProcess_Flag; |
| 388 | // TODO: remove this flag, since we're always float (now) |
| 389 | info->fFlags |= SkPictInfo::kScalarIsFloat_Flag; |
| 390 | |
| 391 | if (8 == sizeof(void*)) { |
| 392 | info->fFlags |= SkPictInfo::kPtrIs64Bit_Flag; |
| 393 | } |
| 394 | } |
| 395 | |
mtklein | 7373456 | 2014-06-24 12:28:34 -0700 | [diff] [blame] | 396 | // fRecord OK |
scroggo@google.com | 32ef131 | 2013-02-22 22:04:19 +0000 | [diff] [blame] | 397 | void SkPicture::serialize(SkWStream* stream, EncodeBitmap encoder) const { |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 398 | const SkPictureData* data = fData.get(); |
mtklein | 7373456 | 2014-06-24 12:28:34 -0700 | [diff] [blame] | 399 | |
| 400 | // If we're a new-format picture, backport to old format for serialization. |
| 401 | SkAutoTDelete<SkPicture> oldFormat; |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 402 | if (NULL == data && NULL != fRecord.get()) { |
mtklein | 7373456 | 2014-06-24 12:28:34 -0700 | [diff] [blame] | 403 | oldFormat.reset(backport(*fRecord, fWidth, fHeight)); |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 404 | data = oldFormat->fData.get(); |
| 405 | SkASSERT(NULL != data); |
mtklein | 7373456 | 2014-06-24 12:28:34 -0700 | [diff] [blame] | 406 | } |
| 407 | |
commit-bot@chromium.org | 2ee3c2c | 2014-05-19 12:36:15 +0000 | [diff] [blame] | 408 | SkPictInfo info; |
| 409 | this->createHeader(&info); |
commit-bot@chromium.org | 0943f5f | 2014-03-28 18:05:47 +0000 | [diff] [blame] | 410 | stream->write(&info, sizeof(info)); |
mtklein | 40684ba | 2014-06-24 10:12:39 -0700 | [diff] [blame] | 411 | |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 412 | if (NULL != data) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 413 | stream->writeBool(true); |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 414 | data->serialize(stream, encoder); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 415 | } else { |
| 416 | stream->writeBool(false); |
| 417 | } |
| 418 | } |
| 419 | |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 420 | // fRecord OK |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 421 | void SkPicture::flatten(SkWriteBuffer& buffer) const { |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 422 | const SkPictureData* data = fData.get(); |
mtklein | 7373456 | 2014-06-24 12:28:34 -0700 | [diff] [blame] | 423 | |
| 424 | // If we're a new-format picture, backport to old format for serialization. |
| 425 | SkAutoTDelete<SkPicture> oldFormat; |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 426 | if (NULL == data && NULL != fRecord.get()) { |
mtklein | 7373456 | 2014-06-24 12:28:34 -0700 | [diff] [blame] | 427 | oldFormat.reset(backport(*fRecord, fWidth, fHeight)); |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 428 | data = oldFormat->fData.get(); |
| 429 | SkASSERT(NULL != data); |
mtklein | 7373456 | 2014-06-24 12:28:34 -0700 | [diff] [blame] | 430 | } |
| 431 | |
commit-bot@chromium.org | 2ee3c2c | 2014-05-19 12:36:15 +0000 | [diff] [blame] | 432 | SkPictInfo info; |
| 433 | this->createHeader(&info); |
commit-bot@chromium.org | 0943f5f | 2014-03-28 18:05:47 +0000 | [diff] [blame] | 434 | buffer.writeByteArray(&info, sizeof(info)); |
mtklein | 40684ba | 2014-06-24 10:12:39 -0700 | [diff] [blame] | 435 | |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 436 | if (NULL != data) { |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 437 | buffer.writeBool(true); |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 438 | data->flatten(buffer); |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 439 | } else { |
| 440 | buffer.writeBool(false); |
| 441 | } |
| 442 | } |
| 443 | |
commit-bot@chromium.org | 1c30818 | 2014-03-19 22:54:40 +0000 | [diff] [blame] | 444 | #if SK_SUPPORT_GPU |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 445 | // fRecord TODO |
commit-bot@chromium.org | a1ff26a | 2014-05-30 21:52:52 +0000 | [diff] [blame] | 446 | bool SkPicture::suitableForGpuRasterization(GrContext* context, const char **reason) const { |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 447 | if (NULL == fData.get()) { |
robertphillips | 0bdbea7 | 2014-06-11 11:37:55 -0700 | [diff] [blame] | 448 | if (NULL != reason) { |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 449 | *reason = "Missing internal data."; |
robertphillips | 0bdbea7 | 2014-06-11 11:37:55 -0700 | [diff] [blame] | 450 | } |
| 451 | return false; |
commit-bot@chromium.org | a1ff26a | 2014-05-30 21:52:52 +0000 | [diff] [blame] | 452 | } |
robertphillips | 0bdbea7 | 2014-06-11 11:37:55 -0700 | [diff] [blame] | 453 | |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 454 | return fData->suitableForGpuRasterization(context, reason); |
commit-bot@chromium.org | eb9547c | 2014-03-19 21:24:25 +0000 | [diff] [blame] | 455 | } |
commit-bot@chromium.org | 1c30818 | 2014-03-19 22:54:40 +0000 | [diff] [blame] | 456 | #endif |
commit-bot@chromium.org | eb9547c | 2014-03-19 21:24:25 +0000 | [diff] [blame] | 457 | |
tomhudson | d930511 | 2014-07-05 13:37:53 -0700 | [diff] [blame] | 458 | // fRecord OK |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 459 | bool SkPicture::willPlayBackBitmaps() const { |
tomhudson | d930511 | 2014-07-05 13:37:53 -0700 | [diff] [blame] | 460 | if (fRecord.get()) { |
| 461 | return fRecordWillPlayBackBitmaps; |
| 462 | } |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 463 | if (!fData.get()) { |
commit-bot@chromium.org | 145d1c0 | 2014-03-16 19:46:36 +0000 | [diff] [blame] | 464 | return false; |
| 465 | } |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 466 | return fData->containsBitmaps(); |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 469 | // fRecord OK |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 470 | static int32_t next_picture_generation_id() { |
| 471 | static int32_t gPictureGenerationID = 0; |
| 472 | // do a loop in case our global wraps around, as we never want to |
| 473 | // return a 0 |
| 474 | int32_t genID; |
| 475 | do { |
| 476 | genID = sk_atomic_inc(&gPictureGenerationID) + 1; |
commit-bot@chromium.org | 2b4e370 | 2014-04-07 18:26:22 +0000 | [diff] [blame] | 477 | } while (SK_InvalidGenID == genID); |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 478 | return genID; |
| 479 | } |
| 480 | |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 481 | // fRecord OK |
commit-bot@chromium.org | 2b4e370 | 2014-04-07 18:26:22 +0000 | [diff] [blame] | 482 | uint32_t SkPicture::uniqueID() const { |
commit-bot@chromium.org | 2b4e370 | 2014-04-07 18:26:22 +0000 | [diff] [blame] | 483 | if (SK_InvalidGenID == fUniqueID) { |
| 484 | fUniqueID = next_picture_generation_id(); |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 485 | } |
commit-bot@chromium.org | 2b4e370 | 2014-04-07 18:26:22 +0000 | [diff] [blame] | 486 | return fUniqueID; |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 487 | } |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 488 | |
| 489 | // fRecord OK |
| 490 | SkPicture::SkPicture(int width, int height, SkRecord* record) |
| 491 | : fWidth(width) |
| 492 | , fHeight(height) |
tomhudson | d930511 | 2014-07-05 13:37:53 -0700 | [diff] [blame] | 493 | , fRecord(record) |
| 494 | , fRecordWillPlayBackBitmaps(SkRecordWillPlaybackBitmaps(*record)) { |
Mike Klein | 744fb73 | 2014-06-23 15:13:26 -0400 | [diff] [blame] | 495 | this->needsNewGenID(); |
| 496 | } |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame^] | 497 | |
| 498 | // Note that we are assuming that this entry point will only be called from |
| 499 | // one thread. Currently the only client of this method is |
| 500 | // SkGpuDevice::EXPERIMENTAL_optimize which should be only called from a single |
| 501 | // thread. |
| 502 | void SkPicture::addDeletionListener(DeletionListener* listener) const { |
| 503 | SkASSERT(NULL != listener); |
| 504 | |
| 505 | *fDeletionListeners.append() = SkRef(listener); |
| 506 | } |
| 507 | |
| 508 | void SkPicture::callDeletionListeners() { |
| 509 | for (int i = 0; i < fDeletionListeners.count(); ++i) { |
| 510 | fDeletionListeners[i]->onDeletion(this->uniqueID()); |
| 511 | } |
| 512 | |
| 513 | fDeletionListeners.unrefAll(); |
| 514 | } |