blob: e7916c6d53653d6a975924c9a3ba8d499ab38f39 [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 Ludwig663afe52019-06-03 16:46:19 -040017#include "src/gpu/geometry/GrShape.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
robertphillips@google.com58b20212012-06-27 20:44:52 +000036/**
37 * Draw a single rect element of the clip stack into the accumulation bitmap
38 */
Brian Salomon74077562017-08-30 13:55:35 -040039void GrSWMaskHelper::drawRect(const SkRect& rect, const SkMatrix& matrix, SkRegion::Op op, GrAA aa,
40 uint8_t alpha) {
robertphillips@google.com58b20212012-06-27 20:44:52 +000041 SkPaint paint;
reed374772b2016-10-05 17:33:02 -070042 paint.setBlendMode(op_to_mode(op));
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050043 paint.setAntiAlias(GrAA::kYes == aa);
robertphillips@google.com366f1c62012-06-29 21:38:47 +000044 paint.setColor(SkColorSetARGB(alpha, alpha, alpha, alpha));
robertphillips@google.com58b20212012-06-27 20:44:52 +000045
Brian Salomon74077562017-08-30 13:55:35 -040046 SkMatrix translatedMatrix = matrix;
47 translatedMatrix.postTranslate(fTranslate.fX, fTranslate.fY);
48 fDraw.fMatrix = &translatedMatrix;
49
robertphillips@google.com366f1c62012-06-29 21:38:47 +000050 fDraw.drawRect(rect, paint);
robertphillips@google.com58b20212012-06-27 20:44:52 +000051}
52
53/**
54 * Draw a single path element of the clip stack into the accumulation bitmap
55 */
Brian Salomon74077562017-08-30 13:55:35 -040056void GrSWMaskHelper::drawShape(const GrShape& shape, const SkMatrix& matrix, SkRegion::Op op,
57 GrAA aa, uint8_t alpha) {
robertphillips@google.com58b20212012-06-27 20:44:52 +000058 SkPaint paint;
Robert Phillipsf809c1e2017-01-13 11:02:42 -050059 paint.setPathEffect(shape.style().refPathEffect());
bsalomon8acedde2016-06-24 10:42:16 -070060 shape.style().strokeRec().applyToPaint(&paint);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050061 paint.setAntiAlias(GrAA::kYes == aa);
reed@google.com84e922b2013-11-04 20:57:36 +000062
Brian Salomon74077562017-08-30 13:55:35 -040063 SkMatrix translatedMatrix = matrix;
64 translatedMatrix.postTranslate(fTranslate.fX, fTranslate.fY);
65 fDraw.fMatrix = &translatedMatrix;
66
bsalomon8acedde2016-06-24 10:42:16 -070067 SkPath path;
68 shape.asPath(&path);
reed@google.com126f7f52013-11-07 16:06:53 +000069 if (SkRegion::kReplace_Op == op && 0xFF == alpha) {
70 SkASSERT(0xFF == paint.getAlpha());
robertphillips98377402016-05-13 05:47:23 -070071 fDraw.drawPathCoverage(path, paint);
reed@google.com126f7f52013-11-07 16:06:53 +000072 } else {
reed374772b2016-10-05 17:33:02 -070073 paint.setBlendMode(op_to_mode(op));
reed@google.com126f7f52013-11-07 16:06:53 +000074 paint.setColor(SkColorSetARGB(alpha, alpha, alpha, alpha));
robertphillips98377402016-05-13 05:47:23 -070075 fDraw.drawPath(path, paint);
reed@google.com126f7f52013-11-07 16:06:53 +000076 }
Brian Salomon74077562017-08-30 13:55:35 -040077};
robertphillips@google.com58b20212012-06-27 20:44:52 +000078
Brian Salomon74077562017-08-30 13:55:35 -040079bool GrSWMaskHelper::init(const SkIRect& resultBounds) {
80 // We will need to translate draws so the bound's UL corner is at the origin
81 fTranslate = {-SkIntToScalar(resultBounds.fLeft), -SkIntToScalar(resultBounds.fTop)};
robertphillips98377402016-05-13 05:47:23 -070082 SkIRect bounds = SkIRect::MakeWH(resultBounds.width(), resultBounds.height());
krajcevski25a67bc2014-07-29 11:44:26 -070083
robertphillips98377402016-05-13 05:47:23 -070084 const SkImageInfo bmImageInfo = SkImageInfo::MakeA8(bounds.width(), bounds.height());
Brian Osmanf9810662017-08-30 10:02:10 -040085 if (!fPixels->tryAlloc(bmImageInfo)) {
robertphillips98377402016-05-13 05:47:23 -070086 return false;
krajcevskib8ccc2f2014-08-07 08:15:14 -070087 }
Brian Osmanf9810662017-08-30 10:02:10 -040088 fPixels->erase(0);
krajcevskib8ccc2f2014-08-07 08:15:14 -070089
Brian Osmanf9810662017-08-30 10:02:10 -040090 fDraw.fDst = *fPixels;
robertphillips@google.com58b20212012-06-27 20:44:52 +000091 fRasterClip.setRect(bounds);
reed41e010c2015-06-09 12:16:53 -070092 fDraw.fRC = &fRasterClip;
robertphillips@google.com58b20212012-06-27 20:44:52 +000093 return true;
94}
95
Greg Daniel9f0dfbd2020-02-10 11:47:11 -050096GrSurfaceProxyView GrSWMaskHelper::toTextureView(GrRecordingContext* context, SkBackingFit fit) {
Robert Phillipscb7b8312018-04-20 11:49:51 -040097 SkImageInfo ii = SkImageInfo::MakeA8(fPixels->width(), fPixels->height());
98 size_t rowBytes = fPixels->rowBytes();
krajcevskifb4f5cb2014-06-12 09:20:38 -070099
Greg Daniel6f5441a2020-01-28 17:02:49 -0500100 SkBitmap bitmap;
101 SkAssertResult(bitmap.installPixels(ii, fPixels->detachPixels(), rowBytes,
102 [](void* addr, void* context) { sk_free(addr); },
103 nullptr));
104 bitmap.setImmutable();
robertphillips@google.com58b20212012-06-27 20:44:52 +0000105
Greg Daniel6f5441a2020-01-28 17:02:49 -0500106 GrBitmapTextureMaker maker(context, bitmap, GrBitmapTextureMaker::Cached::kNo, fit);
Greg Danielc61d7e32020-02-04 14:27:45 -0500107 auto[textureView, ct] = maker.view(GrMipMapped::kNo);
Greg Daniel9f0dfbd2020-02-10 11:47:11 -0500108 return textureView;
robertphillips@google.com58b20212012-06-27 20:44:52 +0000109}