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