reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 1 | /* |
| 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.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 8 | #include "SkBitmapDevice.h" |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 9 | #include "SkCanvas.h" |
| 10 | #include "SkData.h" |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 11 | #include "SkPictureUtils.h" |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 12 | #include "SkPixelRef.h" |
junov@chromium.org | 50a5cfb | 2013-02-01 20:39:48 +0000 | [diff] [blame] | 13 | #include "SkRRect.h" |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 14 | #include "SkShader.h" |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 15 | |
| 16 | class PixelRefSet { |
| 17 | public: |
| 18 | PixelRefSet(SkTDArray<SkPixelRef*>* array) : fArray(array) {} |
| 19 | |
| 20 | // This does a linear search on existing pixelrefs, so if this list gets big |
| 21 | // we should use a more complex sorted/hashy thing. |
| 22 | // |
| 23 | void add(SkPixelRef* pr) { |
| 24 | uint32_t genID = pr->getGenerationID(); |
| 25 | if (fGenID.find(genID) < 0) { |
| 26 | *fArray->append() = pr; |
| 27 | *fGenID.append() = genID; |
| 28 | // SkDebugf("--- adding [%d] %x %d\n", fArray->count() - 1, pr, genID); |
| 29 | } else { |
| 30 | // SkDebugf("--- already have %x %d\n", pr, genID); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | private: |
| 35 | SkTDArray<SkPixelRef*>* fArray; |
| 36 | SkTDArray<uint32_t> fGenID; |
| 37 | }; |
| 38 | |
| 39 | static void not_supported() { |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 40 | SkDEBUGFAIL("this method should never be called"); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | static void nothing_to_do() {} |
| 44 | |
| 45 | /** |
| 46 | * This device will route all bitmaps (primitives and in shaders) to its PRSet. |
| 47 | * It should never actually draw anything, so there need not be any pixels |
reed@google.com | 3f4bf51 | 2013-11-12 22:14:08 +0000 | [diff] [blame^] | 48 | * behind its device. |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 49 | */ |
reed@google.com | 3f4bf51 | 2013-11-12 22:14:08 +0000 | [diff] [blame^] | 50 | class GatherPixelRefDevice : public SkBaseDevice { |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 51 | public: |
reed@google.com | 3f4bf51 | 2013-11-12 22:14:08 +0000 | [diff] [blame^] | 52 | GatherPixelRefDevice(int width, int height, PixelRefSet* prset) { |
| 53 | fSize.set(width, height); |
| 54 | fEmptyBitmap.setConfig(SkBitmap::kNo_Config, width, height); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 55 | fPRSet = prset; |
| 56 | } |
| 57 | |
reed@google.com | 3f4bf51 | 2013-11-12 22:14:08 +0000 | [diff] [blame^] | 58 | virtual uint32_t getDeviceCapabilities() SK_OVERRIDE { return 0; } |
| 59 | virtual int width() const SK_OVERRIDE { return fSize.width(); } |
| 60 | virtual int height() const SK_OVERRIDE { return fSize.height(); } |
| 61 | virtual bool isOpaque() const SK_OVERRIDE { return false; } |
| 62 | virtual SkBitmap::Config config() const SK_OVERRIDE { |
| 63 | return SkBitmap::kNo_Config; |
| 64 | } |
| 65 | virtual GrRenderTarget* accessRenderTarget() SK_OVERRIDE { return NULL; } |
| 66 | virtual bool filterTextFlags(const SkPaint& paint, TextFlags*) SK_OVERRIDE { |
| 67 | return true; |
| 68 | } |
| 69 | // TODO: allow this call to return failure, or move to SkBitmapDevice only. |
| 70 | virtual const SkBitmap& onAccessBitmap() SK_OVERRIDE { |
| 71 | return fEmptyBitmap; |
| 72 | } |
| 73 | virtual void lockPixels() SK_OVERRIDE { nothing_to_do(); } |
| 74 | virtual void unlockPixels() SK_OVERRIDE { nothing_to_do(); } |
| 75 | virtual bool allowImageFilter(SkImageFilter*) SK_OVERRIDE { return false; } |
| 76 | virtual bool canHandleImageFilter(SkImageFilter*) SK_OVERRIDE { return false; } |
| 77 | virtual bool filterImage(SkImageFilter*, const SkBitmap&, const SkMatrix&, |
| 78 | SkBitmap* result, SkIPoint* offset) SK_OVERRIDE { |
| 79 | return false; |
| 80 | } |
| 81 | |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 82 | virtual void clear(SkColor color) SK_OVERRIDE { |
| 83 | nothing_to_do(); |
| 84 | } |
| 85 | virtual void writePixels(const SkBitmap& bitmap, int x, int y, |
| 86 | SkCanvas::Config8888 config8888) SK_OVERRIDE { |
| 87 | not_supported(); |
| 88 | } |
skia.committer@gmail.com | c3d7d90 | 2012-11-30 02:01:24 +0000 | [diff] [blame] | 89 | |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 90 | virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE { |
| 91 | this->addBitmapFromPaint(paint); |
| 92 | } |
| 93 | virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count, |
| 94 | const SkPoint[], const SkPaint& paint) SK_OVERRIDE { |
| 95 | this->addBitmapFromPaint(paint); |
| 96 | } |
reed@google.com | 72aa79c | 2013-01-24 18:27:42 +0000 | [diff] [blame] | 97 | virtual void drawRect(const SkDraw&, const SkRect&, |
| 98 | const SkPaint& paint) SK_OVERRIDE { |
| 99 | this->addBitmapFromPaint(paint); |
| 100 | } |
scroggo@google.com | cac8d01 | 2013-11-12 17:10:02 +0000 | [diff] [blame] | 101 | virtual void drawRRect(const SkDraw&, const SkRRect&, |
| 102 | const SkPaint& paint) SK_OVERRIDE { |
| 103 | this->addBitmapFromPaint(paint); |
| 104 | } |
reed@google.com | 72aa79c | 2013-01-24 18:27:42 +0000 | [diff] [blame] | 105 | virtual void drawOval(const SkDraw&, const SkRect&, |
| 106 | const SkPaint& paint) SK_OVERRIDE { |
| 107 | this->addBitmapFromPaint(paint); |
| 108 | } |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 109 | virtual void drawPath(const SkDraw&, const SkPath& path, |
| 110 | const SkPaint& paint, const SkMatrix* prePathMatrix, |
| 111 | bool pathIsMutable) SK_OVERRIDE { |
| 112 | this->addBitmapFromPaint(paint); |
| 113 | } |
| 114 | virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap, |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 115 | const SkMatrix&, const SkPaint&) SK_OVERRIDE { |
| 116 | this->addBitmap(bitmap); |
| 117 | } |
| 118 | virtual void drawBitmapRect(const SkDraw&, const SkBitmap& bitmap, |
| 119 | const SkRect* srcOrNull, const SkRect& dst, |
skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 120 | const SkPaint&, |
commit-bot@chromium.org | eed779d | 2013-08-16 10:24:37 +0000 | [diff] [blame] | 121 | SkCanvas::DrawBitmapRectFlags flags) SK_OVERRIDE { |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 122 | this->addBitmap(bitmap); |
| 123 | } |
| 124 | virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap, |
| 125 | int x, int y, const SkPaint& paint) SK_OVERRIDE { |
| 126 | this->addBitmap(bitmap); |
| 127 | } |
| 128 | virtual void drawText(const SkDraw&, const void* text, size_t len, |
| 129 | SkScalar x, SkScalar y, |
| 130 | const SkPaint& paint) SK_OVERRIDE { |
| 131 | this->addBitmapFromPaint(paint); |
| 132 | } |
| 133 | virtual void drawPosText(const SkDraw&, const void* text, size_t len, |
| 134 | const SkScalar pos[], SkScalar constY, |
| 135 | int, const SkPaint& paint) SK_OVERRIDE { |
| 136 | this->addBitmapFromPaint(paint); |
| 137 | } |
| 138 | virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len, |
| 139 | const SkPath& path, const SkMatrix* matrix, |
| 140 | const SkPaint& paint) SK_OVERRIDE { |
| 141 | this->addBitmapFromPaint(paint); |
| 142 | } |
| 143 | virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount, |
| 144 | const SkPoint verts[], const SkPoint texs[], |
| 145 | const SkColor colors[], SkXfermode* xmode, |
| 146 | const uint16_t indices[], int indexCount, |
| 147 | const SkPaint& paint) SK_OVERRIDE { |
| 148 | this->addBitmapFromPaint(paint); |
| 149 | } |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 150 | virtual void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y, |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 151 | const SkPaint&) SK_OVERRIDE { |
| 152 | nothing_to_do(); |
| 153 | } |
| 154 | |
| 155 | protected: |
| 156 | virtual bool onReadPixels(const SkBitmap& bitmap, |
| 157 | int x, int y, |
| 158 | SkCanvas::Config8888 config8888) SK_OVERRIDE { |
| 159 | not_supported(); |
| 160 | return false; |
| 161 | } |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 162 | |
reed@google.com | 3f4bf51 | 2013-11-12 22:14:08 +0000 | [diff] [blame^] | 163 | virtual void replaceBitmapBackendForRasterSurface(const SkBitmap&) SK_OVERRIDE { |
| 164 | not_supported(); |
| 165 | } |
| 166 | virtual SkBaseDevice* onCreateCompatibleDevice(SkBitmap::Config config, |
| 167 | int width, int height, |
| 168 | bool isOpaque, |
| 169 | Usage usage) SK_OVERRIDE { |
| 170 | // we expect to only get called via savelayer, in which case it is fine. |
| 171 | SkASSERT(kSaveLayer_Usage == usage); |
| 172 | return SkNEW_ARGS(GatherPixelRefDevice, (width, height, fPRSet)); |
| 173 | } |
| 174 | virtual void flush() SK_OVERRIDE {} |
| 175 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 176 | private: |
reed@google.com | 3f4bf51 | 2013-11-12 22:14:08 +0000 | [diff] [blame^] | 177 | PixelRefSet* fPRSet; |
| 178 | SkBitmap fEmptyBitmap; // legacy -- need to remove the need for this guy |
| 179 | SkISize fSize; |
| 180 | |
| 181 | void addBitmap(const SkBitmap& bm) { |
| 182 | fPRSet->add(bm.pixelRef()); |
| 183 | } |
| 184 | |
| 185 | void addBitmapFromPaint(const SkPaint& paint) { |
| 186 | SkShader* shader = paint.getShader(); |
| 187 | if (shader) { |
| 188 | SkBitmap bm; |
| 189 | // Check whether the shader is a gradient in order to short-circuit |
| 190 | // call to asABitmap to prevent generation of bitmaps from |
| 191 | // gradient shaders, which implement asABitmap. |
| 192 | if (SkShader::kNone_GradientType == shader->asAGradient(NULL) && |
| 193 | shader->asABitmap(&bm, NULL, NULL)) { |
| 194 | fPRSet->add(bm.pixelRef()); |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | typedef SkBaseDevice INHERITED; |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 200 | }; |
| 201 | |
| 202 | class NoSaveLayerCanvas : public SkCanvas { |
| 203 | public: |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 204 | NoSaveLayerCanvas(SkBaseDevice* device) : INHERITED(device) {} |
skia.committer@gmail.com | c3d7d90 | 2012-11-30 02:01:24 +0000 | [diff] [blame] | 205 | |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 206 | // turn saveLayer() into save() for speed, should not affect correctness. |
| 207 | virtual int saveLayer(const SkRect* bounds, const SkPaint* paint, |
| 208 | SaveFlags flags) SK_OVERRIDE { |
| 209 | |
| 210 | // Like SkPictureRecord, we don't want to create layers, but we do need |
| 211 | // to respect the save and (possibly) its rect-clip. |
| 212 | |
| 213 | int count = this->INHERITED::save(flags); |
| 214 | if (bounds) { |
| 215 | this->INHERITED::clipRectBounds(bounds, flags, NULL); |
| 216 | } |
| 217 | return count; |
| 218 | } |
| 219 | |
| 220 | // disable aa for speed |
| 221 | virtual bool clipRect(const SkRect& rect, SkRegion::Op op, |
| 222 | bool doAA) SK_OVERRIDE { |
| 223 | return this->INHERITED::clipRect(rect, op, false); |
| 224 | } |
| 225 | |
| 226 | // for speed, just respect the bounds, and disable AA. May give us a few |
| 227 | // false positives and negatives. |
| 228 | virtual bool clipPath(const SkPath& path, SkRegion::Op op, |
| 229 | bool doAA) SK_OVERRIDE { |
junov@chromium.org | ed8d6bb | 2013-05-29 19:09:48 +0000 | [diff] [blame] | 230 | return this->updateClipConservativelyUsingBounds(path.getBounds(), op, path.isInverseFillType()); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 231 | } |
junov@chromium.org | 50a5cfb | 2013-02-01 20:39:48 +0000 | [diff] [blame] | 232 | virtual bool clipRRect(const SkRRect& rrect, SkRegion::Op op, |
| 233 | bool doAA) SK_OVERRIDE { |
junov@chromium.org | ed8d6bb | 2013-05-29 19:09:48 +0000 | [diff] [blame] | 234 | return this->updateClipConservativelyUsingBounds(rrect.getBounds(), op, false); |
junov@chromium.org | 50a5cfb | 2013-02-01 20:39:48 +0000 | [diff] [blame] | 235 | } |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 236 | |
| 237 | private: |
| 238 | typedef SkCanvas INHERITED; |
| 239 | }; |
| 240 | |
| 241 | SkData* SkPictureUtils::GatherPixelRefs(SkPicture* pict, const SkRect& area) { |
| 242 | if (NULL == pict) { |
| 243 | return NULL; |
| 244 | } |
| 245 | |
| 246 | // this test also handles if either area or pict's width/height are empty |
| 247 | if (!SkRect::Intersects(area, |
| 248 | SkRect::MakeWH(SkIntToScalar(pict->width()), |
| 249 | SkIntToScalar(pict->height())))) { |
| 250 | return NULL; |
| 251 | } |
| 252 | |
| 253 | SkTDArray<SkPixelRef*> array; |
| 254 | PixelRefSet prset(&array); |
| 255 | |
reed@google.com | 3f4bf51 | 2013-11-12 22:14:08 +0000 | [diff] [blame^] | 256 | GatherPixelRefDevice device(pict->width(), pict->height(), &prset); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 257 | NoSaveLayerCanvas canvas(&device); |
| 258 | |
| 259 | canvas.clipRect(area, SkRegion::kIntersect_Op, false); |
| 260 | canvas.drawPicture(*pict); |
| 261 | |
| 262 | SkData* data = NULL; |
| 263 | int count = array.count(); |
| 264 | if (count > 0) { |
| 265 | data = SkData::NewFromMalloc(array.detach(), count * sizeof(SkPixelRef*)); |
| 266 | } |
| 267 | return data; |
| 268 | } |