blob: 68db90802433eca29b70c372d7f0f73f2071a153 [file] [log] [blame]
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001
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
10namespace skiagm {
11
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000012static const char* gColorTypeNames[] = {
13 "unknown",
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000014 "A8",
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000015 "565",
16 "4444",
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000017 "8888",
18 "8888",
19 "Index8",
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000020};
21
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000022static const SkColorType gColorTypes[] = {
23 kRGB_565_SkColorType,
24 kARGB_4444_SkColorType,
25 kPMColor_SkColorType,
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000026};
27
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000028#define NUM_CONFIGS SK_ARRAY_COUNT(gColorTypes)
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000029
30static void draw_checks(SkCanvas* canvas, int width, int height) {
31 SkPaint paint;
32 paint.setColor(SK_ColorRED);
tomhudson@google.com75589252012-04-10 17:42:21 +000033 canvas->drawRectCoords(SkIntToScalar(0), SkIntToScalar(0),
34 SkIntToScalar(width / 2), SkIntToScalar(height / 2), paint);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000035 paint.setColor(SK_ColorGREEN);
tomhudson@google.com75589252012-04-10 17:42:21 +000036 canvas->drawRectCoords(SkIntToScalar(width / 2), SkIntToScalar(0),
37 SkIntToScalar(width), SkIntToScalar(height / 2), paint);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000038 paint.setColor(SK_ColorBLUE);
tomhudson@google.com75589252012-04-10 17:42:21 +000039 canvas->drawRectCoords(SkIntToScalar(0), SkIntToScalar(height / 2),
40 SkIntToScalar(width / 2), SkIntToScalar(height), paint);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000041 paint.setColor(SK_ColorYELLOW);
tomhudson@google.com75589252012-04-10 17:42:21 +000042 canvas->drawRectCoords(SkIntToScalar(width / 2), SkIntToScalar(height / 2),
43 SkIntToScalar(width), SkIntToScalar(height), paint);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000044}
45
46class BitmapCopyGM : public GM {
47public:
48 SkBitmap fDst[NUM_CONFIGS];
49
50 BitmapCopyGM() {
51 this->setBGColor(0xFFDDDDDD);
52 }
53
54protected:
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;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000065 SkScalar horizMargin = 10;
66 SkScalar vertMargin = 10;
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000067
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000068 SkBitmap src;
69 src.allocN32Pixels(40, 40);
70 SkCanvas canvasTmp(src);
skia.committer@gmail.comd55e3572013-08-08 07:01:20 +000071
commit-bot@chromium.org488a3372013-08-07 17:03:09 +000072 draw_checks(&canvasTmp, 40, 40);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000073
74 for (unsigned i = 0; i < NUM_CONFIGS; ++i) {
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000075 src.copyTo(&fDst[i], gColorTypes[i]);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000076 }
77
78 canvas->clear(0xFFDDDDDD);
79 paint.setAntiAlias(true);
80 SkScalar width = SkIntToScalar(40);
81 SkScalar height = SkIntToScalar(40);
82 if (paint.getFontSpacing() > height) {
83 height = paint.getFontSpacing();
84 }
85 for (unsigned i = 0; i < NUM_CONFIGS; i++) {
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000086 const char* name = gColorTypeNames[src.colorType()];
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000087 SkScalar textWidth = paint.measureText(name, strlen(name));
88 if (textWidth > width) {
89 width = textWidth;
90 }
91 }
92 SkScalar horizOffset = width + horizMargin;
93 SkScalar vertOffset = height + vertMargin;
94 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
95
96 for (unsigned i = 0; i < NUM_CONFIGS; i++) {
97 canvas->save();
98 // Draw destination config name
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000099 const char* name = gColorTypeNames[fDst[i].colorType()];
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000100 SkScalar textWidth = paint.measureText(name, strlen(name));
101 SkScalar x = (width - textWidth) / SkScalar(2);
102 SkScalar y = paint.getFontSpacing() / SkScalar(2);
103 canvas->drawText(name, strlen(name), x, y, paint);
104
105 // Draw destination bitmap
106 canvas->translate(0, vertOffset);
107 x = (width - 40) / SkScalar(2);
108 canvas->drawBitmap(fDst[i], x, 0, &paint);
109 canvas->restore();
110
111 canvas->translate(horizOffset, 0);
112 }
113 }
114
borenet@google.comb4e70432012-07-20 18:45:10 +0000115 virtual uint32_t onGetFlags() const { return kSkipPicture_Flag
116 | kSkipPipe_Flag; }
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000117
118private:
119 typedef GM INHERITED;
120};
121
122//////////////////////////////////////////////////////////////////////////////
123
borenet@google.com03fc17c2012-07-20 18:19:06 +0000124#ifndef SK_BUILD_FOR_ANDROID
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000125static GM* MyFactory(void*) { return new BitmapCopyGM; }
126static GMRegistry reg(MyFactory);
borenet@google.com03fc17c2012-07-20 18:19:06 +0000127#endif
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000128}