| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkData.h" | |
| 9 #include "SkRecorder.h" | 8 #include "SkRecorder.h" |
| 10 #include "SkPatchUtils.h" | 9 #include "SkPatchUtils.h" |
| 11 #include "SkPicture.h" | 10 #include "SkPicture.h" |
| 12 | 11 |
| 13 SkRecorder::SkRecorder(SkRecord* record, int width, int height) | 12 SkRecorder::SkRecorder(SkRecord* record, int width, int height) |
| 14 : SkCanvas(SkIRect::MakeWH(width, height), SkCanvas::kConservativeRasterClip
_InitFlag) | 13 : SkCanvas(SkIRect::MakeWH(width, height), SkCanvas::kConservativeRasterClip
_InitFlag) |
| 15 , fRecord(record) | 14 , fRecord(record) |
| 16 , fSaveLayerCount(0) {} | 15 , fSaveLayerCount(0) {} |
| 17 | 16 |
| 18 SkRecorder::SkRecorder(SkRecord* record, const SkRect& bounds) | 17 SkRecorder::SkRecorder(SkRecord* record, const SkRect& bounds) |
| 19 : SkCanvas(bounds.roundOut(), SkCanvas::kConservativeRasterClip_InitFlag) | 18 : SkCanvas(bounds.roundOut(), SkCanvas::kConservativeRasterClip_InitFlag) |
| 20 , fRecord(record) | 19 , fRecord(record) |
| 21 , fSaveLayerCount(0) {} | 20 , fSaveLayerCount(0) {} |
| 22 | 21 |
| 23 SkRecorder::~SkRecorder() { | 22 SkRecorder::~SkRecorder() { |
| 24 fDrawableList.unrefAll(); | 23 fDrawableList.unrefAll(); |
| 25 } | 24 } |
| 26 | 25 |
| 27 void SkRecorder::forgetRecord() { | 26 void SkRecorder::forgetRecord() { |
| 28 fDrawableList.unrefAll(); | 27 fDrawableList.unrefAll(); |
| 29 fDrawableList.reset(); | 28 fDrawableList.reset(); |
| 30 fRecord = NULL; | 29 fRecord = NULL; |
| 31 } | 30 } |
| 32 | 31 |
| 33 // ReleaseProc for SkData, assuming the data was allocated via sk_malloc, and it
s contents are an | 32 SkPicture::SnapshotArray* SkRecorder::newDrawableSnapshot(SkBBHFactory* factory, |
| 34 // array of SkPicture* which need to be unref'd. | 33 uint32_t recordFlags)
{ |
| 35 // | |
| 36 static void unref_all_malloc_releaseProc(const void* ptr, size_t length, void* c
ontext) { | |
| 37 SkASSERT(ptr == context); // our context is our ptr, allocated via sk_mall
oc | |
| 38 int count = SkToInt(length / sizeof(SkPicture*)); | |
| 39 SkASSERT(count * sizeof(SkPicture*) == length); // our length is snug for t
he array | |
| 40 | |
| 41 SkPicture* const* array = reinterpret_cast<SkPicture* const*>(ptr); | |
| 42 for (int i = 0; i < count; ++i) { | |
| 43 SkSafeUnref(array[i]); | |
| 44 } | |
| 45 sk_free(context); | |
| 46 } | |
| 47 | |
| 48 // Return an uninitialized SkData sized for "count" SkPicture pointers. They wil
l be unref'd when | |
| 49 // the SkData is destroyed. | |
| 50 // | |
| 51 static SkData* new_uninitialized_picture_ptrs(int count) { | |
| 52 size_t length = count * sizeof(SkPicture*); | |
| 53 void* array = sk_malloc_throw(length); | |
| 54 void* context = array; | |
| 55 return SkData::NewWithProc(array, length, unref_all_malloc_releaseProc, cont
ext); | |
| 56 } | |
| 57 | |
| 58 SkData* SkRecorder::newDrawableSnapshot(SkBBHFactory* factory, uint32_t recordFl
ags) { | |
| 59 const int count = fDrawableList.count(); | 34 const int count = fDrawableList.count(); |
| 60 if (0 == count) { | 35 if (0 == count) { |
| 61 return NULL; | 36 return NULL; |
| 62 } | 37 } |
| 63 SkData* data = new_uninitialized_picture_ptrs(count); | 38 SkAutoTMalloc<const SkPicture*> pics(count); |
| 64 SkPicture** pics = reinterpret_cast<SkPicture**>(data->writable_data()); | |
| 65 for (int i = 0; i < count; ++i) { | 39 for (int i = 0; i < count; ++i) { |
| 66 pics[i] = fDrawableList[i]->newPictureSnapshot(factory, recordFlags); | 40 pics[i] = fDrawableList[i]->newPictureSnapshot(factory, recordFlags); |
| 67 } | 41 } |
| 68 return data; | 42 return SkNEW_ARGS(SkPicture::SnapshotArray, (pics.detach(), count)); |
| 69 } | 43 } |
| 70 | 44 |
| 71 // To make appending to fRecord a little less verbose. | 45 // To make appending to fRecord a little less verbose. |
| 72 #define APPEND(T, ...) \ | 46 #define APPEND(T, ...) \ |
| 73 SkNEW_PLACEMENT_ARGS(fRecord->append<SkRecords::T>(), SkRecords::T, (__V
A_ARGS__)) | 47 SkNEW_PLACEMENT_ARGS(fRecord->append<SkRecords::T>(), SkRecords::T, (__V
A_ARGS__)) |
| 74 | 48 |
| 75 // For methods which must call back into SkCanvas. | 49 // For methods which must call back into SkCanvas. |
| 76 #define INHERITED(method, ...) this->SkCanvas::method(__VA_ARGS__) | 50 #define INHERITED(method, ...) this->SkCanvas::method(__VA_ARGS__) |
| 77 | 51 |
| 78 // The structs we're creating all copy their constructor arguments. Given the w
ay the SkRecords | 52 // The structs we're creating all copy their constructor arguments. Given the w
ay the SkRecords |
| (...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 374 APPEND(EndCommentGroup); | 348 APPEND(EndCommentGroup); |
| 375 } | 349 } |
| 376 | 350 |
| 377 bool SkRecorder::isDrawingToLayer() const { | 351 bool SkRecorder::isDrawingToLayer() const { |
| 378 return fSaveLayerCount > 0; | 352 return fSaveLayerCount > 0; |
| 379 } | 353 } |
| 380 | 354 |
| 381 void SkRecorder::drawData(const void* data, size_t length) { | 355 void SkRecorder::drawData(const void* data, size_t length) { |
| 382 APPEND(DrawData, copy((const char*)data), length); | 356 APPEND(DrawData, copy((const char*)data), length); |
| 383 } | 357 } |
| OLD | NEW |