| 1 |
/* |
| 2 |
* Copyright 2017 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/SkBlendMode.h" |
| 10 |
#include "include/core/SkCanvas.h" |
| 11 |
#include "include/core/SkColor.h" |
| 12 |
#include "include/core/SkColorSpace.h" |
| 13 |
#include "include/core/SkFont.h" |
| 14 |
#include "include/core/SkImage.h" |
| 15 |
#include "include/core/SkImageFilter.h" |
| 16 |
#include "include/core/SkImageInfo.h" |
| 17 |
#include "include/core/SkMaskFilter.h" |
| 18 |
#include "include/core/SkMatrix.h" |
| 19 |
#include "include/core/SkPaint.h" |
| 20 |
#include "include/core/SkPicture.h" |
| 21 |
#include "include/core/SkPictureRecorder.h" |
| 22 |
#include "include/core/SkPoint.h" |
| 23 |
#include "include/core/SkRSXform.h" |
| 24 |
#include "include/core/SkRect.h" |
| 25 |
#include "include/core/SkRefCnt.h" |
| 26 |
#include "include/core/SkScalar.h" |
| 27 |
#include "include/core/SkShader.h" |
| 28 |
#include "include/core/SkSize.h" |
| 29 |
#include "include/core/SkString.h" |
| 30 |
#include "include/core/SkSurface.h" |
| 31 |
#include "include/core/SkTextBlob.h" |
| 32 |
#include "include/core/SkTileMode.h" |
| 33 |
#include "include/core/SkTypeface.h" |
| 34 |
#include "include/core/SkTypes.h" |
| 35 |
#include "include/core/SkVertices.h" |
| 36 |
#include "include/effects/SkGradient.h" |
| 37 |
#include "include/effects/SkImageFilters.h" |
| 38 |
#include "include/effects/SkShaderMaskFilter.h" |
| 39 |
#include "src/base/SkRandom.h" |
| 40 |
#include "src/core/SkCanvasPriv.h" |
| 41 |
#include "tools/DecodeUtils.h" |
| 42 |
#include "tools/Resources.h" |
| 43 |
#include "tools/ToolUtils.h" |
| 44 |
#include "tools/fonts/FontToolUtils.h" |
| 45 |
|
| 46 |
#include <string.h> |
| 47 |
#include <initializer_list> |
| 48 |
|
| 49 |
// Test kInitWithPrevious_SaveLayerFlag by drawing an image, save a layer with the flag, which |
| 50 |
// should seed the layer with the image (from below). Then we punch a hole in the layer and |
| 51 |
// restore with kPlus mode, which should show the mandrill super-bright on the outside, but |
| 52 |
// normal where we punched the hole. |
| 53 |
DEF_SIMPLE_GM(savelayer_initfromprev, canvas, 256, 256) { |
| 54 |
canvas->drawImage(ToolUtils::GetResourceAsImage("images/mandrill_256.png"), 0, 0); |
| 55 |
|
| 56 |
SkCanvas::SaveLayerRec rec; |
| 57 |
SkPaint paint; |
| 58 |
paint.setBlendMode(SkBlendMode::kPlus); |
| 59 |
rec.fSaveLayerFlags = SkCanvas::kInitWithPrevious_SaveLayerFlag; |
| 60 |
rec.fPaint = &paint; |
| 61 |
canvas->saveLayer(rec); |
| 62 |
paint.setBlendMode(SkBlendMode::kClear); |
| 63 |
canvas->drawCircle(128, 128, 96, paint); |
| 64 |
canvas->restore(); |
| 65 |
} |
| 66 |
|
| 67 |
static void draw_cell(SkCanvas* canvas, sk_sp<SkTextBlob> blob, SkColor c, SkScalar w, SkScalar h, |
| 68 |
bool useDrawBehind) { |
| 69 |
SkRect r = SkRect::MakeWH(w, h); |
| 70 |
SkPaint p; |
| 71 |
p.setColor(c); |
| 72 |
p.setBlendMode(SkBlendMode::kSrc); |
| 73 |
canvas->drawRect(r, p); |
| 74 |
p.setBlendMode(SkBlendMode::kSrcOver); |
| 75 |
|
| 76 |
const SkScalar margin = 80; |
| 77 |
r.fLeft = w - margin; |
| 78 |
|
| 79 |
// save the behind image |
| 80 |
SkDEBUGCODE(int sc0 =) canvas->getSaveCount(); |
| 81 |
SkDEBUGCODE(int sc1 =) SkCanvasPriv::SaveBehind(canvas, &r); |
| 82 |
SkDEBUGCODE(int sc2 =) canvas->getSaveCount(); |
| 83 |
SkASSERT(sc0 == sc1); |
| 84 |
SkASSERT(sc0 + 1 == sc2); |
| 85 |
|
| 86 |
// draw the foreground (including over the 'behind' section) |
| 87 |
p.setColor(SK_ColorBLACK); |
| 88 |
canvas->drawTextBlob(blob, 10, 30, p); |
| 89 |
|
| 90 |
// draw the treatment |
| 91 |
const SkPoint pts[] = { {r.fLeft,0}, {r.fRight, 0} }; |
| 92 |
const SkColor4f colors[] = { {0,0,0,0x88/255.f}, {0,0,0,0} }; |
| 93 |
auto sh = SkShaders::LinearGradient(pts, {{colors, {}, SkTileMode::kClamp}, {}}); |
| 94 |
p.setShader(sh); |
| 95 |
p.setBlendMode(SkBlendMode::kDstIn); |
| 96 |
|
| 97 |
if (useDrawBehind) { |
| 98 |
SkCanvasPriv::DrawBehind(canvas, p); |
| 99 |
} else { |
| 100 |
canvas->drawRect(r, p); |
| 101 |
} |
| 102 |
|
| 103 |
// this should restore the behind image |
| 104 |
canvas->restore(); |
| 105 |
SkDEBUGCODE(int sc3 =) canvas->getSaveCount(); |
| 106 |
SkASSERT(sc3 == sc0); |
| 107 |
|
| 108 |
// just outline where we expect the treatment to appear |
| 109 |
p.reset(); |
| 110 |
p.setStyle(SkPaint::kStroke_Style); |
| 111 |
p.setAlphaf(0.25f); |
| 112 |
} |
| 113 |
|
| 114 |
static void draw_list(SkCanvas* canvas, sk_sp<SkTextBlob> blob, bool useDrawBehind) { |
| 115 |
SkAutoCanvasRestore acr(canvas, true); |
| 116 |
|
| 117 |
SkRandom rand; |
| 118 |
SkScalar w = 400; |
| 119 |
SkScalar h = 40; |
| 120 |
for (int i = 0; i < 8; ++i) { |
| 121 |
SkColor c = rand.nextU(); // ensure we're opaque |
| 122 |
c = (c & 0xFFFFFF) | 0x80000000; |
| 123 |
draw_cell(canvas, blob, c, w, h, useDrawBehind); |
| 124 |
canvas->translate(0, h); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
DEF_SIMPLE_GM(save_behind, canvas, 830, 670) { |
| 129 |
SkFont font = ToolUtils::DefaultPortableFont(); |
| 130 |
font.setSize(30); |
| 131 |
|
| 132 |
const char text[] = "This is a very long line of text"; |
| 133 |
auto blob = SkTextBlob::MakeFromText(text, strlen(text), font); |
| 134 |
|
| 135 |
for (bool useDrawBehind : {false, true}) { |
| 136 |
canvas->save(); |
| 137 |
|
| 138 |
draw_list(canvas, blob, useDrawBehind); |
| 139 |
canvas->translate(0, 350); |
| 140 |
canvas->saveLayer({0, 0, 400, 320}, nullptr); |
| 141 |
draw_list(canvas, blob, useDrawBehind); |
| 142 |
canvas->restore(); |
| 143 |
|
| 144 |
canvas->restore(); |
| 145 |
canvas->translate(430, 0); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
#include "include/effects/SkGradient.h" |
| 150 |
|
| 151 |
DEF_SIMPLE_GM(savelayer_f16, canvas, 900, 300) { |
| 152 |
int n = 15; |
| 153 |
SkRect r{0, 0, 300, 300}; |
| 154 |
SkPaint paint; |
| 155 |
|
| 156 |
const SkColor4f colors[] = {SkColors::kRed, SkColors::kGreen, SkColors::kBlue, SkColors::kRed}; |
| 157 |
paint.setShader(SkShaders::SweepGradient(r.center(), {{colors, {}, SkTileMode::kClamp}, {}})); |
| 158 |
|
| 159 |
canvas->drawOval(r, paint); |
| 160 |
|
| 161 |
paint.setAlphaf(1.0f/n); |
| 162 |
paint.setBlendMode(SkBlendMode::kPlus); |
| 163 |
|
| 164 |
for (auto flags : {0, (int)SkCanvas::kF16ColorType}) { |
| 165 |
canvas->translate(r.width(), 0); |
| 166 |
|
| 167 |
SkCanvas::SaveLayerRec rec; |
| 168 |
rec.fSaveLayerFlags = flags; |
| 169 |
canvas->saveLayer(rec); |
| 170 |
for (int i = 0; i < n; ++i) { |
| 171 |
canvas->drawOval(r, paint); |
| 172 |
} |
| 173 |
canvas->restore(); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
static void draw_atlas(SkCanvas* canvas, SkImage* image) { |
| 178 |
SkRSXform xforms[] = {{1, 0, 0, 0}, {1, 0, 50, 50}}; |
| 179 |
SkRect tex[] = {{0, 0, 100, 100}, {0, 0, 100, 100}}; |
| 180 |
SkColor colors[] = {0xffffffff, 0xffffffff}; |
| 181 |
SkPaint paint; |
| 182 |
|
| 183 |
canvas->drawAtlas(image, |
| 184 |
xforms, |
| 185 |
tex, |
| 186 |
colors, |
| 187 |
SkBlendMode::kSrcIn, |
| 188 |
SkFilterMode::kNearest, |
| 189 |
nullptr, |
| 190 |
&paint); |
| 191 |
} |
| 192 |
|
| 193 |
static void draw_vertices(SkCanvas* canvas, SkImage* image) { |
| 194 |
SkPoint pts[] = {{0, 0}, {0, 100}, {100, 100}, {100, 0}, {100, 100}, {0, 100}}; |
| 195 |
sk_sp<SkVertices> verts = |
| 196 |
SkVertices::MakeCopy(SkVertices::kTriangles_VertexMode, 6, pts, nullptr, nullptr); |
| 197 |
|
| 198 |
SkPaint paint; |
| 199 |
paint.setShader(image->makeShader(SkFilterMode::kNearest)); |
| 200 |
|
| 201 |
canvas->drawVertices(verts, SkBlendMode::kSrc, paint); |
| 202 |
} |
| 203 |
|
| 204 |
static void draw_points(SkCanvas* canvas, SkImage* image) { |
| 205 |
SkPoint pts[] = {{50, 50}, {75, 75}}; |
| 206 |
SkPaint paint; |
| 207 |
paint.setShader(image->makeShader(SkFilterMode::kNearest)); |
| 208 |
paint.setStrokeWidth(100); |
| 209 |
paint.setStrokeCap(SkPaint::kSquare_Cap); |
| 210 |
|
| 211 |
canvas->drawPoints(SkCanvas::kPoints_PointMode, pts, paint); |
| 212 |
} |
| 213 |
|
| 214 |
static void draw_image_set(SkCanvas* canvas, SkImage* image) { |
| 215 |
SkRect r = SkRect::MakeWH(100, 100); |
| 216 |
SkCanvas::ImageSetEntry entries[] = { |
| 217 |
SkCanvas::ImageSetEntry(sk_ref_sp(image), r, r, 1.0f, SkCanvas::kNone_QuadAAFlags), |
| 218 |
SkCanvas::ImageSetEntry( |
| 219 |
sk_ref_sp(image), r, r.makeOffset(50, 50), 1.0f, SkCanvas::kNone_QuadAAFlags), |
| 220 |
}; |
| 221 |
|
| 222 |
SkPaint paint; |
| 223 |
canvas->experimental_DrawEdgeAAImageSet( |
| 224 |
entries, 2, nullptr, nullptr, SkFilterMode::kNearest, &paint); |
| 225 |
} |
| 226 |
|
| 227 |
/* |
| 228 |
Picture optimization looks for single drawing operations inside a saveLayer with alpha. It tries |
| 229 |
to push the alpha into the drawing operation itself. That's only valid if the draw logically |
| 230 |
touches each pixel once. A handful of draws do not behave like that. They instead act like |
| 231 |
repeated, independent draws. This GM tests this with several operations. |
| 232 |
*/ |
| 233 |
DEF_SIMPLE_GM(skbug_14554, canvas, 310, 630) { |
| 234 |
sk_sp<SkImage> image = ToolUtils::GetResourceAsImage("images/mandrill_128.png"); |
| 235 |
SkPictureRecorder rec; |
| 236 |
|
| 237 |
using DrawProc = void(*)(SkCanvas*, SkImage*); |
| 238 |
|
| 239 |
for (DrawProc proc : {draw_atlas, draw_vertices, draw_points, draw_image_set}) { |
| 240 |
canvas->save(); |
| 241 |
for (bool injectExtraOp : {false, true}) { |
| 242 |
auto c = rec.beginRecording(SkRect::MakeWH(150, 150)); |
| 243 |
c->saveLayerAlphaf(nullptr, 0.6f); |
| 244 |
proc(c, image.get()); |
| 245 |
// For the second draw of each test-case, we inject an extra (useless) operation, which |
| 246 |
// inhibits the optimization and produces the correct result. |
| 247 |
if (injectExtraOp) { |
| 248 |
c->translate(1, 0); |
| 249 |
} |
| 250 |
c->restore(); |
| 251 |
|
| 252 |
auto pic = rec.finishRecordingAsPicture(); |
| 253 |
|
| 254 |
canvas->drawPicture(pic); |
| 255 |
canvas->translate(160, 0); |
| 256 |
} |
| 257 |
canvas->restore(); |
| 258 |
canvas->translate(0, 160); |
| 259 |
} |
| 260 |
} |