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" |
8 #include "SkRecorder.h" | 9 #include "SkRecorder.h" |
9 #include "SkPatchUtils.h" | 10 #include "SkPatchUtils.h" |
10 #include "SkPicture.h" | 11 #include "SkPicture.h" |
11 | 12 |
12 // SkCanvas will fail in mysterious ways if it doesn't know the real width and h
eight. | 13 // SkCanvas will fail in mysterious ways if it doesn't know the real width and h
eight. |
13 SkRecorder::SkRecorder(SkRecord* record, int width, int height) | 14 SkRecorder::SkRecorder(SkRecord* record, int width, int height) |
14 : SkCanvas(width, height, SkCanvas::kConservativeRasterClip_InitFlag) | 15 : SkCanvas(width, height, SkCanvas::kConservativeRasterClip_InitFlag) |
15 , fRecord(record) | 16 , fRecord(record) |
16 , fSaveLayerCount(0) {} | 17 , fSaveLayerCount(0) {} |
17 | 18 |
| 19 SkRecorder::~SkRecorder() { |
| 20 fDrawableList.unrefAll(); |
| 21 } |
| 22 |
18 void SkRecorder::forgetRecord() { | 23 void SkRecorder::forgetRecord() { |
| 24 fDrawableList.unrefAll(); |
| 25 fDrawableList.reset(); |
19 fRecord = NULL; | 26 fRecord = NULL; |
20 } | 27 } |
21 | 28 |
| 29 // ReleaseProc for SkData, assuming the data was allocated via sk_malloc, and it
s contents are an |
| 30 // array of SkRefCnt* which need to be unref'd. |
| 31 // |
| 32 static void unref_all_malloc_releaseProc(const void* ptr, size_t length, void* c
ontext) { |
| 33 SkASSERT(ptr == context); // our context is our ptr, allocated via sk_mall
oc |
| 34 int count = SkToInt(length / sizeof(SkRefCnt*)); |
| 35 SkASSERT(count * sizeof(SkRefCnt*) == length); // our length is snug for th
e array |
| 36 |
| 37 SkRefCnt* const* array = reinterpret_cast<SkRefCnt* const*>(ptr); |
| 38 for (int i = 0; i < count; ++i) { |
| 39 SkSafeUnref(array[i]); |
| 40 } |
| 41 sk_free(context); |
| 42 } |
| 43 |
| 44 // Return an uninitialized SkData sized for "count" SkRefCnt pointers. They will
be unref'd when |
| 45 // the SkData is destroyed. |
| 46 // |
| 47 static SkData* new_uninitialized_refcnt_ptrs(int count) { |
| 48 size_t length = count * sizeof(SkRefCnt*); |
| 49 void* array = sk_malloc_throw(length); |
| 50 void* context = array; |
| 51 return SkData::NewWithProc(array, length, unref_all_malloc_releaseProc, cont
ext); |
| 52 } |
| 53 |
| 54 SkData* SkRecorder::newDrawableSnapshot(SkBBHFactory* factory, uint32_t recordFl
ags) { |
| 55 const int count = fDrawableList.count(); |
| 56 if (0 == count) { |
| 57 return NULL; |
| 58 } |
| 59 SkData* data = new_uninitialized_refcnt_ptrs(count); |
| 60 SkPicture** pics = reinterpret_cast<SkPicture**>(data->writable_data()); |
| 61 for (int i = 0; i < count; ++i) { |
| 62 pics[i] = fDrawableList[i]->newPictureSnapshot(factory, recordFlags); |
| 63 } |
| 64 return data; |
| 65 } |
| 66 |
22 // To make appending to fRecord a little less verbose. | 67 // To make appending to fRecord a little less verbose. |
23 #define APPEND(T, ...) \ | 68 #define APPEND(T, ...) \ |
24 SkNEW_PLACEMENT_ARGS(fRecord->append<SkRecords::T>(), SkRecords::T, (__V
A_ARGS__)) | 69 SkNEW_PLACEMENT_ARGS(fRecord->append<SkRecords::T>(), SkRecords::T, (__V
A_ARGS__)) |
25 | 70 |
26 // For methods which must call back into SkCanvas. | 71 // For methods which must call back into SkCanvas. |
27 #define INHERITED(method, ...) this->SkCanvas::method(__VA_ARGS__) | 72 #define INHERITED(method, ...) this->SkCanvas::method(__VA_ARGS__) |
28 | 73 |
29 // The structs we're creating all copy their constructor arguments. Given the w
ay the SkRecords | 74 // The structs we're creating all copy their constructor arguments. Given the w
ay the SkRecords |
30 // framework works, sometimes they happen to technically be copied twice, which
is fine and elided | 75 // framework works, sometimes they happen to technically be copied twice, which
is fine and elided |
31 // into a single copy unless the class has a non-trivial copy constructor. For
classes with | 76 // into a single copy unless the class has a non-trivial copy constructor. For
classes with |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 } | 160 } |
116 | 161 |
117 void SkRecorder::drawRRect(const SkRRect& rrect, const SkPaint& paint) { | 162 void SkRecorder::drawRRect(const SkRRect& rrect, const SkPaint& paint) { |
118 APPEND(DrawRRect, delay_copy(paint), rrect); | 163 APPEND(DrawRRect, delay_copy(paint), rrect); |
119 } | 164 } |
120 | 165 |
121 void SkRecorder::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, const
SkPaint& paint) { | 166 void SkRecorder::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, const
SkPaint& paint) { |
122 APPEND(DrawDRRect, delay_copy(paint), outer, inner); | 167 APPEND(DrawDRRect, delay_copy(paint), outer, inner); |
123 } | 168 } |
124 | 169 |
| 170 void SkRecorder::onDrawDrawable(SkCanvasDrawable* drawable) { |
| 171 *fDrawableList.append() = SkRef(drawable); |
| 172 APPEND(DrawDrawable, drawable->getBounds(), fDrawableList.count() - 1); |
| 173 } |
| 174 |
125 void SkRecorder::drawPath(const SkPath& path, const SkPaint& paint) { | 175 void SkRecorder::drawPath(const SkPath& path, const SkPaint& paint) { |
126 APPEND(DrawPath, delay_copy(paint), delay_copy(path)); | 176 APPEND(DrawPath, delay_copy(paint), delay_copy(path)); |
127 } | 177 } |
128 | 178 |
129 void SkRecorder::drawBitmap(const SkBitmap& bitmap, | 179 void SkRecorder::drawBitmap(const SkBitmap& bitmap, |
130 SkScalar left, | 180 SkScalar left, |
131 SkScalar top, | 181 SkScalar top, |
132 const SkPaint* paint) { | 182 const SkPaint* paint) { |
133 APPEND(DrawBitmap, this->copy(paint), delay_copy(bitmap), left, top); | 183 APPEND(DrawBitmap, this->copy(paint), delay_copy(bitmap), left, top); |
134 } | 184 } |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 APPEND(EndCommentGroup); | 367 APPEND(EndCommentGroup); |
318 } | 368 } |
319 | 369 |
320 bool SkRecorder::isDrawingToLayer() const { | 370 bool SkRecorder::isDrawingToLayer() const { |
321 return fSaveLayerCount > 0; | 371 return fSaveLayerCount > 0; |
322 } | 372 } |
323 | 373 |
324 void SkRecorder::drawData(const void* data, size_t length) { | 374 void SkRecorder::drawData(const void* data, size_t length) { |
325 APPEND(DrawData, copy((const char*)data), length); | 375 APPEND(DrawData, copy((const char*)data), length); |
326 } | 376 } |
OLD | NEW |