bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 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.h" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
bsalomon | b62da80 | 2015-01-31 07:51:14 -0800 | [diff] [blame] | 10 | #include "SkShader.h" |
Mike Klein | b323a5e | 2017-07-24 15:21:31 -0400 | [diff] [blame] | 11 | #include "SkTraceEvent.h" |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 12 | using namespace skiagm; |
| 13 | |
| 14 | GM::GM() { |
commit-bot@chromium.org | b21fac1 | 2014-02-07 21:13:11 +0000 | [diff] [blame] | 15 | fMode = kGM_Mode; |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 16 | fBGColor = SK_ColorWHITE; |
reed@google.com | aef7361 | 2012-11-16 13:41:45 +0000 | [diff] [blame] | 17 | fCanvasIsDeferred = false; |
reed@google.com | 7775d66 | 2012-11-27 15:15:58 +0000 | [diff] [blame] | 18 | fHaveCalledOnceBeforeDraw = false; |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 19 | } |
tfarina | 880914c | 2014-06-09 12:05:34 -0700 | [diff] [blame] | 20 | |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 21 | GM::~GM() {} |
| 22 | |
| 23 | void GM::draw(SkCanvas* canvas) { |
Mike Klein | b323a5e | 2017-07-24 15:21:31 -0400 | [diff] [blame] | 24 | TRACE_EVENT1("GM", TRACE_FUNC, "name", TRACE_STR_COPY(this->getName())); |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 25 | this->drawBackground(canvas); |
| 26 | this->drawContent(canvas); |
| 27 | } |
| 28 | |
| 29 | void GM::drawContent(SkCanvas* canvas) { |
Mike Klein | b323a5e | 2017-07-24 15:21:31 -0400 | [diff] [blame] | 30 | TRACE_EVENT0("GM", TRACE_FUNC); |
reed@google.com | 7775d66 | 2012-11-27 15:15:58 +0000 | [diff] [blame] | 31 | if (!fHaveCalledOnceBeforeDraw) { |
| 32 | fHaveCalledOnceBeforeDraw = true; |
| 33 | this->onOnceBeforeDraw(); |
| 34 | } |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 35 | this->onDraw(canvas); |
| 36 | } |
| 37 | |
| 38 | void GM::drawBackground(SkCanvas* canvas) { |
Mike Klein | b323a5e | 2017-07-24 15:21:31 -0400 | [diff] [blame] | 39 | TRACE_EVENT0("GM", TRACE_FUNC); |
reed@google.com | 7775d66 | 2012-11-27 15:15:58 +0000 | [diff] [blame] | 40 | if (!fHaveCalledOnceBeforeDraw) { |
| 41 | fHaveCalledOnceBeforeDraw = true; |
| 42 | this->onOnceBeforeDraw(); |
| 43 | } |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 44 | this->onDrawBackground(canvas); |
| 45 | } |
| 46 | |
commit-bot@chromium.org | 38aeb0f | 2014-02-26 23:01:57 +0000 | [diff] [blame] | 47 | const char* GM::getName() { |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 48 | if (fShortName.size() == 0) { |
| 49 | fShortName = this->onShortName(); |
| 50 | } |
| 51 | return fShortName.c_str(); |
| 52 | } |
| 53 | |
| 54 | void GM::setBGColor(SkColor color) { |
| 55 | fBGColor = color; |
| 56 | } |
| 57 | |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 58 | bool GM::animate(const SkAnimTimer& timer) { |
| 59 | return this->onAnimate(timer); |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | ///////////////////////////////////////////////////////////////////////////////////////////// |
| 63 | |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 64 | void GM::onDrawBackground(SkCanvas* canvas) { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 65 | canvas->drawColor(fBGColor, SkBlendMode::kSrc); |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 66 | } |
| 67 | |
reed@google.com | 2d6ef52 | 2012-01-03 17:20:38 +0000 | [diff] [blame] | 68 | void GM::drawSizeBounds(SkCanvas* canvas, SkColor color) { |
| 69 | SkISize size = this->getISize(); |
| 70 | SkRect r = SkRect::MakeWH(SkIntToScalar(size.width()), |
| 71 | SkIntToScalar(size.height())); |
| 72 | SkPaint paint; |
| 73 | paint.setColor(color); |
| 74 | canvas->drawRect(r, paint); |
| 75 | } |
| 76 | |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 77 | void GM::DrawGpuOnlyMessage(SkCanvas* canvas) { |
bsalomon | b62da80 | 2015-01-31 07:51:14 -0800 | [diff] [blame] | 78 | SkBitmap bmp; |
| 79 | bmp.allocN32Pixels(128, 64); |
| 80 | SkCanvas bmpCanvas(bmp); |
| 81 | bmpCanvas.drawColor(SK_ColorWHITE); |
| 82 | SkPaint paint; |
| 83 | paint.setAntiAlias(true); |
| 84 | paint.setTextSize(20); |
| 85 | paint.setColor(SK_ColorRED); |
caryclark | f597c42 | 2015-07-28 10:37:53 -0700 | [diff] [blame] | 86 | sk_tool_utils::set_portable_typeface(&paint); |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 87 | constexpr char kTxt[] = "GPU Only"; |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 88 | bmpCanvas.drawString(kTxt, 20, 40, paint); |
bsalomon | b62da80 | 2015-01-31 07:51:14 -0800 | [diff] [blame] | 89 | SkMatrix localM; |
| 90 | localM.setRotate(35.f); |
| 91 | localM.postTranslate(10.f, 0.f); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 92 | paint.setShader(SkShader::MakeBitmapShader(bmp, SkShader::kMirror_TileMode, |
| 93 | SkShader::kMirror_TileMode, |
| 94 | &localM)); |
bsalomon | b62da80 | 2015-01-31 07:51:14 -0800 | [diff] [blame] | 95 | paint.setFilterQuality(kMedium_SkFilterQuality); |
| 96 | canvas->drawPaint(paint); |
| 97 | return; |
| 98 | } |
| 99 | |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 100 | // need to explicitly declare this, or we get some weird infinite loop llist |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 101 | template GMRegistry* GMRegistry::gHead; |
halcanary | f62c634 | 2015-01-12 15:27:46 -0800 | [diff] [blame] | 102 | |
| 103 | void skiagm::SimpleGM::onDraw(SkCanvas* canvas) { |
| 104 | fDrawProc(canvas); |
| 105 | } |
| 106 | |
| 107 | SkISize skiagm::SimpleGM::onISize() { |
| 108 | return fSize; |
| 109 | } |
| 110 | |
| 111 | SkString skiagm::SimpleGM::onShortName() { |
| 112 | return fName; |
| 113 | } |