Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / graphics / ReplayingCanvas.cpp
1 /*
2  * Copyright (C) 2013 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32 #include "platform/graphics/ReplayingCanvas.h"
33
34 #include "third_party/skia/include/core/SkBitmapDevice.h"
35
36 namespace blink {
37
38 class AutoReplayer {
39 public:
40     explicit AutoReplayer(ReplayingCanvas*);
41     ~AutoReplayer();
42
43 private:
44     ReplayingCanvas* m_canvas;
45 };
46
47 AutoReplayer::AutoReplayer(ReplayingCanvas* replayingCanvas) : m_canvas(replayingCanvas)
48 {
49     replayingCanvas->m_depthCount++;
50 }
51
52 AutoReplayer::~AutoReplayer()
53 {
54     m_canvas->m_depthCount--;
55     if (m_canvas->m_depthCount)
56         return;
57     m_canvas->m_stepCount++;
58     m_canvas->updateInRange();
59 }
60
61 ReplayingCanvas::ReplayingCanvas(SkBitmap bitmap, unsigned fromStep, unsigned toStep)
62     : InterceptingCanvas(bitmap), m_fromStep(fromStep), m_toStep(toStep), m_stepCount(0), m_abortDrawing(false)
63 {
64 }
65
66 void ReplayingCanvas::resetStepCount()
67 {
68     m_stepCount = 0;
69 }
70
71 void ReplayingCanvas::updateInRange()
72 {
73     if (m_abortDrawing)
74         return;
75     if (m_toStep && m_stepCount > m_toStep)
76         m_abortDrawing = true;
77     if (m_stepCount == m_fromStep)
78         this->SkCanvas::clear(SkColorSetARGB(255, 255, 255, 255)); // FIXME: fill with nine patch instead.
79 }
80
81 bool ReplayingCanvas::abortDrawing()
82 {
83     return m_abortDrawing;
84 }
85
86 void ReplayingCanvas::clear(SkColor color)
87 {
88     AutoReplayer replayer(this);
89     this->SkCanvas::clear(color);
90 }
91
92 void ReplayingCanvas::drawPaint(const SkPaint& paint)
93 {
94     AutoReplayer replayer(this);
95     this->SkCanvas::drawPaint(paint);
96 }
97
98 void ReplayingCanvas::drawPoints(PointMode mode, size_t count, const SkPoint pts[], const SkPaint& paint)
99 {
100     AutoReplayer replayer(this);
101     this->SkCanvas::drawPoints(mode, count, pts, paint);
102 }
103
104 void ReplayingCanvas::drawRect(const SkRect& rect, const SkPaint& paint)
105 {
106     AutoReplayer replayer(this);
107     this->SkCanvas::drawRect(rect, paint);
108 }
109
110 void ReplayingCanvas::drawOval(const SkRect& rect, const SkPaint& paint)
111 {
112     AutoReplayer replayer(this);
113     this->SkCanvas::drawOval(rect, paint);
114 }
115
116 void ReplayingCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint)
117 {
118     AutoReplayer replayer(this);
119     this->SkCanvas::drawRRect(rrect, paint);
120 }
121
122 void ReplayingCanvas::drawPath(const SkPath& path, const SkPaint& paint)
123 {
124     AutoReplayer replayer(this);
125     this->SkCanvas::drawPath(path, paint);
126 }
127
128 void ReplayingCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top, const SkPaint* paint)
129 {
130     AutoReplayer replayer(this);
131     this->SkCanvas::drawBitmap(bitmap, left, top, paint);
132 }
133
134 void ReplayingCanvas::drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src, const SkRect& dst,
135     const SkPaint* paint, DrawBitmapRectFlags flags)
136 {
137     AutoReplayer replayer(this);
138     this->SkCanvas::drawBitmapRectToRect(bitmap, src, dst, paint, flags);
139 }
140
141 void ReplayingCanvas::drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& m, const SkPaint* paint)
142 {
143     AutoReplayer replayer(this);
144     this->SkCanvas::drawBitmapMatrix(bitmap, m, paint);
145 }
146
147 void ReplayingCanvas::drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, const SkRect& dst, const SkPaint* paint)
148 {
149     AutoReplayer replayer(this);
150     this->SkCanvas::drawBitmapNine(bitmap, center, dst, paint);
151 }
152
153 void ReplayingCanvas::drawSprite(const SkBitmap& bitmap, int left, int top, const SkPaint* paint)
154 {
155     AutoReplayer replayer(this);
156     this->SkCanvas::drawSprite(bitmap, left, top, paint);
157 }
158
159 void ReplayingCanvas::drawVertices(VertexMode vmode, int vertexCount, const SkPoint vertices[], const SkPoint texs[],
160     const SkColor colors[], SkXfermode* xmode, const uint16_t indices[], int indexCount, const SkPaint& paint)
161 {
162     AutoReplayer replayer(this);
163     this->SkCanvas::drawVertices(vmode, vertexCount, vertices, texs, colors, xmode, indices, indexCount, paint);
164 }
165
166 void ReplayingCanvas::drawData(const void* data, size_t length)
167 {
168     AutoReplayer replayer(this);
169     this->SkCanvas::drawData(data, length);
170 }
171
172 void ReplayingCanvas::beginCommentGroup(const char* description)
173 {
174     AutoReplayer replayer(this);
175     this->SkCanvas::beginCommentGroup(description);
176 }
177
178 void ReplayingCanvas::addComment(const char* keyword, const char* value)
179 {
180     AutoReplayer replayer(this);
181     this->SkCanvas::addComment(keyword, value);
182 }
183
184 void ReplayingCanvas::endCommentGroup()
185 {
186     AutoReplayer replayer(this);
187     this->SkCanvas::endCommentGroup();
188 }
189
190 void ReplayingCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint)
191 {
192     AutoReplayer replayer(this);
193     this->SkCanvas::onDrawDRRect(outer, inner, paint);
194 }
195
196 void ReplayingCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y, const SkPaint& paint)
197 {
198     AutoReplayer replayer(this);
199     this->SkCanvas::onDrawText(text, byteLength, x, y, paint);
200 }
201
202 void ReplayingCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[], const SkPaint& paint)
203 {
204     AutoReplayer replayer(this);
205     this->SkCanvas::onDrawPosText(text, byteLength, pos, paint);
206 }
207
208 void ReplayingCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[], SkScalar constY, const SkPaint& paint)
209 {
210     AutoReplayer replayer(this);
211     this->SkCanvas::onDrawPosTextH(text, byteLength, xpos, constY, paint);
212 }
213
214 void ReplayingCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path, const SkMatrix* matrix, const SkPaint& paint)
215 {
216     AutoReplayer replayer(this);
217     this->SkCanvas::onDrawTextOnPath(text, byteLength, path, matrix, paint);
218 }
219
220 void ReplayingCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, const SkPaint& paint)
221 {
222     AutoReplayer replayer(this);
223     this->SkCanvas::onDrawTextBlob(blob, x, y, paint);
224 }
225
226 void ReplayingCanvas::onPushCull(const SkRect& cullRect)
227 {
228     AutoReplayer replayer(this);
229     this->SkCanvas::onPushCull(cullRect);
230 }
231
232 void ReplayingCanvas::onPopCull()
233 {
234     AutoReplayer replayer(this);
235     this->SkCanvas::onPopCull();
236 }
237
238 void ReplayingCanvas::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle)
239 {
240     AutoReplayer replayer(this);
241     this->SkCanvas::onClipRect(rect, op, edgeStyle);
242 }
243
244 void ReplayingCanvas::onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle)
245 {
246     AutoReplayer replayer(this);
247     this->SkCanvas::onClipRRect(rrect, op, edgeStyle);
248 }
249
250 void ReplayingCanvas::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle)
251 {
252     AutoReplayer replayer(this);
253     this->SkCanvas::onClipPath(path, op, edgeStyle);
254 }
255
256 void ReplayingCanvas::onClipRegion(const SkRegion& region, SkRegion::Op op)
257 {
258     AutoReplayer replayer(this);
259     this->SkCanvas::onClipRegion(region, op);
260 }
261
262 void ReplayingCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix, const SkPaint* paint)
263 {
264     AutoReplayer replayer(this);
265     this->SkCanvas::onDrawPicture(picture, matrix, paint);
266 }
267
268 void ReplayingCanvas::didSetMatrix(const SkMatrix& matrix)
269 {
270     AutoReplayer replayer(this);
271     this->SkCanvas::didSetMatrix(matrix);
272 }
273
274 void ReplayingCanvas::didConcat(const SkMatrix& matrix)
275 {
276     AutoReplayer replayer(this);
277     this->SkCanvas::didConcat(matrix);
278 }
279
280 void ReplayingCanvas::willSave()
281 {
282     AutoReplayer replayer(this);
283     this->SkCanvas::willSave();
284 }
285
286 SkCanvas::SaveLayerStrategy ReplayingCanvas::willSaveLayer(const SkRect* bounds, const SkPaint* paint, SaveFlags flags)
287 {
288     AutoReplayer replayer(this);
289     // We're about to create a layer and we have not cleared the device yet.
290     // Let's clear now, so it has effect on all layers.
291     if (m_stepCount < m_fromStep)
292         this->SkCanvas::clear(SkColorSetARGB(255, 255, 255, 255)); // FIXME: fill with nine patch instead.
293
294     return this->SkCanvas::willSaveLayer(bounds, paint, flags);
295 }
296
297 void ReplayingCanvas::willRestore()
298 {
299     AutoReplayer replayer(this);
300     this->SkCanvas::willRestore();
301 }
302
303 } // namespace blink