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