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