| 1 |
/* |
| 2 |
* Copyright 2011 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 |
|
| 10 |
#include "include/core/SkBitmap.h" |
| 11 |
#include "include/core/SkBlendMode.h" |
| 12 |
#include "include/core/SkCanvas.h" |
| 13 |
#include "include/core/SkFont.h" |
| 14 |
#include "include/core/SkFontTypes.h" |
| 15 |
#include "include/core/SkMatrix.h" |
| 16 |
#include "include/core/SkPaint.h" |
| 17 |
#include "include/core/SkRect.h" |
| 18 |
#include "include/core/SkSamplingOptions.h" |
| 19 |
#include "include/core/SkShader.h" // IWYU pragma: keep |
| 20 |
#include "include/core/SkTileMode.h" |
| 21 |
#include "src/core/SkTraceEvent.h" |
| 22 |
#include "tools/ToolUtils.h" |
| 23 |
#include "tools/fonts/FontToolUtils.h" |
| 24 |
|
| 25 |
#if defined(SK_GANESH) |
| 26 |
#include "include/gpu/ganesh/GrRecordingContext.h" |
| 27 |
#endif |
| 28 |
|
| 29 |
#include <cstdarg> |
| 30 |
#include <cstdint> |
| 31 |
|
| 32 |
using namespace skiagm; |
| 33 |
|
| 34 |
static void draw_failure_message(SkCanvas* canvas, const char format[], ...) SK_PRINTF_LIKE(2, 3); |
| 35 |
|
| 36 |
static void draw_failure_message(SkCanvas* canvas, const char format[], ...) { |
| 37 |
SkString failureMsg; |
| 38 |
|
| 39 |
va_list argp; |
| 40 |
va_start(argp, format); |
| 41 |
failureMsg.appendVAList(format, argp); |
| 42 |
va_end(argp); |
| 43 |
|
| 44 |
constexpr SkScalar kOffset = 5.0f; |
| 45 |
canvas->drawColor(SkColorSetRGB(200,0,0)); |
| 46 |
SkFont font = ToolUtils::DefaultPortableFont(); |
| 47 |
SkRect bounds; |
| 48 |
font.measureText(failureMsg.c_str(), failureMsg.size(), SkTextEncoding::kUTF8, &bounds); |
| 49 |
SkPaint textPaint(SkColors::kWhite); |
| 50 |
canvas->drawString(failureMsg, kOffset, bounds.height() + kOffset, font, textPaint); |
| 51 |
} |
| 52 |
|
| 53 |
static void draw_gpu_only_message(SkCanvas* canvas) { |
| 54 |
SkBitmap bmp; |
| 55 |
bmp.allocN32Pixels(128, 64); |
| 56 |
SkCanvas bmpCanvas(bmp); |
| 57 |
bmpCanvas.drawColor(SK_ColorWHITE); |
| 58 |
SkFont font(ToolUtils::DefaultPortableTypeface(), 20); |
| 59 |
SkPaint paint(SkColors::kRed); |
| 60 |
bmpCanvas.drawString("GPU Only", 20, 40, font, paint); |
| 61 |
SkMatrix localM; |
| 62 |
localM.setRotate(35.f); |
| 63 |
localM.postTranslate(10.f, 0.f); |
| 64 |
paint.setShader(bmp.makeShader(SkTileMode::kMirror, SkTileMode::kMirror, |
| 65 |
SkSamplingOptions(SkFilterMode::kLinear, |
| 66 |
SkMipmapMode::kNearest), |
| 67 |
localM)); |
| 68 |
canvas->drawPaint(paint); |
| 69 |
} |
| 70 |
|
| 71 |
static void handle_gm_failure(SkCanvas* canvas, DrawResult result, const SkString& errorMsg) { |
| 72 |
if (DrawResult::kFail == result) { |
| 73 |
draw_failure_message(canvas, "DRAW FAILED: %s", errorMsg.c_str()); |
| 74 |
} else if (SkString(GM::kErrorMsg_DrawSkippedGpuOnly) == errorMsg) { |
| 75 |
draw_gpu_only_message(canvas); |
| 76 |
} else { |
| 77 |
draw_failure_message(canvas, "DRAW SKIPPED: %s", errorMsg.c_str()); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
GM::GM(SkColor bgColor) { |
| 82 |
fMode = kGM_Mode; |
| 83 |
fBGColor = bgColor; |
| 84 |
} |
| 85 |
|
| 86 |
GM::~GM() {} |
| 87 |
|
| 88 |
DrawResult GM::gpuSetup(SkCanvas* canvas, |
| 89 |
SkString* errorMsg, |
| 90 |
GraphiteTestContext* graphiteTestContext) { |
| 91 |
TRACE_EVENT1("GM", TRACE_FUNC, "name", TRACE_STR_COPY(this->getName().c_str())); |
| 92 |
if (!fGpuSetup) { |
| 93 |
// When drawn in viewer, gpuSetup will be called multiple times with the same |
| 94 |
// GrContext or graphite::Context. |
| 95 |
fGpuSetup = true; |
| 96 |
fGpuSetupResult = this->onGpuSetup(canvas, errorMsg, graphiteTestContext); |
| 97 |
} |
| 98 |
if (fGpuSetupResult == DrawResult::kOk) { |
| 99 |
fGraphiteTestContext = graphiteTestContext; |
| 100 |
} else { |
| 101 |
handle_gm_failure(canvas, fGpuSetupResult, *errorMsg); |
| 102 |
} |
| 103 |
|
| 104 |
return fGpuSetupResult; |
| 105 |
} |
| 106 |
|
| 107 |
void GM::gpuTeardown() { |
| 108 |
this->onGpuTeardown(); |
| 109 |
|
| 110 |
// After 'gpuTeardown' a GM can be reused with a different GrContext or graphite::Context. Reset |
| 111 |
// the flag so 'onGpuSetup' will be called. |
| 112 |
fGpuSetup = false; |
| 113 |
fGraphiteTestContext = nullptr; |
| 114 |
} |
| 115 |
|
| 116 |
DrawResult GM::draw(SkCanvas* canvas, SkString* errorMsg) { |
| 117 |
TRACE_EVENT1("GM", TRACE_FUNC, "name", TRACE_STR_COPY(this->getName().c_str())); |
| 118 |
this->drawBackground(canvas); |
| 119 |
return this->drawContent(canvas, errorMsg); |
| 120 |
} |
| 121 |
|
| 122 |
DrawResult GM::drawContent(SkCanvas* canvas, SkString* errorMsg) { |
| 123 |
TRACE_EVENT0("GM", TRACE_FUNC); |
| 124 |
this->onceBeforeDraw(); |
| 125 |
SkAutoCanvasRestore acr(canvas, true); |
| 126 |
DrawResult drawResult = this->onDraw(canvas, errorMsg); |
| 127 |
if (DrawResult::kOk != drawResult) { |
| 128 |
handle_gm_failure(canvas, drawResult, *errorMsg); |
| 129 |
} |
| 130 |
return drawResult; |
| 131 |
} |
| 132 |
|
| 133 |
void GM::drawBackground(SkCanvas* canvas) { |
| 134 |
TRACE_EVENT0("GM", TRACE_FUNC); |
| 135 |
this->onceBeforeDraw(); |
| 136 |
canvas->drawColor(fBGColor, SkBlendMode::kSrc); |
| 137 |
} |
| 138 |
|
| 139 |
DrawResult GM::onDraw(SkCanvas* canvas, SkString* errorMsg) { |
| 140 |
this->onDraw(canvas); |
| 141 |
return DrawResult::kOk; |
| 142 |
} |
| 143 |
void GM::onDraw(SkCanvas*) { SK_ABORT("Not implemented."); } |
| 144 |
|
| 145 |
SkISize SimpleGM::getISize() { return fSize; } |
| 146 |
SkString SimpleGM::getName() const { return fName; } |
| 147 |
DrawResult SimpleGM::onDraw(SkCanvas* canvas, SkString* errorMsg) { |
| 148 |
return fDrawProc(canvas, errorMsg); |
| 149 |
} |
| 150 |
|
| 151 |
#if defined(SK_GANESH) |
| 152 |
SkISize SimpleGpuGM::getISize() { return fSize; } |
| 153 |
SkString SimpleGpuGM::getName() const { return fName; } |
| 154 |
DrawResult SimpleGpuGM::onDraw(GrRecordingContext* rContext, SkCanvas* canvas, SkString* errorMsg) { |
| 155 |
return fDrawProc(rContext, canvas, errorMsg); |
| 156 |
} |
| 157 |
#endif |
| 158 |
|
| 159 |
void GM::setBGColor(SkColor color) { |
| 160 |
fBGColor = color; |
| 161 |
} |
| 162 |
|
| 163 |
bool GM::animate(double nanos) { return this->onAnimate(nanos); } |
| 164 |
|
| 165 |
bool GM::runAsBench() const { return false; } |
| 166 |
|
| 167 |
void GM::onOnceBeforeDraw() {} |
| 168 |
|
| 169 |
bool GM::onAnimate(double /*nanos*/) { return false; } |
| 170 |
|
| 171 |
bool GM::onChar(SkUnichar uni) { return false; } |
| 172 |
|
| 173 |
bool GM::onGetControls(SkMetaData*) { return false; } |
| 174 |
|
| 175 |
void GM::onSetControls(const SkMetaData&) {} |
| 176 |
|
| 177 |
///////////////////////////////////////////////////////////////////////////////////////////// |
| 178 |
|
| 179 |
void GM::drawSizeBounds(SkCanvas* canvas, SkColor color) { |
| 180 |
canvas->drawRect(SkRect::Make(this->getISize()), SkPaint(SkColor4f::FromColor(color))); |
| 181 |
} |
| 182 |
|
| 183 |
// need to explicitly declare this, or we get some weird infinite loop llist |
| 184 |
template GMRegistry* GMRegistry::gHead; |
| 185 |
|
| 186 |
#if defined(SK_GANESH) |
| 187 |
DrawResult GpuGM::onDraw(GrRecordingContext* rContext, SkCanvas* canvas, SkString* errorMsg) { |
| 188 |
this->onDraw(rContext, canvas); |
| 189 |
return DrawResult::kOk; |
| 190 |
} |
| 191 |
void GpuGM::onDraw(GrRecordingContext*, SkCanvas*) { |
| 192 |
SK_ABORT("Not implemented."); |
| 193 |
} |
| 194 |
|
| 195 |
DrawResult GpuGM::onDraw(SkCanvas* canvas, SkString* errorMsg) { |
| 196 |
|
| 197 |
auto rContext = canvas->recordingContext(); |
| 198 |
if (!rContext) { |
| 199 |
*errorMsg = kErrorMsg_DrawSkippedGpuOnly; |
| 200 |
return DrawResult::kSkip; |
| 201 |
} |
| 202 |
if (rContext->abandoned()) { |
| 203 |
*errorMsg = "GrContext abandoned."; |
| 204 |
return DrawResult::kSkip; |
| 205 |
} |
| 206 |
return this->onDraw(rContext, canvas, errorMsg); |
| 207 |
} |
| 208 |
#endif |
| 209 |
|
| 210 |
template <typename Fn> |
| 211 |
static void mark(SkCanvas* canvas, SkScalar x, SkScalar y, Fn&& fn) { |
| 212 |
SkPaint alpha; |
| 213 |
alpha.setAlpha(0x50); |
| 214 |
canvas->saveLayer(nullptr, &alpha); |
| 215 |
canvas->translate(x,y); |
| 216 |
canvas->scale(2,2); |
| 217 |
fn(); |
| 218 |
canvas->restore(); |
| 219 |
} |
| 220 |
|
| 221 |
void MarkGMGood(SkCanvas* canvas, SkScalar x, SkScalar y) { |
| 222 |
mark(canvas, x,y, [&]{ |
| 223 |
// A green circle. |
| 224 |
canvas->drawCircle(0, 0, 12, SkPaint(SkColor4f::FromColor(SkColorSetRGB(27, 158, 119)))); |
| 225 |
|
| 226 |
// Cut out a check mark. |
| 227 |
SkPaint paint(SkColors::kTransparent); |
| 228 |
paint.setBlendMode(SkBlendMode::kSrc); |
| 229 |
paint.setStrokeWidth(2); |
| 230 |
paint.setStyle(SkPaint::kStroke_Style); |
| 231 |
canvas->drawLine(-6, 0, |
| 232 |
-1, 5, paint); |
| 233 |
canvas->drawLine(-1, +5, |
| 234 |
+7, -5, paint); |
| 235 |
}); |
| 236 |
} |
| 237 |
|
| 238 |
void MarkGMBad(SkCanvas* canvas, SkScalar x, SkScalar y) { |
| 239 |
mark(canvas, x,y, [&] { |
| 240 |
// A red circle. |
| 241 |
canvas->drawCircle(0,0, 12, SkPaint(SkColor4f::FromColor(SkColorSetRGB(231, 41, 138)))); |
| 242 |
|
| 243 |
// Cut out an 'X'. |
| 244 |
SkPaint paint(SkColors::kTransparent); |
| 245 |
paint.setBlendMode(SkBlendMode::kSrc); |
| 246 |
paint.setStrokeWidth(2); |
| 247 |
paint.setStyle(SkPaint::kStroke_Style); |
| 248 |
canvas->drawLine(-5,-5, |
| 249 |
+5,+5, paint); |
| 250 |
canvas->drawLine(+5,-5, |
| 251 |
-5,+5, paint); |
| 252 |
}); |
| 253 |
} |
| 254 |
|
| 255 |
namespace skiagm { |
| 256 |
void Register(skiagm::GM* gm) { |
| 257 |
// The skiagm::GMRegistry class is a subclass of sk_tools::Registry. Instances of |
| 258 |
// sk_tools::Registry form a linked list (there is one such list for each subclass), where each |
| 259 |
// instance holds a value and a pointer to the next sk_tools::Registry instance. The head of |
| 260 |
// this linked list is stored in a global variable. The sk_tools::Registry constructor |
| 261 |
// automatically pushes a new instance to the head of said linked list. Therefore, in order to |
| 262 |
// register a value in the GM registry, it suffices to just instantiate skiagm::GMRegistry with |
| 263 |
// the value we wish to register. |
| 264 |
new skiagm::GMRegistry([=]() { return std::unique_ptr<skiagm::GM>(gm); }); |
| 265 |
} |
| 266 |
} // namespace skiagm |