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"
|
2012-08-07 15:54:32 +00:00
|
|
|
#include "SkFlattenableBuffers.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkLayerDrawLooper.h"
|
2013-01-28 20:21:59 +00:00
|
|
|
#include "SkString.h"
|
|
|
|
#include "SkStringUtils.h"
|
2011-04-12 18:32:06 +00:00
|
|
|
#include "SkUnPreMultiply.h"
|
|
|
|
|
2012-06-27 14:03:26 +00:00
|
|
|
SK_DEFINE_INST_COUNT(SkLayerDrawLooper)
|
|
|
|
|
2011-04-12 18:32:06 +00:00
|
|
|
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),
|
2013-05-22 12:35:50 +00:00
|
|
|
fTopRec(NULL),
|
2011-06-22 05:01:28 +00:00
|
|
|
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;
|
2013-05-22 12:35:50 +00:00
|
|
|
if (NULL == fTopRec) {
|
|
|
|
fTopRec = rec;
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-05-22 12:35:50 +00:00
|
|
|
SkPaint* SkLayerDrawLooper::addLayerOnTop(const LayerInfo& info) {
|
|
|
|
fCount += 1;
|
|
|
|
|
|
|
|
Rec* rec = SkNEW(Rec);
|
|
|
|
rec->fNext = NULL;
|
|
|
|
rec->fInfo = info;
|
|
|
|
if (NULL == fRecs) {
|
|
|
|
fRecs = rec;
|
|
|
|
} else {
|
|
|
|
SkASSERT(NULL != fTopRec);
|
|
|
|
fTopRec->fNext = rec;
|
|
|
|
}
|
|
|
|
fTopRec = rec;
|
|
|
|
|
|
|
|
return &rec->fPaint;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-03-29 15:18:04 +00:00
|
|
|
void SkLayerDrawLooper::flatten(SkFlattenableWriteBuffer& buffer) const {
|
2008-12-17 15:59:43 +00:00
|
|
|
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);
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
Rec* rec = fRecs;
|
|
|
|
for (int i = 0; i < fCount; i++) {
|
2012-07-31 14:32:38 +00:00
|
|
|
buffer.writeInt(rec->fInfo.fFlagsMask);
|
2011-04-12 18:32:06 +00:00
|
|
|
buffer.writeInt(rec->fInfo.fPaintBits);
|
|
|
|
buffer.writeInt(rec->fInfo.fColorMode);
|
2012-08-07 15:54:32 +00:00
|
|
|
buffer.writePoint(rec->fInfo.fOffset);
|
2011-04-12 18:32:06 +00:00
|
|
|
buffer.writeBool(rec->fInfo.fPostTranslate);
|
2012-08-07 15:54:32 +00:00
|
|
|
buffer.writePaint(rec->fPaint);
|
2008-12-17 15:59:43 +00:00
|
|
|
rec = rec->fNext;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SkLayerDrawLooper::SkLayerDrawLooper(SkFlattenableReadBuffer& buffer)
|
2011-06-22 05:01:28 +00:00
|
|
|
: INHERITED(buffer),
|
|
|
|
fRecs(NULL),
|
2013-05-22 12:35:50 +00:00
|
|
|
fTopRec(NULL),
|
2011-06-22 05:01:28 +00:00
|
|
|
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;
|
2012-07-31 14:32:38 +00:00
|
|
|
info.fFlagsMask = buffer.readInt();
|
2011-04-12 18:32:06 +00:00
|
|
|
info.fPaintBits = buffer.readInt();
|
|
|
|
info.fColorMode = (SkXfermode::Mode)buffer.readInt();
|
2012-08-07 15:54:32 +00:00
|
|
|
buffer.readPoint(&info.fOffset);
|
2011-04-12 18:32:06 +00:00
|
|
|
info.fPostTranslate = buffer.readBool();
|
2013-05-22 12:35:50 +00:00
|
|
|
buffer.readPaint(this->addLayerOnTop(info));
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
SkASSERT(count == fCount);
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#ifdef SK_DEBUG
|
|
|
|
{
|
|
|
|
Rec* rec = fRecs;
|
|
|
|
int n = 0;
|
|
|
|
while (rec) {
|
|
|
|
rec = rec->fNext;
|
|
|
|
n += 1;
|
|
|
|
}
|
|
|
|
SkASSERT(count == n);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2013-01-28 20:21:59 +00:00
|
|
|
|
|
|
|
#ifdef SK_DEVELOPER
|
|
|
|
void SkLayerDrawLooper::toString(SkString* str) const {
|
|
|
|
str->appendf("SkLayerDrawLooper (%d): ", fCount);
|
|
|
|
|
|
|
|
Rec* rec = fRecs;
|
|
|
|
for (int i = 0; i < fCount; i++) {
|
|
|
|
str->appendf("%d: ", i);
|
|
|
|
|
|
|
|
str->append("flagsMask: (");
|
|
|
|
if (0 == rec->fInfo.fFlagsMask) {
|
|
|
|
str->append("None");
|
|
|
|
} else {
|
|
|
|
bool needSeparator = false;
|
2013-01-29 07:05:52 +00:00
|
|
|
SkAddFlagToString(str, SkToBool(SkPaint::kAntiAlias_Flag & rec->fInfo.fFlagsMask),
|
2013-01-28 20:21:59 +00:00
|
|
|
"AntiAlias", &needSeparator);
|
2013-01-29 07:05:52 +00:00
|
|
|
SkAddFlagToString(str, SkToBool(SkPaint::kFilterBitmap_Flag & rec->fInfo.fFlagsMask),
|
2013-01-28 20:21:59 +00:00
|
|
|
"FilterBitmap", &needSeparator);
|
2013-01-29 07:05:52 +00:00
|
|
|
SkAddFlagToString(str, SkToBool(SkPaint::kDither_Flag & rec->fInfo.fFlagsMask),
|
2013-01-28 20:21:59 +00:00
|
|
|
"Dither", &needSeparator);
|
2013-01-29 07:05:52 +00:00
|
|
|
SkAddFlagToString(str, SkToBool(SkPaint::kUnderlineText_Flag & rec->fInfo.fFlagsMask),
|
2013-01-28 20:21:59 +00:00
|
|
|
"UnderlineText", &needSeparator);
|
2013-01-29 07:05:52 +00:00
|
|
|
SkAddFlagToString(str, SkToBool(SkPaint::kStrikeThruText_Flag & rec->fInfo.fFlagsMask),
|
2013-01-28 20:21:59 +00:00
|
|
|
"StrikeThruText", &needSeparator);
|
2013-01-29 07:05:52 +00:00
|
|
|
SkAddFlagToString(str, SkToBool(SkPaint::kFakeBoldText_Flag & rec->fInfo.fFlagsMask),
|
2013-01-28 20:21:59 +00:00
|
|
|
"FakeBoldText", &needSeparator);
|
2013-01-29 07:05:52 +00:00
|
|
|
SkAddFlagToString(str, SkToBool(SkPaint::kLinearText_Flag & rec->fInfo.fFlagsMask),
|
2013-01-28 20:21:59 +00:00
|
|
|
"LinearText", &needSeparator);
|
2013-01-29 07:05:52 +00:00
|
|
|
SkAddFlagToString(str, SkToBool(SkPaint::kSubpixelText_Flag & rec->fInfo.fFlagsMask),
|
2013-01-28 20:21:59 +00:00
|
|
|
"SubpixelText", &needSeparator);
|
2013-01-29 07:05:52 +00:00
|
|
|
SkAddFlagToString(str, SkToBool(SkPaint::kDevKernText_Flag & rec->fInfo.fFlagsMask),
|
2013-01-28 20:21:59 +00:00
|
|
|
"DevKernText", &needSeparator);
|
2013-01-29 07:05:52 +00:00
|
|
|
SkAddFlagToString(str, SkToBool(SkPaint::kLCDRenderText_Flag & rec->fInfo.fFlagsMask),
|
2013-01-28 20:21:59 +00:00
|
|
|
"LCDRenderText", &needSeparator);
|
|
|
|
SkAddFlagToString(str, SkToBool(SkPaint::kEmbeddedBitmapText_Flag & rec->fInfo.fFlagsMask),
|
|
|
|
"EmbeddedBitmapText", &needSeparator);
|
2013-01-29 07:05:52 +00:00
|
|
|
SkAddFlagToString(str, SkToBool(SkPaint::kAutoHinting_Flag & rec->fInfo.fFlagsMask),
|
2013-01-28 20:21:59 +00:00
|
|
|
"Autohinted", &needSeparator);
|
2013-01-29 07:05:52 +00:00
|
|
|
SkAddFlagToString(str, SkToBool(SkPaint::kVerticalText_Flag & rec->fInfo.fFlagsMask),
|
2013-01-28 20:21:59 +00:00
|
|
|
"VerticalText", &needSeparator);
|
|
|
|
SkAddFlagToString(str, SkToBool(SkPaint::kGenA8FromLCD_Flag & rec->fInfo.fFlagsMask),
|
|
|
|
"GenA8FromLCD", &needSeparator);
|
|
|
|
}
|
|
|
|
str->append(") ");
|
|
|
|
|
|
|
|
str->append("paintBits: (");
|
|
|
|
if (0 == rec->fInfo.fPaintBits) {
|
|
|
|
str->append("None");
|
|
|
|
} else if (kEntirePaint_Bits == rec->fInfo.fPaintBits) {
|
|
|
|
str->append("EntirePaint");
|
|
|
|
} else {
|
|
|
|
bool needSeparator = false;
|
|
|
|
SkAddFlagToString(str, SkToBool(kStyle_Bit & rec->fInfo.fPaintBits), "Style",
|
|
|
|
&needSeparator);
|
2013-01-29 07:05:52 +00:00
|
|
|
SkAddFlagToString(str, SkToBool(kTextSkewX_Bit & rec->fInfo.fPaintBits), "TextSkewX",
|
2013-01-28 20:21:59 +00:00
|
|
|
&needSeparator);
|
|
|
|
SkAddFlagToString(str, SkToBool(kPathEffect_Bit & rec->fInfo.fPaintBits), "PathEffect",
|
|
|
|
&needSeparator);
|
|
|
|
SkAddFlagToString(str, SkToBool(kMaskFilter_Bit & rec->fInfo.fPaintBits), "MaskFilter",
|
|
|
|
&needSeparator);
|
2013-01-29 07:05:52 +00:00
|
|
|
SkAddFlagToString(str, SkToBool(kShader_Bit & rec->fInfo.fPaintBits), "Shader",
|
2013-01-28 20:21:59 +00:00
|
|
|
&needSeparator);
|
2013-01-29 07:05:52 +00:00
|
|
|
SkAddFlagToString(str, SkToBool(kColorFilter_Bit & rec->fInfo.fPaintBits), "ColorFilter",
|
2013-01-28 20:21:59 +00:00
|
|
|
&needSeparator);
|
2013-01-29 07:05:52 +00:00
|
|
|
SkAddFlagToString(str, SkToBool(kXfermode_Bit & rec->fInfo.fPaintBits), "Xfermode",
|
2013-01-28 20:21:59 +00:00
|
|
|
&needSeparator);
|
|
|
|
}
|
|
|
|
str->append(") ");
|
|
|
|
|
|
|
|
static const char* gModeStrings[SkXfermode::kLastMode+1] = {
|
|
|
|
"kClear", "kSrc", "kDst", "kSrcOver", "kDstOver", "kSrcIn", "kDstIn",
|
|
|
|
"kSrcOut", "kDstOut", "kSrcATop", "kDstATop", "kXor", "kPlus",
|
|
|
|
"kMultiply", "kScreen", "kOverlay", "kDarken", "kLighten", "kColorDodge",
|
|
|
|
"kColorBurn", "kHardLight", "kSoftLight", "kDifference", "kExclusion"
|
|
|
|
};
|
|
|
|
|
|
|
|
str->appendf("mode: %s ", gModeStrings[rec->fInfo.fColorMode]);
|
|
|
|
|
|
|
|
str->append("offset: (");
|
|
|
|
str->appendScalar(rec->fInfo.fOffset.fX);
|
|
|
|
str->append(", ");
|
|
|
|
str->appendScalar(rec->fInfo.fOffset.fY);
|
|
|
|
str->append(") ");
|
|
|
|
|
|
|
|
str->append("postTranslate: ");
|
|
|
|
if (rec->fInfo.fPostTranslate) {
|
|
|
|
str->append("true ");
|
|
|
|
} else {
|
|
|
|
str->append("false ");
|
|
|
|
}
|
|
|
|
|
2013-02-14 13:53:53 +00:00
|
|
|
rec->fPaint.toString(str);
|
2013-01-28 20:21:59 +00:00
|
|
|
rec = rec->fNext;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|