blob: fa16af07f28dd696e7cd0f65fb1ab923ac3127af [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
reed@google.comec3ca872013-11-13 16:02:18 +000048 * behind its device.
reed@google.comfe7b1ed2012-11-29 21:00:39 +000049 */
reed@google.comec3ca872013-11-13 16:02:18 +000050class GatherPixelRefDevice : public SkBaseDevice {
reed@google.com4d164702013-11-12 22:27:30 +000051public:
reed@google.comec3ca872013-11-13 16:02:18 +000052 GatherPixelRefDevice(int width, int height, PixelRefSet* prset) {
53 fSize.set(width, height);
54 fEmptyBitmap.setConfig(SkBitmap::kNo_Config, width, height);
reed@google.com4d164702013-11-12 22:27:30 +000055 fPRSet = prset;
reed@google.com3f4bf512013-11-12 22:14:08 +000056 }
57
reed@google.comec3ca872013-11-13 16:02:18 +000058 virtual uint32_t getDeviceCapabilities() SK_OVERRIDE { return 0; }
59 virtual int width() const SK_OVERRIDE { return fSize.width(); }
60 virtual int height() const SK_OVERRIDE { return fSize.height(); }
61 virtual bool isOpaque() const SK_OVERRIDE { return false; }
62 virtual SkBitmap::Config config() const SK_OVERRIDE {
63 return SkBitmap::kNo_Config;
64 }
65 virtual GrRenderTarget* accessRenderTarget() SK_OVERRIDE { return NULL; }
66 virtual bool filterTextFlags(const SkPaint& paint, TextFlags*) SK_OVERRIDE {
67 return true;
68 }
69 // TODO: allow this call to return failure, or move to SkBitmapDevice only.
70 virtual const SkBitmap& onAccessBitmap() SK_OVERRIDE {
71 return fEmptyBitmap;
72 }
73 virtual void lockPixels() SK_OVERRIDE { nothing_to_do(); }
74 virtual void unlockPixels() SK_OVERRIDE { nothing_to_do(); }
75 virtual bool allowImageFilter(SkImageFilter*) SK_OVERRIDE { return false; }
76 virtual bool canHandleImageFilter(SkImageFilter*) SK_OVERRIDE { return false; }
77 virtual bool filterImage(SkImageFilter*, const SkBitmap&, const SkMatrix&,
78 SkBitmap* result, SkIPoint* offset) SK_OVERRIDE {
79 return false;
80 }
81
reed@google.comfe7b1ed2012-11-29 21:00:39 +000082 virtual void clear(SkColor color) SK_OVERRIDE {
83 nothing_to_do();
84 }
85 virtual void writePixels(const SkBitmap& bitmap, int x, int y,
86 SkCanvas::Config8888 config8888) SK_OVERRIDE {
87 not_supported();
88 }
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +000089
reed@google.comfe7b1ed2012-11-29 21:00:39 +000090 virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE {
91 this->addBitmapFromPaint(paint);
92 }
93 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
94 const SkPoint[], const SkPaint& paint) SK_OVERRIDE {
95 this->addBitmapFromPaint(paint);
96 }
reed@google.com72aa79c2013-01-24 18:27:42 +000097 virtual void drawRect(const SkDraw&, const SkRect&,
98 const SkPaint& paint) SK_OVERRIDE {
99 this->addBitmapFromPaint(paint);
100 }
scroggo@google.comcac8d012013-11-12 17:10:02 +0000101 virtual void drawRRect(const SkDraw&, const SkRRect&,
102 const SkPaint& paint) SK_OVERRIDE {
103 this->addBitmapFromPaint(paint);
104 }
reed@google.com72aa79c2013-01-24 18:27:42 +0000105 virtual void drawOval(const SkDraw&, const SkRect&,
106 const SkPaint& paint) SK_OVERRIDE {
107 this->addBitmapFromPaint(paint);
108 }
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000109 virtual void drawPath(const SkDraw&, const SkPath& path,
110 const SkPaint& paint, const SkMatrix* prePathMatrix,
111 bool pathIsMutable) SK_OVERRIDE {
112 this->addBitmapFromPaint(paint);
113 }
114 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000115 const SkMatrix&, const SkPaint&) SK_OVERRIDE {
116 this->addBitmap(bitmap);
117 }
118 virtual void drawBitmapRect(const SkDraw&, const SkBitmap& bitmap,
119 const SkRect* srcOrNull, const SkRect& dst,
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000120 const SkPaint&,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000121 SkCanvas::DrawBitmapRectFlags flags) SK_OVERRIDE {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000122 this->addBitmap(bitmap);
123 }
124 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
125 int x, int y, const SkPaint& paint) SK_OVERRIDE {
126 this->addBitmap(bitmap);
127 }
128 virtual void drawText(const SkDraw&, const void* text, size_t len,
129 SkScalar x, SkScalar y,
130 const SkPaint& paint) SK_OVERRIDE {
131 this->addBitmapFromPaint(paint);
132 }
133 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
134 const SkScalar pos[], SkScalar constY,
135 int, const SkPaint& paint) SK_OVERRIDE {
136 this->addBitmapFromPaint(paint);
137 }
138 virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
139 const SkPath& path, const SkMatrix* matrix,
140 const SkPaint& paint) SK_OVERRIDE {
141 this->addBitmapFromPaint(paint);
142 }
143 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount,
144 const SkPoint verts[], const SkPoint texs[],
145 const SkColor colors[], SkXfermode* xmode,
146 const uint16_t indices[], int indexCount,
147 const SkPaint& paint) SK_OVERRIDE {
148 this->addBitmapFromPaint(paint);
149 }
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000150 virtual void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y,
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000151 const SkPaint&) SK_OVERRIDE {
152 nothing_to_do();
153 }
154
155protected:
156 virtual bool onReadPixels(const SkBitmap& bitmap,
157 int x, int y,
158 SkCanvas::Config8888 config8888) SK_OVERRIDE {
159 not_supported();
160 return false;
161 }
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000162
reed@google.comec3ca872013-11-13 16:02:18 +0000163 virtual void replaceBitmapBackendForRasterSurface(const SkBitmap&) SK_OVERRIDE {
164 not_supported();
165 }
166 virtual SkBaseDevice* onCreateCompatibleDevice(SkBitmap::Config config,
167 int width, int height,
168 bool isOpaque,
169 Usage usage) SK_OVERRIDE {
170 // we expect to only get called via savelayer, in which case it is fine.
171 SkASSERT(kSaveLayer_Usage == usage);
172 return SkNEW_ARGS(GatherPixelRefDevice, (width, height, fPRSet));
173 }
174 virtual void flush() SK_OVERRIDE {}
175
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000176private:
reed@google.comec3ca872013-11-13 16:02:18 +0000177 PixelRefSet* fPRSet;
178 SkBitmap fEmptyBitmap; // legacy -- need to remove the need for this guy
179 SkISize fSize;
180
181 void addBitmap(const SkBitmap& bm) {
182 fPRSet->add(bm.pixelRef());
183 }
184
185 void addBitmapFromPaint(const SkPaint& paint) {
186 SkShader* shader = paint.getShader();
187 if (shader) {
188 SkBitmap bm;
189 // Check whether the shader is a gradient in order to short-circuit
190 // call to asABitmap to prevent generation of bitmaps from
191 // gradient shaders, which implement asABitmap.
192 if (SkShader::kNone_GradientType == shader->asAGradient(NULL) &&
193 shader->asABitmap(&bm, NULL, NULL)) {
194 fPRSet->add(bm.pixelRef());
195 }
196 }
197 }
198
199 typedef SkBaseDevice INHERITED;
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000200};
201
202class NoSaveLayerCanvas : public SkCanvas {
203public:
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000204 NoSaveLayerCanvas(SkBaseDevice* device) : INHERITED(device) {}
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000205
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000206 // turn saveLayer() into save() for speed, should not affect correctness.
207 virtual int saveLayer(const SkRect* bounds, const SkPaint* paint,
208 SaveFlags flags) SK_OVERRIDE {
209
210 // Like SkPictureRecord, we don't want to create layers, but we do need
211 // to respect the save and (possibly) its rect-clip.
212
213 int count = this->INHERITED::save(flags);
214 if (bounds) {
215 this->INHERITED::clipRectBounds(bounds, flags, NULL);
216 }
217 return count;
218 }
219
220 // disable aa for speed
221 virtual bool clipRect(const SkRect& rect, SkRegion::Op op,
222 bool doAA) SK_OVERRIDE {
223 return this->INHERITED::clipRect(rect, op, false);
224 }
225
226 // for speed, just respect the bounds, and disable AA. May give us a few
227 // false positives and negatives.
228 virtual bool clipPath(const SkPath& path, SkRegion::Op op,
229 bool doAA) SK_OVERRIDE {
junov@chromium.orged8d6bb2013-05-29 19:09:48 +0000230 return this->updateClipConservativelyUsingBounds(path.getBounds(), op, path.isInverseFillType());
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000231 }
junov@chromium.org50a5cfb2013-02-01 20:39:48 +0000232 virtual bool clipRRect(const SkRRect& rrect, SkRegion::Op op,
233 bool doAA) SK_OVERRIDE {
junov@chromium.orged8d6bb2013-05-29 19:09:48 +0000234 return this->updateClipConservativelyUsingBounds(rrect.getBounds(), op, false);
junov@chromium.org50a5cfb2013-02-01 20:39:48 +0000235 }
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000236
237private:
238 typedef SkCanvas INHERITED;
239};
240
241SkData* SkPictureUtils::GatherPixelRefs(SkPicture* pict, const SkRect& area) {
242 if (NULL == pict) {
243 return NULL;
244 }
245
246 // this test also handles if either area or pict's width/height are empty
247 if (!SkRect::Intersects(area,
248 SkRect::MakeWH(SkIntToScalar(pict->width()),
249 SkIntToScalar(pict->height())))) {
250 return NULL;
251 }
252
253 SkTDArray<SkPixelRef*> array;
254 PixelRefSet prset(&array);
255
reed@google.comec3ca872013-11-13 16:02:18 +0000256 GatherPixelRefDevice device(pict->width(), pict->height(), &prset);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000257 NoSaveLayerCanvas canvas(&device);
258
259 canvas.clipRect(area, SkRegion::kIntersect_Op, false);
260 canvas.drawPicture(*pict);
261
262 SkData* data = NULL;
263 int count = array.count();
264 if (count > 0) {
265 data = SkData::NewFromMalloc(array.detach(), count * sizeof(SkPixelRef*));
266 }
267 return data;
268}