2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "GrSWMaskHelper.h"
11 #include "GrContext.h"
12 #include "batches/GrDrawBatch.h"
13 #include "GrDrawContext.h"
14 #include "GrPipelineBuilder.h"
17 #include "SkDistanceFieldGen.h"
19 #include "batches/GrRectBatchFactory.h"
22 * Convert a boolean operation into a transfer mode code
24 static SkBlendMode op_to_mode(SkRegion::Op op) {
26 static const SkBlendMode modeMap[] = {
27 SkBlendMode::kDstOut, // kDifference_Op
28 SkBlendMode::kModulate, // kIntersect_Op
29 SkBlendMode::kSrcOver, // kUnion_Op
30 SkBlendMode::kXor, // kXOR_Op
31 SkBlendMode::kClear, // kReverseDifference_Op
32 SkBlendMode::kSrc, // kReplace_Op
39 * Draw a single rect element of the clip stack into the accumulation bitmap
41 void GrSWMaskHelper::drawRect(const SkRect& rect, SkRegion::Op op,
42 bool antiAlias, uint8_t alpha) {
45 paint.setBlendMode(op_to_mode(op));
46 paint.setAntiAlias(antiAlias);
47 paint.setColor(SkColorSetARGB(alpha, alpha, alpha, alpha));
49 fDraw.drawRect(rect, paint);
53 * Draw a single path element of the clip stack into the accumulation bitmap
55 void GrSWMaskHelper::drawShape(const GrShape& shape, SkRegion::Op op, bool antiAlias,
58 paint.setPathEffect(sk_ref_sp(shape.style().pathEffect()));
59 shape.style().strokeRec().applyToPaint(&paint);
60 paint.setAntiAlias(antiAlias);
64 if (SkRegion::kReplace_Op == op && 0xFF == alpha) {
65 SkASSERT(0xFF == paint.getAlpha());
66 fDraw.drawPathCoverage(path, paint);
68 paint.setBlendMode(op_to_mode(op));
69 paint.setColor(SkColorSetARGB(alpha, alpha, alpha, alpha));
70 fDraw.drawPath(path, paint);
74 bool GrSWMaskHelper::init(const SkIRect& resultBounds, const SkMatrix* matrix) {
78 fMatrix.setIdentity();
81 // Now translate so the bound's UL corner is at the origin
82 fMatrix.postTranslate(-SkIntToScalar(resultBounds.fLeft), -SkIntToScalar(resultBounds.fTop));
83 SkIRect bounds = SkIRect::MakeWH(resultBounds.width(), resultBounds.height());
85 const SkImageInfo bmImageInfo = SkImageInfo::MakeA8(bounds.width(), bounds.height());
86 if (!fPixels.tryAlloc(bmImageInfo)) {
91 sk_bzero(&fDraw, sizeof(fDraw));
93 fRasterClip.setRect(bounds);
94 fDraw.fRC = &fRasterClip;
95 fDraw.fMatrix = &fMatrix;
100 * Get a texture (from the texture cache) of the correct size & format.
102 GrTexture* GrSWMaskHelper::createTexture(TextureType textureType) {
104 desc.fWidth = fPixels.width();
105 desc.fHeight = fPixels.height();
106 desc.fConfig = kAlpha_8_GrPixelConfig;
108 if (TextureType::kApproximateFit == textureType) {
109 return fTexProvider->createApproxTexture(desc);
111 return fTexProvider->createTexture(desc, SkBudgeted::kYes);
116 * Move the result of the software mask generation back to the gpu
118 void GrSWMaskHelper::toTexture(GrTexture *texture) {
119 // Since we're uploading to it, and it's compressed, 'texture' shouldn't
120 // have a render target.
121 SkASSERT(!texture->asRenderTarget());
123 texture->writePixels(0, 0, fPixels.width(), fPixels.height(), texture->config(),
124 fPixels.addr(), fPixels.rowBytes());
129 * Convert mask generation results to a signed distance field
131 void GrSWMaskHelper::toSDF(unsigned char* sdf) {
132 SkGenerateDistanceFieldFromA8Image(sdf, (const unsigned char*)fPixels.addr(),
133 fPixels.width(), fPixels.height(), fPixels.rowBytes());
136 ////////////////////////////////////////////////////////////////////////////////
138 * Software rasterizes shape to A8 mask and uploads the result to a scratch texture. Returns the
139 * resulting texture on success; nullptr on failure.
141 GrTexture* GrSWMaskHelper::DrawShapeMaskToTexture(GrTextureProvider* texProvider,
142 const GrShape& shape,
143 const SkIRect& resultBounds,
145 TextureType textureType,
146 const SkMatrix* matrix) {
147 GrSWMaskHelper helper(texProvider);
149 if (!helper.init(resultBounds, matrix)) {
153 helper.drawShape(shape, SkRegion::kReplace_Op, antiAlias, 0xFF);
155 GrTexture* texture(helper.createTexture(textureType));
160 helper.toTexture(texture);
165 void GrSWMaskHelper::DrawToTargetWithShapeMask(GrTexture* texture,
166 GrDrawContext* drawContext,
167 const GrPaint& paint,
168 const GrUserStencilSettings& userStencilSettings,
170 const SkMatrix& viewMatrix,
171 const SkIPoint& textureOriginInDeviceSpace,
172 const SkIRect& deviceSpaceRectToDraw) {
174 if (!viewMatrix.invert(&invert)) {
178 SkRect dstRect = SkRect::Make(deviceSpaceRectToDraw);
180 // We use device coords to compute the texture coordinates. We take the device coords and apply
181 // a translation so that the top-left of the device bounds maps to 0,0, and then a scaling
182 // matrix to normalized coords.
184 maskMatrix.setIDiv(texture->width(), texture->height());
185 maskMatrix.preTranslate(SkIntToScalar(-textureOriginInDeviceSpace.fX),
186 SkIntToScalar(-textureOriginInDeviceSpace.fY));
188 GrPipelineBuilder pipelineBuilder(paint, drawContext->mustUseHWAA(paint));
189 pipelineBuilder.setUserStencil(&userStencilSettings);
191 pipelineBuilder.addCoverageFragmentProcessor(
192 GrSimpleTextureEffect::Make(texture,
195 GrTextureParams::kNone_FilterMode,
196 kDevice_GrCoordSet));
198 SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill(paint.getColor(),
200 dstRect, nullptr, &invert));
201 drawContext->drawBatch(pipelineBuilder, clip, batch);