blob: 4ddc566dacc37973a583466225f7509822c8aca3 [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
8#include "SkPictureUtils.h"
9#include "SkCanvas.h"
10#include "SkData.h"
11#include "SkDevice.h"
12#include "SkPixelRef.h"
13#include "SkShader.h"
14
15class PixelRefSet {
16public:
17 PixelRefSet(SkTDArray<SkPixelRef*>* array) : fArray(array) {}
18
19 // This does a linear search on existing pixelrefs, so if this list gets big
20 // we should use a more complex sorted/hashy thing.
21 //
22 void add(SkPixelRef* pr) {
23 uint32_t genID = pr->getGenerationID();
24 if (fGenID.find(genID) < 0) {
25 *fArray->append() = pr;
26 *fGenID.append() = genID;
27// SkDebugf("--- adding [%d] %x %d\n", fArray->count() - 1, pr, genID);
28 } else {
29// SkDebugf("--- already have %x %d\n", pr, genID);
30 }
31 }
32
33private:
34 SkTDArray<SkPixelRef*>* fArray;
35 SkTDArray<uint32_t> fGenID;
36};
37
38static void not_supported() {
39 SkASSERT(!"this method should never be called");
40}
41
42static void nothing_to_do() {}
43
44/**
45 * This device will route all bitmaps (primitives and in shaders) to its PRSet.
46 * It should never actually draw anything, so there need not be any pixels
47 * behind its device-bitmap.
48 */
49class GatherPixelRefDevice : public SkDevice {
50private:
51 PixelRefSet* fPRSet;
52
53 void addBitmap(const SkBitmap& bm) {
54 fPRSet->add(bm.pixelRef());
55 }
56
57 void addBitmapFromPaint(const SkPaint& paint) {
58 SkShader* shader = paint.getShader();
59 if (shader) {
60 SkBitmap bm;
61 if (shader->asABitmap(&bm, NULL, NULL)) {
62 fPRSet->add(bm.pixelRef());
63 }
64 }
65 }
66
67public:
68 GatherPixelRefDevice(const SkBitmap& bm, PixelRefSet* prset) : SkDevice(bm) {
69 fPRSet = prset;
70 }
71
72 virtual void clear(SkColor color) SK_OVERRIDE {
73 nothing_to_do();
74 }
75 virtual void writePixels(const SkBitmap& bitmap, int x, int y,
76 SkCanvas::Config8888 config8888) SK_OVERRIDE {
77 not_supported();
78 }
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +000079
reed@google.comfe7b1ed2012-11-29 21:00:39 +000080 virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE {
81 this->addBitmapFromPaint(paint);
82 }
83 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
84 const SkPoint[], const SkPaint& paint) SK_OVERRIDE {
85 this->addBitmapFromPaint(paint);
86 }
reed@google.com72aa79c2013-01-24 18:27:42 +000087 virtual void drawRect(const SkDraw&, const SkRect&,
88 const SkPaint& paint) SK_OVERRIDE {
89 this->addBitmapFromPaint(paint);
90 }
91 virtual void drawOval(const SkDraw&, const SkRect&,
92 const SkPaint& paint) SK_OVERRIDE {
93 this->addBitmapFromPaint(paint);
94 }
reed@google.comfe7b1ed2012-11-29 21:00:39 +000095 virtual void drawPath(const SkDraw&, const SkPath& path,
96 const SkPaint& paint, const SkMatrix* prePathMatrix,
97 bool pathIsMutable) SK_OVERRIDE {
98 this->addBitmapFromPaint(paint);
99 }
100 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
101 const SkIRect* srcRectOrNull,
102 const SkMatrix&, const SkPaint&) SK_OVERRIDE {
103 this->addBitmap(bitmap);
104 }
105 virtual void drawBitmapRect(const SkDraw&, const SkBitmap& bitmap,
106 const SkRect* srcOrNull, const SkRect& dst,
107 const SkPaint&) SK_OVERRIDE {
108 this->addBitmap(bitmap);
109 }
110 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
111 int x, int y, const SkPaint& paint) SK_OVERRIDE {
112 this->addBitmap(bitmap);
113 }
114 virtual void drawText(const SkDraw&, const void* text, size_t len,
115 SkScalar x, SkScalar y,
116 const SkPaint& paint) SK_OVERRIDE {
117 this->addBitmapFromPaint(paint);
118 }
119 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
120 const SkScalar pos[], SkScalar constY,
121 int, const SkPaint& paint) SK_OVERRIDE {
122 this->addBitmapFromPaint(paint);
123 }
124 virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
125 const SkPath& path, const SkMatrix* matrix,
126 const SkPaint& paint) SK_OVERRIDE {
127 this->addBitmapFromPaint(paint);
128 }
129 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount,
130 const SkPoint verts[], const SkPoint texs[],
131 const SkColor colors[], SkXfermode* xmode,
132 const uint16_t indices[], int indexCount,
133 const SkPaint& paint) SK_OVERRIDE {
134 this->addBitmapFromPaint(paint);
135 }
136 virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y,
137 const SkPaint&) SK_OVERRIDE {
138 nothing_to_do();
139 }
140
141protected:
142 virtual bool onReadPixels(const SkBitmap& bitmap,
143 int x, int y,
144 SkCanvas::Config8888 config8888) SK_OVERRIDE {
145 not_supported();
146 return false;
147 }
148};
149
150class NoSaveLayerCanvas : public SkCanvas {
151public:
152 NoSaveLayerCanvas(SkDevice* device) : INHERITED(device) {}
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000153
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000154 // turn saveLayer() into save() for speed, should not affect correctness.
155 virtual int saveLayer(const SkRect* bounds, const SkPaint* paint,
156 SaveFlags flags) SK_OVERRIDE {
157
158 // Like SkPictureRecord, we don't want to create layers, but we do need
159 // to respect the save and (possibly) its rect-clip.
160
161 int count = this->INHERITED::save(flags);
162 if (bounds) {
163 this->INHERITED::clipRectBounds(bounds, flags, NULL);
164 }
165 return count;
166 }
167
168 // disable aa for speed
169 virtual bool clipRect(const SkRect& rect, SkRegion::Op op,
170 bool doAA) SK_OVERRIDE {
171 return this->INHERITED::clipRect(rect, op, false);
172 }
173
174 // for speed, just respect the bounds, and disable AA. May give us a few
175 // false positives and negatives.
176 virtual bool clipPath(const SkPath& path, SkRegion::Op op,
177 bool doAA) SK_OVERRIDE {
178 return this->INHERITED::clipRect(path.getBounds(), op, false);
179 }
180
181private:
182 typedef SkCanvas INHERITED;
183};
184
185SkData* SkPictureUtils::GatherPixelRefs(SkPicture* pict, const SkRect& area) {
186 if (NULL == pict) {
187 return NULL;
188 }
189
190 // this test also handles if either area or pict's width/height are empty
191 if (!SkRect::Intersects(area,
192 SkRect::MakeWH(SkIntToScalar(pict->width()),
193 SkIntToScalar(pict->height())))) {
194 return NULL;
195 }
196
197 SkTDArray<SkPixelRef*> array;
198 PixelRefSet prset(&array);
199
200 SkBitmap emptyBitmap;
201 emptyBitmap.setConfig(SkBitmap::kARGB_8888_Config, pict->width(), pict->height());
202 // note: we do not set any pixels (shouldn't need to)
203
204 GatherPixelRefDevice device(emptyBitmap, &prset);
205 NoSaveLayerCanvas canvas(&device);
206
207 canvas.clipRect(area, SkRegion::kIntersect_Op, false);
208 canvas.drawPicture(*pict);
209
210 SkData* data = NULL;
211 int count = array.count();
212 if (count > 0) {
213 data = SkData::NewFromMalloc(array.detach(), count * sizeof(SkPixelRef*));
214 }
215 return data;
216}