blob: ce516146544f6260305df71346204de6593cc0bf [file] [log] [blame]
reed@google.comfe7b1ed2012-11-29 21:00:39 +00001/*
2 * Copyright 2012 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
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00008#include "SkBitmapDevice.h"
reed@google.comfe7b1ed2012-11-29 21:00:39 +00009#include "SkCanvas.h"
10#include "SkData.h"
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000011#include "SkPictureUtils.h"
reed@google.comfe7b1ed2012-11-29 21:00:39 +000012#include "SkPixelRef.h"
junov@chromium.org50a5cfb2013-02-01 20:39:48 +000013#include "SkRRect.h"
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000014#include "SkShader.h"
reed@google.comfe7b1ed2012-11-29 21:00:39 +000015
16class PixelRefSet {
17public:
18 PixelRefSet(SkTDArray<SkPixelRef*>* array) : fArray(array) {}
19
20 // This does a linear search on existing pixelrefs, so if this list gets big
21 // we should use a more complex sorted/hashy thing.
22 //
23 void add(SkPixelRef* pr) {
24 uint32_t genID = pr->getGenerationID();
25 if (fGenID.find(genID) < 0) {
26 *fArray->append() = pr;
27 *fGenID.append() = genID;
28// SkDebugf("--- adding [%d] %x %d\n", fArray->count() - 1, pr, genID);
29 } else {
30// SkDebugf("--- already have %x %d\n", pr, genID);
31 }
32 }
33
34private:
35 SkTDArray<SkPixelRef*>* fArray;
36 SkTDArray<uint32_t> fGenID;
37};
38
39static void not_supported() {
mtklein@google.com330313a2013-08-22 15:37:26 +000040 SkDEBUGFAIL("this method should never be called");
reed@google.comfe7b1ed2012-11-29 21:00:39 +000041}
42
43static void nothing_to_do() {}
44
45/**
46 * This device will route all bitmaps (primitives and in shaders) to its PRSet.
47 * It should never actually draw anything, so there need not be any pixels
48 * behind its device-bitmap.
49 */
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000050class GatherPixelRefDevice : public SkBitmapDevice {
reed@google.comfe7b1ed2012-11-29 21:00:39 +000051private:
52 PixelRefSet* fPRSet;
53
54 void addBitmap(const SkBitmap& bm) {
55 fPRSet->add(bm.pixelRef());
56 }
57
58 void addBitmapFromPaint(const SkPaint& paint) {
59 SkShader* shader = paint.getShader();
60 if (shader) {
61 SkBitmap bm;
junov@google.com8b2f41b2013-02-01 16:41:47 +000062 // Check whether the shader is a gradient in order to short-circuit
63 // call to asABitmap to prevent generation of bitmaps from
64 // gradient shaders, which implement asABitmap.
65 if (SkShader::kNone_GradientType == shader->asAGradient(NULL) &&
66 shader->asABitmap(&bm, NULL, NULL)) {
reed@google.comfe7b1ed2012-11-29 21:00:39 +000067 fPRSet->add(bm.pixelRef());
68 }
69 }
70 }
71
72public:
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000073 GatherPixelRefDevice(const SkBitmap& bm, PixelRefSet* prset) : SkBitmapDevice(bm) {
reed@google.comfe7b1ed2012-11-29 21:00:39 +000074 fPRSet = prset;
75 }
76
77 virtual void clear(SkColor color) SK_OVERRIDE {
78 nothing_to_do();
79 }
80 virtual void writePixels(const SkBitmap& bitmap, int x, int y,
81 SkCanvas::Config8888 config8888) SK_OVERRIDE {
82 not_supported();
83 }
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +000084
reed@google.comfe7b1ed2012-11-29 21:00:39 +000085 virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE {
86 this->addBitmapFromPaint(paint);
87 }
88 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
89 const SkPoint[], const SkPaint& paint) SK_OVERRIDE {
90 this->addBitmapFromPaint(paint);
91 }
reed@google.com72aa79c2013-01-24 18:27:42 +000092 virtual void drawRect(const SkDraw&, const SkRect&,
93 const SkPaint& paint) SK_OVERRIDE {
94 this->addBitmapFromPaint(paint);
95 }
96 virtual void drawOval(const SkDraw&, const SkRect&,
97 const SkPaint& paint) SK_OVERRIDE {
98 this->addBitmapFromPaint(paint);
99 }
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000100 virtual void drawPath(const SkDraw&, const SkPath& path,
101 const SkPaint& paint, const SkMatrix* prePathMatrix,
102 bool pathIsMutable) SK_OVERRIDE {
103 this->addBitmapFromPaint(paint);
104 }
105 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000106 const SkMatrix&, const SkPaint&) SK_OVERRIDE {
107 this->addBitmap(bitmap);
108 }
109 virtual void drawBitmapRect(const SkDraw&, const SkBitmap& bitmap,
110 const SkRect* srcOrNull, const SkRect& dst,
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000111 const SkPaint&,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000112 SkCanvas::DrawBitmapRectFlags flags) SK_OVERRIDE {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000113 this->addBitmap(bitmap);
114 }
115 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
116 int x, int y, const SkPaint& paint) SK_OVERRIDE {
117 this->addBitmap(bitmap);
118 }
119 virtual void drawText(const SkDraw&, const void* text, size_t len,
120 SkScalar x, SkScalar y,
121 const SkPaint& paint) SK_OVERRIDE {
122 this->addBitmapFromPaint(paint);
123 }
124 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
125 const SkScalar pos[], SkScalar constY,
126 int, const SkPaint& paint) SK_OVERRIDE {
127 this->addBitmapFromPaint(paint);
128 }
129 virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
130 const SkPath& path, const SkMatrix* matrix,
131 const SkPaint& paint) SK_OVERRIDE {
132 this->addBitmapFromPaint(paint);
133 }
134 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount,
135 const SkPoint verts[], const SkPoint texs[],
136 const SkColor colors[], SkXfermode* xmode,
137 const uint16_t indices[], int indexCount,
138 const SkPaint& paint) SK_OVERRIDE {
139 this->addBitmapFromPaint(paint);
140 }
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000141 virtual void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y,
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000142 const SkPaint&) SK_OVERRIDE {
143 nothing_to_do();
144 }
145
146protected:
147 virtual bool onReadPixels(const SkBitmap& bitmap,
148 int x, int y,
149 SkCanvas::Config8888 config8888) SK_OVERRIDE {
150 not_supported();
151 return false;
152 }
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000153
154private:
155 typedef SkBitmapDevice INHERITED;
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000156};
157
158class NoSaveLayerCanvas : public SkCanvas {
159public:
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000160 NoSaveLayerCanvas(SkBaseDevice* device) : INHERITED(device) {}
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000161
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000162 // turn saveLayer() into save() for speed, should not affect correctness.
163 virtual int saveLayer(const SkRect* bounds, const SkPaint* paint,
164 SaveFlags flags) SK_OVERRIDE {
165
166 // Like SkPictureRecord, we don't want to create layers, but we do need
167 // to respect the save and (possibly) its rect-clip.
168
169 int count = this->INHERITED::save(flags);
170 if (bounds) {
171 this->INHERITED::clipRectBounds(bounds, flags, NULL);
172 }
173 return count;
174 }
175
176 // disable aa for speed
177 virtual bool clipRect(const SkRect& rect, SkRegion::Op op,
178 bool doAA) SK_OVERRIDE {
179 return this->INHERITED::clipRect(rect, op, false);
180 }
181
182 // for speed, just respect the bounds, and disable AA. May give us a few
183 // false positives and negatives.
184 virtual bool clipPath(const SkPath& path, SkRegion::Op op,
185 bool doAA) SK_OVERRIDE {
junov@chromium.orged8d6bb2013-05-29 19:09:48 +0000186 return this->updateClipConservativelyUsingBounds(path.getBounds(), op, path.isInverseFillType());
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000187 }
junov@chromium.org50a5cfb2013-02-01 20:39:48 +0000188 virtual bool clipRRect(const SkRRect& rrect, SkRegion::Op op,
189 bool doAA) SK_OVERRIDE {
junov@chromium.orged8d6bb2013-05-29 19:09:48 +0000190 return this->updateClipConservativelyUsingBounds(rrect.getBounds(), op, false);
junov@chromium.org50a5cfb2013-02-01 20:39:48 +0000191 }
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000192
193private:
194 typedef SkCanvas INHERITED;
195};
196
197SkData* SkPictureUtils::GatherPixelRefs(SkPicture* pict, const SkRect& area) {
198 if (NULL == pict) {
199 return NULL;
200 }
201
202 // this test also handles if either area or pict's width/height are empty
203 if (!SkRect::Intersects(area,
204 SkRect::MakeWH(SkIntToScalar(pict->width()),
205 SkIntToScalar(pict->height())))) {
206 return NULL;
207 }
208
209 SkTDArray<SkPixelRef*> array;
210 PixelRefSet prset(&array);
211
212 SkBitmap emptyBitmap;
213 emptyBitmap.setConfig(SkBitmap::kARGB_8888_Config, pict->width(), pict->height());
214 // note: we do not set any pixels (shouldn't need to)
215
216 GatherPixelRefDevice device(emptyBitmap, &prset);
217 NoSaveLayerCanvas canvas(&device);
218
219 canvas.clipRect(area, SkRegion::kIntersect_Op, false);
220 canvas.drawPicture(*pict);
221
222 SkData* data = NULL;
223 int count = array.count();
224 if (count > 0) {
225 data = SkData::NewFromMalloc(array.detach(), count * sizeof(SkPixelRef*));
226 }
227 return data;
228}