2011-07-28 14:26:00 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* 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 "SkCanvas.h"
|
2011-04-12 18:32:06 +00:00
|
|
|
#include "SkColor.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkLayerDrawLooper.h"
|
|
|
|
#include "SkPaint.h"
|
2011-04-12 18:32:06 +00:00
|
|
|
#include "SkUnPreMultiply.h"
|
|
|
|
|
|
|
|
SkLayerDrawLooper::LayerInfo::LayerInfo() {
|
2011-04-14 01:22:45 +00:00
|
|
|
fFlagsMask = 0; // ignore our paint flags
|
|
|
|
fPaintBits = 0; // ignore our paint fields
|
2011-04-12 18:32:06 +00:00
|
|
|
fColorMode = SkXfermode::kDst_Mode; // ignore our color
|
|
|
|
fOffset.set(0, 0);
|
|
|
|
fPostTranslate = false;
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2011-06-22 05:01:28 +00:00
|
|
|
SkLayerDrawLooper::SkLayerDrawLooper()
|
|
|
|
: fRecs(NULL),
|
|
|
|
fCount(0),
|
|
|
|
fCurrRec(NULL) {
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkLayerDrawLooper::~SkLayerDrawLooper() {
|
|
|
|
Rec* rec = fRecs;
|
|
|
|
while (rec) {
|
|
|
|
Rec* next = rec->fNext;
|
|
|
|
SkDELETE(rec);
|
|
|
|
rec = next;
|
|
|
|
}
|
|
|
|
}
|
2011-04-12 18:32:06 +00:00
|
|
|
|
|
|
|
SkPaint* SkLayerDrawLooper::addLayer(const LayerInfo& info) {
|
2008-12-17 15:59:43 +00:00
|
|
|
fCount += 1;
|
|
|
|
|
|
|
|
Rec* rec = SkNEW(Rec);
|
|
|
|
rec->fNext = fRecs;
|
2011-04-12 18:32:06 +00:00
|
|
|
rec->fInfo = info;
|
2008-12-17 15:59:43 +00:00
|
|
|
fRecs = rec;
|
|
|
|
|
|
|
|
return &rec->fPaint;
|
|
|
|
}
|
|
|
|
|
2011-04-14 01:22:45 +00:00
|
|
|
void SkLayerDrawLooper::addLayer(SkScalar dx, SkScalar dy) {
|
2011-04-12 18:32:06 +00:00
|
|
|
LayerInfo info;
|
|
|
|
|
|
|
|
info.fOffset.set(dx, dy);
|
2011-04-14 01:22:45 +00:00
|
|
|
(void)this->addLayer(info);
|
2011-04-12 18:32:06 +00:00
|
|
|
}
|
|
|
|
|
2011-04-07 14:18:59 +00:00
|
|
|
void SkLayerDrawLooper::init(SkCanvas* canvas) {
|
|
|
|
fCurrRec = fRecs;
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->save(SkCanvas::kMatrix_SaveFlag);
|
|
|
|
}
|
|
|
|
|
2011-04-12 18:32:06 +00:00
|
|
|
static SkColor xferColor(SkColor src, SkColor dst, SkXfermode::Mode mode) {
|
|
|
|
switch (mode) {
|
|
|
|
case SkXfermode::kSrc_Mode:
|
|
|
|
return src;
|
|
|
|
case SkXfermode::kDst_Mode:
|
|
|
|
return dst;
|
|
|
|
default: {
|
|
|
|
SkPMColor pmS = SkPreMultiplyColor(src);
|
|
|
|
SkPMColor pmD = SkPreMultiplyColor(dst);
|
|
|
|
SkPMColor result = SkXfermode::GetProc(mode)(pmS, pmD);
|
|
|
|
return SkUnPreMultiply::PMColorToColor(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-21 16:42:10 +00:00
|
|
|
// Even with kEntirePaint_Bits, we always ensure that the master paint's
|
|
|
|
// text-encoding is respected, since that controls how we interpret the
|
|
|
|
// text/length parameters of a draw[Pos]Text call.
|
2011-04-14 01:22:45 +00:00
|
|
|
void SkLayerDrawLooper::ApplyInfo(SkPaint* dst, const SkPaint& src,
|
|
|
|
const LayerInfo& info) {
|
|
|
|
|
|
|
|
uint32_t mask = info.fFlagsMask;
|
|
|
|
dst->setFlags((dst->getFlags() & ~mask) | (src.getFlags() & mask));
|
|
|
|
dst->setColor(xferColor(src.getColor(), dst->getColor(), info.fColorMode));
|
|
|
|
|
|
|
|
BitFlags bits = info.fPaintBits;
|
2011-11-21 16:42:10 +00:00
|
|
|
SkPaint::TextEncoding encoding = dst->getTextEncoding();
|
2011-04-12 18:32:06 +00:00
|
|
|
|
2011-04-09 18:54:08 +00:00
|
|
|
if (0 == bits) {
|
|
|
|
return;
|
|
|
|
}
|
2011-04-08 02:41:54 +00:00
|
|
|
if (kEntirePaint_Bits == bits) {
|
2011-04-14 01:22:45 +00:00
|
|
|
// we've already computed these, so save it from the assignment
|
|
|
|
uint32_t f = dst->getFlags();
|
2011-04-12 18:32:06 +00:00
|
|
|
SkColor c = dst->getColor();
|
2011-04-08 02:41:54 +00:00
|
|
|
*dst = src;
|
2011-04-14 01:22:45 +00:00
|
|
|
dst->setFlags(f);
|
2011-04-12 18:32:06 +00:00
|
|
|
dst->setColor(c);
|
2011-11-21 16:42:10 +00:00
|
|
|
dst->setTextEncoding(encoding);
|
2011-04-08 02:41:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bits & kStyle_Bit) {
|
|
|
|
dst->setStyle(src.getStyle());
|
|
|
|
dst->setStrokeWidth(src.getStrokeWidth());
|
|
|
|
dst->setStrokeMiter(src.getStrokeMiter());
|
|
|
|
dst->setStrokeCap(src.getStrokeCap());
|
|
|
|
dst->setStrokeJoin(src.getStrokeJoin());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bits & kTextSkewX_Bit) {
|
|
|
|
dst->setTextSkewX(src.getTextSkewX());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bits & kPathEffect_Bit) {
|
|
|
|
dst->setPathEffect(src.getPathEffect());
|
|
|
|
}
|
|
|
|
if (bits & kMaskFilter_Bit) {
|
|
|
|
dst->setMaskFilter(src.getMaskFilter());
|
|
|
|
}
|
|
|
|
if (bits & kShader_Bit) {
|
|
|
|
dst->setShader(src.getShader());
|
|
|
|
}
|
|
|
|
if (bits & kColorFilter_Bit) {
|
|
|
|
dst->setColorFilter(src.getColorFilter());
|
|
|
|
}
|
|
|
|
if (bits & kXfermode_Bit) {
|
|
|
|
dst->setXfermode(src.getXfermode());
|
|
|
|
}
|
|
|
|
|
2011-11-21 16:42:10 +00:00
|
|
|
// we don't override these
|
2011-04-08 02:41:54 +00:00
|
|
|
#if 0
|
|
|
|
dst->setTypeface(src.getTypeface());
|
|
|
|
dst->setTextSize(src.getTextSize());
|
|
|
|
dst->setTextScaleX(src.getTextScaleX());
|
|
|
|
dst->setRasterizer(src.getRasterizer());
|
|
|
|
dst->setLooper(src.getLooper());
|
|
|
|
dst->setTextEncoding(src.getTextEncoding());
|
|
|
|
dst->setHinting(src.getHinting());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-04-12 18:32:06 +00:00
|
|
|
// Should we add this to canvas?
|
|
|
|
static void postTranslate(SkCanvas* canvas, SkScalar dx, SkScalar dy) {
|
|
|
|
SkMatrix m = canvas->getTotalMatrix();
|
|
|
|
m.postTranslate(dx, dy);
|
|
|
|
canvas->setMatrix(m);
|
|
|
|
}
|
|
|
|
|
2011-04-07 14:18:59 +00:00
|
|
|
bool SkLayerDrawLooper::next(SkCanvas* canvas, SkPaint* paint) {
|
|
|
|
canvas->restore();
|
|
|
|
if (NULL == fCurrRec) {
|
|
|
|
return false;
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2011-04-14 01:22:45 +00:00
|
|
|
ApplyInfo(paint, fCurrRec->fPaint, fCurrRec->fInfo);
|
2011-04-12 18:32:06 +00:00
|
|
|
|
2011-04-07 14:18:59 +00:00
|
|
|
canvas->save(SkCanvas::kMatrix_SaveFlag);
|
2011-04-12 18:32:06 +00:00
|
|
|
if (fCurrRec->fInfo.fPostTranslate) {
|
|
|
|
postTranslate(canvas, fCurrRec->fInfo.fOffset.fX,
|
|
|
|
fCurrRec->fInfo.fOffset.fY);
|
|
|
|
} else {
|
|
|
|
canvas->translate(fCurrRec->fInfo.fOffset.fX, fCurrRec->fInfo.fOffset.fY);
|
|
|
|
}
|
2011-04-08 00:53:48 +00:00
|
|
|
fCurrRec = fCurrRec->fNext;
|
|
|
|
|
2011-04-07 14:18:59 +00:00
|
|
|
return true;
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkLayerDrawLooper::Rec* SkLayerDrawLooper::Rec::Reverse(Rec* head) {
|
|
|
|
Rec* rec = head;
|
|
|
|
Rec* prev = NULL;
|
|
|
|
while (rec) {
|
|
|
|
Rec* next = rec->fNext;
|
|
|
|
rec->fNext = prev;
|
|
|
|
prev = rec;
|
|
|
|
rec = next;
|
|
|
|
}
|
|
|
|
return prev;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void SkLayerDrawLooper::flatten(SkFlattenableWriteBuffer& buffer) {
|
|
|
|
this->INHERITED::flatten(buffer);
|
|
|
|
|
|
|
|
#ifdef SK_DEBUG
|
|
|
|
{
|
|
|
|
Rec* rec = fRecs;
|
|
|
|
int count = 0;
|
|
|
|
while (rec) {
|
|
|
|
rec = rec->fNext;
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
SkASSERT(count == fCount);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
buffer.writeInt(fCount);
|
|
|
|
|
|
|
|
Rec* rec = fRecs;
|
|
|
|
for (int i = 0; i < fCount; i++) {
|
2011-04-12 18:32:06 +00:00
|
|
|
buffer.writeInt(rec->fInfo.fPaintBits);
|
|
|
|
buffer.writeInt(rec->fInfo.fColorMode);
|
|
|
|
buffer.writeScalar(rec->fInfo.fOffset.fX);
|
|
|
|
buffer.writeScalar(rec->fInfo.fOffset.fY);
|
|
|
|
buffer.writeBool(rec->fInfo.fPostTranslate);
|
2008-12-17 15:59:43 +00:00
|
|
|
rec->fPaint.flatten(buffer);
|
|
|
|
rec = rec->fNext;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SkLayerDrawLooper::SkLayerDrawLooper(SkFlattenableReadBuffer& buffer)
|
2011-06-22 05:01:28 +00:00
|
|
|
: INHERITED(buffer),
|
|
|
|
fRecs(NULL),
|
|
|
|
fCount(0),
|
|
|
|
fCurrRec(NULL) {
|
2008-12-17 15:59:43 +00:00
|
|
|
int count = buffer.readInt();
|
|
|
|
|
|
|
|
for (int i = 0; i < count; i++) {
|
2011-04-12 18:32:06 +00:00
|
|
|
LayerInfo info;
|
|
|
|
info.fPaintBits = buffer.readInt();
|
|
|
|
info.fColorMode = (SkXfermode::Mode)buffer.readInt();
|
|
|
|
info.fOffset.fX = buffer.readScalar();
|
|
|
|
info.fOffset.fY = buffer.readScalar();
|
|
|
|
info.fPostTranslate = buffer.readBool();
|
|
|
|
this->addLayer(info)->unflatten(buffer);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
SkASSERT(count == fCount);
|
|
|
|
|
|
|
|
// we're in reverse order, so fix it now
|
|
|
|
fRecs = Rec::Reverse(fRecs);
|
|
|
|
|
|
|
|
#ifdef SK_DEBUG
|
|
|
|
{
|
|
|
|
Rec* rec = fRecs;
|
|
|
|
int n = 0;
|
|
|
|
while (rec) {
|
|
|
|
rec = rec->fNext;
|
|
|
|
n += 1;
|
|
|
|
}
|
|
|
|
SkASSERT(count == n);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2011-12-15 14:16:43 +00:00
|
|
|
SK_DEFINE_FLATTENABLE_REGISTRAR(SkLayerDrawLooper)
|