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