commit-bot@chromium.org | c4b21e6 | 2014-04-11 18:33:31 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2014 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 8 | #include "SkRecorder.h" |
| 9 | #include "SkPicture.h" |
| 10 | |
| 11 | // SkCanvas will fail in mysterious ways if it doesn't know the real width and height. |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 12 | SkRecorder::SkRecorder(SkRecorder::Mode mode, SkRecord* record, int width, int height) |
| 13 | : SkCanvas(width, height), fMode(mode), fRecord(record) {} |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 14 | |
| 15 | // To make appending to fRecord a little less verbose. |
| 16 | #define APPEND(T, ...) \ |
| 17 | SkNEW_PLACEMENT_ARGS(fRecord->append<SkRecords::T>(), SkRecords::T, (__VA_ARGS__)) |
| 18 | |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 19 | // For methods which must call back into SkCanvas in kReadWrite_Mode. |
| 20 | #define INHERITED(method, ...) if (fMode == kReadWrite_Mode) this->SkCanvas::method(__VA_ARGS__) |
| 21 | |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 22 | // The structs we're creating all copy their constructor arguments. Given the way the SkRecords |
| 23 | // framework works, sometimes they happen to technically be copied twice, which is fine and elided |
| 24 | // into a single copy unless the class has a non-trivial copy constructor. For classes with |
| 25 | // non-trivial copy constructors, we skip the first copy (and its destruction) by wrapping the value |
| 26 | // with delay_copy(), forcing the argument to be passed by const&. |
| 27 | // |
| 28 | // This is used below for SkBitmap, SkPaint, SkPath, and SkRegion, which all have non-trivial copy |
| 29 | // constructors and destructors. You'll know you've got a good candidate T if you see ~T() show up |
| 30 | // unexpectedly on a profile of record time. Otherwise don't bother. |
| 31 | template <typename T> |
| 32 | class Reference { |
| 33 | public: |
| 34 | Reference(const T& x) : fX(x) {} |
| 35 | operator const T&() const { return fX; } |
| 36 | private: |
| 37 | const T& fX; |
| 38 | }; |
| 39 | |
| 40 | template <typename T> |
| 41 | static Reference<T> delay_copy(const T& x) { return Reference<T>(x); } |
| 42 | |
| 43 | // Use copy() only for optional arguments, to be copied if present or skipped if not. |
| 44 | // (For most types we just pass by value and let copy constructors do their thing.) |
| 45 | template <typename T> |
| 46 | T* SkRecorder::copy(const T* src) { |
| 47 | if (NULL == src) { |
| 48 | return NULL; |
| 49 | } |
| 50 | return SkNEW_PLACEMENT_ARGS(fRecord->alloc<T>(), T, (*src)); |
| 51 | } |
| 52 | |
| 53 | // This copy() is for arrays. |
| 54 | // It will work with POD or non-POD, though currently we only use it for POD. |
| 55 | template <typename T> |
| 56 | T* SkRecorder::copy(const T src[], unsigned count) { |
| 57 | if (NULL == src) { |
| 58 | return NULL; |
| 59 | } |
| 60 | T* dst = fRecord->alloc<T>(count); |
| 61 | for (unsigned i = 0; i < count; i++) { |
| 62 | SkNEW_PLACEMENT_ARGS(dst + i, T, (src[i])); |
| 63 | } |
| 64 | return dst; |
| 65 | } |
| 66 | |
| 67 | // Specialization for copying strings, using memcpy. |
| 68 | // This measured around 2x faster for copying code points, |
| 69 | // but I found no corresponding speedup for other arrays. |
| 70 | template <> |
| 71 | char* SkRecorder::copy(const char src[], unsigned count) { |
| 72 | if (NULL == src) { |
| 73 | return NULL; |
| 74 | } |
| 75 | char* dst = fRecord->alloc<char>(count); |
| 76 | memcpy(dst, src, count); |
| 77 | return dst; |
| 78 | } |
| 79 | |
| 80 | void SkRecorder::clear(SkColor color) { |
| 81 | APPEND(Clear, color); |
| 82 | } |
| 83 | |
| 84 | void SkRecorder::drawPaint(const SkPaint& paint) { |
| 85 | APPEND(DrawPaint, delay_copy(paint)); |
| 86 | } |
| 87 | |
| 88 | void SkRecorder::drawPoints(PointMode mode, |
| 89 | size_t count, |
| 90 | const SkPoint pts[], |
| 91 | const SkPaint& paint) { |
| 92 | APPEND(DrawPoints, mode, count, this->copy(pts, count), delay_copy(paint)); |
| 93 | } |
| 94 | |
| 95 | void SkRecorder::drawRect(const SkRect& rect, const SkPaint& paint) { |
| 96 | APPEND(DrawRect, rect, delay_copy(paint)); |
| 97 | } |
| 98 | |
| 99 | void SkRecorder::drawOval(const SkRect& oval, const SkPaint& paint) { |
| 100 | APPEND(DrawOval, oval, delay_copy(paint)); |
| 101 | } |
| 102 | |
| 103 | void SkRecorder::drawRRect(const SkRRect& rrect, const SkPaint& paint) { |
| 104 | APPEND(DrawRRect, rrect, delay_copy(paint)); |
| 105 | } |
| 106 | |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 107 | void SkRecorder::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) { |
| 108 | APPEND(DrawDRRect, outer, inner, delay_copy(paint)); |
| 109 | } |
| 110 | |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 111 | void SkRecorder::drawPath(const SkPath& path, const SkPaint& paint) { |
| 112 | APPEND(DrawPath, delay_copy(path), delay_copy(paint)); |
| 113 | } |
| 114 | |
| 115 | void SkRecorder::drawBitmap(const SkBitmap& bitmap, |
| 116 | SkScalar left, |
| 117 | SkScalar top, |
| 118 | const SkPaint* paint) { |
| 119 | APPEND(DrawBitmap, delay_copy(bitmap), left, top, this->copy(paint)); |
| 120 | } |
| 121 | |
| 122 | void SkRecorder::drawBitmapRectToRect(const SkBitmap& bitmap, |
| 123 | const SkRect* src, |
| 124 | const SkRect& dst, |
| 125 | const SkPaint* paint, |
| 126 | DrawBitmapRectFlags flags) { |
| 127 | APPEND(DrawBitmapRectToRect, |
| 128 | delay_copy(bitmap), this->copy(src), dst, this->copy(paint), flags); |
| 129 | } |
| 130 | |
| 131 | void SkRecorder::drawBitmapMatrix(const SkBitmap& bitmap, |
| 132 | const SkMatrix& matrix, |
| 133 | const SkPaint* paint) { |
| 134 | APPEND(DrawBitmapMatrix, delay_copy(bitmap), matrix, this->copy(paint)); |
| 135 | } |
| 136 | |
| 137 | void SkRecorder::drawBitmapNine(const SkBitmap& bitmap, |
| 138 | const SkIRect& center, |
| 139 | const SkRect& dst, |
| 140 | const SkPaint* paint) { |
| 141 | APPEND(DrawBitmapNine, delay_copy(bitmap), center, dst, this->copy(paint)); |
| 142 | } |
| 143 | |
| 144 | void SkRecorder::drawSprite(const SkBitmap& bitmap, int left, int top, const SkPaint* paint) { |
| 145 | APPEND(DrawSprite, delay_copy(bitmap), left, top, this->copy(paint)); |
| 146 | } |
| 147 | |
| 148 | void SkRecorder::drawText(const void* text, size_t byteLength, |
| 149 | SkScalar x, SkScalar y, const SkPaint& paint) { |
| 150 | APPEND(DrawText, |
| 151 | this->copy((const char*)text, byteLength), byteLength, x, y, delay_copy(paint)); |
| 152 | } |
| 153 | |
| 154 | void SkRecorder::drawPosText(const void* text, size_t byteLength, |
| 155 | const SkPoint pos[], const SkPaint& paint) { |
| 156 | const unsigned points = paint.countText(text, byteLength); |
| 157 | APPEND(DrawPosText, |
| 158 | this->copy((const char*)text, byteLength), byteLength, |
| 159 | this->copy(pos, points), delay_copy(paint)); |
| 160 | } |
| 161 | |
| 162 | void SkRecorder::drawPosTextH(const void* text, size_t byteLength, |
| 163 | const SkScalar xpos[], SkScalar constY, const SkPaint& paint) { |
| 164 | const unsigned points = paint.countText(text, byteLength); |
| 165 | APPEND(DrawPosTextH, |
| 166 | this->copy((const char*)text, byteLength), byteLength, |
| 167 | this->copy(xpos, points), constY, delay_copy(paint)); |
| 168 | } |
| 169 | |
| 170 | void SkRecorder::drawTextOnPath(const void* text, size_t byteLength, |
| 171 | const SkPath& path, const SkMatrix* matrix, const SkPaint& paint) { |
| 172 | APPEND(DrawTextOnPath, |
| 173 | this->copy((const char*)text, byteLength), byteLength, |
| 174 | delay_copy(path), this->copy(matrix), delay_copy(paint)); |
| 175 | } |
| 176 | |
| 177 | void SkRecorder::drawPicture(SkPicture& picture) { |
| 178 | picture.draw(this); |
| 179 | } |
| 180 | |
| 181 | void SkRecorder::drawVertices(VertexMode vmode, |
| 182 | int vertexCount, const SkPoint vertices[], |
| 183 | const SkPoint texs[], const SkColor colors[], |
| 184 | SkXfermode* xmode, |
| 185 | const uint16_t indices[], int indexCount, const SkPaint& paint) { |
| 186 | APPEND(DrawVertices, vmode, |
| 187 | vertexCount, |
| 188 | this->copy(vertices, vertexCount), |
| 189 | texs ? this->copy(texs, vertexCount) : NULL, |
| 190 | colors ? this->copy(colors, vertexCount) : NULL, |
| 191 | xmode, |
| 192 | this->copy(indices, indexCount), |
| 193 | indexCount, |
| 194 | delay_copy(paint)); |
| 195 | } |
| 196 | |
| 197 | void SkRecorder::willSave(SkCanvas::SaveFlags flags) { |
| 198 | APPEND(Save, flags); |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 199 | INHERITED(willSave, flags); |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | SkCanvas::SaveLayerStrategy SkRecorder::willSaveLayer(const SkRect* bounds, |
| 203 | const SkPaint* paint, |
| 204 | SkCanvas::SaveFlags flags) { |
| 205 | APPEND(SaveLayer, this->copy(bounds), this->copy(paint), flags); |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 206 | INHERITED(willSaveLayer, bounds, paint, flags); |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 207 | return SkCanvas::kNoLayer_SaveLayerStrategy; |
| 208 | } |
| 209 | |
| 210 | void SkRecorder::willRestore() { |
| 211 | APPEND(Restore); |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 212 | INHERITED(willRestore); |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 213 | } |
| 214 | |
commit-bot@chromium.org | 03a99b8 | 2014-04-08 15:17:17 +0000 | [diff] [blame] | 215 | void SkRecorder::onPushCull(const SkRect& rect) { |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 216 | APPEND(PushCull, rect, SkRecords::kUnsetPopOffset); |
commit-bot@chromium.org | 03a99b8 | 2014-04-08 15:17:17 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | void SkRecorder::onPopCull() { |
| 220 | APPEND(PopCull); |
| 221 | } |
| 222 | |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 223 | void SkRecorder::didConcat(const SkMatrix& matrix) { |
| 224 | APPEND(Concat, matrix); |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 225 | INHERITED(didConcat, matrix); |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | void SkRecorder::didSetMatrix(const SkMatrix& matrix) { |
| 229 | APPEND(SetMatrix, matrix); |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 230 | INHERITED(didSetMatrix, matrix); |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | void SkRecorder::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle) { |
| 234 | APPEND(ClipRect, rect, op, edgeStyle == kSoft_ClipEdgeStyle); |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 235 | INHERITED(onClipRect, rect, op, edgeStyle); |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | void SkRecorder::onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle) { |
| 239 | APPEND(ClipRRect, rrect, op, edgeStyle == kSoft_ClipEdgeStyle); |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 240 | INHERITED(updateClipConservativelyUsingBounds, rrect.getBounds(), op, false); |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | void SkRecorder::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle) { |
| 244 | APPEND(ClipPath, delay_copy(path), op, edgeStyle == kSoft_ClipEdgeStyle); |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 245 | INHERITED(updateClipConservativelyUsingBounds, path.getBounds(), op, path.isInverseFillType()); |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | void SkRecorder::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) { |
| 249 | APPEND(ClipRegion, delay_copy(deviceRgn), op); |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 250 | INHERITED(onClipRegion, deviceRgn, op); |
commit-bot@chromium.org | e3ff558 | 2014-04-01 16:24:06 +0000 | [diff] [blame] | 251 | } |