blob: e9dedf4d03a8a710c88dceac013e959ce6ea099d [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.com81e87392014-01-07 16:08:04 +000011#include "SkNoSaveLayerCanvas.h"
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000012#include "SkPictureUtils.h"
reed@google.comfe7b1ed2012-11-29 21:00:39 +000013#include "SkPixelRef.h"
junov@chromium.org50a5cfb2013-02-01 20:39:48 +000014#include "SkRRect.h"
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000015#include "SkShader.h"
reed@google.comfe7b1ed2012-11-29 21:00:39 +000016
17class PixelRefSet {
18public:
19 PixelRefSet(SkTDArray<SkPixelRef*>* array) : fArray(array) {}
20
21 // This does a linear search on existing pixelrefs, so if this list gets big
22 // we should use a more complex sorted/hashy thing.
23 //
24 void add(SkPixelRef* pr) {
25 uint32_t genID = pr->getGenerationID();
26 if (fGenID.find(genID) < 0) {
27 *fArray->append() = pr;
28 *fGenID.append() = genID;
29// SkDebugf("--- adding [%d] %x %d\n", fArray->count() - 1, pr, genID);
30 } else {
31// SkDebugf("--- already have %x %d\n", pr, genID);
32 }
33 }
34
35private:
36 SkTDArray<SkPixelRef*>* fArray;
37 SkTDArray<uint32_t> fGenID;
38};
39
40static void not_supported() {
mtklein@google.com330313a2013-08-22 15:37:26 +000041 SkDEBUGFAIL("this method should never be called");
reed@google.comfe7b1ed2012-11-29 21:00:39 +000042}
43
44static void nothing_to_do() {}
45
46/**
47 * This device will route all bitmaps (primitives and in shaders) to its PRSet.
48 * It should never actually draw anything, so there need not be any pixels
reed@google.comec3ca872013-11-13 16:02:18 +000049 * behind its device.
reed@google.comfe7b1ed2012-11-29 21:00:39 +000050 */
reed@google.comec3ca872013-11-13 16:02:18 +000051class GatherPixelRefDevice : public SkBaseDevice {
reed@google.com4d164702013-11-12 22:27:30 +000052public:
reed@google.comec3ca872013-11-13 16:02:18 +000053 GatherPixelRefDevice(int width, int height, PixelRefSet* prset) {
54 fSize.set(width, height);
55 fEmptyBitmap.setConfig(SkBitmap::kNo_Config, width, height);
reed@google.com4d164702013-11-12 22:27:30 +000056 fPRSet = prset;
reed@google.com3f4bf512013-11-12 22:14:08 +000057 }
58
reed@google.comec3ca872013-11-13 16:02:18 +000059 virtual uint32_t getDeviceCapabilities() SK_OVERRIDE { return 0; }
60 virtual int width() const SK_OVERRIDE { return fSize.width(); }
61 virtual int height() const SK_OVERRIDE { return fSize.height(); }
62 virtual bool isOpaque() const SK_OVERRIDE { return false; }
63 virtual SkBitmap::Config config() const SK_OVERRIDE {
64 return SkBitmap::kNo_Config;
65 }
66 virtual GrRenderTarget* accessRenderTarget() SK_OVERRIDE { return NULL; }
67 virtual bool filterTextFlags(const SkPaint& paint, TextFlags*) SK_OVERRIDE {
robertphillips@google.com81e87392014-01-07 16:08:04 +000068 return false;
reed@google.comec3ca872013-11-13 16:02:18 +000069 }
70 // TODO: allow this call to return failure, or move to SkBitmapDevice only.
71 virtual const SkBitmap& onAccessBitmap() SK_OVERRIDE {
72 return fEmptyBitmap;
73 }
74 virtual void lockPixels() SK_OVERRIDE { nothing_to_do(); }
75 virtual void unlockPixels() SK_OVERRIDE { nothing_to_do(); }
76 virtual bool allowImageFilter(SkImageFilter*) SK_OVERRIDE { return false; }
77 virtual bool canHandleImageFilter(SkImageFilter*) SK_OVERRIDE { return false; }
78 virtual bool filterImage(SkImageFilter*, const SkBitmap&, const SkMatrix&,
79 SkBitmap* result, SkIPoint* offset) SK_OVERRIDE {
80 return false;
81 }
82
reed@google.comfe7b1ed2012-11-29 21:00:39 +000083 virtual void clear(SkColor color) SK_OVERRIDE {
84 nothing_to_do();
85 }
86 virtual void writePixels(const SkBitmap& bitmap, int x, int y,
87 SkCanvas::Config8888 config8888) SK_OVERRIDE {
88 not_supported();
89 }
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +000090
reed@google.comfe7b1ed2012-11-29 21:00:39 +000091 virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE {
92 this->addBitmapFromPaint(paint);
93 }
94 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
95 const SkPoint[], const SkPaint& paint) SK_OVERRIDE {
96 this->addBitmapFromPaint(paint);
97 }
reed@google.com72aa79c2013-01-24 18:27:42 +000098 virtual void drawRect(const SkDraw&, const SkRect&,
99 const SkPaint& paint) SK_OVERRIDE {
100 this->addBitmapFromPaint(paint);
101 }
scroggo@google.comcac8d012013-11-12 17:10:02 +0000102 virtual void drawRRect(const SkDraw&, const SkRRect&,
103 const SkPaint& paint) SK_OVERRIDE {
104 this->addBitmapFromPaint(paint);
105 }
reed@google.com72aa79c2013-01-24 18:27:42 +0000106 virtual void drawOval(const SkDraw&, const SkRect&,
107 const SkPaint& paint) SK_OVERRIDE {
108 this->addBitmapFromPaint(paint);
109 }
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000110 virtual void drawPath(const SkDraw&, const SkPath& path,
111 const SkPaint& paint, const SkMatrix* prePathMatrix,
112 bool pathIsMutable) SK_OVERRIDE {
113 this->addBitmapFromPaint(paint);
114 }
115 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000116 const SkMatrix&, const SkPaint&) SK_OVERRIDE {
117 this->addBitmap(bitmap);
118 }
119 virtual void drawBitmapRect(const SkDraw&, const SkBitmap& bitmap,
120 const SkRect* srcOrNull, const SkRect& dst,
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000121 const SkPaint&,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000122 SkCanvas::DrawBitmapRectFlags flags) SK_OVERRIDE {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000123 this->addBitmap(bitmap);
124 }
125 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
126 int x, int y, const SkPaint& paint) SK_OVERRIDE {
127 this->addBitmap(bitmap);
128 }
129 virtual void drawText(const SkDraw&, const void* text, size_t len,
130 SkScalar x, SkScalar y,
131 const SkPaint& paint) SK_OVERRIDE {
132 this->addBitmapFromPaint(paint);
133 }
134 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
135 const SkScalar pos[], SkScalar constY,
136 int, const SkPaint& paint) SK_OVERRIDE {
137 this->addBitmapFromPaint(paint);
138 }
139 virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
140 const SkPath& path, const SkMatrix* matrix,
141 const SkPaint& paint) SK_OVERRIDE {
142 this->addBitmapFromPaint(paint);
143 }
144 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount,
145 const SkPoint verts[], const SkPoint texs[],
146 const SkColor colors[], SkXfermode* xmode,
147 const uint16_t indices[], int indexCount,
148 const SkPaint& paint) SK_OVERRIDE {
149 this->addBitmapFromPaint(paint);
150 }
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000151 virtual void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y,
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000152 const SkPaint&) SK_OVERRIDE {
153 nothing_to_do();
154 }
155
156protected:
157 virtual bool onReadPixels(const SkBitmap& bitmap,
158 int x, int y,
159 SkCanvas::Config8888 config8888) SK_OVERRIDE {
160 not_supported();
161 return false;
162 }
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000163
reed@google.comec3ca872013-11-13 16:02:18 +0000164 virtual void replaceBitmapBackendForRasterSurface(const SkBitmap&) SK_OVERRIDE {
165 not_supported();
166 }
167 virtual SkBaseDevice* onCreateCompatibleDevice(SkBitmap::Config config,
168 int width, int height,
169 bool isOpaque,
170 Usage usage) SK_OVERRIDE {
171 // we expect to only get called via savelayer, in which case it is fine.
172 SkASSERT(kSaveLayer_Usage == usage);
173 return SkNEW_ARGS(GatherPixelRefDevice, (width, height, fPRSet));
174 }
175 virtual void flush() SK_OVERRIDE {}
176
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000177private:
reed@google.comec3ca872013-11-13 16:02:18 +0000178 PixelRefSet* fPRSet;
179 SkBitmap fEmptyBitmap; // legacy -- need to remove the need for this guy
180 SkISize fSize;
181
182 void addBitmap(const SkBitmap& bm) {
183 fPRSet->add(bm.pixelRef());
184 }
185
186 void addBitmapFromPaint(const SkPaint& paint) {
187 SkShader* shader = paint.getShader();
188 if (shader) {
189 SkBitmap bm;
190 // Check whether the shader is a gradient in order to short-circuit
191 // call to asABitmap to prevent generation of bitmaps from
192 // gradient shaders, which implement asABitmap.
193 if (SkShader::kNone_GradientType == shader->asAGradient(NULL) &&
194 shader->asABitmap(&bm, NULL, NULL)) {
195 fPRSet->add(bm.pixelRef());
196 }
197 }
198 }
199
200 typedef SkBaseDevice INHERITED;
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000201};
202
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000203SkData* SkPictureUtils::GatherPixelRefs(SkPicture* pict, const SkRect& area) {
204 if (NULL == pict) {
205 return NULL;
206 }
207
208 // this test also handles if either area or pict's width/height are empty
209 if (!SkRect::Intersects(area,
210 SkRect::MakeWH(SkIntToScalar(pict->width()),
211 SkIntToScalar(pict->height())))) {
212 return NULL;
213 }
214
215 SkTDArray<SkPixelRef*> array;
216 PixelRefSet prset(&array);
217
reed@google.comec3ca872013-11-13 16:02:18 +0000218 GatherPixelRefDevice device(pict->width(), pict->height(), &prset);
robertphillips@google.com81e87392014-01-07 16:08:04 +0000219 SkNoSaveLayerCanvas canvas(&device);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000220
221 canvas.clipRect(area, SkRegion::kIntersect_Op, false);
222 canvas.drawPicture(*pict);
223
224 SkData* data = NULL;
225 int count = array.count();
226 if (count > 0) {
227 data = SkData::NewFromMalloc(array.detach(), count * sizeof(SkPixelRef*));
228 }
229 return data;
230}