blob: 89ca5254488ebada77bb32963fafbdffbbd0d047 [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,
24 SkBitmap::kARGB_4444_Config,
25 SkBitmap::kARGB_8888_Config,
26};
27
28#define NUM_CONFIGS (sizeof(gConfigs) / sizeof(SkBitmap::Config))
29
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;
65 SkScalar horizMargin(SkIntToScalar(10));
66 SkScalar vertMargin(SkIntToScalar(10));
67
68 draw_checks(canvas, 40, 40);
bsalomon@google.comc9784782013-05-10 14:12:08 +000069 SkBitmap src = canvas->getTopDevice()->accessBitmap(false);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000070
71 for (unsigned i = 0; i < NUM_CONFIGS; ++i) {
72 if (!src.deepCopyTo(&fDst[i], gConfigs[i])) {
73 src.copyTo(&fDst[i], gConfigs[i]);
74 }
75 }
76
77 canvas->clear(0xFFDDDDDD);
78 paint.setAntiAlias(true);
79 SkScalar width = SkIntToScalar(40);
80 SkScalar height = SkIntToScalar(40);
81 if (paint.getFontSpacing() > height) {
82 height = paint.getFontSpacing();
83 }
84 for (unsigned i = 0; i < NUM_CONFIGS; i++) {
85 const char* name = gConfigNames[src.config()];
86 SkScalar textWidth = paint.measureText(name, strlen(name));
87 if (textWidth > width) {
88 width = textWidth;
89 }
90 }
91 SkScalar horizOffset = width + horizMargin;
92 SkScalar vertOffset = height + vertMargin;
93 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
94
95 for (unsigned i = 0; i < NUM_CONFIGS; i++) {
96 canvas->save();
97 // Draw destination config name
98 const char* name = gConfigNames[fDst[i].config()];
99 SkScalar textWidth = paint.measureText(name, strlen(name));
100 SkScalar x = (width - textWidth) / SkScalar(2);
101 SkScalar y = paint.getFontSpacing() / SkScalar(2);
102 canvas->drawText(name, strlen(name), x, y, paint);
103
104 // Draw destination bitmap
105 canvas->translate(0, vertOffset);
106 x = (width - 40) / SkScalar(2);
107 canvas->drawBitmap(fDst[i], x, 0, &paint);
108 canvas->restore();
109
110 canvas->translate(horizOffset, 0);
111 }
112 }
113
borenet@google.comb4e70432012-07-20 18:45:10 +0000114 virtual uint32_t onGetFlags() const { return kSkipPicture_Flag
115 | kSkipPipe_Flag; }
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000116
117private:
118 typedef GM INHERITED;
119};
120
121//////////////////////////////////////////////////////////////////////////////
122
borenet@google.com03fc17c2012-07-20 18:19:06 +0000123#ifndef SK_BUILD_FOR_ANDROID
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000124static GM* MyFactory(void*) { return new BitmapCopyGM; }
125static GMRegistry reg(MyFactory);
borenet@google.com03fc17c2012-07-20 18:19:06 +0000126#endif
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000127}