return 0.4375f + (-x3 / 6.0f - 3.0f * x2 * 0.25f - 1.125f * x);
}
-/* compute_profile allocates and fills in an array of floating
+/* ComputeBlurProfile allocates and fills in an array of floating
point values between 0 and 255 for the profile signature of
a blurred half-plane with the given blur radius. Since we're
going to be doing screened multiplications (i.e., 1 - (1-x)(1-y))
memory returned in profile_out.
*/
-static void compute_profile(SkScalar sigma, unsigned int **profile_out) {
+void SkBlurMask::ComputeBlurProfile(SkScalar sigma, uint8_t **profile_out) {
int size = SkScalarCeilToInt(6*sigma);
int center = size >> 1;
- unsigned int *profile = SkNEW_ARRAY(unsigned int, size);
+ uint8_t *profile = SkNEW_ARRAY(uint8_t, size);
float invr = 1.f/(2*sigma);
// Implementation adapted from Michael Herf's approach:
// http://stereopsis.com/shadowrect/
-static inline unsigned int profile_lookup( unsigned int *profile, int loc, int blurred_width, int sharp_width ) {
+uint8_t SkBlurMask::ProfileLookup(const uint8_t *profile, int loc, int blurred_width, int sharp_width) {
int dx = SkAbs32(((loc << 1) + 1) - blurred_width) - sharp_width; // how far are we from the original edge?
int ox = dx >> 1;
if (ox < 0) {
return profile[ox];
}
+void SkBlurMask::ComputeBlurredScanline(uint8_t *pixels, const uint8_t *profile,
+ unsigned int width, SkScalar sigma) {
+
+ unsigned int profile_size = SkScalarCeilToInt(6*sigma);
+ SkAutoTMalloc<uint8_t> horizontalScanline(width);
+
+ unsigned int sw = width - profile_size;
+ // nearest odd number less than the profile size represents the center
+ // of the (2x scaled) profile
+ int center = ( profile_size & ~1 ) - 1;
+
+ int w = sw - center;
+
+ for (unsigned int x = 0 ; x < width ; ++x) {
+ if (profile_size <= sw) {
+ pixels[x] = ProfileLookup(profile, x, width, w);
+ } else {
+ float span = float(sw)/(2*sigma);
+ float giX = 1.5f - (x+.5f)/(2*sigma);
+ pixels[x] = (uint8_t) (255 * (gaussianIntegral(giX) - gaussianIntegral(giX + span)));
+ }
+ }
+}
+
bool SkBlurMask::BlurRect(SkMask *dst, const SkRect &src,
SkScalar radius, Style style,
SkIPoint *margin, SkMask::CreateMode createMode) {
}
return true;
}
- unsigned int *profile = NULL;
+ uint8_t *profile = NULL;
- compute_profile(sigma, &profile);
- SkAutoTDeleteArray<unsigned int> ada(profile);
+ ComputeBlurProfile(sigma, &profile);
+ SkAutoTDeleteArray<uint8_t> ada(profile);
size_t dstSize = dst->computeImageSize();
if (0 == dstSize) {
int dstHeight = dst->fBounds.height();
int dstWidth = dst->fBounds.width();
- // nearest odd number less than the profile size represents the center
- // of the (2x scaled) profile
- int center = ( profile_size & ~1 ) - 1;
-
- int w = sw - center;
- int h = sh - center;
-
uint8_t *outptr = dp;
SkAutoTMalloc<uint8_t> horizontalScanline(dstWidth);
-
- for (int x = 0 ; x < dstWidth ; ++x) {
- if (profile_size <= sw) {
- horizontalScanline[x] = profile_lookup(profile, x, dstWidth, w);
- } else {
- float span = float(sw)/(2*sigma);
- float giX = 1.5f - (x+.5f)/(2*sigma);
- horizontalScanline[x] = (uint8_t) (255 * (gaussianIntegral(giX) - gaussianIntegral(giX + span)));
- }
- }
+ SkAutoTMalloc<uint8_t> verticalScanline(dstHeight);
+
+ ComputeBlurredScanline(horizontalScanline, profile, dstWidth, sigma);
+ ComputeBlurredScanline(verticalScanline, profile, dstHeight, sigma);
for (int y = 0 ; y < dstHeight ; ++y) {
- unsigned int profile_y;
- if (profile_size <= sh) {
- profile_y = profile_lookup(profile, y, dstHeight, h);
- } else {
- float span = float(sh)/(2*sigma);
- float giY = 1.5f - (y+.5f)/(2*sigma);
- profile_y = (uint8_t) (255 * (gaussianIntegral(giY) - gaussianIntegral(giY + span)));
- }
-
for (int x = 0 ; x < dstWidth ; x++) {
- unsigned int maskval = SkMulDiv255Round(horizontalScanline[x], profile_y);
+ unsigned int maskval = SkMulDiv255Round(horizontalScanline[x], verticalScanline[y]);
*(outptr++) = maskval;
}
}
#if SK_SUPPORT_GPU
#include "GrContext.h"
#include "GrTexture.h"
+#include "GrEffect.h"
+#include "gl/GrGLEffect.h"
#include "effects/GrSimpleTextureEffect.h"
+#include "GrTBackendEffectFactory.h"
#include "SkGrPixelRef.h"
#endif
const SkIRect& clipBounds,
const SkMatrix& ctm,
SkRect* maskRect) const SK_OVERRIDE;
+ virtual bool directFilterMaskGPU(GrContext* context,
+ GrPaint* grp,
+ const SkStrokeRec& strokeRec,
+ const SkPath& path) const SK_OVERRIDE;
+
virtual bool filterMaskGPU(GrTexture* src,
const SkMatrix& ctm,
const SkRect& maskRect,
#if SK_SUPPORT_GPU
+class GrGLRectBlurEffect;
+
+class GrRectBlurEffect : public GrEffect {
+public:
+ virtual ~GrRectBlurEffect();
+
+ static const char* Name() { return "RectBlur"; }
+
+ typedef GrGLRectBlurEffect GLEffect;
+
+ virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
+ virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE;
+
+ /**
+ * Create a simple filter effect with custom bicubic coefficients.
+ */
+ static GrEffectRef* Create(GrContext *context, const SkRect& rect,
+ float sigma) {
+ GrTexture *horizontalScanline, *verticalScanline;
+ bool createdScanlines = CreateScanlineTextures(context, sigma,
+ SkScalarCeilToInt(rect.width()),
+ SkScalarCeilToInt(rect.height()),
+ &horizontalScanline, &verticalScanline);
+ if (!createdScanlines) {
+ return NULL;
+ }
+ AutoEffectUnref effect(SkNEW_ARGS(GrRectBlurEffect, (rect, sigma,
+ horizontalScanline, verticalScanline)));
+ return CreateEffectRef(effect);
+ }
+
+ unsigned int getWidth() const { return fWidth; }
+ unsigned int getHeight() const { return fHeight; }
+ float getSigma() const { return fSigma; }
+
+private:
+ GrRectBlurEffect(const SkRect& rect, float sigma,
+ GrTexture *horizontal_scanline, GrTexture *vertical_scanline);
+ virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE;
+
+ static bool CreateScanlineTextures(GrContext *context, float sigma,
+ unsigned int width, unsigned int height,
+ GrTexture **horizontalScanline,
+ GrTexture **verticalScanline);
+
+ unsigned int fWidth, fHeight;
+ float fSigma;
+ GrTextureAccess fHorizontalScanlineAccess;
+ GrTextureAccess fVerticalScanlineAccess;
+ GrCoordTransform fTransform;
+
+ GR_DECLARE_EFFECT_TEST;
+
+ typedef GrEffect INHERITED;
+};
+
+class GrGLRectBlurEffect : public GrGLEffect {
+public:
+ GrGLRectBlurEffect(const GrBackendEffectFactory& factory,
+ 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:
+ typedef GrGLUniformManager::UniformHandle UniformHandle;
+
+ UniformHandle fWidthUni;
+ UniformHandle fHeightUni;
+
+ typedef GrGLEffect INHERITED;
+};
+
+GrGLRectBlurEffect::GrGLRectBlurEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
+ : INHERITED(factory) {
+}
+
+void GrGLRectBlurEffect::emitCode(GrGLShaderBuilder* builder,
+ const GrDrawEffect&,
+ EffectKey key,
+ const char* outputColor,
+ const char* inputColor,
+ const TransformedCoordsArray& coords,
+ const TextureSamplerArray& samplers) {
+
+ SkString texture_coords = builder->ensureFSCoords2D(coords, 0);
+
+ if (inputColor) {
+ builder->fsCodeAppendf("\tvec4 src=%s;\n", inputColor);
+ } else {
+ builder->fsCodeAppendf("\tvec4 src=vec4(1)\n;");
+ }
+
+ builder->fsCodeAppendf("\tvec4 horiz = ");
+ builder->fsAppendTextureLookup( samplers[0], texture_coords.c_str() );
+ builder->fsCodeAppendf(";\n");
+ builder->fsCodeAppendf("\tvec4 vert = ");
+ builder->fsAppendTextureLookup( samplers[1], texture_coords.c_str() );
+ builder->fsCodeAppendf(";\n");
+
+ builder->fsCodeAppendf("\tfloat final = (horiz*vert).r;\n");
+ builder->fsCodeAppendf("\t%s = final*src;\n", outputColor);
+}
+
+void GrGLRectBlurEffect::setData(const GrGLUniformManager& uman,
+ const GrDrawEffect& drawEffect) {
+}
+
+bool GrRectBlurEffect::CreateScanlineTextures(GrContext *context, float sigma,
+ unsigned int width, unsigned int height,
+ GrTexture **horizontalScanline,
+ GrTexture **verticalScanline) {
+ GrTextureParams params;
+ GrTextureDesc texDesc;
+
+ unsigned int profile_size = SkScalarFloorToInt(6*sigma);
+
+ texDesc.fWidth = width;
+ texDesc.fHeight = 1;
+ texDesc.fConfig = kAlpha_8_GrPixelConfig;
+
+ static const GrCacheID::Domain gBlurProfileDomain = GrCacheID::GenerateDomain();
+ GrCacheID::Key key;
+ memset(&key, 0, sizeof(key));
+ key.fData32[0] = profile_size;
+ key.fData32[1] = width;
+ key.fData32[2] = 1;
+ GrCacheID horizontalCacheID(gBlurProfileDomain, key);
+
+ uint8_t *profile = NULL;
+ SkAutoTDeleteArray<uint8_t> ada(profile);
+
+ *horizontalScanline = context->findAndRefTexture(texDesc, horizontalCacheID, ¶ms);
+
+ if (NULL == *horizontalScanline) {
+
+ SkBlurMask::ComputeBlurProfile(sigma, &profile);
+
+ SkAutoTMalloc<uint8_t> horizontalPixels(width);
+ SkBlurMask::ComputeBlurredScanline(horizontalPixels, profile, width, sigma);
+
+ *horizontalScanline = context->createTexture(¶ms, texDesc, horizontalCacheID,
+ horizontalPixels, 0);
+
+ if (NULL == *horizontalScanline) {
+ return false;
+ }
+ }
+
+ texDesc.fWidth = 1;
+ texDesc.fHeight = height;
+ key.fData32[1] = 1;
+ key.fData32[2] = height;
+ GrCacheID verticalCacheID(gBlurProfileDomain, key);
+
+ *verticalScanline = context->findAndRefTexture(texDesc, verticalCacheID, ¶ms);
+ if (NULL == *verticalScanline) {
+ if (NULL == profile) {
+ SkBlurMask::ComputeBlurProfile(sigma, &profile);
+ }
+
+ SkAutoTMalloc<uint8_t> verticalPixels(height);
+ SkBlurMask::ComputeBlurredScanline(verticalPixels, profile, height, sigma);
+
+ *verticalScanline = context->createTexture(¶ms, texDesc, verticalCacheID,
+ verticalPixels, 0);
+
+ if (NULL == *verticalScanline) {
+ return false;
+ }
+
+ }
+ return true;
+}
+
+GrRectBlurEffect::GrRectBlurEffect(const SkRect& rect, float sigma,
+ GrTexture *horizontal_scanline, GrTexture *vertical_scanline)
+ : INHERITED(),
+ fWidth(horizontal_scanline->width()),
+ fHeight(vertical_scanline->width()),
+ fSigma(sigma),
+ fHorizontalScanlineAccess(horizontal_scanline),
+ fVerticalScanlineAccess(vertical_scanline) {
+ SkMatrix mat;
+ mat.setRectToRect(rect, SkRect::MakeWH(1,1), SkMatrix::kFill_ScaleToFit);
+ fTransform = GrCoordTransform(kLocal_GrCoordSet, mat);
+ this->addTextureAccess(&fHorizontalScanlineAccess);
+ this->addTextureAccess(&fVerticalScanlineAccess);
+ this->addCoordTransform(&fTransform);
+}
+
+GrRectBlurEffect::~GrRectBlurEffect() {
+}
+
+const GrBackendEffectFactory& GrRectBlurEffect::getFactory() const {
+ return GrTBackendEffectFactory<GrRectBlurEffect>::getInstance();
+}
+
+bool GrRectBlurEffect::onIsEqual(const GrEffect& sBase) const {
+ const GrRectBlurEffect& s = CastEffect<GrRectBlurEffect>(sBase);
+ return this->getWidth() == s.getWidth() &&
+ this->getHeight() == s.getHeight() &&
+ this->getSigma() == s.getSigma();
+}
+
+void GrRectBlurEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
+ *validFlags = 0;
+ return;
+}
+
+GR_DEFINE_EFFECT_TEST(GrRectBlurEffect);
+
+GrEffectRef* GrRectBlurEffect::TestCreate(SkRandom* random,
+ GrContext* context,
+ const GrDrawTargetCaps&,
+ GrTexture**) {
+ float sigma = random->nextRangeF(3,8);
+ float width = random->nextRangeF(200,300);
+ float height = random->nextRangeF(200,300);
+ return GrRectBlurEffect::Create(context, SkRect::MakeWH(width, height), sigma);
+}
+
+
+bool SkBlurMaskFilterImpl::directFilterMaskGPU(GrContext* context,
+ GrPaint* grp,
+ const SkStrokeRec& strokeRec,
+ const SkPath& path) const {
+ if (fBlurStyle != SkBlurMaskFilter::kNormal_BlurStyle) {
+ return false;
+ }
+
+ SkRect rect;
+ if (!path.isRect(&rect)) {
+ return false;
+ }
+
+ if (!strokeRec.isFillStyle()) {
+ return false;
+ }
+
+ SkMatrix ctm = context->getMatrix();
+ SkScalar xformedSigma = this->computeXformedSigma(ctm);
+ rect.outset(3*xformedSigma, 3*xformedSigma);
+
+ SkAutoTUnref<GrEffectRef> effect(GrRectBlurEffect::Create(
+ context, rect, xformedSigma));
+ if (!effect) {
+ return false;
+ }
+
+ GrContext::AutoMatrix am;
+ if (!am.setIdentity(context, grp)) {
+ return false;
+ }
+
+
+ grp->addCoverageEffect(effect);
+
+ context->drawRect(*grp, rect);
+ return true;
+}
+
bool SkBlurMaskFilterImpl::canFilterMaskGPU(const SkRect& srcBounds,
const SkIRect& clipBounds,
const SkMatrix& ctm,