blob: 2117df4bf5fb4c1c22cba168097d40b470336807 [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"
Greg Daniel6f5441a2020-01-28 17:02:49 -050012#include "src/gpu/GrBitmapTextureMaker.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/GrCaps.h"
14#include "src/gpu/GrProxyProvider.h"
15#include "src/gpu/GrRecordingContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/GrSurfaceContext.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040017#include "src/gpu/GrTextureProxy.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000018#include "src/gpu/geometry/GrStyledShape.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000019
robertphillips@google.com58b20212012-06-27 20:44:52 +000020/*
21 * Convert a boolean operation into a transfer mode code
22 */
reed374772b2016-10-05 17:33:02 -070023static SkBlendMode op_to_mode(SkRegion::Op op) {
robertphillips@google.com58b20212012-06-27 20:44:52 +000024
reed374772b2016-10-05 17:33:02 -070025 static const SkBlendMode modeMap[] = {
26 SkBlendMode::kDstOut, // kDifference_Op
27 SkBlendMode::kModulate, // kIntersect_Op
28 SkBlendMode::kSrcOver, // kUnion_Op
29 SkBlendMode::kXor, // kXOR_Op
30 SkBlendMode::kClear, // kReverseDifference_Op
31 SkBlendMode::kSrc, // kReplace_Op
robertphillips@google.com58b20212012-06-27 20:44:52 +000032 };
33
34 return modeMap[op];
35}
36
Michael Ludwig4fc7c5f2020-05-04 14:51:43 -040037static SkPaint get_paint(SkRegion::Op op, GrAA aa, uint8_t alpha) {
38 SkPaint paint;
39 paint.setBlendMode(op_to_mode(op));
40 paint.setAntiAlias(GrAA::kYes == aa);
41 // SkPaint's color is unpremul so this will produce alpha in every channel.
42 paint.setColor(SkColorSetARGB(alpha, 255, 255, 255));
43 return paint;
44}
45
robertphillips@google.com58b20212012-06-27 20:44:52 +000046/**
47 * Draw a single rect element of the clip stack into the accumulation bitmap
48 */
Brian Salomon74077562017-08-30 13:55:35 -040049void GrSWMaskHelper::drawRect(const SkRect& rect, const SkMatrix& matrix, SkRegion::Op op, GrAA aa,
50 uint8_t alpha) {
Brian Salomon74077562017-08-30 13:55:35 -040051 SkMatrix translatedMatrix = matrix;
52 translatedMatrix.postTranslate(fTranslate.fX, fTranslate.fY);
Brian Osman9aaec362020-05-08 14:54:37 -040053 SkSimpleMatrixProvider matrixProvider(translatedMatrix);
54 fDraw.fMatrixProvider = &matrixProvider;
Brian Salomon74077562017-08-30 13:55:35 -040055
Michael Ludwig4fc7c5f2020-05-04 14:51:43 -040056 fDraw.drawRect(rect, get_paint(op, aa, alpha));
57}
58
59void GrSWMaskHelper::drawRRect(const SkRRect& rrect, const SkMatrix& matrix, SkRegion::Op op,
60 GrAA aa, uint8_t alpha) {
61 SkMatrix translatedMatrix = matrix;
62 translatedMatrix.postTranslate(fTranslate.fX, fTranslate.fY);
Brian Osman9aaec362020-05-08 14:54:37 -040063 SkSimpleMatrixProvider matrixProvider(translatedMatrix);
64 fDraw.fMatrixProvider = &matrixProvider;
Michael Ludwig4fc7c5f2020-05-04 14:51:43 -040065
66 fDraw.drawRRect(rrect, get_paint(op, aa, alpha));
robertphillips@google.com58b20212012-06-27 20:44:52 +000067}
68
69/**
70 * Draw a single path element of the clip stack into the accumulation bitmap
71 */
Michael Ludwig2686d692020-04-17 20:21:37 +000072void GrSWMaskHelper::drawShape(const GrStyledShape& shape, const SkMatrix& matrix, SkRegion::Op op,
Brian Salomon74077562017-08-30 13:55:35 -040073 GrAA aa, uint8_t alpha) {
Michael Ludwig4fc7c5f2020-05-04 14:51:43 -040074 SkPaint paint = get_paint(op, aa, alpha);
Robert Phillipsf809c1e2017-01-13 11:02:42 -050075 paint.setPathEffect(shape.style().refPathEffect());
bsalomon8acedde2016-06-24 10:42:16 -070076 shape.style().strokeRec().applyToPaint(&paint);
reed@google.com84e922b2013-11-04 20:57:36 +000077
Brian Salomon74077562017-08-30 13:55:35 -040078 SkMatrix translatedMatrix = matrix;
79 translatedMatrix.postTranslate(fTranslate.fX, fTranslate.fY);
Brian Osman9aaec362020-05-08 14:54:37 -040080 SkSimpleMatrixProvider matrixProvider(translatedMatrix);
81 fDraw.fMatrixProvider = &matrixProvider;
Brian Salomon74077562017-08-30 13:55:35 -040082
bsalomon8acedde2016-06-24 10:42:16 -070083 SkPath path;
84 shape.asPath(&path);
reed@google.com126f7f52013-11-07 16:06:53 +000085 if (SkRegion::kReplace_Op == op && 0xFF == alpha) {
86 SkASSERT(0xFF == paint.getAlpha());
robertphillips98377402016-05-13 05:47:23 -070087 fDraw.drawPathCoverage(path, paint);
reed@google.com126f7f52013-11-07 16:06:53 +000088 } else {
robertphillips98377402016-05-13 05:47:23 -070089 fDraw.drawPath(path, paint);
reed@google.com126f7f52013-11-07 16:06:53 +000090 }
Michael Ludwig4fc7c5f2020-05-04 14:51:43 -040091}
92
93void GrSWMaskHelper::drawShape(const GrShape& shape, const SkMatrix& matrix, SkRegion::Op op,
94 GrAA aa, uint8_t alpha) {
95 SkPaint paint = get_paint(op, aa, alpha);
96
97 SkMatrix translatedMatrix = matrix;
98 translatedMatrix.postTranslate(fTranslate.fX, fTranslate.fY);
Brian Osman9aaec362020-05-08 14:54:37 -040099 SkSimpleMatrixProvider matrixProvider(translatedMatrix);
100 fDraw.fMatrixProvider = &matrixProvider;
Michael Ludwig4fc7c5f2020-05-04 14:51:43 -0400101
102 if (shape.inverted()) {
103 if (shape.isEmpty() || shape.isLine() || shape.isPoint()) {
104 // These shapes are empty for simple fills, so when inverted, cover everything
105 fDraw.drawPaint(paint);
106 return;
107 }
108 // Else fall through to the draw method using asPath(), which will toggle fill type properly
109 } else if (shape.isEmpty() || shape.isLine() || shape.isPoint()) {
110 // Do nothing, these shapes do not cover any pixels for simple fills
111 return;
112 } else if (shape.isRect()) {
113 fDraw.drawRect(shape.rect(), paint);
114 return;
115 } else if (shape.isRRect()) {
116 fDraw.drawRRect(shape.rrect(), paint);
117 return;
118 }
119
120 // A complex, or inverse-filled shape, so go through drawPath.
121 SkPath path;
122 shape.asPath(&path);
123 if (SkRegion::kReplace_Op == op && 0xFF == alpha) {
124 SkASSERT(0xFF == paint.getAlpha());
125 fDraw.drawPathCoverage(path, paint);
126 } else {
127 fDraw.drawPath(path, paint);
128 }
129}
robertphillips@google.com58b20212012-06-27 20:44:52 +0000130
Brian Salomon74077562017-08-30 13:55:35 -0400131bool GrSWMaskHelper::init(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)};
robertphillips98377402016-05-13 05:47:23 -0700134 SkIRect bounds = SkIRect::MakeWH(resultBounds.width(), resultBounds.height());
krajcevski25a67bc2014-07-29 11:44:26 -0700135
robertphillips98377402016-05-13 05:47:23 -0700136 const SkImageInfo bmImageInfo = SkImageInfo::MakeA8(bounds.width(), bounds.height());
Brian Osmanf9810662017-08-30 10:02:10 -0400137 if (!fPixels->tryAlloc(bmImageInfo)) {
robertphillips98377402016-05-13 05:47:23 -0700138 return false;
krajcevskib8ccc2f2014-08-07 08:15:14 -0700139 }
Brian Osmanf9810662017-08-30 10:02:10 -0400140 fPixels->erase(0);
krajcevskib8ccc2f2014-08-07 08:15:14 -0700141
Brian Osmanf9810662017-08-30 10:02:10 -0400142 fDraw.fDst = *fPixels;
robertphillips@google.com58b20212012-06-27 20:44:52 +0000143 fRasterClip.setRect(bounds);
reed41e010c2015-06-09 12:16:53 -0700144 fDraw.fRC = &fRasterClip;
robertphillips@google.com58b20212012-06-27 20:44:52 +0000145 return true;
146}
147
Greg Daniel9f0dfbd2020-02-10 11:47:11 -0500148GrSurfaceProxyView GrSWMaskHelper::toTextureView(GrRecordingContext* context, SkBackingFit fit) {
Robert Phillipscb7b8312018-04-20 11:49:51 -0400149 SkImageInfo ii = SkImageInfo::MakeA8(fPixels->width(), fPixels->height());
150 size_t rowBytes = fPixels->rowBytes();
krajcevskifb4f5cb2014-06-12 09:20:38 -0700151
Greg Daniel6f5441a2020-01-28 17:02:49 -0500152 SkBitmap bitmap;
153 SkAssertResult(bitmap.installPixels(ii, fPixels->detachPixels(), rowBytes,
154 [](void* addr, void* context) { sk_free(addr); },
155 nullptr));
156 bitmap.setImmutable();
robertphillips@google.com58b20212012-06-27 20:44:52 +0000157
Brian Salomonbc074a62020-03-18 10:06:13 -0400158 GrBitmapTextureMaker maker(context, bitmap, fit);
Brian Salomonecbb0fb2020-02-28 18:07:32 -0500159 return maker.view(GrMipMapped::kNo);
robertphillips@google.com58b20212012-06-27 20:44:52 +0000160}