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