blob: af0d1bdc2243ec4ce9648fc7a57c2fd366935430 [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 Salomon0e8fc8b2016-12-09 15:10:07 -050036void GrSWMaskHelper::drawRect(const SkRect& rect, SkRegion::Op op, GrAA aa, uint8_t alpha) {
robertphillips@google.com58b20212012-06-27 20:44:52 +000037 SkPaint paint;
38
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
robertphillips@google.com366f1c62012-06-29 21:38:47 +000043 fDraw.drawRect(rect, paint);
robertphillips@google.com58b20212012-06-27 20:44:52 +000044}
45
46/**
47 * Draw a single path element of the clip stack into the accumulation bitmap
48 */
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050049void GrSWMaskHelper::drawShape(const GrShape& shape, SkRegion::Op op, GrAA aa, uint8_t alpha) {
robertphillips@google.com58b20212012-06-27 20:44:52 +000050 SkPaint paint;
Robert Phillipsf809c1e2017-01-13 11:02:42 -050051 paint.setPathEffect(shape.style().refPathEffect());
bsalomon8acedde2016-06-24 10:42:16 -070052 shape.style().strokeRec().applyToPaint(&paint);
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050053 paint.setAntiAlias(GrAA::kYes == aa);
reed@google.com84e922b2013-11-04 20:57:36 +000054
bsalomon8acedde2016-06-24 10:42:16 -070055 SkPath path;
56 shape.asPath(&path);
reed@google.com126f7f52013-11-07 16:06:53 +000057 if (SkRegion::kReplace_Op == op && 0xFF == alpha) {
58 SkASSERT(0xFF == paint.getAlpha());
robertphillips98377402016-05-13 05:47:23 -070059 fDraw.drawPathCoverage(path, paint);
reed@google.com126f7f52013-11-07 16:06:53 +000060 } else {
reed374772b2016-10-05 17:33:02 -070061 paint.setBlendMode(op_to_mode(op));
reed@google.com126f7f52013-11-07 16:06:53 +000062 paint.setColor(SkColorSetARGB(alpha, alpha, alpha, alpha));
robertphillips98377402016-05-13 05:47:23 -070063 fDraw.drawPath(path, paint);
reed@google.com126f7f52013-11-07 16:06:53 +000064 }
robertphillips@google.com58b20212012-06-27 20:44:52 +000065}
66
robertphillips98377402016-05-13 05:47:23 -070067bool GrSWMaskHelper::init(const SkIRect& resultBounds, const SkMatrix* matrix) {
bsalomon49f085d2014-09-05 13:34:00 -070068 if (matrix) {
robertphillips@google.com366f1c62012-06-29 21:38:47 +000069 fMatrix = *matrix;
robertphillips@google.com58b20212012-06-27 20:44:52 +000070 } else {
71 fMatrix.setIdentity();
72 }
73
robertphillips@google.com366f1c62012-06-29 21:38:47 +000074 // Now translate so the bound's UL corner is at the origin
robertphillips98377402016-05-13 05:47:23 -070075 fMatrix.postTranslate(-SkIntToScalar(resultBounds.fLeft), -SkIntToScalar(resultBounds.fTop));
76 SkIRect bounds = SkIRect::MakeWH(resultBounds.width(), resultBounds.height());
krajcevski25a67bc2014-07-29 11:44:26 -070077
robertphillips98377402016-05-13 05:47:23 -070078 const SkImageInfo bmImageInfo = SkImageInfo::MakeA8(bounds.width(), bounds.height());
Brian Osmanf9810662017-08-30 10:02:10 -040079 if (!fPixels->tryAlloc(bmImageInfo)) {
robertphillips98377402016-05-13 05:47:23 -070080 return false;
krajcevskib8ccc2f2014-08-07 08:15:14 -070081 }
Brian Osmanf9810662017-08-30 10:02:10 -040082 fPixels->erase(0);
krajcevskib8ccc2f2014-08-07 08:15:14 -070083
reed41e010c2015-06-09 12:16:53 -070084 sk_bzero(&fDraw, sizeof(fDraw));
Brian Osmanf9810662017-08-30 10:02:10 -040085 fDraw.fDst = *fPixels;
robertphillips@google.com58b20212012-06-27 20:44:52 +000086 fRasterClip.setRect(bounds);
reed41e010c2015-06-09 12:16:53 -070087 fDraw.fRC = &fRasterClip;
reed41e010c2015-06-09 12:16:53 -070088 fDraw.fMatrix = &fMatrix;
robertphillips@google.com58b20212012-06-27 20:44:52 +000089 return true;
90}
91
Robert Phillipsd3749482017-03-14 09:17:43 -040092sk_sp<GrTextureProxy> GrSWMaskHelper::toTextureProxy(GrContext* context, SkBackingFit fit) {
bsalomonf2703d82014-10-28 14:33:06 -070093 GrSurfaceDesc desc;
Robert Phillipsd3749482017-03-14 09:17:43 -040094 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
Brian Osmanf9810662017-08-30 10:02:10 -040095 desc.fWidth = fPixels->width();
96 desc.fHeight = fPixels->height();
krajcevskib3abe902014-07-30 13:08:11 -070097 desc.fConfig = kAlpha_8_GrPixelConfig;
krajcevskifb4f5cb2014-06-12 09:20:38 -070098
Robert Phillipse305cc1f2016-12-14 12:19:05 -050099 sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeDeferredSurfaceContext(
100 desc,
101 fit,
102 SkBudgeted::kYes);
Robert Phillipsf200a902017-01-30 13:27:37 -0500103 if (!sContext || !sContext->asTextureProxy()) {
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500104 return nullptr;
bsalomon39ef7fb2016-09-21 11:16:05 -0700105 }
robertphillips@google.com58b20212012-06-27 20:44:52 +0000106
Robert Phillipsc949ce92017-01-19 16:59:04 -0500107 SkImageInfo ii = SkImageInfo::MakeA8(desc.fWidth, desc.fHeight);
Brian Osmanf9810662017-08-30 10:02:10 -0400108 if (!sContext->writePixels(ii, fPixels->addr(), fPixels->rowBytes(), 0, 0)) {
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500109 return nullptr;
110 }
cblumeed828002016-02-16 13:00:01 -0800111
Robert Phillipsf200a902017-01-30 13:27:37 -0500112 return sContext->asTextureProxyRef();
robertphillips@google.com58b20212012-06-27 20:44:52 +0000113}