Fixed flattening of SkStippleMaskFilter

http://codereview.appspot.com/6279052/



git-svn-id: http://skia.googlecode.com/svn/trunk@4154 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
robertphillips@google.com 2012-06-05 12:55:05 +00:00
parent 8fc5e47a2d
commit 941ee9303b
5 changed files with 93 additions and 70 deletions

View File

@ -8,79 +8,10 @@
#include "gm.h"
#include "SkCanvas.h"
#include "SkShader.h"
#include "SkMaskFilter.h"
#include "SkStippleMaskFilter.h"
namespace skiagm {
/**
* Simple MaskFilter that creates a screen door stipple pattern
*/
class SkStippleMaskFilter : public SkMaskFilter {
public:
SkStippleMaskFilter() : INHERITED() {
}
virtual ~SkStippleMaskFilter() {
}
virtual bool filterMask(SkMask* dst, const SkMask& src, const SkMatrix&,
SkIPoint* margin) SK_OVERRIDE {
if (src.fFormat != SkMask::kA8_Format) {
return false;
}
dst->fBounds = src.fBounds;
dst->fRowBytes = dst->fBounds.width();
dst->fFormat = SkMask::kA8_Format;
dst->fImage = NULL;
if (NULL != src.fImage) {
size_t dstSize = dst->computeImageSize();
if (0 == dstSize) {
return false; // too big to allocate, abort
}
dst->fImage = SkMask::AllocImage(dstSize);
uint8_t* srcScanLine = src.fImage;
uint8_t* scanline = dst->fImage;
for (int y = 0; y < src.fBounds.height(); ++y) {
for (int x = 0; x < src.fBounds.width(); ++x) {
SkASSERT(size_t(scanline - dst->fImage) < dstSize);
scanline[x] = srcScanLine[x] && ((x+y) % 2) ? 0xFF : 0x00;
}
scanline += dst->fRowBytes;
srcScanLine += src.fRowBytes;
}
}
return true;
}
static SkFlattenable* CreateProc(SkFlattenableReadBuffer& buffer) {
return SkNEW(SkStippleMaskFilter);
}
virtual void flatten(SkFlattenableWriteBuffer& buffer) const SK_OVERRIDE {}
// getFactory is from SkFlattenable
virtual Factory getFactory() SK_OVERRIDE {
return CreateProc;
}
// getFormat is from SkMaskFilter
virtual SkMask::Format getFormat() SK_OVERRIDE {
return SkMask::kA8_Format;
}
protected:
private:
typedef SkMaskFilter INHERITED;
};
/**
* Stress test the samplers by rendering a textured glyph with a mask and
* an AA clip

View File

@ -33,6 +33,7 @@
'../include/effects/SkPixelXorXfermode.h',
'../include/effects/SkPorterDuff.h',
'../include/effects/SkRectShape.h',
'../include/effects/SkStippleMaskFilter.h',
'../include/effects/SkTableColorFilter.h',
'../include/effects/SkTableMaskFilter.h',
'../include/effects/SkTransparentShader.h',
@ -71,6 +72,7 @@
'../src/effects/SkPorterDuff.cpp',
'../src/effects/SkRadialGradient_Table.h',
'../src/effects/SkRectShape.cpp',
'../src/effects/SkStippleMaskFilter.cpp',
'../src/effects/SkTableColorFilter.cpp',
'../src/effects/SkTableMaskFilter.cpp',
'../src/effects/SkTestImageFilters.cpp',

View File

@ -0,0 +1,41 @@
/*
* Copyright 2012 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SkStippleMaskFilter_DEFINED
#define SkStippleMaskFilter_DEFINED
#include "SkMaskFilter.h"
/**
* Simple MaskFilter that creates a screen door stipple pattern
*/
class SkStippleMaskFilter : public SkMaskFilter {
public:
SkStippleMaskFilter() : INHERITED() {
}
virtual bool filterMask(SkMask* dst, const SkMask& src,
const SkMatrix& matrix,
SkIPoint* margin) SK_OVERRIDE;
// getFormat is from SkMaskFilter
virtual SkMask::Format getFormat() SK_OVERRIDE {
return SkMask::kA8_Format;
}
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkStippleMaskFilter);
protected:
SkStippleMaskFilter::SkStippleMaskFilter(SkFlattenableReadBuffer& buffer)
: SkMaskFilter(buffer) {
}
private:
typedef SkMaskFilter INHERITED;
};
#endif // SkStippleMaskFilter_DEFINED

View File

@ -0,0 +1,48 @@
/*
* Copyright 2012 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkStippleMaskFilter.h"
bool SkStippleMaskFilter::filterMask(SkMask* dst,
const SkMask& src,
const SkMatrix& matrix,
SkIPoint* margin) {
if (src.fFormat != SkMask::kA8_Format) {
return false;
}
dst->fBounds = src.fBounds;
dst->fRowBytes = dst->fBounds.width();
dst->fFormat = SkMask::kA8_Format;
dst->fImage = NULL;
if (NULL != src.fImage) {
size_t dstSize = dst->computeImageSize();
if (0 == dstSize) {
return false; // too big to allocate, abort
}
dst->fImage = SkMask::AllocImage(dstSize);
uint8_t* srcScanLine = src.fImage;
uint8_t* scanline = dst->fImage;
for (int y = 0; y < src.fBounds.height(); ++y) {
for (int x = 0; x < src.fBounds.width(); ++x) {
scanline[x] = srcScanLine[x] && ((x+y) & 0x1) ? 0xFF : 0x00;
}
scanline += dst->fRowBytes;
srcScanLine += src.fRowBytes;
}
}
return true;
}
SK_DEFINE_FLATTENABLE_REGISTRAR(SkStippleMaskFilter)

View File

@ -68,6 +68,7 @@ void SkFlattenable::InitializeFlattenables() {
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkPath2DPathEffect)
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkPixelXorXfermode)
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkRectShape)
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkStippleMaskFilter)
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSumPathEffect)
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkShape)