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 | |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 486 | SkPaint fPaint; |
| 487 | void writePaint(const SkPaint&); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 488 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 489 | class AutoPipeNotify { |
| 490 | public: |
| 491 | AutoPipeNotify(SkGPipeCanvas* canvas) : fCanvas(canvas) {} |
| 492 | ~AutoPipeNotify() { fCanvas->doNotify(); } |
| 493 | private: |
| 494 | SkGPipeCanvas* fCanvas; |
| 495 | }; |
| 496 | friend class AutoPipeNotify; |
| 497 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 498 | typedef SkCanvas INHERITED; |
| 499 | }; |
| 500 | |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 501 | int SkGPipeCanvas::flattenToIndex(const SkBitmap & bitmap) { |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 502 | SkASSERT(shouldFlattenBitmaps(fFlags)); |
scroggo@google.com | 4dffc59 | 2012-07-17 16:49:40 +0000 | [diff] [blame] | 503 | uint32_t flags = SkFlattenableWriteBuffer::kInlineFactoryNames_Flag |
| 504 | | SkFlattenableWriteBuffer::kCrossProcess_Flag; |
| 505 | bool added, replaced; |
| 506 | const SkFlatData* flat = fBitmapDictionary.findAndReplace( |
| 507 | bitmap, flags, fBitmapHeap.flatToReplace(), &added, &replaced); |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 508 | |
scroggo@google.com | 4dffc59 | 2012-07-17 16:49:40 +0000 | [diff] [blame] | 509 | int index = flat->index(); |
| 510 | if (added && this->needOpBytes(flat->flatSize())) { |
| 511 | this->writeOp(kDef_Bitmap_DrawOp, 0, index); |
| 512 | fWriter.write(flat->data(), flat->flatSize()); |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 513 | } |
scroggo@google.com | 4dffc59 | 2012-07-17 16:49:40 +0000 | [diff] [blame] | 514 | return index; |
scroggo@google.com | d3ba5cc | 2012-07-09 16:05:53 +0000 | [diff] [blame] | 515 | } |
| 516 | |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 517 | // return 0 for NULL (or unflattenable obj), or index-base-1 |
scroggo@google.com | d3ba5cc | 2012-07-09 16:05:53 +0000 | [diff] [blame] | 518 | // return ~(index-base-1) if an old flattenable was replaced |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 519 | int SkGPipeCanvas::flattenToIndex(SkFlattenable* obj, PaintFlats paintflat) { |
| 520 | if (NULL == obj) { |
| 521 | return 0; |
| 522 | } |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 523 | |
scroggo@google.com | 4dffc59 | 2012-07-17 16:49:40 +0000 | [diff] [blame] | 524 | uint32_t writeBufferFlags; |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 525 | if (fFlags & SkGPipeWriter::kCrossProcess_Flag) { |
scroggo@google.com | 4dffc59 | 2012-07-17 16:49:40 +0000 | [diff] [blame] | 526 | writeBufferFlags = (SkFlattenableWriteBuffer::kInlineFactoryNames_Flag |
| 527 | | SkFlattenableWriteBuffer::kCrossProcess_Flag); |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 528 | } else { |
| 529 | // Needed for bitmap shaders. |
scroggo@google.com | 4dffc59 | 2012-07-17 16:49:40 +0000 | [diff] [blame] | 530 | writeBufferFlags = SkFlattenableWriteBuffer::kForceFlattenBitmapPixels_Flag; |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 531 | } |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 532 | |
scroggo@google.com | 4dffc59 | 2012-07-17 16:49:40 +0000 | [diff] [blame] | 533 | bool added, replaced; |
| 534 | const SkFlatData* flat = fFlatDictionary.findAndReplace( |
| 535 | *obj, writeBufferFlags, fFlattenableHeap.flatToReplace(), &added, &replaced); |
| 536 | int index = flat->index(); |
| 537 | if (added && this->needOpBytes(flat->flatSize())) { |
| 538 | this->writeOp(kDef_Flattenable_DrawOp, paintflat, index); |
| 539 | fWriter.write(flat->data(), flat->flatSize()); |
| 540 | } |
| 541 | if (replaced) { |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 542 | index = ~index; |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 543 | } |
scroggo@google.com | 4dffc59 | 2012-07-17 16:49:40 +0000 | [diff] [blame] | 544 | return index; |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 545 | } |
| 546 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 547 | /////////////////////////////////////////////////////////////////////////////// |
| 548 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 549 | #define MIN_BLOCK_SIZE (16 * 1024) |
scroggo@google.com | 4dffc59 | 2012-07-17 16:49:40 +0000 | [diff] [blame] | 550 | #define BITMAPS_TO_KEEP 5 |
| 551 | #define FLATTENABLES_TO_KEEP 10 |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 552 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 553 | SkGPipeCanvas::SkGPipeCanvas(SkGPipeController* controller, |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 554 | SkWriter32* writer, SkFactorySet* fset, uint32_t flags) |
scroggo@google.com | 4dffc59 | 2012-07-17 16:49:40 +0000 | [diff] [blame] | 555 | : fSharedHeap(!(flags & SkGPipeWriter::kCrossProcess_Flag), controller->numberOfReaders()) |
| 556 | , fWriter(*writer), fFlags(flags) |
| 557 | , fBitmapHeap(BITMAPS_TO_KEEP), fBitmapDictionary(&fBitmapHeap, NULL, NULL, fset) |
| 558 | , fFlattenableHeap(FLATTENABLES_TO_KEEP), fFlatDictionary(&fFlattenableHeap, fset) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 559 | fController = controller; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 560 | fDone = false; |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 561 | fBlockSize = 0; // need first block from controller |
scroggo@google.com | 5a2e879 | 2012-04-20 17:39:51 +0000 | [diff] [blame] | 562 | fBytesNotified = 0; |
junov@chromium.org | fbe9c8f | 2012-07-18 20:22:52 +0000 | [diff] [blame] | 563 | fFirstSaveLayerStackLevel = kNoSaveLayer; |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 564 | sk_bzero(fCurrFlatIndex, sizeof(fCurrFlatIndex)); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 565 | |
reed@google.com | bb6793b | 2011-05-05 15:18:15 +0000 | [diff] [blame] | 566 | // we need a device to limit our clip |
| 567 | // should the caller give us the bounds? |
yangsu@google.com | 06b4da16 | 2011-06-17 15:04:40 +0000 | [diff] [blame] | 568 | // We don't allocate pixels for the bitmap |
reed@google.com | bb6793b | 2011-05-05 15:18:15 +0000 | [diff] [blame] | 569 | SkBitmap bitmap; |
| 570 | bitmap.setConfig(SkBitmap::kARGB_8888_Config, 32767, 32767); |
yangsu@google.com | 06b4da16 | 2011-06-17 15:04:40 +0000 | [diff] [blame] | 571 | SkDevice* device = SkNEW_ARGS(SkDevice, (bitmap)); |
reed@google.com | bb6793b | 2011-05-05 15:18:15 +0000 | [diff] [blame] | 572 | this->setDevice(device)->unref(); |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 573 | // Tell the reader the appropriate flags to use. |
| 574 | if (this->needOpBytes()) { |
| 575 | this->writeOp(kReportFlags_DrawOp, fFlags, 0); |
| 576 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | SkGPipeCanvas::~SkGPipeCanvas() { |
| 580 | this->finish(); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 581 | } |
| 582 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 583 | bool SkGPipeCanvas::needOpBytes(size_t needed) { |
| 584 | if (fDone) { |
| 585 | return false; |
| 586 | } |
| 587 | |
| 588 | needed += 4; // size of DrawOp atom |
| 589 | if (fWriter.size() + needed > fBlockSize) { |
scroggo@google.com | 5a2e879 | 2012-04-20 17:39:51 +0000 | [diff] [blame] | 590 | // Before we wipe out any data that has already been written, read it |
| 591 | // out. |
| 592 | this->doNotify(); |
| 593 | size_t blockSize = SkMax32(MIN_BLOCK_SIZE, needed); |
| 594 | void* block = fController->requestBlock(blockSize, &fBlockSize); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 595 | if (NULL == block) { |
| 596 | fDone = true; |
| 597 | return false; |
| 598 | } |
| 599 | fWriter.reset(block, fBlockSize); |
| 600 | fBytesNotified = 0; |
| 601 | } |
| 602 | return true; |
| 603 | } |
| 604 | |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 605 | uint32_t SkGPipeCanvas::getTypefaceID(SkTypeface* face) { |
| 606 | uint32_t id = 0; // 0 means default/null typeface |
| 607 | if (face) { |
| 608 | id = fTypefaceSet.find(face); |
| 609 | if (0 == id) { |
| 610 | id = fTypefaceSet.add(face); |
| 611 | size_t size = writeTypeface(NULL, face); |
| 612 | if (this->needOpBytes(size)) { |
reed@google.com | bb6793b | 2011-05-05 15:18:15 +0000 | [diff] [blame] | 613 | this->writeOp(kDef_Typeface_DrawOp); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 614 | writeTypeface(&fWriter, face); |
| 615 | } |
| 616 | } |
| 617 | } |
| 618 | return id; |
| 619 | } |
| 620 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 621 | /////////////////////////////////////////////////////////////////////////////// |
| 622 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 623 | #define NOTIFY_SETUP(canvas) \ |
| 624 | AutoPipeNotify apn(canvas) |
| 625 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 626 | int SkGPipeCanvas::save(SaveFlags flags) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 627 | NOTIFY_SETUP(this); |
| 628 | if (this->needOpBytes()) { |
| 629 | this->writeOp(kSave_DrawOp, 0, flags); |
| 630 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 631 | return this->INHERITED::save(flags); |
| 632 | } |
| 633 | |
| 634 | int SkGPipeCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint, |
| 635 | SaveFlags saveFlags) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 636 | NOTIFY_SETUP(this); |
| 637 | size_t size = 0; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 638 | unsigned opFlags = 0; |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 639 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 640 | if (bounds) { |
| 641 | opFlags |= kSaveLayer_HasBounds_DrawOpFlag; |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 642 | size += sizeof(SkRect); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 643 | } |
| 644 | if (paint) { |
| 645 | opFlags |= kSaveLayer_HasPaint_DrawOpFlag; |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 646 | this->writePaint(*paint); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 647 | } |
| 648 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 649 | if (this->needOpBytes(size)) { |
| 650 | this->writeOp(kSaveLayer_DrawOp, opFlags, saveFlags); |
| 651 | if (bounds) { |
| 652 | fWriter.writeRect(*bounds); |
| 653 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 654 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 655 | |
junov@chromium.org | fbe9c8f | 2012-07-18 20:22:52 +0000 | [diff] [blame] | 656 | if (kNoSaveLayer == fFirstSaveLayerStackLevel){ |
| 657 | fFirstSaveLayerStackLevel = this->getSaveCount(); |
| 658 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 659 | // we just pass on the save, so we don't create a layer |
| 660 | return this->INHERITED::save(saveFlags); |
| 661 | } |
| 662 | |
| 663 | void SkGPipeCanvas::restore() { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 664 | NOTIFY_SETUP(this); |
| 665 | if (this->needOpBytes()) { |
| 666 | this->writeOp(kRestore_DrawOp); |
| 667 | } |
junov@chromium.org | fbe9c8f | 2012-07-18 20:22:52 +0000 | [diff] [blame] | 668 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 669 | this->INHERITED::restore(); |
junov@chromium.org | fbe9c8f | 2012-07-18 20:22:52 +0000 | [diff] [blame] | 670 | |
| 671 | if (this->getSaveCount() == fFirstSaveLayerStackLevel){ |
| 672 | fFirstSaveLayerStackLevel = kNoSaveLayer; |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | bool SkGPipeCanvas::isDrawingToLayer() const { |
| 677 | return kNoSaveLayer != fFirstSaveLayerStackLevel; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | bool SkGPipeCanvas::translate(SkScalar dx, SkScalar dy) { |
| 681 | if (dx || dy) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 682 | NOTIFY_SETUP(this); |
| 683 | if (this->needOpBytes(2 * sizeof(SkScalar))) { |
| 684 | this->writeOp(kTranslate_DrawOp); |
| 685 | fWriter.writeScalar(dx); |
| 686 | fWriter.writeScalar(dy); |
| 687 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 688 | } |
| 689 | return this->INHERITED::translate(dx, dy); |
| 690 | } |
| 691 | |
| 692 | bool SkGPipeCanvas::scale(SkScalar sx, SkScalar sy) { |
| 693 | if (sx || sy) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 694 | NOTIFY_SETUP(this); |
| 695 | if (this->needOpBytes(2 * sizeof(SkScalar))) { |
| 696 | this->writeOp(kScale_DrawOp); |
| 697 | fWriter.writeScalar(sx); |
| 698 | fWriter.writeScalar(sy); |
| 699 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 700 | } |
| 701 | return this->INHERITED::scale(sx, sy); |
| 702 | } |
| 703 | |
| 704 | bool SkGPipeCanvas::rotate(SkScalar degrees) { |
| 705 | if (degrees) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 706 | NOTIFY_SETUP(this); |
| 707 | if (this->needOpBytes(sizeof(SkScalar))) { |
| 708 | this->writeOp(kRotate_DrawOp); |
| 709 | fWriter.writeScalar(degrees); |
| 710 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 711 | } |
| 712 | return this->INHERITED::rotate(degrees); |
| 713 | } |
| 714 | |
| 715 | bool SkGPipeCanvas::skew(SkScalar sx, SkScalar sy) { |
| 716 | if (sx || sy) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 717 | NOTIFY_SETUP(this); |
| 718 | if (this->needOpBytes(2 * sizeof(SkScalar))) { |
| 719 | this->writeOp(kSkew_DrawOp); |
| 720 | fWriter.writeScalar(sx); |
| 721 | fWriter.writeScalar(sy); |
| 722 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 723 | } |
| 724 | return this->INHERITED::skew(sx, sy); |
| 725 | } |
| 726 | |
| 727 | bool SkGPipeCanvas::concat(const SkMatrix& matrix) { |
| 728 | if (!matrix.isIdentity()) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 729 | NOTIFY_SETUP(this); |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 730 | if (this->needOpBytes(matrix.writeToMemory(NULL))) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 731 | this->writeOp(kConcat_DrawOp); |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 732 | fWriter.writeMatrix(matrix); |
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 | } |
| 735 | return this->INHERITED::concat(matrix); |
| 736 | } |
| 737 | |
| 738 | void SkGPipeCanvas::setMatrix(const SkMatrix& matrix) { |
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(matrix.writeToMemory(NULL))) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 741 | this->writeOp(kSetMatrix_DrawOp); |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 742 | fWriter.writeMatrix(matrix); |
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 | this->INHERITED::setMatrix(matrix); |
| 745 | } |
| 746 | |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 747 | bool SkGPipeCanvas::clipRect(const SkRect& rect, SkRegion::Op rgnOp, |
| 748 | bool doAntiAlias) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 749 | NOTIFY_SETUP(this); |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 750 | if (this->needOpBytes(sizeof(SkRect)) + sizeof(bool)) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 751 | this->writeOp(kClipRect_DrawOp, 0, rgnOp); |
| 752 | fWriter.writeRect(rect); |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 753 | fWriter.writeBool(doAntiAlias); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 754 | } |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 755 | return this->INHERITED::clipRect(rect, rgnOp, doAntiAlias); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 756 | } |
| 757 | |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 758 | bool SkGPipeCanvas::clipPath(const SkPath& path, SkRegion::Op rgnOp, |
| 759 | bool doAntiAlias) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 760 | NOTIFY_SETUP(this); |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 761 | if (this->needOpBytes(path.writeToMemory(NULL)) + sizeof(bool)) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 762 | this->writeOp(kClipPath_DrawOp, 0, rgnOp); |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 763 | fWriter.writePath(path); |
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 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 766 | // we just pass on the bounds of the path |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 767 | return this->INHERITED::clipRect(path.getBounds(), rgnOp, doAntiAlias); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | bool SkGPipeCanvas::clipRegion(const SkRegion& region, SkRegion::Op rgnOp) { |
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(region.writeToMemory(NULL))) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 773 | this->writeOp(kClipRegion_DrawOp, 0, rgnOp); |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 774 | fWriter.writeRegion(region); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 775 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 776 | return this->INHERITED::clipRegion(region, rgnOp); |
| 777 | } |
| 778 | |
| 779 | /////////////////////////////////////////////////////////////////////////////// |
| 780 | |
| 781 | void SkGPipeCanvas::clear(SkColor color) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 782 | NOTIFY_SETUP(this); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 783 | unsigned flags = 0; |
| 784 | if (color) { |
| 785 | flags |= kClear_HasColor_DrawOpFlag; |
| 786 | } |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 787 | if (this->needOpBytes(sizeof(SkColor))) { |
| 788 | this->writeOp(kDrawClear_DrawOp, flags, 0); |
| 789 | if (color) { |
| 790 | fWriter.write32(color); |
| 791 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 792 | } |
| 793 | } |
| 794 | |
| 795 | void SkGPipeCanvas::drawPaint(const SkPaint& paint) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 796 | NOTIFY_SETUP(this); |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 797 | this->writePaint(paint); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 798 | if (this->needOpBytes()) { |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 799 | this->writeOp(kDrawPaint_DrawOp); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 800 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | void SkGPipeCanvas::drawPoints(PointMode mode, size_t count, |
| 804 | const SkPoint pts[], const SkPaint& paint) { |
| 805 | if (count) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 806 | NOTIFY_SETUP(this); |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 807 | this->writePaint(paint); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 808 | if (this->needOpBytes(4 + count * sizeof(SkPoint))) { |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 809 | this->writeOp(kDrawPoints_DrawOp, mode, 0); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 810 | fWriter.write32(count); |
| 811 | fWriter.write(pts, count * sizeof(SkPoint)); |
| 812 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 813 | } |
| 814 | } |
| 815 | |
| 816 | void SkGPipeCanvas::drawRect(const SkRect& rect, const SkPaint& paint) { |
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(sizeof(SkRect))) { |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 820 | this->writeOp(kDrawRect_DrawOp); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 821 | fWriter.writeRect(rect); |
| 822 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | void SkGPipeCanvas::drawPath(const SkPath& path, const SkPaint& paint) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 826 | NOTIFY_SETUP(this); |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 827 | this->writePaint(paint); |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 828 | if (this->needOpBytes(path.writeToMemory(NULL))) { |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 829 | this->writeOp(kDrawPath_DrawOp); |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 830 | fWriter.writePath(path); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 831 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 832 | } |
| 833 | |
scroggo@google.com | 5a2e879 | 2012-04-20 17:39:51 +0000 | [diff] [blame] | 834 | void SkGPipeCanvas::drawBitmap(const SkBitmap& bm, SkScalar left, SkScalar top, |
| 835 | const SkPaint* paint) { |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 836 | bool flatten = shouldFlattenBitmaps(fFlags); |
| 837 | const void* ptr = 0; |
| 838 | int bitmapIndex = 0; |
| 839 | if (flatten) { |
| 840 | bitmapIndex = this->flattenToIndex(bm); |
| 841 | } else { |
scroggo@google.com | 4dffc59 | 2012-07-17 16:49:40 +0000 | [diff] [blame] | 842 | ptr = fSharedHeap.addBitmap(bm); |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 843 | if (NULL == ptr) { |
| 844 | return; |
| 845 | } |
scroggo@google.com | 8ae3c7f | 2012-06-13 17:47:52 +0000 | [diff] [blame] | 846 | } |
| 847 | |
scroggo@google.com | 5a2e879 | 2012-04-20 17:39:51 +0000 | [diff] [blame] | 848 | NOTIFY_SETUP(this); |
| 849 | if (paint) { |
| 850 | this->writePaint(*paint); |
| 851 | } |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 852 | |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 853 | size_t opBytesNeeded = sizeof(SkScalar) * 2 + sizeof(bool); |
| 854 | if (!flatten) { |
| 855 | opBytesNeeded += sizeof(void*); |
| 856 | } |
| 857 | if (this->needOpBytes(opBytesNeeded)) { |
| 858 | this->writeOp(kDrawBitmap_DrawOp, 0, bitmapIndex); |
| 859 | if (!flatten) { |
| 860 | fWriter.writePtr(const_cast<void*>(ptr)); |
| 861 | } |
scroggo@google.com | 5a2e879 | 2012-04-20 17:39:51 +0000 | [diff] [blame] | 862 | fWriter.writeBool(paint != NULL); |
| 863 | fWriter.writeScalar(left); |
| 864 | fWriter.writeScalar(top); |
| 865 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 866 | } |
| 867 | |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 868 | void SkGPipeCanvas::drawBitmapRect(const SkBitmap& bm, const SkIRect* src, |
| 869 | const SkRect& dst, const SkPaint* paint) { |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 870 | bool flatten = shouldFlattenBitmaps(fFlags); |
| 871 | const void* ptr = 0; |
| 872 | int bitmapIndex = 0; |
| 873 | if (flatten) { |
| 874 | bitmapIndex = this->flattenToIndex(bm); |
| 875 | } else { |
scroggo@google.com | 4dffc59 | 2012-07-17 16:49:40 +0000 | [diff] [blame] | 876 | ptr = fSharedHeap.addBitmap(bm); |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 877 | if (NULL == ptr) { |
| 878 | return; |
| 879 | } |
scroggo@google.com | 8ae3c7f | 2012-06-13 17:47:52 +0000 | [diff] [blame] | 880 | } |
| 881 | |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 882 | NOTIFY_SETUP(this); |
| 883 | if (paint) { |
| 884 | this->writePaint(*paint); |
| 885 | } |
| 886 | |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 887 | size_t opBytesNeeded = sizeof(SkRect) + sizeof(bool) * 2; |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 888 | bool hasSrc = src != NULL; |
| 889 | if (hasSrc) { |
| 890 | opBytesNeeded += sizeof(int32_t) * 4; |
| 891 | } |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 892 | if (!flatten) { |
| 893 | opBytesNeeded += sizeof(void*); |
| 894 | } |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 895 | if (this->needOpBytes(opBytesNeeded)) { |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 896 | this->writeOp(kDrawBitmapRect_DrawOp, 0, bitmapIndex); |
| 897 | if (!flatten) { |
| 898 | fWriter.writePtr(const_cast<void*>(ptr)); |
| 899 | } |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 900 | fWriter.writeBool(paint != NULL); |
| 901 | fWriter.writeBool(hasSrc); |
| 902 | if (hasSrc) { |
| 903 | fWriter.write32(src->fLeft); |
| 904 | fWriter.write32(src->fTop); |
| 905 | fWriter.write32(src->fRight); |
| 906 | fWriter.write32(src->fBottom); |
| 907 | } |
| 908 | fWriter.writeRect(dst); |
| 909 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | void SkGPipeCanvas::drawBitmapMatrix(const SkBitmap&, const SkMatrix&, |
| 913 | const SkPaint*) { |
| 914 | UNIMPLEMENTED |
| 915 | } |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 916 | |
| 917 | void SkGPipeCanvas::drawBitmapNine(const SkBitmap& bm, const SkIRect& center, |
scroggo@google.com | 5a2e879 | 2012-04-20 17:39:51 +0000 | [diff] [blame] | 918 | const SkRect& dst, const SkPaint* paint) { |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 919 | bool flatten = shouldFlattenBitmaps(fFlags); |
| 920 | const void* ptr = 0; |
| 921 | int bitmapIndex = 0; |
| 922 | if (flatten) { |
| 923 | bitmapIndex = this->flattenToIndex(bm); |
| 924 | } else { |
scroggo@google.com | 4dffc59 | 2012-07-17 16:49:40 +0000 | [diff] [blame] | 925 | ptr = fSharedHeap.addBitmap(bm); |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 926 | if (NULL == ptr) { |
| 927 | return; |
| 928 | } |
scroggo@google.com | 8ae3c7f | 2012-06-13 17:47:52 +0000 | [diff] [blame] | 929 | } |
| 930 | |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 931 | NOTIFY_SETUP(this); |
| 932 | if (paint) { |
| 933 | this->writePaint(*paint); |
| 934 | } |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 935 | |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 936 | size_t opBytesNeeded = sizeof(int32_t) * 4 + sizeof(bool) + sizeof(SkRect); |
| 937 | if (!flatten) { |
| 938 | opBytesNeeded += sizeof(void*); |
| 939 | } |
| 940 | if (this->needOpBytes(opBytesNeeded)) { |
| 941 | this->writeOp(kDrawBitmapNine_DrawOp, 0, bitmapIndex); |
| 942 | if (!flatten) { |
| 943 | fWriter.writePtr(const_cast<void*>(ptr)); |
| 944 | } |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 945 | fWriter.writeBool(paint != NULL); |
| 946 | fWriter.write32(center.fLeft); |
| 947 | fWriter.write32(center.fTop); |
| 948 | fWriter.write32(center.fRight); |
| 949 | fWriter.write32(center.fBottom); |
| 950 | fWriter.writeRect(dst); |
| 951 | } |
scroggo@google.com | 5a2e879 | 2012-04-20 17:39:51 +0000 | [diff] [blame] | 952 | } |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 953 | |
| 954 | void SkGPipeCanvas::drawSprite(const SkBitmap& bm, int left, int top, |
| 955 | const SkPaint* paint) { |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 956 | bool flatten = shouldFlattenBitmaps(fFlags); |
| 957 | const void* ptr = 0; |
| 958 | int bitmapIndex = 0; |
| 959 | if (flatten) { |
| 960 | bitmapIndex = this->flattenToIndex(bm); |
| 961 | } else { |
scroggo@google.com | 4dffc59 | 2012-07-17 16:49:40 +0000 | [diff] [blame] | 962 | ptr = fSharedHeap.addBitmap(bm); |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 963 | if (NULL == ptr) { |
| 964 | return; |
| 965 | } |
scroggo@google.com | 8ae3c7f | 2012-06-13 17:47:52 +0000 | [diff] [blame] | 966 | } |
| 967 | |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 968 | NOTIFY_SETUP(this); |
| 969 | if (paint) { |
| 970 | this->writePaint(*paint); |
| 971 | } |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 972 | |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 973 | size_t opBytesNeeded = sizeof(int32_t) * 2 + sizeof(bool); |
| 974 | if (!flatten) { |
| 975 | opBytesNeeded += sizeof(void*); |
| 976 | } |
| 977 | if (this->needOpBytes(opBytesNeeded)) { |
| 978 | this->writeOp(kDrawSprite_DrawOp, 0, bitmapIndex); |
| 979 | if (!flatten) { |
| 980 | fWriter.writePtr(const_cast<void*>(ptr)); |
| 981 | } |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 982 | fWriter.writeBool(paint != NULL); |
| 983 | fWriter.write32(left); |
| 984 | fWriter.write32(top); |
| 985 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 986 | } |
| 987 | |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 988 | void SkGPipeCanvas::drawText(const void* text, size_t byteLength, SkScalar x, |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 989 | SkScalar y, 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 | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 993 | if (this->needOpBytes(4 + SkAlign4(byteLength) + 2 * sizeof(SkScalar))) { |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 994 | this->writeOp(kDrawText_DrawOp); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 995 | fWriter.write32(byteLength); |
| 996 | fWriter.writePad(text, byteLength); |
| 997 | fWriter.writeScalar(x); |
| 998 | fWriter.writeScalar(y); |
| 999 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1000 | } |
| 1001 | } |
| 1002 | |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 1003 | void SkGPipeCanvas::drawPosText(const void* text, size_t byteLength, |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1004 | const SkPoint pos[], 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(SkPoint))) { |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 1010 | this->writeOp(kDrawPosText_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(pos, count * sizeof(SkPoint)); |
| 1015 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | void SkGPipeCanvas::drawPosTextH(const void* text, size_t byteLength, |
| 1020 | const SkScalar xpos[], SkScalar constY, |
| 1021 | const SkPaint& paint) { |
| 1022 | if (byteLength) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1023 | NOTIFY_SETUP(this); |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 1024 | this->writePaint(paint); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1025 | int count = paint.textToGlyphs(text, byteLength, NULL); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1026 | if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkScalar) + 4)) { |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 1027 | this->writeOp(kDrawPosTextH_DrawOp); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1028 | fWriter.write32(byteLength); |
| 1029 | fWriter.writePad(text, byteLength); |
| 1030 | fWriter.write32(count); |
| 1031 | fWriter.write(xpos, count * sizeof(SkScalar)); |
| 1032 | fWriter.writeScalar(constY); |
| 1033 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1034 | } |
| 1035 | } |
| 1036 | |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 1037 | void SkGPipeCanvas::drawTextOnPath(const void* text, size_t byteLength, |
| 1038 | const SkPath& path, const SkMatrix* matrix, |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1039 | const SkPaint& paint) { |
| 1040 | if (byteLength) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1041 | NOTIFY_SETUP(this); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1042 | unsigned flags = 0; |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 1043 | size_t size = 4 + SkAlign4(byteLength) + path.writeToMemory(NULL); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1044 | if (matrix) { |
| 1045 | flags |= kDrawTextOnPath_HasMatrix_DrawOpFlag; |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 1046 | size += matrix->writeToMemory(NULL); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1047 | } |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 1048 | this->writePaint(paint); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1049 | if (this->needOpBytes(size)) { |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 1050 | this->writeOp(kDrawTextOnPath_DrawOp, flags, 0); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1051 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1052 | fWriter.write32(byteLength); |
| 1053 | fWriter.writePad(text, byteLength); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1054 | |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 1055 | fWriter.writePath(path); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1056 | if (matrix) { |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 1057 | fWriter.writeMatrix(*matrix); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1058 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1059 | } |
| 1060 | } |
| 1061 | } |
| 1062 | |
| 1063 | void SkGPipeCanvas::drawPicture(SkPicture& picture) { |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 1064 | // we want to playback the picture into individual draw calls |
| 1065 | this->INHERITED::drawPicture(picture); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1066 | } |
| 1067 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1068 | void SkGPipeCanvas::drawVertices(VertexMode mode, int vertexCount, |
| 1069 | const SkPoint vertices[], const SkPoint texs[], |
| 1070 | const SkColor colors[], SkXfermode*, |
| 1071 | const uint16_t indices[], int indexCount, |
| 1072 | const SkPaint& paint) { |
| 1073 | if (0 == vertexCount) { |
| 1074 | return; |
| 1075 | } |
| 1076 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1077 | NOTIFY_SETUP(this); |
| 1078 | size_t size = 4 + vertexCount * sizeof(SkPoint); |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 1079 | this->writePaint(paint); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1080 | unsigned flags = 0; |
| 1081 | if (texs) { |
| 1082 | flags |= kDrawVertices_HasTexs_DrawOpFlag; |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1083 | size += vertexCount * sizeof(SkPoint); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1084 | } |
| 1085 | if (colors) { |
| 1086 | flags |= kDrawVertices_HasColors_DrawOpFlag; |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1087 | size += vertexCount * sizeof(SkColor); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1088 | } |
| 1089 | if (indices && indexCount > 0) { |
| 1090 | flags |= kDrawVertices_HasIndices_DrawOpFlag; |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1091 | size += 4 + SkAlign4(indexCount * sizeof(uint16_t)); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1092 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 1093 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1094 | if (this->needOpBytes(size)) { |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 1095 | this->writeOp(kDrawVertices_DrawOp, flags, 0); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1096 | fWriter.write32(mode); |
| 1097 | fWriter.write32(vertexCount); |
| 1098 | fWriter.write(vertices, vertexCount * sizeof(SkPoint)); |
| 1099 | if (texs) { |
| 1100 | fWriter.write(texs, vertexCount * sizeof(SkPoint)); |
| 1101 | } |
| 1102 | if (colors) { |
| 1103 | fWriter.write(colors, vertexCount * sizeof(SkColor)); |
| 1104 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1105 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1106 | // TODO: flatten xfermode |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1107 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1108 | if (indices && indexCount > 0) { |
| 1109 | fWriter.write32(indexCount); |
| 1110 | fWriter.writePad(indices, indexCount * sizeof(uint16_t)); |
| 1111 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1112 | } |
| 1113 | } |
| 1114 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1115 | void SkGPipeCanvas::drawData(const void* ptr, size_t size) { |
| 1116 | if (size && ptr) { |
| 1117 | NOTIFY_SETUP(this); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1118 | unsigned data = 0; |
| 1119 | if (size < (1 << DRAWOPS_DATA_BITS)) { |
| 1120 | data = (unsigned)size; |
| 1121 | } |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1122 | if (this->needOpBytes(4 + SkAlign4(size))) { |
| 1123 | this->writeOp(kDrawData_DrawOp, 0, data); |
| 1124 | if (0 == data) { |
| 1125 | fWriter.write32(size); |
| 1126 | } |
reed@google.com | bb6793b | 2011-05-05 15:18:15 +0000 | [diff] [blame] | 1127 | fWriter.writePad(ptr, size); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1128 | } |
| 1129 | } |
| 1130 | } |
| 1131 | |
junov@chromium.org | 77eec24 | 2012-07-18 17:54:45 +0000 | [diff] [blame] | 1132 | void SkGPipeCanvas::flushRecording(bool detachCurrentBlock) { |
| 1133 | doNotify(); |
| 1134 | if (detachCurrentBlock) { |
| 1135 | // force a new block to be requested for the next recorded command |
| 1136 | fBlockSize = 0; |
| 1137 | } |
| 1138 | } |
| 1139 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1140 | /////////////////////////////////////////////////////////////////////////////// |
| 1141 | |
| 1142 | template <typename T> uint32_t castToU32(T value) { |
| 1143 | union { |
| 1144 | T fSrc; |
| 1145 | uint32_t fDst; |
| 1146 | } data; |
| 1147 | data.fSrc = value; |
| 1148 | return data.fDst; |
| 1149 | } |
| 1150 | |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 1151 | void SkGPipeCanvas::writePaint(const SkPaint& paint) { |
| 1152 | SkPaint& base = fPaint; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1153 | uint32_t storage[32]; |
| 1154 | uint32_t* ptr = storage; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1155 | |
| 1156 | if (base.getFlags() != paint.getFlags()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1157 | *ptr++ = PaintOp_packOpData(kFlags_PaintOp, paint.getFlags()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 1158 | base.setFlags(paint.getFlags()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1159 | } |
| 1160 | if (base.getColor() != paint.getColor()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1161 | *ptr++ = PaintOp_packOp(kColor_PaintOp); |
| 1162 | *ptr++ = paint.getColor(); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 1163 | base.setColor(paint.getColor()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1164 | } |
| 1165 | if (base.getStyle() != paint.getStyle()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1166 | *ptr++ = PaintOp_packOpData(kStyle_PaintOp, paint.getStyle()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 1167 | base.setStyle(paint.getStyle()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1168 | } |
| 1169 | if (base.getStrokeJoin() != paint.getStrokeJoin()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1170 | *ptr++ = PaintOp_packOpData(kJoin_PaintOp, paint.getStrokeJoin()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 1171 | base.setStrokeJoin(paint.getStrokeJoin()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1172 | } |
| 1173 | if (base.getStrokeCap() != paint.getStrokeCap()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1174 | *ptr++ = PaintOp_packOpData(kCap_PaintOp, paint.getStrokeCap()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 1175 | base.setStrokeCap(paint.getStrokeCap()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1176 | } |
| 1177 | if (base.getStrokeWidth() != paint.getStrokeWidth()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1178 | *ptr++ = PaintOp_packOp(kWidth_PaintOp); |
| 1179 | *ptr++ = castToU32(paint.getStrokeWidth()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 1180 | base.setStrokeWidth(paint.getStrokeWidth()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1181 | } |
| 1182 | if (base.getStrokeMiter() != paint.getStrokeMiter()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1183 | *ptr++ = PaintOp_packOp(kMiter_PaintOp); |
| 1184 | *ptr++ = castToU32(paint.getStrokeMiter()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 1185 | base.setStrokeMiter(paint.getStrokeMiter()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1186 | } |
| 1187 | if (base.getTextEncoding() != paint.getTextEncoding()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1188 | *ptr++ = PaintOp_packOpData(kEncoding_PaintOp, paint.getTextEncoding()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 1189 | base.setTextEncoding(paint.getTextEncoding()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1190 | } |
| 1191 | if (base.getHinting() != paint.getHinting()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1192 | *ptr++ = PaintOp_packOpData(kHinting_PaintOp, paint.getHinting()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 1193 | base.setHinting(paint.getHinting()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1194 | } |
| 1195 | if (base.getTextAlign() != paint.getTextAlign()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1196 | *ptr++ = PaintOp_packOpData(kAlign_PaintOp, paint.getTextAlign()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 1197 | base.setTextAlign(paint.getTextAlign()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1198 | } |
| 1199 | if (base.getTextSize() != paint.getTextSize()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1200 | *ptr++ = PaintOp_packOp(kTextSize_PaintOp); |
| 1201 | *ptr++ = castToU32(paint.getTextSize()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 1202 | base.setTextSize(paint.getTextSize()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1203 | } |
| 1204 | if (base.getTextScaleX() != paint.getTextScaleX()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1205 | *ptr++ = PaintOp_packOp(kTextScaleX_PaintOp); |
| 1206 | *ptr++ = castToU32(paint.getTextScaleX()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 1207 | base.setTextScaleX(paint.getTextScaleX()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1208 | } |
| 1209 | if (base.getTextSkewX() != paint.getTextSkewX()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1210 | *ptr++ = PaintOp_packOp(kTextSkewX_PaintOp); |
| 1211 | *ptr++ = castToU32(paint.getTextSkewX()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 1212 | base.setTextSkewX(paint.getTextSkewX()); |
| 1213 | } |
| 1214 | |
| 1215 | if (!SkTypeface::Equal(base.getTypeface(), paint.getTypeface())) { |
| 1216 | uint32_t id = this->getTypefaceID(paint.getTypeface()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 1217 | *ptr++ = PaintOp_packOpData(kTypeface_PaintOp, id); |
| 1218 | base.setTypeface(paint.getTypeface()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1219 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1220 | |
scroggo@google.com | 4dffc59 | 2012-07-17 16:49:40 +0000 | [diff] [blame] | 1221 | // This is a new paint, so all old flats can be safely purged, if necessary. |
| 1222 | fFlattenableHeap.markAllFlatsSafeToDelete(); |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 1223 | for (int i = 0; i < kCount_PaintFlats; i++) { |
| 1224 | int index = this->flattenToIndex(get_paintflat(paint, i), (PaintFlats)i); |
scroggo@google.com | d3ba5cc | 2012-07-09 16:05:53 +0000 | [diff] [blame] | 1225 | bool replaced = index < 0; |
| 1226 | if (replaced) { |
| 1227 | index = ~index; |
| 1228 | } |
scroggo@google.com | 4dffc59 | 2012-07-17 16:49:40 +0000 | [diff] [blame] | 1229 | // Store the index of any flat that needs to be kept. 0 means no flat. |
| 1230 | if (index > 0) { |
| 1231 | fFlattenableHeap.markFlatForKeeping(index); |
| 1232 | } |
| 1233 | SkASSERT(index >= 0 && index <= fFlatDictionary.count()); |
scroggo@google.com | d3ba5cc | 2012-07-09 16:05:53 +0000 | [diff] [blame] | 1234 | if (index != fCurrFlatIndex[i] || replaced) { |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 1235 | *ptr++ = PaintOp_packOpFlagData(kFlatIndex_PaintOp, i, index); |
| 1236 | fCurrFlatIndex[i] = index; |
| 1237 | } |
| 1238 | } |
| 1239 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1240 | size_t size = (char*)ptr - (char*)storage; |
| 1241 | if (size && this->needOpBytes(size)) { |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 1242 | this->writeOp(kPaintOp_DrawOp, 0, size); |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 1243 | fWriter.write(storage, size); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1244 | for (size_t i = 0; i < size/4; i++) { |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 1245 | // SkDebugf("[%d] %08X\n", i, storage[i]); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1246 | } |
| 1247 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1248 | } |
| 1249 | |
| 1250 | /////////////////////////////////////////////////////////////////////////////// |
| 1251 | |
| 1252 | #include "SkGPipe.h" |
| 1253 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1254 | SkGPipeWriter::SkGPipeWriter() : fWriter(0) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1255 | fCanvas = NULL; |
| 1256 | } |
| 1257 | |
| 1258 | SkGPipeWriter::~SkGPipeWriter() { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1259 | this->endRecording(); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1260 | SkSafeUnref(fCanvas); |
| 1261 | } |
| 1262 | |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 1263 | SkCanvas* SkGPipeWriter::startRecording(SkGPipeController* controller, uint32_t flags) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1264 | if (NULL == fCanvas) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1265 | fWriter.reset(NULL, 0); |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 1266 | fFactorySet.reset(); |
| 1267 | fCanvas = SkNEW_ARGS(SkGPipeCanvas, (controller, &fWriter, |
| 1268 | (flags & kCrossProcess_Flag) ? |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 1269 | &fFactorySet : NULL, flags)); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1270 | } |
| 1271 | return fCanvas; |
| 1272 | } |
| 1273 | |
| 1274 | void SkGPipeWriter::endRecording() { |
| 1275 | if (fCanvas) { |
| 1276 | fCanvas->finish(); |
| 1277 | fCanvas->unref(); |
| 1278 | fCanvas = NULL; |
| 1279 | } |
| 1280 | } |
| 1281 | |
junov@chromium.org | 77eec24 | 2012-07-18 17:54:45 +0000 | [diff] [blame] | 1282 | void SkGPipeWriter::flushRecording(bool detachCurrentBlock){ |
| 1283 | fCanvas->flushRecording(detachCurrentBlock); |
| 1284 | } |
| 1285 | |
scroggo@google.com | 15011ee | 2012-07-26 20:03:32 +0000 | [diff] [blame^] | 1286 | size_t SkGPipeWriter::storageAllocatedForRecording() { |
| 1287 | return NULL == fCanvas ? 0 : fCanvas->storageAllocatedForRecording(); |
| 1288 | } |
| 1289 | |