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