SkLayerRasterizer.cpp revision c31af44336f5eb4a50e83e76e51962d46c3ed458
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 23struct SkLayerRasterizer_Rec { 24 SkPaint fPaint; 25 SkVector fOffset; 26}; 27 28SkLayerRasterizer::SkLayerRasterizer() 29 : fLayers(SkNEW_ARGS(SkDeque, (sizeof(SkLayerRasterizer_Rec)))) 30{ 31} 32 33SkLayerRasterizer::SkLayerRasterizer(SkDeque* layers) : fLayers(layers) 34{ 35} 36 37// Helper function to call destructors on SkPaints held by layers and delete layers. 38static 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 48SkLayerRasterizer::~SkLayerRasterizer() { 49 SkASSERT(fLayers); 50 clean_up_layers(const_cast<SkDeque*>(fLayers)); 51} 52 53static 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 94bool 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 SkDraw draw; 120 if (!draw.fDst.reset(*mask)) { 121 return false; 122 } 123 124 SkRasterClip rectClip; 125 SkMatrix translatedMatrix; // this translates us to our local pixels 126 SkMatrix drawMatrix; // this translates the path by each layer's offset 127 128 rectClip.setRect(SkIRect::MakeWH(mask->fBounds.width(), mask->fBounds.height())); 129 130 translatedMatrix = matrix; 131 translatedMatrix.postTranslate(-SkIntToScalar(mask->fBounds.fLeft), 132 -SkIntToScalar(mask->fBounds.fTop)); 133 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 151SkFlattenable* SkLayerRasterizer::CreateProc(SkReadBuffer& buffer) { 152 return SkNEW_ARGS(SkLayerRasterizer, (ReadLayers(buffer))); 153} 154 155SkDeque* 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 169void 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 184SkLayerRasterizer::Builder::Builder() 185 : fLayers(SkNEW_ARGS(SkDeque, (sizeof(SkLayerRasterizer_Rec)))) 186{ 187} 188 189SkLayerRasterizer::Builder::~Builder() 190{ 191 if (fLayers != NULL) { 192 clean_up_layers(fLayers); 193 } 194} 195 196void 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 205SkLayerRasterizer* 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 217SkLayerRasterizer* 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