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"
|
2014-01-30 18:58:24 +00:00
|
|
|
#include "SkReadBuffer.h"
|
|
|
|
#include "SkWriteBuffer.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"
|
|
|
|
|
|
|
|
SkLayerDrawLooper::LayerInfo::LayerInfo() {
|
2011-04-14 01:22:45 +00:00
|
|
|
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),
|
2014-03-12 09:42:01 +00:00
|
|
|
fCount(0) {
|
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
|
|
|
|
2014-03-12 09:42:01 +00:00
|
|
|
SkLayerDrawLooper::Context* SkLayerDrawLooper::createContext(SkCanvas* canvas, void* storage) const {
|
2014-04-18 14:19:31 +00:00
|
|
|
canvas->save();
|
2014-03-12 09:42:01 +00:00
|
|
|
return SkNEW_PLACEMENT_ARGS(storage, LayerDrawLooperContext, (this));
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
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.
|
2014-03-12 09:42:01 +00:00
|
|
|
void SkLayerDrawLooper::LayerDrawLooperContext::ApplyInfo(
|
|
|
|
SkPaint* dst, const SkPaint& src, const LayerInfo& info) {
|
2011-04-14 01:22:45 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-03-12 09:42:01 +00:00
|
|
|
SkLayerDrawLooper::LayerDrawLooperContext::LayerDrawLooperContext(
|
|
|
|
const SkLayerDrawLooper* looper) : fCurrRec(looper->fRecs) {}
|
|
|
|
|
|
|
|
bool SkLayerDrawLooper::LayerDrawLooperContext::next(SkCanvas* canvas,
|
|
|
|
SkPaint* paint) {
|
2011-04-07 14:18:59 +00:00
|
|
|
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
|
|
|
|
2014-04-18 14:19:31 +00:00
|
|
|
canvas->save();
|
2011-04-12 18:32:06 +00:00
|
|
|
if (fCurrRec->fInfo.fPostTranslate) {
|
|
|
|
postTranslate(canvas, fCurrRec->fInfo.fOffset.fX,
|
|
|
|
fCurrRec->fInfo.fOffset.fY);
|
|
|
|
} else {
|
2014-03-12 09:42:01 +00:00
|
|
|
canvas->translate(fCurrRec->fInfo.fOffset.fX,
|
|
|
|
fCurrRec->fInfo.fOffset.fY);
|
2011-04-12 18:32:06 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2014-04-29 15:20:16 +00:00
|
|
|
bool SkLayerDrawLooper::asABlurShadow(BlurShadowRec* bsRec) const {
|
|
|
|
if (fCount != 2) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const Rec* rec = fRecs;
|
|
|
|
|
|
|
|
// bottom layer needs to be just blur(maskfilter)
|
|
|
|
if ((rec->fInfo.fPaintBits & ~kMaskFilter_Bit)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (SkXfermode::kSrc_Mode != rec->fInfo.fColorMode) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const SkMaskFilter* mf = rec->fPaint.getMaskFilter();
|
|
|
|
if (NULL == mf) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
SkMaskFilter::BlurRec maskBlur;
|
|
|
|
if (!mf->asABlur(&maskBlur)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
rec = rec->fNext;
|
|
|
|
// top layer needs to be "plain"
|
|
|
|
if (rec->fInfo.fPaintBits) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (SkXfermode::kDst_Mode != rec->fInfo.fColorMode) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!rec->fInfo.fOffset.equals(0, 0)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bsRec) {
|
|
|
|
bsRec->fSigma = maskBlur.fSigma;
|
|
|
|
bsRec->fOffset = fRecs->fInfo.fOffset;
|
|
|
|
bsRec->fColor = fRecs->fPaint.getColor();
|
|
|
|
bsRec->fStyle = maskBlur.fStyle;
|
|
|
|
bsRec->fQuality = maskBlur.fQuality;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-01-30 18:58:24 +00:00
|
|
|
void SkLayerDrawLooper::flatten(SkWriteBuffer& buffer) const {
|
2008-12-17 15:59:43 +00:00
|
|
|
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++) {
|
2014-02-28 15:38:22 +00:00
|
|
|
// Legacy "flagsmask" field -- now ignored, remove when we bump version
|
|
|
|
buffer.writeInt(0);
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-14 10:06:42 +00:00
|
|
|
SkFlattenable* SkLayerDrawLooper::CreateProc(SkReadBuffer& buffer) {
|
2008-12-17 15:59:43 +00:00
|
|
|
int count = buffer.readInt();
|
|
|
|
|
2014-02-14 10:06:42 +00:00
|
|
|
Builder builder;
|
2008-12-17 15:59:43 +00:00
|
|
|
for (int i = 0; i < count; i++) {
|
2011-04-12 18:32:06 +00:00
|
|
|
LayerInfo info;
|
2014-02-28 15:38:22 +00:00
|
|
|
// Legacy "flagsmask" field -- now ignored, remove when we bump version
|
2014-02-27 14:27:44 +00:00
|
|
|
(void)buffer.readInt();
|
2014-02-28 15:38:22 +00:00
|
|
|
|
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();
|
2014-02-14 10:06:42 +00:00
|
|
|
buffer.readPaint(builder.addLayerOnTop(info));
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2014-08-21 14:59:51 +00:00
|
|
|
return builder.detachLooper();
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2013-01-28 20:21:59 +00:00
|
|
|
|
2014-03-13 18:02:17 +00:00
|
|
|
#ifndef SK_IGNORE_TO_STRING
|
2013-01-28 20:21:59 +00:00
|
|
|
void SkLayerDrawLooper::toString(SkString* str) const {
|
|
|
|
str->appendf("SkLayerDrawLooper (%d): ", fCount);
|
|
|
|
|
|
|
|
Rec* rec = fRecs;
|
|
|
|
for (int i = 0; i < fCount; i++) {
|
2014-02-28 15:38:22 +00:00
|
|
|
str->appendf("%d: paintBits: (", i);
|
2013-01-28 20:21:59 +00:00
|
|
|
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
|
2014-02-14 10:06:42 +00:00
|
|
|
|
|
|
|
SkLayerDrawLooper::Builder::Builder()
|
|
|
|
: fRecs(NULL),
|
|
|
|
fTopRec(NULL),
|
|
|
|
fCount(0) {
|
|
|
|
}
|
|
|
|
|
|
|
|
SkLayerDrawLooper::Builder::~Builder() {
|
|
|
|
Rec* rec = fRecs;
|
|
|
|
while (rec) {
|
|
|
|
Rec* next = rec->fNext;
|
|
|
|
SkDELETE(rec);
|
|
|
|
rec = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SkPaint* SkLayerDrawLooper::Builder::addLayer(const LayerInfo& info) {
|
|
|
|
fCount += 1;
|
|
|
|
|
|
|
|
Rec* rec = SkNEW(Rec);
|
|
|
|
rec->fNext = fRecs;
|
|
|
|
rec->fInfo = info;
|
|
|
|
fRecs = rec;
|
|
|
|
if (NULL == fTopRec) {
|
|
|
|
fTopRec = rec;
|
|
|
|
}
|
|
|
|
|
|
|
|
return &rec->fPaint;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkLayerDrawLooper::Builder::addLayer(SkScalar dx, SkScalar dy) {
|
|
|
|
LayerInfo info;
|
|
|
|
|
|
|
|
info.fOffset.set(dx, dy);
|
|
|
|
(void)this->addLayer(info);
|
|
|
|
}
|
|
|
|
|
|
|
|
SkPaint* SkLayerDrawLooper::Builder::addLayerOnTop(const LayerInfo& info) {
|
|
|
|
fCount += 1;
|
|
|
|
|
|
|
|
Rec* rec = SkNEW(Rec);
|
|
|
|
rec->fNext = NULL;
|
|
|
|
rec->fInfo = info;
|
|
|
|
if (NULL == fRecs) {
|
|
|
|
fRecs = rec;
|
|
|
|
} else {
|
2014-09-05 20:34:00 +00:00
|
|
|
SkASSERT(fTopRec);
|
2014-02-14 10:06:42 +00:00
|
|
|
fTopRec->fNext = rec;
|
|
|
|
}
|
|
|
|
fTopRec = rec;
|
|
|
|
|
|
|
|
return &rec->fPaint;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkLayerDrawLooper* SkLayerDrawLooper::Builder::detachLooper() {
|
|
|
|
SkLayerDrawLooper* looper = SkNEW(SkLayerDrawLooper);
|
|
|
|
looper->fCount = fCount;
|
|
|
|
looper->fRecs = fRecs;
|
|
|
|
|
|
|
|
fCount = 0;
|
|
|
|
fRecs = NULL;
|
|
|
|
fTopRec = NULL;
|
|
|
|
|
|
|
|
return looper;
|
|
|
|
}
|