1 
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 
9 
10 #include "SkLayerRasterizer.h"
11 #include "SkDraw.h"
12 #include "SkReadBuffer.h"
13 #include "SkWriteBuffer.h"
14 #include "SkMask.h"
15 #include "SkMaskFilter.h"
16 #include "SkPaint.h"
17 #include "SkPath.h"
18 #include "SkPathEffect.h"
19 #include "../core/SkRasterClip.h"
20 #include "SkXfermode.h"
21 #include <new>
22 
23 struct SkLayerRasterizer_Rec {
24     SkPaint     fPaint;
25     SkVector    fOffset;
26 };
27 
SkLayerRasterizer()28 SkLayerRasterizer::SkLayerRasterizer()
29     : fLayers(SkNEW_ARGS(SkDeque, (sizeof(SkLayerRasterizer_Rec))))
30 {
31 }
32 
SkLayerRasterizer(SkDeque * layers)33 SkLayerRasterizer::SkLayerRasterizer(SkDeque* layers) : fLayers(layers)
34 {
35 }
36 
37 // Helper function to call destructors on SkPaints held by layers and delete layers.
clean_up_layers(SkDeque * layers)38 static void clean_up_layers(SkDeque* layers) {
39     SkDeque::F2BIter        iter(*layers);
40     SkLayerRasterizer_Rec*  rec;
41 
42     while ((rec = (SkLayerRasterizer_Rec*)iter.next()) != NULL)
43         rec->fPaint.~SkPaint();
44 
45     SkDELETE(layers);
46 }
47 
~SkLayerRasterizer()48 SkLayerRasterizer::~SkLayerRasterizer() {
49     SkASSERT(fLayers);
50     clean_up_layers(const_cast<SkDeque*>(fLayers));
51 }
52 
compute_bounds(const SkDeque & layers,const SkPath & path,const SkMatrix & matrix,const SkIRect * clipBounds,SkIRect * bounds)53 static bool compute_bounds(const SkDeque& layers, const SkPath& path,
54                            const SkMatrix& matrix,
55                            const SkIRect* clipBounds, SkIRect* bounds) {
56     SkDeque::F2BIter        iter(layers);
57     SkLayerRasterizer_Rec*  rec;
58 
59     bounds->set(SK_MaxS32, SK_MaxS32, SK_MinS32, SK_MinS32);
60 
61     while ((rec = (SkLayerRasterizer_Rec*)iter.next()) != NULL) {
62         const SkPaint&  paint = rec->fPaint;
63         SkPath          fillPath, devPath;
64         const SkPath*   p = &path;
65 
66         if (paint.getPathEffect() || paint.getStyle() != SkPaint::kFill_Style) {
67             paint.getFillPath(path, &fillPath);
68             p = &fillPath;
69         }
70         if (p->isEmpty()) {
71             continue;
72         }
73 
74         // apply the matrix and offset
75         {
76             SkMatrix m = matrix;
77             m.preTranslate(rec->fOffset.fX, rec->fOffset.fY);
78             p->transform(m, &devPath);
79         }
80 
81         SkMask  mask;
82         if (!SkDraw::DrawToMask(devPath, clipBounds, paint.getMaskFilter(),
83                                 &matrix, &mask,
84                                 SkMask::kJustComputeBounds_CreateMode,
85                                 SkPaint::kFill_Style)) {
86             return false;
87         }
88 
89         bounds->join(mask.fBounds);
90     }
91     return true;
92 }
93 
onRasterize(const SkPath & path,const SkMatrix & matrix,const SkIRect * clipBounds,SkMask * mask,SkMask::CreateMode mode) const94 bool SkLayerRasterizer::onRasterize(const SkPath& path, const SkMatrix& matrix,
95                                     const SkIRect* clipBounds,
96                                     SkMask* mask, SkMask::CreateMode mode) const {
97     SkASSERT(fLayers);
98     if (fLayers->empty()) {
99         return false;
100     }
101 
102     if (SkMask::kJustRenderImage_CreateMode != mode) {
103         if (!compute_bounds(*fLayers, path, matrix, clipBounds, &mask->fBounds))
104             return false;
105     }
106 
107     if (SkMask::kComputeBoundsAndRenderImage_CreateMode == mode) {
108         mask->fFormat   = SkMask::kA8_Format;
109         mask->fRowBytes = mask->fBounds.width();
110         size_t size = mask->computeImageSize();
111         if (0 == size) {
112             return false;   // too big to allocate, abort
113         }
114         mask->fImage = SkMask::AllocImage(size);
115         memset(mask->fImage, 0, size);
116     }
117 
118     if (SkMask::kJustComputeBounds_CreateMode != mode) {
119         SkBitmap        device;
120         SkRasterClip    rectClip;
121         SkDraw          draw;
122         SkMatrix        translatedMatrix;  // this translates us to our local pixels
123         SkMatrix        drawMatrix;        // this translates the path by each layer's offset
124 
125         rectClip.setRect(SkIRect::MakeWH(mask->fBounds.width(), mask->fBounds.height()));
126 
127         translatedMatrix = matrix;
128         translatedMatrix.postTranslate(-SkIntToScalar(mask->fBounds.fLeft),
129                                        -SkIntToScalar(mask->fBounds.fTop));
130 
131         device.installMaskPixels(*mask);
132 
133         draw.fBitmap    = &device;
134         draw.fMatrix    = &drawMatrix;
135         draw.fRC        = &rectClip;
136         draw.fClip      = &rectClip.bwRgn();
137         // we set the matrixproc in the loop, as the matrix changes each time (potentially)
138 
139         SkDeque::F2BIter        iter(*fLayers);
140         SkLayerRasterizer_Rec*  rec;
141 
142         while ((rec = (SkLayerRasterizer_Rec*)iter.next()) != NULL) {
143             drawMatrix = translatedMatrix;
144             drawMatrix.preTranslate(rec->fOffset.fX, rec->fOffset.fY);
145             draw.drawPath(path, rec->fPaint);
146         }
147     }
148     return true;
149 }
150 
CreateProc(SkReadBuffer & buffer)151 SkFlattenable* SkLayerRasterizer::CreateProc(SkReadBuffer& buffer) {
152     return SkNEW_ARGS(SkLayerRasterizer, (ReadLayers(buffer)));
153 }
154 
ReadLayers(SkReadBuffer & buffer)155 SkDeque* SkLayerRasterizer::ReadLayers(SkReadBuffer& buffer) {
156     int count = buffer.readInt();
157 
158     SkDeque* layers = SkNEW_ARGS(SkDeque, (sizeof(SkLayerRasterizer_Rec)));
159     for (int i = 0; i < count; i++) {
160         SkLayerRasterizer_Rec* rec = (SkLayerRasterizer_Rec*)layers->push_back();
161 
162         SkNEW_PLACEMENT(&rec->fPaint, SkPaint);
163         buffer.readPaint(&rec->fPaint);
164         buffer.readPoint(&rec->fOffset);
165     }
166     return layers;
167 }
168 
flatten(SkWriteBuffer & buffer) const169 void SkLayerRasterizer::flatten(SkWriteBuffer& buffer) const {
170     this->INHERITED::flatten(buffer);
171 
172     SkASSERT(fLayers);
173     buffer.writeInt(fLayers->count());
174 
175     SkDeque::F2BIter                iter(*fLayers);
176     const SkLayerRasterizer_Rec*    rec;
177 
178     while ((rec = (const SkLayerRasterizer_Rec*)iter.next()) != NULL) {
179         buffer.writePaint(rec->fPaint);
180         buffer.writePoint(rec->fOffset);
181     }
182 }
183 
Builder()184 SkLayerRasterizer::Builder::Builder()
185         : fLayers(SkNEW_ARGS(SkDeque, (sizeof(SkLayerRasterizer_Rec))))
186 {
187 }
188 
~Builder()189 SkLayerRasterizer::Builder::~Builder()
190 {
191     if (fLayers != NULL) {
192         clean_up_layers(fLayers);
193     }
194 }
195 
addLayer(const SkPaint & paint,SkScalar dx,SkScalar dy)196 void SkLayerRasterizer::Builder::addLayer(const SkPaint& paint, SkScalar dx,
197                                           SkScalar dy) {
198     SkASSERT(fLayers);
199     SkLayerRasterizer_Rec* rec = (SkLayerRasterizer_Rec*)fLayers->push_back();
200 
201     SkNEW_PLACEMENT_ARGS(&rec->fPaint, SkPaint, (paint));
202     rec->fOffset.set(dx, dy);
203 }
204 
detachRasterizer()205 SkLayerRasterizer* SkLayerRasterizer::Builder::detachRasterizer() {
206     SkLayerRasterizer* rasterizer;
207     if (0 == fLayers->count()) {
208         rasterizer = NULL;
209         SkDELETE(fLayers);
210     } else {
211         rasterizer = SkNEW_ARGS(SkLayerRasterizer, (fLayers));
212     }
213     fLayers = NULL;
214     return rasterizer;
215 }
216 
snapshotRasterizer() const217 SkLayerRasterizer* SkLayerRasterizer::Builder::snapshotRasterizer() const {
218     if (0 == fLayers->count()) {
219         return NULL;
220     }
221     SkDeque* layers = SkNEW_ARGS(SkDeque, (sizeof(SkLayerRasterizer_Rec), fLayers->count()));
222     SkDeque::F2BIter                iter(*fLayers);
223     const SkLayerRasterizer_Rec*    recOrig;
224     SkDEBUGCODE(int                 count = 0;)
225     while ((recOrig = static_cast<SkLayerRasterizer_Rec*>(iter.next())) != NULL) {
226         SkDEBUGCODE(count++);
227         SkLayerRasterizer_Rec* recCopy = static_cast<SkLayerRasterizer_Rec*>(layers->push_back());
228         SkNEW_PLACEMENT_ARGS(&recCopy->fPaint, SkPaint, (recOrig->fPaint));
229         recCopy->fOffset = recOrig->fOffset;
230     }
231     SkASSERT(fLayers->count() == count);
232     SkASSERT(layers->count() == count);
233     SkLayerRasterizer* rasterizer = SkNEW_ARGS(SkLayerRasterizer, (layers));
234     return rasterizer;
235 }
236