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