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 | |
| 12 | static const char* gConfigNames[] = { |
| 13 | "unknown config", |
| 14 | "A1", |
| 15 | "A8", |
| 16 | "Index8", |
| 17 | "565", |
| 18 | "4444", |
| 19 | "8888" |
| 20 | }; |
| 21 | |
| 22 | SkBitmap::Config gConfigs[] = { |
| 23 | SkBitmap::kRGB_565_Config, |
commit-bot@chromium.org | 488a337 | 2013-08-07 17:03:09 +0000 | [diff] [blame] | 24 | SkBitmap::kARGB_4444_Config, // TODO(edisonn): Should we remove it from GM? |
| 25 | // it fails to copy in bitmap with this config. |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 26 | SkBitmap::kARGB_8888_Config, |
| 27 | }; |
| 28 | |
| 29 | #define NUM_CONFIGS (sizeof(gConfigs) / sizeof(SkBitmap::Config)) |
| 30 | |
| 31 | static void draw_checks(SkCanvas* canvas, int width, int height) { |
| 32 | SkPaint paint; |
| 33 | paint.setColor(SK_ColorRED); |
tomhudson@google.com | 7558925 | 2012-04-10 17:42:21 +0000 | [diff] [blame] | 34 | canvas->drawRectCoords(SkIntToScalar(0), SkIntToScalar(0), |
| 35 | SkIntToScalar(width / 2), SkIntToScalar(height / 2), paint); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 36 | paint.setColor(SK_ColorGREEN); |
tomhudson@google.com | 7558925 | 2012-04-10 17:42:21 +0000 | [diff] [blame] | 37 | canvas->drawRectCoords(SkIntToScalar(width / 2), SkIntToScalar(0), |
| 38 | SkIntToScalar(width), SkIntToScalar(height / 2), paint); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 39 | paint.setColor(SK_ColorBLUE); |
tomhudson@google.com | 7558925 | 2012-04-10 17:42:21 +0000 | [diff] [blame] | 40 | canvas->drawRectCoords(SkIntToScalar(0), SkIntToScalar(height / 2), |
| 41 | SkIntToScalar(width / 2), SkIntToScalar(height), paint); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 42 | paint.setColor(SK_ColorYELLOW); |
tomhudson@google.com | 7558925 | 2012-04-10 17:42:21 +0000 | [diff] [blame] | 43 | canvas->drawRectCoords(SkIntToScalar(width / 2), SkIntToScalar(height / 2), |
| 44 | SkIntToScalar(width), SkIntToScalar(height), paint); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | class BitmapCopyGM : public GM { |
| 48 | public: |
| 49 | SkBitmap fDst[NUM_CONFIGS]; |
| 50 | |
| 51 | BitmapCopyGM() { |
| 52 | this->setBGColor(0xFFDDDDDD); |
| 53 | } |
| 54 | |
| 55 | protected: |
| 56 | virtual SkString onShortName() { |
| 57 | return SkString("bitmapcopy"); |
| 58 | } |
| 59 | |
| 60 | virtual SkISize onISize() { |
| 61 | return make_isize(540, 330); |
| 62 | } |
| 63 | |
| 64 | virtual void onDraw(SkCanvas* canvas) { |
| 65 | SkPaint paint; |
| 66 | SkScalar horizMargin(SkIntToScalar(10)); |
| 67 | SkScalar vertMargin(SkIntToScalar(10)); |
| 68 | |
robertphillips@google.com | 9b051a3 | 2013-08-20 20:06:40 +0000 | [diff] [blame^] | 69 | SkDevice devTmp(SkBitmap::kARGB_8888_Config, 40, 40); |
commit-bot@chromium.org | 488a337 | 2013-08-07 17:03:09 +0000 | [diff] [blame] | 70 | SkCanvas canvasTmp(&devTmp); |
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); |
| 73 | SkBitmap src = canvasTmp.getTopDevice()->accessBitmap(false); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 74 | |
| 75 | for (unsigned i = 0; i < NUM_CONFIGS; ++i) { |
| 76 | if (!src.deepCopyTo(&fDst[i], gConfigs[i])) { |
| 77 | src.copyTo(&fDst[i], gConfigs[i]); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | canvas->clear(0xFFDDDDDD); |
| 82 | paint.setAntiAlias(true); |
| 83 | SkScalar width = SkIntToScalar(40); |
| 84 | SkScalar height = SkIntToScalar(40); |
| 85 | if (paint.getFontSpacing() > height) { |
| 86 | height = paint.getFontSpacing(); |
| 87 | } |
| 88 | for (unsigned i = 0; i < NUM_CONFIGS; i++) { |
| 89 | const char* name = gConfigNames[src.config()]; |
| 90 | SkScalar textWidth = paint.measureText(name, strlen(name)); |
| 91 | if (textWidth > width) { |
| 92 | width = textWidth; |
| 93 | } |
| 94 | } |
| 95 | SkScalar horizOffset = width + horizMargin; |
| 96 | SkScalar vertOffset = height + vertMargin; |
| 97 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 98 | |
| 99 | for (unsigned i = 0; i < NUM_CONFIGS; i++) { |
| 100 | canvas->save(); |
| 101 | // Draw destination config name |
| 102 | const char* name = gConfigNames[fDst[i].config()]; |
| 103 | SkScalar textWidth = paint.measureText(name, strlen(name)); |
| 104 | SkScalar x = (width - textWidth) / SkScalar(2); |
| 105 | SkScalar y = paint.getFontSpacing() / SkScalar(2); |
| 106 | canvas->drawText(name, strlen(name), x, y, paint); |
| 107 | |
| 108 | // Draw destination bitmap |
| 109 | canvas->translate(0, vertOffset); |
| 110 | x = (width - 40) / SkScalar(2); |
| 111 | canvas->drawBitmap(fDst[i], x, 0, &paint); |
| 112 | canvas->restore(); |
| 113 | |
| 114 | canvas->translate(horizOffset, 0); |
| 115 | } |
| 116 | } |
| 117 | |
borenet@google.com | b4e7043 | 2012-07-20 18:45:10 +0000 | [diff] [blame] | 118 | virtual uint32_t onGetFlags() const { return kSkipPicture_Flag |
| 119 | | kSkipPipe_Flag; } |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 120 | |
| 121 | private: |
| 122 | typedef GM INHERITED; |
| 123 | }; |
| 124 | |
| 125 | ////////////////////////////////////////////////////////////////////////////// |
| 126 | |
borenet@google.com | 03fc17c | 2012-07-20 18:19:06 +0000 | [diff] [blame] | 127 | #ifndef SK_BUILD_FOR_ANDROID |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 128 | static GM* MyFactory(void*) { return new BitmapCopyGM; } |
| 129 | static GMRegistry reg(MyFactory); |
borenet@google.com | 03fc17c | 2012-07-20 18:19:06 +0000 | [diff] [blame] | 130 | #endif |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 131 | } |