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, |
| 24 | SkBitmap::kARGB_4444_Config, |
| 25 | SkBitmap::kARGB_8888_Config, |
| 26 | }; |
| 27 | |
| 28 | #define NUM_CONFIGS (sizeof(gConfigs) / sizeof(SkBitmap::Config)) |
| 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() { |
| 51 | this->setBGColor(0xFFDDDDDD); |
| 52 | } |
| 53 | |
| 54 | protected: |
| 55 | virtual SkString onShortName() { |
| 56 | return SkString("bitmapcopy"); |
| 57 | } |
| 58 | |
| 59 | virtual SkISize onISize() { |
| 60 | return make_isize(540, 330); |
| 61 | } |
| 62 | |
| 63 | virtual void onDraw(SkCanvas* canvas) { |
| 64 | SkPaint paint; |
| 65 | SkScalar horizMargin(SkIntToScalar(10)); |
| 66 | SkScalar vertMargin(SkIntToScalar(10)); |
| 67 | |
| 68 | draw_checks(canvas, 40, 40); |
bsalomon@google.com | c978478 | 2013-05-10 14:12:08 +0000 | [diff] [blame^] | 69 | SkBitmap src = canvas->getTopDevice()->accessBitmap(false); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 70 | |
| 71 | for (unsigned i = 0; i < NUM_CONFIGS; ++i) { |
| 72 | if (!src.deepCopyTo(&fDst[i], gConfigs[i])) { |
| 73 | src.copyTo(&fDst[i], gConfigs[i]); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | canvas->clear(0xFFDDDDDD); |
| 78 | paint.setAntiAlias(true); |
| 79 | SkScalar width = SkIntToScalar(40); |
| 80 | SkScalar height = SkIntToScalar(40); |
| 81 | if (paint.getFontSpacing() > height) { |
| 82 | height = paint.getFontSpacing(); |
| 83 | } |
| 84 | for (unsigned i = 0; i < NUM_CONFIGS; i++) { |
| 85 | const char* name = gConfigNames[src.config()]; |
| 86 | SkScalar textWidth = paint.measureText(name, strlen(name)); |
| 87 | if (textWidth > width) { |
| 88 | width = textWidth; |
| 89 | } |
| 90 | } |
| 91 | SkScalar horizOffset = width + horizMargin; |
| 92 | SkScalar vertOffset = height + vertMargin; |
| 93 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 94 | |
| 95 | for (unsigned i = 0; i < NUM_CONFIGS; i++) { |
| 96 | canvas->save(); |
| 97 | // Draw destination config name |
| 98 | const char* name = gConfigNames[fDst[i].config()]; |
| 99 | SkScalar textWidth = paint.measureText(name, strlen(name)); |
| 100 | SkScalar x = (width - textWidth) / SkScalar(2); |
| 101 | SkScalar y = paint.getFontSpacing() / SkScalar(2); |
| 102 | canvas->drawText(name, strlen(name), x, y, paint); |
| 103 | |
| 104 | // Draw destination bitmap |
| 105 | canvas->translate(0, vertOffset); |
| 106 | x = (width - 40) / SkScalar(2); |
| 107 | canvas->drawBitmap(fDst[i], x, 0, &paint); |
| 108 | canvas->restore(); |
| 109 | |
| 110 | canvas->translate(horizOffset, 0); |
| 111 | } |
| 112 | } |
| 113 | |
borenet@google.com | b4e7043 | 2012-07-20 18:45:10 +0000 | [diff] [blame] | 114 | virtual uint32_t onGetFlags() const { return kSkipPicture_Flag |
| 115 | | kSkipPipe_Flag; } |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 116 | |
| 117 | private: |
| 118 | typedef GM INHERITED; |
| 119 | }; |
| 120 | |
| 121 | ////////////////////////////////////////////////////////////////////////////// |
| 122 | |
borenet@google.com | 03fc17c | 2012-07-20 18:19:06 +0000 | [diff] [blame] | 123 | #ifndef SK_BUILD_FOR_ANDROID |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 124 | static GM* MyFactory(void*) { return new BitmapCopyGM; } |
| 125 | static GMRegistry reg(MyFactory); |
borenet@google.com | 03fc17c | 2012-07-20 18:19:06 +0000 | [diff] [blame] | 126 | #endif |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 127 | } |