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" |
| 8 | |
| 9 | namespace skiagm { |
| 10 | |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 11 | static const char* gColorTypeNames[] = { |
| 12 | "unknown", |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 13 | "A8", |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 14 | "565", |
| 15 | "4444", |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 16 | "8888", |
| 17 | "8888", |
| 18 | "Index8", |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 19 | }; |
| 20 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 21 | constexpr SkColorType gColorTypes[] = { |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 22 | kRGB_565_SkColorType, |
| 23 | kARGB_4444_SkColorType, |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 24 | kN32_SkColorType, |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 25 | }; |
| 26 | |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 27 | #define NUM_CONFIGS SK_ARRAY_COUNT(gColorTypes) |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 28 | |
| 29 | static void draw_checks(SkCanvas* canvas, int width, int height) { |
| 30 | SkPaint paint; |
| 31 | paint.setColor(SK_ColorRED); |
tomhudson@google.com | 7558925 | 2012-04-10 17:42:21 +0000 | [diff] [blame] | 32 | canvas->drawRectCoords(SkIntToScalar(0), SkIntToScalar(0), |
| 33 | SkIntToScalar(width / 2), SkIntToScalar(height / 2), paint); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 34 | paint.setColor(SK_ColorGREEN); |
tomhudson@google.com | 7558925 | 2012-04-10 17:42:21 +0000 | [diff] [blame] | 35 | canvas->drawRectCoords(SkIntToScalar(width / 2), SkIntToScalar(0), |
| 36 | SkIntToScalar(width), SkIntToScalar(height / 2), paint); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 37 | paint.setColor(SK_ColorBLUE); |
tomhudson@google.com | 7558925 | 2012-04-10 17:42:21 +0000 | [diff] [blame] | 38 | canvas->drawRectCoords(SkIntToScalar(0), SkIntToScalar(height / 2), |
| 39 | SkIntToScalar(width / 2), SkIntToScalar(height), paint); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 40 | paint.setColor(SK_ColorYELLOW); |
tomhudson@google.com | 7558925 | 2012-04-10 17:42:21 +0000 | [diff] [blame] | 41 | canvas->drawRectCoords(SkIntToScalar(width / 2), SkIntToScalar(height / 2), |
| 42 | SkIntToScalar(width), 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; |
| 68 | src.allocN32Pixels(40, 40); |
| 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) { |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 74 | src.copyTo(&fDst[i], gColorTypes[i]); |
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); |
| 104 | canvas->drawText(name, strlen(name), x, y, paint); |
| 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 | } |