| 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 "gm/gm.h" |
| 9 |
#include "include/core/SkBitmap.h" |
| 10 |
#include "include/core/SkCanvas.h" |
| 11 |
#include "include/core/SkClipOp.h" |
| 12 |
#include "include/core/SkColor.h" |
| 13 |
#include "include/core/SkFont.h" |
| 14 |
#include "include/core/SkPaint.h" |
| 15 |
#include "include/core/SkPath.h" |
| 16 |
#include "include/core/SkRect.h" |
| 17 |
#include "include/core/SkRegion.h" |
| 18 |
#include "include/core/SkScalar.h" |
| 19 |
#include "include/core/SkSize.h" |
| 20 |
#include "include/core/SkString.h" |
| 21 |
#include "include/core/SkTypeface.h" |
| 22 |
#include "include/core/SkTypes.h" |
| 23 |
#include "src/core/SkAAClip.h" |
| 24 |
#include "src/core/SkMask.h" |
| 25 |
#include "tools/ToolUtils.h" |
| 26 |
#include "tools/fonts/FontToolUtils.h" |
| 27 |
|
| 28 |
namespace skiagm { |
| 29 |
|
| 30 |
static void paint_rgn(SkCanvas* canvas, const SkAAClip& clip, |
| 31 |
const SkPaint& paint) { |
| 32 |
SkMaskBuilder mask; |
| 33 |
SkBitmap bm; |
| 34 |
|
| 35 |
clip.copyToMask(&mask); |
| 36 |
|
| 37 |
SkAutoMaskFreeImage amfi(mask.image()); |
| 38 |
|
| 39 |
SkAssertResult( |
| 40 |
bm.installPixels(SkImageInfo::MakeA8(mask.fBounds.width(), mask.fBounds.height()), |
| 41 |
mask.image(), |
| 42 |
mask.fRowBytes)); |
| 43 |
|
| 44 |
// need to copy for deferred drawing test to work |
| 45 |
SkBitmap bm2; |
| 46 |
|
| 47 |
ToolUtils::copy_to(&bm2, bm.colorType(), bm); |
| 48 |
|
| 49 |
canvas->drawImage(bm2.asImage(), |
| 50 |
SK_Scalar1 * mask.fBounds.fLeft, |
| 51 |
SK_Scalar1 * mask.fBounds.fTop, |
| 52 |
SkSamplingOptions(), |
| 53 |
&paint); |
| 54 |
} |
| 55 |
|
| 56 |
////////////////////////////////////////////////////////////////////////////// |
| 57 |
/* |
| 58 |
* This GM tests anti aliased single operation booleans with SkAAClips, |
| 59 |
* SkRect and SkPaths. |
| 60 |
*/ |
| 61 |
class SimpleClipGM : public GM { |
| 62 |
public: |
| 63 |
enum SkGeomTypes { |
| 64 |
kRect_GeomType, |
| 65 |
kPath_GeomType, |
| 66 |
kAAClip_GeomType |
| 67 |
}; |
| 68 |
|
| 69 |
SimpleClipGM(SkGeomTypes geomType) |
| 70 |
: fGeomType(geomType) { |
| 71 |
} |
| 72 |
|
| 73 |
protected: |
| 74 |
void onOnceBeforeDraw() override { |
| 75 |
// offset the rects a bit so we get anti-aliasing in the rect case |
| 76 |
fBase.setLTRB(100.65f, |
| 77 |
100.65f, |
| 78 |
150.65f, |
| 79 |
150.65f); |
| 80 |
fRect = fBase; |
| 81 |
fRect.inset(5, 5); |
| 82 |
fRect.offset(25, 25); |
| 83 |
|
| 84 |
fBasePath = SkPath::RRect(fBase, 5, 5); |
| 85 |
fRectPath = SkPath::RRect(fRect, 5, 5); |
| 86 |
INHERITED::setBGColor(0xFFDDDDDD); |
| 87 |
} |
| 88 |
|
| 89 |
void buildRgn(SkAAClip* clip, SkClipOp op) { |
| 90 |
clip->setPath(fBasePath, fBasePath.getBounds().roundOut(), true); |
| 91 |
|
| 92 |
SkAAClip clip2; |
| 93 |
clip2.setPath(fRectPath, fRectPath.getBounds().roundOut(), true); |
| 94 |
clip->op(clip2, op); |
| 95 |
} |
| 96 |
|
| 97 |
void drawOrig(SkCanvas* canvas) { |
| 98 |
SkPaint paint; |
| 99 |
|
| 100 |
paint.setStyle(SkPaint::kStroke_Style); |
| 101 |
paint.setColor(SK_ColorBLACK); |
| 102 |
|
| 103 |
canvas->drawRect(fBase, paint); |
| 104 |
canvas->drawRect(fRect, paint); |
| 105 |
} |
| 106 |
|
| 107 |
void drawRgnOped(SkCanvas* canvas, SkClipOp op, SkColor color) { |
| 108 |
|
| 109 |
SkAAClip clip; |
| 110 |
|
| 111 |
this->buildRgn(&clip, op); |
| 112 |
this->drawOrig(canvas); |
| 113 |
|
| 114 |
SkPaint paint; |
| 115 |
paint.setColor(color); |
| 116 |
paint_rgn(canvas, clip, paint); |
| 117 |
} |
| 118 |
|
| 119 |
void drawPathsOped(SkCanvas* canvas, SkClipOp op, SkColor color) { |
| 120 |
|
| 121 |
this->drawOrig(canvas); |
| 122 |
|
| 123 |
canvas->save(); |
| 124 |
|
| 125 |
// create the clip mask with the supplied boolean op |
| 126 |
if (kPath_GeomType == fGeomType) { |
| 127 |
// path-based case |
| 128 |
canvas->clipPath(fBasePath, true); |
| 129 |
canvas->clipPath(fRectPath, op, true); |
| 130 |
} else { |
| 131 |
// rect-based case |
| 132 |
canvas->clipRect(fBase, true); |
| 133 |
canvas->clipRect(fRect, op, true); |
| 134 |
} |
| 135 |
|
| 136 |
// draw a rect that will entirely cover the clip mask area |
| 137 |
SkPaint paint; |
| 138 |
paint.setColor(color); |
| 139 |
|
| 140 |
SkRect r = SkRect::MakeLTRB(SkIntToScalar(90), SkIntToScalar(90), |
| 141 |
SkIntToScalar(180), SkIntToScalar(180)); |
| 142 |
|
| 143 |
canvas->drawRect(r, paint); |
| 144 |
|
| 145 |
canvas->restore(); |
| 146 |
} |
| 147 |
|
| 148 |
SkString getName() const override { |
| 149 |
SkString str; |
| 150 |
str.printf("simpleaaclip_%s", |
| 151 |
kRect_GeomType == fGeomType ? "rect" : |
| 152 |
(kPath_GeomType == fGeomType ? "path" : |
| 153 |
"aaclip")); |
| 154 |
return str; |
| 155 |
} |
| 156 |
|
| 157 |
SkISize getISize() override { return SkISize::Make(500, 240); } |
| 158 |
|
| 159 |
void onDraw(SkCanvas* canvas) override { |
| 160 |
|
| 161 |
const struct { |
| 162 |
SkColor fColor; |
| 163 |
const char* fName; |
| 164 |
SkClipOp fOp; |
| 165 |
} gOps[] = { |
| 166 |
{SK_ColorBLACK, "Difference", SkClipOp::kDifference}, |
| 167 |
{SK_ColorRED, "Intersect", SkClipOp::kIntersect}, |
| 168 |
}; |
| 169 |
|
| 170 |
SkPaint textPaint; |
| 171 |
SkFont font(ToolUtils::DefaultPortableTypeface(), 24); |
| 172 |
int xOff = 0; |
| 173 |
|
| 174 |
for (size_t op = 0; op < std::size(gOps); op++) { |
| 175 |
canvas->drawString(gOps[op].fName, 75.0f, 50.0f, font, textPaint); |
| 176 |
|
| 177 |
if (kAAClip_GeomType == fGeomType) { |
| 178 |
this->drawRgnOped(canvas, gOps[op].fOp, gOps[op].fColor); |
| 179 |
} else { |
| 180 |
this->drawPathsOped(canvas, gOps[op].fOp, gOps[op].fColor); |
| 181 |
} |
| 182 |
|
| 183 |
if (xOff >= 400) { |
| 184 |
canvas->translate(SkIntToScalar(-400), SkIntToScalar(250)); |
| 185 |
xOff = 0; |
| 186 |
} else { |
| 187 |
canvas->translate(SkIntToScalar(200), 0); |
| 188 |
xOff += 200; |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
private: |
| 193 |
|
| 194 |
SkGeomTypes fGeomType; |
| 195 |
|
| 196 |
SkRect fBase; |
| 197 |
SkRect fRect; |
| 198 |
|
| 199 |
SkPath fBasePath; // fBase as a round rect |
| 200 |
SkPath fRectPath; // fRect as a round rect |
| 201 |
|
| 202 |
using INHERITED = GM; |
| 203 |
}; |
| 204 |
|
| 205 |
////////////////////////////////////////////////////////////////////////////// |
| 206 |
|
| 207 |
// rects |
| 208 |
DEF_GM( return new SimpleClipGM(SimpleClipGM::kRect_GeomType); ) |
| 209 |
DEF_GM( return new SimpleClipGM(SimpleClipGM::kPath_GeomType); ) |
| 210 |
DEF_GM( return new SimpleClipGM(SimpleClipGM::kAAClip_GeomType); ) |
| 211 |
|
| 212 |
} // namespace skiagm |