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