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