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 | |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 9 | #include "SkAnnotation.h" |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 10 | #include "SkBitmapDevice.h" |
scroggo@google.com | 10dccde | 2012-08-08 20:43:22 +0000 | [diff] [blame] | 11 | #include "SkBitmapHeap.h" |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 12 | #include "SkCanvas.h" |
scroggo@google.com | 10dccde | 2012-08-08 20:43:22 +0000 | [diff] [blame] | 13 | #include "SkColorFilter.h" |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 14 | #include "SkData.h" |
scroggo@google.com | 10dccde | 2012-08-08 20:43:22 +0000 | [diff] [blame] | 15 | #include "SkDrawLooper.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" |
scroggo@google.com | 10dccde | 2012-08-08 20:43:22 +0000 | [diff] [blame] | 19 | #include "SkMaskFilter.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 20 | #include "SkWriteBuffer.h" |
scroggo@google.com | 10dccde | 2012-08-08 20:43:22 +0000 | [diff] [blame] | 21 | #include "SkPaint.h" |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 22 | #include "SkPatchUtils.h" |
scroggo@google.com | 10dccde | 2012-08-08 20:43:22 +0000 | [diff] [blame] | 23 | #include "SkPathEffect.h" |
| 24 | #include "SkPictureFlat.h" |
fmalita | acb882c | 2014-09-16 17:58:34 -0700 | [diff] [blame] | 25 | #include "SkPtrRecorder.h" |
scroggo@google.com | 10dccde | 2012-08-08 20:43:22 +0000 | [diff] [blame] | 26 | #include "SkRasterizer.h" |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 27 | #include "SkRRect.h" |
scroggo@google.com | 10dccde | 2012-08-08 20:43:22 +0000 | [diff] [blame] | 28 | #include "SkShader.h" |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 29 | #include "SkStream.h" |
fmalita | 228a6f2 | 2014-08-28 13:59:42 -0700 | [diff] [blame] | 30 | #include "SkTextBlob.h" |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 31 | #include "SkTSearch.h" |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 32 | #include "SkTypeface.h" |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 33 | #include "SkWriter32.h" |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 34 | |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 35 | enum { |
| 36 | kSizeOfFlatRRect = sizeof(SkRect) + 4 * sizeof(SkVector) |
| 37 | }; |
| 38 | |
fmalita | acb882c | 2014-09-16 17:58:34 -0700 | [diff] [blame] | 39 | static bool is_cross_process(uint32_t flags) { |
scroggo@google.com | 0c3e5fe | 2012-08-01 19:34:20 +0000 | [diff] [blame] | 40 | return SkToBool(flags & SkGPipeWriter::kCrossProcess_Flag); |
| 41 | } |
| 42 | |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 43 | static SkFlattenable* get_paintflat(const SkPaint& paint, unsigned paintFlat) { |
| 44 | SkASSERT(paintFlat < kCount_PaintFlats); |
| 45 | switch (paintFlat) { |
| 46 | case kColorFilter_PaintFlat: return paint.getColorFilter(); |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 47 | case kDrawLooper_PaintFlat: return paint.getLooper(); |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 48 | case kMaskFilter_PaintFlat: return paint.getMaskFilter(); |
| 49 | case kPathEffect_PaintFlat: return paint.getPathEffect(); |
| 50 | case kRasterizer_PaintFlat: return paint.getRasterizer(); |
| 51 | case kShader_PaintFlat: return paint.getShader(); |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 52 | case kImageFilter_PaintFlat: return paint.getImageFilter(); |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 53 | case kXfermode_PaintFlat: return paint.getXfermode(); |
| 54 | } |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 55 | SkDEBUGFAIL("never gets here"); |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 56 | return NULL; |
| 57 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 58 | |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 59 | static size_t writeTypeface(SkWriter32* writer, SkTypeface* typeface) { |
| 60 | SkASSERT(typeface); |
| 61 | SkDynamicMemoryWStream stream; |
| 62 | typeface->serialize(&stream); |
| 63 | size_t size = stream.getOffset(); |
| 64 | if (writer) { |
commit-bot@chromium.org | b45bd1f | 2014-04-24 18:17:30 +0000 | [diff] [blame] | 65 | writer->write32(SkToU32(size)); |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 66 | SkAutoDataUnref data(stream.copyToData()); |
robertphillips@google.com | 59f46b8 | 2012-07-10 17:30:58 +0000 | [diff] [blame] | 67 | writer->writePad(data->data(), size); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 68 | } |
scroggo@google.com | 5af9b20 | 2012-06-04 17:17:36 +0000 | [diff] [blame] | 69 | return 4 + SkAlign4(size); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 70 | } |
| 71 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 72 | /////////////////////////////////////////////////////////////////////////////// |
| 73 | |
scroggo@google.com | 4dffc59 | 2012-07-17 16:49:40 +0000 | [diff] [blame] | 74 | class FlattenableHeap : public SkFlatController { |
| 75 | public: |
scroggo@google.com | 664fab1 | 2012-08-14 19:22:05 +0000 | [diff] [blame] | 76 | FlattenableHeap(int numFlatsToKeep, SkNamedFactorySet* fset, bool isCrossProcess) |
commit-bot@chromium.org | a2bd2d1 | 2014-01-30 22:16:32 +0000 | [diff] [blame] | 77 | : INHERITED(isCrossProcess ? SkWriteBuffer::kCrossProcess_Flag : 0) |
| 78 | , fNumFlatsToKeep(numFlatsToKeep) { |
scroggo@google.com | 664fab1 | 2012-08-14 19:22:05 +0000 | [diff] [blame] | 79 | SkASSERT((isCrossProcess && fset != NULL) || (!isCrossProcess && NULL == fset)); |
| 80 | if (isCrossProcess) { |
| 81 | this->setNamedFactorySet(fset); |
scroggo@google.com | 664fab1 | 2012-08-14 19:22:05 +0000 | [diff] [blame] | 82 | } |
scroggo@google.com | 0c3e5fe | 2012-08-01 19:34:20 +0000 | [diff] [blame] | 83 | } |
scroggo@google.com | 4dffc59 | 2012-07-17 16:49:40 +0000 | [diff] [blame] | 84 | |
| 85 | ~FlattenableHeap() { |
| 86 | fPointers.freeAll(); |
| 87 | } |
| 88 | |
| 89 | virtual void* allocThrow(size_t bytes) SK_OVERRIDE; |
| 90 | |
| 91 | virtual void unalloc(void* ptr) SK_OVERRIDE; |
| 92 | |
scroggo@google.com | 7ca2443 | 2012-08-14 15:48:43 +0000 | [diff] [blame] | 93 | void setBitmapStorage(SkBitmapHeap* heap) { |
| 94 | this->setBitmapHeap(heap); |
| 95 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 96 | |
scroggo@google.com | 4dffc59 | 2012-07-17 16:49:40 +0000 | [diff] [blame] | 97 | const SkFlatData* flatToReplace() const; |
| 98 | |
| 99 | // Mark an SkFlatData as one that should not be returned by flatToReplace. |
| 100 | // Takes the result of SkFlatData::index() as its parameter. |
| 101 | void markFlatForKeeping(int index) { |
| 102 | *fFlatsThatMustBeKept.append() = index; |
| 103 | } |
| 104 | |
| 105 | void markAllFlatsSafeToDelete() { |
| 106 | fFlatsThatMustBeKept.reset(); |
| 107 | } |
| 108 | |
| 109 | private: |
| 110 | // Keep track of the indices (i.e. the result of SkFlatData::index()) of |
| 111 | // flats that must be kept, since they are on the current paint. |
| 112 | SkTDArray<int> fFlatsThatMustBeKept; |
| 113 | SkTDArray<void*> fPointers; |
| 114 | const int fNumFlatsToKeep; |
commit-bot@chromium.org | a2bd2d1 | 2014-01-30 22:16:32 +0000 | [diff] [blame] | 115 | |
| 116 | typedef SkFlatController INHERITED; |
scroggo@google.com | 4dffc59 | 2012-07-17 16:49:40 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | void FlattenableHeap::unalloc(void* ptr) { |
| 120 | int indexToRemove = fPointers.rfind(ptr); |
| 121 | if (indexToRemove >= 0) { |
| 122 | sk_free(ptr); |
| 123 | fPointers.remove(indexToRemove); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | void* FlattenableHeap::allocThrow(size_t bytes) { |
| 128 | void* ptr = sk_malloc_throw(bytes); |
| 129 | *fPointers.append() = ptr; |
| 130 | return ptr; |
| 131 | } |
| 132 | |
| 133 | const SkFlatData* FlattenableHeap::flatToReplace() const { |
| 134 | // First, determine whether we should replace one. |
| 135 | if (fPointers.count() > fNumFlatsToKeep) { |
| 136 | // Look through the flattenable heap. |
| 137 | // TODO: Return the LRU flat. |
| 138 | for (int i = 0; i < fPointers.count(); i++) { |
| 139 | SkFlatData* potential = (SkFlatData*)fPointers[i]; |
| 140 | // Make sure that it is not one that must be kept. |
| 141 | bool mustKeep = false; |
| 142 | for (int j = 0; j < fFlatsThatMustBeKept.count(); j++) { |
| 143 | if (potential->index() == fFlatsThatMustBeKept[j]) { |
| 144 | mustKeep = true; |
| 145 | break; |
| 146 | } |
| 147 | } |
| 148 | if (!mustKeep) { |
| 149 | return potential; |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | return NULL; |
| 154 | } |
| 155 | |
| 156 | /////////////////////////////////////////////////////////////////////////////// |
| 157 | |
commit-bot@chromium.org | 07adb63 | 2014-01-02 22:20:49 +0000 | [diff] [blame] | 158 | struct SkFlattenableTraits { |
commit-bot@chromium.org | 186c0cc | 2014-02-18 16:15:05 +0000 | [diff] [blame] | 159 | static void Flatten(SkWriteBuffer& buffer, const SkFlattenable& flattenable) { |
commit-bot@chromium.org | 07adb63 | 2014-01-02 22:20:49 +0000 | [diff] [blame] | 160 | buffer.writeFlattenable(&flattenable); |
scroggo@google.com | 4dffc59 | 2012-07-17 16:49:40 +0000 | [diff] [blame] | 161 | } |
commit-bot@chromium.org | 07adb63 | 2014-01-02 22:20:49 +0000 | [diff] [blame] | 162 | // No need to define unflatten if we never call it. |
scroggo@google.com | 4dffc59 | 2012-07-17 16:49:40 +0000 | [diff] [blame] | 163 | }; |
commit-bot@chromium.org | 07adb63 | 2014-01-02 22:20:49 +0000 | [diff] [blame] | 164 | typedef SkFlatDictionary<SkFlattenable, SkFlattenableTraits> FlatDictionary; |
scroggo@google.com | 4dffc59 | 2012-07-17 16:49:40 +0000 | [diff] [blame] | 165 | |
| 166 | /////////////////////////////////////////////////////////////////////////////// |
| 167 | |
scroggo@google.com | 59c3ab6 | 2013-11-12 14:32:38 +0000 | [diff] [blame] | 168 | /** |
| 169 | * If SkBitmaps are to be flattened to send to the reader, this class is |
| 170 | * provided to the SkBitmapHeap to tell the SkGPipeCanvas to do so. |
| 171 | */ |
| 172 | class BitmapShuttle : public SkBitmapHeap::ExternalStorage { |
| 173 | public: |
| 174 | BitmapShuttle(SkGPipeCanvas*); |
| 175 | |
| 176 | ~BitmapShuttle(); |
| 177 | |
| 178 | virtual bool insert(const SkBitmap& bitmap, int32_t slot) SK_OVERRIDE; |
| 179 | |
| 180 | /** |
| 181 | * Remove the SkGPipeCanvas used for insertion. After this, calls to |
| 182 | * insert will crash. |
| 183 | */ |
| 184 | void removeCanvas(); |
| 185 | |
| 186 | private: |
| 187 | SkGPipeCanvas* fCanvas; |
| 188 | }; |
| 189 | |
| 190 | /////////////////////////////////////////////////////////////////////////////// |
| 191 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 192 | class SkGPipeCanvas : public SkCanvas { |
| 193 | public: |
junov@chromium.org | a8db8fe | 2012-08-15 19:49:22 +0000 | [diff] [blame] | 194 | SkGPipeCanvas(SkGPipeController*, SkWriter32*, uint32_t flags, |
| 195 | uint32_t width, uint32_t height); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 196 | virtual ~SkGPipeCanvas(); |
| 197 | |
scroggo@google.com | 59c3ab6 | 2013-11-12 14:32:38 +0000 | [diff] [blame] | 198 | /** |
| 199 | * Called when nothing else is to be written to the stream. Any repeated |
| 200 | * calls are ignored. |
| 201 | * |
| 202 | * @param notifyReaders Whether to send a message to the reader(s) that |
| 203 | * the writer is through sending commands. Should generally be true, |
| 204 | * unless there is an error which prevents further messages from |
| 205 | * being sent. |
| 206 | */ |
| 207 | void finish(bool notifyReaders) { |
| 208 | if (fDone) { |
| 209 | return; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 210 | } |
scroggo@google.com | 59c3ab6 | 2013-11-12 14:32:38 +0000 | [diff] [blame] | 211 | if (notifyReaders && this->needOpBytes()) { |
| 212 | this->writeOp(kDone_DrawOp); |
| 213 | this->doNotify(); |
| 214 | } |
| 215 | if (shouldFlattenBitmaps(fFlags)) { |
| 216 | // The following circular references exist: |
| 217 | // fFlattenableHeap -> fWriteBuffer -> fBitmapStorage -> fExternalStorage -> fCanvas |
| 218 | // fBitmapHeap -> fExternalStorage -> fCanvas |
| 219 | // fFlattenableHeap -> fBitmapStorage -> fExternalStorage -> fCanvas |
| 220 | |
| 221 | // Break them all by destroying the final link to this SkGPipeCanvas. |
| 222 | fBitmapShuttle->removeCanvas(); |
| 223 | } |
| 224 | fDone = true; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 225 | } |
| 226 | |
junov@chromium.org | 77eec24 | 2012-07-18 17:54:45 +0000 | [diff] [blame] | 227 | void flushRecording(bool detachCurrentBlock); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 228 | size_t freeMemoryIfPossible(size_t bytesToFree); |
junov@chromium.org | 77eec24 | 2012-07-18 17:54:45 +0000 | [diff] [blame] | 229 | |
scroggo@google.com | 15011ee | 2012-07-26 20:03:32 +0000 | [diff] [blame] | 230 | size_t storageAllocatedForRecording() { |
scroggo@google.com | d5d158b | 2012-08-14 20:38:28 +0000 | [diff] [blame] | 231 | return (NULL == fBitmapHeap) ? 0 : fBitmapHeap->bytesAllocated(); |
scroggo@google.com | 15011ee | 2012-07-26 20:03:32 +0000 | [diff] [blame] | 232 | } |
| 233 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 234 | // overrides from SkCanvas |
junov@chromium.org | fbe9c8f | 2012-07-18 20:22:52 +0000 | [diff] [blame] | 235 | virtual bool isDrawingToLayer() const SK_OVERRIDE; |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 236 | virtual void clear(SkColor) SK_OVERRIDE; |
| 237 | virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 238 | virtual void drawPoints(PointMode, size_t count, const SkPoint pts[], |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 239 | const SkPaint&) SK_OVERRIDE; |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 240 | virtual void drawOval(const SkRect&, const SkPaint&) SK_OVERRIDE; |
bsalomon@google.com | 7ce564c | 2013-10-22 16:54:15 +0000 | [diff] [blame] | 241 | virtual void drawRect(const SkRect& rect, const SkPaint&) SK_OVERRIDE; |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 242 | virtual void drawRRect(const SkRRect&, const SkPaint&) SK_OVERRIDE; |
bsalomon@google.com | 7ce564c | 2013-10-22 16:54:15 +0000 | [diff] [blame] | 243 | virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 244 | virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top, |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 245 | const SkPaint*) SK_OVERRIDE; |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 246 | virtual void drawBitmapRectToRect(const SkBitmap&, const SkRect* src, |
skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 247 | const SkRect& dst, const SkPaint* paint, |
commit-bot@chromium.org | eed779d | 2013-08-16 10:24:37 +0000 | [diff] [blame] | 248 | DrawBitmapRectFlags flags) SK_OVERRIDE; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 249 | virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&, |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 250 | const SkPaint*) SK_OVERRIDE; |
scroggo@google.com | 5a2e879 | 2012-04-20 17:39:51 +0000 | [diff] [blame] | 251 | virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, |
| 252 | const SkRect& dst, const SkPaint* paint = NULL) SK_OVERRIDE; |
piotaixr | 9bb7539 | 2014-10-22 11:02:32 -0700 | [diff] [blame] | 253 | virtual void drawImage(const SkImage* image, SkScalar left, SkScalar top, |
| 254 | const SkPaint* paint) SK_OVERRIDE; |
| 255 | virtual void drawImageRect(const SkImage* image, const SkRect* src, |
| 256 | const SkRect& dst, |
| 257 | const SkPaint* paint) SK_OVERRIDE; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 258 | virtual void drawSprite(const SkBitmap&, int left, int top, |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 259 | const SkPaint*) SK_OVERRIDE; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 260 | virtual void drawVertices(VertexMode, int vertexCount, |
| 261 | const SkPoint vertices[], const SkPoint texs[], |
| 262 | const SkColor colors[], SkXfermode*, |
| 263 | const uint16_t indices[], int indexCount, |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 264 | const SkPaint&) SK_OVERRIDE; |
| 265 | virtual void drawData(const void*, size_t) SK_OVERRIDE; |
robertphillips@google.com | 0a4805e | 2013-05-29 13:24:23 +0000 | [diff] [blame] | 266 | virtual void beginCommentGroup(const char* description) SK_OVERRIDE; |
| 267 | virtual void addComment(const char* kywd, const char* value) SK_OVERRIDE; |
| 268 | virtual void endCommentGroup() SK_OVERRIDE; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 269 | |
scroggo@google.com | 3e26bd0 | 2012-08-14 15:20:01 +0000 | [diff] [blame] | 270 | /** |
| 271 | * Flatten an SkBitmap to send to the reader, where it will be referenced |
| 272 | * according to slot. |
| 273 | */ |
| 274 | bool shuttleBitmap(const SkBitmap&, int32_t slot); |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 275 | |
| 276 | protected: |
Florin Malita | 5f6102d | 2014-06-30 10:13:28 -0400 | [diff] [blame] | 277 | virtual void willSave() SK_OVERRIDE; |
commit-bot@chromium.org | e54a23f | 2014-03-12 20:21:48 +0000 | [diff] [blame] | 278 | virtual SaveLayerStrategy willSaveLayer(const SkRect*, const SkPaint*, SaveFlags) SK_OVERRIDE; |
| 279 | virtual void willRestore() SK_OVERRIDE; |
| 280 | |
commit-bot@chromium.org | 44c48d0 | 2014-03-13 20:03:58 +0000 | [diff] [blame] | 281 | virtual void didConcat(const SkMatrix&) SK_OVERRIDE; |
| 282 | virtual void didSetMatrix(const SkMatrix&) SK_OVERRIDE; |
| 283 | |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 284 | virtual void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) SK_OVERRIDE; |
reed@google.com | e0d9ce8 | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 285 | virtual void onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y, |
| 286 | const SkPaint&) SK_OVERRIDE; |
| 287 | virtual void onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[], |
| 288 | const SkPaint&) SK_OVERRIDE; |
| 289 | virtual void onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[], |
| 290 | SkScalar constY, const SkPaint&) SK_OVERRIDE; |
| 291 | virtual void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path, |
| 292 | const SkMatrix* matrix, const SkPaint&) SK_OVERRIDE; |
fmalita | b742517 | 2014-08-26 07:56:44 -0700 | [diff] [blame] | 293 | virtual void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, |
| 294 | const SkPaint& paint) SK_OVERRIDE; |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 295 | virtual void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4], |
| 296 | const SkPoint texCoords[4], SkXfermode* xmode, |
| 297 | const SkPaint& paint) SK_OVERRIDE; |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 298 | virtual void onClipRect(const SkRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE; |
| 299 | virtual void onClipRRect(const SkRRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE; |
| 300 | virtual void onClipPath(const SkPath&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE; |
| 301 | virtual void onClipRegion(const SkRegion&, SkRegion::Op) SK_OVERRIDE; |
| 302 | |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 303 | virtual void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) SK_OVERRIDE; |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 304 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 305 | private: |
commit-bot@chromium.org | d9ea09e | 2014-03-25 17:32:26 +0000 | [diff] [blame] | 306 | void recordTranslate(const SkMatrix&); |
| 307 | void recordScale(const SkMatrix&); |
| 308 | void recordConcat(const SkMatrix&); |
| 309 | |
junov@chromium.org | fbe9c8f | 2012-07-18 20:22:52 +0000 | [diff] [blame] | 310 | enum { |
| 311 | kNoSaveLayer = -1, |
| 312 | }; |
scroggo@google.com | 0c3e5fe | 2012-08-01 19:34:20 +0000 | [diff] [blame] | 313 | SkNamedFactorySet* fFactorySet; |
| 314 | int fFirstSaveLayerStackLevel; |
scroggo@google.com | d9d2967 | 2012-08-14 17:21:34 +0000 | [diff] [blame] | 315 | SkBitmapHeap* fBitmapHeap; |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 316 | SkGPipeController* fController; |
scroggo@google.com | 0c3e5fe | 2012-08-01 19:34:20 +0000 | [diff] [blame] | 317 | SkWriter32& fWriter; |
| 318 | size_t fBlockSize; // amount allocated for writer |
| 319 | size_t fBytesNotified; |
| 320 | bool fDone; |
scroggo@google.com | 3e26bd0 | 2012-08-14 15:20:01 +0000 | [diff] [blame] | 321 | const uint32_t fFlags; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 322 | |
scroggo@google.com | 0c3e5fe | 2012-08-01 19:34:20 +0000 | [diff] [blame] | 323 | SkRefCntSet fTypefaceSet; |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 324 | |
| 325 | uint32_t getTypefaceID(SkTypeface*); |
| 326 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 327 | inline void writeOp(DrawOps op, unsigned flags, unsigned data) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 328 | fWriter.write32(DrawOp_packOpFlagData(op, flags, data)); |
| 329 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 330 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 331 | inline void writeOp(DrawOps op) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 332 | fWriter.write32(DrawOp_packOpFlagData(op, 0, 0)); |
| 333 | } |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 334 | |
| 335 | bool needOpBytes(size_t size = 0); |
| 336 | |
| 337 | inline void doNotify() { |
| 338 | if (!fDone) { |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 339 | size_t bytes = fWriter.bytesWritten() - fBytesNotified; |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 340 | if (bytes > 0) { |
| 341 | fController->notifyWritten(bytes); |
| 342 | fBytesNotified += bytes; |
| 343 | } |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 344 | } |
| 345 | } |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 346 | |
fmalita | acb882c | 2014-09-16 17:58:34 -0700 | [diff] [blame] | 347 | typedef SkAutoSTMalloc<128, uint8_t> TypefaceBuffer; |
| 348 | size_t getInProcessTypefaces(const SkRefCntSet& typefaceSet, TypefaceBuffer*); |
| 349 | size_t getCrossProcessTypefaces(const SkRefCntSet& typefaceSet, TypefaceBuffer*); |
| 350 | |
scroggo@google.com | 0c3e5fe | 2012-08-01 19:34:20 +0000 | [diff] [blame] | 351 | // Should be called after any calls to an SkFlatDictionary::findAndReplace |
| 352 | // if a new SkFlatData was added when in cross process mode |
| 353 | void flattenFactoryNames(); |
| 354 | |
scroggo@google.com | 59c3ab6 | 2013-11-12 14:32:38 +0000 | [diff] [blame] | 355 | FlattenableHeap fFlattenableHeap; |
| 356 | FlatDictionary fFlatDictionary; |
| 357 | SkAutoTUnref<BitmapShuttle> fBitmapShuttle; |
| 358 | int fCurrFlatIndex[kCount_PaintFlats]; |
| 359 | |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 360 | int flattenToIndex(SkFlattenable* obj, PaintFlats); |
| 361 | |
scroggo@google.com | 3e26bd0 | 2012-08-14 15:20:01 +0000 | [diff] [blame] | 362 | // Common code used by drawBitmap*. Behaves differently depending on the |
| 363 | // type of SkBitmapHeap being used, which is determined by the flags used. |
| 364 | bool commonDrawBitmap(const SkBitmap& bm, DrawOps op, unsigned flags, |
| 365 | size_t opBytesNeeded, const SkPaint* paint); |
scroggo@google.com | 58be682 | 2012-07-30 14:40:01 +0000 | [diff] [blame] | 366 | |
piotaixr | 9bb7539 | 2014-10-22 11:02:32 -0700 | [diff] [blame] | 367 | bool commonDrawImage(const SkImage* image, DrawOps op, unsigned flags, |
| 368 | size_t opBytesNeeded, const SkPaint* paint); |
| 369 | |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 370 | SkPaint fPaint; |
| 371 | void writePaint(const SkPaint&); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 372 | |
commit-bot@chromium.org | f700fb2 | 2014-05-14 14:50:57 +0000 | [diff] [blame] | 373 | class AutoPipeNotify { |
| 374 | public: |
| 375 | AutoPipeNotify(SkGPipeCanvas* canvas) : fCanvas(canvas) {} |
| 376 | ~AutoPipeNotify() { fCanvas->doNotify(); } |
| 377 | private: |
| 378 | SkGPipeCanvas* fCanvas; |
| 379 | }; |
| 380 | friend class AutoPipeNotify; |
| 381 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 382 | typedef SkCanvas INHERITED; |
| 383 | }; |
| 384 | |
scroggo@google.com | 0c3e5fe | 2012-08-01 19:34:20 +0000 | [diff] [blame] | 385 | void SkGPipeCanvas::flattenFactoryNames() { |
| 386 | const char* name; |
| 387 | while ((name = fFactorySet->getNextAddedFactoryName()) != NULL) { |
| 388 | size_t len = strlen(name); |
commit-bot@chromium.org | d049017 | 2014-05-14 15:12:08 +0000 | [diff] [blame] | 389 | if (this->needOpBytes(SkWriter32::WriteStringSize(name, len))) { |
scroggo@google.com | 0c3e5fe | 2012-08-01 19:34:20 +0000 | [diff] [blame] | 390 | this->writeOp(kDef_Factory_DrawOp); |
| 391 | fWriter.writeString(name, len); |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | |
scroggo@google.com | 3e26bd0 | 2012-08-14 15:20:01 +0000 | [diff] [blame] | 396 | bool SkGPipeCanvas::shuttleBitmap(const SkBitmap& bm, int32_t slot) { |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 397 | SkASSERT(shouldFlattenBitmaps(fFlags)); |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 398 | SkWriteBuffer buffer; |
scroggo@google.com | 3e26bd0 | 2012-08-14 15:20:01 +0000 | [diff] [blame] | 399 | buffer.setNamedFactoryRecorder(fFactorySet); |
scroggo@google.com | 74b7ffd | 2013-04-30 02:32:41 +0000 | [diff] [blame] | 400 | buffer.writeBitmap(bm); |
scroggo@google.com | 3e26bd0 | 2012-08-14 15:20:01 +0000 | [diff] [blame] | 401 | this->flattenFactoryNames(); |
commit-bot@chromium.org | b45bd1f | 2014-04-24 18:17:30 +0000 | [diff] [blame] | 402 | size_t size = buffer.bytesWritten(); |
scroggo@google.com | 3e26bd0 | 2012-08-14 15:20:01 +0000 | [diff] [blame] | 403 | if (this->needOpBytes(size)) { |
| 404 | this->writeOp(kDef_Bitmap_DrawOp, 0, slot); |
| 405 | void* dst = static_cast<void*>(fWriter.reserve(size)); |
| 406 | buffer.writeToMemory(dst); |
| 407 | return true; |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 408 | } |
scroggo@google.com | 3e26bd0 | 2012-08-14 15:20:01 +0000 | [diff] [blame] | 409 | return false; |
scroggo@google.com | d3ba5cc | 2012-07-09 16:05:53 +0000 | [diff] [blame] | 410 | } |
| 411 | |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 412 | // return 0 for NULL (or unflattenable obj), or index-base-1 |
scroggo@google.com | d3ba5cc | 2012-07-09 16:05:53 +0000 | [diff] [blame] | 413 | // return ~(index-base-1) if an old flattenable was replaced |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 414 | int SkGPipeCanvas::flattenToIndex(SkFlattenable* obj, PaintFlats paintflat) { |
scroggo@google.com | d5d158b | 2012-08-14 20:38:28 +0000 | [diff] [blame] | 415 | SkASSERT(!fDone && fBitmapHeap != NULL); |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 416 | if (NULL == obj) { |
| 417 | return 0; |
| 418 | } |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 419 | |
scroggo@google.com | d9d2967 | 2012-08-14 17:21:34 +0000 | [diff] [blame] | 420 | fBitmapHeap->deferAddingOwners(); |
scroggo@google.com | 4dffc59 | 2012-07-17 16:49:40 +0000 | [diff] [blame] | 421 | bool added, replaced; |
scroggo@google.com | 664fab1 | 2012-08-14 19:22:05 +0000 | [diff] [blame] | 422 | const SkFlatData* flat = fFlatDictionary.findAndReplace(*obj, fFlattenableHeap.flatToReplace(), |
| 423 | &added, &replaced); |
scroggo@google.com | d9d2967 | 2012-08-14 17:21:34 +0000 | [diff] [blame] | 424 | fBitmapHeap->endAddingOwnersDeferral(added); |
scroggo@google.com | 4dffc59 | 2012-07-17 16:49:40 +0000 | [diff] [blame] | 425 | int index = flat->index(); |
scroggo@google.com | 0c3e5fe | 2012-08-01 19:34:20 +0000 | [diff] [blame] | 426 | if (added) { |
fmalita | acb882c | 2014-09-16 17:58:34 -0700 | [diff] [blame] | 427 | if (is_cross_process(fFlags)) { |
scroggo@google.com | 0c3e5fe | 2012-08-01 19:34:20 +0000 | [diff] [blame] | 428 | this->flattenFactoryNames(); |
| 429 | } |
| 430 | size_t flatSize = flat->flatSize(); |
| 431 | if (this->needOpBytes(flatSize)) { |
| 432 | this->writeOp(kDef_Flattenable_DrawOp, paintflat, index); |
| 433 | fWriter.write(flat->data(), flatSize); |
| 434 | } |
scroggo@google.com | 4dffc59 | 2012-07-17 16:49:40 +0000 | [diff] [blame] | 435 | } |
| 436 | if (replaced) { |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 437 | index = ~index; |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 438 | } |
scroggo@google.com | 4dffc59 | 2012-07-17 16:49:40 +0000 | [diff] [blame] | 439 | return index; |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 440 | } |
| 441 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 442 | /////////////////////////////////////////////////////////////////////////////// |
| 443 | |
commit-bot@chromium.org | f700fb2 | 2014-05-14 14:50:57 +0000 | [diff] [blame] | 444 | #define MIN_BLOCK_SIZE (16 * 1024) |
scroggo@google.com | 4dffc59 | 2012-07-17 16:49:40 +0000 | [diff] [blame] | 445 | #define BITMAPS_TO_KEEP 5 |
| 446 | #define FLATTENABLES_TO_KEEP 10 |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 447 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 448 | SkGPipeCanvas::SkGPipeCanvas(SkGPipeController* controller, |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 449 | SkWriter32* writer, uint32_t flags, |
junov@chromium.org | a8db8fe | 2012-08-15 19:49:22 +0000 | [diff] [blame] | 450 | uint32_t width, uint32_t height) |
commit-bot@chromium.org | 403f8d7 | 2014-02-17 15:24:26 +0000 | [diff] [blame] | 451 | : SkCanvas(width, height) |
fmalita | acb882c | 2014-09-16 17:58:34 -0700 | [diff] [blame] | 452 | , fFactorySet(is_cross_process(flags) ? SkNEW(SkNamedFactorySet) : NULL) |
commit-bot@chromium.org | 403f8d7 | 2014-02-17 15:24:26 +0000 | [diff] [blame] | 453 | , fWriter(*writer) |
| 454 | , fFlags(flags) |
fmalita | acb882c | 2014-09-16 17:58:34 -0700 | [diff] [blame] | 455 | , fFlattenableHeap(FLATTENABLES_TO_KEEP, fFactorySet, is_cross_process(flags)) |
commit-bot@chromium.org | 403f8d7 | 2014-02-17 15:24:26 +0000 | [diff] [blame] | 456 | , fFlatDictionary(&fFlattenableHeap) |
| 457 | { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 458 | fController = controller; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 459 | fDone = false; |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 460 | fBlockSize = 0; // need first block from controller |
scroggo@google.com | 5a2e879 | 2012-04-20 17:39:51 +0000 | [diff] [blame] | 461 | fBytesNotified = 0; |
junov@chromium.org | fbe9c8f | 2012-07-18 20:22:52 +0000 | [diff] [blame] | 462 | fFirstSaveLayerStackLevel = kNoSaveLayer; |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 463 | sk_bzero(fCurrFlatIndex, sizeof(fCurrFlatIndex)); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 464 | |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 465 | // Tell the reader the appropriate flags to use. |
| 466 | if (this->needOpBytes()) { |
| 467 | this->writeOp(kReportFlags_DrawOp, fFlags, 0); |
| 468 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 469 | |
scroggo@google.com | 10dccde | 2012-08-08 20:43:22 +0000 | [diff] [blame] | 470 | if (shouldFlattenBitmaps(flags)) { |
scroggo@google.com | 59c3ab6 | 2013-11-12 14:32:38 +0000 | [diff] [blame] | 471 | fBitmapShuttle.reset(SkNEW_ARGS(BitmapShuttle, (this))); |
| 472 | fBitmapHeap = SkNEW_ARGS(SkBitmapHeap, (fBitmapShuttle.get(), BITMAPS_TO_KEEP)); |
scroggo@google.com | 10dccde | 2012-08-08 20:43:22 +0000 | [diff] [blame] | 473 | } else { |
scroggo@google.com | d9d2967 | 2012-08-14 17:21:34 +0000 | [diff] [blame] | 474 | fBitmapHeap = SkNEW_ARGS(SkBitmapHeap, |
scroggo@google.com | 3e26bd0 | 2012-08-14 15:20:01 +0000 | [diff] [blame] | 475 | (BITMAPS_TO_KEEP, controller->numberOfReaders())); |
scroggo@google.com | 10dccde | 2012-08-08 20:43:22 +0000 | [diff] [blame] | 476 | if (this->needOpBytes(sizeof(void*))) { |
scroggo@google.com | d9d2967 | 2012-08-14 17:21:34 +0000 | [diff] [blame] | 477 | this->writeOp(kShareBitmapHeap_DrawOp); |
| 478 | fWriter.writePtr(static_cast<void*>(fBitmapHeap)); |
scroggo@google.com | 10dccde | 2012-08-08 20:43:22 +0000 | [diff] [blame] | 479 | } |
| 480 | } |
scroggo@google.com | d9d2967 | 2012-08-14 17:21:34 +0000 | [diff] [blame] | 481 | fFlattenableHeap.setBitmapStorage(fBitmapHeap); |
piotaixr | 9bb7539 | 2014-10-22 11:02:32 -0700 | [diff] [blame] | 482 | |
commit-bot@chromium.org | f700fb2 | 2014-05-14 14:50:57 +0000 | [diff] [blame] | 483 | this->doNotify(); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | SkGPipeCanvas::~SkGPipeCanvas() { |
scroggo@google.com | 59c3ab6 | 2013-11-12 14:32:38 +0000 | [diff] [blame] | 487 | this->finish(true); |
scroggo@google.com | 0c3e5fe | 2012-08-01 19:34:20 +0000 | [diff] [blame] | 488 | SkSafeUnref(fFactorySet); |
scroggo@google.com | d9d2967 | 2012-08-14 17:21:34 +0000 | [diff] [blame] | 489 | SkSafeUnref(fBitmapHeap); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 490 | } |
| 491 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 492 | bool SkGPipeCanvas::needOpBytes(size_t needed) { |
| 493 | if (fDone) { |
| 494 | return false; |
| 495 | } |
| 496 | |
| 497 | needed += 4; // size of DrawOp atom |
commit-bot@chromium.org | d049017 | 2014-05-14 15:12:08 +0000 | [diff] [blame] | 498 | needed = SkAlign4(needed); |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 499 | if (fWriter.bytesWritten() + needed > fBlockSize) { |
mtklein | 6d88e6c | 2014-07-30 09:17:54 -0700 | [diff] [blame] | 500 | // Before we wipe out any data that has already been written, read it out. |
scroggo@google.com | 5a2e879 | 2012-04-20 17:39:51 +0000 | [diff] [blame] | 501 | this->doNotify(); |
mtklein | 6d88e6c | 2014-07-30 09:17:54 -0700 | [diff] [blame] | 502 | |
| 503 | // If we're going to allocate a new block, allocate enough to make it worthwhile. |
| 504 | needed = SkTMax<size_t>(MIN_BLOCK_SIZE, needed); |
| 505 | |
commit-bot@chromium.org | d049017 | 2014-05-14 15:12:08 +0000 | [diff] [blame] | 506 | void* block = fController->requestBlock(needed, &fBlockSize); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 507 | if (NULL == block) { |
scroggo@google.com | 59c3ab6 | 2013-11-12 14:32:38 +0000 | [diff] [blame] | 508 | // Do not notify the readers, which would call this function again. |
| 509 | this->finish(false); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 510 | return false; |
| 511 | } |
scroggo@google.com | 460a23e | 2012-08-16 17:56:49 +0000 | [diff] [blame] | 512 | SkASSERT(SkIsAlign4(fBlockSize)); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 513 | fWriter.reset(block, fBlockSize); |
| 514 | fBytesNotified = 0; |
| 515 | } |
| 516 | return true; |
| 517 | } |
| 518 | |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 519 | uint32_t SkGPipeCanvas::getTypefaceID(SkTypeface* face) { |
| 520 | uint32_t id = 0; // 0 means default/null typeface |
| 521 | if (face) { |
| 522 | id = fTypefaceSet.find(face); |
| 523 | if (0 == id) { |
| 524 | id = fTypefaceSet.add(face); |
| 525 | size_t size = writeTypeface(NULL, face); |
| 526 | if (this->needOpBytes(size)) { |
reed@google.com | bb6793b | 2011-05-05 15:18:15 +0000 | [diff] [blame] | 527 | this->writeOp(kDef_Typeface_DrawOp); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 528 | writeTypeface(&fWriter, face); |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | return id; |
| 533 | } |
| 534 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 535 | /////////////////////////////////////////////////////////////////////////////// |
| 536 | |
commit-bot@chromium.org | f700fb2 | 2014-05-14 14:50:57 +0000 | [diff] [blame] | 537 | #define NOTIFY_SETUP(canvas) \ |
| 538 | AutoPipeNotify apn(canvas) |
| 539 | |
Florin Malita | 5f6102d | 2014-06-30 10:13:28 -0400 | [diff] [blame] | 540 | void SkGPipeCanvas::willSave() { |
commit-bot@chromium.org | f700fb2 | 2014-05-14 14:50:57 +0000 | [diff] [blame] | 541 | NOTIFY_SETUP(this); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 542 | if (this->needOpBytes()) { |
Florin Malita | 5f6102d | 2014-06-30 10:13:28 -0400 | [diff] [blame] | 543 | this->writeOp(kSave_DrawOp); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 544 | } |
commit-bot@chromium.org | e54a23f | 2014-03-12 20:21:48 +0000 | [diff] [blame] | 545 | |
Florin Malita | 5f6102d | 2014-06-30 10:13:28 -0400 | [diff] [blame] | 546 | this->INHERITED::willSave(); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 547 | } |
| 548 | |
commit-bot@chromium.org | e54a23f | 2014-03-12 20:21:48 +0000 | [diff] [blame] | 549 | SkCanvas::SaveLayerStrategy SkGPipeCanvas::willSaveLayer(const SkRect* bounds, const SkPaint* paint, |
| 550 | SaveFlags saveFlags) { |
commit-bot@chromium.org | f700fb2 | 2014-05-14 14:50:57 +0000 | [diff] [blame] | 551 | NOTIFY_SETUP(this); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 552 | size_t size = 0; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 553 | unsigned opFlags = 0; |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 554 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 555 | if (bounds) { |
| 556 | opFlags |= kSaveLayer_HasBounds_DrawOpFlag; |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 557 | size += sizeof(SkRect); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 558 | } |
| 559 | if (paint) { |
| 560 | opFlags |= kSaveLayer_HasPaint_DrawOpFlag; |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 561 | this->writePaint(*paint); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 562 | } |
| 563 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 564 | if (this->needOpBytes(size)) { |
| 565 | this->writeOp(kSaveLayer_DrawOp, opFlags, saveFlags); |
| 566 | if (bounds) { |
| 567 | fWriter.writeRect(*bounds); |
| 568 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 569 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 570 | |
junov@chromium.org | fbe9c8f | 2012-07-18 20:22:52 +0000 | [diff] [blame] | 571 | if (kNoSaveLayer == fFirstSaveLayerStackLevel){ |
| 572 | fFirstSaveLayerStackLevel = this->getSaveCount(); |
| 573 | } |
commit-bot@chromium.org | e54a23f | 2014-03-12 20:21:48 +0000 | [diff] [blame] | 574 | |
| 575 | this->INHERITED::willSaveLayer(bounds, paint, saveFlags); |
| 576 | // we don't create a layer |
| 577 | return kNoLayer_SaveLayerStrategy; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 578 | } |
| 579 | |
commit-bot@chromium.org | e54a23f | 2014-03-12 20:21:48 +0000 | [diff] [blame] | 580 | void SkGPipeCanvas::willRestore() { |
commit-bot@chromium.org | f700fb2 | 2014-05-14 14:50:57 +0000 | [diff] [blame] | 581 | NOTIFY_SETUP(this); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 582 | if (this->needOpBytes()) { |
| 583 | this->writeOp(kRestore_DrawOp); |
| 584 | } |
junov@chromium.org | fbe9c8f | 2012-07-18 20:22:52 +0000 | [diff] [blame] | 585 | |
commit-bot@chromium.org | e54a23f | 2014-03-12 20:21:48 +0000 | [diff] [blame] | 586 | if (this->getSaveCount() - 1 == fFirstSaveLayerStackLevel){ |
junov@chromium.org | fbe9c8f | 2012-07-18 20:22:52 +0000 | [diff] [blame] | 587 | fFirstSaveLayerStackLevel = kNoSaveLayer; |
| 588 | } |
commit-bot@chromium.org | e54a23f | 2014-03-12 20:21:48 +0000 | [diff] [blame] | 589 | |
| 590 | this->INHERITED::willRestore(); |
junov@chromium.org | fbe9c8f | 2012-07-18 20:22:52 +0000 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | bool SkGPipeCanvas::isDrawingToLayer() const { |
| 594 | return kNoSaveLayer != fFirstSaveLayerStackLevel; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 595 | } |
| 596 | |
commit-bot@chromium.org | d9ea09e | 2014-03-25 17:32:26 +0000 | [diff] [blame] | 597 | void SkGPipeCanvas::recordTranslate(const SkMatrix& m) { |
| 598 | if (this->needOpBytes(2 * sizeof(SkScalar))) { |
| 599 | this->writeOp(kTranslate_DrawOp); |
| 600 | fWriter.writeScalar(m.getTranslateX()); |
| 601 | fWriter.writeScalar(m.getTranslateY()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 602 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 603 | } |
| 604 | |
commit-bot@chromium.org | d9ea09e | 2014-03-25 17:32:26 +0000 | [diff] [blame] | 605 | void SkGPipeCanvas::recordScale(const SkMatrix& m) { |
| 606 | if (this->needOpBytes(2 * sizeof(SkScalar))) { |
| 607 | this->writeOp(kScale_DrawOp); |
| 608 | fWriter.writeScalar(m.getScaleX()); |
| 609 | fWriter.writeScalar(m.getScaleY()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 610 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 611 | } |
| 612 | |
commit-bot@chromium.org | d9ea09e | 2014-03-25 17:32:26 +0000 | [diff] [blame] | 613 | void SkGPipeCanvas::recordConcat(const SkMatrix& m) { |
| 614 | if (this->needOpBytes(m.writeToMemory(NULL))) { |
| 615 | this->writeOp(kConcat_DrawOp); |
| 616 | fWriter.writeMatrix(m); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 617 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 618 | } |
| 619 | |
commit-bot@chromium.org | 44c48d0 | 2014-03-13 20:03:58 +0000 | [diff] [blame] | 620 | void SkGPipeCanvas::didConcat(const SkMatrix& matrix) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 621 | if (!matrix.isIdentity()) { |
commit-bot@chromium.org | f700fb2 | 2014-05-14 14:50:57 +0000 | [diff] [blame] | 622 | NOTIFY_SETUP(this); |
commit-bot@chromium.org | d9ea09e | 2014-03-25 17:32:26 +0000 | [diff] [blame] | 623 | switch (matrix.getType()) { |
| 624 | case SkMatrix::kTranslate_Mask: |
| 625 | this->recordTranslate(matrix); |
| 626 | break; |
| 627 | case SkMatrix::kScale_Mask: |
| 628 | this->recordScale(matrix); |
| 629 | break; |
| 630 | default: |
| 631 | this->recordConcat(matrix); |
| 632 | break; |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 633 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 634 | } |
commit-bot@chromium.org | d9ea09e | 2014-03-25 17:32:26 +0000 | [diff] [blame] | 635 | |
commit-bot@chromium.org | 44c48d0 | 2014-03-13 20:03:58 +0000 | [diff] [blame] | 636 | this->INHERITED::didConcat(matrix); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 637 | } |
| 638 | |
commit-bot@chromium.org | 44c48d0 | 2014-03-13 20:03:58 +0000 | [diff] [blame] | 639 | void SkGPipeCanvas::didSetMatrix(const SkMatrix& matrix) { |
commit-bot@chromium.org | f700fb2 | 2014-05-14 14:50:57 +0000 | [diff] [blame] | 640 | NOTIFY_SETUP(this); |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 641 | if (this->needOpBytes(matrix.writeToMemory(NULL))) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 642 | this->writeOp(kSetMatrix_DrawOp); |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 643 | fWriter.writeMatrix(matrix); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 644 | } |
commit-bot@chromium.org | 44c48d0 | 2014-03-13 20:03:58 +0000 | [diff] [blame] | 645 | this->INHERITED::didSetMatrix(matrix); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 646 | } |
| 647 | |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 648 | void SkGPipeCanvas::onClipRect(const SkRect& rect, SkRegion::Op rgnOp, |
| 649 | ClipEdgeStyle edgeStyle) { |
commit-bot@chromium.org | f700fb2 | 2014-05-14 14:50:57 +0000 | [diff] [blame] | 650 | NOTIFY_SETUP(this); |
scroggo@google.com | 460a23e | 2012-08-16 17:56:49 +0000 | [diff] [blame] | 651 | if (this->needOpBytes(sizeof(SkRect))) { |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 652 | unsigned flags = 0; |
| 653 | if (kSoft_ClipEdgeStyle == edgeStyle) { |
| 654 | flags = kClip_HasAntiAlias_DrawOpFlag; |
| 655 | } |
scroggo@google.com | 460a23e | 2012-08-16 17:56:49 +0000 | [diff] [blame] | 656 | this->writeOp(kClipRect_DrawOp, flags, rgnOp); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 657 | fWriter.writeRect(rect); |
| 658 | } |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 659 | this->INHERITED::onClipRect(rect, rgnOp, edgeStyle); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 660 | } |
| 661 | |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 662 | void SkGPipeCanvas::onClipRRect(const SkRRect& rrect, SkRegion::Op rgnOp, |
| 663 | ClipEdgeStyle edgeStyle) { |
commit-bot@chromium.org | f700fb2 | 2014-05-14 14:50:57 +0000 | [diff] [blame] | 664 | NOTIFY_SETUP(this); |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 665 | if (this->needOpBytes(kSizeOfFlatRRect)) { |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 666 | unsigned flags = 0; |
| 667 | if (kSoft_ClipEdgeStyle == edgeStyle) { |
| 668 | flags = kClip_HasAntiAlias_DrawOpFlag; |
| 669 | } |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 670 | this->writeOp(kClipRRect_DrawOp, flags, rgnOp); |
| 671 | fWriter.writeRRect(rrect); |
| 672 | } |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 673 | this->INHERITED::onClipRRect(rrect, rgnOp, edgeStyle); |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 674 | } |
| 675 | |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 676 | void SkGPipeCanvas::onClipPath(const SkPath& path, SkRegion::Op rgnOp, |
| 677 | ClipEdgeStyle edgeStyle) { |
commit-bot@chromium.org | f700fb2 | 2014-05-14 14:50:57 +0000 | [diff] [blame] | 678 | NOTIFY_SETUP(this); |
scroggo@google.com | 460a23e | 2012-08-16 17:56:49 +0000 | [diff] [blame] | 679 | if (this->needOpBytes(path.writeToMemory(NULL))) { |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 680 | unsigned flags = 0; |
| 681 | if (kSoft_ClipEdgeStyle == edgeStyle) { |
| 682 | flags = kClip_HasAntiAlias_DrawOpFlag; |
| 683 | } |
scroggo@google.com | 460a23e | 2012-08-16 17:56:49 +0000 | [diff] [blame] | 684 | this->writeOp(kClipPath_DrawOp, flags, rgnOp); |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 685 | fWriter.writePath(path); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 686 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 687 | // we just pass on the bounds of the path |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 688 | this->INHERITED::onClipRect(path.getBounds(), rgnOp, edgeStyle); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 689 | } |
| 690 | |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 691 | void SkGPipeCanvas::onClipRegion(const SkRegion& region, SkRegion::Op rgnOp) { |
commit-bot@chromium.org | f700fb2 | 2014-05-14 14:50:57 +0000 | [diff] [blame] | 692 | NOTIFY_SETUP(this); |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 693 | if (this->needOpBytes(region.writeToMemory(NULL))) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 694 | this->writeOp(kClipRegion_DrawOp, 0, rgnOp); |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 695 | fWriter.writeRegion(region); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 696 | } |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 697 | this->INHERITED::onClipRegion(region, rgnOp); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | /////////////////////////////////////////////////////////////////////////////// |
| 701 | |
| 702 | void SkGPipeCanvas::clear(SkColor color) { |
commit-bot@chromium.org | f700fb2 | 2014-05-14 14:50:57 +0000 | [diff] [blame] | 703 | NOTIFY_SETUP(this); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 704 | unsigned flags = 0; |
| 705 | if (color) { |
| 706 | flags |= kClear_HasColor_DrawOpFlag; |
| 707 | } |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 708 | if (this->needOpBytes(sizeof(SkColor))) { |
| 709 | this->writeOp(kDrawClear_DrawOp, flags, 0); |
| 710 | if (color) { |
| 711 | fWriter.write32(color); |
| 712 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 713 | } |
| 714 | } |
| 715 | |
| 716 | void SkGPipeCanvas::drawPaint(const SkPaint& paint) { |
commit-bot@chromium.org | f700fb2 | 2014-05-14 14:50:57 +0000 | [diff] [blame] | 717 | NOTIFY_SETUP(this); |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 718 | this->writePaint(paint); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 719 | if (this->needOpBytes()) { |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 720 | this->writeOp(kDrawPaint_DrawOp); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 721 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | void SkGPipeCanvas::drawPoints(PointMode mode, size_t count, |
robertphillips@google.com | 8b16931 | 2013-10-15 17:47:36 +0000 | [diff] [blame] | 725 | const SkPoint pts[], const SkPaint& paint) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 726 | if (count) { |
commit-bot@chromium.org | f700fb2 | 2014-05-14 14:50:57 +0000 | [diff] [blame] | 727 | NOTIFY_SETUP(this); |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 728 | this->writePaint(paint); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 729 | if (this->needOpBytes(4 + count * sizeof(SkPoint))) { |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 730 | this->writeOp(kDrawPoints_DrawOp, mode, 0); |
commit-bot@chromium.org | b45bd1f | 2014-04-24 18:17:30 +0000 | [diff] [blame] | 731 | fWriter.write32(SkToU32(count)); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 732 | fWriter.write(pts, count * sizeof(SkPoint)); |
| 733 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 734 | } |
| 735 | } |
| 736 | |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 737 | void SkGPipeCanvas::drawOval(const SkRect& rect, const SkPaint& paint) { |
commit-bot@chromium.org | f700fb2 | 2014-05-14 14:50:57 +0000 | [diff] [blame] | 738 | NOTIFY_SETUP(this); |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 739 | this->writePaint(paint); |
| 740 | if (this->needOpBytes(sizeof(SkRect))) { |
| 741 | this->writeOp(kDrawOval_DrawOp); |
| 742 | fWriter.writeRect(rect); |
| 743 | } |
| 744 | } |
| 745 | |
bsalomon@google.com | 7ce564c | 2013-10-22 16:54:15 +0000 | [diff] [blame] | 746 | void SkGPipeCanvas::drawRect(const SkRect& rect, const SkPaint& paint) { |
commit-bot@chromium.org | f700fb2 | 2014-05-14 14:50:57 +0000 | [diff] [blame] | 747 | NOTIFY_SETUP(this); |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 748 | this->writePaint(paint); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 749 | if (this->needOpBytes(sizeof(SkRect))) { |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 750 | this->writeOp(kDrawRect_DrawOp); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 751 | fWriter.writeRect(rect); |
| 752 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 753 | } |
| 754 | |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 755 | void SkGPipeCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) { |
commit-bot@chromium.org | f700fb2 | 2014-05-14 14:50:57 +0000 | [diff] [blame] | 756 | NOTIFY_SETUP(this); |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 757 | this->writePaint(paint); |
| 758 | if (this->needOpBytes(kSizeOfFlatRRect)) { |
| 759 | this->writeOp(kDrawRRect_DrawOp); |
| 760 | fWriter.writeRRect(rrect); |
| 761 | } |
| 762 | } |
| 763 | |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 764 | void SkGPipeCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, |
| 765 | const SkPaint& paint) { |
commit-bot@chromium.org | f700fb2 | 2014-05-14 14:50:57 +0000 | [diff] [blame] | 766 | NOTIFY_SETUP(this); |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 767 | this->writePaint(paint); |
| 768 | if (this->needOpBytes(kSizeOfFlatRRect * 2)) { |
| 769 | this->writeOp(kDrawDRRect_DrawOp); |
| 770 | fWriter.writeRRect(outer); |
| 771 | fWriter.writeRRect(inner); |
| 772 | } |
| 773 | } |
| 774 | |
bsalomon@google.com | 7ce564c | 2013-10-22 16:54:15 +0000 | [diff] [blame] | 775 | void SkGPipeCanvas::drawPath(const SkPath& path, const SkPaint& paint) { |
commit-bot@chromium.org | f700fb2 | 2014-05-14 14:50:57 +0000 | [diff] [blame] | 776 | NOTIFY_SETUP(this); |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 777 | this->writePaint(paint); |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 778 | if (this->needOpBytes(path.writeToMemory(NULL))) { |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 779 | this->writeOp(kDrawPath_DrawOp); |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 780 | fWriter.writePath(path); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 781 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 782 | } |
| 783 | |
scroggo@google.com | 3e26bd0 | 2012-08-14 15:20:01 +0000 | [diff] [blame] | 784 | bool SkGPipeCanvas::commonDrawBitmap(const SkBitmap& bm, DrawOps op, |
| 785 | unsigned flags, |
| 786 | size_t opBytesNeeded, |
| 787 | const SkPaint* paint) { |
commit-bot@chromium.org | 44803fb | 2014-05-14 15:38:25 +0000 | [diff] [blame] | 788 | if (fDone) { |
| 789 | return false; |
| 790 | } |
| 791 | |
scroggo@google.com | 58be682 | 2012-07-30 14:40:01 +0000 | [diff] [blame] | 792 | if (paint != NULL) { |
scroggo@google.com | 460a23e | 2012-08-16 17:56:49 +0000 | [diff] [blame] | 793 | flags |= kDrawBitmap_HasPaint_DrawOpFlag; |
scroggo@google.com | 58be682 | 2012-07-30 14:40:01 +0000 | [diff] [blame] | 794 | this->writePaint(*paint); |
| 795 | } |
commit-bot@chromium.org | 44803fb | 2014-05-14 15:38:25 +0000 | [diff] [blame] | 796 | // This needs to run first so its calls to needOpBytes() and its writes |
| 797 | // don't interlace with the needOpBytes() and write below. |
| 798 | SkASSERT(fBitmapHeap != NULL); |
| 799 | int32_t bitmapIndex = fBitmapHeap->insert(bm); |
| 800 | if (SkBitmapHeap::INVALID_SLOT == bitmapIndex) { |
| 801 | return false; |
| 802 | } |
| 803 | |
scroggo@google.com | 10dccde | 2012-08-08 20:43:22 +0000 | [diff] [blame] | 804 | if (this->needOpBytes(opBytesNeeded)) { |
| 805 | this->writeOp(op, flags, bitmapIndex); |
scroggo@google.com | 58be682 | 2012-07-30 14:40:01 +0000 | [diff] [blame] | 806 | return true; |
| 807 | } |
| 808 | return false; |
| 809 | } |
| 810 | |
piotaixr | 9bb7539 | 2014-10-22 11:02:32 -0700 | [diff] [blame] | 811 | bool SkGPipeCanvas::commonDrawImage(const SkImage* image, DrawOps op, |
| 812 | unsigned flags, |
| 813 | size_t opBytesNeeded, |
| 814 | const SkPaint* paint) { |
| 815 | if (fDone) { |
| 816 | return false; |
| 817 | } |
| 818 | |
| 819 | if (paint != NULL) { |
| 820 | flags |= kDrawBitmap_HasPaint_DrawOpFlag; |
| 821 | this->writePaint(*paint); |
| 822 | } |
| 823 | |
| 824 | opBytesNeeded += sizeof (SkImage*); |
| 825 | |
| 826 | if (this->needOpBytes(opBytesNeeded)) { |
| 827 | this->writeOp(op, flags, 0); |
| 828 | |
| 829 | image->ref(); // The SkGPipeReader will have to call unref() |
| 830 | fWriter.writePtr(static_cast<void*>(const_cast<SkImage*>(image))); |
| 831 | |
| 832 | return true; |
| 833 | } |
| 834 | return false; |
| 835 | } |
| 836 | |
| 837 | void SkGPipeCanvas::drawImage(const SkImage* image, SkScalar left, SkScalar top, |
| 838 | const SkPaint* paint) { |
| 839 | if (is_cross_process(fFlags)){ |
| 840 | // If the SkGPipe is cross-process, we will have to flatten the data in the SkImage, so |
| 841 | // fallback to the default implementation in SkCanvas (which calls SkImage::draw()) |
| 842 | // https://code.google.com//p/skia/issues/detail?id=2985 |
| 843 | this->INHERITED::drawImage(image, left, top, paint); |
| 844 | } else { |
| 845 | NOTIFY_SETUP(this); |
| 846 | size_t opBytesNeeded = sizeof(SkScalar) * 2; |
| 847 | |
| 848 | if (this->commonDrawImage(image, kDrawImage_DrawOp, 0, opBytesNeeded, paint)) { |
| 849 | fWriter.writeScalar(left); |
| 850 | fWriter.writeScalar(top); |
| 851 | } |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | void SkGPipeCanvas::drawImageRect(const SkImage* image, const SkRect* src, |
| 856 | const SkRect& dst, |
| 857 | const SkPaint* paint) { |
| 858 | if (is_cross_process(fFlags)){ |
| 859 | // If the SkGPipe is cross-process, we will have to flatten the data in the SkImage, so |
| 860 | // fallback to the default implementation in SkCanvas (which calls SkImage::drawRect()) |
| 861 | // https://code.google.com//p/skia/issues/detail?id=2985 |
| 862 | this->INHERITED::drawImageRect(image, src, dst, paint); |
| 863 | } else { |
| 864 | NOTIFY_SETUP(this); |
| 865 | size_t opBytesNeeded = sizeof (SkRect); |
| 866 | bool hasSrc = src != NULL; |
| 867 | unsigned flags; |
| 868 | if (hasSrc) { |
| 869 | flags = kDrawImage_HasSrcRect_DrawOpFlag; |
| 870 | opBytesNeeded += sizeof (SkRect); |
| 871 | } else { |
| 872 | flags = 0; |
| 873 | } |
| 874 | |
| 875 | if (this->commonDrawImage(image, kDrawImageRect_DrawOp, flags, opBytesNeeded, paint)) { |
| 876 | if (hasSrc) { |
| 877 | fWriter.writeRect(*src); |
| 878 | } |
| 879 | fWriter.writeRect(dst); |
| 880 | } |
| 881 | } |
| 882 | } |
| 883 | |
scroggo@google.com | 58be682 | 2012-07-30 14:40:01 +0000 | [diff] [blame] | 884 | void SkGPipeCanvas::drawBitmap(const SkBitmap& bm, SkScalar left, SkScalar top, |
| 885 | const SkPaint* paint) { |
commit-bot@chromium.org | f700fb2 | 2014-05-14 14:50:57 +0000 | [diff] [blame] | 886 | NOTIFY_SETUP(this); |
scroggo@google.com | 58be682 | 2012-07-30 14:40:01 +0000 | [diff] [blame] | 887 | size_t opBytesNeeded = sizeof(SkScalar) * 2; |
| 888 | |
scroggo@google.com | 3e26bd0 | 2012-08-14 15:20:01 +0000 | [diff] [blame] | 889 | if (this->commonDrawBitmap(bm, kDrawBitmap_DrawOp, 0, opBytesNeeded, paint)) { |
scroggo@google.com | 5a2e879 | 2012-04-20 17:39:51 +0000 | [diff] [blame] | 890 | fWriter.writeScalar(left); |
| 891 | fWriter.writeScalar(top); |
| 892 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 893 | } |
| 894 | |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 895 | void SkGPipeCanvas::drawBitmapRectToRect(const SkBitmap& bm, const SkRect* src, |
commit-bot@chromium.org | eed779d | 2013-08-16 10:24:37 +0000 | [diff] [blame] | 896 | const SkRect& dst, const SkPaint* paint, |
| 897 | DrawBitmapRectFlags dbmrFlags) { |
commit-bot@chromium.org | f700fb2 | 2014-05-14 14:50:57 +0000 | [diff] [blame] | 898 | NOTIFY_SETUP(this); |
scroggo@google.com | 58be682 | 2012-07-30 14:40:01 +0000 | [diff] [blame] | 899 | size_t opBytesNeeded = sizeof(SkRect); |
| 900 | bool hasSrc = src != NULL; |
| 901 | unsigned flags; |
| 902 | if (hasSrc) { |
scroggo@google.com | 460a23e | 2012-08-16 17:56:49 +0000 | [diff] [blame] | 903 | flags = kDrawBitmap_HasSrcRect_DrawOpFlag; |
scroggo@google.com | 58be682 | 2012-07-30 14:40:01 +0000 | [diff] [blame] | 904 | opBytesNeeded += sizeof(int32_t) * 4; |
| 905 | } else { |
| 906 | flags = 0; |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 907 | } |
commit-bot@chromium.org | eed779d | 2013-08-16 10:24:37 +0000 | [diff] [blame] | 908 | if (dbmrFlags & kBleed_DrawBitmapRectFlag) { |
| 909 | flags |= kDrawBitmap_Bleed_DrawOpFlag; |
| 910 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 911 | |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 912 | if (this->commonDrawBitmap(bm, kDrawBitmapRectToRect_DrawOp, flags, opBytesNeeded, paint)) { |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 913 | if (hasSrc) { |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 914 | fWriter.writeRect(*src); |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 915 | } |
| 916 | fWriter.writeRect(dst); |
| 917 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 918 | } |
| 919 | |
scroggo@google.com | 58be682 | 2012-07-30 14:40:01 +0000 | [diff] [blame] | 920 | void SkGPipeCanvas::drawBitmapMatrix(const SkBitmap& bm, const SkMatrix& matrix, |
| 921 | const SkPaint* paint) { |
commit-bot@chromium.org | f700fb2 | 2014-05-14 14:50:57 +0000 | [diff] [blame] | 922 | NOTIFY_SETUP(this); |
scroggo@google.com | 58be682 | 2012-07-30 14:40:01 +0000 | [diff] [blame] | 923 | size_t opBytesNeeded = matrix.writeToMemory(NULL); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 924 | |
scroggo@google.com | 3e26bd0 | 2012-08-14 15:20:01 +0000 | [diff] [blame] | 925 | if (this->commonDrawBitmap(bm, kDrawBitmapMatrix_DrawOp, 0, opBytesNeeded, paint)) { |
scroggo@google.com | 58be682 | 2012-07-30 14:40:01 +0000 | [diff] [blame] | 926 | fWriter.writeMatrix(matrix); |
| 927 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 928 | } |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 929 | |
| 930 | void SkGPipeCanvas::drawBitmapNine(const SkBitmap& bm, const SkIRect& center, |
scroggo@google.com | 5a2e879 | 2012-04-20 17:39:51 +0000 | [diff] [blame] | 931 | const SkRect& dst, const SkPaint* paint) { |
commit-bot@chromium.org | f700fb2 | 2014-05-14 14:50:57 +0000 | [diff] [blame] | 932 | NOTIFY_SETUP(this); |
scroggo@google.com | 58be682 | 2012-07-30 14:40:01 +0000 | [diff] [blame] | 933 | size_t opBytesNeeded = sizeof(int32_t) * 4 + sizeof(SkRect); |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 934 | |
scroggo@google.com | 3e26bd0 | 2012-08-14 15:20:01 +0000 | [diff] [blame] | 935 | if (this->commonDrawBitmap(bm, kDrawBitmapNine_DrawOp, 0, opBytesNeeded, paint)) { |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 936 | fWriter.write32(center.fLeft); |
| 937 | fWriter.write32(center.fTop); |
| 938 | fWriter.write32(center.fRight); |
| 939 | fWriter.write32(center.fBottom); |
| 940 | fWriter.writeRect(dst); |
| 941 | } |
scroggo@google.com | 5a2e879 | 2012-04-20 17:39:51 +0000 | [diff] [blame] | 942 | } |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 943 | |
| 944 | void SkGPipeCanvas::drawSprite(const SkBitmap& bm, int left, int top, |
| 945 | const SkPaint* paint) { |
commit-bot@chromium.org | f700fb2 | 2014-05-14 14:50:57 +0000 | [diff] [blame] | 946 | NOTIFY_SETUP(this); |
scroggo@google.com | 58be682 | 2012-07-30 14:40:01 +0000 | [diff] [blame] | 947 | size_t opBytesNeeded = sizeof(int32_t) * 2; |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 948 | |
scroggo@google.com | 3e26bd0 | 2012-08-14 15:20:01 +0000 | [diff] [blame] | 949 | if (this->commonDrawBitmap(bm, kDrawSprite_DrawOp, 0, opBytesNeeded, paint)) { |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 950 | fWriter.write32(left); |
| 951 | fWriter.write32(top); |
| 952 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 953 | } |
| 954 | |
reed@google.com | e0d9ce8 | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 955 | void SkGPipeCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y, |
| 956 | const SkPaint& paint) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 957 | if (byteLength) { |
commit-bot@chromium.org | f700fb2 | 2014-05-14 14:50:57 +0000 | [diff] [blame] | 958 | NOTIFY_SETUP(this); |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 959 | this->writePaint(paint); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 960 | if (this->needOpBytes(4 + SkAlign4(byteLength) + 2 * sizeof(SkScalar))) { |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 961 | this->writeOp(kDrawText_DrawOp); |
commit-bot@chromium.org | b45bd1f | 2014-04-24 18:17:30 +0000 | [diff] [blame] | 962 | fWriter.write32(SkToU32(byteLength)); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 963 | fWriter.writePad(text, byteLength); |
| 964 | fWriter.writeScalar(x); |
| 965 | fWriter.writeScalar(y); |
| 966 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 967 | } |
| 968 | } |
| 969 | |
reed@google.com | e0d9ce8 | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 970 | void SkGPipeCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[], |
| 971 | const SkPaint& paint) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 972 | if (byteLength) { |
commit-bot@chromium.org | f700fb2 | 2014-05-14 14:50:57 +0000 | [diff] [blame] | 973 | NOTIFY_SETUP(this); |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 974 | this->writePaint(paint); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 975 | int count = paint.textToGlyphs(text, byteLength, NULL); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 976 | if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkPoint))) { |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 977 | this->writeOp(kDrawPosText_DrawOp); |
commit-bot@chromium.org | b45bd1f | 2014-04-24 18:17:30 +0000 | [diff] [blame] | 978 | fWriter.write32(SkToU32(byteLength)); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 979 | fWriter.writePad(text, byteLength); |
| 980 | fWriter.write32(count); |
| 981 | fWriter.write(pos, count * sizeof(SkPoint)); |
| 982 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 983 | } |
| 984 | } |
| 985 | |
reed@google.com | e0d9ce8 | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 986 | void SkGPipeCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[], |
| 987 | SkScalar constY, const SkPaint& paint) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 988 | if (byteLength) { |
commit-bot@chromium.org | f700fb2 | 2014-05-14 14:50:57 +0000 | [diff] [blame] | 989 | NOTIFY_SETUP(this); |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 990 | this->writePaint(paint); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 991 | int count = paint.textToGlyphs(text, byteLength, NULL); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 992 | if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkScalar) + 4)) { |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 993 | this->writeOp(kDrawPosTextH_DrawOp); |
commit-bot@chromium.org | b45bd1f | 2014-04-24 18:17:30 +0000 | [diff] [blame] | 994 | fWriter.write32(SkToU32(byteLength)); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 995 | fWriter.writePad(text, byteLength); |
| 996 | fWriter.write32(count); |
| 997 | fWriter.write(xpos, count * sizeof(SkScalar)); |
| 998 | fWriter.writeScalar(constY); |
| 999 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1000 | } |
| 1001 | } |
| 1002 | |
reed@google.com | e0d9ce8 | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 1003 | void SkGPipeCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path, |
| 1004 | const SkMatrix* matrix, const SkPaint& paint) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1005 | if (byteLength) { |
commit-bot@chromium.org | f700fb2 | 2014-05-14 14:50:57 +0000 | [diff] [blame] | 1006 | NOTIFY_SETUP(this); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1007 | unsigned flags = 0; |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 1008 | size_t size = 4 + SkAlign4(byteLength) + path.writeToMemory(NULL); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1009 | if (matrix) { |
| 1010 | flags |= kDrawTextOnPath_HasMatrix_DrawOpFlag; |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 1011 | size += matrix->writeToMemory(NULL); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1012 | } |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 1013 | this->writePaint(paint); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1014 | if (this->needOpBytes(size)) { |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 1015 | this->writeOp(kDrawTextOnPath_DrawOp, flags, 0); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1016 | |
commit-bot@chromium.org | b45bd1f | 2014-04-24 18:17:30 +0000 | [diff] [blame] | 1017 | fWriter.write32(SkToU32(byteLength)); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1018 | fWriter.writePad(text, byteLength); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1019 | |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 1020 | fWriter.writePath(path); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1021 | if (matrix) { |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 1022 | fWriter.writeMatrix(*matrix); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1023 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1024 | } |
| 1025 | } |
| 1026 | } |
| 1027 | |
fmalita | acb882c | 2014-09-16 17:58:34 -0700 | [diff] [blame] | 1028 | size_t SkGPipeCanvas::getInProcessTypefaces(const SkRefCntSet& typefaceSet, |
| 1029 | TypefaceBuffer* buffer) { |
| 1030 | // When in-process, we simply write out the typeface pointers. |
| 1031 | size_t size = typefaceSet.count() * sizeof(SkTypeface*); |
| 1032 | buffer->reset(size); |
| 1033 | typefaceSet.copyToArray(reinterpret_cast<SkRefCnt**>(buffer->get())); |
| 1034 | |
| 1035 | return size; |
| 1036 | } |
| 1037 | |
| 1038 | size_t SkGPipeCanvas::getCrossProcessTypefaces(const SkRefCntSet& typefaceSet, |
| 1039 | TypefaceBuffer* buffer) { |
| 1040 | // For cross-process we use typeface IDs. |
| 1041 | size_t size = typefaceSet.count() * sizeof(uint32_t); |
| 1042 | buffer->reset(size); |
| 1043 | |
| 1044 | uint32_t* idBuffer = reinterpret_cast<uint32_t*>(buffer->get()); |
| 1045 | SkRefCntSet::Iter iter(typefaceSet); |
| 1046 | int i = 0; |
| 1047 | |
| 1048 | for (void* setPtr = iter.next(); setPtr; setPtr = iter.next()) { |
| 1049 | idBuffer[i++] = this->getTypefaceID(reinterpret_cast<SkTypeface*>(setPtr)); |
| 1050 | } |
| 1051 | |
| 1052 | SkASSERT(i == typefaceSet.count()); |
| 1053 | |
| 1054 | return size; |
| 1055 | } |
| 1056 | |
fmalita | b742517 | 2014-08-26 07:56:44 -0700 | [diff] [blame] | 1057 | void SkGPipeCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, |
| 1058 | const SkPaint& paint) { |
fmalita | 228a6f2 | 2014-08-28 13:59:42 -0700 | [diff] [blame] | 1059 | NOTIFY_SETUP(this); |
| 1060 | this->writePaint(paint); |
| 1061 | |
| 1062 | // FIXME: this is inefficient but avoids duplicating the blob serialization logic. |
fmalita | acb882c | 2014-09-16 17:58:34 -0700 | [diff] [blame] | 1063 | SkRefCntSet typefaceSet; |
fmalita | 228a6f2 | 2014-08-28 13:59:42 -0700 | [diff] [blame] | 1064 | SkWriteBuffer blobBuffer; |
fmalita | acb882c | 2014-09-16 17:58:34 -0700 | [diff] [blame] | 1065 | blobBuffer.setTypefaceRecorder(&typefaceSet); |
fmalita | 228a6f2 | 2014-08-28 13:59:42 -0700 | [diff] [blame] | 1066 | blob->flatten(blobBuffer); |
| 1067 | |
fmalita | acb882c | 2014-09-16 17:58:34 -0700 | [diff] [blame] | 1068 | // Unlike most draw ops (which only use one paint/typeface), text blobs may reference |
| 1069 | // an arbitrary number of typefaces. Since the one-paint-per-op model is not applicable, |
| 1070 | // we need to serialize these explicitly. |
| 1071 | TypefaceBuffer typefaceBuffer; |
| 1072 | size_t typefaceSize = is_cross_process(fFlags) |
| 1073 | ? this->getCrossProcessTypefaces(typefaceSet, &typefaceBuffer) |
| 1074 | : this->getInProcessTypefaces(typefaceSet, &typefaceBuffer); |
| 1075 | |
| 1076 | // blob byte count + typeface count + x + y + blob data + an index (cross-process) |
| 1077 | // or pointer (in-process) for each typeface |
| 1078 | size_t size = 2 * sizeof(uint32_t) |
| 1079 | + 2 * sizeof(SkScalar) |
| 1080 | + blobBuffer.bytesWritten() |
| 1081 | + typefaceSize; |
| 1082 | |
fmalita | 228a6f2 | 2014-08-28 13:59:42 -0700 | [diff] [blame] | 1083 | if (this->needOpBytes(size)) { |
| 1084 | this->writeOp(kDrawTextBlob_DrawOp); |
fmalita | acb882c | 2014-09-16 17:58:34 -0700 | [diff] [blame] | 1085 | SkDEBUGCODE(size_t initialOffset = fWriter.bytesWritten();) |
| 1086 | |
fmalita | 228a6f2 | 2014-08-28 13:59:42 -0700 | [diff] [blame] | 1087 | fWriter.writeScalar(x); |
| 1088 | fWriter.writeScalar(y); |
fmalita | acb882c | 2014-09-16 17:58:34 -0700 | [diff] [blame] | 1089 | |
| 1090 | fWriter.write32(typefaceSet.count()); |
| 1091 | fWriter.write(typefaceBuffer.get(), typefaceSize); |
| 1092 | |
fmalita | 228a6f2 | 2014-08-28 13:59:42 -0700 | [diff] [blame] | 1093 | fWriter.write32(SkToU32(blobBuffer.bytesWritten())); |
| 1094 | uint32_t* pad = fWriter.reservePad(blobBuffer.bytesWritten()); |
| 1095 | blobBuffer.writeToMemory(pad); |
fmalita | acb882c | 2014-09-16 17:58:34 -0700 | [diff] [blame] | 1096 | |
| 1097 | SkASSERT(initialOffset + size == fWriter.bytesWritten()); |
fmalita | 228a6f2 | 2014-08-28 13:59:42 -0700 | [diff] [blame] | 1098 | } |
fmalita | b742517 | 2014-08-26 07:56:44 -0700 | [diff] [blame] | 1099 | } |
| 1100 | |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 1101 | void SkGPipeCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix, |
| 1102 | const SkPaint* paint) { |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 1103 | // we want to playback the picture into individual draw calls |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 1104 | // |
| 1105 | // todo: do we always have to unroll? If the pipe is not cross-process, seems like |
| 1106 | // we could just ref the picture and move on...? <reed, scroggo> |
| 1107 | // |
| 1108 | this->INHERITED::onDrawPicture(picture, matrix, paint); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
reed@google.com | 85e143c | 2013-12-30 15:51:25 +0000 | [diff] [blame] | 1111 | void SkGPipeCanvas::drawVertices(VertexMode vmode, int vertexCount, |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1112 | const SkPoint vertices[], const SkPoint texs[], |
reed@google.com | 85e143c | 2013-12-30 15:51:25 +0000 | [diff] [blame] | 1113 | const SkColor colors[], SkXfermode* xfer, |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1114 | const uint16_t indices[], int indexCount, |
| 1115 | const SkPaint& paint) { |
| 1116 | if (0 == vertexCount) { |
| 1117 | return; |
| 1118 | } |
| 1119 | |
commit-bot@chromium.org | f700fb2 | 2014-05-14 14:50:57 +0000 | [diff] [blame] | 1120 | NOTIFY_SETUP(this); |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 1121 | this->writePaint(paint); |
commit-bot@chromium.org | d049017 | 2014-05-14 15:12:08 +0000 | [diff] [blame] | 1122 | |
| 1123 | unsigned flags = 0; // packs with the op, so needs no extra space |
| 1124 | |
| 1125 | size_t size = 0; |
| 1126 | size += 4; // vmode |
| 1127 | size += 4; // vertex count |
| 1128 | size += vertexCount * sizeof(SkPoint); // vertices |
| 1129 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1130 | if (texs) { |
| 1131 | flags |= kDrawVertices_HasTexs_DrawOpFlag; |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1132 | size += vertexCount * sizeof(SkPoint); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1133 | } |
| 1134 | if (colors) { |
| 1135 | flags |= kDrawVertices_HasColors_DrawOpFlag; |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1136 | size += vertexCount * sizeof(SkColor); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1137 | } |
commit-bot@chromium.org | f700fb2 | 2014-05-14 14:50:57 +0000 | [diff] [blame] | 1138 | if (xfer && !SkXfermode::IsMode(xfer, SkXfermode::kModulate_Mode)) { |
| 1139 | flags |= kDrawVertices_HasXfermode_DrawOpFlag; |
commit-bot@chromium.org | d049017 | 2014-05-14 15:12:08 +0000 | [diff] [blame] | 1140 | size += sizeof(int32_t); // SkXfermode::Mode |
| 1141 | } |
| 1142 | if (indices && indexCount > 0) { |
| 1143 | flags |= kDrawVertices_HasIndices_DrawOpFlag; |
| 1144 | size += 4; // index count |
| 1145 | size += SkAlign4(indexCount * sizeof(uint16_t)); // indices |
reed@google.com | 85e143c | 2013-12-30 15:51:25 +0000 | [diff] [blame] | 1146 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 1147 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1148 | if (this->needOpBytes(size)) { |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 1149 | this->writeOp(kDrawVertices_DrawOp, flags, 0); |
reed@google.com | 85e143c | 2013-12-30 15:51:25 +0000 | [diff] [blame] | 1150 | fWriter.write32(vmode); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1151 | fWriter.write32(vertexCount); |
| 1152 | fWriter.write(vertices, vertexCount * sizeof(SkPoint)); |
commit-bot@chromium.org | d049017 | 2014-05-14 15:12:08 +0000 | [diff] [blame] | 1153 | if (flags & kDrawVertices_HasTexs_DrawOpFlag) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1154 | fWriter.write(texs, vertexCount * sizeof(SkPoint)); |
| 1155 | } |
commit-bot@chromium.org | d049017 | 2014-05-14 15:12:08 +0000 | [diff] [blame] | 1156 | if (flags & kDrawVertices_HasColors_DrawOpFlag) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1157 | fWriter.write(colors, vertexCount * sizeof(SkColor)); |
| 1158 | } |
reed@google.com | 85e143c | 2013-12-30 15:51:25 +0000 | [diff] [blame] | 1159 | if (flags & kDrawVertices_HasXfermode_DrawOpFlag) { |
| 1160 | SkXfermode::Mode mode = SkXfermode::kModulate_Mode; |
commit-bot@chromium.org | d049017 | 2014-05-14 15:12:08 +0000 | [diff] [blame] | 1161 | SkAssertResult(xfer->asMode(&mode)); |
reed@google.com | 85e143c | 2013-12-30 15:51:25 +0000 | [diff] [blame] | 1162 | fWriter.write32(mode); |
| 1163 | } |
commit-bot@chromium.org | d049017 | 2014-05-14 15:12:08 +0000 | [diff] [blame] | 1164 | if (flags & kDrawVertices_HasIndices_DrawOpFlag) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1165 | fWriter.write32(indexCount); |
| 1166 | fWriter.writePad(indices, indexCount * sizeof(uint16_t)); |
| 1167 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1168 | } |
| 1169 | } |
| 1170 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 1171 | void SkGPipeCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4], |
| 1172 | const SkPoint texCoords[4], SkXfermode* xmode, |
| 1173 | const SkPaint& paint) { |
dandov | 963137b | 2014-08-07 07:49:53 -0700 | [diff] [blame] | 1174 | NOTIFY_SETUP(this); |
piotaixr | 9bb7539 | 2014-10-22 11:02:32 -0700 | [diff] [blame] | 1175 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 1176 | size_t size = SkPatchUtils::kNumCtrlPts * sizeof(SkPoint); |
| 1177 | unsigned flags = 0; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1178 | if (colors) { |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 1179 | flags |= kDrawVertices_HasColors_DrawOpFlag; |
| 1180 | size += SkPatchUtils::kNumCorners * sizeof(SkColor); |
| 1181 | } |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1182 | if (texCoords) { |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 1183 | flags |= kDrawVertices_HasTexs_DrawOpFlag; |
| 1184 | size += SkPatchUtils::kNumCorners * sizeof(SkPoint); |
| 1185 | } |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1186 | if (xmode) { |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 1187 | SkXfermode::Mode mode; |
| 1188 | if (xmode->asMode(&mode) && SkXfermode::kModulate_Mode != mode) { |
| 1189 | flags |= kDrawVertices_HasXfermode_DrawOpFlag; |
| 1190 | size += sizeof(int32_t); |
| 1191 | } |
| 1192 | } |
piotaixr | 9bb7539 | 2014-10-22 11:02:32 -0700 | [diff] [blame] | 1193 | |
dandov | 963137b | 2014-08-07 07:49:53 -0700 | [diff] [blame] | 1194 | this->writePaint(paint); |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 1195 | if (this->needOpBytes(size)) { |
| 1196 | this->writeOp(kDrawPatch_DrawOp, flags, 0); |
piotaixr | 9bb7539 | 2014-10-22 11:02:32 -0700 | [diff] [blame] | 1197 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 1198 | fWriter.write(cubics, SkPatchUtils::kNumCtrlPts * sizeof(SkPoint)); |
piotaixr | 9bb7539 | 2014-10-22 11:02:32 -0700 | [diff] [blame] | 1199 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1200 | if (colors) { |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 1201 | fWriter.write(colors, SkPatchUtils::kNumCorners * sizeof(SkColor)); |
| 1202 | } |
piotaixr | 9bb7539 | 2014-10-22 11:02:32 -0700 | [diff] [blame] | 1203 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1204 | if (texCoords) { |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 1205 | fWriter.write(texCoords, SkPatchUtils::kNumCorners * sizeof(SkPoint)); |
| 1206 | } |
piotaixr | 9bb7539 | 2014-10-22 11:02:32 -0700 | [diff] [blame] | 1207 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 1208 | if (flags & kDrawVertices_HasXfermode_DrawOpFlag) { |
| 1209 | SkXfermode::Mode mode = SkXfermode::kModulate_Mode; |
| 1210 | SkAssertResult(xmode->asMode(&mode)); |
| 1211 | fWriter.write32(mode); |
| 1212 | } |
dandov | 963137b | 2014-08-07 07:49:53 -0700 | [diff] [blame] | 1213 | } |
| 1214 | } |
| 1215 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1216 | void SkGPipeCanvas::drawData(const void* ptr, size_t size) { |
| 1217 | if (size && ptr) { |
commit-bot@chromium.org | f700fb2 | 2014-05-14 14:50:57 +0000 | [diff] [blame] | 1218 | NOTIFY_SETUP(this); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1219 | unsigned data = 0; |
| 1220 | if (size < (1 << DRAWOPS_DATA_BITS)) { |
| 1221 | data = (unsigned)size; |
| 1222 | } |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1223 | if (this->needOpBytes(4 + SkAlign4(size))) { |
| 1224 | this->writeOp(kDrawData_DrawOp, 0, data); |
| 1225 | if (0 == data) { |
commit-bot@chromium.org | b45bd1f | 2014-04-24 18:17:30 +0000 | [diff] [blame] | 1226 | fWriter.write32(SkToU32(size)); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1227 | } |
reed@google.com | bb6793b | 2011-05-05 15:18:15 +0000 | [diff] [blame] | 1228 | fWriter.writePad(ptr, size); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1229 | } |
| 1230 | } |
| 1231 | } |
| 1232 | |
robertphillips@google.com | 0a4805e | 2013-05-29 13:24:23 +0000 | [diff] [blame] | 1233 | void SkGPipeCanvas::beginCommentGroup(const char* description) { |
| 1234 | // ignore for now |
| 1235 | } |
| 1236 | |
| 1237 | void SkGPipeCanvas::addComment(const char* kywd, const char* value) { |
| 1238 | // ignore for now |
| 1239 | } |
| 1240 | |
| 1241 | void SkGPipeCanvas::endCommentGroup() { |
| 1242 | // ignore for now |
| 1243 | } |
| 1244 | |
junov@chromium.org | 77eec24 | 2012-07-18 17:54:45 +0000 | [diff] [blame] | 1245 | void SkGPipeCanvas::flushRecording(bool detachCurrentBlock) { |
commit-bot@chromium.org | d049017 | 2014-05-14 15:12:08 +0000 | [diff] [blame] | 1246 | this->doNotify(); |
junov@chromium.org | 77eec24 | 2012-07-18 17:54:45 +0000 | [diff] [blame] | 1247 | if (detachCurrentBlock) { |
| 1248 | // force a new block to be requested for the next recorded command |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1249 | fBlockSize = 0; |
junov@chromium.org | 77eec24 | 2012-07-18 17:54:45 +0000 | [diff] [blame] | 1250 | } |
| 1251 | } |
| 1252 | |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 1253 | size_t SkGPipeCanvas::freeMemoryIfPossible(size_t bytesToFree) { |
scroggo@google.com | d5d158b | 2012-08-14 20:38:28 +0000 | [diff] [blame] | 1254 | return (NULL == fBitmapHeap) ? 0 : fBitmapHeap->freeMemoryIfPossible(bytesToFree); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 1255 | } |
| 1256 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1257 | /////////////////////////////////////////////////////////////////////////////// |
| 1258 | |
| 1259 | template <typename T> uint32_t castToU32(T value) { |
| 1260 | union { |
| 1261 | T fSrc; |
| 1262 | uint32_t fDst; |
| 1263 | } data; |
| 1264 | data.fSrc = value; |
| 1265 | return data.fDst; |
| 1266 | } |
| 1267 | |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 1268 | void SkGPipeCanvas::writePaint(const SkPaint& paint) { |
scroggo@google.com | d5d158b | 2012-08-14 20:38:28 +0000 | [diff] [blame] | 1269 | if (fDone) { |
| 1270 | return; |
| 1271 | } |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 1272 | SkPaint& base = fPaint; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1273 | uint32_t storage[32]; |
| 1274 | uint32_t* ptr = storage; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1275 | |
| 1276 | if (base.getFlags() != paint.getFlags()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1277 | *ptr++ = PaintOp_packOpData(kFlags_PaintOp, paint.getFlags()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 1278 | base.setFlags(paint.getFlags()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1279 | } |
| 1280 | if (base.getColor() != paint.getColor()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1281 | *ptr++ = PaintOp_packOp(kColor_PaintOp); |
| 1282 | *ptr++ = paint.getColor(); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 1283 | base.setColor(paint.getColor()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1284 | } |
commit-bot@chromium.org | 85faf50 | 2014-04-16 12:58:02 +0000 | [diff] [blame] | 1285 | if (base.getFilterLevel() != paint.getFilterLevel()) { |
| 1286 | *ptr++ = PaintOp_packOpData(kFilterLevel_PaintOp, paint.getFilterLevel()); |
| 1287 | base.setFilterLevel(paint.getFilterLevel()); |
| 1288 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1289 | if (base.getStyle() != paint.getStyle()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1290 | *ptr++ = PaintOp_packOpData(kStyle_PaintOp, paint.getStyle()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 1291 | base.setStyle(paint.getStyle()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1292 | } |
| 1293 | if (base.getStrokeJoin() != paint.getStrokeJoin()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1294 | *ptr++ = PaintOp_packOpData(kJoin_PaintOp, paint.getStrokeJoin()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 1295 | base.setStrokeJoin(paint.getStrokeJoin()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1296 | } |
| 1297 | if (base.getStrokeCap() != paint.getStrokeCap()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1298 | *ptr++ = PaintOp_packOpData(kCap_PaintOp, paint.getStrokeCap()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 1299 | base.setStrokeCap(paint.getStrokeCap()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1300 | } |
| 1301 | if (base.getStrokeWidth() != paint.getStrokeWidth()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1302 | *ptr++ = PaintOp_packOp(kWidth_PaintOp); |
| 1303 | *ptr++ = castToU32(paint.getStrokeWidth()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 1304 | base.setStrokeWidth(paint.getStrokeWidth()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1305 | } |
| 1306 | if (base.getStrokeMiter() != paint.getStrokeMiter()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1307 | *ptr++ = PaintOp_packOp(kMiter_PaintOp); |
| 1308 | *ptr++ = castToU32(paint.getStrokeMiter()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 1309 | base.setStrokeMiter(paint.getStrokeMiter()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1310 | } |
| 1311 | if (base.getTextEncoding() != paint.getTextEncoding()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1312 | *ptr++ = PaintOp_packOpData(kEncoding_PaintOp, paint.getTextEncoding()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 1313 | base.setTextEncoding(paint.getTextEncoding()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1314 | } |
| 1315 | if (base.getHinting() != paint.getHinting()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1316 | *ptr++ = PaintOp_packOpData(kHinting_PaintOp, paint.getHinting()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 1317 | base.setHinting(paint.getHinting()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1318 | } |
| 1319 | if (base.getTextAlign() != paint.getTextAlign()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1320 | *ptr++ = PaintOp_packOpData(kAlign_PaintOp, paint.getTextAlign()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 1321 | base.setTextAlign(paint.getTextAlign()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1322 | } |
| 1323 | if (base.getTextSize() != paint.getTextSize()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1324 | *ptr++ = PaintOp_packOp(kTextSize_PaintOp); |
| 1325 | *ptr++ = castToU32(paint.getTextSize()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 1326 | base.setTextSize(paint.getTextSize()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1327 | } |
| 1328 | if (base.getTextScaleX() != paint.getTextScaleX()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1329 | *ptr++ = PaintOp_packOp(kTextScaleX_PaintOp); |
| 1330 | *ptr++ = castToU32(paint.getTextScaleX()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 1331 | base.setTextScaleX(paint.getTextScaleX()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1332 | } |
| 1333 | if (base.getTextSkewX() != paint.getTextSkewX()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1334 | *ptr++ = PaintOp_packOp(kTextSkewX_PaintOp); |
| 1335 | *ptr++ = castToU32(paint.getTextSkewX()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 1336 | base.setTextSkewX(paint.getTextSkewX()); |
| 1337 | } |
| 1338 | |
| 1339 | if (!SkTypeface::Equal(base.getTypeface(), paint.getTypeface())) { |
fmalita | acb882c | 2014-09-16 17:58:34 -0700 | [diff] [blame] | 1340 | if (is_cross_process(fFlags)) { |
scroggo@google.com | 3cb969f | 2012-07-27 20:39:19 +0000 | [diff] [blame] | 1341 | uint32_t id = this->getTypefaceID(paint.getTypeface()); |
| 1342 | *ptr++ = PaintOp_packOpData(kTypeface_PaintOp, id); |
| 1343 | } else if (this->needOpBytes(sizeof(void*))) { |
| 1344 | // Add to the set for ref counting. |
| 1345 | fTypefaceSet.add(paint.getTypeface()); |
| 1346 | // It is safe to write the typeface to the stream before the rest |
| 1347 | // of the paint unless we ever send a kReset_PaintOp, which we |
| 1348 | // currently never do. |
| 1349 | this->writeOp(kSetTypeface_DrawOp); |
| 1350 | fWriter.writePtr(paint.getTypeface()); |
| 1351 | } |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 1352 | base.setTypeface(paint.getTypeface()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1353 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1354 | |
scroggo@google.com | 4dffc59 | 2012-07-17 16:49:40 +0000 | [diff] [blame] | 1355 | // This is a new paint, so all old flats can be safely purged, if necessary. |
| 1356 | fFlattenableHeap.markAllFlatsSafeToDelete(); |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 1357 | for (int i = 0; i < kCount_PaintFlats; i++) { |
| 1358 | int index = this->flattenToIndex(get_paintflat(paint, i), (PaintFlats)i); |
scroggo@google.com | d3ba5cc | 2012-07-09 16:05:53 +0000 | [diff] [blame] | 1359 | bool replaced = index < 0; |
| 1360 | if (replaced) { |
| 1361 | index = ~index; |
| 1362 | } |
scroggo@google.com | 4dffc59 | 2012-07-17 16:49:40 +0000 | [diff] [blame] | 1363 | // Store the index of any flat that needs to be kept. 0 means no flat. |
| 1364 | if (index > 0) { |
| 1365 | fFlattenableHeap.markFlatForKeeping(index); |
| 1366 | } |
| 1367 | SkASSERT(index >= 0 && index <= fFlatDictionary.count()); |
scroggo@google.com | d3ba5cc | 2012-07-09 16:05:53 +0000 | [diff] [blame] | 1368 | if (index != fCurrFlatIndex[i] || replaced) { |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 1369 | *ptr++ = PaintOp_packOpFlagData(kFlatIndex_PaintOp, i, index); |
| 1370 | fCurrFlatIndex[i] = index; |
| 1371 | } |
| 1372 | } |
| 1373 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1374 | size_t size = (char*)ptr - (char*)storage; |
| 1375 | if (size && this->needOpBytes(size)) { |
commit-bot@chromium.org | b45bd1f | 2014-04-24 18:17:30 +0000 | [diff] [blame] | 1376 | this->writeOp(kPaintOp_DrawOp, 0, SkToU32(size)); |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 1377 | fWriter.write(storage, size); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1378 | for (size_t i = 0; i < size/4; i++) { |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 1379 | // SkDebugf("[%d] %08X\n", i, storage[i]); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1380 | } |
| 1381 | } |
reed@google.com | 0cd2ac6 | 2013-10-14 20:02:44 +0000 | [diff] [blame] | 1382 | |
| 1383 | // |
| 1384 | // Do these after we've written kPaintOp_DrawOp |
skia.committer@gmail.com | fbc58a3 | 2013-10-15 07:02:27 +0000 | [diff] [blame] | 1385 | |
reed@google.com | 0cd2ac6 | 2013-10-14 20:02:44 +0000 | [diff] [blame] | 1386 | if (base.getAnnotation() != paint.getAnnotation()) { |
| 1387 | if (NULL == paint.getAnnotation()) { |
commit-bot@chromium.org | 89ff3dd | 2013-10-29 20:29:38 +0000 | [diff] [blame] | 1388 | if (this->needOpBytes()) { |
| 1389 | this->writeOp(kSetAnnotation_DrawOp, 0, 0); |
| 1390 | } |
reed@google.com | 0cd2ac6 | 2013-10-14 20:02:44 +0000 | [diff] [blame] | 1391 | } else { |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 1392 | SkWriteBuffer buffer; |
reed@google.com | 0cd2ac6 | 2013-10-14 20:02:44 +0000 | [diff] [blame] | 1393 | paint.getAnnotation()->writeToBuffer(buffer); |
commit-bot@chromium.org | 89ff3dd | 2013-10-29 20:29:38 +0000 | [diff] [blame] | 1394 | const size_t size = buffer.bytesWritten(); |
| 1395 | if (this->needOpBytes(size)) { |
commit-bot@chromium.org | b45bd1f | 2014-04-24 18:17:30 +0000 | [diff] [blame] | 1396 | this->writeOp(kSetAnnotation_DrawOp, 0, SkToU32(size)); |
commit-bot@chromium.org | 89ff3dd | 2013-10-29 20:29:38 +0000 | [diff] [blame] | 1397 | buffer.writeToMemory(fWriter.reserve(size)); |
| 1398 | } |
reed@google.com | 0cd2ac6 | 2013-10-14 20:02:44 +0000 | [diff] [blame] | 1399 | } |
commit-bot@chromium.org | 40258a5 | 2013-10-29 19:23:26 +0000 | [diff] [blame] | 1400 | base.setAnnotation(paint.getAnnotation()); |
reed@google.com | 0cd2ac6 | 2013-10-14 20:02:44 +0000 | [diff] [blame] | 1401 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1402 | } |
| 1403 | |
| 1404 | /////////////////////////////////////////////////////////////////////////////// |
| 1405 | |
| 1406 | #include "SkGPipe.h" |
| 1407 | |
scroggo@google.com | 3cb969f | 2012-07-27 20:39:19 +0000 | [diff] [blame] | 1408 | SkGPipeController::~SkGPipeController() { |
| 1409 | SkSafeUnref(fCanvas); |
| 1410 | } |
| 1411 | |
| 1412 | void SkGPipeController::setCanvas(SkGPipeCanvas* canvas) { |
| 1413 | SkRefCnt_SafeAssign(fCanvas, canvas); |
| 1414 | } |
| 1415 | |
| 1416 | /////////////////////////////////////////////////////////////////////////////// |
| 1417 | |
| 1418 | SkGPipeWriter::SkGPipeWriter() |
scroggo@google.com | 0c3e5fe | 2012-08-01 19:34:20 +0000 | [diff] [blame] | 1419 | : fWriter(0) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1420 | fCanvas = NULL; |
| 1421 | } |
| 1422 | |
| 1423 | SkGPipeWriter::~SkGPipeWriter() { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1424 | this->endRecording(); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1425 | } |
| 1426 | |
junov@chromium.org | a8db8fe | 2012-08-15 19:49:22 +0000 | [diff] [blame] | 1427 | SkCanvas* SkGPipeWriter::startRecording(SkGPipeController* controller, uint32_t flags, |
| 1428 | uint32_t width, uint32_t height) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1429 | if (NULL == fCanvas) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1430 | fWriter.reset(NULL, 0); |
junov@chromium.org | a8db8fe | 2012-08-15 19:49:22 +0000 | [diff] [blame] | 1431 | fCanvas = SkNEW_ARGS(SkGPipeCanvas, (controller, &fWriter, flags, width, height)); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1432 | } |
scroggo@google.com | 3cb969f | 2012-07-27 20:39:19 +0000 | [diff] [blame] | 1433 | controller->setCanvas(fCanvas); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1434 | return fCanvas; |
| 1435 | } |
| 1436 | |
| 1437 | void SkGPipeWriter::endRecording() { |
| 1438 | if (fCanvas) { |
scroggo@google.com | 59c3ab6 | 2013-11-12 14:32:38 +0000 | [diff] [blame] | 1439 | fCanvas->finish(true); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1440 | fCanvas->unref(); |
| 1441 | fCanvas = NULL; |
| 1442 | } |
| 1443 | } |
| 1444 | |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 1445 | void SkGPipeWriter::flushRecording(bool detachCurrentBlock) { |
| 1446 | if (fCanvas) { |
| 1447 | fCanvas->flushRecording(detachCurrentBlock); |
| 1448 | } |
junov@chromium.org | 77eec24 | 2012-07-18 17:54:45 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 1451 | size_t SkGPipeWriter::freeMemoryIfPossible(size_t bytesToFree) { |
| 1452 | if (fCanvas) { |
| 1453 | return fCanvas->freeMemoryIfPossible(bytesToFree); |
| 1454 | } |
| 1455 | return 0; |
| 1456 | } |
| 1457 | |
| 1458 | size_t SkGPipeWriter::storageAllocatedForRecording() const { |
scroggo@google.com | 15011ee | 2012-07-26 20:03:32 +0000 | [diff] [blame] | 1459 | return NULL == fCanvas ? 0 : fCanvas->storageAllocatedForRecording(); |
| 1460 | } |
| 1461 | |
scroggo@google.com | 3e26bd0 | 2012-08-14 15:20:01 +0000 | [diff] [blame] | 1462 | /////////////////////////////////////////////////////////////////////////////// |
| 1463 | |
| 1464 | BitmapShuttle::BitmapShuttle(SkGPipeCanvas* canvas) { |
| 1465 | SkASSERT(canvas != NULL); |
| 1466 | fCanvas = canvas; |
| 1467 | fCanvas->ref(); |
| 1468 | } |
| 1469 | |
| 1470 | BitmapShuttle::~BitmapShuttle() { |
scroggo@google.com | 59c3ab6 | 2013-11-12 14:32:38 +0000 | [diff] [blame] | 1471 | this->removeCanvas(); |
scroggo@google.com | 3e26bd0 | 2012-08-14 15:20:01 +0000 | [diff] [blame] | 1472 | } |
| 1473 | |
| 1474 | bool BitmapShuttle::insert(const SkBitmap& bitmap, int32_t slot) { |
scroggo@google.com | 59c3ab6 | 2013-11-12 14:32:38 +0000 | [diff] [blame] | 1475 | SkASSERT(fCanvas != NULL); |
scroggo@google.com | 3e26bd0 | 2012-08-14 15:20:01 +0000 | [diff] [blame] | 1476 | return fCanvas->shuttleBitmap(bitmap, slot); |
| 1477 | } |
scroggo@google.com | 59c3ab6 | 2013-11-12 14:32:38 +0000 | [diff] [blame] | 1478 | |
| 1479 | void BitmapShuttle::removeCanvas() { |
| 1480 | if (NULL == fCanvas) { |
| 1481 | return; |
| 1482 | } |
| 1483 | fCanvas->unref(); |
| 1484 | fCanvas = NULL; |
| 1485 | } |