blob: 6c08afc6f5c164022c03ac3a1ce6977a2089ebfc [file] [log] [blame]
reed9f014712014-06-18 15:51:20 -07001
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002/*
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
reed9f014712014-06-18 15:51:20 -07009
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkLayerRasterizer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000011#include "SkDraw.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000012#include "SkReadBuffer.h"
13#include "SkWriteBuffer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#include "SkMask.h"
15#include "SkMaskFilter.h"
16#include "SkPaint.h"
17#include "SkPath.h"
reed@google.com75a09722012-05-10 12:56:16 +000018#include "SkPathEffect.h"
reed@google.com045e62d2011-10-24 12:19:46 +000019#include "../core/SkRasterClip.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000020#include "SkXfermode.h"
21#include <new>
22
23struct SkLayerRasterizer_Rec {
24 SkPaint fPaint;
25 SkVector fOffset;
26};
27
halcanary385fe4d2015-08-26 13:07:48 -070028SkLayerRasterizer::SkLayerRasterizer() : fLayers(new SkDeque(sizeof(SkLayerRasterizer_Rec))) {}
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000029
30SkLayerRasterizer::SkLayerRasterizer(SkDeque* layers) : fLayers(layers)
reed@android.com8a1c16f2008-12-17 15:59:43 +000031{
32}
33
commit-bot@chromium.org6573ce72014-04-10 20:42:53 +000034// Helper function to call destructors on SkPaints held by layers and delete layers.
35static void clean_up_layers(SkDeque* layers) {
36 SkDeque::F2BIter iter(*layers);
reed@android.com8a1c16f2008-12-17 15:59:43 +000037 SkLayerRasterizer_Rec* rec;
38
halcanary96fcdcc2015-08-27 07:41:13 -070039 while ((rec = (SkLayerRasterizer_Rec*)iter.next()) != nullptr)
reed@android.com8a1c16f2008-12-17 15:59:43 +000040 rec->fPaint.~SkPaint();
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000041
halcanary385fe4d2015-08-26 13:07:48 -070042 delete layers;
commit-bot@chromium.org6573ce72014-04-10 20:42:53 +000043}
44
45SkLayerRasterizer::~SkLayerRasterizer() {
46 SkASSERT(fLayers);
47 clean_up_layers(const_cast<SkDeque*>(fLayers));
reed@android.com8a1c16f2008-12-17 15:59:43 +000048}
49
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000050static bool compute_bounds(const SkDeque& layers, const SkPath& path,
51 const SkMatrix& matrix,
52 const SkIRect* clipBounds, SkIRect* bounds) {
reed@google.com4c09d5c2011-02-22 13:16:38 +000053 SkDeque::F2BIter iter(layers);
reed@android.com8a1c16f2008-12-17 15:59:43 +000054 SkLayerRasterizer_Rec* rec;
55
56 bounds->set(SK_MaxS32, SK_MaxS32, SK_MinS32, SK_MinS32);
reed@google.com82065d62011-02-07 15:30:46 +000057
halcanary96fcdcc2015-08-27 07:41:13 -070058 while ((rec = (SkLayerRasterizer_Rec*)iter.next()) != nullptr) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000059 const SkPaint& paint = rec->fPaint;
60 SkPath fillPath, devPath;
61 const SkPath* p = &path;
62
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000063 if (paint.getPathEffect() || paint.getStyle() != SkPaint::kFill_Style) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000064 paint.getFillPath(path, &fillPath);
65 p = &fillPath;
66 }
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000067 if (p->isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000068 continue;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000069 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000070
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.org3334c3a2011-04-20 11:39:28 +000079 if (!SkDraw::DrawToMask(devPath, clipBounds, paint.getMaskFilter(),
80 &matrix, &mask,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +000081 SkMask::kJustComputeBounds_CreateMode,
82 SkPaint::kFill_Style)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000083 return false;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000084 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000085
86 bounds->join(mask.fBounds);
87 }
88 return true;
89}
90
91bool SkLayerRasterizer::onRasterize(const SkPath& path, const SkMatrix& matrix,
92 const SkIRect* clipBounds,
reed@google.comfdba4042012-12-18 16:57:03 +000093 SkMask* mask, SkMask::CreateMode mode) const {
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000094 SkASSERT(fLayers);
95 if (fLayers->empty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000096 return false;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000097 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000098
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000099 if (SkMask::kJustRenderImage_CreateMode != mode) {
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000100 if (!compute_bounds(*fLayers, path, matrix, clipBounds, &mask->fBounds))
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101 return false;
102 }
103
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000104 if (SkMask::kComputeBoundsAndRenderImage_CreateMode == mode) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105 mask->fFormat = SkMask::kA8_Format;
reed@android.com49f0ff22009-03-19 21:52:42 +0000106 mask->fRowBytes = mask->fBounds.width();
reed@android.com543ed932009-04-24 12:43:40 +0000107 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.com8a1c16f2008-12-17 15:59:43 +0000113 }
114
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000115 if (SkMask::kJustComputeBounds_CreateMode != mode) {
reedb3f0ec92015-06-08 19:58:07 -0700116 SkDraw draw;
reed41e010c2015-06-09 12:16:53 -0700117 if (!draw.fDst.reset(*mask)) {
118 return false;
119 }
120
121 SkRasterClip rectClip;
reed@google.com045e62d2011-10-24 12:19:46 +0000122 SkMatrix translatedMatrix; // this translates us to our local pixels
123 SkMatrix drawMatrix; // this translates the path by each layer's offset
reed@google.com82065d62011-02-07 15:30:46 +0000124
reed@google.com045e62d2011-10-24 12:19:46 +0000125 rectClip.setRect(SkIRect::MakeWH(mask->fBounds.width(), mask->fBounds.height()));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126
127 translatedMatrix = matrix;
128 translatedMatrix.postTranslate(-SkIntToScalar(mask->fBounds.fLeft),
129 -SkIntToScalar(mask->fBounds.fTop));
130
reed@android.com8a1c16f2008-12-17 15:59:43 +0000131 draw.fMatrix = &drawMatrix;
reed@google.com045e62d2011-10-24 12:19:46 +0000132 draw.fRC = &rectClip;
133 draw.fClip = &rectClip.bwRgn();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000134 // we set the matrixproc in the loop, as the matrix changes each time (potentially)
reed@google.com82065d62011-02-07 15:30:46 +0000135
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000136 SkDeque::F2BIter iter(*fLayers);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000137 SkLayerRasterizer_Rec* rec;
138
halcanary96fcdcc2015-08-27 07:41:13 -0700139 while ((rec = (SkLayerRasterizer_Rec*)iter.next()) != nullptr) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000140 drawMatrix = translatedMatrix;
141 drawMatrix.preTranslate(rec->fOffset.fX, rec->fOffset.fY);
142 draw.drawPath(path, rec->fPaint);
143 }
144 }
145 return true;
146}
147
reed9fa60da2014-08-21 07:59:51 -0700148SkFlattenable* SkLayerRasterizer::CreateProc(SkReadBuffer& buffer) {
halcanary385fe4d2015-08-26 13:07:48 -0700149 return new SkLayerRasterizer(ReadLayers(buffer));
reed9fa60da2014-08-21 07:59:51 -0700150}
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000151
152SkDeque* SkLayerRasterizer::ReadLayers(SkReadBuffer& buffer) {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000153 int count = buffer.readInt();
mtklein3f3b3d02014-12-01 11:47:08 -0800154
halcanary385fe4d2015-08-26 13:07:48 -0700155 SkDeque* layers = new SkDeque(sizeof(SkLayerRasterizer_Rec));
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000156 for (int i = 0; i < count; i++) {
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000157 SkLayerRasterizer_Rec* rec = (SkLayerRasterizer_Rec*)layers->push_back();
reed@google.com82065d62011-02-07 15:30:46 +0000158
halcanary385fe4d2015-08-26 13:07:48 -0700159 new (&rec->fPaint) SkPaint;
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000160 buffer.readPaint(&rec->fPaint);
161 buffer.readPoint(&rec->fOffset);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000162 }
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000163 return layers;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000164}
165
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000166void SkLayerRasterizer::flatten(SkWriteBuffer& buffer) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000167 this->INHERITED::flatten(buffer);
168
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000169 SkASSERT(fLayers);
170 buffer.writeInt(fLayers->count());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000171
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000172 SkDeque::F2BIter iter(*fLayers);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000173 const SkLayerRasterizer_Rec* rec;
174
halcanary96fcdcc2015-08-27 07:41:13 -0700175 while ((rec = (const SkLayerRasterizer_Rec*)iter.next()) != nullptr) {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000176 buffer.writePaint(rec->fPaint);
177 buffer.writePoint(rec->fOffset);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000178 }
179}
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000180
halcanary385fe4d2015-08-26 13:07:48 -0700181SkLayerRasterizer::Builder::Builder() : fLayers(new SkDeque(sizeof(SkLayerRasterizer_Rec))) {}
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000182
183SkLayerRasterizer::Builder::~Builder()
184{
halcanary96fcdcc2015-08-27 07:41:13 -0700185 if (fLayers != nullptr) {
commit-bot@chromium.org6573ce72014-04-10 20:42:53 +0000186 clean_up_layers(fLayers);
187 }
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000188}
189
190void SkLayerRasterizer::Builder::addLayer(const SkPaint& paint, SkScalar dx,
191 SkScalar dy) {
192 SkASSERT(fLayers);
193 SkLayerRasterizer_Rec* rec = (SkLayerRasterizer_Rec*)fLayers->push_back();
194
halcanary385fe4d2015-08-26 13:07:48 -0700195 new (&rec->fPaint) SkPaint(paint);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000196 rec->fOffset.set(dx, dy);
197}
198
199SkLayerRasterizer* SkLayerRasterizer::Builder::detachRasterizer() {
scroggo65044bf2014-06-03 13:12:51 -0700200 SkLayerRasterizer* rasterizer;
201 if (0 == fLayers->count()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700202 rasterizer = nullptr;
halcanary385fe4d2015-08-26 13:07:48 -0700203 delete fLayers;
scroggo65044bf2014-06-03 13:12:51 -0700204 } else {
halcanary385fe4d2015-08-26 13:07:48 -0700205 rasterizer = new SkLayerRasterizer(fLayers);
scroggo65044bf2014-06-03 13:12:51 -0700206 }
halcanary96fcdcc2015-08-27 07:41:13 -0700207 fLayers = nullptr;
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000208 return rasterizer;
209}
commit-bot@chromium.org6573ce72014-04-10 20:42:53 +0000210
211SkLayerRasterizer* SkLayerRasterizer::Builder::snapshotRasterizer() const {
scroggo65044bf2014-06-03 13:12:51 -0700212 if (0 == fLayers->count()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700213 return nullptr;
scroggo65044bf2014-06-03 13:12:51 -0700214 }
halcanary385fe4d2015-08-26 13:07:48 -0700215 SkDeque* layers = new SkDeque(sizeof(SkLayerRasterizer_Rec), fLayers->count());
commit-bot@chromium.org6573ce72014-04-10 20:42:53 +0000216 SkDeque::F2BIter iter(*fLayers);
217 const SkLayerRasterizer_Rec* recOrig;
218 SkDEBUGCODE(int count = 0;)
halcanary96fcdcc2015-08-27 07:41:13 -0700219 while ((recOrig = static_cast<SkLayerRasterizer_Rec*>(iter.next())) != nullptr) {
commit-bot@chromium.org6573ce72014-04-10 20:42:53 +0000220 SkDEBUGCODE(count++);
221 SkLayerRasterizer_Rec* recCopy = static_cast<SkLayerRasterizer_Rec*>(layers->push_back());
halcanary385fe4d2015-08-26 13:07:48 -0700222 new (&recCopy->fPaint) SkPaint(recOrig->fPaint);
commit-bot@chromium.org6573ce72014-04-10 20:42:53 +0000223 recCopy->fOffset = recOrig->fOffset;
224 }
225 SkASSERT(fLayers->count() == count);
226 SkASSERT(layers->count() == count);
halcanary385fe4d2015-08-26 13:07:48 -0700227 SkLayerRasterizer* rasterizer = new SkLayerRasterizer(layers);
commit-bot@chromium.org6573ce72014-04-10 20:42:53 +0000228 return rasterizer;
229}