blob: 4af8da8ecfcc541f594053c3ec3da75061733293 [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:
robertphillips@google.com56bf6e42014-01-13 13:33:26 +000053 SK_DECLARE_INST_COUNT(GatherPixelRefDevice)
54
reed@google.comec3ca872013-11-13 16:02:18 +000055 GatherPixelRefDevice(int width, int height, PixelRefSet* prset) {
56 fSize.set(width, height);
57 fEmptyBitmap.setConfig(SkBitmap::kNo_Config, width, height);
reed@google.com4d164702013-11-12 22:27:30 +000058 fPRSet = prset;
reed@google.com3f4bf512013-11-12 22:14:08 +000059 }
60
reed@google.comec3ca872013-11-13 16:02:18 +000061 virtual uint32_t getDeviceCapabilities() SK_OVERRIDE { return 0; }
62 virtual int width() const SK_OVERRIDE { return fSize.width(); }
63 virtual int height() const SK_OVERRIDE { return fSize.height(); }
64 virtual bool isOpaque() const SK_OVERRIDE { return false; }
65 virtual SkBitmap::Config config() const SK_OVERRIDE {
66 return SkBitmap::kNo_Config;
67 }
68 virtual GrRenderTarget* accessRenderTarget() SK_OVERRIDE { return NULL; }
69 virtual bool filterTextFlags(const SkPaint& paint, TextFlags*) SK_OVERRIDE {
robertphillips@google.com81e87392014-01-07 16:08:04 +000070 return false;
reed@google.comec3ca872013-11-13 16:02:18 +000071 }
72 // TODO: allow this call to return failure, or move to SkBitmapDevice only.
73 virtual const SkBitmap& onAccessBitmap() SK_OVERRIDE {
74 return fEmptyBitmap;
75 }
76 virtual void lockPixels() SK_OVERRIDE { nothing_to_do(); }
77 virtual void unlockPixels() SK_OVERRIDE { nothing_to_do(); }
78 virtual bool allowImageFilter(SkImageFilter*) SK_OVERRIDE { return false; }
79 virtual bool canHandleImageFilter(SkImageFilter*) SK_OVERRIDE { return false; }
80 virtual bool filterImage(SkImageFilter*, const SkBitmap&, const SkMatrix&,
81 SkBitmap* result, SkIPoint* offset) SK_OVERRIDE {
82 return false;
83 }
84
reed@google.comfe7b1ed2012-11-29 21:00:39 +000085 virtual void clear(SkColor color) SK_OVERRIDE {
86 nothing_to_do();
87 }
88 virtual void writePixels(const SkBitmap& bitmap, int x, int y,
89 SkCanvas::Config8888 config8888) SK_OVERRIDE {
90 not_supported();
91 }
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +000092
reed@google.comfe7b1ed2012-11-29 21:00:39 +000093 virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE {
94 this->addBitmapFromPaint(paint);
95 }
96 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
97 const SkPoint[], const SkPaint& paint) SK_OVERRIDE {
98 this->addBitmapFromPaint(paint);
99 }
reed@google.com72aa79c2013-01-24 18:27:42 +0000100 virtual void drawRect(const SkDraw&, const SkRect&,
101 const SkPaint& paint) SK_OVERRIDE {
102 this->addBitmapFromPaint(paint);
103 }
scroggo@google.comcac8d012013-11-12 17:10:02 +0000104 virtual void drawRRect(const SkDraw&, const SkRRect&,
105 const SkPaint& paint) SK_OVERRIDE {
106 this->addBitmapFromPaint(paint);
107 }
reed@google.com72aa79c2013-01-24 18:27:42 +0000108 virtual void drawOval(const SkDraw&, const SkRect&,
109 const SkPaint& paint) SK_OVERRIDE {
110 this->addBitmapFromPaint(paint);
111 }
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000112 virtual void drawPath(const SkDraw&, const SkPath& path,
113 const SkPaint& paint, const SkMatrix* prePathMatrix,
114 bool pathIsMutable) SK_OVERRIDE {
115 this->addBitmapFromPaint(paint);
116 }
117 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000118 const SkMatrix&, const SkPaint& paint) SK_OVERRIDE {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000119 this->addBitmap(bitmap);
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000120 if (SkBitmap::kA8_Config == bitmap.config()) {
121 this->addBitmapFromPaint(paint);
122 }
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000123 }
124 virtual void drawBitmapRect(const SkDraw&, const SkBitmap& bitmap,
125 const SkRect* srcOrNull, const SkRect& dst,
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000126 const SkPaint& paint,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000127 SkCanvas::DrawBitmapRectFlags flags) SK_OVERRIDE {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000128 this->addBitmap(bitmap);
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000129 if (SkBitmap::kA8_Config == bitmap.config()) {
130 this->addBitmapFromPaint(paint);
131 }
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000132 }
133 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
134 int x, int y, const SkPaint& paint) SK_OVERRIDE {
135 this->addBitmap(bitmap);
136 }
137 virtual void drawText(const SkDraw&, const void* text, size_t len,
138 SkScalar x, SkScalar y,
139 const SkPaint& paint) SK_OVERRIDE {
140 this->addBitmapFromPaint(paint);
141 }
142 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
143 const SkScalar pos[], SkScalar constY,
144 int, const SkPaint& paint) SK_OVERRIDE {
145 this->addBitmapFromPaint(paint);
146 }
147 virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
148 const SkPath& path, const SkMatrix* matrix,
149 const SkPaint& paint) SK_OVERRIDE {
150 this->addBitmapFromPaint(paint);
151 }
152 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount,
153 const SkPoint verts[], const SkPoint texs[],
154 const SkColor colors[], SkXfermode* xmode,
155 const uint16_t indices[], int indexCount,
156 const SkPaint& paint) SK_OVERRIDE {
157 this->addBitmapFromPaint(paint);
158 }
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000159 virtual void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y,
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000160 const SkPaint&) SK_OVERRIDE {
161 nothing_to_do();
162 }
163
164protected:
165 virtual bool onReadPixels(const SkBitmap& bitmap,
166 int x, int y,
167 SkCanvas::Config8888 config8888) SK_OVERRIDE {
168 not_supported();
169 return false;
170 }
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000171
reed@google.comec3ca872013-11-13 16:02:18 +0000172 virtual void replaceBitmapBackendForRasterSurface(const SkBitmap&) SK_OVERRIDE {
173 not_supported();
174 }
175 virtual SkBaseDevice* onCreateCompatibleDevice(SkBitmap::Config config,
176 int width, int height,
177 bool isOpaque,
178 Usage usage) SK_OVERRIDE {
179 // we expect to only get called via savelayer, in which case it is fine.
180 SkASSERT(kSaveLayer_Usage == usage);
181 return SkNEW_ARGS(GatherPixelRefDevice, (width, height, fPRSet));
182 }
183 virtual void flush() SK_OVERRIDE {}
184
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000185private:
reed@google.comec3ca872013-11-13 16:02:18 +0000186 PixelRefSet* fPRSet;
187 SkBitmap fEmptyBitmap; // legacy -- need to remove the need for this guy
188 SkISize fSize;
189
190 void addBitmap(const SkBitmap& bm) {
191 fPRSet->add(bm.pixelRef());
192 }
193
194 void addBitmapFromPaint(const SkPaint& paint) {
195 SkShader* shader = paint.getShader();
196 if (shader) {
197 SkBitmap bm;
198 // Check whether the shader is a gradient in order to short-circuit
199 // call to asABitmap to prevent generation of bitmaps from
200 // gradient shaders, which implement asABitmap.
201 if (SkShader::kNone_GradientType == shader->asAGradient(NULL) &&
202 shader->asABitmap(&bm, NULL, NULL)) {
203 fPRSet->add(bm.pixelRef());
204 }
205 }
206 }
207
208 typedef SkBaseDevice INHERITED;
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000209};
210
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000211SkData* SkPictureUtils::GatherPixelRefs(SkPicture* pict, const SkRect& area) {
212 if (NULL == pict) {
213 return NULL;
214 }
215
216 // this test also handles if either area or pict's width/height are empty
217 if (!SkRect::Intersects(area,
218 SkRect::MakeWH(SkIntToScalar(pict->width()),
219 SkIntToScalar(pict->height())))) {
220 return NULL;
221 }
222
223 SkTDArray<SkPixelRef*> array;
224 PixelRefSet prset(&array);
225
reed@google.comec3ca872013-11-13 16:02:18 +0000226 GatherPixelRefDevice device(pict->width(), pict->height(), &prset);
robertphillips@google.com81e87392014-01-07 16:08:04 +0000227 SkNoSaveLayerCanvas canvas(&device);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000228
229 canvas.clipRect(area, SkRegion::kIntersect_Op, false);
230 canvas.drawPicture(*pict);
231
232 SkData* data = NULL;
233 int count = array.count();
234 if (count > 0) {
235 data = SkData::NewFromMalloc(array.detach(), count * sizeof(SkPixelRef*));
236 }
237 return data;
238}