blob: 69c254f94eab77123df1706c16903da025e9e0bc [file] [log] [blame]
robertphillips@google.com58b20212012-06-27 20:44:52 +00001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrSWMaskHelper.h"
Brian Osmanf9810662017-08-30 10:02:10 -04009
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040010#include "include/gpu/GrRecordingContext.h"
Brian Osman9aaec362020-05-08 14:54:37 -040011#include "src/core/SkMatrixProvider.h"
Adlai Holler990a0d82021-02-05 13:40:51 -050012#include "src/core/SkTaskGroup.h"
Greg Daniel6f5441a2020-01-28 17:02:49 -050013#include "src/gpu/GrBitmapTextureMaker.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/GrCaps.h"
Adlai Holler990a0d82021-02-05 13:40:51 -050015#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/GrProxyProvider.h"
17#include "src/gpu/GrRecordingContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrSurfaceContext.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040019#include "src/gpu/GrTextureProxy.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000020#include "src/gpu/geometry/GrStyledShape.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000021
robertphillips@google.com58b20212012-06-27 20:44:52 +000022/*
23 * Convert a boolean operation into a transfer mode code
24 */
reed374772b2016-10-05 17:33:02 -070025static SkBlendMode op_to_mode(SkRegion::Op op) {
robertphillips@google.com58b20212012-06-27 20:44:52 +000026
reed374772b2016-10-05 17:33:02 -070027 static const SkBlendMode modeMap[] = {
28 SkBlendMode::kDstOut, // kDifference_Op
29 SkBlendMode::kModulate, // kIntersect_Op
30 SkBlendMode::kSrcOver, // kUnion_Op
31 SkBlendMode::kXor, // kXOR_Op
32 SkBlendMode::kClear, // kReverseDifference_Op
33 SkBlendMode::kSrc, // kReplace_Op
robertphillips@google.com58b20212012-06-27 20:44:52 +000034 };
35
36 return modeMap[op];
37}
38
Michael Ludwig4fc7c5f2020-05-04 14:51:43 -040039static SkPaint get_paint(SkRegion::Op op, GrAA aa, uint8_t alpha) {
40 SkPaint paint;
41 paint.setBlendMode(op_to_mode(op));
42 paint.setAntiAlias(GrAA::kYes == aa);
43 // SkPaint's color is unpremul so this will produce alpha in every channel.
44 paint.setColor(SkColorSetARGB(alpha, 255, 255, 255));
45 return paint;
46}
47
Adlai Holler990a0d82021-02-05 13:40:51 -050048GrSurfaceProxyView GrSWMaskHelper::MakeTexture(SkIRect bounds,
49 GrRecordingContext* context,
50 SkBackingFit fit,
51 DrawFunc&& draw) {
52 SkTaskGroup* taskGroup = nullptr;
53 if (auto dContext = context->asDirectContext()) {
54 taskGroup = dContext->priv().getTaskGroup();
55 }
56 if (taskGroup) {
57 GrSWMaskHelper* helper = new GrSWMaskHelper(bounds);
58 return helper->threadedExecute(taskGroup, context, fit, std::move(draw));
59 } else {
60 GrSWMaskHelper helper(bounds);
61 return helper.nonThreadedExecute(context, fit, draw);
62 }
63}
64
65GrSurfaceProxyView GrSWMaskHelper::threadedExecute(SkTaskGroup* taskGroup,
66 GrRecordingContext* context,
67 SkBackingFit fit,
68 DrawFunc&& draw) {
69 sk_sp<GrSWMaskHelper> spThis(this);
70 taskGroup->add([spThis, draw{std::move(draw)}]() {
71 if (spThis->allocate()) {
72 draw(spThis.get());
73 spThis->fBitmap.setImmutable();
74 }
75 spThis->fSemaphore.signal();
76 });
77 auto lazy_cb = [spThis{std::move(spThis)}](GrResourceProvider* provider,
78 const GrProxyProvider::LazySurfaceDesc& desc) {
79 spThis->fSemaphore.wait();
80 const SkBitmap& mask = spThis->fBitmap;
81 if (!mask.getPixels()) {
82 return GrProxyProvider::LazyCallbackResult();
83 }
84 GrMipLevel mip{mask.getPixels(), mask.pixmap().rowBytes()};
85 GrColorType ct{SkColorTypeToGrColorType(mask.colorType())};
86 sk_sp<GrTexture> tex = provider->createTexture(desc.fDimensions,
87 desc.fFormat,
88 ct,
89 desc.fRenderable,
90 desc.fSampleCnt,
91 desc.fBudgeted,
92 desc.fFit,
93 desc.fProtected,
94 mip);
Adlai Holler863497b2021-02-09 17:42:04 +000095 GrProxyProvider::LazyCallbackResult result(std::move(tex));
96 // Callback refs us, we own bitmap, don't release callback.
97 result.fReleaseCallback = false;
98 return result;
Adlai Holler990a0d82021-02-05 13:40:51 -050099 };
100 const GrCaps* caps = context->priv().caps();
101 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
102
103 GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kAlpha_8,
104 GrRenderable::kNo);
105 GrSwizzle swizzle = caps->getReadSwizzle(format, GrColorType::kAlpha_8);
106 sk_sp<GrTextureProxy> p = proxyProvider->createLazyProxy(std::move(lazy_cb),
107 format,
108 fBitmap.dimensions(),
109 GrMipMapped::kNo,
110 GrMipmapStatus::kNotAllocated,
111 GrInternalSurfaceFlags::kNone,
112 fit,
113 SkBudgeted::kYes,
114 GrProtected::kNo,
115 GrProxyProvider::UseAllocator::kYes);
116 return GrSurfaceProxyView(std::move(p), kTopLeft_GrSurfaceOrigin, swizzle);
117}
118
119GrSurfaceProxyView GrSWMaskHelper::nonThreadedExecute(GrRecordingContext* context,
120 SkBackingFit fit,
121 const DrawFunc& draw) {
122 if (!this->allocate()) {
123 return {};
124 }
125 draw(this);
126 fBitmap.setImmutable();
127 GrBitmapTextureMaker maker(context, fBitmap, fit);
128 return maker.view(GrMipmapped::kNo);
129}
130
131GrSWMaskHelper::GrSWMaskHelper(const SkIRect& resultBounds) {
132 // We will need to translate draws so the bound's UL corner is at the origin
133 fTranslate = {-SkIntToScalar(resultBounds.fLeft), -SkIntToScalar(resultBounds.fTop)};
134 SkIRect bounds = SkIRect::MakeWH(resultBounds.width(), resultBounds.height());
135 fBitmap.setInfo(SkImageInfo::MakeA8(bounds.width(), bounds.height()));
136}
137
robertphillips@google.com58b20212012-06-27 20:44:52 +0000138/**
139 * Draw a single rect element of the clip stack into the accumulation bitmap
140 */
Brian Salomon74077562017-08-30 13:55:35 -0400141void GrSWMaskHelper::drawRect(const SkRect& rect, const SkMatrix& matrix, SkRegion::Op op, GrAA aa,
142 uint8_t alpha) {
Brian Salomon74077562017-08-30 13:55:35 -0400143 SkMatrix translatedMatrix = matrix;
144 translatedMatrix.postTranslate(fTranslate.fX, fTranslate.fY);
Brian Osman9aaec362020-05-08 14:54:37 -0400145 SkSimpleMatrixProvider matrixProvider(translatedMatrix);
146 fDraw.fMatrixProvider = &matrixProvider;
Brian Salomon74077562017-08-30 13:55:35 -0400147
Michael Ludwig4fc7c5f2020-05-04 14:51:43 -0400148 fDraw.drawRect(rect, get_paint(op, aa, alpha));
149}
150
151void GrSWMaskHelper::drawRRect(const SkRRect& rrect, const SkMatrix& matrix, SkRegion::Op op,
152 GrAA aa, uint8_t alpha) {
153 SkMatrix translatedMatrix = matrix;
154 translatedMatrix.postTranslate(fTranslate.fX, fTranslate.fY);
Brian Osman9aaec362020-05-08 14:54:37 -0400155 SkSimpleMatrixProvider matrixProvider(translatedMatrix);
156 fDraw.fMatrixProvider = &matrixProvider;
Michael Ludwig4fc7c5f2020-05-04 14:51:43 -0400157
158 fDraw.drawRRect(rrect, get_paint(op, aa, alpha));
robertphillips@google.com58b20212012-06-27 20:44:52 +0000159}
160
161/**
162 * Draw a single path element of the clip stack into the accumulation bitmap
163 */
Michael Ludwig2686d692020-04-17 20:21:37 +0000164void GrSWMaskHelper::drawShape(const GrStyledShape& shape, const SkMatrix& matrix, SkRegion::Op op,
Brian Salomon74077562017-08-30 13:55:35 -0400165 GrAA aa, uint8_t alpha) {
Michael Ludwig4fc7c5f2020-05-04 14:51:43 -0400166 SkPaint paint = get_paint(op, aa, alpha);
Robert Phillipsf809c1e2017-01-13 11:02:42 -0500167 paint.setPathEffect(shape.style().refPathEffect());
bsalomon8acedde2016-06-24 10:42:16 -0700168 shape.style().strokeRec().applyToPaint(&paint);
reed@google.com84e922b2013-11-04 20:57:36 +0000169
Brian Salomon74077562017-08-30 13:55:35 -0400170 SkMatrix translatedMatrix = matrix;
171 translatedMatrix.postTranslate(fTranslate.fX, fTranslate.fY);
Brian Osman9aaec362020-05-08 14:54:37 -0400172 SkSimpleMatrixProvider matrixProvider(translatedMatrix);
173 fDraw.fMatrixProvider = &matrixProvider;
Brian Salomon74077562017-08-30 13:55:35 -0400174
bsalomon8acedde2016-06-24 10:42:16 -0700175 SkPath path;
176 shape.asPath(&path);
reed@google.com126f7f52013-11-07 16:06:53 +0000177 if (SkRegion::kReplace_Op == op && 0xFF == alpha) {
178 SkASSERT(0xFF == paint.getAlpha());
robertphillips98377402016-05-13 05:47:23 -0700179 fDraw.drawPathCoverage(path, paint);
reed@google.com126f7f52013-11-07 16:06:53 +0000180 } else {
robertphillips98377402016-05-13 05:47:23 -0700181 fDraw.drawPath(path, paint);
reed@google.com126f7f52013-11-07 16:06:53 +0000182 }
Michael Ludwig4fc7c5f2020-05-04 14:51:43 -0400183}
184
185void GrSWMaskHelper::drawShape(const GrShape& shape, const SkMatrix& matrix, SkRegion::Op op,
186 GrAA aa, uint8_t alpha) {
187 SkPaint paint = get_paint(op, aa, alpha);
188
189 SkMatrix translatedMatrix = matrix;
190 translatedMatrix.postTranslate(fTranslate.fX, fTranslate.fY);
Brian Osman9aaec362020-05-08 14:54:37 -0400191 SkSimpleMatrixProvider matrixProvider(translatedMatrix);
192 fDraw.fMatrixProvider = &matrixProvider;
Michael Ludwig4fc7c5f2020-05-04 14:51:43 -0400193
194 if (shape.inverted()) {
195 if (shape.isEmpty() || shape.isLine() || shape.isPoint()) {
196 // These shapes are empty for simple fills, so when inverted, cover everything
197 fDraw.drawPaint(paint);
198 return;
199 }
200 // Else fall through to the draw method using asPath(), which will toggle fill type properly
201 } else if (shape.isEmpty() || shape.isLine() || shape.isPoint()) {
202 // Do nothing, these shapes do not cover any pixels for simple fills
203 return;
204 } else if (shape.isRect()) {
205 fDraw.drawRect(shape.rect(), paint);
206 return;
207 } else if (shape.isRRect()) {
208 fDraw.drawRRect(shape.rrect(), paint);
209 return;
210 }
211
212 // A complex, or inverse-filled shape, so go through drawPath.
213 SkPath path;
214 shape.asPath(&path);
215 if (SkRegion::kReplace_Op == op && 0xFF == alpha) {
216 SkASSERT(0xFF == paint.getAlpha());
217 fDraw.drawPathCoverage(path, paint);
218 } else {
219 fDraw.drawPath(path, paint);
220 }
221}
robertphillips@google.com58b20212012-06-27 20:44:52 +0000222
Adlai Holler990a0d82021-02-05 13:40:51 -0500223bool GrSWMaskHelper::allocate() {
224 if (!fBitmap.tryAllocPixels()) {
225 SkDEBUGFAIL("Unable to allocate SW mask.");
robertphillips98377402016-05-13 05:47:23 -0700226 return false;
krajcevskib8ccc2f2014-08-07 08:15:14 -0700227 }
krajcevskib8ccc2f2014-08-07 08:15:14 -0700228
Adlai Holler990a0d82021-02-05 13:40:51 -0500229 fDraw.fDst = fBitmap.pixmap();
230 fRasterClip.setRect(fBitmap.info().bounds());
reed41e010c2015-06-09 12:16:53 -0700231 fDraw.fRC = &fRasterClip;
robertphillips@google.com58b20212012-06-27 20:44:52 +0000232 return true;
233}