senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | #include "gm.h" |
| 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 | |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 22 | static const SkColorType gColorTypes[] = { |
| 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); |
tomhudson@google.com | 7558925 | 2012-04-10 17:42:21 +0000 | [diff] [blame] | 33 | canvas->drawRectCoords(SkIntToScalar(0), SkIntToScalar(0), |
| 34 | SkIntToScalar(width / 2), SkIntToScalar(height / 2), paint); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 35 | paint.setColor(SK_ColorGREEN); |
tomhudson@google.com | 7558925 | 2012-04-10 17:42:21 +0000 | [diff] [blame] | 36 | canvas->drawRectCoords(SkIntToScalar(width / 2), SkIntToScalar(0), |
| 37 | SkIntToScalar(width), SkIntToScalar(height / 2), paint); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 38 | paint.setColor(SK_ColorBLUE); |
tomhudson@google.com | 7558925 | 2012-04-10 17:42:21 +0000 | [diff] [blame] | 39 | canvas->drawRectCoords(SkIntToScalar(0), SkIntToScalar(height / 2), |
| 40 | SkIntToScalar(width / 2), SkIntToScalar(height), paint); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 41 | paint.setColor(SK_ColorYELLOW); |
tomhudson@google.com | 7558925 | 2012-04-10 17:42:21 +0000 | [diff] [blame] | 42 | canvas->drawRectCoords(SkIntToScalar(width / 2), SkIntToScalar(height / 2), |
| 43 | SkIntToScalar(width), SkIntToScalar(height), paint); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | class BitmapCopyGM : public GM { |
| 47 | public: |
| 48 | SkBitmap fDst[NUM_CONFIGS]; |
| 49 | |
| 50 | BitmapCopyGM() { |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 51 | this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD)); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | protected: |
| 55 | virtual SkString onShortName() { |
| 56 | return SkString("bitmapcopy"); |
| 57 | } |
| 58 | |
| 59 | virtual SkISize onISize() { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 60 | return SkISize::Make(540, 330); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | virtual void onDraw(SkCanvas* canvas) { |
| 64 | SkPaint paint; |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 65 | SkScalar horizMargin = 10; |
| 66 | SkScalar vertMargin = 10; |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 67 | |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 68 | SkBitmap src; |
| 69 | src.allocN32Pixels(40, 40); |
| 70 | SkCanvas canvasTmp(src); |
skia.committer@gmail.com | d55e357 | 2013-08-08 07:01:20 +0000 | [diff] [blame] | 71 | |
commit-bot@chromium.org | 488a337 | 2013-08-07 17:03:09 +0000 | [diff] [blame] | 72 | draw_checks(&canvasTmp, 40, 40); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 73 | |
| 74 | for (unsigned i = 0; i < NUM_CONFIGS; ++i) { |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 75 | src.copyTo(&fDst[i], gColorTypes[i]); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 76 | } |
| 77 | |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 78 | canvas->clear(sk_tool_utils::color_to_565(0xFFDDDDDD)); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 79 | paint.setAntiAlias(true); |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 80 | sk_tool_utils::set_portable_typeface_always(&paint); |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 81 | |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 82 | SkScalar width = SkIntToScalar(40); |
| 83 | SkScalar height = SkIntToScalar(40); |
| 84 | if (paint.getFontSpacing() > height) { |
| 85 | height = paint.getFontSpacing(); |
| 86 | } |
| 87 | for (unsigned i = 0; i < NUM_CONFIGS; i++) { |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 88 | const char* name = gColorTypeNames[src.colorType()]; |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 89 | SkScalar textWidth = paint.measureText(name, strlen(name)); |
| 90 | if (textWidth > width) { |
| 91 | width = textWidth; |
| 92 | } |
| 93 | } |
| 94 | SkScalar horizOffset = width + horizMargin; |
| 95 | SkScalar vertOffset = height + vertMargin; |
| 96 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 97 | |
| 98 | for (unsigned i = 0; i < NUM_CONFIGS; i++) { |
| 99 | canvas->save(); |
| 100 | // Draw destination config name |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 101 | const char* name = gColorTypeNames[fDst[i].colorType()]; |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 102 | SkScalar textWidth = paint.measureText(name, strlen(name)); |
| 103 | SkScalar x = (width - textWidth) / SkScalar(2); |
| 104 | SkScalar y = paint.getFontSpacing() / SkScalar(2); |
| 105 | canvas->drawText(name, strlen(name), x, y, paint); |
| 106 | |
| 107 | // Draw destination bitmap |
| 108 | canvas->translate(0, vertOffset); |
| 109 | x = (width - 40) / SkScalar(2); |
| 110 | canvas->drawBitmap(fDst[i], x, 0, &paint); |
| 111 | canvas->restore(); |
| 112 | |
| 113 | canvas->translate(horizOffset, 0); |
| 114 | } |
| 115 | } |
| 116 | |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 117 | private: |
| 118 | typedef GM INHERITED; |
| 119 | }; |
| 120 | |
| 121 | ////////////////////////////////////////////////////////////////////////////// |
| 122 | |
| 123 | static GM* MyFactory(void*) { return new BitmapCopyGM; } |
| 124 | static GMRegistry reg(MyFactory); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 125 | } |