| commit-bot@chromium.org | 8ddc26b | 2014-03-31 17:55:12 +0000 | [diff] [blame] | 1 | /* |
| 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" |
| 10 | |
| skia.committer@gmail.com | 2c48ee8 | 2014-04-01 03:07:47 +0000 | [diff] [blame] | 11 | // The GrGather device performs GPU-backend-specific preprocessing on |
| commit-bot@chromium.org | 8ddc26b | 2014-03-31 17:55:12 +0000 | [diff] [blame] | 12 | // a picture. The results are stored in a GPUAccelData. |
| 13 | // |
| 14 | // Currently the only interesting work is done in drawDevice (i.e., when a |
| 15 | // saveLayer is collapsed back into its parent) and, maybe, in onCreateDevice. |
| 16 | // All the current work could be done much more efficiently by just traversing the |
| 17 | // raw op codes in the SkPicture (although we would still need to replay all the |
| 18 | // clip calls). |
| 19 | class GrGatherDevice : public SkBaseDevice { |
| 20 | public: |
| 21 | SK_DECLARE_INST_COUNT(GrGatherDevice) |
| 22 | |
| 23 | GrGatherDevice(int width, int height, SkPicture* picture, GPUAccelData* accelData) { |
| 24 | fPicture = picture; |
| 25 | fInfo.fSize.set(width, height); |
| 26 | fInfo.fSaveLayerOpID = fPicture->EXPERIMENTAL_curOpID(); |
| 27 | fInfo.fRestoreOpID = 0; |
| 28 | fInfo.fHasNestedLayers = false; |
| 29 | |
| skia.committer@gmail.com | 2c48ee8 | 2014-04-01 03:07:47 +0000 | [diff] [blame] | 30 | fEmptyBitmap.setConfig(SkImageInfo::Make(fInfo.fSize.fWidth, |
| commit-bot@chromium.org | 8ddc26b | 2014-03-31 17:55:12 +0000 | [diff] [blame] | 31 | fInfo.fSize.fHeight, |
| 32 | kUnknown_SkColorType, |
| 33 | kIgnore_SkAlphaType)); |
| 34 | fAccelData = accelData; |
| 35 | fAlreadyDrawn = false; |
| 36 | } |
| 37 | |
| 38 | virtual ~GrGatherDevice() { } |
| 39 | |
| 40 | virtual int width() const SK_OVERRIDE { return fInfo.fSize.width(); } |
| 41 | virtual int height() const SK_OVERRIDE { return fInfo.fSize.height(); } |
| 42 | virtual bool isOpaque() const SK_OVERRIDE { return false; } |
| 43 | virtual SkBitmap::Config config() const SK_OVERRIDE { |
| 44 | return SkBitmap::kNo_Config; |
| 45 | } |
| 46 | virtual SkImageInfo imageInfo() const SK_OVERRIDE { |
| 47 | return fEmptyBitmap.info(); |
| 48 | } |
| 49 | |
| 50 | #ifdef SK_SUPPORT_LEGACY_WRITEPIXELSCONFIG |
| 51 | virtual void writePixels(const SkBitmap& bitmap, int x, int y, |
| 52 | SkCanvas::Config8888 config8888) SK_OVERRIDE { |
| 53 | NotSupported(); |
| 54 | } |
| 55 | #endif |
| 56 | virtual GrRenderTarget* accessRenderTarget() SK_OVERRIDE { return NULL; } |
| 57 | |
| 58 | protected: |
| 59 | virtual bool filterTextFlags(const SkPaint& paint, TextFlags*) SK_OVERRIDE { |
| 60 | return false; |
| 61 | } |
| 62 | virtual void clear(SkColor color) SK_OVERRIDE { |
| 63 | NothingToDo(); |
| 64 | } |
| 65 | virtual void drawPaint(const SkDraw& draw, const SkPaint& paint) SK_OVERRIDE { |
| 66 | } |
| 67 | virtual void drawPoints(const SkDraw& draw, SkCanvas::PointMode mode, size_t count, |
| 68 | const SkPoint points[], const SkPaint& paint) SK_OVERRIDE { |
| 69 | } |
| 70 | virtual void drawRect(const SkDraw& draw, const SkRect& rect, |
| 71 | const SkPaint& paint) SK_OVERRIDE { |
| 72 | } |
| 73 | virtual void drawOval(const SkDraw& draw, const SkRect& rect, |
| 74 | const SkPaint& paint) SK_OVERRIDE { |
| 75 | } |
| 76 | virtual void drawRRect(const SkDraw& draw, const SkRRect& rrect, |
| 77 | const SkPaint& paint) SK_OVERRIDE { |
| 78 | } |
| 79 | virtual void drawPath(const SkDraw& draw, const SkPath& path, |
| 80 | const SkPaint& paint, const SkMatrix* prePathMatrix, |
| 81 | bool pathIsMutable) SK_OVERRIDE { |
| 82 | } |
| 83 | virtual void drawBitmap(const SkDraw& draw, const SkBitmap& bitmap, |
| 84 | const SkMatrix& matrix, const SkPaint& paint) SK_OVERRIDE { |
| 85 | } |
| 86 | virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap, |
| 87 | int x, int y, const SkPaint& paint) SK_OVERRIDE { |
| 88 | } |
| 89 | virtual void drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap, |
| 90 | const SkRect* srcOrNull, const SkRect& dst, |
| 91 | const SkPaint& paint, |
| 92 | SkCanvas::DrawBitmapRectFlags flags) SK_OVERRIDE { |
| 93 | } |
| 94 | virtual void drawText(const SkDraw& draw, const void* text, size_t len, |
| 95 | SkScalar x, SkScalar y, |
| 96 | const SkPaint& paint) SK_OVERRIDE { |
| 97 | } |
| 98 | virtual void drawPosText(const SkDraw& draw, const void* text, size_t len, |
| 99 | const SkScalar pos[], SkScalar constY, |
| 100 | int scalarsPerPos, const SkPaint& paint) SK_OVERRIDE { |
| 101 | } |
| 102 | virtual void drawTextOnPath(const SkDraw& draw, const void* text, size_t len, |
| 103 | const SkPath& path, const SkMatrix* matrix, |
| 104 | const SkPaint& paint) SK_OVERRIDE { |
| 105 | } |
| 106 | virtual void drawVertices(const SkDraw& draw, SkCanvas::VertexMode, int vertexCount, |
| 107 | const SkPoint verts[], const SkPoint texs[], |
| 108 | const SkColor colors[], SkXfermode* xmode, |
| 109 | const uint16_t indices[], int indexCount, |
| 110 | const SkPaint& paint) SK_OVERRIDE { |
| 111 | } |
| 112 | virtual void drawDevice(const SkDraw& draw, SkBaseDevice* deviceIn, int x, int y, |
| 113 | const SkPaint&) SK_OVERRIDE { |
| 114 | GrGatherDevice* device = static_cast<GrGatherDevice*>(deviceIn); |
| 115 | |
| 116 | if (device->fAlreadyDrawn) { |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | device->fInfo.fRestoreOpID = fPicture->EXPERIMENTAL_curOpID(); |
| 121 | fAccelData->addSaveLayerInfo(device->fInfo); |
| 122 | device->fAlreadyDrawn = true; |
| 123 | } |
| 124 | // TODO: allow this call to return failure, or move to SkBitmapDevice only. |
| 125 | virtual const SkBitmap& onAccessBitmap() SK_OVERRIDE { |
| 126 | return fEmptyBitmap; |
| 127 | } |
| 128 | #ifdef SK_SUPPORT_LEGACY_READPIXELSCONFIG |
| 129 | virtual bool onReadPixels(const SkBitmap& bitmap, |
| 130 | int x, int y, |
| 131 | SkCanvas::Config8888 config8888) SK_OVERRIDE { |
| 132 | NotSupported(); |
| 133 | return false; |
| 134 | } |
| 135 | #endif |
| 136 | virtual void lockPixels() SK_OVERRIDE { NothingToDo(); } |
| 137 | virtual void unlockPixels() SK_OVERRIDE { NothingToDo(); } |
| 138 | virtual bool allowImageFilter(const SkImageFilter*) SK_OVERRIDE { return false; } |
| 139 | virtual bool canHandleImageFilter(const SkImageFilter*) SK_OVERRIDE { return false; } |
| 140 | virtual bool filterImage(const SkImageFilter*, const SkBitmap&, const SkImageFilter::Context&, |
| 141 | SkBitmap* result, SkIPoint* offset) SK_OVERRIDE { |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | private: |
| 146 | // The picture being processed |
| 147 | SkPicture *fPicture; |
| 148 | |
| 149 | SkBitmap fEmptyBitmap; // legacy -- need to remove |
| 150 | |
| 151 | // All information gathered during the gather process is stored here |
| 152 | GPUAccelData* fAccelData; |
| 153 | |
| 154 | // true if this device has already been drawn back to its parent(s) at least |
| 155 | // once. |
| 156 | bool fAlreadyDrawn; |
| 157 | |
| 158 | // The information regarding the saveLayer call this device represents. |
| 159 | GPUAccelData::SaveLayerInfo fInfo; |
| 160 | |
| 161 | virtual void replaceBitmapBackendForRasterSurface(const SkBitmap&) SK_OVERRIDE { |
| 162 | NotSupported(); |
| 163 | } |
| 164 | |
| 165 | virtual SkBaseDevice* onCreateDevice(const SkImageInfo& info, Usage usage) SK_OVERRIDE { |
| 166 | // we expect to only get called via savelayer, in which case it is fine. |
| 167 | SkASSERT(kSaveLayer_Usage == usage); |
| 168 | |
| 169 | fInfo.fHasNestedLayers = true; |
| 170 | return SkNEW_ARGS(GrGatherDevice, (info.width(), info.height(), fPicture, fAccelData)); |
| 171 | } |
| 172 | |
| 173 | virtual void flush() SK_OVERRIDE {} |
| 174 | |
| 175 | static void NotSupported() { |
| 176 | SkDEBUGFAIL("this method should never be called"); |
| 177 | } |
| 178 | |
| 179 | static void NothingToDo() {} |
| 180 | |
| 181 | typedef SkBaseDevice INHERITED; |
| 182 | }; |
| 183 | |
| 184 | // The GrGatherCanvas allows saveLayers but simplifies clipping. It is really |
| 185 | // only intended to be used as: |
| 186 | // |
| 187 | // GrGatherDevice dev(w, h, picture, accelData); |
| 188 | // GrGatherCanvas canvas(..., picture); |
| 189 | // canvas.gather(); |
| skia.committer@gmail.com | 2c48ee8 | 2014-04-01 03:07:47 +0000 | [diff] [blame] | 190 | // |
| 191 | // which is all just to fill in 'accelData' |
| commit-bot@chromium.org | 8ddc26b | 2014-03-31 17:55:12 +0000 | [diff] [blame] | 192 | class SK_API GrGatherCanvas : public SkCanvas { |
| 193 | public: |
| skia.committer@gmail.com | 2c48ee8 | 2014-04-01 03:07:47 +0000 | [diff] [blame] | 194 | GrGatherCanvas(GrGatherDevice* device, SkPicture* pict) |
| commit-bot@chromium.org | 8ddc26b | 2014-03-31 17:55:12 +0000 | [diff] [blame] | 195 | : INHERITED(device) |
| 196 | , fPicture(pict) { |
| 197 | } |
| 198 | |
| 199 | void gather() { |
| 200 | if (NULL == fPicture || 0 == fPicture->width() || 0 == fPicture->height()) { |
| 201 | return; |
| 202 | } |
| 203 | |
| 204 | this->clipRect(SkRect::MakeWH(SkIntToScalar(fPicture->width()), |
| 205 | SkIntToScalar(fPicture->height())), |
| 206 | SkRegion::kIntersect_Op, false); |
| 207 | this->drawPicture(*fPicture); |
| 208 | } |
| 209 | |
| 210 | virtual void drawPicture(SkPicture& picture) SK_OVERRIDE { |
| 211 | picture.draw(this); |
| 212 | } |
| 213 | protected: |
| 214 | // disable aa for speed |
| 215 | virtual void onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle) SK_OVERRIDE { |
| 216 | this->INHERITED::onClipRect(rect, op, kHard_ClipEdgeStyle); |
| 217 | } |
| 218 | |
| 219 | // for speed, just respect the bounds, and disable AA. May give us a few |
| 220 | // false positives and negatives. |
| 221 | virtual void onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle) SK_OVERRIDE { |
| 222 | this->updateClipConservativelyUsingBounds(path.getBounds(), op, |
| 223 | path.isInverseFillType()); |
| 224 | } |
| 225 | virtual void onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle) SK_OVERRIDE { |
| 226 | this->updateClipConservativelyUsingBounds(rrect.getBounds(), op, false); |
| 227 | } |
| 228 | |
| 229 | private: |
| 230 | SkPicture* fPicture; |
| 231 | |
| 232 | typedef SkCanvas INHERITED; |
| 233 | }; |
| 234 | |
| skia.committer@gmail.com | 2c48ee8 | 2014-04-01 03:07:47 +0000 | [diff] [blame] | 235 | // GatherGPUInfo is only intended to be called within the context of SkGpuDevice's |
| 236 | // EXPERIMENTAL_optimize method. |
| commit-bot@chromium.org | 8ddc26b | 2014-03-31 17:55:12 +0000 | [diff] [blame] | 237 | void GatherGPUInfo(SkPicture* pict, GPUAccelData* accelData) { |
| 238 | if (0 == pict->width() || 0 == pict->height()) { |
| 239 | return ; |
| 240 | } |
| 241 | |
| 242 | GrGatherDevice device(pict->width(), pict->height(), pict, accelData); |
| 243 | GrGatherCanvas canvas(&device, pict); |
| 244 | |
| 245 | canvas.gather(); |
| 246 | } |