2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "sk_tool_utils.h"
12 static const char* gColorTypeNames[] = {
22 constexpr SkColorType gColorTypes[] = {
24 kARGB_4444_SkColorType,
28 #define NUM_CONFIGS SK_ARRAY_COUNT(gColorTypes)
30 static void draw_checks(SkCanvas* canvas, int width, int height) {
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) },
37 paint.setColor(SK_ColorBLUE);
38 canvas->drawRect({ 0, SkIntToScalar(height/2), SkIntToScalar(width/2), SkIntToScalar(height) },
40 paint.setColor(SK_ColorYELLOW);
41 canvas->drawRect({ SkIntToScalar(width/2), SkIntToScalar(height/2), SkIntToScalar(width),
42 SkIntToScalar(height) }, paint);
45 class BitmapCopyGM : public GM {
47 SkBitmap fDst[NUM_CONFIGS];
50 this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
54 virtual SkString onShortName() {
55 return SkString("bitmapcopy");
58 virtual SkISize onISize() {
59 return SkISize::Make(540, 330);
62 virtual void onDraw(SkCanvas* canvas) {
64 SkScalar horizMargin = 10;
65 SkScalar vertMargin = 10;
68 src.allocN32Pixels(40, 40, kOpaque_SkAlphaType);
69 SkCanvas canvasTmp(src);
71 draw_checks(&canvasTmp, 40, 40);
73 for (unsigned i = 0; i < NUM_CONFIGS; ++i) {
74 sk_tool_utils::copy_to(&fDst[i], gColorTypes[i], src);
77 canvas->clear(sk_tool_utils::color_to_565(0xFFDDDDDD));
78 paint.setAntiAlias(true);
79 sk_tool_utils::set_portable_typeface(&paint);
81 SkScalar width = SkIntToScalar(40);
82 SkScalar height = SkIntToScalar(40);
83 if (paint.getFontSpacing() > height) {
84 height = paint.getFontSpacing();
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) {
93 SkScalar horizOffset = width + horizMargin;
94 SkScalar vertOffset = height + vertMargin;
95 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
97 for (unsigned i = 0; i < NUM_CONFIGS; i++) {
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);
106 // Draw destination bitmap
107 canvas->translate(0, vertOffset);
108 x = (width - 40) / SkScalar(2);
109 canvas->drawBitmap(fDst[i], x, 0, &paint);
112 canvas->translate(horizOffset, 0);
117 typedef GM INHERITED;
120 //////////////////////////////////////////////////////////////////////////////
122 static GM* MyFactory(void*) { return new BitmapCopyGM; }
123 static GMRegistry reg(MyFactory);