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, // TODO(edisonn): Should we remove it from GM? 25 // it fails to copy in bitmap with this config. 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); 34 canvas->drawRectCoords(SkIntToScalar(0), SkIntToScalar(0), 35 SkIntToScalar(width / 2), SkIntToScalar(height / 2), paint); 36 paint.setColor(SK_ColorGREEN); 37 canvas->drawRectCoords(SkIntToScalar(width / 2), SkIntToScalar(0), 38 SkIntToScalar(width), SkIntToScalar(height / 2), paint); 39 paint.setColor(SK_ColorBLUE); 40 canvas->drawRectCoords(SkIntToScalar(0), SkIntToScalar(height / 2), 41 SkIntToScalar(width / 2), SkIntToScalar(height), paint); 42 paint.setColor(SK_ColorYELLOW); 43 canvas->drawRectCoords(SkIntToScalar(width / 2), SkIntToScalar(height / 2), 44 SkIntToScalar(width), SkIntToScalar(height), paint); 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 69 SkDevice devTmp(SkBitmap::kARGB_8888_Config, 40, 40); 70 SkCanvas canvasTmp(&devTmp); 71 72 draw_checks(&canvasTmp, 40, 40); 73 SkBitmap src = canvasTmp.getTopDevice()->accessBitmap(false); 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 118 virtual uint32_t onGetFlags() const { return kSkipPicture_Flag 119 | kSkipPipe_Flag; } 120 121 private: 122 typedef GM INHERITED; 123 }; 124 125 ////////////////////////////////////////////////////////////////////////////// 126 127 #ifndef SK_BUILD_FOR_ANDROID 128 static GM* MyFactory(void*) { return new BitmapCopyGM; } 129 static GMRegistry reg(MyFactory); 130 #endif 131 } 132