blob: 30a1d39a3c7b59ed4cc37d31d1180b13343b56aa [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
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +000044 fEmptyBitmap.setInfo(SkImageInfo::MakeUnknown(fInfo.fSize.fWidth, fInfo.fSize.fHeight));
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +000045 fAccelData = accelData;
46 fAlreadyDrawn = false;
47 }
48
49 virtual ~GrGatherDevice() { }
50
51 virtual int width() const SK_OVERRIDE { return fInfo.fSize.width(); }
52 virtual int height() const SK_OVERRIDE { return fInfo.fSize.height(); }
53 virtual bool isOpaque() const SK_OVERRIDE { return false; }
54 virtual SkBitmap::Config config() const SK_OVERRIDE {
55 return SkBitmap::kNo_Config;
56 }
57 virtual SkImageInfo imageInfo() const SK_OVERRIDE {
58 return fEmptyBitmap.info();
59 }
60
61#ifdef SK_SUPPORT_LEGACY_WRITEPIXELSCONFIG
62 virtual void writePixels(const SkBitmap& bitmap, int x, int y,
63 SkCanvas::Config8888 config8888) SK_OVERRIDE {
64 NotSupported();
65 }
66#endif
67 virtual GrRenderTarget* accessRenderTarget() SK_OVERRIDE { return NULL; }
68
69protected:
70 virtual bool filterTextFlags(const SkPaint& paint, TextFlags*) SK_OVERRIDE {
71 return false;
72 }
73 virtual void clear(SkColor color) SK_OVERRIDE {
74 NothingToDo();
75 }
76 virtual void drawPaint(const SkDraw& draw, const SkPaint& paint) SK_OVERRIDE {
77 }
78 virtual void drawPoints(const SkDraw& draw, SkCanvas::PointMode mode, size_t count,
79 const SkPoint points[], const SkPaint& paint) SK_OVERRIDE {
80 }
81 virtual void drawRect(const SkDraw& draw, const SkRect& rect,
82 const SkPaint& paint) SK_OVERRIDE {
83 }
84 virtual void drawOval(const SkDraw& draw, const SkRect& rect,
85 const SkPaint& paint) SK_OVERRIDE {
86 }
87 virtual void drawRRect(const SkDraw& draw, const SkRRect& rrect,
88 const SkPaint& paint) SK_OVERRIDE {
89 }
90 virtual void drawPath(const SkDraw& draw, const SkPath& path,
91 const SkPaint& paint, const SkMatrix* prePathMatrix,
92 bool pathIsMutable) SK_OVERRIDE {
93 }
94 virtual void drawBitmap(const SkDraw& draw, const SkBitmap& bitmap,
95 const SkMatrix& matrix, const SkPaint& paint) SK_OVERRIDE {
96 }
97 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
98 int x, int y, const SkPaint& paint) SK_OVERRIDE {
99 }
100 virtual void drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
101 const SkRect* srcOrNull, const SkRect& dst,
102 const SkPaint& paint,
103 SkCanvas::DrawBitmapRectFlags flags) SK_OVERRIDE {
104 }
105 virtual void drawText(const SkDraw& draw, const void* text, size_t len,
106 SkScalar x, SkScalar y,
107 const SkPaint& paint) SK_OVERRIDE {
108 }
109 virtual void drawPosText(const SkDraw& draw, const void* text, size_t len,
110 const SkScalar pos[], SkScalar constY,
111 int scalarsPerPos, const SkPaint& paint) SK_OVERRIDE {
112 }
113 virtual void drawTextOnPath(const SkDraw& draw, const void* text, size_t len,
114 const SkPath& path, const SkMatrix* matrix,
115 const SkPaint& paint) SK_OVERRIDE {
116 }
117 virtual void drawVertices(const SkDraw& draw, SkCanvas::VertexMode, int vertexCount,
118 const SkPoint verts[], const SkPoint texs[],
119 const SkColor colors[], SkXfermode* xmode,
120 const uint16_t indices[], int indexCount,
121 const SkPaint& paint) SK_OVERRIDE {
122 }
123 virtual void drawDevice(const SkDraw& draw, SkBaseDevice* deviceIn, int x, int y,
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000124 const SkPaint& paint) SK_OVERRIDE {
125 // deviceIn is the one that is being "restored" back to its parent
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +0000126 GrGatherDevice* device = static_cast<GrGatherDevice*>(deviceIn);
127
128 if (device->fAlreadyDrawn) {
129 return;
130 }
131
132 device->fInfo.fRestoreOpID = fPicture->EXPERIMENTAL_curOpID();
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000133 device->fInfo.fCTM = *draw.fMatrix;
134 device->fInfo.fCTM.postTranslate(SkIntToScalar(-device->getOrigin().fX),
135 SkIntToScalar(-device->getOrigin().fY));
136
commit-bot@chromium.orgf97d65d2014-05-08 23:24:05 +0000137 device->fInfo.fOffset = device->getOrigin();
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000138
139 if (NeedsDeepCopy(paint)) {
140 // This NULL acts as a signal that the paint was uncopyable (for now)
141 device->fInfo.fPaint = NULL;
142 device->fInfo.fValid = false;
143 } else {
144 device->fInfo.fPaint = SkNEW_ARGS(SkPaint, (paint));
145 }
146
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +0000147 fAccelData->addSaveLayerInfo(device->fInfo);
148 device->fAlreadyDrawn = true;
149 }
150 // TODO: allow this call to return failure, or move to SkBitmapDevice only.
151 virtual const SkBitmap& onAccessBitmap() SK_OVERRIDE {
152 return fEmptyBitmap;
153 }
154#ifdef SK_SUPPORT_LEGACY_READPIXELSCONFIG
155 virtual bool onReadPixels(const SkBitmap& bitmap,
156 int x, int y,
157 SkCanvas::Config8888 config8888) SK_OVERRIDE {
158 NotSupported();
159 return false;
160 }
161#endif
162 virtual void lockPixels() SK_OVERRIDE { NothingToDo(); }
163 virtual void unlockPixels() SK_OVERRIDE { NothingToDo(); }
164 virtual bool allowImageFilter(const SkImageFilter*) SK_OVERRIDE { return false; }
165 virtual bool canHandleImageFilter(const SkImageFilter*) SK_OVERRIDE { return false; }
166 virtual bool filterImage(const SkImageFilter*, const SkBitmap&, const SkImageFilter::Context&,
167 SkBitmap* result, SkIPoint* offset) SK_OVERRIDE {
168 return false;
169 }
170
171private:
172 // The picture being processed
173 SkPicture *fPicture;
174
175 SkBitmap fEmptyBitmap; // legacy -- need to remove
176
177 // All information gathered during the gather process is stored here
178 GPUAccelData* fAccelData;
179
180 // true if this device has already been drawn back to its parent(s) at least
181 // once.
182 bool fAlreadyDrawn;
183
184 // The information regarding the saveLayer call this device represents.
185 GPUAccelData::SaveLayerInfo fInfo;
186
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000187 // The depth of this device in the saveLayer stack
188 int fSaveLayerDepth;
189
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +0000190 virtual void replaceBitmapBackendForRasterSurface(const SkBitmap&) SK_OVERRIDE {
191 NotSupported();
192 }
193
194 virtual SkBaseDevice* onCreateDevice(const SkImageInfo& info, Usage usage) SK_OVERRIDE {
195 // we expect to only get called via savelayer, in which case it is fine.
196 SkASSERT(kSaveLayer_Usage == usage);
197
198 fInfo.fHasNestedLayers = true;
skia.committer@gmail.coma5b068c2014-05-07 03:04:15 +0000199 return SkNEW_ARGS(GrGatherDevice, (info.width(), info.height(), fPicture,
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000200 fAccelData, fSaveLayerDepth+1));
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +0000201 }
202
203 virtual void flush() SK_OVERRIDE {}
204
205 static void NotSupported() {
206 SkDEBUGFAIL("this method should never be called");
207 }
208
209 static void NothingToDo() {}
210
211 typedef SkBaseDevice INHERITED;
212};
213
214// The GrGatherCanvas allows saveLayers but simplifies clipping. It is really
215// only intended to be used as:
216//
217// GrGatherDevice dev(w, h, picture, accelData);
218// GrGatherCanvas canvas(..., picture);
219// canvas.gather();
skia.committer@gmail.com2c48ee82014-04-01 03:07:47 +0000220//
221// which is all just to fill in 'accelData'
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +0000222class SK_API GrGatherCanvas : public SkCanvas {
223public:
skia.committer@gmail.com2c48ee82014-04-01 03:07:47 +0000224 GrGatherCanvas(GrGatherDevice* device, SkPicture* pict)
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +0000225 : INHERITED(device)
226 , fPicture(pict) {
227 }
228
229 void gather() {
230 if (NULL == fPicture || 0 == fPicture->width() || 0 == fPicture->height()) {
231 return;
232 }
233
234 this->clipRect(SkRect::MakeWH(SkIntToScalar(fPicture->width()),
235 SkIntToScalar(fPicture->height())),
236 SkRegion::kIntersect_Op, false);
237 this->drawPicture(*fPicture);
238 }
239
240 virtual void drawPicture(SkPicture& picture) SK_OVERRIDE {
robertphillips@google.combeb1af22014-05-07 21:31:09 +0000241 // BBH-based rendering doesn't re-issue many of the operations the gather
242 // process cares about (e.g., saves and restores) so it must be disabled.
243 if (NULL != picture.fPlayback) {
244 picture.fPlayback->setUseBBH(false);
245 }
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +0000246 picture.draw(this);
robertphillips@google.combeb1af22014-05-07 21:31:09 +0000247 if (NULL != picture.fPlayback) {
248 picture.fPlayback->setUseBBH(true);
249 }
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +0000250 }
251protected:
252 // disable aa for speed
253 virtual void onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle) SK_OVERRIDE {
254 this->INHERITED::onClipRect(rect, op, kHard_ClipEdgeStyle);
255 }
256
257 // for speed, just respect the bounds, and disable AA. May give us a few
258 // false positives and negatives.
259 virtual void onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle) SK_OVERRIDE {
260 this->updateClipConservativelyUsingBounds(path.getBounds(), op,
261 path.isInverseFillType());
262 }
263 virtual void onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle) SK_OVERRIDE {
264 this->updateClipConservativelyUsingBounds(rrect.getBounds(), op, false);
265 }
266
267private:
268 SkPicture* fPicture;
269
270 typedef SkCanvas INHERITED;
271};
272
skia.committer@gmail.com2c48ee82014-04-01 03:07:47 +0000273// GatherGPUInfo is only intended to be called within the context of SkGpuDevice's
274// EXPERIMENTAL_optimize method.
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +0000275void GatherGPUInfo(SkPicture* pict, GPUAccelData* accelData) {
276 if (0 == pict->width() || 0 == pict->height()) {
277 return ;
278 }
279
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +0000280 GrGatherDevice device(pict->width(), pict->height(), pict, accelData, 0);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +0000281 GrGatherCanvas canvas(&device, pict);
282
283 canvas.gather();
284}