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