1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/effects/SkLayerRasterizer.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,213 @@ 1.4 + 1.5 +/* 1.6 + * Copyright 2006 The Android Open Source Project 1.7 + * 1.8 + * Use of this source code is governed by a BSD-style license that can be 1.9 + * found in the LICENSE file. 1.10 + */ 1.11 + 1.12 + 1.13 +#include "SkLayerRasterizer.h" 1.14 +#include "SkDraw.h" 1.15 +#include "SkReadBuffer.h" 1.16 +#include "SkWriteBuffer.h" 1.17 +#include "SkMask.h" 1.18 +#include "SkMaskFilter.h" 1.19 +#include "SkPaint.h" 1.20 +#include "SkPath.h" 1.21 +#include "SkPathEffect.h" 1.22 +#include "../core/SkRasterClip.h" 1.23 +#include "SkXfermode.h" 1.24 +#include <new> 1.25 + 1.26 +struct SkLayerRasterizer_Rec { 1.27 + SkPaint fPaint; 1.28 + SkVector fOffset; 1.29 +}; 1.30 + 1.31 +SkLayerRasterizer::SkLayerRasterizer() 1.32 + : fLayers(SkNEW_ARGS(SkDeque, (sizeof(SkLayerRasterizer_Rec)))) 1.33 +{ 1.34 +} 1.35 + 1.36 +SkLayerRasterizer::SkLayerRasterizer(SkDeque* layers) : fLayers(layers) 1.37 +{ 1.38 +} 1.39 + 1.40 +SkLayerRasterizer::~SkLayerRasterizer() { 1.41 + SkASSERT(fLayers); 1.42 + SkDeque::F2BIter iter(*fLayers); 1.43 + SkLayerRasterizer_Rec* rec; 1.44 + 1.45 + while ((rec = (SkLayerRasterizer_Rec*)iter.next()) != NULL) 1.46 + rec->fPaint.~SkPaint(); 1.47 + 1.48 + SkDELETE(fLayers); 1.49 +} 1.50 + 1.51 +#ifdef SK_SUPPORT_LEGACY_LAYERRASTERIZER_API 1.52 +void SkLayerRasterizer::addLayer(const SkPaint& paint, SkScalar dx, 1.53 + SkScalar dy) { 1.54 + SkASSERT(fLayers); 1.55 + SkLayerRasterizer_Rec* rec = (SkLayerRasterizer_Rec*)fLayers->push_back(); 1.56 + 1.57 + SkNEW_PLACEMENT_ARGS(&rec->fPaint, SkPaint, (paint)); 1.58 + rec->fOffset.set(dx, dy); 1.59 +} 1.60 +#endif 1.61 + 1.62 +static bool compute_bounds(const SkDeque& layers, const SkPath& path, 1.63 + const SkMatrix& matrix, 1.64 + const SkIRect* clipBounds, SkIRect* bounds) { 1.65 + SkDeque::F2BIter iter(layers); 1.66 + SkLayerRasterizer_Rec* rec; 1.67 + 1.68 + bounds->set(SK_MaxS32, SK_MaxS32, SK_MinS32, SK_MinS32); 1.69 + 1.70 + while ((rec = (SkLayerRasterizer_Rec*)iter.next()) != NULL) { 1.71 + const SkPaint& paint = rec->fPaint; 1.72 + SkPath fillPath, devPath; 1.73 + const SkPath* p = &path; 1.74 + 1.75 + if (paint.getPathEffect() || paint.getStyle() != SkPaint::kFill_Style) { 1.76 + paint.getFillPath(path, &fillPath); 1.77 + p = &fillPath; 1.78 + } 1.79 + if (p->isEmpty()) { 1.80 + continue; 1.81 + } 1.82 + 1.83 + // apply the matrix and offset 1.84 + { 1.85 + SkMatrix m = matrix; 1.86 + m.preTranslate(rec->fOffset.fX, rec->fOffset.fY); 1.87 + p->transform(m, &devPath); 1.88 + } 1.89 + 1.90 + SkMask mask; 1.91 + if (!SkDraw::DrawToMask(devPath, clipBounds, paint.getMaskFilter(), 1.92 + &matrix, &mask, 1.93 + SkMask::kJustComputeBounds_CreateMode, 1.94 + SkPaint::kFill_Style)) { 1.95 + return false; 1.96 + } 1.97 + 1.98 + bounds->join(mask.fBounds); 1.99 + } 1.100 + return true; 1.101 +} 1.102 + 1.103 +bool SkLayerRasterizer::onRasterize(const SkPath& path, const SkMatrix& matrix, 1.104 + const SkIRect* clipBounds, 1.105 + SkMask* mask, SkMask::CreateMode mode) const { 1.106 + SkASSERT(fLayers); 1.107 + if (fLayers->empty()) { 1.108 + return false; 1.109 + } 1.110 + 1.111 + if (SkMask::kJustRenderImage_CreateMode != mode) { 1.112 + if (!compute_bounds(*fLayers, path, matrix, clipBounds, &mask->fBounds)) 1.113 + return false; 1.114 + } 1.115 + 1.116 + if (SkMask::kComputeBoundsAndRenderImage_CreateMode == mode) { 1.117 + mask->fFormat = SkMask::kA8_Format; 1.118 + mask->fRowBytes = mask->fBounds.width(); 1.119 + size_t size = mask->computeImageSize(); 1.120 + if (0 == size) { 1.121 + return false; // too big to allocate, abort 1.122 + } 1.123 + mask->fImage = SkMask::AllocImage(size); 1.124 + memset(mask->fImage, 0, size); 1.125 + } 1.126 + 1.127 + if (SkMask::kJustComputeBounds_CreateMode != mode) { 1.128 + SkBitmap device; 1.129 + SkRasterClip rectClip; 1.130 + SkDraw draw; 1.131 + SkMatrix translatedMatrix; // this translates us to our local pixels 1.132 + SkMatrix drawMatrix; // this translates the path by each layer's offset 1.133 + 1.134 + rectClip.setRect(SkIRect::MakeWH(mask->fBounds.width(), mask->fBounds.height())); 1.135 + 1.136 + translatedMatrix = matrix; 1.137 + translatedMatrix.postTranslate(-SkIntToScalar(mask->fBounds.fLeft), 1.138 + -SkIntToScalar(mask->fBounds.fTop)); 1.139 + 1.140 + device.installMaskPixels(*mask); 1.141 + 1.142 + draw.fBitmap = &device; 1.143 + draw.fMatrix = &drawMatrix; 1.144 + draw.fRC = &rectClip; 1.145 + draw.fClip = &rectClip.bwRgn(); 1.146 + // we set the matrixproc in the loop, as the matrix changes each time (potentially) 1.147 + draw.fBounder = NULL; 1.148 + 1.149 + SkDeque::F2BIter iter(*fLayers); 1.150 + SkLayerRasterizer_Rec* rec; 1.151 + 1.152 + while ((rec = (SkLayerRasterizer_Rec*)iter.next()) != NULL) { 1.153 + drawMatrix = translatedMatrix; 1.154 + drawMatrix.preTranslate(rec->fOffset.fX, rec->fOffset.fY); 1.155 + draw.drawPath(path, rec->fPaint); 1.156 + } 1.157 + } 1.158 + return true; 1.159 +} 1.160 + 1.161 +SkLayerRasterizer::SkLayerRasterizer(SkReadBuffer& buffer) 1.162 + : SkRasterizer(buffer), fLayers(ReadLayers(buffer)) {} 1.163 + 1.164 +SkDeque* SkLayerRasterizer::ReadLayers(SkReadBuffer& buffer) { 1.165 + int count = buffer.readInt(); 1.166 + 1.167 + SkDeque* layers = SkNEW_ARGS(SkDeque, (sizeof(SkLayerRasterizer_Rec))); 1.168 + for (int i = 0; i < count; i++) { 1.169 + SkLayerRasterizer_Rec* rec = (SkLayerRasterizer_Rec*)layers->push_back(); 1.170 + 1.171 + SkNEW_PLACEMENT(&rec->fPaint, SkPaint); 1.172 + buffer.readPaint(&rec->fPaint); 1.173 + buffer.readPoint(&rec->fOffset); 1.174 + } 1.175 + return layers; 1.176 +} 1.177 + 1.178 +void SkLayerRasterizer::flatten(SkWriteBuffer& buffer) const { 1.179 + this->INHERITED::flatten(buffer); 1.180 + 1.181 + SkASSERT(fLayers); 1.182 + buffer.writeInt(fLayers->count()); 1.183 + 1.184 + SkDeque::F2BIter iter(*fLayers); 1.185 + const SkLayerRasterizer_Rec* rec; 1.186 + 1.187 + while ((rec = (const SkLayerRasterizer_Rec*)iter.next()) != NULL) { 1.188 + buffer.writePaint(rec->fPaint); 1.189 + buffer.writePoint(rec->fOffset); 1.190 + } 1.191 +} 1.192 + 1.193 +SkLayerRasterizer::Builder::Builder() 1.194 + : fLayers(SkNEW_ARGS(SkDeque, (sizeof(SkLayerRasterizer_Rec)))) 1.195 +{ 1.196 +} 1.197 + 1.198 +SkLayerRasterizer::Builder::~Builder() 1.199 +{ 1.200 + SkDELETE(fLayers); 1.201 +} 1.202 + 1.203 +void SkLayerRasterizer::Builder::addLayer(const SkPaint& paint, SkScalar dx, 1.204 + SkScalar dy) { 1.205 + SkASSERT(fLayers); 1.206 + SkLayerRasterizer_Rec* rec = (SkLayerRasterizer_Rec*)fLayers->push_back(); 1.207 + 1.208 + SkNEW_PLACEMENT_ARGS(&rec->fPaint, SkPaint, (paint)); 1.209 + rec->fOffset.set(dx, dy); 1.210 +} 1.211 + 1.212 +SkLayerRasterizer* SkLayerRasterizer::Builder::detachRasterizer() { 1.213 + SkLayerRasterizer* rasterizer = SkNEW_ARGS(SkLayerRasterizer, (fLayers)); 1.214 + fLayers = NULL; 1.215 + return rasterizer; 1.216 +}