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