blob: 84a13be46629ef70ac482aafd4604dd0c5dc6de4 [file] [log] [blame]
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001/*
2 * Copyright 2014 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
8#include "GrPictureUtils.h"
9#include "SkDevice.h"
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +000010#include "SkDraw.h"
11#include "SkPaintPriv.h"
robertphillips@google.combeb1af22014-05-07 21:31:09 +000012#include "SkPicturePlayback.h"
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +000013
14SkPicture::AccelData::Key GPUAccelData::ComputeAccelDataKey() {
15 static const SkPicture::AccelData::Key gGPUID = SkPicture::AccelData::GenerateDomain();
16
17 return gGPUID;
18}
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +000019
skia.committer@gmail.com2c48ee82014-04-01 03:07:47 +000020// The GrGather device performs GPU-backend-specific preprocessing on
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +000021// a picture. The results are stored in a GPUAccelData.
22//
23// Currently the only interesting work is done in drawDevice (i.e., when a
24// saveLayer is collapsed back into its parent) and, maybe, in onCreateDevice.
25// All the current work could be done much more efficiently by just traversing the
26// raw op codes in the SkPicture (although we would still need to replay all the
27// clip calls).
28class GrGatherDevice : public SkBaseDevice {
29public:
30 SK_DECLARE_INST_COUNT(GrGatherDevice)
31
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +000032 GrGatherDevice(int width, int height, SkPicture* picture, GPUAccelData* accelData,
33 int saveLayerDepth) {
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +000034 fPicture = picture;
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +000035 fSaveLayerDepth = saveLayerDepth;
36 fInfo.fValid = true;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +000037 fInfo.fSize.set(width, height);
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +000038 fInfo.fPaint = NULL;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +000039 fInfo.fSaveLayerOpID = fPicture->EXPERIMENTAL_curOpID();
40 fInfo.fRestoreOpID = 0;
41 fInfo.fHasNestedLayers = false;
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +000042 fInfo.fIsNested = (2 == fSaveLayerDepth);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +000043
skia.committer@gmail.com2c48ee82014-04-01 03:07:47 +000044 fEmptyBitmap.setConfig(SkImageInfo::Make(fInfo.fSize.fWidth,
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +000045 fInfo.fSize.fHeight,
46 kUnknown_SkColorType,
47 kIgnore_SkAlphaType));
48 fAccelData = accelData;
49 fAlreadyDrawn = false;
50 }
51
52 virtual ~GrGatherDevice() { }
53
54 virtual int width() const SK_OVERRIDE { return fInfo.fSize.width(); }
55 virtual int height() const SK_OVERRIDE { return fInfo.fSize.height(); }
56 virtual bool isOpaque() const SK_OVERRIDE { return false; }
57 virtual SkBitmap::Config config() const SK_OVERRIDE {
58 return SkBitmap::kNo_Config;
59 }
60 virtual SkImageInfo imageInfo() const SK_OVERRIDE {
61 return fEmptyBitmap.info();
62 }
63
64#ifdef SK_SUPPORT_LEGACY_WRITEPIXELSCONFIG
65 virtual void writePixels(const SkBitmap& bitmap, int x, int y,
66 SkCanvas::Config8888 config8888) SK_OVERRIDE {
67 NotSupported();
68 }
69#endif
70 virtual GrRenderTarget* accessRenderTarget() SK_OVERRIDE { return NULL; }
71
72protected:
73 virtual bool filterTextFlags(const SkPaint& paint, TextFlags*) SK_OVERRIDE {
74 return false;
75 }
76 virtual void clear(SkColor color) SK_OVERRIDE {
77 NothingToDo();
78 }
79 virtual void drawPaint(const SkDraw& draw, const SkPaint& paint) SK_OVERRIDE {
80 }
81 virtual void drawPoints(const SkDraw& draw, SkCanvas::PointMode mode, size_t count,
82 const SkPoint points[], const SkPaint& paint) SK_OVERRIDE {
83 }
84 virtual void drawRect(const SkDraw& draw, const SkRect& rect,
85 const SkPaint& paint) SK_OVERRIDE {
86 }
87 virtual void drawOval(const SkDraw& draw, const SkRect& rect,
88 const SkPaint& paint) SK_OVERRIDE {
89 }
90 virtual void drawRRect(const SkDraw& draw, const SkRRect& rrect,
91 const SkPaint& paint) SK_OVERRIDE {
92 }
93 virtual void drawPath(const SkDraw& draw, const SkPath& path,
94 const SkPaint& paint, const SkMatrix* prePathMatrix,
95 bool pathIsMutable) SK_OVERRIDE {
96 }
97 virtual void drawBitmap(const SkDraw& draw, const SkBitmap& bitmap,
98 const SkMatrix& matrix, const SkPaint& paint) SK_OVERRIDE {
99 }
100 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
101 int x, int y, const SkPaint& paint) SK_OVERRIDE {
102 }
103 virtual void drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
104 const SkRect* srcOrNull, const SkRect& dst,
105 const SkPaint& paint,
106 SkCanvas::DrawBitmapRectFlags flags) SK_OVERRIDE {
107 }
108 virtual void drawText(const SkDraw& draw, const void* text, size_t len,
109 SkScalar x, SkScalar y,
110 const SkPaint& paint) SK_OVERRIDE {
111 }
112 virtual void drawPosText(const SkDraw& draw, const void* text, size_t len,
113 const SkScalar pos[], SkScalar constY,
114 int scalarsPerPos, const SkPaint& paint) SK_OVERRIDE {
115 }
116 virtual void drawTextOnPath(const SkDraw& draw, const void* text, size_t len,
117 const SkPath& path, const SkMatrix* matrix,
118 const SkPaint& paint) SK_OVERRIDE {
119 }
120 virtual void drawVertices(const SkDraw& draw, SkCanvas::VertexMode, int vertexCount,
121 const SkPoint verts[], const SkPoint texs[],
122 const SkColor colors[], SkXfermode* xmode,
123 const uint16_t indices[], int indexCount,
124 const SkPaint& paint) SK_OVERRIDE {
125 }
126 virtual void drawDevice(const SkDraw& draw, SkBaseDevice* deviceIn, int x, int y,
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000127 const SkPaint& paint) SK_OVERRIDE {
128 // deviceIn is the one that is being "restored" back to its parent
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +0000129 GrGatherDevice* device = static_cast<GrGatherDevice*>(deviceIn);
130
131 if (device->fAlreadyDrawn) {
132 return;
133 }
134
135 device->fInfo.fRestoreOpID = fPicture->EXPERIMENTAL_curOpID();
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000136 device->fInfo.fCTM = *draw.fMatrix;
137 device->fInfo.fCTM.postTranslate(SkIntToScalar(-device->getOrigin().fX),
138 SkIntToScalar(-device->getOrigin().fY));
139
commit-bot@chromium.orgf97d65d2014-05-08 23:24:05 +0000140 device->fInfo.fOffset = device->getOrigin();
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000141
142 if (NeedsDeepCopy(paint)) {
143 // This NULL acts as a signal that the paint was uncopyable (for now)
144 device->fInfo.fPaint = NULL;
145 device->fInfo.fValid = false;
146 } else {
147 device->fInfo.fPaint = SkNEW_ARGS(SkPaint, (paint));
148 }
149
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +0000150 fAccelData->addSaveLayerInfo(device->fInfo);
151 device->fAlreadyDrawn = true;
152 }
153 // TODO: allow this call to return failure, or move to SkBitmapDevice only.
154 virtual const SkBitmap& onAccessBitmap() SK_OVERRIDE {
155 return fEmptyBitmap;
156 }
157#ifdef SK_SUPPORT_LEGACY_READPIXELSCONFIG
158 virtual bool onReadPixels(const SkBitmap& bitmap,
159 int x, int y,
160 SkCanvas::Config8888 config8888) SK_OVERRIDE {
161 NotSupported();
162 return false;
163 }
164#endif
165 virtual void lockPixels() SK_OVERRIDE { NothingToDo(); }
166 virtual void unlockPixels() SK_OVERRIDE { NothingToDo(); }
167 virtual bool allowImageFilter(const SkImageFilter*) SK_OVERRIDE { return false; }
168 virtual bool canHandleImageFilter(const SkImageFilter*) SK_OVERRIDE { return false; }
169 virtual bool filterImage(const SkImageFilter*, const SkBitmap&, const SkImageFilter::Context&,
170 SkBitmap* result, SkIPoint* offset) SK_OVERRIDE {
171 return false;
172 }
173
174private:
175 // The picture being processed
176 SkPicture *fPicture;
177
178 SkBitmap fEmptyBitmap; // legacy -- need to remove
179
180 // All information gathered during the gather process is stored here
181 GPUAccelData* fAccelData;
182
183 // true if this device has already been drawn back to its parent(s) at least
184 // once.
185 bool fAlreadyDrawn;
186
187 // The information regarding the saveLayer call this device represents.
188 GPUAccelData::SaveLayerInfo fInfo;
189
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000190 // The depth of this device in the saveLayer stack
191 int fSaveLayerDepth;
192
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +0000193 virtual void replaceBitmapBackendForRasterSurface(const SkBitmap&) SK_OVERRIDE {
194 NotSupported();
195 }
196
197 virtual SkBaseDevice* onCreateDevice(const SkImageInfo& info, Usage usage) SK_OVERRIDE {
198 // we expect to only get called via savelayer, in which case it is fine.
199 SkASSERT(kSaveLayer_Usage == usage);
200
201 fInfo.fHasNestedLayers = true;
skia.committer@gmail.coma5b068c2014-05-07 03:04:15 +0000202 return SkNEW_ARGS(GrGatherDevice, (info.width(), info.height(), fPicture,
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000203 fAccelData, fSaveLayerDepth+1));
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +0000204 }
205
206 virtual void flush() SK_OVERRIDE {}
207
208 static void NotSupported() {
209 SkDEBUGFAIL("this method should never be called");
210 }
211
212 static void NothingToDo() {}
213
214 typedef SkBaseDevice INHERITED;
215};
216
217// The GrGatherCanvas allows saveLayers but simplifies clipping. It is really
218// only intended to be used as:
219//
220// GrGatherDevice dev(w, h, picture, accelData);
221// GrGatherCanvas canvas(..., picture);
222// canvas.gather();
skia.committer@gmail.com2c48ee82014-04-01 03:07:47 +0000223//
224// which is all just to fill in 'accelData'
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +0000225class SK_API GrGatherCanvas : public SkCanvas {
226public:
skia.committer@gmail.com2c48ee82014-04-01 03:07:47 +0000227 GrGatherCanvas(GrGatherDevice* device, SkPicture* pict)
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +0000228 : INHERITED(device)
229 , fPicture(pict) {
230 }
231
232 void gather() {
233 if (NULL == fPicture || 0 == fPicture->width() || 0 == fPicture->height()) {
234 return;
235 }
236
237 this->clipRect(SkRect::MakeWH(SkIntToScalar(fPicture->width()),
238 SkIntToScalar(fPicture->height())),
239 SkRegion::kIntersect_Op, false);
240 this->drawPicture(*fPicture);
241 }
242
243 virtual void drawPicture(SkPicture& picture) SK_OVERRIDE {
robertphillips@google.combeb1af22014-05-07 21:31:09 +0000244 // BBH-based rendering doesn't re-issue many of the operations the gather
245 // process cares about (e.g., saves and restores) so it must be disabled.
246 if (NULL != picture.fPlayback) {
247 picture.fPlayback->setUseBBH(false);
248 }
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +0000249 picture.draw(this);
robertphillips@google.combeb1af22014-05-07 21:31:09 +0000250 if (NULL != picture.fPlayback) {
251 picture.fPlayback->setUseBBH(true);
252 }
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +0000253 }
254protected:
255 // disable aa for speed
256 virtual void onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle) SK_OVERRIDE {
257 this->INHERITED::onClipRect(rect, op, kHard_ClipEdgeStyle);
258 }
259
260 // for speed, just respect the bounds, and disable AA. May give us a few
261 // false positives and negatives.
262 virtual void onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle) SK_OVERRIDE {
263 this->updateClipConservativelyUsingBounds(path.getBounds(), op,
264 path.isInverseFillType());
265 }
266 virtual void onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle) SK_OVERRIDE {
267 this->updateClipConservativelyUsingBounds(rrect.getBounds(), op, false);
268 }
269
270private:
271 SkPicture* fPicture;
272
273 typedef SkCanvas INHERITED;
274};
275
skia.committer@gmail.com2c48ee82014-04-01 03:07:47 +0000276// GatherGPUInfo is only intended to be called within the context of SkGpuDevice's
277// EXPERIMENTAL_optimize method.
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +0000278void GatherGPUInfo(SkPicture* pict, GPUAccelData* accelData) {
279 if (0 == pict->width() || 0 == pict->height()) {
280 return ;
281 }
282
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000283 GrGatherDevice device(pict->width(), pict->height(), pict, accelData, 0);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +0000284 GrGatherCanvas canvas(&device, pict);
285
286 canvas.gather();
287}