| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2011 Google Inc. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | |
| 18 | #include "SkCanvas.h" |
| reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 19 | #include "SkData.h" |
| reed@google.com | bb6793b | 2011-05-05 15:18:15 +0000 | [diff] [blame] | 20 | #include "SkDevice.h" |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 21 | #include "SkPaint.h" |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 22 | #include "SkGPipe.h" |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 23 | #include "SkGPipePriv.h" |
| reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 24 | #include "SkStream.h" |
| reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 25 | #include "SkTSearch.h" |
| reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 26 | #include "SkTypeface.h" |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 27 | #include "SkWriter32.h" |
| reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 28 | #include "SkColorFilter.h" |
| reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 29 | #include "SkDrawLooper.h" |
| reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 30 | #include "SkMaskFilter.h" |
| 31 | #include "SkRasterizer.h" |
| 32 | #include "SkShader.h" |
| 33 | |
| 34 | static SkFlattenable* get_paintflat(const SkPaint& paint, unsigned paintFlat) { |
| 35 | SkASSERT(paintFlat < kCount_PaintFlats); |
| 36 | switch (paintFlat) { |
| 37 | case kColorFilter_PaintFlat: return paint.getColorFilter(); |
| reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 38 | case kDrawLooper_PaintFlat: return paint.getLooper(); |
| reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 39 | case kMaskFilter_PaintFlat: return paint.getMaskFilter(); |
| 40 | case kPathEffect_PaintFlat: return paint.getPathEffect(); |
| 41 | case kRasterizer_PaintFlat: return paint.getRasterizer(); |
| 42 | case kShader_PaintFlat: return paint.getShader(); |
| 43 | case kXfermode_PaintFlat: return paint.getXfermode(); |
| 44 | } |
| 45 | SkASSERT(!"never gets here"); |
| 46 | return NULL; |
| 47 | } |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 48 | |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 49 | static size_t estimateFlattenSize(const SkPath& path) { |
| 50 | int n = path.countPoints(); |
| 51 | size_t bytes = 3 * sizeof(int32_t); |
| 52 | bytes += n * sizeof(SkPoint); |
| 53 | bytes += SkAlign4(n + 2); // verbs: add 2 for move/close extras |
| 54 | |
| 55 | #ifdef SK_DEBUG |
| 56 | { |
| 57 | SkWriter32 writer(1024); |
| 58 | path.flatten(writer); |
| 59 | SkASSERT(writer.size() <= bytes); |
| 60 | } |
| 61 | #endif |
| 62 | return bytes; |
| 63 | } |
| 64 | |
| reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 65 | static size_t writeTypeface(SkWriter32* writer, SkTypeface* typeface) { |
| 66 | SkASSERT(typeface); |
| 67 | SkDynamicMemoryWStream stream; |
| 68 | typeface->serialize(&stream); |
| 69 | size_t size = stream.getOffset(); |
| 70 | if (writer) { |
| 71 | writer->write32(size); |
| reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 72 | SkAutoDataUnref data(stream.copyToData()); |
| 73 | writer->write(data.data(), size); |
| reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 74 | } |
| 75 | return 4 + size; |
| 76 | } |
| 77 | |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 78 | /////////////////////////////////////////////////////////////////////////////// |
| 79 | |
| 80 | class SkGPipeCanvas : public SkCanvas { |
| 81 | public: |
| reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 82 | SkGPipeCanvas(SkGPipeController*, SkWriter32*, SkFactorySet*); |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 83 | virtual ~SkGPipeCanvas(); |
| 84 | |
| 85 | void finish() { |
| 86 | if (!fDone) { |
| 87 | this->writeOp(kDone_DrawOp); |
| reed@google.com | eb5a815 | 2011-05-23 21:09:13 +0000 | [diff] [blame] | 88 | this->doNotify(); |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 89 | fDone = true; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // overrides from SkCanvas |
| 94 | virtual int save(SaveFlags); |
| 95 | virtual int saveLayer(const SkRect* bounds, const SkPaint*, SaveFlags); |
| 96 | virtual void restore(); |
| 97 | virtual bool translate(SkScalar dx, SkScalar dy); |
| 98 | virtual bool scale(SkScalar sx, SkScalar sy); |
| 99 | virtual bool rotate(SkScalar degrees); |
| 100 | virtual bool skew(SkScalar sx, SkScalar sy); |
| 101 | virtual bool concat(const SkMatrix& matrix); |
| 102 | virtual void setMatrix(const SkMatrix& matrix); |
| 103 | virtual bool clipRect(const SkRect& rect, SkRegion::Op op); |
| 104 | virtual bool clipPath(const SkPath& path, SkRegion::Op op); |
| 105 | virtual bool clipRegion(const SkRegion& region, SkRegion::Op op); |
| 106 | virtual void clear(SkColor); |
| 107 | virtual void drawPaint(const SkPaint& paint); |
| 108 | virtual void drawPoints(PointMode, size_t count, const SkPoint pts[], |
| 109 | const SkPaint&); |
| 110 | virtual void drawRect(const SkRect& rect, const SkPaint&); |
| 111 | virtual void drawPath(const SkPath& path, const SkPaint&); |
| 112 | virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top, |
| 113 | const SkPaint*); |
| 114 | virtual void drawBitmapRect(const SkBitmap&, const SkIRect* src, |
| 115 | const SkRect& dst, const SkPaint*); |
| 116 | virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&, |
| 117 | const SkPaint*); |
| 118 | virtual void drawSprite(const SkBitmap&, int left, int top, |
| 119 | const SkPaint*); |
| 120 | virtual void drawText(const void* text, size_t byteLength, SkScalar x, |
| 121 | SkScalar y, const SkPaint&); |
| 122 | virtual void drawPosText(const void* text, size_t byteLength, |
| 123 | const SkPoint pos[], const SkPaint&); |
| 124 | virtual void drawPosTextH(const void* text, size_t byteLength, |
| 125 | const SkScalar xpos[], SkScalar constY, const SkPaint&); |
| 126 | virtual void drawTextOnPath(const void* text, size_t byteLength, |
| 127 | const SkPath& path, const SkMatrix* matrix, |
| 128 | const SkPaint&); |
| 129 | virtual void drawPicture(SkPicture& picture); |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 130 | virtual void drawVertices(VertexMode, int vertexCount, |
| 131 | const SkPoint vertices[], const SkPoint texs[], |
| 132 | const SkColor colors[], SkXfermode*, |
| 133 | const uint16_t indices[], int indexCount, |
| 134 | const SkPaint&); |
| 135 | virtual void drawData(const void*, size_t); |
| 136 | |
| 137 | private: |
| reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 138 | SkFactorySet* fFactorySet; // optional, only used if cross-process |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 139 | SkGPipeController* fController; |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 140 | SkWriter32& fWriter; |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 141 | size_t fBlockSize; // amount allocated for writer |
| 142 | size_t fBytesNotified; |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 143 | bool fDone; |
| 144 | |
| reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 145 | SkRefCntSet fTypefaceSet; |
| 146 | |
| 147 | uint32_t getTypefaceID(SkTypeface*); |
| 148 | |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 149 | inline void writeOp(DrawOps op, unsigned flags, unsigned data) { |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 150 | fWriter.write32(DrawOp_packOpFlagData(op, flags, data)); |
| 151 | } |
| 152 | |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 153 | inline void writeOp(DrawOps op) { |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 154 | fWriter.write32(DrawOp_packOpFlagData(op, 0, 0)); |
| 155 | } |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 156 | |
| 157 | bool needOpBytes(size_t size = 0); |
| 158 | |
| 159 | inline void doNotify() { |
| 160 | if (!fDone) { |
| 161 | size_t bytes = fWriter.size() - fBytesNotified; |
| 162 | fController->notifyWritten(bytes); |
| 163 | fBytesNotified += bytes; |
| 164 | } |
| 165 | } |
| reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 166 | |
| 167 | struct FlatData { |
| 168 | uint32_t fIndex; // always > 0 |
| 169 | uint32_t fSize; |
| 170 | |
| 171 | void* data() { return (char*)this + sizeof(*this); } |
| 172 | |
| 173 | static int Compare(const FlatData* a, const FlatData* b) { |
| 174 | return memcmp(&a->fSize, &b->fSize, a->fSize + sizeof(a->fSize)); |
| 175 | } |
| 176 | }; |
| 177 | SkTDArray<FlatData*> fFlatArray; |
| 178 | int fCurrFlatIndex[kCount_PaintFlats]; |
| 179 | int flattenToIndex(SkFlattenable* obj, PaintFlats); |
| 180 | |
| reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 181 | SkPaint fPaint; |
| 182 | void writePaint(const SkPaint&); |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 183 | |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 184 | class AutoPipeNotify { |
| 185 | public: |
| 186 | AutoPipeNotify(SkGPipeCanvas* canvas) : fCanvas(canvas) {} |
| 187 | ~AutoPipeNotify() { fCanvas->doNotify(); } |
| 188 | private: |
| 189 | SkGPipeCanvas* fCanvas; |
| 190 | }; |
| 191 | friend class AutoPipeNotify; |
| 192 | |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 193 | typedef SkCanvas INHERITED; |
| 194 | }; |
| 195 | |
| reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 196 | // return 0 for NULL (or unflattenable obj), or index-base-1 |
| 197 | int SkGPipeCanvas::flattenToIndex(SkFlattenable* obj, PaintFlats paintflat) { |
| 198 | if (NULL == obj) { |
| 199 | return 0; |
| 200 | } |
| reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 201 | |
| 202 | SkFlattenableWriteBuffer tmpWriter(1024); |
| reed@google.com | 6bac947 | 2011-06-21 19:24:00 +0000 | [diff] [blame] | 203 | tmpWriter.setFlags(SkFlattenableWriteBuffer::kInlineFactoryNames_Flag); |
| reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 204 | tmpWriter.setFactoryRecorder(fFactorySet); |
| 205 | |
| reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 206 | tmpWriter.writeFlattenable(obj); |
| 207 | size_t len = tmpWriter.size(); |
| 208 | size_t allocSize = len + sizeof(FlatData); |
| 209 | |
| 210 | SkAutoSMalloc<1024> storage(allocSize); |
| 211 | FlatData* flat = (FlatData*)storage.get(); |
| 212 | flat->fSize = len; |
| 213 | tmpWriter.flatten(flat->data()); |
| 214 | |
| 215 | int index = SkTSearch<FlatData>((const FlatData**)fFlatArray.begin(), |
| 216 | fFlatArray.count(), flat, sizeof(flat), |
| 217 | &FlatData::Compare); |
| 218 | if (index < 0) { |
| 219 | index = ~index; |
| 220 | FlatData* copy = (FlatData*)sk_malloc_throw(allocSize); |
| 221 | memcpy(copy, flat, allocSize); |
| 222 | *fFlatArray.insert(index) = copy; |
| 223 | // call this after the insert, so that count() will have been grown |
| 224 | copy->fIndex = fFlatArray.count(); |
| 225 | // SkDebugf("--- add flattenable[%d] size=%d index=%d\n", paintflat, len, copy->fIndex); |
| 226 | |
| 227 | if (this->needOpBytes(len)) { |
| reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 228 | this->writeOp(kDef_Flattenable_DrawOp, paintflat, copy->fIndex); |
| reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 229 | fWriter.write(copy->data(), len); |
| 230 | } |
| 231 | } |
| 232 | return fFlatArray[index]->fIndex; |
| 233 | } |
| 234 | |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 235 | /////////////////////////////////////////////////////////////////////////////// |
| 236 | |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 237 | #define MIN_BLOCK_SIZE (16 * 1024) |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 238 | |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 239 | SkGPipeCanvas::SkGPipeCanvas(SkGPipeController* controller, |
| reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 240 | SkWriter32* writer, SkFactorySet* fset) |
| 241 | : fWriter(*writer), fFactorySet(fset) { |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 242 | fController = controller; |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 243 | fDone = false; |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 244 | fBlockSize = 0; // need first block from controller |
| reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 245 | sk_bzero(fCurrFlatIndex, sizeof(fCurrFlatIndex)); |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 246 | |
| reed@google.com | bb6793b | 2011-05-05 15:18:15 +0000 | [diff] [blame] | 247 | // we need a device to limit our clip |
| 248 | // should the caller give us the bounds? |
| yangsu@google.com | 06b4da1 | 2011-06-17 15:04:40 +0000 | [diff] [blame] | 249 | // We don't allocate pixels for the bitmap |
| reed@google.com | bb6793b | 2011-05-05 15:18:15 +0000 | [diff] [blame] | 250 | SkBitmap bitmap; |
| 251 | bitmap.setConfig(SkBitmap::kARGB_8888_Config, 32767, 32767); |
| yangsu@google.com | 06b4da1 | 2011-06-17 15:04:40 +0000 | [diff] [blame] | 252 | SkDevice* device = SkNEW_ARGS(SkDevice, (bitmap)); |
| reed@google.com | bb6793b | 2011-05-05 15:18:15 +0000 | [diff] [blame] | 253 | this->setDevice(device)->unref(); |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | SkGPipeCanvas::~SkGPipeCanvas() { |
| 257 | this->finish(); |
| 258 | |
| reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 259 | fFlatArray.freeAll(); |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 262 | bool SkGPipeCanvas::needOpBytes(size_t needed) { |
| 263 | if (fDone) { |
| 264 | return false; |
| 265 | } |
| 266 | |
| 267 | needed += 4; // size of DrawOp atom |
| 268 | if (fWriter.size() + needed > fBlockSize) { |
| 269 | void* block = fController->requestBlock(MIN_BLOCK_SIZE, &fBlockSize); |
| 270 | if (NULL == block) { |
| 271 | fDone = true; |
| 272 | return false; |
| 273 | } |
| 274 | fWriter.reset(block, fBlockSize); |
| 275 | fBytesNotified = 0; |
| 276 | } |
| 277 | return true; |
| 278 | } |
| 279 | |
| reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 280 | uint32_t SkGPipeCanvas::getTypefaceID(SkTypeface* face) { |
| 281 | uint32_t id = 0; // 0 means default/null typeface |
| 282 | if (face) { |
| 283 | id = fTypefaceSet.find(face); |
| 284 | if (0 == id) { |
| 285 | id = fTypefaceSet.add(face); |
| 286 | size_t size = writeTypeface(NULL, face); |
| 287 | if (this->needOpBytes(size)) { |
| reed@google.com | bb6793b | 2011-05-05 15:18:15 +0000 | [diff] [blame] | 288 | this->writeOp(kDef_Typeface_DrawOp); |
| reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 289 | writeTypeface(&fWriter, face); |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | return id; |
| 294 | } |
| 295 | |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 296 | /////////////////////////////////////////////////////////////////////////////// |
| 297 | |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 298 | #define NOTIFY_SETUP(canvas) \ |
| 299 | AutoPipeNotify apn(canvas) |
| 300 | |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 301 | int SkGPipeCanvas::save(SaveFlags flags) { |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 302 | NOTIFY_SETUP(this); |
| 303 | if (this->needOpBytes()) { |
| 304 | this->writeOp(kSave_DrawOp, 0, flags); |
| 305 | } |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 306 | return this->INHERITED::save(flags); |
| 307 | } |
| 308 | |
| 309 | int SkGPipeCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint, |
| 310 | SaveFlags saveFlags) { |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 311 | NOTIFY_SETUP(this); |
| 312 | size_t size = 0; |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 313 | unsigned opFlags = 0; |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 314 | |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 315 | if (bounds) { |
| 316 | opFlags |= kSaveLayer_HasBounds_DrawOpFlag; |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 317 | size += sizeof(SkRect); |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 318 | } |
| 319 | if (paint) { |
| 320 | opFlags |= kSaveLayer_HasPaint_DrawOpFlag; |
| reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 321 | this->writePaint(*paint); |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 324 | if (this->needOpBytes(size)) { |
| 325 | this->writeOp(kSaveLayer_DrawOp, opFlags, saveFlags); |
| 326 | if (bounds) { |
| 327 | fWriter.writeRect(*bounds); |
| 328 | } |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | // we just pass on the save, so we don't create a layer |
| 332 | return this->INHERITED::save(saveFlags); |
| 333 | } |
| 334 | |
| 335 | void SkGPipeCanvas::restore() { |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 336 | NOTIFY_SETUP(this); |
| 337 | if (this->needOpBytes()) { |
| 338 | this->writeOp(kRestore_DrawOp); |
| 339 | } |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 340 | this->INHERITED::restore(); |
| 341 | } |
| 342 | |
| 343 | bool SkGPipeCanvas::translate(SkScalar dx, SkScalar dy) { |
| 344 | if (dx || dy) { |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 345 | NOTIFY_SETUP(this); |
| 346 | if (this->needOpBytes(2 * sizeof(SkScalar))) { |
| 347 | this->writeOp(kTranslate_DrawOp); |
| 348 | fWriter.writeScalar(dx); |
| 349 | fWriter.writeScalar(dy); |
| 350 | } |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 351 | } |
| 352 | return this->INHERITED::translate(dx, dy); |
| 353 | } |
| 354 | |
| 355 | bool SkGPipeCanvas::scale(SkScalar sx, SkScalar sy) { |
| 356 | if (sx || sy) { |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 357 | NOTIFY_SETUP(this); |
| 358 | if (this->needOpBytes(2 * sizeof(SkScalar))) { |
| 359 | this->writeOp(kScale_DrawOp); |
| 360 | fWriter.writeScalar(sx); |
| 361 | fWriter.writeScalar(sy); |
| 362 | } |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 363 | } |
| 364 | return this->INHERITED::scale(sx, sy); |
| 365 | } |
| 366 | |
| 367 | bool SkGPipeCanvas::rotate(SkScalar degrees) { |
| 368 | if (degrees) { |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 369 | NOTIFY_SETUP(this); |
| 370 | if (this->needOpBytes(sizeof(SkScalar))) { |
| 371 | this->writeOp(kRotate_DrawOp); |
| 372 | fWriter.writeScalar(degrees); |
| 373 | } |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 374 | } |
| 375 | return this->INHERITED::rotate(degrees); |
| 376 | } |
| 377 | |
| 378 | bool SkGPipeCanvas::skew(SkScalar sx, SkScalar sy) { |
| 379 | if (sx || sy) { |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 380 | NOTIFY_SETUP(this); |
| 381 | if (this->needOpBytes(2 * sizeof(SkScalar))) { |
| 382 | this->writeOp(kSkew_DrawOp); |
| 383 | fWriter.writeScalar(sx); |
| 384 | fWriter.writeScalar(sy); |
| 385 | } |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 386 | } |
| 387 | return this->INHERITED::skew(sx, sy); |
| 388 | } |
| 389 | |
| 390 | bool SkGPipeCanvas::concat(const SkMatrix& matrix) { |
| 391 | if (!matrix.isIdentity()) { |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 392 | NOTIFY_SETUP(this); |
| 393 | if (this->needOpBytes(matrix.flatten(NULL))) { |
| 394 | this->writeOp(kConcat_DrawOp); |
| reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 395 | SkWriteMatrix(&fWriter, matrix); |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 396 | } |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 397 | } |
| 398 | return this->INHERITED::concat(matrix); |
| 399 | } |
| 400 | |
| 401 | void SkGPipeCanvas::setMatrix(const SkMatrix& matrix) { |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 402 | NOTIFY_SETUP(this); |
| 403 | if (this->needOpBytes(matrix.flatten(NULL))) { |
| 404 | this->writeOp(kSetMatrix_DrawOp); |
| reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 405 | SkWriteMatrix(&fWriter, matrix); |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 406 | } |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 407 | this->INHERITED::setMatrix(matrix); |
| 408 | } |
| 409 | |
| 410 | bool SkGPipeCanvas::clipRect(const SkRect& rect, SkRegion::Op rgnOp) { |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 411 | NOTIFY_SETUP(this); |
| 412 | if (this->needOpBytes(sizeof(SkRect))) { |
| 413 | this->writeOp(kClipRect_DrawOp, 0, rgnOp); |
| 414 | fWriter.writeRect(rect); |
| 415 | } |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 416 | return this->INHERITED::clipRect(rect, rgnOp); |
| 417 | } |
| 418 | |
| 419 | bool SkGPipeCanvas::clipPath(const SkPath& path, SkRegion::Op rgnOp) { |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 420 | NOTIFY_SETUP(this); |
| 421 | if (this->needOpBytes(estimateFlattenSize(path))) { |
| 422 | this->writeOp(kClipPath_DrawOp, 0, rgnOp); |
| 423 | path.flatten(fWriter); |
| 424 | } |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 425 | // we just pass on the bounds of the path |
| 426 | return this->INHERITED::clipRect(path.getBounds(), rgnOp); |
| 427 | } |
| 428 | |
| 429 | bool SkGPipeCanvas::clipRegion(const SkRegion& region, SkRegion::Op rgnOp) { |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 430 | NOTIFY_SETUP(this); |
| 431 | if (this->needOpBytes(region.flatten(NULL))) { |
| 432 | this->writeOp(kClipRegion_DrawOp, 0, rgnOp); |
| reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 433 | SkWriteRegion(&fWriter, region); |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 434 | } |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 435 | return this->INHERITED::clipRegion(region, rgnOp); |
| 436 | } |
| 437 | |
| 438 | /////////////////////////////////////////////////////////////////////////////// |
| 439 | |
| 440 | void SkGPipeCanvas::clear(SkColor color) { |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 441 | NOTIFY_SETUP(this); |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 442 | unsigned flags = 0; |
| 443 | if (color) { |
| 444 | flags |= kClear_HasColor_DrawOpFlag; |
| 445 | } |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 446 | if (this->needOpBytes(sizeof(SkColor))) { |
| 447 | this->writeOp(kDrawClear_DrawOp, flags, 0); |
| 448 | if (color) { |
| 449 | fWriter.write32(color); |
| 450 | } |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 451 | } |
| 452 | } |
| 453 | |
| 454 | void SkGPipeCanvas::drawPaint(const SkPaint& paint) { |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 455 | NOTIFY_SETUP(this); |
| reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 456 | this->writePaint(paint); |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 457 | if (this->needOpBytes()) { |
| reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 458 | this->writeOp(kDrawPaint_DrawOp); |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 459 | } |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | void SkGPipeCanvas::drawPoints(PointMode mode, size_t count, |
| 463 | const SkPoint pts[], const SkPaint& paint) { |
| 464 | if (count) { |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 465 | NOTIFY_SETUP(this); |
| reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 466 | this->writePaint(paint); |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 467 | if (this->needOpBytes(4 + count * sizeof(SkPoint))) { |
| reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 468 | this->writeOp(kDrawPoints_DrawOp, mode, 0); |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 469 | fWriter.write32(count); |
| 470 | fWriter.write(pts, count * sizeof(SkPoint)); |
| 471 | } |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | |
| 475 | void SkGPipeCanvas::drawRect(const SkRect& rect, const SkPaint& paint) { |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 476 | NOTIFY_SETUP(this); |
| reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 477 | this->writePaint(paint); |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 478 | if (this->needOpBytes(sizeof(SkRect))) { |
| reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 479 | this->writeOp(kDrawRect_DrawOp); |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 480 | fWriter.writeRect(rect); |
| 481 | } |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | void SkGPipeCanvas::drawPath(const SkPath& path, const SkPaint& paint) { |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 485 | NOTIFY_SETUP(this); |
| reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 486 | this->writePaint(paint); |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 487 | if (this->needOpBytes(estimateFlattenSize(path))) { |
| reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 488 | this->writeOp(kDrawPath_DrawOp); |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 489 | path.flatten(fWriter); |
| 490 | } |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | void SkGPipeCanvas::drawBitmap(const SkBitmap&, SkScalar left, SkScalar top, |
| 494 | const SkPaint*) { |
| 495 | UNIMPLEMENTED |
| 496 | } |
| 497 | |
| 498 | void SkGPipeCanvas::drawBitmapRect(const SkBitmap&, const SkIRect* src, |
| 499 | const SkRect& dst, const SkPaint*) { |
| 500 | UNIMPLEMENTED |
| 501 | } |
| 502 | |
| 503 | void SkGPipeCanvas::drawBitmapMatrix(const SkBitmap&, const SkMatrix&, |
| 504 | const SkPaint*) { |
| 505 | UNIMPLEMENTED |
| 506 | } |
| 507 | |
| 508 | void SkGPipeCanvas::drawSprite(const SkBitmap&, int left, int top, |
| 509 | const SkPaint*) { |
| 510 | UNIMPLEMENTED |
| 511 | } |
| 512 | |
| 513 | void SkGPipeCanvas::drawText(const void* text, size_t byteLength, SkScalar x, |
| 514 | SkScalar y, const SkPaint& paint) { |
| 515 | if (byteLength) { |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 516 | NOTIFY_SETUP(this); |
| reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 517 | this->writePaint(paint); |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 518 | if (this->needOpBytes(4 + SkAlign4(byteLength) + 2 * sizeof(SkScalar))) { |
| reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 519 | this->writeOp(kDrawText_DrawOp); |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 520 | fWriter.write32(byteLength); |
| 521 | fWriter.writePad(text, byteLength); |
| 522 | fWriter.writeScalar(x); |
| 523 | fWriter.writeScalar(y); |
| 524 | } |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 525 | } |
| 526 | } |
| 527 | |
| 528 | void SkGPipeCanvas::drawPosText(const void* text, size_t byteLength, |
| 529 | const SkPoint pos[], const SkPaint& paint) { |
| 530 | if (byteLength) { |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 531 | NOTIFY_SETUP(this); |
| reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 532 | this->writePaint(paint); |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 533 | int count = paint.textToGlyphs(text, byteLength, NULL); |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 534 | if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkPoint))) { |
| reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 535 | this->writeOp(kDrawPosText_DrawOp); |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 536 | fWriter.write32(byteLength); |
| 537 | fWriter.writePad(text, byteLength); |
| 538 | fWriter.write32(count); |
| 539 | fWriter.write(pos, count * sizeof(SkPoint)); |
| 540 | } |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | |
| 544 | void SkGPipeCanvas::drawPosTextH(const void* text, size_t byteLength, |
| 545 | const SkScalar xpos[], SkScalar constY, |
| 546 | const SkPaint& paint) { |
| 547 | if (byteLength) { |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 548 | NOTIFY_SETUP(this); |
| reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 549 | this->writePaint(paint); |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 550 | int count = paint.textToGlyphs(text, byteLength, NULL); |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 551 | if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkScalar) + 4)) { |
| reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 552 | this->writeOp(kDrawPosTextH_DrawOp); |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 553 | fWriter.write32(byteLength); |
| 554 | fWriter.writePad(text, byteLength); |
| 555 | fWriter.write32(count); |
| 556 | fWriter.write(xpos, count * sizeof(SkScalar)); |
| 557 | fWriter.writeScalar(constY); |
| 558 | } |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 559 | } |
| 560 | } |
| 561 | |
| 562 | void SkGPipeCanvas::drawTextOnPath(const void* text, size_t byteLength, |
| 563 | const SkPath& path, const SkMatrix* matrix, |
| 564 | const SkPaint& paint) { |
| 565 | if (byteLength) { |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 566 | NOTIFY_SETUP(this); |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 567 | unsigned flags = 0; |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 568 | size_t size = 4 + SkAlign4(byteLength) + estimateFlattenSize(path); |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 569 | if (matrix) { |
| 570 | flags |= kDrawTextOnPath_HasMatrix_DrawOpFlag; |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 571 | size += matrix->flatten(NULL); |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 572 | } |
| reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 573 | this->writePaint(paint); |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 574 | if (this->needOpBytes(size)) { |
| reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 575 | this->writeOp(kDrawTextOnPath_DrawOp, flags, 0); |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 576 | |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 577 | fWriter.write32(byteLength); |
| 578 | fWriter.writePad(text, byteLength); |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 579 | |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 580 | path.flatten(fWriter); |
| 581 | if (matrix) { |
| reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 582 | SkWriteMatrix(&fWriter, *matrix); |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 583 | } |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 584 | } |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | void SkGPipeCanvas::drawPicture(SkPicture& picture) { |
| reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 589 | // we want to playback the picture into individual draw calls |
| 590 | this->INHERITED::drawPicture(picture); |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 591 | } |
| 592 | |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 593 | void SkGPipeCanvas::drawVertices(VertexMode mode, int vertexCount, |
| 594 | const SkPoint vertices[], const SkPoint texs[], |
| 595 | const SkColor colors[], SkXfermode*, |
| 596 | const uint16_t indices[], int indexCount, |
| 597 | const SkPaint& paint) { |
| 598 | if (0 == vertexCount) { |
| 599 | return; |
| 600 | } |
| 601 | |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 602 | NOTIFY_SETUP(this); |
| 603 | size_t size = 4 + vertexCount * sizeof(SkPoint); |
| reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 604 | this->writePaint(paint); |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 605 | unsigned flags = 0; |
| 606 | if (texs) { |
| 607 | flags |= kDrawVertices_HasTexs_DrawOpFlag; |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 608 | size += vertexCount * sizeof(SkPoint); |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 609 | } |
| 610 | if (colors) { |
| 611 | flags |= kDrawVertices_HasColors_DrawOpFlag; |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 612 | size += vertexCount * sizeof(SkColor); |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 613 | } |
| 614 | if (indices && indexCount > 0) { |
| 615 | flags |= kDrawVertices_HasIndices_DrawOpFlag; |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 616 | size += 4 + SkAlign4(indexCount * sizeof(uint16_t)); |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 617 | } |
| 618 | |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 619 | if (this->needOpBytes(size)) { |
| reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 620 | this->writeOp(kDrawVertices_DrawOp, flags, 0); |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 621 | fWriter.write32(mode); |
| 622 | fWriter.write32(vertexCount); |
| 623 | fWriter.write(vertices, vertexCount * sizeof(SkPoint)); |
| 624 | if (texs) { |
| 625 | fWriter.write(texs, vertexCount * sizeof(SkPoint)); |
| 626 | } |
| 627 | if (colors) { |
| 628 | fWriter.write(colors, vertexCount * sizeof(SkColor)); |
| 629 | } |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 630 | |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 631 | // TODO: flatten xfermode |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 632 | |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 633 | if (indices && indexCount > 0) { |
| 634 | fWriter.write32(indexCount); |
| 635 | fWriter.writePad(indices, indexCount * sizeof(uint16_t)); |
| 636 | } |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 637 | } |
| 638 | } |
| 639 | |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 640 | void SkGPipeCanvas::drawData(const void* ptr, size_t size) { |
| 641 | if (size && ptr) { |
| 642 | NOTIFY_SETUP(this); |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 643 | unsigned data = 0; |
| 644 | if (size < (1 << DRAWOPS_DATA_BITS)) { |
| 645 | data = (unsigned)size; |
| 646 | } |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 647 | if (this->needOpBytes(4 + SkAlign4(size))) { |
| 648 | this->writeOp(kDrawData_DrawOp, 0, data); |
| 649 | if (0 == data) { |
| 650 | fWriter.write32(size); |
| 651 | } |
| reed@google.com | bb6793b | 2011-05-05 15:18:15 +0000 | [diff] [blame] | 652 | fWriter.writePad(ptr, size); |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 653 | } |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | /////////////////////////////////////////////////////////////////////////////// |
| 658 | |
| 659 | template <typename T> uint32_t castToU32(T value) { |
| 660 | union { |
| 661 | T fSrc; |
| 662 | uint32_t fDst; |
| 663 | } data; |
| 664 | data.fSrc = value; |
| 665 | return data.fDst; |
| 666 | } |
| 667 | |
| reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 668 | void SkGPipeCanvas::writePaint(const SkPaint& paint) { |
| 669 | SkPaint& base = fPaint; |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 670 | uint32_t storage[32]; |
| 671 | uint32_t* ptr = storage; |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 672 | |
| 673 | if (base.getFlags() != paint.getFlags()) { |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 674 | *ptr++ = PaintOp_packOpData(kFlags_PaintOp, paint.getFlags()); |
| reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 675 | base.setFlags(paint.getFlags()); |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 676 | } |
| 677 | if (base.getColor() != paint.getColor()) { |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 678 | *ptr++ = PaintOp_packOp(kColor_PaintOp); |
| 679 | *ptr++ = paint.getColor(); |
| reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 680 | base.setColor(paint.getColor()); |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 681 | } |
| 682 | if (base.getStyle() != paint.getStyle()) { |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 683 | *ptr++ = PaintOp_packOpData(kStyle_PaintOp, paint.getStyle()); |
| reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 684 | base.setStyle(paint.getStyle()); |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 685 | } |
| 686 | if (base.getStrokeJoin() != paint.getStrokeJoin()) { |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 687 | *ptr++ = PaintOp_packOpData(kJoin_PaintOp, paint.getStrokeJoin()); |
| reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 688 | base.setStrokeJoin(paint.getStrokeJoin()); |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 689 | } |
| 690 | if (base.getStrokeCap() != paint.getStrokeCap()) { |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 691 | *ptr++ = PaintOp_packOpData(kCap_PaintOp, paint.getStrokeCap()); |
| reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 692 | base.setStrokeCap(paint.getStrokeCap()); |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 693 | } |
| 694 | if (base.getStrokeWidth() != paint.getStrokeWidth()) { |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 695 | *ptr++ = PaintOp_packOp(kWidth_PaintOp); |
| 696 | *ptr++ = castToU32(paint.getStrokeWidth()); |
| reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 697 | base.setStrokeWidth(paint.getStrokeWidth()); |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 698 | } |
| 699 | if (base.getStrokeMiter() != paint.getStrokeMiter()) { |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 700 | *ptr++ = PaintOp_packOp(kMiter_PaintOp); |
| 701 | *ptr++ = castToU32(paint.getStrokeMiter()); |
| reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 702 | base.setStrokeMiter(paint.getStrokeMiter()); |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 703 | } |
| 704 | if (base.getTextEncoding() != paint.getTextEncoding()) { |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 705 | *ptr++ = PaintOp_packOpData(kEncoding_PaintOp, paint.getTextEncoding()); |
| reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 706 | base.setTextEncoding(paint.getTextEncoding()); |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 707 | } |
| 708 | if (base.getHinting() != paint.getHinting()) { |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 709 | *ptr++ = PaintOp_packOpData(kHinting_PaintOp, paint.getHinting()); |
| reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 710 | base.setHinting(paint.getHinting()); |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 711 | } |
| 712 | if (base.getTextAlign() != paint.getTextAlign()) { |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 713 | *ptr++ = PaintOp_packOpData(kAlign_PaintOp, paint.getTextAlign()); |
| reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 714 | base.setTextAlign(paint.getTextAlign()); |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 715 | } |
| 716 | if (base.getTextSize() != paint.getTextSize()) { |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 717 | *ptr++ = PaintOp_packOp(kTextSize_PaintOp); |
| 718 | *ptr++ = castToU32(paint.getTextSize()); |
| reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 719 | base.setTextSize(paint.getTextSize()); |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 720 | } |
| 721 | if (base.getTextScaleX() != paint.getTextScaleX()) { |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 722 | *ptr++ = PaintOp_packOp(kTextScaleX_PaintOp); |
| 723 | *ptr++ = castToU32(paint.getTextScaleX()); |
| reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 724 | base.setTextScaleX(paint.getTextScaleX()); |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 725 | } |
| 726 | if (base.getTextSkewX() != paint.getTextSkewX()) { |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 727 | *ptr++ = PaintOp_packOp(kTextSkewX_PaintOp); |
| 728 | *ptr++ = castToU32(paint.getTextSkewX()); |
| reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 729 | base.setTextSkewX(paint.getTextSkewX()); |
| 730 | } |
| 731 | |
| 732 | if (!SkTypeface::Equal(base.getTypeface(), paint.getTypeface())) { |
| 733 | uint32_t id = this->getTypefaceID(paint.getTypeface()); |
| reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 734 | *ptr++ = PaintOp_packOpData(kTypeface_PaintOp, id); |
| 735 | base.setTypeface(paint.getTypeface()); |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 736 | } |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 737 | |
| reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 738 | for (int i = 0; i < kCount_PaintFlats; i++) { |
| 739 | int index = this->flattenToIndex(get_paintflat(paint, i), (PaintFlats)i); |
| 740 | SkASSERT(index >= 0 && index <= fFlatArray.count()); |
| 741 | if (index != fCurrFlatIndex[i]) { |
| reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 742 | *ptr++ = PaintOp_packOpFlagData(kFlatIndex_PaintOp, i, index); |
| 743 | fCurrFlatIndex[i] = index; |
| 744 | } |
| 745 | } |
| 746 | |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 747 | size_t size = (char*)ptr - (char*)storage; |
| 748 | if (size && this->needOpBytes(size)) { |
| reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 749 | this->writeOp(kPaintOp_DrawOp, 0, size); |
| reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 750 | fWriter.write(storage, size); |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 751 | for (size_t i = 0; i < size/4; i++) { |
| reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 752 | // SkDebugf("[%d] %08X\n", i, storage[i]); |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 753 | } |
| 754 | } |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | /////////////////////////////////////////////////////////////////////////////// |
| 758 | |
| 759 | #include "SkGPipe.h" |
| 760 | |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 761 | SkGPipeWriter::SkGPipeWriter() : fWriter(0) { |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 762 | fCanvas = NULL; |
| 763 | } |
| 764 | |
| 765 | SkGPipeWriter::~SkGPipeWriter() { |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 766 | this->endRecording(); |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 767 | SkSafeUnref(fCanvas); |
| 768 | } |
| 769 | |
| reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 770 | SkCanvas* SkGPipeWriter::startRecording(SkGPipeController* controller, |
| 771 | uint32_t flags) { |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 772 | if (NULL == fCanvas) { |
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 773 | fWriter.reset(NULL, 0); |
| reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 774 | fFactorySet.reset(); |
| 775 | fCanvas = SkNEW_ARGS(SkGPipeCanvas, (controller, &fWriter, |
| 776 | (flags & kCrossProcess_Flag) ? |
| 777 | &fFactorySet : NULL)); |
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 778 | } |
| 779 | return fCanvas; |
| 780 | } |
| 781 | |
| 782 | void SkGPipeWriter::endRecording() { |
| 783 | if (fCanvas) { |
| 784 | fCanvas->finish(); |
| 785 | fCanvas->unref(); |
| 786 | fCanvas = NULL; |
| 787 | } |
| 788 | } |
| 789 | |