2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "platform/graphics/ReplayingCanvas.h"
34 #include "third_party/skia/include/core/SkBitmapDevice.h"
40 explicit AutoReplayer(ReplayingCanvas*);
44 ReplayingCanvas* m_canvas;
47 AutoReplayer::AutoReplayer(ReplayingCanvas* replayingCanvas) : m_canvas(replayingCanvas)
49 replayingCanvas->m_depthCount++;
52 AutoReplayer::~AutoReplayer()
54 m_canvas->m_depthCount--;
55 if (m_canvas->m_depthCount)
57 m_canvas->m_stepCount++;
58 m_canvas->updateInRange();
61 ReplayingCanvas::ReplayingCanvas(SkBitmap bitmap, unsigned fromStep, unsigned toStep)
62 : InterceptingCanvas(bitmap), m_fromStep(fromStep), m_toStep(toStep), m_stepCount(0), m_abortDrawing(false)
66 void ReplayingCanvas::resetStepCount()
71 void ReplayingCanvas::updateInRange()
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.
81 bool ReplayingCanvas::abortDrawing()
83 return m_abortDrawing;
86 void ReplayingCanvas::clear(SkColor color)
88 AutoReplayer replayer(this);
89 this->SkCanvas::clear(color);
92 void ReplayingCanvas::drawPaint(const SkPaint& paint)
94 AutoReplayer replayer(this);
95 this->SkCanvas::drawPaint(paint);
98 void ReplayingCanvas::drawPoints(PointMode mode, size_t count, const SkPoint pts[], const SkPaint& paint)
100 AutoReplayer replayer(this);
101 this->SkCanvas::drawPoints(mode, count, pts, paint);
104 void ReplayingCanvas::drawRect(const SkRect& rect, const SkPaint& paint)
106 AutoReplayer replayer(this);
107 this->SkCanvas::drawRect(rect, paint);
110 void ReplayingCanvas::drawOval(const SkRect& rect, const SkPaint& paint)
112 AutoReplayer replayer(this);
113 this->SkCanvas::drawOval(rect, paint);
116 void ReplayingCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint)
118 AutoReplayer replayer(this);
119 this->SkCanvas::drawRRect(rrect, paint);
122 void ReplayingCanvas::drawPath(const SkPath& path, const SkPaint& paint)
124 AutoReplayer replayer(this);
125 this->SkCanvas::drawPath(path, paint);
128 void ReplayingCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top, const SkPaint* paint)
130 AutoReplayer replayer(this);
131 this->SkCanvas::drawBitmap(bitmap, left, top, paint);
134 void ReplayingCanvas::drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src, const SkRect& dst,
135 const SkPaint* paint, DrawBitmapRectFlags flags)
137 AutoReplayer replayer(this);
138 this->SkCanvas::drawBitmapRectToRect(bitmap, src, dst, paint, flags);
141 void ReplayingCanvas::drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& m, const SkPaint* paint)
143 AutoReplayer replayer(this);
144 this->SkCanvas::drawBitmapMatrix(bitmap, m, paint);
147 void ReplayingCanvas::drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, const SkRect& dst, const SkPaint* paint)
149 AutoReplayer replayer(this);
150 this->SkCanvas::drawBitmapNine(bitmap, center, dst, paint);
153 void ReplayingCanvas::drawSprite(const SkBitmap& bitmap, int left, int top, const SkPaint* paint)
155 AutoReplayer replayer(this);
156 this->SkCanvas::drawSprite(bitmap, left, top, paint);
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)
162 AutoReplayer replayer(this);
163 this->SkCanvas::drawVertices(vmode, vertexCount, vertices, texs, colors, xmode, indices, indexCount, paint);
166 void ReplayingCanvas::drawData(const void* data, size_t length)
168 AutoReplayer replayer(this);
169 this->SkCanvas::drawData(data, length);
172 void ReplayingCanvas::beginCommentGroup(const char* description)
174 AutoReplayer replayer(this);
175 this->SkCanvas::beginCommentGroup(description);
178 void ReplayingCanvas::addComment(const char* keyword, const char* value)
180 AutoReplayer replayer(this);
181 this->SkCanvas::addComment(keyword, value);
184 void ReplayingCanvas::endCommentGroup()
186 AutoReplayer replayer(this);
187 this->SkCanvas::endCommentGroup();
190 void ReplayingCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint)
192 AutoReplayer replayer(this);
193 this->SkCanvas::onDrawDRRect(outer, inner, paint);
196 void ReplayingCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y, const SkPaint& paint)
198 AutoReplayer replayer(this);
199 this->SkCanvas::onDrawText(text, byteLength, x, y, paint);
202 void ReplayingCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[], const SkPaint& paint)
204 AutoReplayer replayer(this);
205 this->SkCanvas::onDrawPosText(text, byteLength, pos, paint);
208 void ReplayingCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[], SkScalar constY, const SkPaint& paint)
210 AutoReplayer replayer(this);
211 this->SkCanvas::onDrawPosTextH(text, byteLength, xpos, constY, paint);
214 void ReplayingCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path, const SkMatrix* matrix, const SkPaint& paint)
216 AutoReplayer replayer(this);
217 this->SkCanvas::onDrawTextOnPath(text, byteLength, path, matrix, paint);
220 void ReplayingCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, const SkPaint& paint)
222 AutoReplayer replayer(this);
223 this->SkCanvas::onDrawTextBlob(blob, x, y, paint);
226 void ReplayingCanvas::onPushCull(const SkRect& cullRect)
228 AutoReplayer replayer(this);
229 this->SkCanvas::onPushCull(cullRect);
232 void ReplayingCanvas::onPopCull()
234 AutoReplayer replayer(this);
235 this->SkCanvas::onPopCull();
238 void ReplayingCanvas::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle)
240 AutoReplayer replayer(this);
241 this->SkCanvas::onClipRect(rect, op, edgeStyle);
244 void ReplayingCanvas::onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle)
246 AutoReplayer replayer(this);
247 this->SkCanvas::onClipRRect(rrect, op, edgeStyle);
250 void ReplayingCanvas::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle)
252 AutoReplayer replayer(this);
253 this->SkCanvas::onClipPath(path, op, edgeStyle);
256 void ReplayingCanvas::onClipRegion(const SkRegion& region, SkRegion::Op op)
258 AutoReplayer replayer(this);
259 this->SkCanvas::onClipRegion(region, op);
262 void ReplayingCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix, const SkPaint* paint)
264 AutoReplayer replayer(this);
265 this->SkCanvas::onDrawPicture(picture, matrix, paint);
268 void ReplayingCanvas::didSetMatrix(const SkMatrix& matrix)
270 AutoReplayer replayer(this);
271 this->SkCanvas::didSetMatrix(matrix);
274 void ReplayingCanvas::didConcat(const SkMatrix& matrix)
276 AutoReplayer replayer(this);
277 this->SkCanvas::didConcat(matrix);
280 void ReplayingCanvas::willSave()
282 AutoReplayer replayer(this);
283 this->SkCanvas::willSave();
286 SkCanvas::SaveLayerStrategy ReplayingCanvas::willSaveLayer(const SkRect* bounds, const SkPaint* paint, SaveFlags flags)
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.
294 return this->SkCanvas::willSaveLayer(bounds, paint, flags);
297 void ReplayingCanvas::willRestore()
299 AutoReplayer replayer(this);
300 this->SkCanvas::willRestore();