blob: 852a04099e0d6b8f1b14aab37d4a7643d366cb89 [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"
Robert Phillipscb7b8312018-04-20 11:49:51 -040012#include "GrProxyProvider.h"
bsalomon8acedde2016-06-24 10:42:16 -070013#include "GrShape.h"
Robert Phillipse305cc1f2016-12-14 12:19:05 -050014#include "GrSurfaceContext.h"
15#include "GrTextureProxy.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000016
robertphillips@google.com58b20212012-06-27 20:44:52 +000017/*
18 * Convert a boolean operation into a transfer mode code
19 */
reed374772b2016-10-05 17:33:02 -070020static SkBlendMode op_to_mode(SkRegion::Op op) {
robertphillips@google.com58b20212012-06-27 20:44:52 +000021
reed374772b2016-10-05 17:33:02 -070022 static const SkBlendMode modeMap[] = {
23 SkBlendMode::kDstOut, // kDifference_Op
24 SkBlendMode::kModulate, // kIntersect_Op
25 SkBlendMode::kSrcOver, // kUnion_Op
26 SkBlendMode::kXor, // kXOR_Op
27 SkBlendMode::kClear, // kReverseDifference_Op
28 SkBlendMode::kSrc, // kReplace_Op
robertphillips@google.com58b20212012-06-27 20:44:52 +000029 };
30
31 return modeMap[op];
32}
33
robertphillips@google.com58b20212012-06-27 20:44:52 +000034/**
35 * Draw a single rect element of the clip stack into the accumulation bitmap
36 */
Brian Salomon74077562017-08-30 13:55:35 -040037void GrSWMaskHelper::drawRect(const SkRect& rect, const SkMatrix& matrix, SkRegion::Op op, GrAA aa,
38 uint8_t alpha) {
robertphillips@google.com58b20212012-06-27 20:44:52 +000039 SkPaint paint;
reed374772b2016-10-05 17:33:02 -070040 paint.setBlendMode(op_to_mode(op));
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050041 paint.setAntiAlias(GrAA::kYes == aa);
robertphillips@google.com366f1c62012-06-29 21:38:47 +000042 paint.setColor(SkColorSetARGB(alpha, alpha, alpha, alpha));
robertphillips@google.com58b20212012-06-27 20:44:52 +000043
Brian Salomon74077562017-08-30 13:55:35 -040044 SkMatrix translatedMatrix = matrix;
45 translatedMatrix.postTranslate(fTranslate.fX, fTranslate.fY);
46 fDraw.fMatrix = &translatedMatrix;
47
robertphillips@google.com366f1c62012-06-29 21:38:47 +000048 fDraw.drawRect(rect, paint);
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};
robertphillips@google.com58b20212012-06-27 20:44:52 +000076
Brian Salomon74077562017-08-30 13:55:35 -040077bool GrSWMaskHelper::init(const SkIRect& resultBounds) {
78 // We will need to translate draws so the bound's UL corner is at the origin
79 fTranslate = {-SkIntToScalar(resultBounds.fLeft), -SkIntToScalar(resultBounds.fTop)};
robertphillips98377402016-05-13 05:47:23 -070080 SkIRect bounds = SkIRect::MakeWH(resultBounds.width(), resultBounds.height());
krajcevski25a67bc2014-07-29 11:44:26 -070081
robertphillips98377402016-05-13 05:47:23 -070082 const SkImageInfo bmImageInfo = SkImageInfo::MakeA8(bounds.width(), bounds.height());
Brian Osmanf9810662017-08-30 10:02:10 -040083 if (!fPixels->tryAlloc(bmImageInfo)) {
robertphillips98377402016-05-13 05:47:23 -070084 return false;
krajcevskib8ccc2f2014-08-07 08:15:14 -070085 }
Brian Osmanf9810662017-08-30 10:02:10 -040086 fPixels->erase(0);
krajcevskib8ccc2f2014-08-07 08:15:14 -070087
reed41e010c2015-06-09 12:16:53 -070088 sk_bzero(&fDraw, sizeof(fDraw));
Brian Osmanf9810662017-08-30 10:02:10 -040089 fDraw.fDst = *fPixels;
robertphillips@google.com58b20212012-06-27 20:44:52 +000090 fRasterClip.setRect(bounds);
reed41e010c2015-06-09 12:16:53 -070091 fDraw.fRC = &fRasterClip;
robertphillips@google.com58b20212012-06-27 20:44:52 +000092 return true;
93}
94
Robert Phillipsd3749482017-03-14 09:17:43 -040095sk_sp<GrTextureProxy> GrSWMaskHelper::toTextureProxy(GrContext* context, SkBackingFit fit) {
Robert Phillipscb7b8312018-04-20 11:49:51 -040096 SkImageInfo ii = SkImageInfo::MakeA8(fPixels->width(), fPixels->height());
97 size_t rowBytes = fPixels->rowBytes();
krajcevskifb4f5cb2014-06-12 09:20:38 -070098
Robert Phillipscb7b8312018-04-20 11:49:51 -040099 sk_sp<SkData> data = fPixels->detachPixelsAsData();
100 if (!data) {
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500101 return nullptr;
bsalomon39ef7fb2016-09-21 11:16:05 -0700102 }
robertphillips@google.com58b20212012-06-27 20:44:52 +0000103
Robert Phillipscb7b8312018-04-20 11:49:51 -0400104 sk_sp<SkImage> img = SkImage::MakeRasterData(ii, std::move(data), rowBytes);
105 if (!img) {
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500106 return nullptr;
107 }
cblumeed828002016-02-16 13:00:01 -0800108
Chris Daltond004e0b2018-09-27 09:28:03 -0600109 // TODO: http://skbug.com/8422: Although this fixes http://skbug.com/8351, it seems like these
110 // should just participate in the normal allocation process and not need the pending IO flag.
111 auto surfaceFlags = GrInternalSurfaceFlags::kNone;
112 if (!context->contextPriv().resourceProvider()) {
113 // In DDL mode, this texture proxy will be instantiated at flush time, therfore it cannot
114 // have pending IO.
115 surfaceFlags |= GrInternalSurfaceFlags::kNoPendingIO;
116 }
117
118 return context->contextPriv().proxyProvider()->createTextureProxy(
119 std::move(img), kNone_GrSurfaceFlags, 1, SkBudgeted::kYes, fit, surfaceFlags);
robertphillips@google.com58b20212012-06-27 20:44:52 +0000120}