Created SW clip mask creation helper class (in GrSoftwarePathRenderer)

http://codereview.appspot.com/6198070/



git-svn-id: http://skia.googlecode.com/svn/trunk@3925 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
robertphillips@google.com 2012-05-14 17:37:05 +00:00
parent 4debcac8c3
commit 6f31a3b7d7

View File

@ -94,19 +94,21 @@ bool get_path_and_clip_bounds(const GrDrawTarget* target,
return true;
}
////////////////////////////////////////////////////////////////////////////////
/**
* sw rasterizes path to A8 mask using the context's matrix and uploads to a
* scratch texture.
* The GrSWMaskHelper helps generate clip masks using the software rendering
* path.
*/
class GrSWMaskHelper : public GrNoncopyable {
public:
GrSWMaskHelper(GrContext* context)
: fContext(context) {
bool sw_draw_path_to_mask_texture(const SkPath& clientPath,
const GrIRect& pathDevBounds,
GrPathFill fill,
GrContext* context,
const GrPoint* translate,
GrAutoScratchTexture* tex,
bool antiAlias) {
}
/**
* Draw a single element of the clip stack into the accumulation bitmap
*/
void draw(const SkPath& clientPath, GrPathFill fill, bool antiAlias) {
SkPaint paint;
SkPath tmpPath;
const SkPath* pathToDraw = &clientPath;
@ -125,49 +127,94 @@ bool sw_draw_path_to_mask_texture(const SkPath& clientPath,
paint.setAntiAlias(antiAlias);
paint.setColor(SK_ColorWHITE);
GrMatrix matrix = context->getMatrix();
if (NULL != translate) {
matrix.postTranslate(translate->fX, translate->fY);
fDraw.drawPath(*pathToDraw, paint);
}
matrix.postTranslate(-pathDevBounds.fLeft * SK_Scalar1,
bool init(const GrIRect& pathDevBounds, const GrPoint* translate) {
fMatrix = fContext->getMatrix();
if (NULL != translate) {
fMatrix.postTranslate(translate->fX, translate->fY);
}
fMatrix.postTranslate(-pathDevBounds.fLeft * SK_Scalar1,
-pathDevBounds.fTop * SK_Scalar1);
GrIRect bounds = GrIRect::MakeWH(pathDevBounds.width(),
pathDevBounds.height());
SkBitmap bm;
bm.setConfig(SkBitmap::kA8_Config, bounds.fRight, bounds.fBottom);
if (!bm.allocPixels()) {
fBM.setConfig(SkBitmap::kA8_Config, bounds.fRight, bounds.fBottom);
if (!fBM.allocPixels()) {
return false;
}
sk_bzero(bm.getPixels(), bm.getSafeSize());
sk_bzero(fBM.getPixels(), fBM.getSafeSize());
SkDraw draw;
sk_bzero(&draw, sizeof(draw));
SkRasterClip rc(bounds);
draw.fRC = &rc;
draw.fClip = &rc.bwRgn();
draw.fMatrix = &matrix;
draw.fBitmap = &bm;
draw.drawPath(*pathToDraw, paint);
sk_bzero(&fDraw, sizeof(fDraw));
fRasterClip.setRect(bounds);
fDraw.fRC = &fRasterClip;
fDraw.fClip = &fRasterClip.bwRgn();
fDraw.fMatrix = &fMatrix;
fDraw.fBitmap = &fBM;
return true;
}
/**
* Move the result of the software mask generation back to the gpu
*/
bool toTexture(GrAutoScratchTexture* tex) {
const GrTextureDesc desc = {
kNone_GrTextureFlags,
bounds.fRight,
bounds.fBottom,
fBM.width(),
fBM.height(),
kAlpha_8_GrPixelConfig,
0 // samples
};
tex->set(context, desc);
tex->set(fContext, desc);
GrTexture* texture = tex->texture();
if (NULL == texture) {
return false;
}
SkAutoLockPixels alp(bm);
SkAutoLockPixels alp(fBM);
texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bm.getPixels(), bm.rowBytes());
fBM.getPixels(), fBM.rowBytes());
return true;
}
protected:
private:
GrContext* fContext;
GrMatrix fMatrix;
SkBitmap fBM;
SkDraw fDraw;
SkRasterClip fRasterClip;
typedef GrPathRenderer INHERITED;
};
////////////////////////////////////////////////////////////////////////////////
/**
* sw rasterizes path to A8 mask using the context's matrix and uploads to a
* scratch texture.
*/
bool sw_draw_path_to_mask_texture(const SkPath& clientPath,
const GrIRect& pathDevBounds,
GrPathFill fill,
GrContext* context,
const GrPoint* translate,
GrAutoScratchTexture* tex,
bool antiAlias) {
GrSWMaskHelper helper(context);
if (!helper.init(pathDevBounds, translate)) {
return false;
}
helper.draw(clientPath, fill, antiAlias);
if (!helper.toTexture(tex)) {
return false;
}
return true;
}