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