2013-01-22 14:32:09 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2016-09-26 22:51:24 +00:00
|
|
|
#include "SkArithmeticModePriv.h"
|
2011-12-10 19:36:56 +00:00
|
|
|
#include "SkColorPriv.h"
|
2016-04-10 13:23:28 +00:00
|
|
|
#include "SkNx.h"
|
2016-07-25 12:56:16 +00:00
|
|
|
#include "SkRasterPipeline.h"
|
2014-01-30 18:58:24 +00:00
|
|
|
#include "SkReadBuffer.h"
|
2013-01-22 14:32:09 +00:00
|
|
|
#include "SkString.h"
|
2011-12-10 19:36:56 +00:00
|
|
|
#include "SkUnPreMultiply.h"
|
2016-04-10 13:23:28 +00:00
|
|
|
#include "SkWriteBuffer.h"
|
2013-05-29 20:55:09 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
2015-01-14 18:49:18 +00:00
|
|
|
#include "SkArithmeticMode_gpu.h"
|
2013-05-29 20:55:09 +00:00
|
|
|
#endif
|
2011-12-10 19:36:56 +00:00
|
|
|
|
|
|
|
class SkArithmeticMode_scalar : public SkXfermode {
|
|
|
|
public:
|
2016-02-22 14:59:37 +00:00
|
|
|
SkArithmeticMode_scalar(SkScalar k1, SkScalar k2, SkScalar k3, SkScalar k4,
|
|
|
|
bool enforcePMColor) {
|
|
|
|
fK[0] = k1;
|
|
|
|
fK[1] = k2;
|
|
|
|
fK[2] = k3;
|
|
|
|
fK[3] = k4;
|
|
|
|
fEnforcePMColor = enforcePMColor;
|
2011-12-10 19:36:56 +00:00
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2016-02-22 14:59:37 +00:00
|
|
|
void xfer32(SkPMColor[], const SkPMColor[], int count, const SkAlpha[]) const override;
|
2011-12-10 19:36:56 +00:00
|
|
|
|
2016-07-25 12:56:16 +00:00
|
|
|
bool onAppendStages(SkRasterPipeline* p) const override {
|
|
|
|
p->append(&Stage, this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-03-13 18:02:17 +00:00
|
|
|
SK_TO_STRING_OVERRIDE()
|
2013-05-29 20:55:09 +00:00
|
|
|
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkArithmeticMode_scalar)
|
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
2016-06-09 15:01:03 +00:00
|
|
|
sk_sp<GrFragmentProcessor> makeFragmentProcessorForImageFilter(
|
|
|
|
sk_sp<GrFragmentProcessor> dst) const override;
|
|
|
|
sk_sp<GrXPFactory> asXPFactory() const override;
|
2013-05-29 20:55:09 +00:00
|
|
|
#endif
|
2011-12-10 19:36:56 +00:00
|
|
|
|
|
|
|
private:
|
2016-07-25 12:56:16 +00:00
|
|
|
static void SK_VECTORCALL Stage(SkRasterPipeline::Stage* st, size_t x,
|
|
|
|
Sk4f r, Sk4f g, Sk4f b, Sk4f a,
|
|
|
|
Sk4f dr, Sk4f dg, Sk4f db, Sk4f da);
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void flatten(SkWriteBuffer& buffer) const override {
|
2013-05-29 20:55:09 +00:00
|
|
|
buffer.writeScalar(fK[0]);
|
|
|
|
buffer.writeScalar(fK[1]);
|
|
|
|
buffer.writeScalar(fK[2]);
|
|
|
|
buffer.writeScalar(fK[3]);
|
2014-04-02 19:32:15 +00:00
|
|
|
buffer.writeBool(fEnforcePMColor);
|
2013-05-29 20:55:09 +00:00
|
|
|
}
|
2016-01-21 22:15:10 +00:00
|
|
|
|
2011-12-10 19:36:56 +00:00
|
|
|
SkScalar fK[4];
|
2014-04-02 19:32:15 +00:00
|
|
|
bool fEnforcePMColor;
|
2013-01-22 14:32:09 +00:00
|
|
|
|
2014-08-21 14:59:51 +00:00
|
|
|
friend class SkArithmeticMode;
|
|
|
|
|
2013-01-23 07:06:17 +00:00
|
|
|
typedef SkXfermode INHERITED;
|
2011-12-10 19:36:56 +00:00
|
|
|
};
|
|
|
|
|
2016-04-03 16:11:13 +00:00
|
|
|
sk_sp<SkFlattenable> SkArithmeticMode_scalar::CreateProc(SkReadBuffer& buffer) {
|
2014-08-21 14:59:51 +00:00
|
|
|
const SkScalar k1 = buffer.readScalar();
|
|
|
|
const SkScalar k2 = buffer.readScalar();
|
|
|
|
const SkScalar k3 = buffer.readScalar();
|
|
|
|
const SkScalar k4 = buffer.readScalar();
|
|
|
|
const bool enforcePMColor = buffer.readBool();
|
2016-04-03 16:11:13 +00:00
|
|
|
return SkArithmeticMode::Make(k1, k2, k3, k4, enforcePMColor);
|
2014-08-21 14:59:51 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 12:56:16 +00:00
|
|
|
void SK_VECTORCALL SkArithmeticMode_scalar::Stage(SkRasterPipeline::Stage* st, size_t x,
|
|
|
|
Sk4f r, Sk4f g, Sk4f b, Sk4f a,
|
|
|
|
Sk4f dr, Sk4f dg, Sk4f db, Sk4f da) {
|
|
|
|
auto self = st->ctx<const SkArithmeticMode_scalar*>();
|
|
|
|
|
|
|
|
const Sk4f k1 = self->fK[0],
|
|
|
|
k2 = self->fK[1],
|
|
|
|
k3 = self->fK[2],
|
|
|
|
k4 = self->fK[3];
|
|
|
|
|
|
|
|
r = k1*r*dr + k2*r + k3*dr + k4;
|
|
|
|
g = k1*g*dg + k2*g + k3*dg + k4;
|
|
|
|
b = k1*b*db + k2*b + k3*db + k4;
|
|
|
|
a = k1*a*da + k2*a + k3*da + k4;
|
|
|
|
|
|
|
|
// A later stage (clamp_01_premul) will pin and fEnforcePMColor for us.
|
|
|
|
|
|
|
|
st->next(x, r,g,b,a, dr,dg,db,da);
|
|
|
|
}
|
|
|
|
|
2016-04-10 13:23:28 +00:00
|
|
|
void SkArithmeticMode_scalar::xfer32(SkPMColor dst[], const SkPMColor src[],
|
|
|
|
int count, const SkAlpha aaCoverage[]) const {
|
|
|
|
const Sk4f k1 = fK[0] * (1/255.0f),
|
|
|
|
k2 = fK[1],
|
|
|
|
k3 = fK[2],
|
|
|
|
k4 = fK[3] * 255.0f + 0.5f;
|
|
|
|
|
|
|
|
auto pin = [](float min, const Sk4f& val, float max) {
|
|
|
|
return Sk4f::Max(min, Sk4f::Min(val, max));
|
|
|
|
};
|
|
|
|
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
if (aaCoverage && aaCoverage[i] == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2011-12-10 19:36:56 +00:00
|
|
|
|
2016-04-10 13:23:28 +00:00
|
|
|
Sk4f s = SkNx_cast<float>(Sk4b::Load(src+i)),
|
|
|
|
d = SkNx_cast<float>(Sk4b::Load(dst+i)),
|
|
|
|
r = pin(0, k1*s*d + k2*s + k3*d + k4, 255);
|
2011-12-10 19:36:56 +00:00
|
|
|
|
2016-04-10 13:23:28 +00:00
|
|
|
if (fEnforcePMColor) {
|
|
|
|
Sk4f a = SkNx_shuffle<3,3,3,3>(r);
|
|
|
|
r = Sk4f::Min(a, r);
|
|
|
|
}
|
2011-12-10 19:36:56 +00:00
|
|
|
|
2016-04-10 13:23:28 +00:00
|
|
|
if (aaCoverage && aaCoverage[i] != 255) {
|
|
|
|
Sk4f c = aaCoverage[i] * (1/255.0f);
|
|
|
|
r = d + (r-d)*c;
|
2011-12-10 19:36:56 +00:00
|
|
|
}
|
2016-04-10 13:23:28 +00:00
|
|
|
|
|
|
|
SkNx_cast<uint8_t>(r).store(dst+i);
|
2011-12-10 19:36:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-13 18:02:17 +00:00
|
|
|
#ifndef SK_IGNORE_TO_STRING
|
2013-01-22 14:32:09 +00:00
|
|
|
void SkArithmeticMode_scalar::toString(SkString* str) const {
|
|
|
|
str->append("SkArithmeticMode_scalar: ");
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
str->appendScalar(fK[i]);
|
2014-04-02 19:32:15 +00:00
|
|
|
str->append(" ");
|
2013-01-22 14:32:09 +00:00
|
|
|
}
|
2014-04-02 19:32:15 +00:00
|
|
|
str->appendS32(fEnforcePMColor ? 1 : 0);
|
2013-01-22 14:32:09 +00:00
|
|
|
}
|
|
|
|
#endif
|
2011-12-10 19:36:56 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-03-29 18:32:50 +00:00
|
|
|
sk_sp<SkXfermode> SkArithmeticMode::Make(SkScalar k1, SkScalar k2, SkScalar k3, SkScalar k4,
|
|
|
|
bool enforcePMColor) {
|
2016-02-22 14:59:37 +00:00
|
|
|
if (SkScalarNearlyZero(k1) && SkScalarNearlyEqual(k2, SK_Scalar1) &&
|
|
|
|
SkScalarNearlyZero(k3) && SkScalarNearlyZero(k4)) {
|
2016-03-29 18:32:50 +00:00
|
|
|
return SkXfermode::Make(SkXfermode::kSrc_Mode);
|
2016-02-22 14:59:37 +00:00
|
|
|
} else if (SkScalarNearlyZero(k1) && SkScalarNearlyZero(k2) &&
|
|
|
|
SkScalarNearlyEqual(k3, SK_Scalar1) && SkScalarNearlyZero(k4)) {
|
2016-03-29 18:32:50 +00:00
|
|
|
return SkXfermode::Make(SkXfermode::kDst_Mode);
|
2011-12-10 19:36:56 +00:00
|
|
|
}
|
2016-03-29 18:32:50 +00:00
|
|
|
return sk_make_sp<SkArithmeticMode_scalar>(k1, k2, k3, k4, enforcePMColor);
|
2011-12-10 19:36:56 +00:00
|
|
|
}
|
2013-05-29 20:55:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
2016-06-09 15:01:03 +00:00
|
|
|
sk_sp<GrFragmentProcessor> SkArithmeticMode_scalar::makeFragmentProcessorForImageFilter(
|
|
|
|
sk_sp<GrFragmentProcessor> dst) const {
|
|
|
|
return GrArithmeticFP::Make(SkScalarToFloat(fK[0]),
|
|
|
|
SkScalarToFloat(fK[1]),
|
|
|
|
SkScalarToFloat(fK[2]),
|
|
|
|
SkScalarToFloat(fK[3]),
|
|
|
|
fEnforcePMColor,
|
|
|
|
std::move(dst));
|
2013-05-29 20:55:09 +00:00
|
|
|
}
|
|
|
|
|
2016-06-09 15:01:03 +00:00
|
|
|
sk_sp<GrXPFactory> SkArithmeticMode_scalar::asXPFactory() const {
|
|
|
|
return GrArithmeticXPFactory::Make(SkScalarToFloat(fK[0]),
|
|
|
|
SkScalarToFloat(fK[1]),
|
|
|
|
SkScalarToFloat(fK[2]),
|
|
|
|
SkScalarToFloat(fK[3]),
|
|
|
|
fEnforcePMColor);
|
2015-01-14 20:53:01 +00:00
|
|
|
}
|
|
|
|
|
2013-05-29 20:55:09 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkArithmeticMode)
|
|
|
|
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkArithmeticMode_scalar)
|
|
|
|
SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END
|