Add AlphaThreshold filter.
This is based on the Bitmap Alpha Threshold filter, and will be used by Chromium to implement the window shape API. R=bsalomon@chromium.org, wez@chromium.org, bsalomon@google.com, reed@google.com Author: zork@chromium.org Review URL: https://codereview.chromium.org/115633002 git-svn-id: http://skia.googlecode.com/svn/trunk@12935 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
parent
f6c8aeb174
commit
40eb3c1000
82
gm/imagealphathreshold.cpp
Normal file
82
gm/imagealphathreshold.cpp
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2013 Google Inc.
|
||||||
|
*
|
||||||
|
* Use of this source code is governed by a BSD-style license that can be
|
||||||
|
* found in the LICENSE file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "gm.h"
|
||||||
|
#include "SkAlphaThresholdFilter.h"
|
||||||
|
#include "SkRandom.h"
|
||||||
|
|
||||||
|
#define WIDTH 500
|
||||||
|
#define HEIGHT 500
|
||||||
|
|
||||||
|
namespace skiagm {
|
||||||
|
|
||||||
|
class ImageAlphaThresholdGM : public GM {
|
||||||
|
public:
|
||||||
|
ImageAlphaThresholdGM() {
|
||||||
|
this->setBGColor(0xFFFFFFFF);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual uint32_t onGetFlags() const SK_OVERRIDE {
|
||||||
|
return this->INHERITED::onGetFlags() | GM::kSkipTiled_Flag;
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual SkString onShortName() SK_OVERRIDE {
|
||||||
|
return SkString("imagealphathreshold");
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual SkISize onISize() SK_OVERRIDE {
|
||||||
|
return make_isize(WIDTH, HEIGHT);
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
|
||||||
|
SkIRect rects[2];
|
||||||
|
rects[0] = SkIRect::MakeXYWH(0, 150, WIDTH, HEIGHT - 300);
|
||||||
|
rects[1] = SkIRect::MakeXYWH(150, 0, WIDTH - 300, HEIGHT);
|
||||||
|
SkRegion region;
|
||||||
|
region.setRects(rects, 2);
|
||||||
|
|
||||||
|
SkMatrix matrix;
|
||||||
|
matrix.reset();
|
||||||
|
matrix.setTranslate(WIDTH * .1f, HEIGHT * .1f);
|
||||||
|
matrix.postScale(.8, .8);
|
||||||
|
|
||||||
|
canvas->concat(matrix);
|
||||||
|
|
||||||
|
SkPaint paint;
|
||||||
|
paint.setImageFilter(
|
||||||
|
SkAlphaThresholdFilter::Create(region, 0.2f, 0.7f))->unref();
|
||||||
|
canvas->saveLayer(NULL, &paint);
|
||||||
|
paint.setAntiAlias(true);
|
||||||
|
|
||||||
|
SkPaint rect_paint;
|
||||||
|
rect_paint.setColor(0xFF0000FF);
|
||||||
|
canvas->drawRect(SkRect::MakeXYWH(0, 0, WIDTH / 2, HEIGHT / 2),
|
||||||
|
rect_paint);
|
||||||
|
rect_paint.setColor(0xBFFF0000);
|
||||||
|
canvas->drawRect(SkRect::MakeXYWH(WIDTH / 2, 0, WIDTH / 2, HEIGHT / 2),
|
||||||
|
rect_paint);
|
||||||
|
rect_paint.setColor(0x3F00FF00);
|
||||||
|
canvas->drawRect(SkRect::MakeXYWH(0, HEIGHT / 2, WIDTH / 2, HEIGHT / 2),
|
||||||
|
rect_paint);
|
||||||
|
rect_paint.setColor(0x00000000);
|
||||||
|
canvas->drawRect(SkRect::MakeXYWH(WIDTH / 2, HEIGHT / 2, WIDTH / 2, HEIGHT / 2),
|
||||||
|
rect_paint);
|
||||||
|
|
||||||
|
canvas->restore();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
typedef GM INHERITED;
|
||||||
|
};
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
static GM* MyFactory(void*) { return new ImageAlphaThresholdGM; }
|
||||||
|
static GMRegistry reg(MyFactory);
|
||||||
|
|
||||||
|
}
|
@ -9,6 +9,7 @@
|
|||||||
'sources': [
|
'sources': [
|
||||||
'<(skia_src_path)/effects/Sk1DPathEffect.cpp',
|
'<(skia_src_path)/effects/Sk1DPathEffect.cpp',
|
||||||
'<(skia_src_path)/effects/Sk2DPathEffect.cpp',
|
'<(skia_src_path)/effects/Sk2DPathEffect.cpp',
|
||||||
|
'<(skia_src_path)/effects/SkAlphaThresholdFilter.cpp',
|
||||||
'<(skia_src_path)/effects/SkArithmeticMode.cpp',
|
'<(skia_src_path)/effects/SkArithmeticMode.cpp',
|
||||||
'<(skia_src_path)/effects/SkAvoidXfermode.cpp',
|
'<(skia_src_path)/effects/SkAvoidXfermode.cpp',
|
||||||
'<(skia_src_path)/effects/SkBicubicImageFilter.cpp',
|
'<(skia_src_path)/effects/SkBicubicImageFilter.cpp',
|
||||||
@ -80,6 +81,7 @@
|
|||||||
'<(skia_include_path)/effects/Sk1DPathEffect.h',
|
'<(skia_include_path)/effects/Sk1DPathEffect.h',
|
||||||
'<(skia_include_path)/effects/Sk2DPathEffect.h',
|
'<(skia_include_path)/effects/Sk2DPathEffect.h',
|
||||||
'<(skia_include_path)/effects/SkXfermodeImageFilter.h',
|
'<(skia_include_path)/effects/SkXfermodeImageFilter.h',
|
||||||
|
'<(skia_include_path)/effects/SkAlphaThresholdFilter.h',
|
||||||
'<(skia_include_path)/effects/SkArithmeticMode.h',
|
'<(skia_include_path)/effects/SkArithmeticMode.h',
|
||||||
'<(skia_include_path)/effects/SkAvoidXfermode.h',
|
'<(skia_include_path)/effects/SkAvoidXfermode.h',
|
||||||
'<(skia_include_path)/effects/SkBitmapSource.h',
|
'<(skia_include_path)/effects/SkBitmapSource.h',
|
||||||
|
@ -73,6 +73,7 @@
|
|||||||
'../gm/hairlines.cpp',
|
'../gm/hairlines.cpp',
|
||||||
'../gm/hairmodes.cpp',
|
'../gm/hairmodes.cpp',
|
||||||
'../gm/hittestpath.cpp',
|
'../gm/hittestpath.cpp',
|
||||||
|
'../gm/imagealphathreshold.cpp',
|
||||||
'../gm/imageblur.cpp',
|
'../gm/imageblur.cpp',
|
||||||
'../gm/imagemagnifier.cpp',
|
'../gm/imagemagnifier.cpp',
|
||||||
'../gm/inversepaths.cpp',
|
'../gm/inversepaths.cpp',
|
||||||
|
@ -72,6 +72,7 @@
|
|||||||
'effects/SkMorphologyImageFilter.h',
|
'effects/SkMorphologyImageFilter.h',
|
||||||
'effects/Sk2DPathEffect.h',
|
'effects/Sk2DPathEffect.h',
|
||||||
'effects/SkXfermodeImageFilter.h',
|
'effects/SkXfermodeImageFilter.h',
|
||||||
|
'effects/SkAlphaThresholdFilter.h',
|
||||||
'effects/SkArithmeticMode.h',
|
'effects/SkArithmeticMode.h',
|
||||||
'effects/SkMergeImageFilter.h',
|
'effects/SkMergeImageFilter.h',
|
||||||
'effects/SkPerlinNoiseShader.h',
|
'effects/SkPerlinNoiseShader.h',
|
||||||
|
26
include/effects/SkAlphaThresholdFilter.h
Normal file
26
include/effects/SkAlphaThresholdFilter.h
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2013 Google Inc.
|
||||||
|
*
|
||||||
|
* Use of this source code is governed by a BSD-style license that can be
|
||||||
|
* found in the LICENSE file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef SkAlphaThresholdFilter_DEFINED
|
||||||
|
#define SkAlphaThresholdFilter_DEFINED
|
||||||
|
|
||||||
|
#include "SkRegion.h"
|
||||||
|
#include "SkImageFilter.h"
|
||||||
|
|
||||||
|
class SK_API SkAlphaThresholdFilter {
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* Creates an image filter that samples a region. If the sample is inside the
|
||||||
|
* region the alpha of the image is boosted up to a threshold value. If it is
|
||||||
|
* outside the region then the alpha is decreased to the threshold value.
|
||||||
|
* The 0,0 point of the region corresponds to the upper left corner of the
|
||||||
|
* source image.
|
||||||
|
*/
|
||||||
|
static SkImageFilter* Create(const SkRegion& region, SkScalar innerThreshold, SkScalar outerThreshold);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
367
src/effects/SkAlphaThresholdFilter.cpp
Normal file
367
src/effects/SkAlphaThresholdFilter.cpp
Normal file
@ -0,0 +1,367 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2013 Google Inc.
|
||||||
|
*
|
||||||
|
* Use of this source code is governed by a BSD-style license that can be
|
||||||
|
* found in the LICENSE file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "SkAlphaThresholdFilter.h"
|
||||||
|
#include "SkBitmap.h"
|
||||||
|
#include "SkFlattenableBuffers.h"
|
||||||
|
#include "SkRegion.h"
|
||||||
|
|
||||||
|
class SK_API SkAlphaThresholdFilterImpl : public SkImageFilter {
|
||||||
|
public:
|
||||||
|
SkAlphaThresholdFilterImpl(const SkRegion& region, SkScalar innerThreshold, SkScalar outerThreshold);
|
||||||
|
|
||||||
|
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkAlphaThresholdFilterImpl)
|
||||||
|
|
||||||
|
protected:
|
||||||
|
explicit SkAlphaThresholdFilterImpl(SkFlattenableReadBuffer& buffer);
|
||||||
|
virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE;
|
||||||
|
|
||||||
|
virtual bool onFilterImage(Proxy*, const SkBitmap& src, const SkMatrix&,
|
||||||
|
SkBitmap* result, SkIPoint* offset) SK_OVERRIDE;
|
||||||
|
#if SK_SUPPORT_GPU
|
||||||
|
virtual bool asNewEffect(GrEffectRef** effect, GrTexture* texture,
|
||||||
|
const SkMatrix& matrix, const SkIRect& bounds) const SK_OVERRIDE;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
private:
|
||||||
|
SkRegion fRegion;
|
||||||
|
SkScalar fInnerThreshold;
|
||||||
|
SkScalar fOuterThreshold;
|
||||||
|
typedef SkImageFilter INHERITED;
|
||||||
|
};
|
||||||
|
|
||||||
|
SkImageFilter* SkAlphaThresholdFilter::Create(const SkRegion& region,
|
||||||
|
SkScalar innerThreshold,
|
||||||
|
SkScalar outerThreshold) {
|
||||||
|
return SkNEW_ARGS(SkAlphaThresholdFilterImpl, (region, innerThreshold, outerThreshold));
|
||||||
|
}
|
||||||
|
|
||||||
|
#if SK_SUPPORT_GPU
|
||||||
|
#include "GrContext.h"
|
||||||
|
#include "GrCoordTransform.h"
|
||||||
|
#include "GrEffect.h"
|
||||||
|
#include "gl/GrGLEffect.h"
|
||||||
|
#include "GrTBackendEffectFactory.h"
|
||||||
|
#include "GrTextureAccess.h"
|
||||||
|
|
||||||
|
#include "SkGr.h"
|
||||||
|
|
||||||
|
class GrGLAlphaThresholdEffect;
|
||||||
|
|
||||||
|
class AlphaThresholdEffect : public GrEffect {
|
||||||
|
|
||||||
|
public:
|
||||||
|
static GrEffectRef* Create(GrTexture* texture,
|
||||||
|
GrTexture* maskTexture,
|
||||||
|
float innerThreshold,
|
||||||
|
float outerThreshold) {
|
||||||
|
AutoEffectUnref effect(SkNEW_ARGS(AlphaThresholdEffect, (texture,
|
||||||
|
maskTexture,
|
||||||
|
innerThreshold,
|
||||||
|
outerThreshold)));
|
||||||
|
return CreateEffectRef(effect);
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual ~AlphaThresholdEffect() {};
|
||||||
|
|
||||||
|
static const char* Name() { return "Alpha Threshold"; }
|
||||||
|
|
||||||
|
virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
|
||||||
|
virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE;
|
||||||
|
|
||||||
|
float innerThreshold() const { return fInnerThreshold; }
|
||||||
|
float outerThreshold() const { return fOuterThreshold; }
|
||||||
|
|
||||||
|
typedef GrGLAlphaThresholdEffect GLEffect;
|
||||||
|
|
||||||
|
private:
|
||||||
|
AlphaThresholdEffect(GrTexture* texture,
|
||||||
|
GrTexture* maskTexture,
|
||||||
|
float innerThreshold,
|
||||||
|
float outerThreshold)
|
||||||
|
: fInnerThreshold(innerThreshold)
|
||||||
|
, fOuterThreshold(outerThreshold)
|
||||||
|
, fImageCoordTransform(kLocal_GrCoordSet, MakeDivByTextureWHMatrix(texture), texture)
|
||||||
|
, fImageTextureAccess(texture)
|
||||||
|
, fMaskCoordTransform(kLocal_GrCoordSet, MakeDivByTextureWHMatrix(maskTexture), maskTexture)
|
||||||
|
, fMaskTextureAccess(maskTexture) {
|
||||||
|
this->addCoordTransform(&fImageCoordTransform);
|
||||||
|
this->addTextureAccess(&fImageTextureAccess);
|
||||||
|
this->addCoordTransform(&fMaskCoordTransform);
|
||||||
|
this->addTextureAccess(&fMaskTextureAccess);
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE;
|
||||||
|
|
||||||
|
GR_DECLARE_EFFECT_TEST;
|
||||||
|
|
||||||
|
float fInnerThreshold;
|
||||||
|
float fOuterThreshold;
|
||||||
|
GrCoordTransform fImageCoordTransform;
|
||||||
|
GrTextureAccess fImageTextureAccess;
|
||||||
|
GrCoordTransform fMaskCoordTransform;
|
||||||
|
GrTextureAccess fMaskTextureAccess;
|
||||||
|
|
||||||
|
typedef GrEffect INHERITED;
|
||||||
|
};
|
||||||
|
|
||||||
|
class GrGLAlphaThresholdEffect : public GrGLEffect {
|
||||||
|
public:
|
||||||
|
GrGLAlphaThresholdEffect(const GrBackendEffectFactory&, const GrDrawEffect&);
|
||||||
|
|
||||||
|
virtual void emitCode(GrGLShaderBuilder*,
|
||||||
|
const GrDrawEffect&,
|
||||||
|
EffectKey,
|
||||||
|
const char* outputColor,
|
||||||
|
const char* inputColor,
|
||||||
|
const TransformedCoordsArray&,
|
||||||
|
const TextureSamplerArray&) SK_OVERRIDE;
|
||||||
|
|
||||||
|
virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE;
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
GrGLUniformManager::UniformHandle fInnerThresholdVar;
|
||||||
|
GrGLUniformManager::UniformHandle fOuterThresholdVar;
|
||||||
|
|
||||||
|
typedef GrGLEffect INHERITED;
|
||||||
|
};
|
||||||
|
|
||||||
|
GrGLAlphaThresholdEffect::GrGLAlphaThresholdEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
|
||||||
|
: INHERITED(factory) {
|
||||||
|
}
|
||||||
|
|
||||||
|
void GrGLAlphaThresholdEffect::emitCode(GrGLShaderBuilder* builder,
|
||||||
|
const GrDrawEffect&,
|
||||||
|
EffectKey key,
|
||||||
|
const char* outputColor,
|
||||||
|
const char* inputColor,
|
||||||
|
const TransformedCoordsArray& coords,
|
||||||
|
const TextureSamplerArray& samplers) {
|
||||||
|
SkString coords2D = builder->ensureFSCoords2D(coords, 0);
|
||||||
|
SkString maskCoords2D = builder->ensureFSCoords2D(coords, 1);
|
||||||
|
fInnerThresholdVar = builder->addUniform(
|
||||||
|
GrGLShaderBuilder::kFragment_Visibility,
|
||||||
|
kFloat_GrSLType, "inner_threshold");
|
||||||
|
fOuterThresholdVar = builder->addUniform(
|
||||||
|
GrGLShaderBuilder::kFragment_Visibility,
|
||||||
|
kFloat_GrSLType, "outer_threshold");
|
||||||
|
|
||||||
|
builder->fsCodeAppendf("\t\tvec2 coord = %s;\n", coords2D.c_str());
|
||||||
|
builder->fsCodeAppendf("\t\tvec2 mask_coord = %s;\n", maskCoords2D.c_str());
|
||||||
|
builder->fsCodeAppend("\t\tvec4 input_color = ");
|
||||||
|
builder->fsAppendTextureLookup(samplers[0], "coord");
|
||||||
|
builder->fsCodeAppend(";\n");
|
||||||
|
builder->fsCodeAppend("\t\tvec4 mask_color = ");
|
||||||
|
builder->fsAppendTextureLookup(samplers[1], "mask_coord");
|
||||||
|
builder->fsCodeAppend(";\n");
|
||||||
|
|
||||||
|
builder->fsCodeAppendf("\t\tfloat inner_thresh = %s;\n",
|
||||||
|
builder->getUniformCStr(fInnerThresholdVar));
|
||||||
|
builder->fsCodeAppendf("\t\tfloat outer_thresh = %s;\n",
|
||||||
|
builder->getUniformCStr(fOuterThresholdVar));
|
||||||
|
builder->fsCodeAppend("\t\tfloat mask = mask_color.a;\n");
|
||||||
|
|
||||||
|
builder->fsCodeAppend("vec4 color = input_color;\n");
|
||||||
|
builder->fsCodeAppend("\t\tif (mask < 0.5) {\n"
|
||||||
|
"\t\t\tif (color.a > outer_thresh) {\n"
|
||||||
|
"\t\t\t\tfloat scale = outer_thresh / color.a;\n"
|
||||||
|
"\t\t\t\tcolor.rgb *= scale;\n"
|
||||||
|
"\t\t\t\tcolor.a = outer_thresh;\n"
|
||||||
|
"\t\t\t}\n"
|
||||||
|
"\t\t} else if (color.a < inner_thresh) {\n"
|
||||||
|
"\t\t\tfloat scale = inner_thresh / max(0.001, color.a);\n"
|
||||||
|
"\t\t\tcolor.rgb *= scale;\n"
|
||||||
|
"\t\t\tcolor.a = inner_thresh;\n"
|
||||||
|
"\t\t}\n");
|
||||||
|
|
||||||
|
builder->fsCodeAppendf("%s = %s;\n", outputColor,
|
||||||
|
(GrGLSLExpr4(inputColor) * GrGLSLExpr4("color")).c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
void GrGLAlphaThresholdEffect::setData(const GrGLUniformManager& uman,
|
||||||
|
const GrDrawEffect& drawEffect) {
|
||||||
|
const AlphaThresholdEffect& alpha_threshold =
|
||||||
|
drawEffect.castEffect<AlphaThresholdEffect>();
|
||||||
|
uman.set1f(fInnerThresholdVar, alpha_threshold.innerThreshold());
|
||||||
|
uman.set1f(fOuterThresholdVar, alpha_threshold.outerThreshold());
|
||||||
|
}
|
||||||
|
|
||||||
|
/////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
GR_DEFINE_EFFECT_TEST(AlphaThresholdEffect);
|
||||||
|
|
||||||
|
GrEffectRef* AlphaThresholdEffect::TestCreate(SkRandom* random,
|
||||||
|
GrContext* context,
|
||||||
|
const GrDrawTargetCaps&,
|
||||||
|
GrTexture** textures) {
|
||||||
|
GrTexture* bmpTex = textures[GrEffectUnitTest::kSkiaPMTextureIdx];
|
||||||
|
GrTexture* maskTex = textures[GrEffectUnitTest::kAlphaTextureIdx];
|
||||||
|
float inner_thresh = random->nextUScalar1();
|
||||||
|
float outer_thresh = random->nextUScalar1();
|
||||||
|
return AlphaThresholdEffect::Create(bmpTex, maskTex, inner_thresh, outer_thresh);
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
const GrBackendEffectFactory& AlphaThresholdEffect::getFactory() const {
|
||||||
|
return GrTBackendEffectFactory<AlphaThresholdEffect>::getInstance();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool AlphaThresholdEffect::onIsEqual(const GrEffect& sBase) const {
|
||||||
|
const AlphaThresholdEffect& s = CastEffect<AlphaThresholdEffect>(sBase);
|
||||||
|
return (this->texture(0) == s.texture(0) &&
|
||||||
|
this->fInnerThreshold == s.fInnerThreshold &&
|
||||||
|
this->fOuterThreshold == s.fOuterThreshold);
|
||||||
|
}
|
||||||
|
|
||||||
|
void AlphaThresholdEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
|
||||||
|
if ((*validFlags & kA_GrColorComponentFlag) && 0xFF == GrColorUnpackA(*color) &&
|
||||||
|
GrPixelConfigIsOpaque(this->texture(0)->config())) {
|
||||||
|
*validFlags = kA_GrColorComponentFlag;
|
||||||
|
} else {
|
||||||
|
*validFlags = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
SkAlphaThresholdFilterImpl::SkAlphaThresholdFilterImpl(SkFlattenableReadBuffer& buffer)
|
||||||
|
: INHERITED(1, buffer) {
|
||||||
|
fInnerThreshold = buffer.readScalar();
|
||||||
|
fOuterThreshold = buffer.readScalar();
|
||||||
|
buffer.readRegion(&fRegion);
|
||||||
|
}
|
||||||
|
|
||||||
|
SkAlphaThresholdFilterImpl::SkAlphaThresholdFilterImpl(const SkRegion& region,
|
||||||
|
SkScalar innerThreshold,
|
||||||
|
SkScalar outerThreshold)
|
||||||
|
: INHERITED(0)
|
||||||
|
, fRegion(region)
|
||||||
|
, fInnerThreshold(innerThreshold)
|
||||||
|
, fOuterThreshold(outerThreshold) {
|
||||||
|
}
|
||||||
|
|
||||||
|
#if SK_SUPPORT_GPU
|
||||||
|
bool SkAlphaThresholdFilterImpl::asNewEffect(GrEffectRef** effect, GrTexture* texture,
|
||||||
|
const SkMatrix& in_matrix, const SkIRect&) const {
|
||||||
|
if (effect) {
|
||||||
|
GrContext* context = texture->getContext();
|
||||||
|
GrTextureDesc maskDesc;
|
||||||
|
if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
|
||||||
|
maskDesc.fConfig = kAlpha_8_GrPixelConfig;
|
||||||
|
} else {
|
||||||
|
maskDesc.fConfig = kRGBA_8888_GrPixelConfig;
|
||||||
|
}
|
||||||
|
maskDesc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
|
||||||
|
// Add one pixel of border to ensure that clamp mode will be all zeros
|
||||||
|
// the outside.
|
||||||
|
maskDesc.fWidth = texture->width();
|
||||||
|
maskDesc.fHeight = texture->height();
|
||||||
|
GrAutoScratchTexture ast(context, maskDesc, GrContext::kApprox_ScratchTexMatch);
|
||||||
|
GrTexture* maskTexture = ast.texture();
|
||||||
|
if (NULL == maskTexture) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
GrContext::AutoRenderTarget art(context, ast.texture()->asRenderTarget());
|
||||||
|
GrPaint grPaint;
|
||||||
|
grPaint.setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
|
||||||
|
SkRegion::Iterator iter(fRegion);
|
||||||
|
context->clear(NULL, 0x0, true);
|
||||||
|
|
||||||
|
SkMatrix old_matrix = context->getMatrix();
|
||||||
|
context->setMatrix(in_matrix);
|
||||||
|
|
||||||
|
while (!iter.done()) {
|
||||||
|
SkRect rect = SkRect::Make(iter.rect());
|
||||||
|
context->drawRect(grPaint, rect);
|
||||||
|
iter.next();
|
||||||
|
}
|
||||||
|
context->setMatrix(old_matrix);
|
||||||
|
}
|
||||||
|
|
||||||
|
*effect = AlphaThresholdEffect::Create(texture,
|
||||||
|
maskTexture,
|
||||||
|
fInnerThreshold,
|
||||||
|
fOuterThreshold);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void SkAlphaThresholdFilterImpl::flatten(SkFlattenableWriteBuffer& buffer) const {
|
||||||
|
this->INHERITED::flatten(buffer);
|
||||||
|
buffer.writeScalar(fInnerThreshold);
|
||||||
|
buffer.writeScalar(fOuterThreshold);
|
||||||
|
buffer.writeRegion(fRegion);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool SkAlphaThresholdFilterImpl::onFilterImage(Proxy*, const SkBitmap& src,
|
||||||
|
const SkMatrix& matrix, SkBitmap* dst,
|
||||||
|
SkIPoint* offset) {
|
||||||
|
SkASSERT(src.config() == SkBitmap::kARGB_8888_Config);
|
||||||
|
|
||||||
|
if (src.config() != SkBitmap::kARGB_8888_Config) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
SkMatrix localInverse;
|
||||||
|
if (!matrix.invert(&localInverse)) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
SkAutoLockPixels alp(src);
|
||||||
|
SkASSERT(src.getPixels());
|
||||||
|
if (!src.getPixels() || src.width() <= 0 || src.height() <= 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
dst->setConfig(src.config(), src.width(), src.height());
|
||||||
|
dst->allocPixels();
|
||||||
|
if (!dst->getPixels()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
U8CPU innerThreshold = fInnerThreshold * 0xFF;
|
||||||
|
U8CPU outerThreshold = fOuterThreshold * 0xFF;
|
||||||
|
SkColor* sptr = src.getAddr32(0, 0);
|
||||||
|
SkColor* dptr = dst->getAddr32(0, 0);
|
||||||
|
int width = src.width(), height = src.height();
|
||||||
|
for (int y = 0; y < height; ++y) {
|
||||||
|
for (int x = 0; x < width; ++x) {
|
||||||
|
const SkColor& source = sptr[y * width + x];
|
||||||
|
SkColor output_color(source);
|
||||||
|
SkPoint position;
|
||||||
|
localInverse.mapXY(x, y, &position);
|
||||||
|
if (fRegion.contains(position.x(), position.y())) {
|
||||||
|
if (SkColorGetA(source) < innerThreshold) {
|
||||||
|
U8CPU alpha = SkColorGetA(source);
|
||||||
|
if (alpha == 0)
|
||||||
|
alpha = 1;
|
||||||
|
float scale = (float)innerThreshold / alpha;
|
||||||
|
output_color = SkColorSetARGB(innerThreshold,
|
||||||
|
SkColorGetR(source) * scale,
|
||||||
|
SkColorGetG(source) * scale,
|
||||||
|
SkColorGetB(source) * scale);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (SkColorGetA(source) > outerThreshold) {
|
||||||
|
float scale = (float)outerThreshold / SkColorGetA(source);
|
||||||
|
output_color = SkColorSetARGB(outerThreshold,
|
||||||
|
SkColorGetR(source) * scale,
|
||||||
|
SkColorGetG(source) * scale,
|
||||||
|
SkColorGetB(source) * scale);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dptr[y * dst->width() + x] = output_color;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
@ -251,6 +251,7 @@ DEFINE_GPUTESTCLASS("GLPrograms", GLProgramsTestClass, GLProgramsTest)
|
|||||||
// in the unit that could pass pointers to functions from the unit out to other translation units!
|
// in the unit that could pass pointers to functions from the unit out to other translation units!
|
||||||
// We force some of the effects that would otherwise be discarded to link here.
|
// We force some of the effects that would otherwise be discarded to link here.
|
||||||
|
|
||||||
|
#include "SkAlphaThresholdFilter.h"
|
||||||
#include "SkLightingImageFilter.h"
|
#include "SkLightingImageFilter.h"
|
||||||
#include "SkMagnifierImageFilter.h"
|
#include "SkMagnifierImageFilter.h"
|
||||||
#include "SkColorMatrixFilter.h"
|
#include "SkColorMatrixFilter.h"
|
||||||
@ -259,6 +260,7 @@ void forceLinking();
|
|||||||
|
|
||||||
void forceLinking() {
|
void forceLinking() {
|
||||||
SkLightingImageFilter::CreateDistantLitDiffuse(SkPoint3(0,0,0), 0, 0, 0);
|
SkLightingImageFilter::CreateDistantLitDiffuse(SkPoint3(0,0,0), 0, 0, 0);
|
||||||
|
SkAlphaThresholdFilter::Create(SkRegion(), .5, .5);
|
||||||
SkMagnifierImageFilter mag(SkRect::MakeWH(SK_Scalar1, SK_Scalar1), SK_Scalar1);
|
SkMagnifierImageFilter mag(SkRect::MakeWH(SK_Scalar1, SK_Scalar1), SK_Scalar1);
|
||||||
GrConfigConversionEffect::Create(NULL,
|
GrConfigConversionEffect::Create(NULL,
|
||||||
false,
|
false,
|
||||||
|
Loading…
Reference in New Issue
Block a user