blob: 5d0c3755d0f542fe727a169a7ecc526ab74aa7c7 [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
8#include "GrSWMaskHelper.h"
Brian Osmanf9810662017-08-30 10:02:10 -04009
robertphillips976f5f02016-06-03 10:59:20 -070010#include "GrContext.h"
Robert Phillipse305cc1f2016-12-14 12:19:05 -050011#include "GrContextPriv.h"
bsalomon8acedde2016-06-24 10:42:16 -070012#include "GrShape.h"
Robert Phillipse305cc1f2016-12-14 12:19:05 -050013#include "GrSurfaceContext.h"
14#include "GrTextureProxy.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000015
robertphillips@google.com58b20212012-06-27 20:44:52 +000016/*
17 * Convert a boolean operation into a transfer mode code
18 */
reed374772b2016-10-05 17:33:02 -070019static SkBlendMode op_to_mode(SkRegion::Op op) {
robertphillips@google.com58b20212012-06-27 20:44:52 +000020
reed374772b2016-10-05 17:33:02 -070021 static const SkBlendMode modeMap[] = {
22 SkBlendMode::kDstOut, // kDifference_Op
23 SkBlendMode::kModulate, // kIntersect_Op
24 SkBlendMode::kSrcOver, // kUnion_Op
25 SkBlendMode::kXor, // kXOR_Op
26 SkBlendMode::kClear, // kReverseDifference_Op
27 SkBlendMode::kSrc, // kReplace_Op
robertphillips@google.com58b20212012-06-27 20:44:52 +000028 };
29
30 return modeMap[op];
31}
32
robertphillips@google.com58b20212012-06-27 20:44:52 +000033/**
34 * Draw a single rect element of the clip stack into the accumulation bitmap
35 */
Brian Salomon74077562017-08-30 13:55:35 -040036void GrSWMaskHelper::drawRect(const SkRect& rect, const SkMatrix& matrix, SkRegion::Op op, GrAA aa,
37 uint8_t alpha) {
robertphillips@google.com58b20212012-06-27 20:44:52 +000038 SkPaint paint;
reed374772b2016-10-05 17:33:02 -070039 paint.setBlendMode(op_to_mode(op));
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050040 paint.setAntiAlias(GrAA::kYes == aa);
robertphillips@google.com366f1c62012-06-29 21:38:47 +000041 paint.setColor(SkColorSetARGB(alpha, alpha, alpha, alpha));
robertphillips@google.com58b20212012-06-27 20:44:52 +000042
Brian Salomon74077562017-08-30 13:55:35 -040043 SkMatrix translatedMatrix = matrix;
44 translatedMatrix.postTranslate(fTranslate.fX, fTranslate.fY);
45 fDraw.fMatrix = &translatedMatrix;
46
robertphillips@google.com366f1c62012-06-29 21:38:47 +000047 fDraw.drawRect(rect, paint);
Brian Salomon74077562017-08-30 13:55:35 -040048 SkDEBUGCODE(fDraw.fMatrix = (SkMatrix*)0xbbbbbbbb);
robertphillips@google.com58b20212012-06-27 20:44:52 +000049}
50
51/**
52 * Draw a single path element of the clip stack into the accumulation bitmap
53 */
Brian Salomon74077562017-08-30 13:55:35 -040054void GrSWMaskHelper::drawShape(const GrShape& shape, const SkMatrix& matrix, SkRegion::Op op,
55 GrAA aa, uint8_t alpha) {
robertphillips@google.com58b20212012-06-27 20:44:52 +000056 SkPaint paint;
Robert Phillipsf809c1e2017-01-13 11:02:42 -050057 paint.setPathEffect(shape.style().refPathEffect());
bsalomon8acedde2016-06-24 10:42:16 -070058 shape.style().strokeRec().applyToPaint(&paint);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050059 paint.setAntiAlias(GrAA::kYes == aa);
reed@google.com84e922b2013-11-04 20:57:36 +000060
Brian Salomon74077562017-08-30 13:55:35 -040061 SkMatrix translatedMatrix = matrix;
62 translatedMatrix.postTranslate(fTranslate.fX, fTranslate.fY);
63 fDraw.fMatrix = &translatedMatrix;
64
bsalomon8acedde2016-06-24 10:42:16 -070065 SkPath path;
66 shape.asPath(&path);
reed@google.com126f7f52013-11-07 16:06:53 +000067 if (SkRegion::kReplace_Op == op && 0xFF == alpha) {
68 SkASSERT(0xFF == paint.getAlpha());
robertphillips98377402016-05-13 05:47:23 -070069 fDraw.drawPathCoverage(path, paint);
reed@google.com126f7f52013-11-07 16:06:53 +000070 } else {
reed374772b2016-10-05 17:33:02 -070071 paint.setBlendMode(op_to_mode(op));
reed@google.com126f7f52013-11-07 16:06:53 +000072 paint.setColor(SkColorSetARGB(alpha, alpha, alpha, alpha));
robertphillips98377402016-05-13 05:47:23 -070073 fDraw.drawPath(path, paint);
reed@google.com126f7f52013-11-07 16:06:53 +000074 }
Brian Salomon74077562017-08-30 13:55:35 -040075 SkDEBUGCODE(fDraw.fMatrix = (SkMatrix*)0xbbbbbbbb);
76};
robertphillips@google.com58b20212012-06-27 20:44:52 +000077
Brian Salomon74077562017-08-30 13:55:35 -040078bool GrSWMaskHelper::init(const SkIRect& resultBounds) {
79 // We will need to translate draws so the bound's UL corner is at the origin
80 fTranslate = {-SkIntToScalar(resultBounds.fLeft), -SkIntToScalar(resultBounds.fTop)};
robertphillips98377402016-05-13 05:47:23 -070081 SkIRect bounds = SkIRect::MakeWH(resultBounds.width(), resultBounds.height());
krajcevski25a67bc2014-07-29 11:44:26 -070082
robertphillips98377402016-05-13 05:47:23 -070083 const SkImageInfo bmImageInfo = SkImageInfo::MakeA8(bounds.width(), bounds.height());
Brian Osmanf9810662017-08-30 10:02:10 -040084 if (!fPixels->tryAlloc(bmImageInfo)) {
robertphillips98377402016-05-13 05:47:23 -070085 return false;
krajcevskib8ccc2f2014-08-07 08:15:14 -070086 }
Brian Osmanf9810662017-08-30 10:02:10 -040087 fPixels->erase(0);
krajcevskib8ccc2f2014-08-07 08:15:14 -070088
reed41e010c2015-06-09 12:16:53 -070089 sk_bzero(&fDraw, sizeof(fDraw));
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;
Brian Salomon74077562017-08-30 13:55:35 -040093 // Each draw must specify the matrix.
94 SkDEBUGCODE(fDraw.fMatrix = (SkMatrix*)0xbbbbbbbb);
robertphillips@google.com58b20212012-06-27 20:44:52 +000095 return true;
96}
97
Robert Phillipsd3749482017-03-14 09:17:43 -040098sk_sp<GrTextureProxy> GrSWMaskHelper::toTextureProxy(GrContext* context, SkBackingFit fit) {
bsalomonf2703d82014-10-28 14:33:06 -070099 GrSurfaceDesc desc;
Robert Phillipsd3749482017-03-14 09:17:43 -0400100 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
Brian Osmanf9810662017-08-30 10:02:10 -0400101 desc.fWidth = fPixels->width();
102 desc.fHeight = fPixels->height();
krajcevskib3abe902014-07-30 13:08:11 -0700103 desc.fConfig = kAlpha_8_GrPixelConfig;
krajcevskifb4f5cb2014-06-12 09:20:38 -0700104
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500105 sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeDeferredSurfaceContext(
106 desc,
107 fit,
108 SkBudgeted::kYes);
Robert Phillipsf200a902017-01-30 13:27:37 -0500109 if (!sContext || !sContext->asTextureProxy()) {
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500110 return nullptr;
bsalomon39ef7fb2016-09-21 11:16:05 -0700111 }
robertphillips@google.com58b20212012-06-27 20:44:52 +0000112
Robert Phillipsc949ce92017-01-19 16:59:04 -0500113 SkImageInfo ii = SkImageInfo::MakeA8(desc.fWidth, desc.fHeight);
Brian Osmanf9810662017-08-30 10:02:10 -0400114 if (!sContext->writePixels(ii, fPixels->addr(), fPixels->rowBytes(), 0, 0)) {
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500115 return nullptr;
116 }
cblumeed828002016-02-16 13:00:01 -0800117
Robert Phillipsf200a902017-01-30 13:27:37 -0500118 return sContext->asTextureProxyRef();
robertphillips@google.com58b20212012-06-27 20:44:52 +0000119}