senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +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 | #include "gm.h" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 8 | #include "sk_tool_utils.h" |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 9 | |
| 10 | namespace skiagm { |
| 11 | |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 12 | static const char* gColorTypeNames[] = { |
| 13 | "unknown", |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 14 | "A8", |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 15 | "565", |
| 16 | "4444", |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 17 | "8888", |
| 18 | "8888", |
| 19 | "Index8", |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 20 | }; |
| 21 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 22 | constexpr SkColorType gColorTypes[] = { |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 23 | kRGB_565_SkColorType, |
| 24 | kARGB_4444_SkColorType, |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 25 | kN32_SkColorType, |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 26 | }; |
| 27 | |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 28 | #define NUM_CONFIGS SK_ARRAY_COUNT(gColorTypes) |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 29 | |
| 30 | static void draw_checks(SkCanvas* canvas, int width, int height) { |
| 31 | SkPaint paint; |
| 32 | paint.setColor(SK_ColorRED); |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 33 | canvas->drawRect(SkRect::MakeIWH(width/2, height/2), paint); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 34 | paint.setColor(SK_ColorGREEN); |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 35 | canvas->drawRect({ SkIntToScalar(width/2), 0, SkIntToScalar(width), SkIntToScalar(height/2) }, |
| 36 | paint); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 37 | paint.setColor(SK_ColorBLUE); |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 38 | canvas->drawRect({ 0, SkIntToScalar(height/2), SkIntToScalar(width/2), SkIntToScalar(height) }, |
| 39 | paint); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 40 | paint.setColor(SK_ColorYELLOW); |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 41 | canvas->drawRect({ SkIntToScalar(width/2), SkIntToScalar(height/2), SkIntToScalar(width), |
| 42 | SkIntToScalar(height) }, paint); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | class BitmapCopyGM : public GM { |
| 46 | public: |
| 47 | SkBitmap fDst[NUM_CONFIGS]; |
| 48 | |
| 49 | BitmapCopyGM() { |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 50 | this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD)); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | protected: |
| 54 | virtual SkString onShortName() { |
| 55 | return SkString("bitmapcopy"); |
| 56 | } |
| 57 | |
| 58 | virtual SkISize onISize() { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 59 | return SkISize::Make(540, 330); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | virtual void onDraw(SkCanvas* canvas) { |
| 63 | SkPaint paint; |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 64 | SkScalar horizMargin = 10; |
| 65 | SkScalar vertMargin = 10; |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 66 | |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 67 | SkBitmap src; |
Matt Sarett | ec138b4 | 2017-04-28 16:06:34 -0400 | [diff] [blame] | 68 | src.allocN32Pixels(40, 40, kOpaque_SkAlphaType); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 69 | SkCanvas canvasTmp(src); |
skia.committer@gmail.com | d55e357 | 2013-08-08 07:01:20 +0000 | [diff] [blame] | 70 | |
commit-bot@chromium.org | 488a337 | 2013-08-07 17:03:09 +0000 | [diff] [blame] | 71 | draw_checks(&canvasTmp, 40, 40); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 72 | |
| 73 | for (unsigned i = 0; i < NUM_CONFIGS; ++i) { |
Matt Sarett | 68b8e3d | 2017-04-28 11:15:22 -0400 | [diff] [blame] | 74 | sk_tool_utils::copy_to(&fDst[i], gColorTypes[i], src); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 75 | } |
| 76 | |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 77 | canvas->clear(sk_tool_utils::color_to_565(0xFFDDDDDD)); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 78 | paint.setAntiAlias(true); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 79 | sk_tool_utils::set_portable_typeface(&paint); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 80 | |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 81 | SkScalar width = SkIntToScalar(40); |
| 82 | SkScalar height = SkIntToScalar(40); |
| 83 | if (paint.getFontSpacing() > height) { |
| 84 | height = paint.getFontSpacing(); |
| 85 | } |
| 86 | for (unsigned i = 0; i < NUM_CONFIGS; i++) { |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 87 | const char* name = gColorTypeNames[src.colorType()]; |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 88 | SkScalar textWidth = paint.measureText(name, strlen(name)); |
| 89 | if (textWidth > width) { |
| 90 | width = textWidth; |
| 91 | } |
| 92 | } |
| 93 | SkScalar horizOffset = width + horizMargin; |
| 94 | SkScalar vertOffset = height + vertMargin; |
| 95 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 96 | |
| 97 | for (unsigned i = 0; i < NUM_CONFIGS; i++) { |
| 98 | canvas->save(); |
| 99 | // Draw destination config name |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 100 | const char* name = gColorTypeNames[fDst[i].colorType()]; |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 101 | SkScalar textWidth = paint.measureText(name, strlen(name)); |
| 102 | SkScalar x = (width - textWidth) / SkScalar(2); |
| 103 | SkScalar y = paint.getFontSpacing() / SkScalar(2); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 104 | canvas->drawString(name, x, y, paint); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 105 | |
| 106 | // Draw destination bitmap |
| 107 | canvas->translate(0, vertOffset); |
| 108 | x = (width - 40) / SkScalar(2); |
| 109 | canvas->drawBitmap(fDst[i], x, 0, &paint); |
| 110 | canvas->restore(); |
| 111 | |
| 112 | canvas->translate(horizOffset, 0); |
| 113 | } |
| 114 | } |
| 115 | |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 116 | private: |
| 117 | typedef GM INHERITED; |
| 118 | }; |
| 119 | |
| 120 | ////////////////////////////////////////////////////////////////////////////// |
| 121 | |
| 122 | static GM* MyFactory(void*) { return new BitmapCopyGM; } |
| 123 | static GMRegistry reg(MyFactory); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 124 | } |