blob: 5e88cfdd0fe2e0abf4fdf7be119163c834f13f82 [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
12static const char* gConfigNames[] = {
13 "unknown config",
14 "A1",
15 "A8",
16 "Index8",
17 "565",
18 "4444",
19 "8888"
20};
21
22SkBitmap::Config gConfigs[] = {
23 SkBitmap::kRGB_565_Config,
commit-bot@chromium.org488a3372013-08-07 17:03:09 +000024 SkBitmap::kARGB_4444_Config, // TODO(edisonn): Should we remove it from GM?
25 // it fails to copy in bitmap with this config.
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000026 SkBitmap::kARGB_8888_Config,
27};
28
29#define NUM_CONFIGS (sizeof(gConfigs) / sizeof(SkBitmap::Config))
30
31static void draw_checks(SkCanvas* canvas, int width, int height) {
32 SkPaint paint;
33 paint.setColor(SK_ColorRED);
tomhudson@google.com75589252012-04-10 17:42:21 +000034 canvas->drawRectCoords(SkIntToScalar(0), SkIntToScalar(0),
35 SkIntToScalar(width / 2), SkIntToScalar(height / 2), paint);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000036 paint.setColor(SK_ColorGREEN);
tomhudson@google.com75589252012-04-10 17:42:21 +000037 canvas->drawRectCoords(SkIntToScalar(width / 2), SkIntToScalar(0),
38 SkIntToScalar(width), SkIntToScalar(height / 2), paint);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000039 paint.setColor(SK_ColorBLUE);
tomhudson@google.com75589252012-04-10 17:42:21 +000040 canvas->drawRectCoords(SkIntToScalar(0), SkIntToScalar(height / 2),
41 SkIntToScalar(width / 2), SkIntToScalar(height), paint);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000042 paint.setColor(SK_ColorYELLOW);
tomhudson@google.com75589252012-04-10 17:42:21 +000043 canvas->drawRectCoords(SkIntToScalar(width / 2), SkIntToScalar(height / 2),
44 SkIntToScalar(width), SkIntToScalar(height), paint);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000045}
46
47class BitmapCopyGM : public GM {
48public:
49 SkBitmap fDst[NUM_CONFIGS];
50
51 BitmapCopyGM() {
52 this->setBGColor(0xFFDDDDDD);
53 }
54
55protected:
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
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000069 SkBitmapDevice devTmp(SkBitmap::kARGB_8888_Config, 40, 40, false);
commit-bot@chromium.org488a3372013-08-07 17:03:09 +000070 SkCanvas canvasTmp(&devTmp);
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);
73 SkBitmap src = canvasTmp.getTopDevice()->accessBitmap(false);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000074
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
borenet@google.comb4e70432012-07-20 18:45:10 +0000118 virtual uint32_t onGetFlags() const { return kSkipPicture_Flag
119 | kSkipPipe_Flag; }
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000120
121private:
122 typedef GM INHERITED;
123};
124
125//////////////////////////////////////////////////////////////////////////////
126
borenet@google.com03fc17c2012-07-20 18:19:06 +0000127#ifndef SK_BUILD_FOR_ANDROID
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000128static GM* MyFactory(void*) { return new BitmapCopyGM; }
129static GMRegistry reg(MyFactory);
borenet@google.com03fc17c2012-07-20 18:19:06 +0000130#endif
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000131}