Line data Source code
1 : /*
2 : * Copyright 2012 Google Inc.
3 : *
4 : * Use of this source code is governed by a BSD-style license that can be
5 : * found in the LICENSE file.
6 : */
7 :
8 : #include "GrSWMaskHelper.h"
9 :
10 : #include "GrCaps.h"
11 : #include "GrContext.h"
12 : #include "GrContextPriv.h"
13 : #include "GrPipelineBuilder.h"
14 : #include "GrRenderTargetContext.h"
15 : #include "GrShape.h"
16 : #include "GrSurfaceContext.h"
17 : #include "GrTextureProxy.h"
18 : #include "ops/GrDrawOp.h"
19 :
20 : #include "SkDistanceFieldGen.h"
21 :
22 : #include "ops/GrRectOpFactory.h"
23 :
24 : /*
25 : * Convert a boolean operation into a transfer mode code
26 : */
27 0 : static SkBlendMode op_to_mode(SkRegion::Op op) {
28 :
29 : static const SkBlendMode modeMap[] = {
30 : SkBlendMode::kDstOut, // kDifference_Op
31 : SkBlendMode::kModulate, // kIntersect_Op
32 : SkBlendMode::kSrcOver, // kUnion_Op
33 : SkBlendMode::kXor, // kXOR_Op
34 : SkBlendMode::kClear, // kReverseDifference_Op
35 : SkBlendMode::kSrc, // kReplace_Op
36 : };
37 :
38 0 : return modeMap[op];
39 : }
40 :
41 : /**
42 : * Draw a single rect element of the clip stack into the accumulation bitmap
43 : */
44 0 : void GrSWMaskHelper::drawRect(const SkRect& rect, SkRegion::Op op, GrAA aa, uint8_t alpha) {
45 0 : SkPaint paint;
46 :
47 0 : paint.setBlendMode(op_to_mode(op));
48 0 : paint.setAntiAlias(GrAA::kYes == aa);
49 0 : paint.setColor(SkColorSetARGB(alpha, alpha, alpha, alpha));
50 :
51 0 : fDraw.drawRect(rect, paint);
52 0 : }
53 :
54 : /**
55 : * Draw a single path element of the clip stack into the accumulation bitmap
56 : */
57 0 : void GrSWMaskHelper::drawShape(const GrShape& shape, SkRegion::Op op, GrAA aa, uint8_t alpha) {
58 0 : SkPaint paint;
59 0 : paint.setPathEffect(shape.style().refPathEffect());
60 0 : shape.style().strokeRec().applyToPaint(&paint);
61 0 : paint.setAntiAlias(GrAA::kYes == aa);
62 :
63 0 : SkPath path;
64 0 : shape.asPath(&path);
65 0 : if (SkRegion::kReplace_Op == op && 0xFF == alpha) {
66 0 : SkASSERT(0xFF == paint.getAlpha());
67 0 : fDraw.drawPathCoverage(path, paint);
68 : } else {
69 0 : paint.setBlendMode(op_to_mode(op));
70 0 : paint.setColor(SkColorSetARGB(alpha, alpha, alpha, alpha));
71 0 : fDraw.drawPath(path, paint);
72 : }
73 0 : }
74 :
75 0 : bool GrSWMaskHelper::init(const SkIRect& resultBounds, const SkMatrix* matrix) {
76 0 : if (matrix) {
77 0 : fMatrix = *matrix;
78 : } else {
79 0 : fMatrix.setIdentity();
80 : }
81 :
82 : // Now translate so the bound's UL corner is at the origin
83 0 : fMatrix.postTranslate(-SkIntToScalar(resultBounds.fLeft), -SkIntToScalar(resultBounds.fTop));
84 0 : SkIRect bounds = SkIRect::MakeWH(resultBounds.width(), resultBounds.height());
85 :
86 0 : const SkImageInfo bmImageInfo = SkImageInfo::MakeA8(bounds.width(), bounds.height());
87 0 : if (!fPixels.tryAlloc(bmImageInfo)) {
88 0 : return false;
89 : }
90 0 : fPixels.erase(0);
91 :
92 0 : sk_bzero(&fDraw, sizeof(fDraw));
93 0 : fDraw.fDst = fPixels;
94 0 : fRasterClip.setRect(bounds);
95 0 : fDraw.fRC = &fRasterClip;
96 0 : fDraw.fMatrix = &fMatrix;
97 0 : return true;
98 : }
99 :
100 0 : sk_sp<GrTextureProxy> GrSWMaskHelper::toTextureProxy(GrContext* context, SkBackingFit fit) {
101 0 : GrSurfaceDesc desc;
102 0 : desc.fOrigin = kTopLeft_GrSurfaceOrigin;
103 0 : desc.fWidth = fPixels.width();
104 0 : desc.fHeight = fPixels.height();
105 0 : desc.fConfig = kAlpha_8_GrPixelConfig;
106 :
107 0 : sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeDeferredSurfaceContext(
108 : desc,
109 : fit,
110 0 : SkBudgeted::kYes);
111 0 : if (!sContext || !sContext->asTextureProxy()) {
112 0 : return nullptr;
113 : }
114 :
115 0 : SkImageInfo ii = SkImageInfo::MakeA8(desc.fWidth, desc.fHeight);
116 0 : if (!sContext->writePixels(ii, fPixels.addr(), fPixels.rowBytes(), 0, 0)) {
117 0 : return nullptr;
118 : }
119 :
120 0 : return sContext->asTextureProxyRef();
121 : }
122 :
123 : /**
124 : * Convert mask generation results to a signed distance field
125 : */
126 0 : void GrSWMaskHelper::toSDF(unsigned char* sdf) {
127 0 : SkGenerateDistanceFieldFromA8Image(sdf, (const unsigned char*)fPixels.addr(),
128 0 : fPixels.width(), fPixels.height(), fPixels.rowBytes());
129 0 : }
130 :
131 : ////////////////////////////////////////////////////////////////////////////////
132 : /**
133 : * Software rasterizes shape to A8 mask and uploads the result to a scratch texture. Returns the
134 : * resulting texture on success; nullptr on failure.
135 : */
136 0 : sk_sp<GrTextureProxy> GrSWMaskHelper::DrawShapeMaskToTexture(GrContext* context,
137 : const GrShape& shape,
138 : const SkIRect& resultBounds,
139 : GrAA aa,
140 : SkBackingFit fit,
141 : const SkMatrix* matrix) {
142 0 : GrSWMaskHelper helper;
143 :
144 0 : if (!helper.init(resultBounds, matrix)) {
145 0 : return nullptr;
146 : }
147 :
148 0 : helper.drawShape(shape, SkRegion::kReplace_Op, aa, 0xFF);
149 :
150 0 : return helper.toTextureProxy(context, fit);
151 : }
152 :
153 0 : void GrSWMaskHelper::DrawToTargetWithShapeMask(sk_sp<GrTextureProxy> proxy,
154 : GrRenderTargetContext* renderTargetContext,
155 : GrPaint&& paint,
156 : const GrUserStencilSettings& userStencilSettings,
157 : const GrClip& clip,
158 : const SkMatrix& viewMatrix,
159 : const SkIPoint& textureOriginInDeviceSpace,
160 : const SkIRect& deviceSpaceRectToDraw) {
161 : SkMatrix invert;
162 0 : if (!viewMatrix.invert(&invert)) {
163 0 : return;
164 : }
165 :
166 0 : GrResourceProvider* resourceProvider = renderTargetContext->resourceProvider();
167 :
168 0 : SkRect dstRect = SkRect::Make(deviceSpaceRectToDraw);
169 :
170 : // We use device coords to compute the texture coordinates. We take the device coords and apply
171 : // a translation so that the top-left of the device bounds maps to 0,0, and then a scaling
172 : // matrix to normalized coords.
173 0 : SkMatrix maskMatrix = SkMatrix::MakeTrans(SkIntToScalar(-textureOriginInDeviceSpace.fX),
174 0 : SkIntToScalar(-textureOriginInDeviceSpace.fY));
175 0 : maskMatrix.preConcat(viewMatrix);
176 : std::unique_ptr<GrLegacyMeshDrawOp> op = GrRectOpFactory::MakeNonAAFill(
177 0 : paint.getColor(), SkMatrix::I(), dstRect, nullptr, &invert);
178 0 : paint.addCoverageFragmentProcessor(GrSimpleTextureEffect::Make(
179 0 : resourceProvider, std::move(proxy), nullptr, maskMatrix,
180 0 : GrSamplerParams::kNone_FilterMode));
181 0 : GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
182 0 : pipelineBuilder.setUserStencil(&userStencilSettings);
183 0 : renderTargetContext->addLegacyMeshDrawOp(std::move(pipelineBuilder), clip, std::move(op));
184 : }
|