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 | 81e8739 | 2014-01-07 16:08:04 +0000 | [diff] [blame] | 11 | #include "SkNoSaveLayerCanvas.h" |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 12 | #include "SkPictureUtils.h" |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 13 | #include "SkPixelRef.h" |
junov@chromium.org | 50a5cfb | 2013-02-01 20:39:48 +0000 | [diff] [blame] | 14 | #include "SkRRect.h" |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 15 | #include "SkShader.h" |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 16 | |
| 17 | class PixelRefSet { |
| 18 | public: |
| 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 | |
| 35 | private: |
| 36 | SkTDArray<SkPixelRef*>* fArray; |
| 37 | SkTDArray<uint32_t> fGenID; |
| 38 | }; |
| 39 | |
| 40 | static void not_supported() { |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 41 | SkDEBUGFAIL("this method should never be called"); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | static 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.com | ec3ca87 | 2013-11-13 16:02:18 +0000 | [diff] [blame] | 49 | * behind its device. |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 50 | */ |
reed@google.com | ec3ca87 | 2013-11-13 16:02:18 +0000 | [diff] [blame] | 51 | class GatherPixelRefDevice : public SkBaseDevice { |
reed@google.com | 4d16470 | 2013-11-12 22:27:30 +0000 | [diff] [blame] | 52 | public: |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 53 | SK_DECLARE_INST_COUNT(GatherPixelRefDevice) |
| 54 | |
reed@google.com | ec3ca87 | 2013-11-13 16:02:18 +0000 | [diff] [blame] | 55 | GatherPixelRefDevice(int width, int height, PixelRefSet* prset) { |
| 56 | fSize.set(width, height); |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame^] | 57 | fEmptyBitmap.setConfig(SkImageInfo::MakeUnknown(width, height)); |
reed@google.com | 4d16470 | 2013-11-12 22:27:30 +0000 | [diff] [blame] | 58 | fPRSet = prset; |
reed@google.com | 3f4bf51 | 2013-11-12 22:14:08 +0000 | [diff] [blame] | 59 | } |
| 60 | |
reed@google.com | ec3ca87 | 2013-11-13 16:02:18 +0000 | [diff] [blame] | 61 | virtual uint32_t getDeviceCapabilities() SK_OVERRIDE { return 0; } |
| 62 | virtual int width() const SK_OVERRIDE { return fSize.width(); } |
| 63 | virtual int height() const SK_OVERRIDE { return fSize.height(); } |
| 64 | virtual bool isOpaque() const SK_OVERRIDE { return false; } |
| 65 | virtual SkBitmap::Config config() const SK_OVERRIDE { |
| 66 | return SkBitmap::kNo_Config; |
| 67 | } |
| 68 | virtual GrRenderTarget* accessRenderTarget() SK_OVERRIDE { return NULL; } |
| 69 | virtual bool filterTextFlags(const SkPaint& paint, TextFlags*) SK_OVERRIDE { |
robertphillips@google.com | 81e8739 | 2014-01-07 16:08:04 +0000 | [diff] [blame] | 70 | return false; |
reed@google.com | ec3ca87 | 2013-11-13 16:02:18 +0000 | [diff] [blame] | 71 | } |
| 72 | // TODO: allow this call to return failure, or move to SkBitmapDevice only. |
| 73 | virtual const SkBitmap& onAccessBitmap() SK_OVERRIDE { |
| 74 | return fEmptyBitmap; |
| 75 | } |
| 76 | virtual void lockPixels() SK_OVERRIDE { nothing_to_do(); } |
| 77 | virtual void unlockPixels() SK_OVERRIDE { nothing_to_do(); } |
commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 78 | virtual bool allowImageFilter(const SkImageFilter*) SK_OVERRIDE { return false; } |
| 79 | virtual bool canHandleImageFilter(const SkImageFilter*) SK_OVERRIDE { return false; } |
| 80 | virtual bool filterImage(const SkImageFilter*, const SkBitmap&, const SkMatrix&, |
reed@google.com | ec3ca87 | 2013-11-13 16:02:18 +0000 | [diff] [blame] | 81 | SkBitmap* result, SkIPoint* offset) SK_OVERRIDE { |
| 82 | return false; |
| 83 | } |
| 84 | |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 85 | virtual void clear(SkColor color) SK_OVERRIDE { |
| 86 | nothing_to_do(); |
| 87 | } |
| 88 | virtual void writePixels(const SkBitmap& bitmap, int x, int y, |
| 89 | SkCanvas::Config8888 config8888) SK_OVERRIDE { |
| 90 | not_supported(); |
| 91 | } |
skia.committer@gmail.com | c3d7d90 | 2012-11-30 02:01:24 +0000 | [diff] [blame] | 92 | |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 93 | virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE { |
| 94 | this->addBitmapFromPaint(paint); |
| 95 | } |
| 96 | virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count, |
| 97 | const SkPoint[], const SkPaint& paint) SK_OVERRIDE { |
| 98 | this->addBitmapFromPaint(paint); |
| 99 | } |
reed@google.com | 72aa79c | 2013-01-24 18:27:42 +0000 | [diff] [blame] | 100 | virtual void drawRect(const SkDraw&, const SkRect&, |
| 101 | const SkPaint& paint) SK_OVERRIDE { |
| 102 | this->addBitmapFromPaint(paint); |
| 103 | } |
scroggo@google.com | cac8d01 | 2013-11-12 17:10:02 +0000 | [diff] [blame] | 104 | virtual void drawRRect(const SkDraw&, const SkRRect&, |
| 105 | const SkPaint& paint) SK_OVERRIDE { |
| 106 | this->addBitmapFromPaint(paint); |
| 107 | } |
reed@google.com | 72aa79c | 2013-01-24 18:27:42 +0000 | [diff] [blame] | 108 | virtual void drawOval(const SkDraw&, const SkRect&, |
| 109 | const SkPaint& paint) SK_OVERRIDE { |
| 110 | this->addBitmapFromPaint(paint); |
| 111 | } |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 112 | virtual void drawPath(const SkDraw&, const SkPath& path, |
| 113 | const SkPaint& paint, const SkMatrix* prePathMatrix, |
| 114 | bool pathIsMutable) SK_OVERRIDE { |
| 115 | this->addBitmapFromPaint(paint); |
| 116 | } |
| 117 | virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap, |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 118 | const SkMatrix&, const SkPaint& paint) SK_OVERRIDE { |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 119 | this->addBitmap(bitmap); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 120 | if (SkBitmap::kA8_Config == bitmap.config()) { |
| 121 | this->addBitmapFromPaint(paint); |
| 122 | } |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 123 | } |
| 124 | virtual void drawBitmapRect(const SkDraw&, const SkBitmap& bitmap, |
| 125 | const SkRect* srcOrNull, const SkRect& dst, |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 126 | const SkPaint& paint, |
commit-bot@chromium.org | eed779d | 2013-08-16 10:24:37 +0000 | [diff] [blame] | 127 | SkCanvas::DrawBitmapRectFlags flags) SK_OVERRIDE { |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 128 | this->addBitmap(bitmap); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 129 | if (SkBitmap::kA8_Config == bitmap.config()) { |
| 130 | this->addBitmapFromPaint(paint); |
| 131 | } |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 132 | } |
| 133 | virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap, |
| 134 | int x, int y, const SkPaint& paint) SK_OVERRIDE { |
| 135 | this->addBitmap(bitmap); |
| 136 | } |
| 137 | virtual void drawText(const SkDraw&, const void* text, size_t len, |
| 138 | SkScalar x, SkScalar y, |
| 139 | const SkPaint& paint) SK_OVERRIDE { |
| 140 | this->addBitmapFromPaint(paint); |
| 141 | } |
| 142 | virtual void drawPosText(const SkDraw&, const void* text, size_t len, |
| 143 | const SkScalar pos[], SkScalar constY, |
| 144 | int, const SkPaint& paint) SK_OVERRIDE { |
| 145 | this->addBitmapFromPaint(paint); |
| 146 | } |
| 147 | virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len, |
| 148 | const SkPath& path, const SkMatrix* matrix, |
| 149 | const SkPaint& paint) SK_OVERRIDE { |
| 150 | this->addBitmapFromPaint(paint); |
| 151 | } |
| 152 | virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount, |
| 153 | const SkPoint verts[], const SkPoint texs[], |
| 154 | const SkColor colors[], SkXfermode* xmode, |
| 155 | const uint16_t indices[], int indexCount, |
| 156 | const SkPaint& paint) SK_OVERRIDE { |
| 157 | this->addBitmapFromPaint(paint); |
| 158 | } |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 159 | virtual void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y, |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 160 | const SkPaint&) SK_OVERRIDE { |
| 161 | nothing_to_do(); |
| 162 | } |
| 163 | |
| 164 | protected: |
| 165 | virtual bool onReadPixels(const SkBitmap& bitmap, |
| 166 | int x, int y, |
| 167 | SkCanvas::Config8888 config8888) SK_OVERRIDE { |
| 168 | not_supported(); |
| 169 | return false; |
| 170 | } |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 171 | |
reed@google.com | ec3ca87 | 2013-11-13 16:02:18 +0000 | [diff] [blame] | 172 | virtual void replaceBitmapBackendForRasterSurface(const SkBitmap&) SK_OVERRIDE { |
| 173 | not_supported(); |
| 174 | } |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 175 | virtual SkBaseDevice* onCreateDevice(const SkImageInfo& info, Usage usage) SK_OVERRIDE { |
reed@google.com | ec3ca87 | 2013-11-13 16:02:18 +0000 | [diff] [blame] | 176 | // we expect to only get called via savelayer, in which case it is fine. |
| 177 | SkASSERT(kSaveLayer_Usage == usage); |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 178 | return SkNEW_ARGS(GatherPixelRefDevice, (info.width(), info.height(), fPRSet)); |
reed@google.com | ec3ca87 | 2013-11-13 16:02:18 +0000 | [diff] [blame] | 179 | } |
| 180 | virtual void flush() SK_OVERRIDE {} |
| 181 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 182 | private: |
reed@google.com | ec3ca87 | 2013-11-13 16:02:18 +0000 | [diff] [blame] | 183 | PixelRefSet* fPRSet; |
| 184 | SkBitmap fEmptyBitmap; // legacy -- need to remove the need for this guy |
| 185 | SkISize fSize; |
| 186 | |
| 187 | void addBitmap(const SkBitmap& bm) { |
| 188 | fPRSet->add(bm.pixelRef()); |
| 189 | } |
| 190 | |
| 191 | void addBitmapFromPaint(const SkPaint& paint) { |
| 192 | SkShader* shader = paint.getShader(); |
| 193 | if (shader) { |
| 194 | SkBitmap bm; |
| 195 | // Check whether the shader is a gradient in order to short-circuit |
| 196 | // call to asABitmap to prevent generation of bitmaps from |
| 197 | // gradient shaders, which implement asABitmap. |
| 198 | if (SkShader::kNone_GradientType == shader->asAGradient(NULL) && |
| 199 | shader->asABitmap(&bm, NULL, NULL)) { |
| 200 | fPRSet->add(bm.pixelRef()); |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | typedef SkBaseDevice INHERITED; |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 206 | }; |
| 207 | |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 208 | SkData* SkPictureUtils::GatherPixelRefs(SkPicture* pict, const SkRect& area) { |
| 209 | if (NULL == pict) { |
| 210 | return NULL; |
| 211 | } |
| 212 | |
| 213 | // this test also handles if either area or pict's width/height are empty |
| 214 | if (!SkRect::Intersects(area, |
| 215 | SkRect::MakeWH(SkIntToScalar(pict->width()), |
| 216 | SkIntToScalar(pict->height())))) { |
| 217 | return NULL; |
| 218 | } |
| 219 | |
| 220 | SkTDArray<SkPixelRef*> array; |
| 221 | PixelRefSet prset(&array); |
| 222 | |
reed@google.com | ec3ca87 | 2013-11-13 16:02:18 +0000 | [diff] [blame] | 223 | GatherPixelRefDevice device(pict->width(), pict->height(), &prset); |
robertphillips@google.com | 81e8739 | 2014-01-07 16:08:04 +0000 | [diff] [blame] | 224 | SkNoSaveLayerCanvas canvas(&device); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 225 | |
| 226 | canvas.clipRect(area, SkRegion::kIntersect_Op, false); |
| 227 | canvas.drawPicture(*pict); |
| 228 | |
| 229 | SkData* data = NULL; |
| 230 | int count = array.count(); |
| 231 | if (count > 0) { |
| 232 | data = SkData::NewFromMalloc(array.detach(), count * sizeof(SkPixelRef*)); |
| 233 | } |
| 234 | return data; |
| 235 | } |