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 | |
| 8 | #include "SkPictureUtils.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkData.h" |
| 11 | #include "SkDevice.h" |
| 12 | #include "SkPixelRef.h" |
| 13 | #include "SkShader.h" |
| 14 | |
| 15 | class PixelRefSet { |
| 16 | public: |
| 17 | PixelRefSet(SkTDArray<SkPixelRef*>* array) : fArray(array) {} |
| 18 | |
| 19 | // This does a linear search on existing pixelrefs, so if this list gets big |
| 20 | // we should use a more complex sorted/hashy thing. |
| 21 | // |
| 22 | void add(SkPixelRef* pr) { |
| 23 | uint32_t genID = pr->getGenerationID(); |
| 24 | if (fGenID.find(genID) < 0) { |
| 25 | *fArray->append() = pr; |
| 26 | *fGenID.append() = genID; |
| 27 | // SkDebugf("--- adding [%d] %x %d\n", fArray->count() - 1, pr, genID); |
| 28 | } else { |
| 29 | // SkDebugf("--- already have %x %d\n", pr, genID); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | private: |
| 34 | SkTDArray<SkPixelRef*>* fArray; |
| 35 | SkTDArray<uint32_t> fGenID; |
| 36 | }; |
| 37 | |
| 38 | static void not_supported() { |
| 39 | SkASSERT(!"this method should never be called"); |
| 40 | } |
| 41 | |
| 42 | static void nothing_to_do() {} |
| 43 | |
| 44 | /** |
| 45 | * This device will route all bitmaps (primitives and in shaders) to its PRSet. |
| 46 | * It should never actually draw anything, so there need not be any pixels |
| 47 | * behind its device-bitmap. |
| 48 | */ |
| 49 | class GatherPixelRefDevice : public SkDevice { |
| 50 | private: |
| 51 | PixelRefSet* fPRSet; |
| 52 | |
| 53 | void addBitmap(const SkBitmap& bm) { |
| 54 | fPRSet->add(bm.pixelRef()); |
| 55 | } |
| 56 | |
| 57 | void addBitmapFromPaint(const SkPaint& paint) { |
| 58 | SkShader* shader = paint.getShader(); |
| 59 | if (shader) { |
| 60 | SkBitmap bm; |
| 61 | if (shader->asABitmap(&bm, NULL, NULL)) { |
| 62 | fPRSet->add(bm.pixelRef()); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | public: |
| 68 | GatherPixelRefDevice(const SkBitmap& bm, PixelRefSet* prset) : SkDevice(bm) { |
| 69 | fPRSet = prset; |
| 70 | } |
| 71 | |
| 72 | virtual void clear(SkColor color) SK_OVERRIDE { |
| 73 | nothing_to_do(); |
| 74 | } |
| 75 | virtual void writePixels(const SkBitmap& bitmap, int x, int y, |
| 76 | SkCanvas::Config8888 config8888) SK_OVERRIDE { |
| 77 | not_supported(); |
| 78 | } |
| 79 | |
| 80 | virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE { |
| 81 | this->addBitmapFromPaint(paint); |
| 82 | } |
| 83 | virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count, |
| 84 | const SkPoint[], const SkPaint& paint) SK_OVERRIDE { |
| 85 | this->addBitmapFromPaint(paint); |
| 86 | } |
| 87 | virtual void drawRect(const SkDraw&, const SkRect& r, |
| 88 | const SkPaint& paint) SK_OVERRIDE { |
| 89 | this->addBitmapFromPaint(paint); |
| 90 | } |
| 91 | virtual void drawPath(const SkDraw&, const SkPath& path, |
| 92 | const SkPaint& paint, const SkMatrix* prePathMatrix, |
| 93 | bool pathIsMutable) SK_OVERRIDE { |
| 94 | this->addBitmapFromPaint(paint); |
| 95 | } |
| 96 | virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap, |
| 97 | const SkIRect* srcRectOrNull, |
| 98 | const SkMatrix&, const SkPaint&) SK_OVERRIDE { |
| 99 | this->addBitmap(bitmap); |
| 100 | } |
| 101 | virtual void drawBitmapRect(const SkDraw&, const SkBitmap& bitmap, |
| 102 | const SkRect* srcOrNull, const SkRect& dst, |
| 103 | const SkPaint&) SK_OVERRIDE { |
| 104 | this->addBitmap(bitmap); |
| 105 | } |
| 106 | virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap, |
| 107 | int x, int y, const SkPaint& paint) SK_OVERRIDE { |
| 108 | this->addBitmap(bitmap); |
| 109 | } |
| 110 | virtual void drawText(const SkDraw&, const void* text, size_t len, |
| 111 | SkScalar x, SkScalar y, |
| 112 | const SkPaint& paint) SK_OVERRIDE { |
| 113 | this->addBitmapFromPaint(paint); |
| 114 | } |
| 115 | virtual void drawPosText(const SkDraw&, const void* text, size_t len, |
| 116 | const SkScalar pos[], SkScalar constY, |
| 117 | int, const SkPaint& paint) SK_OVERRIDE { |
| 118 | this->addBitmapFromPaint(paint); |
| 119 | } |
| 120 | virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len, |
| 121 | const SkPath& path, const SkMatrix* matrix, |
| 122 | const SkPaint& paint) SK_OVERRIDE { |
| 123 | this->addBitmapFromPaint(paint); |
| 124 | } |
| 125 | virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount, |
| 126 | const SkPoint verts[], const SkPoint texs[], |
| 127 | const SkColor colors[], SkXfermode* xmode, |
| 128 | const uint16_t indices[], int indexCount, |
| 129 | const SkPaint& paint) SK_OVERRIDE { |
| 130 | this->addBitmapFromPaint(paint); |
| 131 | } |
| 132 | virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y, |
| 133 | const SkPaint&) SK_OVERRIDE { |
| 134 | nothing_to_do(); |
| 135 | } |
| 136 | |
| 137 | protected: |
| 138 | virtual bool onReadPixels(const SkBitmap& bitmap, |
| 139 | int x, int y, |
| 140 | SkCanvas::Config8888 config8888) SK_OVERRIDE { |
| 141 | not_supported(); |
| 142 | return false; |
| 143 | } |
| 144 | }; |
| 145 | |
| 146 | class NoSaveLayerCanvas : public SkCanvas { |
| 147 | public: |
| 148 | NoSaveLayerCanvas(SkDevice* device) : INHERITED(device) {} |
| 149 | |
| 150 | // turn saveLayer() into save() for speed, should not affect correctness. |
| 151 | virtual int saveLayer(const SkRect* bounds, const SkPaint* paint, |
| 152 | SaveFlags flags) SK_OVERRIDE { |
| 153 | |
| 154 | // Like SkPictureRecord, we don't want to create layers, but we do need |
| 155 | // to respect the save and (possibly) its rect-clip. |
| 156 | |
| 157 | int count = this->INHERITED::save(flags); |
| 158 | if (bounds) { |
| 159 | this->INHERITED::clipRectBounds(bounds, flags, NULL); |
| 160 | } |
| 161 | return count; |
| 162 | } |
| 163 | |
| 164 | // disable aa for speed |
| 165 | virtual bool clipRect(const SkRect& rect, SkRegion::Op op, |
| 166 | bool doAA) SK_OVERRIDE { |
| 167 | return this->INHERITED::clipRect(rect, op, false); |
| 168 | } |
| 169 | |
| 170 | // for speed, just respect the bounds, and disable AA. May give us a few |
| 171 | // false positives and negatives. |
| 172 | virtual bool clipPath(const SkPath& path, SkRegion::Op op, |
| 173 | bool doAA) SK_OVERRIDE { |
| 174 | return this->INHERITED::clipRect(path.getBounds(), op, false); |
| 175 | } |
| 176 | |
| 177 | private: |
| 178 | typedef SkCanvas INHERITED; |
| 179 | }; |
| 180 | |
| 181 | SkData* SkPictureUtils::GatherPixelRefs(SkPicture* pict, const SkRect& area) { |
| 182 | if (NULL == pict) { |
| 183 | return NULL; |
| 184 | } |
| 185 | |
| 186 | // this test also handles if either area or pict's width/height are empty |
| 187 | if (!SkRect::Intersects(area, |
| 188 | SkRect::MakeWH(SkIntToScalar(pict->width()), |
| 189 | SkIntToScalar(pict->height())))) { |
| 190 | return NULL; |
| 191 | } |
| 192 | |
| 193 | SkTDArray<SkPixelRef*> array; |
| 194 | PixelRefSet prset(&array); |
| 195 | |
| 196 | SkBitmap emptyBitmap; |
| 197 | emptyBitmap.setConfig(SkBitmap::kARGB_8888_Config, pict->width(), pict->height()); |
| 198 | // note: we do not set any pixels (shouldn't need to) |
| 199 | |
| 200 | GatherPixelRefDevice device(emptyBitmap, &prset); |
| 201 | NoSaveLayerCanvas canvas(&device); |
| 202 | |
| 203 | canvas.clipRect(area, SkRegion::kIntersect_Op, false); |
| 204 | canvas.drawPicture(*pict); |
| 205 | |
| 206 | SkData* data = NULL; |
| 207 | int count = array.count(); |
| 208 | if (count > 0) { |
| 209 | data = SkData::NewFromMalloc(array.detach(), count * sizeof(SkPixelRef*)); |
| 210 | } |
| 211 | return data; |
| 212 | } |
| 213 | |