blob: ffd9aa4429d24c307693b2b07fe465de8e78f5fb [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.com4d164702013-11-12 22:27:30 +000048 * behind its device-bitmap.
49 * FIXME: Derive from SkBaseDevice.
reed@google.comfe7b1ed2012-11-29 21:00:39 +000050 */
reed@google.com4d164702013-11-12 22:27:30 +000051class GatherPixelRefDevice : public SkBitmapDevice {
52private:
53 PixelRefSet* fPRSet;
54
55 void addBitmap(const SkBitmap& bm) {
56 fPRSet->add(bm.pixelRef());
reed@google.comfe7b1ed2012-11-29 21:00:39 +000057 }
58
reed@google.com4d164702013-11-12 22:27:30 +000059 void addBitmapFromPaint(const SkPaint& paint) {
60 SkShader* shader = paint.getShader();
61 if (shader) {
62 SkBitmap bm;
63 // Check whether the shader is a gradient in order to short-circuit
64 // call to asABitmap to prevent generation of bitmaps from
65 // gradient shaders, which implement asABitmap.
66 if (SkShader::kNone_GradientType == shader->asAGradient(NULL) &&
67 shader->asABitmap(&bm, NULL, NULL)) {
68 fPRSet->add(bm.pixelRef());
69 }
70 }
reed@google.com3f4bf512013-11-12 22:14:08 +000071 }
reed@google.com4d164702013-11-12 22:27:30 +000072
73public:
74 GatherPixelRefDevice(const SkBitmap& bm, PixelRefSet* prset) : SkBitmapDevice(bm) {
75 fPRSet = prset;
reed@google.com3f4bf512013-11-12 22:14:08 +000076 }
77
reed@google.comfe7b1ed2012-11-29 21:00:39 +000078 virtual void clear(SkColor color) SK_OVERRIDE {
79 nothing_to_do();
80 }
81 virtual void writePixels(const SkBitmap& bitmap, int x, int y,
82 SkCanvas::Config8888 config8888) SK_OVERRIDE {
83 not_supported();
84 }
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +000085
reed@google.comfe7b1ed2012-11-29 21:00:39 +000086 virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE {
87 this->addBitmapFromPaint(paint);
88 }
89 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
90 const SkPoint[], const SkPaint& paint) SK_OVERRIDE {
91 this->addBitmapFromPaint(paint);
92 }
reed@google.com72aa79c2013-01-24 18:27:42 +000093 virtual void drawRect(const SkDraw&, const SkRect&,
94 const SkPaint& paint) SK_OVERRIDE {
95 this->addBitmapFromPaint(paint);
96 }
scroggo@google.comcac8d012013-11-12 17:10:02 +000097 virtual void drawRRect(const SkDraw&, const SkRRect&,
98 const SkPaint& paint) SK_OVERRIDE {
99 this->addBitmapFromPaint(paint);
100 }
reed@google.com72aa79c2013-01-24 18:27:42 +0000101 virtual void drawOval(const SkDraw&, const SkRect&,
102 const SkPaint& paint) SK_OVERRIDE {
103 this->addBitmapFromPaint(paint);
104 }
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000105 virtual void drawPath(const SkDraw&, const SkPath& path,
106 const SkPaint& paint, const SkMatrix* prePathMatrix,
107 bool pathIsMutable) SK_OVERRIDE {
108 this->addBitmapFromPaint(paint);
109 }
110 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000111 const SkMatrix&, const SkPaint&) SK_OVERRIDE {
112 this->addBitmap(bitmap);
113 }
114 virtual void drawBitmapRect(const SkDraw&, const SkBitmap& bitmap,
115 const SkRect* srcOrNull, const SkRect& dst,
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000116 const SkPaint&,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000117 SkCanvas::DrawBitmapRectFlags flags) SK_OVERRIDE {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000118 this->addBitmap(bitmap);
119 }
120 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
121 int x, int y, const SkPaint& paint) SK_OVERRIDE {
122 this->addBitmap(bitmap);
123 }
124 virtual void drawText(const SkDraw&, const void* text, size_t len,
125 SkScalar x, SkScalar y,
126 const SkPaint& paint) SK_OVERRIDE {
127 this->addBitmapFromPaint(paint);
128 }
129 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
130 const SkScalar pos[], SkScalar constY,
131 int, const SkPaint& paint) SK_OVERRIDE {
132 this->addBitmapFromPaint(paint);
133 }
134 virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
135 const SkPath& path, const SkMatrix* matrix,
136 const SkPaint& paint) SK_OVERRIDE {
137 this->addBitmapFromPaint(paint);
138 }
139 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount,
140 const SkPoint verts[], const SkPoint texs[],
141 const SkColor colors[], SkXfermode* xmode,
142 const uint16_t indices[], int indexCount,
143 const SkPaint& paint) SK_OVERRIDE {
144 this->addBitmapFromPaint(paint);
145 }
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000146 virtual void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y,
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000147 const SkPaint&) SK_OVERRIDE {
148 nothing_to_do();
149 }
150
151protected:
152 virtual bool onReadPixels(const SkBitmap& bitmap,
153 int x, int y,
154 SkCanvas::Config8888 config8888) SK_OVERRIDE {
155 not_supported();
156 return false;
157 }
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000158
159private:
reed@google.com4d164702013-11-12 22:27:30 +0000160 typedef SkBitmapDevice INHERITED;
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000161};
162
163class NoSaveLayerCanvas : public SkCanvas {
164public:
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000165 NoSaveLayerCanvas(SkBaseDevice* device) : INHERITED(device) {}
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000166
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000167 // turn saveLayer() into save() for speed, should not affect correctness.
168 virtual int saveLayer(const SkRect* bounds, const SkPaint* paint,
169 SaveFlags flags) SK_OVERRIDE {
170
171 // Like SkPictureRecord, we don't want to create layers, but we do need
172 // to respect the save and (possibly) its rect-clip.
173
174 int count = this->INHERITED::save(flags);
175 if (bounds) {
176 this->INHERITED::clipRectBounds(bounds, flags, NULL);
177 }
178 return count;
179 }
180
181 // disable aa for speed
182 virtual bool clipRect(const SkRect& rect, SkRegion::Op op,
183 bool doAA) SK_OVERRIDE {
184 return this->INHERITED::clipRect(rect, op, false);
185 }
186
187 // for speed, just respect the bounds, and disable AA. May give us a few
188 // false positives and negatives.
189 virtual bool clipPath(const SkPath& path, SkRegion::Op op,
190 bool doAA) SK_OVERRIDE {
junov@chromium.orged8d6bb2013-05-29 19:09:48 +0000191 return this->updateClipConservativelyUsingBounds(path.getBounds(), op, path.isInverseFillType());
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000192 }
junov@chromium.org50a5cfb2013-02-01 20:39:48 +0000193 virtual bool clipRRect(const SkRRect& rrect, SkRegion::Op op,
194 bool doAA) SK_OVERRIDE {
junov@chromium.orged8d6bb2013-05-29 19:09:48 +0000195 return this->updateClipConservativelyUsingBounds(rrect.getBounds(), op, false);
junov@chromium.org50a5cfb2013-02-01 20:39:48 +0000196 }
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000197
198private:
199 typedef SkCanvas INHERITED;
200};
201
202SkData* SkPictureUtils::GatherPixelRefs(SkPicture* pict, const SkRect& area) {
203 if (NULL == pict) {
204 return NULL;
205 }
206
207 // this test also handles if either area or pict's width/height are empty
208 if (!SkRect::Intersects(area,
209 SkRect::MakeWH(SkIntToScalar(pict->width()),
210 SkIntToScalar(pict->height())))) {
211 return NULL;
212 }
213
214 SkTDArray<SkPixelRef*> array;
215 PixelRefSet prset(&array);
216
reed@google.com4d164702013-11-12 22:27:30 +0000217 SkBitmap emptyBitmap;
218 emptyBitmap.setConfig(SkBitmap::kARGB_8888_Config, pict->width(), pict->height());
219 // note: we do not set any pixels (shouldn't need to)
220
221 GatherPixelRefDevice device(emptyBitmap, &prset);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000222 NoSaveLayerCanvas canvas(&device);
223
224 canvas.clipRect(area, SkRegion::kIntersect_Op, false);
225 canvas.drawPicture(*pict);
226
227 SkData* data = NULL;
228 int count = array.count();
229 if (count > 0) {
230 data = SkData::NewFromMalloc(array.detach(), count * sizeof(SkPixelRef*));
231 }
232 return data;
233}