2011-07-28 14:26:00 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2006 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
#include "SkLayerRasterizer.h"
|
|
|
|
#include "SkBuffer.h"
|
|
|
|
#include "SkDraw.h"
|
|
|
|
#include "SkMask.h"
|
|
|
|
#include "SkMaskFilter.h"
|
|
|
|
#include "SkPaint.h"
|
|
|
|
#include "SkPath.h"
|
|
|
|
#include "SkRegion.h"
|
|
|
|
#include "SkXfermode.h"
|
|
|
|
#include <new>
|
|
|
|
|
|
|
|
struct SkLayerRasterizer_Rec {
|
|
|
|
SkPaint fPaint;
|
|
|
|
SkVector fOffset;
|
|
|
|
};
|
|
|
|
|
|
|
|
SkLayerRasterizer::SkLayerRasterizer() : fLayers(sizeof(SkLayerRasterizer_Rec))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-04-20 11:39:28 +00:00
|
|
|
SkLayerRasterizer::~SkLayerRasterizer() {
|
2011-02-22 13:16:38 +00:00
|
|
|
SkDeque::F2BIter iter(fLayers);
|
2008-12-17 15:59:43 +00:00
|
|
|
SkLayerRasterizer_Rec* rec;
|
|
|
|
|
|
|
|
while ((rec = (SkLayerRasterizer_Rec*)iter.next()) != NULL)
|
|
|
|
rec->fPaint.~SkPaint();
|
|
|
|
}
|
|
|
|
|
2011-04-20 11:39:28 +00:00
|
|
|
void SkLayerRasterizer::addLayer(const SkPaint& paint, SkScalar dx,
|
|
|
|
SkScalar dy) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkLayerRasterizer_Rec* rec = (SkLayerRasterizer_Rec*)fLayers.push_back();
|
|
|
|
|
|
|
|
new (&rec->fPaint) SkPaint(paint);
|
|
|
|
rec->fOffset.set(dx, dy);
|
|
|
|
}
|
|
|
|
|
2011-04-20 11:39:28 +00:00
|
|
|
static bool compute_bounds(const SkDeque& layers, const SkPath& path,
|
|
|
|
const SkMatrix& matrix,
|
|
|
|
const SkIRect* clipBounds, SkIRect* bounds) {
|
2011-02-22 13:16:38 +00:00
|
|
|
SkDeque::F2BIter iter(layers);
|
2008-12-17 15:59:43 +00:00
|
|
|
SkLayerRasterizer_Rec* rec;
|
|
|
|
|
|
|
|
bounds->set(SK_MaxS32, SK_MaxS32, SK_MinS32, SK_MinS32);
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2011-04-20 11:39:28 +00:00
|
|
|
while ((rec = (SkLayerRasterizer_Rec*)iter.next()) != NULL) {
|
2008-12-17 15:59:43 +00:00
|
|
|
const SkPaint& paint = rec->fPaint;
|
|
|
|
SkPath fillPath, devPath;
|
|
|
|
const SkPath* p = &path;
|
|
|
|
|
2011-04-20 11:39:28 +00:00
|
|
|
if (paint.getPathEffect() || paint.getStyle() != SkPaint::kFill_Style) {
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.getFillPath(path, &fillPath);
|
|
|
|
p = &fillPath;
|
|
|
|
}
|
2011-04-20 11:39:28 +00:00
|
|
|
if (p->isEmpty()) {
|
2008-12-17 15:59:43 +00:00
|
|
|
continue;
|
2011-04-20 11:39:28 +00:00
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
// apply the matrix and offset
|
|
|
|
{
|
|
|
|
SkMatrix m = matrix;
|
|
|
|
m.preTranslate(rec->fOffset.fX, rec->fOffset.fY);
|
|
|
|
p->transform(m, &devPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
SkMask mask;
|
2011-04-20 11:39:28 +00:00
|
|
|
if (!SkDraw::DrawToMask(devPath, clipBounds, paint.getMaskFilter(),
|
|
|
|
&matrix, &mask,
|
|
|
|
SkMask::kJustComputeBounds_CreateMode)) {
|
2008-12-17 15:59:43 +00:00
|
|
|
return false;
|
2011-04-20 11:39:28 +00:00
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
bounds->join(mask.fBounds);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SkLayerRasterizer::onRasterize(const SkPath& path, const SkMatrix& matrix,
|
|
|
|
const SkIRect* clipBounds,
|
2011-04-20 11:39:28 +00:00
|
|
|
SkMask* mask, SkMask::CreateMode mode) {
|
|
|
|
if (fLayers.empty()) {
|
2008-12-17 15:59:43 +00:00
|
|
|
return false;
|
2011-04-20 11:39:28 +00:00
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2011-04-20 11:39:28 +00:00
|
|
|
if (SkMask::kJustRenderImage_CreateMode != mode) {
|
2008-12-17 15:59:43 +00:00
|
|
|
if (!compute_bounds(fLayers, path, matrix, clipBounds, &mask->fBounds))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-04-20 11:39:28 +00:00
|
|
|
if (SkMask::kComputeBoundsAndRenderImage_CreateMode == mode) {
|
2008-12-17 15:59:43 +00:00
|
|
|
mask->fFormat = SkMask::kA8_Format;
|
2009-03-19 21:52:42 +00:00
|
|
|
mask->fRowBytes = mask->fBounds.width();
|
2009-04-24 12:43:40 +00:00
|
|
|
size_t size = mask->computeImageSize();
|
|
|
|
if (0 == size) {
|
|
|
|
return false; // too big to allocate, abort
|
|
|
|
}
|
|
|
|
mask->fImage = SkMask::AllocImage(size);
|
|
|
|
memset(mask->fImage, 0, size);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2011-04-20 11:39:28 +00:00
|
|
|
if (SkMask::kJustComputeBounds_CreateMode != mode) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkBitmap device;
|
|
|
|
SkDraw draw;
|
|
|
|
SkMatrix translatedMatrix; // this translates us to our local pixels
|
|
|
|
SkMatrix drawMatrix; // this translates the path by each layer's offset
|
|
|
|
SkRegion rectClip;
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
rectClip.setRect(0, 0, mask->fBounds.width(), mask->fBounds.height());
|
|
|
|
|
|
|
|
translatedMatrix = matrix;
|
|
|
|
translatedMatrix.postTranslate(-SkIntToScalar(mask->fBounds.fLeft),
|
|
|
|
-SkIntToScalar(mask->fBounds.fTop));
|
|
|
|
|
|
|
|
device.setConfig(SkBitmap::kA8_Config, mask->fBounds.width(), mask->fBounds.height(), mask->fRowBytes);
|
|
|
|
device.setPixels(mask->fImage);
|
|
|
|
|
|
|
|
draw.fBitmap = &device;
|
|
|
|
draw.fMatrix = &drawMatrix;
|
|
|
|
draw.fClip = &rectClip;
|
|
|
|
// we set the matrixproc in the loop, as the matrix changes each time (potentially)
|
|
|
|
draw.fBounder = NULL;
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2011-02-22 13:16:38 +00:00
|
|
|
SkDeque::F2BIter iter(fLayers);
|
2008-12-17 15:59:43 +00:00
|
|
|
SkLayerRasterizer_Rec* rec;
|
|
|
|
|
|
|
|
while ((rec = (SkLayerRasterizer_Rec*)iter.next()) != NULL) {
|
|
|
|
drawMatrix = translatedMatrix;
|
|
|
|
drawMatrix.preTranslate(rec->fOffset.fX, rec->fOffset.fY);
|
|
|
|
draw.drawPath(path, rec->fPaint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////// Routines for flattening /////////////////
|
|
|
|
|
2011-04-20 11:39:28 +00:00
|
|
|
static void paint_read(SkPaint* paint, SkFlattenableReadBuffer& buffer) {
|
2008-12-17 15:59:43 +00:00
|
|
|
paint->setAntiAlias(buffer.readBool());
|
|
|
|
paint->setStyle((SkPaint::Style)buffer.readU8());
|
|
|
|
paint->setAlpha(buffer.readU8());
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2011-04-20 11:39:28 +00:00
|
|
|
if (paint->getStyle() != SkPaint::kFill_Style) {
|
2008-12-17 15:59:43 +00:00
|
|
|
paint->setStrokeWidth(buffer.readScalar());
|
|
|
|
paint->setStrokeMiter(buffer.readScalar());
|
|
|
|
paint->setStrokeCap((SkPaint::Cap)buffer.readU8());
|
|
|
|
paint->setStrokeJoin((SkPaint::Join)buffer.readU8());
|
|
|
|
}
|
|
|
|
|
2011-02-07 15:30:46 +00:00
|
|
|
SkSafeUnref(paint->setMaskFilter((SkMaskFilter*)buffer.readFlattenable()));
|
|
|
|
SkSafeUnref(paint->setPathEffect((SkPathEffect*)buffer.readFlattenable()));
|
|
|
|
SkSafeUnref(paint->setRasterizer((SkRasterizer*)buffer.readFlattenable()));
|
|
|
|
SkSafeUnref(paint->setXfermode((SkXfermode*)buffer.readFlattenable()));
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2011-04-20 11:39:28 +00:00
|
|
|
static void paint_write(const SkPaint& paint, SkFlattenableWriteBuffer& buffer) {
|
2008-12-17 15:59:43 +00:00
|
|
|
buffer.writeBool(paint.isAntiAlias());
|
|
|
|
buffer.write8(paint.getStyle());
|
|
|
|
buffer.write8(paint.getAlpha());
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2011-04-20 11:39:28 +00:00
|
|
|
if (paint.getStyle() != SkPaint::kFill_Style) {
|
2008-12-17 15:59:43 +00:00
|
|
|
buffer.writeScalar(paint.getStrokeWidth());
|
|
|
|
buffer.writeScalar(paint.getStrokeMiter());
|
|
|
|
buffer.write8(paint.getStrokeCap());
|
|
|
|
buffer.write8(paint.getStrokeJoin());
|
|
|
|
}
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
buffer.writeFlattenable(paint.getMaskFilter());
|
|
|
|
buffer.writeFlattenable(paint.getPathEffect());
|
|
|
|
buffer.writeFlattenable(paint.getRasterizer());
|
|
|
|
buffer.writeFlattenable(paint.getXfermode());
|
|
|
|
}
|
|
|
|
|
|
|
|
SkLayerRasterizer::SkLayerRasterizer(SkFlattenableReadBuffer& buffer)
|
2011-04-20 11:39:28 +00:00
|
|
|
: SkRasterizer(buffer), fLayers(sizeof(SkLayerRasterizer_Rec)) {
|
2008-12-17 15:59:43 +00:00
|
|
|
int count = buffer.readS32();
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2011-04-20 11:39:28 +00:00
|
|
|
for (int i = 0; i < count; i++) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkLayerRasterizer_Rec* rec = (SkLayerRasterizer_Rec*)fLayers.push_back();
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#if 0
|
|
|
|
new (&rec->fPaint) SkPaint(buffer);
|
|
|
|
#else
|
|
|
|
new (&rec->fPaint) SkPaint;
|
|
|
|
paint_read(&rec->fPaint, buffer);
|
|
|
|
#endif
|
|
|
|
rec->fOffset.fX = buffer.readScalar();
|
|
|
|
rec->fOffset.fY = buffer.readScalar();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-20 11:39:28 +00:00
|
|
|
void SkLayerRasterizer::flatten(SkFlattenableWriteBuffer& buffer) {
|
2008-12-17 15:59:43 +00:00
|
|
|
this->INHERITED::flatten(buffer);
|
|
|
|
|
|
|
|
buffer.write32(fLayers.count());
|
|
|
|
|
2011-02-22 13:16:38 +00:00
|
|
|
SkDeque::F2BIter iter(fLayers);
|
2008-12-17 15:59:43 +00:00
|
|
|
const SkLayerRasterizer_Rec* rec;
|
|
|
|
|
2011-04-20 11:39:28 +00:00
|
|
|
while ((rec = (const SkLayerRasterizer_Rec*)iter.next()) != NULL) {
|
2008-12-17 15:59:43 +00:00
|
|
|
#if 0
|
|
|
|
rec->fPaint.flatten(buffer);
|
|
|
|
#else
|
|
|
|
paint_write(rec->fPaint, buffer);
|
|
|
|
#endif
|
|
|
|
buffer.writeScalar(rec->fOffset.fX);
|
|
|
|
buffer.writeScalar(rec->fOffset.fY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-20 11:39:28 +00:00
|
|
|
SkFlattenable* SkLayerRasterizer::CreateProc(SkFlattenableReadBuffer& buffer) {
|
2008-12-17 15:59:43 +00:00
|
|
|
return SkNEW_ARGS(SkLayerRasterizer, (buffer));
|
|
|
|
}
|
|
|
|
|
2011-04-20 11:39:28 +00:00
|
|
|
SkFlattenable::Factory SkLayerRasterizer::getFactory() {
|
2008-12-17 15:59:43 +00:00
|
|
|
return CreateProc;
|
|
|
|
}
|
|
|
|
|