blob: 4f4d2bf30a9294be4211a1c9dbb54c4eef29c02b [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);
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +000057 fEmptyBitmap.setInfo(SkImageInfo::MakeUnknown(width, height));
reed@google.com4d164702013-11-12 22:27:30 +000058 fPRSet = prset;
reed@google.com3f4bf512013-11-12 22:14:08 +000059 }
60
reedf252f642014-06-14 04:24:56 -070061 virtual SkImageInfo imageInfo() const SK_OVERRIDE {
62 return SkImageInfo::MakeUnknown(fSize.width(), fSize.height());
63 }
reed@google.comec3ca872013-11-13 16:02:18 +000064 virtual GrRenderTarget* accessRenderTarget() SK_OVERRIDE { return NULL; }
65 virtual bool filterTextFlags(const SkPaint& paint, TextFlags*) SK_OVERRIDE {
robertphillips@google.com81e87392014-01-07 16:08:04 +000066 return false;
reed@google.comec3ca872013-11-13 16:02:18 +000067 }
68 // TODO: allow this call to return failure, or move to SkBitmapDevice only.
69 virtual const SkBitmap& onAccessBitmap() SK_OVERRIDE {
70 return fEmptyBitmap;
71 }
72 virtual void lockPixels() SK_OVERRIDE { nothing_to_do(); }
73 virtual void unlockPixels() SK_OVERRIDE { nothing_to_do(); }
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +000074 virtual bool allowImageFilter(const SkImageFilter*) SK_OVERRIDE { return false; }
75 virtual bool canHandleImageFilter(const SkImageFilter*) SK_OVERRIDE { return false; }
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +000076 virtual bool filterImage(const SkImageFilter*, const SkBitmap&, const SkImageFilter::Context&,
reed@google.comec3ca872013-11-13 16:02:18 +000077 SkBitmap* result, SkIPoint* offset) SK_OVERRIDE {
78 return false;
79 }
80
reed@google.comfe7b1ed2012-11-29 21:00:39 +000081 virtual void clear(SkColor color) SK_OVERRIDE {
82 nothing_to_do();
83 }
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000084
reed@google.comfe7b1ed2012-11-29 21:00:39 +000085 virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE {
86 this->addBitmapFromPaint(paint);
87 }
88 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
89 const SkPoint[], const SkPaint& paint) SK_OVERRIDE {
90 this->addBitmapFromPaint(paint);
91 }
reed@google.com72aa79c2013-01-24 18:27:42 +000092 virtual void drawRect(const SkDraw&, const SkRect&,
93 const SkPaint& paint) SK_OVERRIDE {
94 this->addBitmapFromPaint(paint);
95 }
scroggo@google.comcac8d012013-11-12 17:10:02 +000096 virtual void drawRRect(const SkDraw&, const SkRRect&,
97 const SkPaint& paint) SK_OVERRIDE {
98 this->addBitmapFromPaint(paint);
99 }
reed@google.com72aa79c2013-01-24 18:27:42 +0000100 virtual void drawOval(const SkDraw&, const SkRect&,
101 const SkPaint& paint) SK_OVERRIDE {
102 this->addBitmapFromPaint(paint);
103 }
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000104 virtual void drawPath(const SkDraw&, const SkPath& path,
105 const SkPaint& paint, const SkMatrix* prePathMatrix,
106 bool pathIsMutable) SK_OVERRIDE {
107 this->addBitmapFromPaint(paint);
108 }
109 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000110 const SkMatrix&, const SkPaint& paint) SK_OVERRIDE {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000111 this->addBitmap(bitmap);
reedc77392e2014-06-02 13:07:26 -0700112 if (kAlpha_8_SkColorType == bitmap.colorType()) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000113 this->addBitmapFromPaint(paint);
114 }
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000115 }
116 virtual void drawBitmapRect(const SkDraw&, const SkBitmap& bitmap,
117 const SkRect* srcOrNull, const SkRect& dst,
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000118 const SkPaint& paint,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000119 SkCanvas::DrawBitmapRectFlags flags) SK_OVERRIDE {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000120 this->addBitmap(bitmap);
reedc77392e2014-06-02 13:07:26 -0700121 if (kAlpha_8_SkColorType == bitmap.colorType()) {
robertphillips@google.comed9866c2014-01-09 19:20:45 +0000122 this->addBitmapFromPaint(paint);
123 }
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000124 }
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:
reed@google.comec3ca872013-11-13 16:02:18 +0000157 virtual void replaceBitmapBackendForRasterSurface(const SkBitmap&) SK_OVERRIDE {
158 not_supported();
159 }
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000160 virtual SkBaseDevice* onCreateDevice(const SkImageInfo& info, Usage usage) SK_OVERRIDE {
reed@google.comec3ca872013-11-13 16:02:18 +0000161 // we expect to only get called via savelayer, in which case it is fine.
162 SkASSERT(kSaveLayer_Usage == usage);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000163 return SkNEW_ARGS(GatherPixelRefDevice, (info.width(), info.height(), fPRSet));
reed@google.comec3ca872013-11-13 16:02:18 +0000164 }
165 virtual void flush() SK_OVERRIDE {}
166
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000167private:
reed@google.comec3ca872013-11-13 16:02:18 +0000168 PixelRefSet* fPRSet;
169 SkBitmap fEmptyBitmap; // legacy -- need to remove the need for this guy
170 SkISize fSize;
171
172 void addBitmap(const SkBitmap& bm) {
173 fPRSet->add(bm.pixelRef());
174 }
175
176 void addBitmapFromPaint(const SkPaint& paint) {
177 SkShader* shader = paint.getShader();
178 if (shader) {
179 SkBitmap bm;
180 // Check whether the shader is a gradient in order to short-circuit
181 // call to asABitmap to prevent generation of bitmaps from
182 // gradient shaders, which implement asABitmap.
183 if (SkShader::kNone_GradientType == shader->asAGradient(NULL) &&
184 shader->asABitmap(&bm, NULL, NULL)) {
185 fPRSet->add(bm.pixelRef());
186 }
187 }
188 }
189
190 typedef SkBaseDevice INHERITED;
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000191};
192
robertphillipsce4dd3d2014-07-07 13:46:35 -0700193SkData* SkPictureUtils::GatherPixelRefs(const SkPicture* pict, const SkRect& area) {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000194 if (NULL == pict) {
195 return NULL;
196 }
197
198 // this test also handles if either area or pict's width/height are empty
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700199 if (!SkRect::Intersects(area, pict->cullRect())) {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000200 return NULL;
201 }
202
203 SkTDArray<SkPixelRef*> array;
204 PixelRefSet prset(&array);
205
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700206 GatherPixelRefDevice device(SkScalarCeilToInt(pict->cullRect().width()),
207 SkScalarCeilToInt(pict->cullRect().height()),
208 &prset);
robertphillips@google.com81e87392014-01-07 16:08:04 +0000209 SkNoSaveLayerCanvas canvas(&device);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000210
211 canvas.clipRect(area, SkRegion::kIntersect_Op, false);
robertphillips9b14f262014-06-04 05:40:44 -0700212 canvas.drawPicture(pict);
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000213
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}