blob: b52337b4bfb4d92288670d2d9ad1d026f1af8623 [file] [log] [blame]
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001/*
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
9namespace skiagm {
10
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000011static const char* gColorTypeNames[] = {
12 "unknown",
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000013 "A8",
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000014 "565",
15 "4444",
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000016 "8888",
17 "8888",
18 "Index8",
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000019};
20
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000021static const SkColorType gColorTypes[] = {
22 kRGB_565_SkColorType,
23 kARGB_4444_SkColorType,
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000024 kN32_SkColorType,
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000025};
26
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000027#define NUM_CONFIGS SK_ARRAY_COUNT(gColorTypes)
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000028
29static void draw_checks(SkCanvas* canvas, int width, int height) {
30 SkPaint paint;
31 paint.setColor(SK_ColorRED);
tomhudson@google.com75589252012-04-10 17:42:21 +000032 canvas->drawRectCoords(SkIntToScalar(0), SkIntToScalar(0),
33 SkIntToScalar(width / 2), SkIntToScalar(height / 2), paint);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000034 paint.setColor(SK_ColorGREEN);
tomhudson@google.com75589252012-04-10 17:42:21 +000035 canvas->drawRectCoords(SkIntToScalar(width / 2), SkIntToScalar(0),
36 SkIntToScalar(width), SkIntToScalar(height / 2), paint);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000037 paint.setColor(SK_ColorBLUE);
tomhudson@google.com75589252012-04-10 17:42:21 +000038 canvas->drawRectCoords(SkIntToScalar(0), SkIntToScalar(height / 2),
39 SkIntToScalar(width / 2), SkIntToScalar(height), paint);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000040 paint.setColor(SK_ColorYELLOW);
tomhudson@google.com75589252012-04-10 17:42:21 +000041 canvas->drawRectCoords(SkIntToScalar(width / 2), SkIntToScalar(height / 2),
42 SkIntToScalar(width), SkIntToScalar(height), paint);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000043}
44
45class BitmapCopyGM : public GM {
46public:
47 SkBitmap fDst[NUM_CONFIGS];
48
49 BitmapCopyGM() {
caryclark65cdba62015-06-15 06:51:08 -070050 this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000051 }
52
53protected:
54 virtual SkString onShortName() {
55 return SkString("bitmapcopy");
56 }
57
58 virtual SkISize onISize() {
tfarinaf5393182014-06-09 23:59:03 -070059 return SkISize::Make(540, 330);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000060 }
61
62 virtual void onDraw(SkCanvas* canvas) {
63 SkPaint paint;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000064 SkScalar horizMargin = 10;
65 SkScalar vertMargin = 10;
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000066
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000067 SkBitmap src;
68 src.allocN32Pixels(40, 40);
69 SkCanvas canvasTmp(src);
skia.committer@gmail.comd55e3572013-08-08 07:01:20 +000070
commit-bot@chromium.org488a3372013-08-07 17:03:09 +000071 draw_checks(&canvasTmp, 40, 40);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000072
73 for (unsigned i = 0; i < NUM_CONFIGS; ++i) {
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000074 src.copyTo(&fDst[i], gColorTypes[i]);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000075 }
76
caryclark65cdba62015-06-15 06:51:08 -070077 canvas->clear(sk_tool_utils::color_to_565(0xFFDDDDDD));
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000078 paint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070079 sk_tool_utils::set_portable_typeface(&paint);
halcanary9d524f22016-03-29 09:03:52 -070080
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000081 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++) {
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000087 const char* name = gColorTypeNames[src.colorType()];
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000088 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
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000100 const char* name = gColorTypeNames[fDst[i].colorType()];
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000101 SkScalar textWidth = paint.measureText(name, strlen(name));
102 SkScalar x = (width - textWidth) / SkScalar(2);
103 SkScalar y = paint.getFontSpacing() / SkScalar(2);
104 canvas->drawText(name, strlen(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
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000116private:
117 typedef GM INHERITED;
118};
119
120//////////////////////////////////////////////////////////////////////////////
121
122static GM* MyFactory(void*) { return new BitmapCopyGM; }
123static GMRegistry reg(MyFactory);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000124}