blob: 0da665b69d688d9f3bd2e7ea5529a4cc3ffef41e [file] [log] [blame]
junov@google.com4370aed2012-01-18 16:21:08 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#include "SkDeferredCanvas.h"
10
11#include "SkPaint.h"
12#include "SkShader.h"
13#include "SkColorFilter.h"
14#include "SkDrawFilter.h"
15
16namespace {
17
junov@chromium.orgc16ca922012-02-24 22:06:27 +000018bool isPaintOpaque(const SkPaint* paint,
19 const SkBitmap* bmpReplacesShader = NULL) {
junov@google.com4370aed2012-01-18 16:21:08 +000020 // TODO: SkXfermode should have a virtual isOpaque method, which would
21 // make it possible to test modes that do not have a Coeff representation.
junov@chromium.org87f982c2012-02-23 21:34:34 +000022
23 if (!paint) {
24 return bmpReplacesShader ? bmpReplacesShader->isOpaque() : true;
25 }
26
junov@google.com4370aed2012-01-18 16:21:08 +000027 SkXfermode::Coeff srcCoeff, dstCoeff;
junov@chromium.org87f982c2012-02-23 21:34:34 +000028 if (SkXfermode::AsCoeff(paint->getXfermode(), &srcCoeff, &dstCoeff)){
junov@google.com4370aed2012-01-18 16:21:08 +000029 switch (dstCoeff) {
30 case SkXfermode::kZero_Coeff:
31 return true;
32 case SkXfermode::kISA_Coeff:
junov@chromium.org87f982c2012-02-23 21:34:34 +000033 if (paint->getAlpha() != 255) {
junov@google.com4370aed2012-01-18 16:21:08 +000034 break;
35 }
36 if (bmpReplacesShader) {
37 if (!bmpReplacesShader->isOpaque()) {
38 break;
39 }
junov@chromium.org87f982c2012-02-23 21:34:34 +000040 } else if (paint->getShader() && !paint->getShader()->isOpaque()) {
junov@google.com4370aed2012-01-18 16:21:08 +000041 break;
42 }
junov@chromium.orgc16ca922012-02-24 22:06:27 +000043 if (paint->getColorFilter() &&
44 ((paint->getColorFilter()->getFlags() &
junov@google.com4370aed2012-01-18 16:21:08 +000045 SkColorFilter::kAlphaUnchanged_Flag) == 0)) {
46 break;
47 }
48 return true;
49 case SkXfermode::kSA_Coeff:
junov@chromium.org87f982c2012-02-23 21:34:34 +000050 if (paint->getAlpha() != 0) {
junov@google.com4370aed2012-01-18 16:21:08 +000051 break;
52 }
junov@chromium.orgc16ca922012-02-24 22:06:27 +000053 if (paint->getColorFilter() &&
54 ((paint->getColorFilter()->getFlags() &
junov@google.com4370aed2012-01-18 16:21:08 +000055 SkColorFilter::kAlphaUnchanged_Flag) == 0)) {
56 break;
57 }
58 return true;
59 case SkXfermode::kSC_Coeff:
junov@chromium.org87f982c2012-02-23 21:34:34 +000060 if (paint->getColor() != 0) { // all components must be 0
junov@google.com4370aed2012-01-18 16:21:08 +000061 break;
62 }
junov@chromium.org87f982c2012-02-23 21:34:34 +000063 if (bmpReplacesShader || paint->getShader()) {
junov@google.com4370aed2012-01-18 16:21:08 +000064 break;
65 }
junov@chromium.orgc16ca922012-02-24 22:06:27 +000066 if (paint->getColorFilter() && (
67 (paint->getColorFilter()->getFlags() &
junov@google.com4370aed2012-01-18 16:21:08 +000068 SkColorFilter::kAlphaUnchanged_Flag) == 0)) {
69 break;
70 }
71 return true;
72 default:
73 break;
74 }
75 }
76 return false;
77}
78
79} // unnamed namespace
80
junov@chromium.orgc16ca922012-02-24 22:06:27 +000081SkDeferredCanvas::SkDeferredCanvas() {
junov@google.com4370aed2012-01-18 16:21:08 +000082 init();
83}
84
junov@chromium.orgc16ca922012-02-24 22:06:27 +000085SkDeferredCanvas::SkDeferredCanvas(SkDevice* device) {
junov@google.com4370aed2012-01-18 16:21:08 +000086 init();
87 setDevice(device);
88}
89
90SkDeferredCanvas::SkDeferredCanvas(SkDevice* device,
junov@chromium.orgc16ca922012-02-24 22:06:27 +000091 DeviceContext* deviceContext) {
junov@google.com4370aed2012-01-18 16:21:08 +000092 init();
93 setDevice(device);
94 setDeviceContext(deviceContext);
95}
96
junov@chromium.orgc16ca922012-02-24 22:06:27 +000097void SkDeferredCanvas::init() {
junov@chromium.orgfeba6892012-02-28 15:02:06 +000098 fDeferredDrawing = true;
99 fDrawingCanvas = NULL;
junov@google.com4370aed2012-01-18 16:21:08 +0000100}
101
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000102void SkDeferredCanvas::validate() const {
junov@google.com4370aed2012-01-18 16:21:08 +0000103 SkASSERT(getDevice());
junov@google.com4370aed2012-01-18 16:21:08 +0000104}
105
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000106void SkDeferredCanvas::flushIfNeeded(const SkBitmap& bitmap) {
junov@google.com4370aed2012-01-18 16:21:08 +0000107 validate();
108 if (fDeferredDrawing) {
109 getDeferredDevice()->flushIfNeeded(bitmap);
110 }
111}
112
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000113SkDeferredCanvas::DeferredDevice* SkDeferredCanvas::getDeferredDevice() const {
junov@google.com4370aed2012-01-18 16:21:08 +0000114 return static_cast<SkDeferredCanvas::DeferredDevice*>(getDevice());
115}
116
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000117void SkDeferredCanvas::setDeferredDrawing(bool val) {
junov@google.com4370aed2012-01-18 16:21:08 +0000118 validate(); // Must set device before calling this method
junov@chromium.orgfeba6892012-02-28 15:02:06 +0000119 SkASSERT(fDrawingCanvas->getSaveCount() == 1);
junov@google.com4370aed2012-01-18 16:21:08 +0000120 if (val != fDeferredDrawing) {
121 if (fDeferredDrawing) {
junov@chromium.orgfeba6892012-02-28 15:02:06 +0000122 // Going live.
junov@google.com4370aed2012-01-18 16:21:08 +0000123 getDeferredDevice()->flushPending();
124 }
125 fDeferredDrawing = val;
junov@chromium.orgfeba6892012-02-28 15:02:06 +0000126 fDrawingCanvas = val ?
127 getDeferredDevice()->recordingCanvas() :
128 getDeferredDevice()->immediateCanvas();
129 }
junov@google.com4370aed2012-01-18 16:21:08 +0000130 }
131}
132
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000133SkDeferredCanvas::~SkDeferredCanvas() {
junov@google.com4370aed2012-01-18 16:21:08 +0000134}
135
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000136SkDevice* SkDeferredCanvas::setDevice(SkDevice* device) {
junov@google.com4370aed2012-01-18 16:21:08 +0000137 INHERITED::setDevice(SkNEW_ARGS(DeferredDevice, (device)))->unref();
junov@chromium.orgfeba6892012-02-28 15:02:06 +0000138 fDrawingCanvas = fDeferredDrawing ?
139 getDeferredDevice()->recordingCanvas() :
140 getDeferredDevice()->immediateCanvas();
junov@google.com4370aed2012-01-18 16:21:08 +0000141 return device;
142}
143
144SkDeferredCanvas::DeviceContext* SkDeferredCanvas::setDeviceContext(
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000145 DeviceContext* deviceContext) {
146
junov@google.com4370aed2012-01-18 16:21:08 +0000147 DeferredDevice* deferredDevice = getDeferredDevice();
148 SkASSERT(deferredDevice);
149 if (deferredDevice) {
150 deferredDevice->setDeviceContext(deviceContext);
151 }
152 return deviceContext;
153}
154
155bool SkDeferredCanvas::isFullFrame(const SkRect* rect,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000156 const SkPaint* paint) const {
junov@chromium.orgfeba6892012-02-28 15:02:06 +0000157 SkCanvas* canvas = fDrawingCanvas;
junov@google.com4370aed2012-01-18 16:21:08 +0000158 SkISize canvasSize = getDeviceSize();
159 if (rect) {
160 if (!canvas->getTotalMatrix().rectStaysRect()) {
161 return false; // conservative
162 }
163
164 SkRect transformedRect;
165 canvas->getTotalMatrix().mapRect(&transformedRect, *rect);
166
167 if (paint) {
168 SkPaint::Style paintStyle = paint->getStyle();
169 if (!(paintStyle == SkPaint::kFill_Style ||
170 paintStyle == SkPaint::kStrokeAndFill_Style)) {
171 return false;
172 }
173 if (paint->getMaskFilter() || paint->getLooper()
174 || paint->getPathEffect() || paint->getImageFilter()) {
175 return false; // conservative
176 }
177 }
178
179 // The following test holds with AA enabled, and is conservative
180 // by a 0.5 pixel margin with AA disabled
junov@chromium.orgb1e218e2012-02-13 22:27:58 +0000181 if (transformedRect.fLeft > SkIntToScalar(0) ||
182 transformedRect.fTop > SkIntToScalar(0) ||
183 transformedRect.fRight < SkIntToScalar(canvasSize.fWidth) ||
184 transformedRect.fBottom < SkIntToScalar(canvasSize.fHeight)) {
junov@google.com4370aed2012-01-18 16:21:08 +0000185 return false;
186 }
187 }
188
189 switch (canvas->getClipType()) {
190 case SkCanvas::kRect_ClipType :
191 {
192 SkIRect bounds;
193 canvas->getClipDeviceBounds(&bounds);
194 if (bounds.fLeft > 0 || bounds.fTop > 0 ||
195 bounds.fRight < canvasSize.fWidth ||
196 bounds.fBottom < canvasSize.fHeight)
197 return false;
198 }
199 break;
200 case SkCanvas::kComplex_ClipType :
201 return false; // conservative
202 case SkCanvas::kEmpty_ClipType:
203 default:
204 break;
205 };
206
207 return true;
208}
209
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000210int SkDeferredCanvas::save(SaveFlags flags) {
junov@chromium.orgfeba6892012-02-28 15:02:06 +0000211 fDrawingCanvas->save(flags);
junov@chromium.orga907ac32012-02-24 21:54:07 +0000212 return this->INHERITED::save(flags);
junov@google.com4370aed2012-01-18 16:21:08 +0000213}
214
215int SkDeferredCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000216 SaveFlags flags) {
junov@chromium.orgfeba6892012-02-28 15:02:06 +0000217 fDrawingCanvas->saveLayer(bounds, paint, flags);
junov@chromium.orga907ac32012-02-24 21:54:07 +0000218 int count = this->INHERITED::save(flags);
219 this->clipRectBounds(bounds, flags, NULL);
220 return count;
junov@google.com4370aed2012-01-18 16:21:08 +0000221}
222
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000223void SkDeferredCanvas::restore() {
junov@chromium.orgfeba6892012-02-28 15:02:06 +0000224 fDrawingCanvas->restore();
junov@chromium.orga907ac32012-02-24 21:54:07 +0000225 this->INHERITED::restore();
junov@google.com4370aed2012-01-18 16:21:08 +0000226}
227
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000228bool SkDeferredCanvas::isDrawingToLayer() const {
junov@chromium.orgfeba6892012-02-28 15:02:06 +0000229 return fDrawingCanvas->isDrawingToLayer();
junov@google.com4370aed2012-01-18 16:21:08 +0000230}
231
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000232bool SkDeferredCanvas::translate(SkScalar dx, SkScalar dy) {
junov@chromium.orgfeba6892012-02-28 15:02:06 +0000233 fDrawingCanvas->translate(dx, dy);
junov@chromium.orga907ac32012-02-24 21:54:07 +0000234 return this->INHERITED::translate(dx, dy);
junov@google.com4370aed2012-01-18 16:21:08 +0000235}
236
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000237bool SkDeferredCanvas::scale(SkScalar sx, SkScalar sy) {
junov@chromium.orgfeba6892012-02-28 15:02:06 +0000238 fDrawingCanvas->scale(sx, sy);
junov@chromium.orga907ac32012-02-24 21:54:07 +0000239 return this->INHERITED::scale(sx, sy);
junov@google.com4370aed2012-01-18 16:21:08 +0000240}
241
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000242bool SkDeferredCanvas::rotate(SkScalar degrees) {
junov@chromium.orgfeba6892012-02-28 15:02:06 +0000243 fDrawingCanvas->rotate(degrees);
junov@chromium.orga907ac32012-02-24 21:54:07 +0000244 return this->INHERITED::rotate(degrees);
junov@google.com4370aed2012-01-18 16:21:08 +0000245}
246
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000247bool SkDeferredCanvas::skew(SkScalar sx, SkScalar sy) {
junov@chromium.orgfeba6892012-02-28 15:02:06 +0000248 fDrawingCanvas->skew(sx, sy);
junov@chromium.orga907ac32012-02-24 21:54:07 +0000249 return this->INHERITED::skew(sx, sy);
junov@google.com4370aed2012-01-18 16:21:08 +0000250}
251
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000252bool SkDeferredCanvas::concat(const SkMatrix& matrix) {
junov@chromium.orgfeba6892012-02-28 15:02:06 +0000253 fDrawingCanvas->concat(matrix);
junov@chromium.orga907ac32012-02-24 21:54:07 +0000254 return this->INHERITED::concat(matrix);
junov@google.com4370aed2012-01-18 16:21:08 +0000255}
256
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000257void SkDeferredCanvas::setMatrix(const SkMatrix& matrix) {
junov@chromium.orgfeba6892012-02-28 15:02:06 +0000258 fDrawingCanvas->setMatrix(matrix);
junov@chromium.orga907ac32012-02-24 21:54:07 +0000259 this->INHERITED::setMatrix(matrix);
junov@google.com4370aed2012-01-18 16:21:08 +0000260}
261
262bool SkDeferredCanvas::clipRect(const SkRect& rect,
263 SkRegion::Op op,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000264 bool doAntiAlias) {
junov@chromium.orgfeba6892012-02-28 15:02:06 +0000265 fDrawingCanvas->clipRect(rect, op, doAntiAlias);
junov@chromium.orga907ac32012-02-24 21:54:07 +0000266 return this->INHERITED::clipRect(rect, op, doAntiAlias);
junov@google.com4370aed2012-01-18 16:21:08 +0000267}
268
269bool SkDeferredCanvas::clipPath(const SkPath& path,
270 SkRegion::Op op,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000271 bool doAntiAlias) {
junov@chromium.orgfeba6892012-02-28 15:02:06 +0000272 fDrawingCanvas->clipPath(path, op, doAntiAlias);
junov@chromium.orga907ac32012-02-24 21:54:07 +0000273 return this->INHERITED::clipPath(path, op, doAntiAlias);
junov@google.com4370aed2012-01-18 16:21:08 +0000274}
275
276bool SkDeferredCanvas::clipRegion(const SkRegion& deviceRgn,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000277 SkRegion::Op op) {
junov@chromium.orgfeba6892012-02-28 15:02:06 +0000278 fDrawingCanvas->clipRegion(deviceRgn, op);
junov@chromium.orga907ac32012-02-24 21:54:07 +0000279 return this->INHERITED::clipRegion(deviceRgn, op);
junov@google.com4370aed2012-01-18 16:21:08 +0000280}
281
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000282void SkDeferredCanvas::clear(SkColor color) {
junov@google.com4370aed2012-01-18 16:21:08 +0000283 // purge pending commands
284 if (fDeferredDrawing) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000285 getDeferredDevice()->contentsCleared();
junov@google.com4370aed2012-01-18 16:21:08 +0000286 }
287
junov@chromium.orgfeba6892012-02-28 15:02:06 +0000288 fDrawingCanvas->clear(color);
junov@google.com4370aed2012-01-18 16:21:08 +0000289}
290
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000291void SkDeferredCanvas::drawPaint(const SkPaint& paint) {
292 if (fDeferredDrawing && isFullFrame(NULL, &paint) &&
293 isPaintOpaque(&paint)) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000294 getDeferredDevice()->contentsCleared();
junov@google.com4370aed2012-01-18 16:21:08 +0000295 }
296
junov@chromium.orgfeba6892012-02-28 15:02:06 +0000297 fDrawingCanvas->drawPaint(paint);
junov@google.com4370aed2012-01-18 16:21:08 +0000298}
299
300void SkDeferredCanvas::drawPoints(PointMode mode, size_t count,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000301 const SkPoint pts[], const SkPaint& paint) {
junov@chromium.orgfeba6892012-02-28 15:02:06 +0000302 fDrawingCanvas->drawPoints(mode, count, pts, paint);
junov@google.com4370aed2012-01-18 16:21:08 +0000303}
304
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000305void SkDeferredCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
306 if (fDeferredDrawing && isFullFrame(&rect, &paint) &&
307 isPaintOpaque(&paint)) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000308 getDeferredDevice()->contentsCleared();
junov@google.com4370aed2012-01-18 16:21:08 +0000309 }
310
junov@chromium.orgfeba6892012-02-28 15:02:06 +0000311 fDrawingCanvas->drawRect(rect, paint);
junov@google.com4370aed2012-01-18 16:21:08 +0000312}
313
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000314void SkDeferredCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
junov@chromium.orgfeba6892012-02-28 15:02:06 +0000315 fDrawingCanvas->drawPath(path, paint);
junov@google.com4370aed2012-01-18 16:21:08 +0000316}
317
318void SkDeferredCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar left,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000319 SkScalar top, const SkPaint* paint) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000320 SkRect bitmapRect = SkRect::MakeXYWH(left, top,
321 SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
junov@google.com4370aed2012-01-18 16:21:08 +0000322 if (fDeferredDrawing &&
323 isFullFrame(&bitmapRect, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000324 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000325 getDeferredDevice()->contentsCleared();
junov@google.com4370aed2012-01-18 16:21:08 +0000326 }
327
junov@chromium.orgfeba6892012-02-28 15:02:06 +0000328 fDrawingCanvas->drawBitmap(bitmap, left, top, paint);
junov@google.com4370aed2012-01-18 16:21:08 +0000329 flushIfNeeded(bitmap);
330}
331
332void SkDeferredCanvas::drawBitmapRect(const SkBitmap& bitmap,
333 const SkIRect* src,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000334 const SkRect& dst,
335 const SkPaint* paint) {
junov@google.com4370aed2012-01-18 16:21:08 +0000336 if (fDeferredDrawing &&
337 isFullFrame(&dst, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000338 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000339 getDeferredDevice()->contentsCleared();
junov@google.com4370aed2012-01-18 16:21:08 +0000340 }
341
junov@chromium.orgfeba6892012-02-28 15:02:06 +0000342 fDrawingCanvas->drawBitmapRect(bitmap, src,
junov@google.com4370aed2012-01-18 16:21:08 +0000343 dst, paint);
344 flushIfNeeded(bitmap);
345}
346
347
348void SkDeferredCanvas::drawBitmapMatrix(const SkBitmap& bitmap,
349 const SkMatrix& m,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000350 const SkPaint* paint) {
junov@google.com4370aed2012-01-18 16:21:08 +0000351 // TODO: reset recording canvas if paint+bitmap is opaque and clip rect
352 // covers canvas entirely and transformed bitmap covers canvas entirely
junov@chromium.orgfeba6892012-02-28 15:02:06 +0000353 fDrawingCanvas->drawBitmapMatrix(bitmap, m, paint);
junov@google.com4370aed2012-01-18 16:21:08 +0000354 flushIfNeeded(bitmap);
355}
356
357void SkDeferredCanvas::drawBitmapNine(const SkBitmap& bitmap,
358 const SkIRect& center, const SkRect& dst,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000359 const SkPaint* paint) {
junov@google.com4370aed2012-01-18 16:21:08 +0000360 // TODO: reset recording canvas if paint+bitmap is opaque and clip rect
361 // covers canvas entirely and dst covers canvas entirely
junov@chromium.orgfeba6892012-02-28 15:02:06 +0000362 fDrawingCanvas->drawBitmapNine(bitmap, center,
junov@google.com4370aed2012-01-18 16:21:08 +0000363 dst, paint);
364 flushIfNeeded(bitmap);
365}
366
367void SkDeferredCanvas::drawSprite(const SkBitmap& bitmap, int left, int top,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000368 const SkPaint* paint) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000369 SkRect bitmapRect = SkRect::MakeXYWH(
370 SkIntToScalar(left),
371 SkIntToScalar(top),
372 SkIntToScalar(bitmap.width()),
373 SkIntToScalar(bitmap.height()));
junov@google.com4370aed2012-01-18 16:21:08 +0000374 if (fDeferredDrawing &&
375 isFullFrame(&bitmapRect, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000376 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000377 getDeferredDevice()->contentsCleared();
junov@google.com4370aed2012-01-18 16:21:08 +0000378 }
379
junov@chromium.orgfeba6892012-02-28 15:02:06 +0000380 fDrawingCanvas->drawSprite(bitmap, left, top,
junov@google.com4370aed2012-01-18 16:21:08 +0000381 paint);
382 flushIfNeeded(bitmap);
383}
384
385void SkDeferredCanvas::drawText(const void* text, size_t byteLength,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000386 SkScalar x, SkScalar y, const SkPaint& paint) {
junov@chromium.orgfeba6892012-02-28 15:02:06 +0000387 fDrawingCanvas->drawText(text, byteLength, x, y, paint);
junov@google.com4370aed2012-01-18 16:21:08 +0000388}
389
390void SkDeferredCanvas::drawPosText(const void* text, size_t byteLength,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000391 const SkPoint pos[], const SkPaint& paint) {
junov@chromium.orgfeba6892012-02-28 15:02:06 +0000392 fDrawingCanvas->drawPosText(text, byteLength, pos, paint);
junov@google.com4370aed2012-01-18 16:21:08 +0000393}
394
395void SkDeferredCanvas::drawPosTextH(const void* text, size_t byteLength,
396 const SkScalar xpos[], SkScalar constY,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000397 const SkPaint& paint) {
junov@chromium.orgfeba6892012-02-28 15:02:06 +0000398 fDrawingCanvas->drawPosTextH(text, byteLength, xpos, constY, paint);
junov@google.com4370aed2012-01-18 16:21:08 +0000399}
400
401void SkDeferredCanvas::drawTextOnPath(const void* text, size_t byteLength,
402 const SkPath& path,
403 const SkMatrix* matrix,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000404 const SkPaint& paint) {
junov@chromium.orgfeba6892012-02-28 15:02:06 +0000405 fDrawingCanvas->drawTextOnPath(text, byteLength,
junov@google.com4370aed2012-01-18 16:21:08 +0000406 path, matrix,
407 paint);
408}
409
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000410void SkDeferredCanvas::drawPicture(SkPicture& picture) {
junov@chromium.orgfeba6892012-02-28 15:02:06 +0000411 fDrawingCanvas->drawPicture(picture);
junov@google.com4370aed2012-01-18 16:21:08 +0000412}
413
414void SkDeferredCanvas::drawVertices(VertexMode vmode, int vertexCount,
415 const SkPoint vertices[],
416 const SkPoint texs[],
417 const SkColor colors[], SkXfermode* xmode,
418 const uint16_t indices[], int indexCount,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000419 const SkPaint& paint) {
junov@chromium.orgfeba6892012-02-28 15:02:06 +0000420 fDrawingCanvas->drawVertices(vmode, vertexCount,
junov@google.com4370aed2012-01-18 16:21:08 +0000421 vertices, texs,
422 colors, xmode,
423 indices, indexCount,
424 paint);
425}
426
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000427SkBounder* SkDeferredCanvas::setBounder(SkBounder* bounder) {
junov@chromium.orgfeba6892012-02-28 15:02:06 +0000428 fDrawingCanvas->setBounder(bounder);
junov@chromium.orga907ac32012-02-24 21:54:07 +0000429 return INHERITED::setBounder(bounder);
junov@google.com4370aed2012-01-18 16:21:08 +0000430}
431
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000432SkDrawFilter* SkDeferredCanvas::setDrawFilter(SkDrawFilter* filter) {
junov@chromium.orgfeba6892012-02-28 15:02:06 +0000433 fDrawingCanvas->setDrawFilter(filter);
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000434 return INHERITED::setDrawFilter(filter);
junov@google.com4370aed2012-01-18 16:21:08 +0000435}
436
437SkCanvas* SkDeferredCanvas::canvasForDrawIter() {
junov@chromium.orgfeba6892012-02-28 15:02:06 +0000438 return fDrawingCanvas;
junov@google.com4370aed2012-01-18 16:21:08 +0000439}
440
441// SkDeferredCanvas::DeferredDevice
442//------------------------------------
443
444SkDeferredCanvas::DeferredDevice::DeferredDevice(
445 SkDevice* immediateDevice, DeviceContext* deviceContext) :
446 SkDevice(SkBitmap::kNo_Config, immediateDevice->width(),
447 immediateDevice->height(), immediateDevice->isOpaque())
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000448 , fFreshFrame(true) {
449
junov@google.com4370aed2012-01-18 16:21:08 +0000450 fDeviceContext = deviceContext;
451 SkSafeRef(fDeviceContext);
452 fImmediateDevice = immediateDevice; // ref counted via fImmediateCanvas
453 fImmediateCanvas = SkNEW_ARGS(SkCanvas, (fImmediateDevice));
junov@google.com4370aed2012-01-18 16:21:08 +0000454 fRecordingCanvas = fPicture.beginRecording(fImmediateDevice->width(),
junov@chromium.orga907ac32012-02-24 21:54:07 +0000455 fImmediateDevice->height(), 0);
junov@google.com4370aed2012-01-18 16:21:08 +0000456}
457
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000458SkDeferredCanvas::DeferredDevice::~DeferredDevice() {
junov@google.com4370aed2012-01-18 16:21:08 +0000459 SkSafeUnref(fImmediateCanvas);
460 SkSafeUnref(fDeviceContext);
461}
462
463void SkDeferredCanvas::DeferredDevice::setDeviceContext(
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000464 DeviceContext* deviceContext) {
junov@google.com4370aed2012-01-18 16:21:08 +0000465 SkRefCnt_SafeAssign(fDeviceContext, deviceContext);
466}
467
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000468void SkDeferredCanvas::DeferredDevice::contentsCleared() {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000469 if (!fRecordingCanvas->isDrawingToLayer()) {
470 fFreshFrame = true;
junov@google.com4370aed2012-01-18 16:21:08 +0000471
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000472 // TODO: find a way to transfer the state stack and layers
473 // to the new recording canvas. For now, purging only works
474 // with an empty stack.
475 if (fRecordingCanvas->getSaveCount() == 0) {
junov@google.com4370aed2012-01-18 16:21:08 +0000476
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000477 // Save state that is trashed by the purge
478 SkDrawFilter* drawFilter = fRecordingCanvas->getDrawFilter();
479 SkSafeRef(drawFilter); // So that it survives the purge
480 SkMatrix matrix = fRecordingCanvas->getTotalMatrix();
481 SkRegion clipRegion = fRecordingCanvas->getTotalClip();
junov@google.com4370aed2012-01-18 16:21:08 +0000482
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000483 // beginRecording creates a new recording canvas and discards the
484 // old one, hence purging deferred draw ops.
485 fRecordingCanvas = fPicture.beginRecording(
486 fImmediateDevice->width(),
junov@chromium.orga907ac32012-02-24 21:54:07 +0000487 fImmediateDevice->height(), 0);
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000488
489 // Restore pre-purge state
490 if (!clipRegion.isEmpty()) {
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000491 fRecordingCanvas->clipRegion(clipRegion,
492 SkRegion::kReplace_Op);
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000493 }
494 if (!matrix.isIdentity()) {
495 fRecordingCanvas->setMatrix(matrix);
496 }
497 if (drawFilter) {
498 fRecordingCanvas->setDrawFilter(drawFilter)->unref();
499 }
500 }
junov@google.com4370aed2012-01-18 16:21:08 +0000501 }
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000502}
503
504bool SkDeferredCanvas::DeferredDevice::isFreshFrame() {
505 bool ret = fFreshFrame;
506 fFreshFrame = false;
507 return ret;
junov@google.com4370aed2012-01-18 16:21:08 +0000508}
509
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000510void SkDeferredCanvas::DeferredDevice::flushPending() {
junov@google.com4370aed2012-01-18 16:21:08 +0000511 if (fDeviceContext) {
512 fDeviceContext->prepareForDraw();
513 }
514 fPicture.draw(fImmediateCanvas);
515 fRecordingCanvas = fPicture.beginRecording(fImmediateDevice->width(),
junov@chromium.orga907ac32012-02-24 21:54:07 +0000516 fImmediateDevice->height(), 0);
junov@google.com4370aed2012-01-18 16:21:08 +0000517}
518
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000519void SkDeferredCanvas::DeferredDevice::flush() {
junov@google.com4370aed2012-01-18 16:21:08 +0000520 flushPending();
junov@chromium.orgbf6c1e42012-01-30 14:53:22 +0000521 fImmediateCanvas->flush();
junov@google.com4370aed2012-01-18 16:21:08 +0000522}
523
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000524void SkDeferredCanvas::DeferredDevice::flushIfNeeded(const SkBitmap& bitmap) {
junov@google.com4370aed2012-01-18 16:21:08 +0000525 if (bitmap.isImmutable()) {
526 return; // safe to deffer without registering a dependency
527 }
528
529 // For now, drawing a writable bitmap triggers a flush
530 // TODO: implement read-only semantics and auto buffer duplication on write
531 // in SkBitmap/SkPixelRef, which will make deferral possible in this case.
532 flushPending();
533}
534
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000535uint32_t SkDeferredCanvas::DeferredDevice::getDeviceCapabilities() {
junov@google.com4370aed2012-01-18 16:21:08 +0000536 return fImmediateDevice->getDeviceCapabilities();
537}
538
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000539int SkDeferredCanvas::DeferredDevice::width() const {
junov@google.com4370aed2012-01-18 16:21:08 +0000540 return fImmediateDevice->width();
541}
542
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000543int SkDeferredCanvas::DeferredDevice::height() const {
junov@google.com4370aed2012-01-18 16:21:08 +0000544 return fImmediateDevice->height();
545}
546
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000547SkGpuRenderTarget* SkDeferredCanvas::DeferredDevice::accessRenderTarget() {
junov@google.com4370aed2012-01-18 16:21:08 +0000548 flushPending();
549 return fImmediateDevice->accessRenderTarget();
550}
551
552void SkDeferredCanvas::DeferredDevice::writePixels(const SkBitmap& bitmap,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000553 int x, int y, SkCanvas::Config8888 config8888) {
554
junov@google.com4370aed2012-01-18 16:21:08 +0000555 if (x <= 0 && y <= 0 && (x + bitmap.width()) >= width() &&
556 (y + bitmap.height()) >= height()) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000557 contentsCleared();
junov@google.com4370aed2012-01-18 16:21:08 +0000558 }
559
560 if (SkBitmap::kARGB_8888_Config == bitmap.config() &&
561 SkCanvas::kNative_Premul_Config8888 != config8888 &&
562 kPMColorAlias != config8888) {
563 //Special case config: no deferral
564 flushPending();
565 fImmediateDevice->writePixels(bitmap, x, y, config8888);
566 }
567
568 SkPaint paint;
569 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
570 fRecordingCanvas->drawSprite(bitmap, x, y, &paint);
571 flushIfNeeded(bitmap);
572}
573
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000574const SkBitmap& SkDeferredCanvas::DeferredDevice::onAccessBitmap(SkBitmap*) {
junov@google.com4370aed2012-01-18 16:21:08 +0000575 flushPending();
junov@chromium.org1f9767c2012-02-07 16:27:57 +0000576 return fImmediateDevice->accessBitmap(false);
junov@google.com4370aed2012-01-18 16:21:08 +0000577}
578
579SkDevice* SkDeferredCanvas::DeferredDevice::onCreateCompatibleDevice(
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000580 SkBitmap::Config config, int width, int height, bool isOpaque,
581 Usage usage) {
582
junov@google.com4370aed2012-01-18 16:21:08 +0000583 // Save layer usage not supported, and not required by SkDeferredCanvas.
584 SkASSERT(usage != kSaveLayer_Usage);
585 // Create a compatible non-deferred device.
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000586 SkDevice* compatibleDevice =
587 fImmediateDevice->createCompatibleDevice(config, width, height,
588 isOpaque);
junov@google.com4370aed2012-01-18 16:21:08 +0000589 return SkNEW_ARGS(DeferredDevice, (compatibleDevice, fDeviceContext));
590}
591
592bool SkDeferredCanvas::DeferredDevice::onReadPixels(
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000593 const SkBitmap& bitmap, int x, int y, SkCanvas::Config8888 config8888) {
junov@google.com4370aed2012-01-18 16:21:08 +0000594 flushPending();
595 return fImmediateCanvas->readPixels(const_cast<SkBitmap*>(&bitmap),
596 x, y, config8888);
597}