1/*
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 "src/gpu/GrSWMaskHelper.h"
9
10#include "include/private/GrRecordingContext.h"
11#include "src/gpu/GrBitmapTextureMaker.h"
12#include "src/gpu/GrCaps.h"
13#include "src/gpu/GrProxyProvider.h"
14#include "src/gpu/GrRecordingContextPriv.h"
15#include "src/gpu/GrSurfaceContext.h"
16#include "src/gpu/GrTextureProxy.h"
17#include "src/gpu/geometry/GrShape.h"
18
19/*
20 * Convert a boolean operation into a transfer mode code
21 */
22static SkBlendMode op_to_mode(SkRegion::Op op) {
23
24 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
31 };
32
33 return modeMap[op];
34}
35
36/**
37 * Draw a single rect element of the clip stack into the accumulation bitmap
38 */
39void GrSWMaskHelper::drawRect(const SkRect& rect, const SkMatrix& matrix, SkRegion::Op op, GrAA aa,
40 uint8_t alpha) {
41 SkPaint paint;
42 paint.setBlendMode(op_to_mode(op));
43 paint.setAntiAlias(GrAA::kYes == aa);
44 paint.setColor(SkColorSetARGB(alpha, alpha, alpha, alpha));
45
46 SkMatrix translatedMatrix = matrix;
47 translatedMatrix.postTranslate(fTranslate.fX, fTranslate.fY);
48 fDraw.fMatrix = &translatedMatrix;
49
50 fDraw.drawRect(rect, paint);
51}
52
53/**
54 * Draw a single path element of the clip stack into the accumulation bitmap
55 */
56void GrSWMaskHelper::drawShape(const GrShape& shape, const SkMatrix& matrix, SkRegion::Op op,
57 GrAA aa, uint8_t alpha) {
58 SkPaint paint;
59 paint.setPathEffect(shape.style().refPathEffect());
60 shape.style().strokeRec().applyToPaint(&paint);
61 paint.setAntiAlias(GrAA::kYes == aa);
62
63 SkMatrix translatedMatrix = matrix;
64 translatedMatrix.postTranslate(fTranslate.fX, fTranslate.fY);
65 fDraw.fMatrix = &translatedMatrix;
66
67 SkPath path;
68 shape.asPath(&path);
69 if (SkRegion::kReplace_Op == op && 0xFF == alpha) {
70 SkASSERT(0xFF == paint.getAlpha());
71 fDraw.drawPathCoverage(path, paint);
72 } else {
73 paint.setBlendMode(op_to_mode(op));
74 paint.setColor(SkColorSetARGB(alpha, alpha, alpha, alpha));
75 fDraw.drawPath(path, paint);
76 }
77};
78
79bool 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)};
82 SkIRect bounds = SkIRect::MakeWH(resultBounds.width(), resultBounds.height());
83
84 const SkImageInfo bmImageInfo = SkImageInfo::MakeA8(bounds.width(), bounds.height());
85 if (!fPixels->tryAlloc(bmImageInfo)) {
86 return false;
87 }
88 fPixels->erase(0);
89
90 fDraw.fDst = *fPixels;
91 fRasterClip.setRect(bounds);
92 fDraw.fRC = &fRasterClip;
93 return true;
94}
95
96GrSurfaceProxyView GrSWMaskHelper::toTextureView(GrRecordingContext* context, SkBackingFit fit) {
97 SkImageInfo ii = SkImageInfo::MakeA8(fPixels->width(), fPixels->height());
98 size_t rowBytes = fPixels->rowBytes();
99
100 SkBitmap bitmap;
101 SkAssertResult(bitmap.installPixels(ii, fPixels->detachPixels(), rowBytes,
102 [](void* addr, void* context) { sk_free(addr); },
103 nullptr));
104 bitmap.setImmutable();
105
106 GrBitmapTextureMaker maker(context, bitmap, fit);
107 return maker.view(GrMipMapped::kNo);
108}
109