1
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
10 namespace skiagm {
11
12 static const char* gConfigNames[] = {
13 "unknown config",
14 "A8",
15 "Index8",
16 "565",
17 "4444",
18 "8888"
19 };
20
21 SkBitmap::Config gConfigs[] = {
22 SkBitmap::kRGB_565_Config,
23 SkBitmap::kARGB_4444_Config, // TODO(edisonn): Should we remove it from GM?
24 // it fails to copy in bitmap with this config.
25 SkBitmap::kARGB_8888_Config,
26 };
27
28 #define NUM_CONFIGS (sizeof(gConfigs) / sizeof(SkBitmap::Config))
29
draw_checks(SkCanvas * canvas,int width,int height)30 static void draw_checks(SkCanvas* canvas, int width, int height) {
31 SkPaint paint;
32 paint.setColor(SK_ColorRED);
33 canvas->drawRectCoords(SkIntToScalar(0), SkIntToScalar(0),
34 SkIntToScalar(width / 2), SkIntToScalar(height / 2), paint);
35 paint.setColor(SK_ColorGREEN);
36 canvas->drawRectCoords(SkIntToScalar(width / 2), SkIntToScalar(0),
37 SkIntToScalar(width), SkIntToScalar(height / 2), paint);
38 paint.setColor(SK_ColorBLUE);
39 canvas->drawRectCoords(SkIntToScalar(0), SkIntToScalar(height / 2),
40 SkIntToScalar(width / 2), SkIntToScalar(height), paint);
41 paint.setColor(SK_ColorYELLOW);
42 canvas->drawRectCoords(SkIntToScalar(width / 2), SkIntToScalar(height / 2),
43 SkIntToScalar(width), SkIntToScalar(height), paint);
44 }
45
46 class BitmapCopyGM : public GM {
47 public:
48 SkBitmap fDst[NUM_CONFIGS];
49
BitmapCopyGM()50 BitmapCopyGM() {
51 this->setBGColor(0xFFDDDDDD);
52 }
53
54 protected:
onShortName()55 virtual SkString onShortName() {
56 return SkString("bitmapcopy");
57 }
58
onISize()59 virtual SkISize onISize() {
60 return make_isize(540, 330);
61 }
62
onDraw(SkCanvas * canvas)63 virtual void onDraw(SkCanvas* canvas) {
64 SkPaint paint;
65 SkScalar horizMargin(SkIntToScalar(10));
66 SkScalar vertMargin(SkIntToScalar(10));
67
68 SkBitmapDevice devTmp(SkBitmap::kARGB_8888_Config, 40, 40, false);
69 SkCanvas canvasTmp(&devTmp);
70
71 draw_checks(&canvasTmp, 40, 40);
72 SkBitmap src = canvasTmp.getTopDevice()->accessBitmap(false);
73
74 for (unsigned i = 0; i < NUM_CONFIGS; ++i) {
75 if (!src.deepCopyTo(&fDst[i], gConfigs[i])) {
76 src.copyTo(&fDst[i], gConfigs[i]);
77 }
78 }
79
80 canvas->clear(0xFFDDDDDD);
81 paint.setAntiAlias(true);
82 SkScalar width = SkIntToScalar(40);
83 SkScalar height = SkIntToScalar(40);
84 if (paint.getFontSpacing() > height) {
85 height = paint.getFontSpacing();
86 }
87 for (unsigned i = 0; i < NUM_CONFIGS; i++) {
88 const char* name = gConfigNames[src.config()];
89 SkScalar textWidth = paint.measureText(name, strlen(name));
90 if (textWidth > width) {
91 width = textWidth;
92 }
93 }
94 SkScalar horizOffset = width + horizMargin;
95 SkScalar vertOffset = height + vertMargin;
96 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
97
98 for (unsigned i = 0; i < NUM_CONFIGS; i++) {
99 canvas->save();
100 // Draw destination config name
101 const char* name = gConfigNames[fDst[i].config()];
102 SkScalar textWidth = paint.measureText(name, strlen(name));
103 SkScalar x = (width - textWidth) / SkScalar(2);
104 SkScalar y = paint.getFontSpacing() / SkScalar(2);
105 canvas->drawText(name, strlen(name), x, y, paint);
106
107 // Draw destination bitmap
108 canvas->translate(0, vertOffset);
109 x = (width - 40) / SkScalar(2);
110 canvas->drawBitmap(fDst[i], x, 0, &paint);
111 canvas->restore();
112
113 canvas->translate(horizOffset, 0);
114 }
115 }
116
onGetFlags() const117 virtual uint32_t onGetFlags() const { return kSkipPicture_Flag
118 | kSkipPipe_Flag; }
119
120 private:
121 typedef GM INHERITED;
122 };
123
124 //////////////////////////////////////////////////////////////////////////////
125
126 #ifndef SK_BUILD_FOR_ANDROID
MyFactory(void *)127 static GM* MyFactory(void*) { return new BitmapCopyGM; }
128 static GMRegistry reg(MyFactory);
129 #endif
130 }
131