epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 11 | #include "SkCanvas.h" |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 12 | #include "SkData.h" |
reed@google.com | bb6793b | 2011-05-05 15:18:15 +0000 | [diff] [blame] | 13 | #include "SkDevice.h" |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 14 | #include "SkPaint.h" |
reed@google.com | 75a0972 | 2012-05-10 12:56:16 +0000 | [diff] [blame] | 15 | #include "SkPathEffect.h" |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 16 | #include "SkGPipe.h" |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 17 | #include "SkGPipePriv.h" |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 18 | #include "SkImageFilter.h" |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 19 | #include "SkStream.h" |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 20 | #include "SkTSearch.h" |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 21 | #include "SkTypeface.h" |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 22 | #include "SkWriter32.h" |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 23 | #include "SkColorFilter.h" |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 24 | #include "SkDrawLooper.h" |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 25 | #include "SkMaskFilter.h" |
| 26 | #include "SkRasterizer.h" |
| 27 | #include "SkShader.h" |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 28 | #include "SkOrderedWriteBuffer.h" |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 29 | |
| 30 | static SkFlattenable* get_paintflat(const SkPaint& paint, unsigned paintFlat) { |
| 31 | SkASSERT(paintFlat < kCount_PaintFlats); |
| 32 | switch (paintFlat) { |
| 33 | case kColorFilter_PaintFlat: return paint.getColorFilter(); |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 34 | case kDrawLooper_PaintFlat: return paint.getLooper(); |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 35 | case kMaskFilter_PaintFlat: return paint.getMaskFilter(); |
| 36 | case kPathEffect_PaintFlat: return paint.getPathEffect(); |
| 37 | case kRasterizer_PaintFlat: return paint.getRasterizer(); |
| 38 | case kShader_PaintFlat: return paint.getShader(); |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 39 | case kImageFilter_PaintFlat: return paint.getImageFilter(); |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 40 | case kXfermode_PaintFlat: return paint.getXfermode(); |
| 41 | } |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 42 | SkDEBUGFAIL("never gets here"); |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 43 | return NULL; |
| 44 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 45 | |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 46 | static size_t writeTypeface(SkWriter32* writer, SkTypeface* typeface) { |
| 47 | SkASSERT(typeface); |
| 48 | SkDynamicMemoryWStream stream; |
| 49 | typeface->serialize(&stream); |
| 50 | size_t size = stream.getOffset(); |
| 51 | if (writer) { |
| 52 | writer->write32(size); |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 53 | SkAutoDataUnref data(stream.copyToData()); |
scroggo@google.com | 5af9b20 | 2012-06-04 17:17:36 +0000 | [diff] [blame] | 54 | writer->writePad(data.data(), size); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 55 | } |
scroggo@google.com | 5af9b20 | 2012-06-04 17:17:36 +0000 | [diff] [blame] | 56 | return 4 + SkAlign4(size); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 57 | } |
| 58 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 59 | /////////////////////////////////////////////////////////////////////////////// |
| 60 | |
scroggo@google.com | 8ae3c7f | 2012-06-13 17:47:52 +0000 | [diff] [blame] | 61 | /* |
| 62 | * Shared heap for storing large things that can be shared, for a stream |
| 63 | * used by multiple readers. |
| 64 | * TODO: Make the allocations all come from cross process safe address space |
| 65 | * TODO: Store paths (others?) |
| 66 | * TODO: Allow reclaiming of memory. Will require us to know when all readers |
| 67 | * have used the object. |
| 68 | */ |
| 69 | class Heap { |
| 70 | public: |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 71 | Heap(bool shallow) : fCanDoShallowCopies(shallow) {} |
scroggo@google.com | 8ae3c7f | 2012-06-13 17:47:52 +0000 | [diff] [blame] | 72 | ~Heap() { |
| 73 | for (int i = 0; i < fBitmaps.count(); i++) { |
| 74 | delete fBitmaps[i].fBitmap; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * Add a copy of a bitmap to the heap. |
| 80 | * @param bm The SkBitmap to be copied and placed in the heap. |
| 81 | * @return void* Pointer to the heap's copy of the bitmap. If NULL, |
| 82 | * the bitmap could not be copied. |
| 83 | */ |
scroggo@google.com | 4f1f6bf | 2012-07-02 13:35:09 +0000 | [diff] [blame] | 84 | const SkBitmap* addBitmap(const SkBitmap& orig) { |
| 85 | const uint32_t genID = orig.getGenerationID(); |
| 86 | SkPixelRef* sharedPixelRef = NULL; |
scroggo@google.com | 8ae3c7f | 2012-06-13 17:47:52 +0000 | [diff] [blame] | 87 | for (int i = fBitmaps.count() - 1; i >= 0; i--) { |
scroggo@google.com | 6ea165d | 2012-07-03 14:52:08 +0000 | [diff] [blame^] | 88 | SkBitmap* storedBitmap = fBitmaps[i].fBitmap; |
scroggo@google.com | 8ae3c7f | 2012-06-13 17:47:52 +0000 | [diff] [blame] | 89 | if (genID == fBitmaps[i].fGenID) { |
scroggo@google.com | 6ea165d | 2012-07-03 14:52:08 +0000 | [diff] [blame^] | 90 | if (orig.pixelRefOffset() != storedBitmap->pixelRefOffset() |
| 91 | || orig.width() != storedBitmap->width() |
| 92 | || orig.height() != storedBitmap->height()) { |
scroggo@google.com | 4f1f6bf | 2012-07-02 13:35:09 +0000 | [diff] [blame] | 93 | // In this case, the bitmaps share a pixelRef, but have |
scroggo@google.com | 6ea165d | 2012-07-03 14:52:08 +0000 | [diff] [blame^] | 94 | // different offsets or sizes. Keep track of the other |
| 95 | // bitmap so that instead of making another copy of the |
| 96 | // pixelRef we can use the copy we already made. |
| 97 | sharedPixelRef = storedBitmap->pixelRef(); |
scroggo@google.com | 4f1f6bf | 2012-07-02 13:35:09 +0000 | [diff] [blame] | 98 | break; |
| 99 | } |
scroggo@google.com | 6ea165d | 2012-07-03 14:52:08 +0000 | [diff] [blame^] | 100 | return storedBitmap; |
scroggo@google.com | 8ae3c7f | 2012-06-13 17:47:52 +0000 | [diff] [blame] | 101 | } |
| 102 | } |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 103 | SkBitmap* copy; |
| 104 | // If the bitmap is mutable, we still need to do a deep copy, since the |
| 105 | // caller may modify it afterwards. That said, if the bitmap is mutable, |
| 106 | // but has no pixelRef, the copy constructor actually does a deep copy. |
scroggo@google.com | 4f1f6bf | 2012-07-02 13:35:09 +0000 | [diff] [blame] | 107 | if (fCanDoShallowCopies && (orig.isImmutable() || !orig.pixelRef())) { |
| 108 | copy = new SkBitmap(orig); |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 109 | } else { |
scroggo@google.com | 4f1f6bf | 2012-07-02 13:35:09 +0000 | [diff] [blame] | 110 | if (sharedPixelRef != NULL) { |
| 111 | // Do a shallow copy of the bitmap to get the width, height, etc |
| 112 | copy = new SkBitmap(orig); |
| 113 | // Replace the pixelRef with the copy that was already made, and |
| 114 | // use the appropriate offset. |
| 115 | copy->setPixelRef(sharedPixelRef, orig.pixelRefOffset()); |
| 116 | } else { |
| 117 | copy = new SkBitmap(); |
| 118 | if (!orig.copyTo(copy, orig.getConfig())) { |
| 119 | delete copy; |
| 120 | return NULL; |
| 121 | } |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 122 | } |
scroggo@google.com | 8ae3c7f | 2012-06-13 17:47:52 +0000 | [diff] [blame] | 123 | } |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 124 | BitmapInfo* info = fBitmaps.append(); |
| 125 | info->fBitmap = copy; |
| 126 | info->fGenID = genID; |
| 127 | return copy; |
scroggo@google.com | 8ae3c7f | 2012-06-13 17:47:52 +0000 | [diff] [blame] | 128 | } |
| 129 | private: |
| 130 | struct BitmapInfo { |
| 131 | SkBitmap* fBitmap; |
| 132 | // Store the generation ID of the original bitmap, since copying does |
| 133 | // not copy this field, so fBitmap's generation ID will not be useful |
| 134 | // for comparing. |
| 135 | uint32_t fGenID; |
| 136 | }; |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 137 | SkTDArray<BitmapInfo> fBitmaps; |
| 138 | const bool fCanDoShallowCopies; |
scroggo@google.com | 8ae3c7f | 2012-06-13 17:47:52 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | /////////////////////////////////////////////////////////////////////////////// |
| 142 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 143 | class SkGPipeCanvas : public SkCanvas { |
| 144 | public: |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 145 | SkGPipeCanvas(SkGPipeController*, SkWriter32*, SkFactorySet*, uint32_t flags); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 146 | virtual ~SkGPipeCanvas(); |
| 147 | |
| 148 | void finish() { |
| 149 | if (!fDone) { |
reed@google.com | dbccc88 | 2011-07-08 18:53:39 +0000 | [diff] [blame] | 150 | if (this->needOpBytes()) { |
| 151 | this->writeOp(kDone_DrawOp); |
| 152 | this->doNotify(); |
| 153 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 154 | fDone = true; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // overrides from SkCanvas |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 159 | virtual int save(SaveFlags) SK_OVERRIDE; |
| 160 | virtual int saveLayer(const SkRect* bounds, const SkPaint*, |
| 161 | SaveFlags) SK_OVERRIDE; |
| 162 | virtual void restore() SK_OVERRIDE; |
| 163 | virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE; |
| 164 | virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE; |
| 165 | virtual bool rotate(SkScalar degrees) SK_OVERRIDE; |
| 166 | virtual bool skew(SkScalar sx, SkScalar sy) SK_OVERRIDE; |
| 167 | virtual bool concat(const SkMatrix& matrix) SK_OVERRIDE; |
| 168 | virtual void setMatrix(const SkMatrix& matrix) SK_OVERRIDE; |
| 169 | virtual bool clipRect(const SkRect& rect, SkRegion::Op op, |
| 170 | bool doAntiAlias = false) SK_OVERRIDE; |
| 171 | virtual bool clipPath(const SkPath& path, SkRegion::Op op, |
| 172 | bool doAntiAlias = false) SK_OVERRIDE; |
| 173 | virtual bool clipRegion(const SkRegion& region, SkRegion::Op op) SK_OVERRIDE; |
| 174 | virtual void clear(SkColor) SK_OVERRIDE; |
| 175 | virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 176 | virtual void drawPoints(PointMode, size_t count, const SkPoint pts[], |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 177 | const SkPaint&) SK_OVERRIDE; |
| 178 | virtual void drawRect(const SkRect& rect, const SkPaint&) SK_OVERRIDE; |
| 179 | virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 180 | virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top, |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 181 | const SkPaint*) SK_OVERRIDE; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 182 | virtual void drawBitmapRect(const SkBitmap&, const SkIRect* src, |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 183 | const SkRect& dst, const SkPaint*) SK_OVERRIDE; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 184 | virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&, |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 185 | const SkPaint*) SK_OVERRIDE; |
scroggo@google.com | 5a2e879 | 2012-04-20 17:39:51 +0000 | [diff] [blame] | 186 | virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, |
| 187 | const SkRect& dst, const SkPaint* paint = NULL) SK_OVERRIDE; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 188 | virtual void drawSprite(const SkBitmap&, int left, int top, |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 189 | const SkPaint*) SK_OVERRIDE; |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 190 | virtual void drawText(const void* text, size_t byteLength, SkScalar x, |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 191 | SkScalar y, const SkPaint&) SK_OVERRIDE; |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 192 | virtual void drawPosText(const void* text, size_t byteLength, |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 193 | const SkPoint pos[], const SkPaint&) SK_OVERRIDE; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 194 | virtual void drawPosTextH(const void* text, size_t byteLength, |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 195 | const SkScalar xpos[], SkScalar constY, |
| 196 | const SkPaint&) SK_OVERRIDE; |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 197 | virtual void drawTextOnPath(const void* text, size_t byteLength, |
| 198 | const SkPath& path, const SkMatrix* matrix, |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 199 | const SkPaint&) SK_OVERRIDE; |
| 200 | virtual void drawPicture(SkPicture& picture) SK_OVERRIDE; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 201 | virtual void drawVertices(VertexMode, int vertexCount, |
| 202 | const SkPoint vertices[], const SkPoint texs[], |
| 203 | const SkColor colors[], SkXfermode*, |
| 204 | const uint16_t indices[], int indexCount, |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 205 | const SkPaint&) SK_OVERRIDE; |
| 206 | virtual void drawData(const void*, size_t) SK_OVERRIDE; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 207 | |
| 208 | private: |
scroggo@google.com | 8ae3c7f | 2012-06-13 17:47:52 +0000 | [diff] [blame] | 209 | Heap fHeap; |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 210 | SkFactorySet* fFactorySet; // optional, only used if cross-process |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 211 | SkGPipeController* fController; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 212 | SkWriter32& fWriter; |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 213 | size_t fBlockSize; // amount allocated for writer |
| 214 | size_t fBytesNotified; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 215 | bool fDone; |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 216 | uint32_t fFlags; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 217 | |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 218 | SkRefCntSet fTypefaceSet; |
| 219 | |
| 220 | uint32_t getTypefaceID(SkTypeface*); |
| 221 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 222 | inline void writeOp(DrawOps op, unsigned flags, unsigned data) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 223 | fWriter.write32(DrawOp_packOpFlagData(op, flags, data)); |
| 224 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 225 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 226 | inline void writeOp(DrawOps op) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 227 | fWriter.write32(DrawOp_packOpFlagData(op, 0, 0)); |
| 228 | } |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 229 | |
| 230 | bool needOpBytes(size_t size = 0); |
| 231 | |
| 232 | inline void doNotify() { |
| 233 | if (!fDone) { |
| 234 | size_t bytes = fWriter.size() - fBytesNotified; |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 235 | if (bytes > 0) { |
| 236 | fController->notifyWritten(bytes); |
| 237 | fBytesNotified += bytes; |
| 238 | } |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 239 | } |
| 240 | } |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 241 | |
| 242 | struct FlatData { |
| 243 | uint32_t fIndex; // always > 0 |
| 244 | uint32_t fSize; |
| 245 | |
| 246 | void* data() { return (char*)this + sizeof(*this); } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 247 | |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 248 | static int Compare(const FlatData* a, const FlatData* b) { |
| 249 | return memcmp(&a->fSize, &b->fSize, a->fSize + sizeof(a->fSize)); |
| 250 | } |
| 251 | }; |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 252 | |
| 253 | SkTDArray<FlatData*> fBitmapArray; |
| 254 | int flattenToIndex(const SkBitmap&); |
| 255 | |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 256 | SkTDArray<FlatData*> fFlatArray; |
| 257 | int fCurrFlatIndex[kCount_PaintFlats]; |
| 258 | int flattenToIndex(SkFlattenable* obj, PaintFlats); |
| 259 | |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 260 | SkPaint fPaint; |
| 261 | void writePaint(const SkPaint&); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 262 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 263 | class AutoPipeNotify { |
| 264 | public: |
| 265 | AutoPipeNotify(SkGPipeCanvas* canvas) : fCanvas(canvas) {} |
| 266 | ~AutoPipeNotify() { fCanvas->doNotify(); } |
| 267 | private: |
| 268 | SkGPipeCanvas* fCanvas; |
| 269 | }; |
| 270 | friend class AutoPipeNotify; |
| 271 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 272 | typedef SkCanvas INHERITED; |
| 273 | }; |
| 274 | |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 275 | int SkGPipeCanvas::flattenToIndex(const SkBitmap & bitmap) { |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 276 | SkASSERT(shouldFlattenBitmaps(fFlags)); |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 277 | SkOrderedWriteBuffer tmpWriter(1024); |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 278 | tmpWriter.setFlags((SkFlattenableWriteBuffer::Flags) |
| 279 | (SkFlattenableWriteBuffer::kInlineFactoryNames_Flag |
| 280 | | SkFlattenableWriteBuffer::kCrossProcess_Flag)); |
| 281 | tmpWriter.setFactoryRecorder(fFactorySet); |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 282 | bitmap.flatten(tmpWriter); |
| 283 | |
| 284 | size_t len = tmpWriter.size(); |
| 285 | size_t allocSize = len + sizeof(FlatData); |
| 286 | |
| 287 | SkAutoSMalloc<1024> storage(allocSize); |
| 288 | FlatData* flat = (FlatData*)storage.get(); |
| 289 | flat->fSize = len; |
| 290 | tmpWriter.flatten(flat->data()); |
| 291 | |
| 292 | int index = SkTSearch<FlatData>((const FlatData**)fBitmapArray.begin(), |
| 293 | fBitmapArray.count(), flat, sizeof(flat), |
| 294 | &FlatData::Compare); |
| 295 | if (index < 0) { |
| 296 | index = ~index; |
| 297 | FlatData* copy = (FlatData*)sk_malloc_throw(allocSize); |
| 298 | memcpy(copy, flat, allocSize); |
| 299 | // For bitmaps, we can use zero based indices, since we will never ask |
| 300 | // for a NULL bitmap (unlike with paint flattenables). |
| 301 | copy->fIndex = fBitmapArray.count(); |
| 302 | *fBitmapArray.insert(index) = copy; |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 303 | if (this->needOpBytes(len)) { |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 304 | this->writeOp(kDef_Bitmap_DrawOp, 0, copy->fIndex); |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 305 | fWriter.write(copy->data(), len); |
| 306 | } |
| 307 | } |
| 308 | return fBitmapArray[index]->fIndex; |
| 309 | } |
| 310 | |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 311 | // return 0 for NULL (or unflattenable obj), or index-base-1 |
| 312 | int SkGPipeCanvas::flattenToIndex(SkFlattenable* obj, PaintFlats paintflat) { |
| 313 | if (NULL == obj) { |
| 314 | return 0; |
| 315 | } |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 316 | |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 317 | SkOrderedWriteBuffer tmpWriter(1024); |
scroggo@google.com | 5a2e879 | 2012-04-20 17:39:51 +0000 | [diff] [blame] | 318 | |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 319 | if (fFlags & SkGPipeWriter::kCrossProcess_Flag) { |
| 320 | tmpWriter.setFlags((SkFlattenableWriteBuffer::Flags) |
| 321 | (SkFlattenableWriteBuffer::kInlineFactoryNames_Flag |
| 322 | | SkFlattenableWriteBuffer::kCrossProcess_Flag)); |
| 323 | tmpWriter.setFactoryRecorder(fFactorySet); |
| 324 | } else { |
| 325 | // Needed for bitmap shaders. |
| 326 | tmpWriter.setFlags(SkFlattenableWriteBuffer::kForceFlattenBitmapPixels_Flag); |
| 327 | } |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 328 | |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 329 | tmpWriter.writeFlattenable(obj); |
| 330 | size_t len = tmpWriter.size(); |
| 331 | size_t allocSize = len + sizeof(FlatData); |
| 332 | |
| 333 | SkAutoSMalloc<1024> storage(allocSize); |
| 334 | FlatData* flat = (FlatData*)storage.get(); |
| 335 | flat->fSize = len; |
| 336 | tmpWriter.flatten(flat->data()); |
| 337 | |
| 338 | int index = SkTSearch<FlatData>((const FlatData**)fFlatArray.begin(), |
| 339 | fFlatArray.count(), flat, sizeof(flat), |
| 340 | &FlatData::Compare); |
| 341 | if (index < 0) { |
| 342 | index = ~index; |
| 343 | FlatData* copy = (FlatData*)sk_malloc_throw(allocSize); |
| 344 | memcpy(copy, flat, allocSize); |
| 345 | *fFlatArray.insert(index) = copy; |
| 346 | // call this after the insert, so that count() will have been grown |
| 347 | copy->fIndex = fFlatArray.count(); |
| 348 | // SkDebugf("--- add flattenable[%d] size=%d index=%d\n", paintflat, len, copy->fIndex); |
| 349 | |
| 350 | if (this->needOpBytes(len)) { |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 351 | this->writeOp(kDef_Flattenable_DrawOp, paintflat, copy->fIndex); |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 352 | fWriter.write(copy->data(), len); |
| 353 | } |
| 354 | } |
| 355 | return fFlatArray[index]->fIndex; |
| 356 | } |
| 357 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 358 | /////////////////////////////////////////////////////////////////////////////// |
| 359 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 360 | #define MIN_BLOCK_SIZE (16 * 1024) |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 361 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 362 | SkGPipeCanvas::SkGPipeCanvas(SkGPipeController* controller, |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 363 | SkWriter32* writer, SkFactorySet* fset, uint32_t flags) |
scroggo@google.com | d33819a | 2012-06-29 19:24:25 +0000 | [diff] [blame] | 364 | : fHeap(!(flags & SkGPipeWriter::kCrossProcess_Flag)), fWriter(*writer), fFlags(flags) { |
reed@google.com | 67908f2 | 2011-06-27 14:47:50 +0000 | [diff] [blame] | 365 | fFactorySet = fset; |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 366 | fController = controller; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 367 | fDone = false; |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 368 | fBlockSize = 0; // need first block from controller |
scroggo@google.com | 5a2e879 | 2012-04-20 17:39:51 +0000 | [diff] [blame] | 369 | fBytesNotified = 0; |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 370 | sk_bzero(fCurrFlatIndex, sizeof(fCurrFlatIndex)); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 371 | |
reed@google.com | bb6793b | 2011-05-05 15:18:15 +0000 | [diff] [blame] | 372 | // we need a device to limit our clip |
| 373 | // should the caller give us the bounds? |
yangsu@google.com | 06b4da16 | 2011-06-17 15:04:40 +0000 | [diff] [blame] | 374 | // We don't allocate pixels for the bitmap |
reed@google.com | bb6793b | 2011-05-05 15:18:15 +0000 | [diff] [blame] | 375 | SkBitmap bitmap; |
| 376 | bitmap.setConfig(SkBitmap::kARGB_8888_Config, 32767, 32767); |
yangsu@google.com | 06b4da16 | 2011-06-17 15:04:40 +0000 | [diff] [blame] | 377 | SkDevice* device = SkNEW_ARGS(SkDevice, (bitmap)); |
reed@google.com | bb6793b | 2011-05-05 15:18:15 +0000 | [diff] [blame] | 378 | this->setDevice(device)->unref(); |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 379 | // Tell the reader the appropriate flags to use. |
| 380 | if (this->needOpBytes()) { |
| 381 | this->writeOp(kReportFlags_DrawOp, fFlags, 0); |
| 382 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | SkGPipeCanvas::~SkGPipeCanvas() { |
| 386 | this->finish(); |
| 387 | |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 388 | fFlatArray.freeAll(); |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 389 | fBitmapArray.freeAll(); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 390 | } |
| 391 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 392 | bool SkGPipeCanvas::needOpBytes(size_t needed) { |
| 393 | if (fDone) { |
| 394 | return false; |
| 395 | } |
| 396 | |
| 397 | needed += 4; // size of DrawOp atom |
| 398 | if (fWriter.size() + needed > fBlockSize) { |
scroggo@google.com | 5a2e879 | 2012-04-20 17:39:51 +0000 | [diff] [blame] | 399 | // Before we wipe out any data that has already been written, read it |
| 400 | // out. |
| 401 | this->doNotify(); |
| 402 | size_t blockSize = SkMax32(MIN_BLOCK_SIZE, needed); |
| 403 | void* block = fController->requestBlock(blockSize, &fBlockSize); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 404 | if (NULL == block) { |
| 405 | fDone = true; |
| 406 | return false; |
| 407 | } |
| 408 | fWriter.reset(block, fBlockSize); |
| 409 | fBytesNotified = 0; |
| 410 | } |
| 411 | return true; |
| 412 | } |
| 413 | |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 414 | uint32_t SkGPipeCanvas::getTypefaceID(SkTypeface* face) { |
| 415 | uint32_t id = 0; // 0 means default/null typeface |
| 416 | if (face) { |
| 417 | id = fTypefaceSet.find(face); |
| 418 | if (0 == id) { |
| 419 | id = fTypefaceSet.add(face); |
| 420 | size_t size = writeTypeface(NULL, face); |
| 421 | if (this->needOpBytes(size)) { |
reed@google.com | bb6793b | 2011-05-05 15:18:15 +0000 | [diff] [blame] | 422 | this->writeOp(kDef_Typeface_DrawOp); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 423 | writeTypeface(&fWriter, face); |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | return id; |
| 428 | } |
| 429 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 430 | /////////////////////////////////////////////////////////////////////////////// |
| 431 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 432 | #define NOTIFY_SETUP(canvas) \ |
| 433 | AutoPipeNotify apn(canvas) |
| 434 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 435 | int SkGPipeCanvas::save(SaveFlags flags) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 436 | NOTIFY_SETUP(this); |
| 437 | if (this->needOpBytes()) { |
| 438 | this->writeOp(kSave_DrawOp, 0, flags); |
| 439 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 440 | return this->INHERITED::save(flags); |
| 441 | } |
| 442 | |
| 443 | int SkGPipeCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint, |
| 444 | SaveFlags saveFlags) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 445 | NOTIFY_SETUP(this); |
| 446 | size_t size = 0; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 447 | unsigned opFlags = 0; |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 448 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 449 | if (bounds) { |
| 450 | opFlags |= kSaveLayer_HasBounds_DrawOpFlag; |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 451 | size += sizeof(SkRect); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 452 | } |
| 453 | if (paint) { |
| 454 | opFlags |= kSaveLayer_HasPaint_DrawOpFlag; |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 455 | this->writePaint(*paint); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 456 | } |
| 457 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 458 | if (this->needOpBytes(size)) { |
| 459 | this->writeOp(kSaveLayer_DrawOp, opFlags, saveFlags); |
| 460 | if (bounds) { |
| 461 | fWriter.writeRect(*bounds); |
| 462 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 463 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 464 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 465 | // we just pass on the save, so we don't create a layer |
| 466 | return this->INHERITED::save(saveFlags); |
| 467 | } |
| 468 | |
| 469 | void SkGPipeCanvas::restore() { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 470 | NOTIFY_SETUP(this); |
| 471 | if (this->needOpBytes()) { |
| 472 | this->writeOp(kRestore_DrawOp); |
| 473 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 474 | this->INHERITED::restore(); |
| 475 | } |
| 476 | |
| 477 | bool SkGPipeCanvas::translate(SkScalar dx, SkScalar dy) { |
| 478 | if (dx || dy) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 479 | NOTIFY_SETUP(this); |
| 480 | if (this->needOpBytes(2 * sizeof(SkScalar))) { |
| 481 | this->writeOp(kTranslate_DrawOp); |
| 482 | fWriter.writeScalar(dx); |
| 483 | fWriter.writeScalar(dy); |
| 484 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 485 | } |
| 486 | return this->INHERITED::translate(dx, dy); |
| 487 | } |
| 488 | |
| 489 | bool SkGPipeCanvas::scale(SkScalar sx, SkScalar sy) { |
| 490 | if (sx || sy) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 491 | NOTIFY_SETUP(this); |
| 492 | if (this->needOpBytes(2 * sizeof(SkScalar))) { |
| 493 | this->writeOp(kScale_DrawOp); |
| 494 | fWriter.writeScalar(sx); |
| 495 | fWriter.writeScalar(sy); |
| 496 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 497 | } |
| 498 | return this->INHERITED::scale(sx, sy); |
| 499 | } |
| 500 | |
| 501 | bool SkGPipeCanvas::rotate(SkScalar degrees) { |
| 502 | if (degrees) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 503 | NOTIFY_SETUP(this); |
| 504 | if (this->needOpBytes(sizeof(SkScalar))) { |
| 505 | this->writeOp(kRotate_DrawOp); |
| 506 | fWriter.writeScalar(degrees); |
| 507 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 508 | } |
| 509 | return this->INHERITED::rotate(degrees); |
| 510 | } |
| 511 | |
| 512 | bool SkGPipeCanvas::skew(SkScalar sx, SkScalar sy) { |
| 513 | if (sx || sy) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 514 | NOTIFY_SETUP(this); |
| 515 | if (this->needOpBytes(2 * sizeof(SkScalar))) { |
| 516 | this->writeOp(kSkew_DrawOp); |
| 517 | fWriter.writeScalar(sx); |
| 518 | fWriter.writeScalar(sy); |
| 519 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 520 | } |
| 521 | return this->INHERITED::skew(sx, sy); |
| 522 | } |
| 523 | |
| 524 | bool SkGPipeCanvas::concat(const SkMatrix& matrix) { |
| 525 | if (!matrix.isIdentity()) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 526 | NOTIFY_SETUP(this); |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 527 | if (this->needOpBytes(matrix.writeToMemory(NULL))) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 528 | this->writeOp(kConcat_DrawOp); |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 529 | fWriter.writeMatrix(matrix); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 530 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 531 | } |
| 532 | return this->INHERITED::concat(matrix); |
| 533 | } |
| 534 | |
| 535 | void SkGPipeCanvas::setMatrix(const SkMatrix& matrix) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 536 | NOTIFY_SETUP(this); |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 537 | if (this->needOpBytes(matrix.writeToMemory(NULL))) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 538 | this->writeOp(kSetMatrix_DrawOp); |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 539 | fWriter.writeMatrix(matrix); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 540 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 541 | this->INHERITED::setMatrix(matrix); |
| 542 | } |
| 543 | |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 544 | bool SkGPipeCanvas::clipRect(const SkRect& rect, SkRegion::Op rgnOp, |
| 545 | bool doAntiAlias) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 546 | NOTIFY_SETUP(this); |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 547 | if (this->needOpBytes(sizeof(SkRect)) + sizeof(bool)) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 548 | this->writeOp(kClipRect_DrawOp, 0, rgnOp); |
| 549 | fWriter.writeRect(rect); |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 550 | fWriter.writeBool(doAntiAlias); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 551 | } |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 552 | return this->INHERITED::clipRect(rect, rgnOp, doAntiAlias); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 553 | } |
| 554 | |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 555 | bool SkGPipeCanvas::clipPath(const SkPath& path, SkRegion::Op rgnOp, |
| 556 | bool doAntiAlias) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 557 | NOTIFY_SETUP(this); |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 558 | if (this->needOpBytes(path.writeToMemory(NULL)) + sizeof(bool)) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 559 | this->writeOp(kClipPath_DrawOp, 0, rgnOp); |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 560 | fWriter.writePath(path); |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 561 | fWriter.writeBool(doAntiAlias); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 562 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 563 | // we just pass on the bounds of the path |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 564 | return this->INHERITED::clipRect(path.getBounds(), rgnOp, doAntiAlias); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | bool SkGPipeCanvas::clipRegion(const SkRegion& region, SkRegion::Op rgnOp) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 568 | NOTIFY_SETUP(this); |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 569 | if (this->needOpBytes(region.writeToMemory(NULL))) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 570 | this->writeOp(kClipRegion_DrawOp, 0, rgnOp); |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 571 | fWriter.writeRegion(region); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 572 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 573 | return this->INHERITED::clipRegion(region, rgnOp); |
| 574 | } |
| 575 | |
| 576 | /////////////////////////////////////////////////////////////////////////////// |
| 577 | |
| 578 | void SkGPipeCanvas::clear(SkColor color) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 579 | NOTIFY_SETUP(this); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 580 | unsigned flags = 0; |
| 581 | if (color) { |
| 582 | flags |= kClear_HasColor_DrawOpFlag; |
| 583 | } |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 584 | if (this->needOpBytes(sizeof(SkColor))) { |
| 585 | this->writeOp(kDrawClear_DrawOp, flags, 0); |
| 586 | if (color) { |
| 587 | fWriter.write32(color); |
| 588 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 589 | } |
| 590 | } |
| 591 | |
| 592 | void SkGPipeCanvas::drawPaint(const SkPaint& paint) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 593 | NOTIFY_SETUP(this); |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 594 | this->writePaint(paint); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 595 | if (this->needOpBytes()) { |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 596 | this->writeOp(kDrawPaint_DrawOp); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 597 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | void SkGPipeCanvas::drawPoints(PointMode mode, size_t count, |
| 601 | const SkPoint pts[], const SkPaint& paint) { |
| 602 | if (count) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 603 | NOTIFY_SETUP(this); |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 604 | this->writePaint(paint); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 605 | if (this->needOpBytes(4 + count * sizeof(SkPoint))) { |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 606 | this->writeOp(kDrawPoints_DrawOp, mode, 0); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 607 | fWriter.write32(count); |
| 608 | fWriter.write(pts, count * sizeof(SkPoint)); |
| 609 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 610 | } |
| 611 | } |
| 612 | |
| 613 | void SkGPipeCanvas::drawRect(const SkRect& rect, const SkPaint& paint) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 614 | NOTIFY_SETUP(this); |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 615 | this->writePaint(paint); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 616 | if (this->needOpBytes(sizeof(SkRect))) { |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 617 | this->writeOp(kDrawRect_DrawOp); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 618 | fWriter.writeRect(rect); |
| 619 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | void SkGPipeCanvas::drawPath(const SkPath& path, const SkPaint& paint) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 623 | NOTIFY_SETUP(this); |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 624 | this->writePaint(paint); |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 625 | if (this->needOpBytes(path.writeToMemory(NULL))) { |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 626 | this->writeOp(kDrawPath_DrawOp); |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 627 | fWriter.writePath(path); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 628 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 629 | } |
| 630 | |
scroggo@google.com | 5a2e879 | 2012-04-20 17:39:51 +0000 | [diff] [blame] | 631 | void SkGPipeCanvas::drawBitmap(const SkBitmap& bm, SkScalar left, SkScalar top, |
| 632 | const SkPaint* paint) { |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 633 | bool flatten = shouldFlattenBitmaps(fFlags); |
| 634 | const void* ptr = 0; |
| 635 | int bitmapIndex = 0; |
| 636 | if (flatten) { |
| 637 | bitmapIndex = this->flattenToIndex(bm); |
| 638 | } else { |
| 639 | ptr = fHeap.addBitmap(bm); |
| 640 | if (NULL == ptr) { |
| 641 | return; |
| 642 | } |
scroggo@google.com | 8ae3c7f | 2012-06-13 17:47:52 +0000 | [diff] [blame] | 643 | } |
| 644 | |
scroggo@google.com | 5a2e879 | 2012-04-20 17:39:51 +0000 | [diff] [blame] | 645 | NOTIFY_SETUP(this); |
| 646 | if (paint) { |
| 647 | this->writePaint(*paint); |
| 648 | } |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 649 | |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 650 | size_t opBytesNeeded = sizeof(SkScalar) * 2 + sizeof(bool); |
| 651 | if (!flatten) { |
| 652 | opBytesNeeded += sizeof(void*); |
| 653 | } |
| 654 | if (this->needOpBytes(opBytesNeeded)) { |
| 655 | this->writeOp(kDrawBitmap_DrawOp, 0, bitmapIndex); |
| 656 | if (!flatten) { |
| 657 | fWriter.writePtr(const_cast<void*>(ptr)); |
| 658 | } |
scroggo@google.com | 5a2e879 | 2012-04-20 17:39:51 +0000 | [diff] [blame] | 659 | fWriter.writeBool(paint != NULL); |
| 660 | fWriter.writeScalar(left); |
| 661 | fWriter.writeScalar(top); |
| 662 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 663 | } |
| 664 | |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 665 | void SkGPipeCanvas::drawBitmapRect(const SkBitmap& bm, const SkIRect* src, |
| 666 | const SkRect& dst, const SkPaint* paint) { |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 667 | bool flatten = shouldFlattenBitmaps(fFlags); |
| 668 | const void* ptr = 0; |
| 669 | int bitmapIndex = 0; |
| 670 | if (flatten) { |
| 671 | bitmapIndex = this->flattenToIndex(bm); |
| 672 | } else { |
| 673 | ptr = fHeap.addBitmap(bm); |
| 674 | if (NULL == ptr) { |
| 675 | return; |
| 676 | } |
scroggo@google.com | 8ae3c7f | 2012-06-13 17:47:52 +0000 | [diff] [blame] | 677 | } |
| 678 | |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 679 | NOTIFY_SETUP(this); |
| 680 | if (paint) { |
| 681 | this->writePaint(*paint); |
| 682 | } |
| 683 | |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 684 | size_t opBytesNeeded = sizeof(SkRect) + sizeof(bool) * 2; |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 685 | bool hasSrc = src != NULL; |
| 686 | if (hasSrc) { |
| 687 | opBytesNeeded += sizeof(int32_t) * 4; |
| 688 | } |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 689 | if (!flatten) { |
| 690 | opBytesNeeded += sizeof(void*); |
| 691 | } |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 692 | if (this->needOpBytes(opBytesNeeded)) { |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 693 | this->writeOp(kDrawBitmapRect_DrawOp, 0, bitmapIndex); |
| 694 | if (!flatten) { |
| 695 | fWriter.writePtr(const_cast<void*>(ptr)); |
| 696 | } |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 697 | fWriter.writeBool(paint != NULL); |
| 698 | fWriter.writeBool(hasSrc); |
| 699 | if (hasSrc) { |
| 700 | fWriter.write32(src->fLeft); |
| 701 | fWriter.write32(src->fTop); |
| 702 | fWriter.write32(src->fRight); |
| 703 | fWriter.write32(src->fBottom); |
| 704 | } |
| 705 | fWriter.writeRect(dst); |
| 706 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | void SkGPipeCanvas::drawBitmapMatrix(const SkBitmap&, const SkMatrix&, |
| 710 | const SkPaint*) { |
| 711 | UNIMPLEMENTED |
| 712 | } |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 713 | |
| 714 | void SkGPipeCanvas::drawBitmapNine(const SkBitmap& bm, const SkIRect& center, |
scroggo@google.com | 5a2e879 | 2012-04-20 17:39:51 +0000 | [diff] [blame] | 715 | const SkRect& dst, const SkPaint* paint) { |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 716 | bool flatten = shouldFlattenBitmaps(fFlags); |
| 717 | const void* ptr = 0; |
| 718 | int bitmapIndex = 0; |
| 719 | if (flatten) { |
| 720 | bitmapIndex = this->flattenToIndex(bm); |
| 721 | } else { |
| 722 | ptr = fHeap.addBitmap(bm); |
| 723 | if (NULL == ptr) { |
| 724 | return; |
| 725 | } |
scroggo@google.com | 8ae3c7f | 2012-06-13 17:47:52 +0000 | [diff] [blame] | 726 | } |
| 727 | |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 728 | NOTIFY_SETUP(this); |
| 729 | if (paint) { |
| 730 | this->writePaint(*paint); |
| 731 | } |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 732 | |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 733 | size_t opBytesNeeded = sizeof(int32_t) * 4 + sizeof(bool) + sizeof(SkRect); |
| 734 | if (!flatten) { |
| 735 | opBytesNeeded += sizeof(void*); |
| 736 | } |
| 737 | if (this->needOpBytes(opBytesNeeded)) { |
| 738 | this->writeOp(kDrawBitmapNine_DrawOp, 0, bitmapIndex); |
| 739 | if (!flatten) { |
| 740 | fWriter.writePtr(const_cast<void*>(ptr)); |
| 741 | } |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 742 | fWriter.writeBool(paint != NULL); |
| 743 | fWriter.write32(center.fLeft); |
| 744 | fWriter.write32(center.fTop); |
| 745 | fWriter.write32(center.fRight); |
| 746 | fWriter.write32(center.fBottom); |
| 747 | fWriter.writeRect(dst); |
| 748 | } |
scroggo@google.com | 5a2e879 | 2012-04-20 17:39:51 +0000 | [diff] [blame] | 749 | } |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 750 | |
| 751 | void SkGPipeCanvas::drawSprite(const SkBitmap& bm, int left, int top, |
| 752 | const SkPaint* paint) { |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 753 | bool flatten = shouldFlattenBitmaps(fFlags); |
| 754 | const void* ptr = 0; |
| 755 | int bitmapIndex = 0; |
| 756 | if (flatten) { |
| 757 | bitmapIndex = this->flattenToIndex(bm); |
| 758 | } else { |
| 759 | ptr = fHeap.addBitmap(bm); |
| 760 | if (NULL == ptr) { |
| 761 | return; |
| 762 | } |
scroggo@google.com | 8ae3c7f | 2012-06-13 17:47:52 +0000 | [diff] [blame] | 763 | } |
| 764 | |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 765 | NOTIFY_SETUP(this); |
| 766 | if (paint) { |
| 767 | this->writePaint(*paint); |
| 768 | } |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 769 | |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 770 | size_t opBytesNeeded = sizeof(int32_t) * 2 + sizeof(bool); |
| 771 | if (!flatten) { |
| 772 | opBytesNeeded += sizeof(void*); |
| 773 | } |
| 774 | if (this->needOpBytes(opBytesNeeded)) { |
| 775 | this->writeOp(kDrawSprite_DrawOp, 0, bitmapIndex); |
| 776 | if (!flatten) { |
| 777 | fWriter.writePtr(const_cast<void*>(ptr)); |
| 778 | } |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 779 | fWriter.writeBool(paint != NULL); |
| 780 | fWriter.write32(left); |
| 781 | fWriter.write32(top); |
| 782 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 783 | } |
| 784 | |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 785 | void SkGPipeCanvas::drawText(const void* text, size_t byteLength, SkScalar x, |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 786 | SkScalar y, const SkPaint& paint) { |
| 787 | if (byteLength) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 788 | NOTIFY_SETUP(this); |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 789 | this->writePaint(paint); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 790 | if (this->needOpBytes(4 + SkAlign4(byteLength) + 2 * sizeof(SkScalar))) { |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 791 | this->writeOp(kDrawText_DrawOp); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 792 | fWriter.write32(byteLength); |
| 793 | fWriter.writePad(text, byteLength); |
| 794 | fWriter.writeScalar(x); |
| 795 | fWriter.writeScalar(y); |
| 796 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 797 | } |
| 798 | } |
| 799 | |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 800 | void SkGPipeCanvas::drawPosText(const void* text, size_t byteLength, |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 801 | const SkPoint pos[], const SkPaint& paint) { |
| 802 | if (byteLength) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 803 | NOTIFY_SETUP(this); |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 804 | this->writePaint(paint); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 805 | int count = paint.textToGlyphs(text, byteLength, NULL); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 806 | if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkPoint))) { |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 807 | this->writeOp(kDrawPosText_DrawOp); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 808 | fWriter.write32(byteLength); |
| 809 | fWriter.writePad(text, byteLength); |
| 810 | fWriter.write32(count); |
| 811 | fWriter.write(pos, count * sizeof(SkPoint)); |
| 812 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 813 | } |
| 814 | } |
| 815 | |
| 816 | void SkGPipeCanvas::drawPosTextH(const void* text, size_t byteLength, |
| 817 | const SkScalar xpos[], SkScalar constY, |
| 818 | const SkPaint& paint) { |
| 819 | if (byteLength) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 820 | NOTIFY_SETUP(this); |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 821 | this->writePaint(paint); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 822 | int count = paint.textToGlyphs(text, byteLength, NULL); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 823 | if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkScalar) + 4)) { |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 824 | this->writeOp(kDrawPosTextH_DrawOp); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 825 | fWriter.write32(byteLength); |
| 826 | fWriter.writePad(text, byteLength); |
| 827 | fWriter.write32(count); |
| 828 | fWriter.write(xpos, count * sizeof(SkScalar)); |
| 829 | fWriter.writeScalar(constY); |
| 830 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 831 | } |
| 832 | } |
| 833 | |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 834 | void SkGPipeCanvas::drawTextOnPath(const void* text, size_t byteLength, |
| 835 | const SkPath& path, const SkMatrix* matrix, |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 836 | const SkPaint& paint) { |
| 837 | if (byteLength) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 838 | NOTIFY_SETUP(this); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 839 | unsigned flags = 0; |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 840 | size_t size = 4 + SkAlign4(byteLength) + path.writeToMemory(NULL); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 841 | if (matrix) { |
| 842 | flags |= kDrawTextOnPath_HasMatrix_DrawOpFlag; |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 843 | size += matrix->writeToMemory(NULL); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 844 | } |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 845 | this->writePaint(paint); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 846 | if (this->needOpBytes(size)) { |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 847 | this->writeOp(kDrawTextOnPath_DrawOp, flags, 0); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 848 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 849 | fWriter.write32(byteLength); |
| 850 | fWriter.writePad(text, byteLength); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 851 | |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 852 | fWriter.writePath(path); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 853 | if (matrix) { |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 854 | fWriter.writeMatrix(*matrix); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 855 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 856 | } |
| 857 | } |
| 858 | } |
| 859 | |
| 860 | void SkGPipeCanvas::drawPicture(SkPicture& picture) { |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 861 | // we want to playback the picture into individual draw calls |
| 862 | this->INHERITED::drawPicture(picture); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 863 | } |
| 864 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 865 | void SkGPipeCanvas::drawVertices(VertexMode mode, int vertexCount, |
| 866 | const SkPoint vertices[], const SkPoint texs[], |
| 867 | const SkColor colors[], SkXfermode*, |
| 868 | const uint16_t indices[], int indexCount, |
| 869 | const SkPaint& paint) { |
| 870 | if (0 == vertexCount) { |
| 871 | return; |
| 872 | } |
| 873 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 874 | NOTIFY_SETUP(this); |
| 875 | size_t size = 4 + vertexCount * sizeof(SkPoint); |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 876 | this->writePaint(paint); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 877 | unsigned flags = 0; |
| 878 | if (texs) { |
| 879 | flags |= kDrawVertices_HasTexs_DrawOpFlag; |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 880 | size += vertexCount * sizeof(SkPoint); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 881 | } |
| 882 | if (colors) { |
| 883 | flags |= kDrawVertices_HasColors_DrawOpFlag; |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 884 | size += vertexCount * sizeof(SkColor); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 885 | } |
| 886 | if (indices && indexCount > 0) { |
| 887 | flags |= kDrawVertices_HasIndices_DrawOpFlag; |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 888 | size += 4 + SkAlign4(indexCount * sizeof(uint16_t)); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 889 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 890 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 891 | if (this->needOpBytes(size)) { |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 892 | this->writeOp(kDrawVertices_DrawOp, flags, 0); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 893 | fWriter.write32(mode); |
| 894 | fWriter.write32(vertexCount); |
| 895 | fWriter.write(vertices, vertexCount * sizeof(SkPoint)); |
| 896 | if (texs) { |
| 897 | fWriter.write(texs, vertexCount * sizeof(SkPoint)); |
| 898 | } |
| 899 | if (colors) { |
| 900 | fWriter.write(colors, vertexCount * sizeof(SkColor)); |
| 901 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 902 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 903 | // TODO: flatten xfermode |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 904 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 905 | if (indices && indexCount > 0) { |
| 906 | fWriter.write32(indexCount); |
| 907 | fWriter.writePad(indices, indexCount * sizeof(uint16_t)); |
| 908 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 909 | } |
| 910 | } |
| 911 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 912 | void SkGPipeCanvas::drawData(const void* ptr, size_t size) { |
| 913 | if (size && ptr) { |
| 914 | NOTIFY_SETUP(this); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 915 | unsigned data = 0; |
| 916 | if (size < (1 << DRAWOPS_DATA_BITS)) { |
| 917 | data = (unsigned)size; |
| 918 | } |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 919 | if (this->needOpBytes(4 + SkAlign4(size))) { |
| 920 | this->writeOp(kDrawData_DrawOp, 0, data); |
| 921 | if (0 == data) { |
| 922 | fWriter.write32(size); |
| 923 | } |
reed@google.com | bb6793b | 2011-05-05 15:18:15 +0000 | [diff] [blame] | 924 | fWriter.writePad(ptr, size); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 925 | } |
| 926 | } |
| 927 | } |
| 928 | |
| 929 | /////////////////////////////////////////////////////////////////////////////// |
| 930 | |
| 931 | template <typename T> uint32_t castToU32(T value) { |
| 932 | union { |
| 933 | T fSrc; |
| 934 | uint32_t fDst; |
| 935 | } data; |
| 936 | data.fSrc = value; |
| 937 | return data.fDst; |
| 938 | } |
| 939 | |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 940 | void SkGPipeCanvas::writePaint(const SkPaint& paint) { |
| 941 | SkPaint& base = fPaint; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 942 | uint32_t storage[32]; |
| 943 | uint32_t* ptr = storage; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 944 | |
| 945 | if (base.getFlags() != paint.getFlags()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 946 | *ptr++ = PaintOp_packOpData(kFlags_PaintOp, paint.getFlags()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 947 | base.setFlags(paint.getFlags()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 948 | } |
| 949 | if (base.getColor() != paint.getColor()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 950 | *ptr++ = PaintOp_packOp(kColor_PaintOp); |
| 951 | *ptr++ = paint.getColor(); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 952 | base.setColor(paint.getColor()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 953 | } |
| 954 | if (base.getStyle() != paint.getStyle()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 955 | *ptr++ = PaintOp_packOpData(kStyle_PaintOp, paint.getStyle()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 956 | base.setStyle(paint.getStyle()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 957 | } |
| 958 | if (base.getStrokeJoin() != paint.getStrokeJoin()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 959 | *ptr++ = PaintOp_packOpData(kJoin_PaintOp, paint.getStrokeJoin()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 960 | base.setStrokeJoin(paint.getStrokeJoin()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 961 | } |
| 962 | if (base.getStrokeCap() != paint.getStrokeCap()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 963 | *ptr++ = PaintOp_packOpData(kCap_PaintOp, paint.getStrokeCap()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 964 | base.setStrokeCap(paint.getStrokeCap()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 965 | } |
| 966 | if (base.getStrokeWidth() != paint.getStrokeWidth()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 967 | *ptr++ = PaintOp_packOp(kWidth_PaintOp); |
| 968 | *ptr++ = castToU32(paint.getStrokeWidth()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 969 | base.setStrokeWidth(paint.getStrokeWidth()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 970 | } |
| 971 | if (base.getStrokeMiter() != paint.getStrokeMiter()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 972 | *ptr++ = PaintOp_packOp(kMiter_PaintOp); |
| 973 | *ptr++ = castToU32(paint.getStrokeMiter()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 974 | base.setStrokeMiter(paint.getStrokeMiter()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 975 | } |
| 976 | if (base.getTextEncoding() != paint.getTextEncoding()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 977 | *ptr++ = PaintOp_packOpData(kEncoding_PaintOp, paint.getTextEncoding()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 978 | base.setTextEncoding(paint.getTextEncoding()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 979 | } |
| 980 | if (base.getHinting() != paint.getHinting()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 981 | *ptr++ = PaintOp_packOpData(kHinting_PaintOp, paint.getHinting()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 982 | base.setHinting(paint.getHinting()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 983 | } |
| 984 | if (base.getTextAlign() != paint.getTextAlign()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 985 | *ptr++ = PaintOp_packOpData(kAlign_PaintOp, paint.getTextAlign()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 986 | base.setTextAlign(paint.getTextAlign()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 987 | } |
| 988 | if (base.getTextSize() != paint.getTextSize()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 989 | *ptr++ = PaintOp_packOp(kTextSize_PaintOp); |
| 990 | *ptr++ = castToU32(paint.getTextSize()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 991 | base.setTextSize(paint.getTextSize()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 992 | } |
| 993 | if (base.getTextScaleX() != paint.getTextScaleX()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 994 | *ptr++ = PaintOp_packOp(kTextScaleX_PaintOp); |
| 995 | *ptr++ = castToU32(paint.getTextScaleX()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 996 | base.setTextScaleX(paint.getTextScaleX()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 997 | } |
| 998 | if (base.getTextSkewX() != paint.getTextSkewX()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 999 | *ptr++ = PaintOp_packOp(kTextSkewX_PaintOp); |
| 1000 | *ptr++ = castToU32(paint.getTextSkewX()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 1001 | base.setTextSkewX(paint.getTextSkewX()); |
| 1002 | } |
| 1003 | |
| 1004 | if (!SkTypeface::Equal(base.getTypeface(), paint.getTypeface())) { |
| 1005 | uint32_t id = this->getTypefaceID(paint.getTypeface()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 1006 | *ptr++ = PaintOp_packOpData(kTypeface_PaintOp, id); |
| 1007 | base.setTypeface(paint.getTypeface()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1008 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1009 | |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 1010 | for (int i = 0; i < kCount_PaintFlats; i++) { |
| 1011 | int index = this->flattenToIndex(get_paintflat(paint, i), (PaintFlats)i); |
| 1012 | SkASSERT(index >= 0 && index <= fFlatArray.count()); |
| 1013 | if (index != fCurrFlatIndex[i]) { |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 1014 | *ptr++ = PaintOp_packOpFlagData(kFlatIndex_PaintOp, i, index); |
| 1015 | fCurrFlatIndex[i] = index; |
| 1016 | } |
| 1017 | } |
| 1018 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1019 | size_t size = (char*)ptr - (char*)storage; |
| 1020 | if (size && this->needOpBytes(size)) { |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 1021 | this->writeOp(kPaintOp_DrawOp, 0, size); |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 1022 | fWriter.write(storage, size); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1023 | for (size_t i = 0; i < size/4; i++) { |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 1024 | // SkDebugf("[%d] %08X\n", i, storage[i]); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1025 | } |
| 1026 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | /////////////////////////////////////////////////////////////////////////////// |
| 1030 | |
| 1031 | #include "SkGPipe.h" |
| 1032 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1033 | SkGPipeWriter::SkGPipeWriter() : fWriter(0) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1034 | fCanvas = NULL; |
| 1035 | } |
| 1036 | |
| 1037 | SkGPipeWriter::~SkGPipeWriter() { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1038 | this->endRecording(); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1039 | SkSafeUnref(fCanvas); |
| 1040 | } |
| 1041 | |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 1042 | SkCanvas* SkGPipeWriter::startRecording(SkGPipeController* controller, uint32_t flags) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1043 | if (NULL == fCanvas) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1044 | fWriter.reset(NULL, 0); |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 1045 | fFactorySet.reset(); |
| 1046 | fCanvas = SkNEW_ARGS(SkGPipeCanvas, (controller, &fWriter, |
| 1047 | (flags & kCrossProcess_Flag) ? |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 1048 | &fFactorySet : NULL, flags)); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1049 | } |
| 1050 | return fCanvas; |
| 1051 | } |
| 1052 | |
| 1053 | void SkGPipeWriter::endRecording() { |
| 1054 | if (fCanvas) { |
| 1055 | fCanvas->finish(); |
| 1056 | fCanvas->unref(); |
| 1057 | fCanvas = NULL; |
| 1058 | } |
| 1059 | } |
| 1060 | |