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