reed | 9f01471 | 2014-06-18 15:51:20 -0700 | [diff] [blame] | 1 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2006 The Android Open Source Project |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
reed | 9f01471 | 2014-06-18 15:51:20 -0700 | [diff] [blame] | 9 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #include "SkLayerRasterizer.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 11 | #include "SkDraw.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 12 | #include "SkReadBuffer.h" |
| 13 | #include "SkWriteBuffer.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | #include "SkMask.h" |
| 15 | #include "SkMaskFilter.h" |
| 16 | #include "SkPaint.h" |
| 17 | #include "SkPath.h" |
reed@google.com | 75a0972 | 2012-05-10 12:56:16 +0000 | [diff] [blame] | 18 | #include "SkPathEffect.h" |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 19 | #include "../core/SkRasterClip.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 20 | #include "SkXfermode.h" |
| 21 | #include <new> |
| 22 | |
| 23 | struct SkLayerRasterizer_Rec { |
| 24 | SkPaint fPaint; |
| 25 | SkVector fOffset; |
| 26 | }; |
| 27 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 28 | SkLayerRasterizer::SkLayerRasterizer() : fLayers(new SkDeque(sizeof(SkLayerRasterizer_Rec))) {} |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 29 | |
| 30 | SkLayerRasterizer::SkLayerRasterizer(SkDeque* layers) : fLayers(layers) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 31 | { |
| 32 | } |
| 33 | |
commit-bot@chromium.org | 6573ce7 | 2014-04-10 20:42:53 +0000 | [diff] [blame] | 34 | // Helper function to call destructors on SkPaints held by layers and delete layers. |
| 35 | static void clean_up_layers(SkDeque* layers) { |
| 36 | SkDeque::F2BIter iter(*layers); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 37 | SkLayerRasterizer_Rec* rec; |
| 38 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 39 | while ((rec = (SkLayerRasterizer_Rec*)iter.next()) != nullptr) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 40 | rec->fPaint.~SkPaint(); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 41 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 42 | delete layers; |
commit-bot@chromium.org | 6573ce7 | 2014-04-10 20:42:53 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | SkLayerRasterizer::~SkLayerRasterizer() { |
| 46 | SkASSERT(fLayers); |
| 47 | clean_up_layers(const_cast<SkDeque*>(fLayers)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 48 | } |
| 49 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 50 | static bool compute_bounds(const SkDeque& layers, const SkPath& path, |
| 51 | const SkMatrix& matrix, |
| 52 | const SkIRect* clipBounds, SkIRect* bounds) { |
reed@google.com | 4c09d5c | 2011-02-22 13:16:38 +0000 | [diff] [blame] | 53 | SkDeque::F2BIter iter(layers); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 54 | SkLayerRasterizer_Rec* rec; |
| 55 | |
| 56 | bounds->set(SK_MaxS32, SK_MaxS32, SK_MinS32, SK_MinS32); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 57 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 58 | while ((rec = (SkLayerRasterizer_Rec*)iter.next()) != nullptr) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 59 | const SkPaint& paint = rec->fPaint; |
| 60 | SkPath fillPath, devPath; |
| 61 | const SkPath* p = &path; |
| 62 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 63 | if (paint.getPathEffect() || paint.getStyle() != SkPaint::kFill_Style) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 64 | paint.getFillPath(path, &fillPath); |
| 65 | p = &fillPath; |
| 66 | } |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 67 | if (p->isEmpty()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 68 | continue; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 69 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 70 | |
| 71 | // apply the matrix and offset |
| 72 | { |
| 73 | SkMatrix m = matrix; |
| 74 | m.preTranslate(rec->fOffset.fX, rec->fOffset.fY); |
| 75 | p->transform(m, &devPath); |
| 76 | } |
| 77 | |
| 78 | SkMask mask; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 79 | if (!SkDraw::DrawToMask(devPath, clipBounds, paint.getMaskFilter(), |
| 80 | &matrix, &mask, |
junov@chromium.org | 2ac4ef5 | 2012-04-04 15:16:51 +0000 | [diff] [blame] | 81 | SkMask::kJustComputeBounds_CreateMode, |
| 82 | SkPaint::kFill_Style)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 83 | return false; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 84 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 85 | |
| 86 | bounds->join(mask.fBounds); |
| 87 | } |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | bool SkLayerRasterizer::onRasterize(const SkPath& path, const SkMatrix& matrix, |
| 92 | const SkIRect* clipBounds, |
reed@google.com | fdba404 | 2012-12-18 16:57:03 +0000 | [diff] [blame] | 93 | SkMask* mask, SkMask::CreateMode mode) const { |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 94 | SkASSERT(fLayers); |
| 95 | if (fLayers->empty()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 96 | return false; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 97 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 98 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 99 | if (SkMask::kJustRenderImage_CreateMode != mode) { |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 100 | if (!compute_bounds(*fLayers, path, matrix, clipBounds, &mask->fBounds)) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 101 | return false; |
| 102 | } |
| 103 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 104 | if (SkMask::kComputeBoundsAndRenderImage_CreateMode == mode) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 105 | mask->fFormat = SkMask::kA8_Format; |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 106 | mask->fRowBytes = mask->fBounds.width(); |
reed@android.com | 543ed93 | 2009-04-24 12:43:40 +0000 | [diff] [blame] | 107 | size_t size = mask->computeImageSize(); |
| 108 | if (0 == size) { |
| 109 | return false; // too big to allocate, abort |
| 110 | } |
| 111 | mask->fImage = SkMask::AllocImage(size); |
| 112 | memset(mask->fImage, 0, size); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 113 | } |
| 114 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 115 | if (SkMask::kJustComputeBounds_CreateMode != mode) { |
reed | b3f0ec9 | 2015-06-08 19:58:07 -0700 | [diff] [blame] | 116 | SkDraw draw; |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 117 | if (!draw.fDst.reset(*mask)) { |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | SkRasterClip rectClip; |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 122 | SkMatrix translatedMatrix; // this translates us to our local pixels |
| 123 | SkMatrix drawMatrix; // this translates the path by each layer's offset |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 124 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 125 | rectClip.setRect(SkIRect::MakeWH(mask->fBounds.width(), mask->fBounds.height())); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 126 | |
| 127 | translatedMatrix = matrix; |
| 128 | translatedMatrix.postTranslate(-SkIntToScalar(mask->fBounds.fLeft), |
| 129 | -SkIntToScalar(mask->fBounds.fTop)); |
| 130 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 131 | draw.fMatrix = &drawMatrix; |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 132 | draw.fRC = &rectClip; |
| 133 | draw.fClip = &rectClip.bwRgn(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 134 | // we set the matrixproc in the loop, as the matrix changes each time (potentially) |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 135 | |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 136 | SkDeque::F2BIter iter(*fLayers); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 137 | SkLayerRasterizer_Rec* rec; |
| 138 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 139 | while ((rec = (SkLayerRasterizer_Rec*)iter.next()) != nullptr) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 140 | drawMatrix = translatedMatrix; |
| 141 | drawMatrix.preTranslate(rec->fOffset.fX, rec->fOffset.fY); |
| 142 | draw.drawPath(path, rec->fPaint); |
| 143 | } |
| 144 | } |
| 145 | return true; |
| 146 | } |
| 147 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 148 | SkFlattenable* SkLayerRasterizer::CreateProc(SkReadBuffer& buffer) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 149 | return new SkLayerRasterizer(ReadLayers(buffer)); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 150 | } |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 151 | |
| 152 | SkDeque* SkLayerRasterizer::ReadLayers(SkReadBuffer& buffer) { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 153 | int count = buffer.readInt(); |
mtklein | 3f3b3d0 | 2014-12-01 11:47:08 -0800 | [diff] [blame] | 154 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 155 | SkDeque* layers = new SkDeque(sizeof(SkLayerRasterizer_Rec)); |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 156 | for (int i = 0; i < count; i++) { |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 157 | SkLayerRasterizer_Rec* rec = (SkLayerRasterizer_Rec*)layers->push_back(); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 158 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 159 | new (&rec->fPaint) SkPaint; |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 160 | buffer.readPaint(&rec->fPaint); |
| 161 | buffer.readPoint(&rec->fOffset); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 162 | } |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 163 | return layers; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 164 | } |
| 165 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 166 | void SkLayerRasterizer::flatten(SkWriteBuffer& buffer) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 167 | this->INHERITED::flatten(buffer); |
| 168 | |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 169 | SkASSERT(fLayers); |
| 170 | buffer.writeInt(fLayers->count()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 171 | |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 172 | SkDeque::F2BIter iter(*fLayers); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 173 | const SkLayerRasterizer_Rec* rec; |
| 174 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 175 | while ((rec = (const SkLayerRasterizer_Rec*)iter.next()) != nullptr) { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 176 | buffer.writePaint(rec->fPaint); |
| 177 | buffer.writePoint(rec->fOffset); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 178 | } |
| 179 | } |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 180 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 181 | SkLayerRasterizer::Builder::Builder() : fLayers(new SkDeque(sizeof(SkLayerRasterizer_Rec))) {} |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 182 | |
| 183 | SkLayerRasterizer::Builder::~Builder() |
| 184 | { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 185 | if (fLayers != nullptr) { |
commit-bot@chromium.org | 6573ce7 | 2014-04-10 20:42:53 +0000 | [diff] [blame] | 186 | clean_up_layers(fLayers); |
| 187 | } |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | void SkLayerRasterizer::Builder::addLayer(const SkPaint& paint, SkScalar dx, |
| 191 | SkScalar dy) { |
| 192 | SkASSERT(fLayers); |
| 193 | SkLayerRasterizer_Rec* rec = (SkLayerRasterizer_Rec*)fLayers->push_back(); |
| 194 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 195 | new (&rec->fPaint) SkPaint(paint); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 196 | rec->fOffset.set(dx, dy); |
| 197 | } |
| 198 | |
| 199 | SkLayerRasterizer* SkLayerRasterizer::Builder::detachRasterizer() { |
scroggo | 65044bf | 2014-06-03 13:12:51 -0700 | [diff] [blame] | 200 | SkLayerRasterizer* rasterizer; |
| 201 | if (0 == fLayers->count()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 202 | rasterizer = nullptr; |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 203 | delete fLayers; |
scroggo | 65044bf | 2014-06-03 13:12:51 -0700 | [diff] [blame] | 204 | } else { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 205 | rasterizer = new SkLayerRasterizer(fLayers); |
scroggo | 65044bf | 2014-06-03 13:12:51 -0700 | [diff] [blame] | 206 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 207 | fLayers = nullptr; |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 208 | return rasterizer; |
| 209 | } |
commit-bot@chromium.org | 6573ce7 | 2014-04-10 20:42:53 +0000 | [diff] [blame] | 210 | |
| 211 | SkLayerRasterizer* SkLayerRasterizer::Builder::snapshotRasterizer() const { |
scroggo | 65044bf | 2014-06-03 13:12:51 -0700 | [diff] [blame] | 212 | if (0 == fLayers->count()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 213 | return nullptr; |
scroggo | 65044bf | 2014-06-03 13:12:51 -0700 | [diff] [blame] | 214 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 215 | SkDeque* layers = new SkDeque(sizeof(SkLayerRasterizer_Rec), fLayers->count()); |
commit-bot@chromium.org | 6573ce7 | 2014-04-10 20:42:53 +0000 | [diff] [blame] | 216 | SkDeque::F2BIter iter(*fLayers); |
| 217 | const SkLayerRasterizer_Rec* recOrig; |
| 218 | SkDEBUGCODE(int count = 0;) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 219 | while ((recOrig = static_cast<SkLayerRasterizer_Rec*>(iter.next())) != nullptr) { |
commit-bot@chromium.org | 6573ce7 | 2014-04-10 20:42:53 +0000 | [diff] [blame] | 220 | SkDEBUGCODE(count++); |
| 221 | SkLayerRasterizer_Rec* recCopy = static_cast<SkLayerRasterizer_Rec*>(layers->push_back()); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 222 | new (&recCopy->fPaint) SkPaint(recOrig->fPaint); |
commit-bot@chromium.org | 6573ce7 | 2014-04-10 20:42:53 +0000 | [diff] [blame] | 223 | recCopy->fOffset = recOrig->fOffset; |
| 224 | } |
| 225 | SkASSERT(fLayers->count() == count); |
| 226 | SkASSERT(layers->count() == count); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 227 | SkLayerRasterizer* rasterizer = new SkLayerRasterizer(layers); |
commit-bot@chromium.org | 6573ce7 | 2014-04-10 20:42:53 +0000 | [diff] [blame] | 228 | return rasterizer; |
| 229 | } |