blob: 0ccec74e7a906de9c2cbc3e8e96c765c75c84270 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/private/GrRecordingContext.h"
Greg Daniel6f5441a2020-01-28 17:02:49 -050011#include "src/gpu/GrBitmapTextureMaker.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/GrCaps.h"
13#include "src/gpu/GrProxyProvider.h"
14#include "src/gpu/GrRecordingContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrSurfaceContext.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040016#include "src/gpu/GrTextureProxy.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000017#include "src/gpu/geometry/GrStyledShape.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000018
robertphillips@google.com58b20212012-06-27 20:44:52 +000019/*
20 * Convert a boolean operation into a transfer mode code
21 */
reed374772b2016-10-05 17:33:02 -070022static SkBlendMode op_to_mode(SkRegion::Op op) {
robertphillips@google.com58b20212012-06-27 20:44:52 +000023
reed374772b2016-10-05 17:33:02 -070024 static const SkBlendMode modeMap[] = {
25 SkBlendMode::kDstOut, // kDifference_Op
26 SkBlendMode::kModulate, // kIntersect_Op
27 SkBlendMode::kSrcOver, // kUnion_Op
28 SkBlendMode::kXor, // kXOR_Op
29 SkBlendMode::kClear, // kReverseDifference_Op
30 SkBlendMode::kSrc, // kReplace_Op
robertphillips@google.com58b20212012-06-27 20:44:52 +000031 };
32
33 return modeMap[op];
34}
35
Michael Ludwig4fc7c5f2020-05-04 14:51:43 -040036static SkPaint get_paint(SkRegion::Op op, GrAA aa, uint8_t alpha) {
37 SkPaint paint;
38 paint.setBlendMode(op_to_mode(op));
39 paint.setAntiAlias(GrAA::kYes == aa);
40 // SkPaint's color is unpremul so this will produce alpha in every channel.
41 paint.setColor(SkColorSetARGB(alpha, 255, 255, 255));
42 return paint;
43}
44
robertphillips@google.com58b20212012-06-27 20:44:52 +000045/**
46 * Draw a single rect element of the clip stack into the accumulation bitmap
47 */
Brian Salomon74077562017-08-30 13:55:35 -040048void GrSWMaskHelper::drawRect(const SkRect& rect, const SkMatrix& matrix, SkRegion::Op op, GrAA aa,
49 uint8_t alpha) {
Brian Salomon74077562017-08-30 13:55:35 -040050 SkMatrix translatedMatrix = matrix;
51 translatedMatrix.postTranslate(fTranslate.fX, fTranslate.fY);
52 fDraw.fMatrix = &translatedMatrix;
53
Michael Ludwig4fc7c5f2020-05-04 14:51:43 -040054 fDraw.drawRect(rect, get_paint(op, aa, alpha));
55}
56
57void GrSWMaskHelper::drawRRect(const SkRRect& rrect, const SkMatrix& matrix, SkRegion::Op op,
58 GrAA aa, uint8_t alpha) {
59 SkMatrix translatedMatrix = matrix;
60 translatedMatrix.postTranslate(fTranslate.fX, fTranslate.fY);
61 fDraw.fMatrix = &translatedMatrix;
62
63 fDraw.drawRRect(rrect, get_paint(op, aa, alpha));
robertphillips@google.com58b20212012-06-27 20:44:52 +000064}
65
66/**
67 * Draw a single path element of the clip stack into the accumulation bitmap
68 */
Michael Ludwig2686d692020-04-17 20:21:37 +000069void GrSWMaskHelper::drawShape(const GrStyledShape& shape, const SkMatrix& matrix, SkRegion::Op op,
Brian Salomon74077562017-08-30 13:55:35 -040070 GrAA aa, uint8_t alpha) {
Michael Ludwig4fc7c5f2020-05-04 14:51:43 -040071 SkPaint paint = get_paint(op, aa, alpha);
Robert Phillipsf809c1e2017-01-13 11:02:42 -050072 paint.setPathEffect(shape.style().refPathEffect());
bsalomon8acedde2016-06-24 10:42:16 -070073 shape.style().strokeRec().applyToPaint(&paint);
reed@google.com84e922b2013-11-04 20:57:36 +000074
Brian Salomon74077562017-08-30 13:55:35 -040075 SkMatrix translatedMatrix = matrix;
76 translatedMatrix.postTranslate(fTranslate.fX, fTranslate.fY);
77 fDraw.fMatrix = &translatedMatrix;
78
bsalomon8acedde2016-06-24 10:42:16 -070079 SkPath path;
80 shape.asPath(&path);
reed@google.com126f7f52013-11-07 16:06:53 +000081 if (SkRegion::kReplace_Op == op && 0xFF == alpha) {
82 SkASSERT(0xFF == paint.getAlpha());
robertphillips98377402016-05-13 05:47:23 -070083 fDraw.drawPathCoverage(path, paint);
reed@google.com126f7f52013-11-07 16:06:53 +000084 } else {
robertphillips98377402016-05-13 05:47:23 -070085 fDraw.drawPath(path, paint);
reed@google.com126f7f52013-11-07 16:06:53 +000086 }
Michael Ludwig4fc7c5f2020-05-04 14:51:43 -040087}
88
89void GrSWMaskHelper::drawShape(const GrShape& shape, const SkMatrix& matrix, SkRegion::Op op,
90 GrAA aa, uint8_t alpha) {
91 SkPaint paint = get_paint(op, aa, alpha);
92
93 SkMatrix translatedMatrix = matrix;
94 translatedMatrix.postTranslate(fTranslate.fX, fTranslate.fY);
95 fDraw.fMatrix = &translatedMatrix;
96
97 if (shape.inverted()) {
98 if (shape.isEmpty() || shape.isLine() || shape.isPoint()) {
99 // These shapes are empty for simple fills, so when inverted, cover everything
100 fDraw.drawPaint(paint);
101 return;
102 }
103 // Else fall through to the draw method using asPath(), which will toggle fill type properly
104 } else if (shape.isEmpty() || shape.isLine() || shape.isPoint()) {
105 // Do nothing, these shapes do not cover any pixels for simple fills
106 return;
107 } else if (shape.isRect()) {
108 fDraw.drawRect(shape.rect(), paint);
109 return;
110 } else if (shape.isRRect()) {
111 fDraw.drawRRect(shape.rrect(), paint);
112 return;
113 }
114
115 // A complex, or inverse-filled shape, so go through drawPath.
116 SkPath path;
117 shape.asPath(&path);
118 if (SkRegion::kReplace_Op == op && 0xFF == alpha) {
119 SkASSERT(0xFF == paint.getAlpha());
120 fDraw.drawPathCoverage(path, paint);
121 } else {
122 fDraw.drawPath(path, paint);
123 }
124}
robertphillips@google.com58b20212012-06-27 20:44:52 +0000125
Brian Salomon74077562017-08-30 13:55:35 -0400126bool GrSWMaskHelper::init(const SkIRect& resultBounds) {
127 // We will need to translate draws so the bound's UL corner is at the origin
128 fTranslate = {-SkIntToScalar(resultBounds.fLeft), -SkIntToScalar(resultBounds.fTop)};
robertphillips98377402016-05-13 05:47:23 -0700129 SkIRect bounds = SkIRect::MakeWH(resultBounds.width(), resultBounds.height());
krajcevski25a67bc2014-07-29 11:44:26 -0700130
robertphillips98377402016-05-13 05:47:23 -0700131 const SkImageInfo bmImageInfo = SkImageInfo::MakeA8(bounds.width(), bounds.height());
Brian Osmanf9810662017-08-30 10:02:10 -0400132 if (!fPixels->tryAlloc(bmImageInfo)) {
robertphillips98377402016-05-13 05:47:23 -0700133 return false;
krajcevskib8ccc2f2014-08-07 08:15:14 -0700134 }
Brian Osmanf9810662017-08-30 10:02:10 -0400135 fPixels->erase(0);
krajcevskib8ccc2f2014-08-07 08:15:14 -0700136
Brian Osmanf9810662017-08-30 10:02:10 -0400137 fDraw.fDst = *fPixels;
robertphillips@google.com58b20212012-06-27 20:44:52 +0000138 fRasterClip.setRect(bounds);
reed41e010c2015-06-09 12:16:53 -0700139 fDraw.fRC = &fRasterClip;
robertphillips@google.com58b20212012-06-27 20:44:52 +0000140 return true;
141}
142
Greg Daniel9f0dfbd2020-02-10 11:47:11 -0500143GrSurfaceProxyView GrSWMaskHelper::toTextureView(GrRecordingContext* context, SkBackingFit fit) {
Robert Phillipscb7b8312018-04-20 11:49:51 -0400144 SkImageInfo ii = SkImageInfo::MakeA8(fPixels->width(), fPixels->height());
145 size_t rowBytes = fPixels->rowBytes();
krajcevskifb4f5cb2014-06-12 09:20:38 -0700146
Greg Daniel6f5441a2020-01-28 17:02:49 -0500147 SkBitmap bitmap;
148 SkAssertResult(bitmap.installPixels(ii, fPixels->detachPixels(), rowBytes,
149 [](void* addr, void* context) { sk_free(addr); },
150 nullptr));
151 bitmap.setImmutable();
robertphillips@google.com58b20212012-06-27 20:44:52 +0000152
Brian Salomonbc074a62020-03-18 10:06:13 -0400153 GrBitmapTextureMaker maker(context, bitmap, fit);
Brian Salomonecbb0fb2020-02-28 18:07:32 -0500154 return maker.view(GrMipMapped::kNo);
robertphillips@google.com58b20212012-06-27 20:44:52 +0000155}