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
36namespace blink {
37
38class AutoReplayer {
39public:
40    explicit AutoReplayer(ReplayingCanvas*);
41    ~AutoReplayer();
42
43private:
44    ReplayingCanvas* m_canvas;
45};
46
47AutoReplayer::AutoReplayer(ReplayingCanvas* replayingCanvas) : m_canvas(replayingCanvas)
48{
49    replayingCanvas->m_depthCount++;
50}
51
52AutoReplayer::~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
61ReplayingCanvas::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
66void ReplayingCanvas::resetStepCount()
67{
68    m_stepCount = 0;
69}
70
71void 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
81bool ReplayingCanvas::abortDrawing()
82{
83    return m_abortDrawing;
84}
85
86void ReplayingCanvas::clear(SkColor color)
87{
88    AutoReplayer replayer(this);
89    this->SkCanvas::clear(color);
90}
91
92void ReplayingCanvas::drawPaint(const SkPaint& paint)
93{
94    AutoReplayer replayer(this);
95    this->SkCanvas::drawPaint(paint);
96}
97
98void 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
104void ReplayingCanvas::drawRect(const SkRect& rect, const SkPaint& paint)
105{
106    AutoReplayer replayer(this);
107    this->SkCanvas::drawRect(rect, paint);
108}
109
110void ReplayingCanvas::drawOval(const SkRect& rect, const SkPaint& paint)
111{
112    AutoReplayer replayer(this);
113    this->SkCanvas::drawOval(rect, paint);
114}
115
116void ReplayingCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint)
117{
118    AutoReplayer replayer(this);
119    this->SkCanvas::drawRRect(rrect, paint);
120}
121
122void ReplayingCanvas::drawPath(const SkPath& path, const SkPaint& paint)
123{
124    AutoReplayer replayer(this);
125    this->SkCanvas::drawPath(path, paint);
126}
127
128void 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
134void 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
141void 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
147void 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
153void 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
159void 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
166void ReplayingCanvas::drawData(const void* data, size_t length)
167{
168    AutoReplayer replayer(this);
169    this->SkCanvas::drawData(data, length);
170}
171
172void ReplayingCanvas::beginCommentGroup(const char* description)
173{
174    AutoReplayer replayer(this);
175    this->SkCanvas::beginCommentGroup(description);
176}
177
178void ReplayingCanvas::addComment(const char* keyword, const char* value)
179{
180    AutoReplayer replayer(this);
181    this->SkCanvas::addComment(keyword, value);
182}
183
184void ReplayingCanvas::endCommentGroup()
185{
186    AutoReplayer replayer(this);
187    this->SkCanvas::endCommentGroup();
188}
189
190void 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
196void 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
202void 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
208void 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
214void 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
220void ReplayingCanvas::onPushCull(const SkRect& cullRect)
221{
222    AutoReplayer replayer(this);
223    this->SkCanvas::onPushCull(cullRect);
224}
225
226void ReplayingCanvas::onPopCull()
227{
228    AutoReplayer replayer(this);
229    this->SkCanvas::onPopCull();
230}
231
232void ReplayingCanvas::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle)
233{
234    AutoReplayer replayer(this);
235    this->SkCanvas::onClipRect(rect, op, edgeStyle);
236}
237
238void ReplayingCanvas::onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle)
239{
240    AutoReplayer replayer(this);
241    this->SkCanvas::onClipRRect(rrect, op, edgeStyle);
242}
243
244void ReplayingCanvas::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle)
245{
246    AutoReplayer replayer(this);
247    this->SkCanvas::onClipPath(path, op, edgeStyle);
248}
249
250void ReplayingCanvas::onClipRegion(const SkRegion& region, SkRegion::Op op)
251{
252    AutoReplayer replayer(this);
253    this->SkCanvas::onClipRegion(region, op);
254}
255
256void ReplayingCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix, const SkPaint* paint)
257{
258    AutoReplayer replayer(this);
259    this->SkCanvas::onDrawPicture(picture, matrix, paint);
260}
261
262void ReplayingCanvas::didSetMatrix(const SkMatrix& matrix)
263{
264    AutoReplayer replayer(this);
265    this->SkCanvas::didSetMatrix(matrix);
266}
267
268void ReplayingCanvas::didConcat(const SkMatrix& matrix)
269{
270    AutoReplayer replayer(this);
271    this->SkCanvas::didConcat(matrix);
272}
273
274void ReplayingCanvas::willSave()
275{
276    AutoReplayer replayer(this);
277    this->SkCanvas::willSave();
278}
279
280SkCanvas::SaveLayerStrategy ReplayingCanvas::willSaveLayer(const SkRect* bounds, const SkPaint* paint, SaveFlags flags)
281{
282    AutoReplayer replayer(this);
283    // We're about to create a layer and we have not cleared the device yet.
284    // Let's clear now, so it has effect on all layers.
285    if (m_stepCount < m_fromStep)
286        this->SkCanvas::clear(SkColorSetARGB(255, 255, 255, 255)); // FIXME: fill with nine patch instead.
287
288    return this->SkCanvas::willSaveLayer(bounds, paint, flags);
289}
290
291void ReplayingCanvas::willRestore()
292{
293    AutoReplayer replayer(this);
294    this->SkCanvas::willRestore();
295}
296
297} // namespace blink
298