blob: 0b147fa5ef9b8aaa1fbba745846310f88a616725 [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 }
95 virtual void drawRRect(const SkDraw&, const SkRRect&,
reed@google.comfe7b1ed2012-11-29 21:00:39 +000096 const SkPaint& paint) SK_OVERRIDE {
97 this->addBitmapFromPaint(paint);
98 }
99 virtual void drawPath(const SkDraw&, const SkPath& path,
100 const SkPaint& paint, const SkMatrix* prePathMatrix,
101 bool pathIsMutable) SK_OVERRIDE {
102 this->addBitmapFromPaint(paint);
103 }
104 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
105 const SkIRect* srcRectOrNull,
106 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,
111 const SkPaint&) SK_OVERRIDE {
112 this->addBitmap(bitmap);
113 }
114 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
115 int x, int y, const SkPaint& paint) SK_OVERRIDE {
116 this->addBitmap(bitmap);
117 }
118 virtual void drawText(const SkDraw&, const void* text, size_t len,
119 SkScalar x, SkScalar y,
120 const SkPaint& paint) SK_OVERRIDE {
121 this->addBitmapFromPaint(paint);
122 }
123 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
124 const SkScalar pos[], SkScalar constY,
125 int, const SkPaint& paint) SK_OVERRIDE {
126 this->addBitmapFromPaint(paint);
127 }
128 virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
129 const SkPath& path, const SkMatrix* matrix,
130 const SkPaint& paint) SK_OVERRIDE {
131 this->addBitmapFromPaint(paint);
132 }
133 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount,
134 const SkPoint verts[], const SkPoint texs[],
135 const SkColor colors[], SkXfermode* xmode,
136 const uint16_t indices[], int indexCount,
137 const SkPaint& paint) SK_OVERRIDE {
138 this->addBitmapFromPaint(paint);
139 }
140 virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y,
141 const SkPaint&) SK_OVERRIDE {
142 nothing_to_do();
143 }
144
145protected:
146 virtual bool onReadPixels(const SkBitmap& bitmap,
147 int x, int y,
148 SkCanvas::Config8888 config8888) SK_OVERRIDE {
149 not_supported();
150 return false;
151 }
152};
153
154class NoSaveLayerCanvas : public SkCanvas {
155public:
156 NoSaveLayerCanvas(SkDevice* device) : INHERITED(device) {}
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000157
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000158 // turn saveLayer() into save() for speed, should not affect correctness.
159 virtual int saveLayer(const SkRect* bounds, const SkPaint* paint,
160 SaveFlags flags) SK_OVERRIDE {
161
162 // Like SkPictureRecord, we don't want to create layers, but we do need
163 // to respect the save and (possibly) its rect-clip.
164
165 int count = this->INHERITED::save(flags);
166 if (bounds) {
167 this->INHERITED::clipRectBounds(bounds, flags, NULL);
168 }
169 return count;
170 }
171
172 // disable aa for speed
173 virtual bool clipRect(const SkRect& rect, SkRegion::Op op,
174 bool doAA) SK_OVERRIDE {
175 return this->INHERITED::clipRect(rect, op, false);
176 }
177
178 // for speed, just respect the bounds, and disable AA. May give us a few
179 // false positives and negatives.
180 virtual bool clipPath(const SkPath& path, SkRegion::Op op,
181 bool doAA) SK_OVERRIDE {
182 return this->INHERITED::clipRect(path.getBounds(), op, false);
183 }
184
185private:
186 typedef SkCanvas INHERITED;
187};
188
189SkData* SkPictureUtils::GatherPixelRefs(SkPicture* pict, const SkRect& area) {
190 if (NULL == pict) {
191 return NULL;
192 }
193
194 // this test also handles if either area or pict's width/height are empty
195 if (!SkRect::Intersects(area,
196 SkRect::MakeWH(SkIntToScalar(pict->width()),
197 SkIntToScalar(pict->height())))) {
198 return NULL;
199 }
200
201 SkTDArray<SkPixelRef*> array;
202 PixelRefSet prset(&array);
203
204 SkBitmap emptyBitmap;
205 emptyBitmap.setConfig(SkBitmap::kARGB_8888_Config, pict->width(), pict->height());
206 // note: we do not set any pixels (shouldn't need to)
207
208 GatherPixelRefDevice device(emptyBitmap, &prset);
209 NoSaveLayerCanvas canvas(&device);
210
211 canvas.clipRect(area, SkRegion::kIntersect_Op, false);
212 canvas.drawPicture(*pict);
213
214 SkData* data = NULL;
215 int count = array.count();
216 if (count > 0) {
217 data = SkData::NewFromMalloc(array.detach(), count * sizeof(SkPixelRef*));
218 }
219 return data;
220}
221