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 #include "sk_tool_utils.h" 9 10 namespace skiagm { 11 12 static const char* gColorTypeNames[] = { 13 "unknown", 14 "A8", 15 "565", 16 "4444", 17 "8888", 18 "8888", 19 "Index8", 20 }; 21 22 constexpr SkColorType gColorTypes[] = { 23 kRGB_565_SkColorType, 24 kARGB_4444_SkColorType, 25 kN32_SkColorType, 26 }; 27 28 #define NUM_CONFIGS SK_ARRAY_COUNT(gColorTypes) 29 30 static void draw_checks(SkCanvas* canvas, int width, int height) { 31 SkPaint paint; 32 paint.setColor(SK_ColorRED); 33 canvas->drawRect(SkRect::MakeIWH(width/2, height/2), paint); 34 paint.setColor(SK_ColorGREEN); 35 canvas->drawRect({ SkIntToScalar(width/2), 0, SkIntToScalar(width), SkIntToScalar(height/2) }, 36 paint); 37 paint.setColor(SK_ColorBLUE); 38 canvas->drawRect({ 0, SkIntToScalar(height/2), SkIntToScalar(width/2), SkIntToScalar(height) }, 39 paint); 40 paint.setColor(SK_ColorYELLOW); 41 canvas->drawRect({ SkIntToScalar(width/2), SkIntToScalar(height/2), SkIntToScalar(width), 42 SkIntToScalar(height) }, paint); 43 } 44 45 class BitmapCopyGM : public GM { 46 public: 47 SkBitmap fDst[NUM_CONFIGS]; 48 49 BitmapCopyGM() { 50 this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD)); 51 } 52 53 protected: 54 virtual SkString onShortName() { 55 return SkString("bitmapcopy"); 56 } 57 58 virtual SkISize onISize() { 59 return SkISize::Make(540, 330); 60 } 61 62 virtual void onDraw(SkCanvas* canvas) { 63 SkPaint paint; 64 SkScalar horizMargin = 10; 65 SkScalar vertMargin = 10; 66 67 SkBitmap src; 68 src.allocN32Pixels(40, 40, kOpaque_SkAlphaType); 69 SkCanvas canvasTmp(src); 70 71 draw_checks(&canvasTmp, 40, 40); 72 73 for (unsigned i = 0; i < NUM_CONFIGS; ++i) { 74 sk_tool_utils::copy_to(&fDst[i], gColorTypes[i], src); 75 } 76 77 canvas->clear(sk_tool_utils::color_to_565(0xFFDDDDDD)); 78 paint.setAntiAlias(true); 79 sk_tool_utils::set_portable_typeface(&paint); 80 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++) { 87 const char* name = gColorTypeNames[src.colorType()]; 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 100 const char* name = gColorTypeNames[fDst[i].colorType()]; 101 SkScalar textWidth = paint.measureText(name, strlen(name)); 102 SkScalar x = (width - textWidth) / SkScalar(2); 103 SkScalar y = paint.getFontSpacing() / SkScalar(2); 104 canvas->drawString(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 116 private: 117 typedef GM INHERITED; 118 }; 119 120 ////////////////////////////////////////////////////////////////////////////// 121 122 static GM* MyFactory(void*) { return new BitmapCopyGM; } 123 static GMRegistry reg(MyFactory); 124 } 125