blob: fff565fdb60c8702c4c084e0bbd60b85c0d051d8 [file] [log] [blame]
epoger@google.comd4af56c2011-07-25 16:27:59 +00001#include "gm.h"
2
3namespace skiagm {
4
5/** Create a bitmap image suitable for testing SkBitmap::scrollRect().
6 *
7 * @param quarterWidth bitmap will be 4x this many pixels wide
8 * @param quarterHeight bitmap will be 4x this many pixels tall
9 * @param bitmap the bitmap data is written into this object
10 */
11static void make_bitmap(int quarterWidth, int quarterHeight, SkBitmap *bitmap) {
epoger@google.com4067d7d2011-07-25 16:56:37 +000012 SkPaint pRed, pWhite, pGreen, pBlue, pLine, pAlphaGray;
epoger@google.comd4af56c2011-07-25 16:27:59 +000013 pRed.setColor(0xFFFF9999);
14 pWhite.setColor(0xFFFFFFFF);
15 pGreen.setColor(0xFF99FF99);
16 pBlue.setColor(0xFF9999FF);
17 pLine.setColor(0xFF000000);
18 pLine.setStyle(SkPaint::kStroke_Style);
epoger@google.com4067d7d2011-07-25 16:56:37 +000019 pAlphaGray.setColor(0x66888888);
epoger@google.comd4af56c2011-07-25 16:27:59 +000020
21 // Prepare bitmap, and a canvas that draws into it.
22 bitmap->reset();
23 bitmap->setConfig(SkBitmap::kARGB_8888_Config,
24 quarterWidth*4, quarterHeight*4);
25 bitmap->allocPixels();
26 SkCanvas canvas(*bitmap);
27
28 SkScalar w = SkIntToScalar(quarterWidth);
29 SkScalar h = SkIntToScalar(quarterHeight);
30 canvas.drawRectCoords( 0, 0, w*2, h*2, pRed);
31 canvas.drawRectCoords(w*2, 0, w*4, h*2, pGreen);
32 canvas.drawRectCoords( 0, h*2, w*2, h*4, pBlue);
33 canvas.drawRectCoords(w*2, h*2, w*4, h*4, pWhite);
epoger@google.com4067d7d2011-07-25 16:56:37 +000034 canvas.drawRectCoords(w, h, w*3, h*3, pAlphaGray);
epoger@google.comd4af56c2011-07-25 16:27:59 +000035 canvas.drawLine(w*2, 0, w*2, h*4, pLine);
36 canvas.drawLine( 0, h*2, w*4, h*2, pLine);
37 canvas.drawRectCoords(w, h, w*3, h*3, pLine);
38}
39
40class BitmapScrollGM : public GM {
41public:
42 BitmapScrollGM() {
43 // Create the original bitmap.
44 make_bitmap(quarterWidth, quarterHeight, &origBitmap);
45 }
46
47protected:
48 virtual SkString onShortName() {
49 return SkString("bitmapscroll");
50 }
51
52 virtual SkISize onISize() {
53 return make_isize(800, 600);
54 }
55
56 virtual void onDraw(SkCanvas* canvas) {
57 SkIRect scrollCenterRegion = SkIRect::MakeXYWH(
epoger@google.com4067d7d2011-07-25 16:56:37 +000058 quarterWidth, quarterHeight, quarterWidth*2+1, quarterHeight*2+1);
epoger@google.comd4af56c2011-07-25 16:27:59 +000059 int x = quarterWidth;
60 int y = quarterHeight;
61 int xSpacing = quarterWidth * 20;
62 int ySpacing = quarterHeight * 16;
63
64 // Draw background and left-hand text labels.
65 canvas->drawColor(0xFFDDDDDD);
66 drawLabel(canvas, "scroll entire bitmap",
67 x, y, x, y + ySpacing);
68 drawLabel(canvas, "scroll part of bitmap",
69 x, y + ySpacing, x, y + ySpacing*2);
70 x += 30;
71
72 // Draw various permutations of scrolled bitmaps, scrolling a bit
73 // further each time.
74 draw9(canvas, x, y, NULL, quarterWidth*1/2, quarterHeight*1/2);
75 draw9(canvas, x, y+ySpacing, &scrollCenterRegion,
76 quarterWidth*1/2, quarterHeight*1/2);
77 x += xSpacing;
78 draw9(canvas, x, y, NULL, quarterWidth*3/2, quarterHeight*3/2);
79 draw9(canvas, x, y+ySpacing, &scrollCenterRegion,
80 quarterWidth*3/2, quarterHeight*3/2);
81 x += xSpacing;
82 draw9(canvas, x, y, NULL, quarterWidth*5/2, quarterHeight*5/2);
83 draw9(canvas, x, y+ySpacing, &scrollCenterRegion,
84 quarterWidth*5/2, quarterHeight*5/2);
85 x += xSpacing;
86 draw9(canvas, x, y, NULL, quarterWidth*9/2, quarterHeight*9/2);
87 draw9(canvas, x, y+ySpacing, &scrollCenterRegion,
88 quarterWidth*9/2, quarterHeight*9/2);
89 }
90
91 void drawLabel(SkCanvas* canvas, const char *text, int startX, int startY,
92 int endX, int endY) {
93 SkPaint paint;
94 paint.setColor(0xFF000000);
95 SkPath path;
96 path.moveTo(SkIntToScalar(startX), SkIntToScalar(startY));
97 path.lineTo(SkIntToScalar(endX), SkIntToScalar(endY));
98 canvas->drawTextOnPath(text, strlen(text), path, NULL, paint);
99 }
100
101 /** Stamp out 9 copies of origBitmap, scrolled in each direction (and
102 * not scrolled at all).
103 */
104 void draw9(SkCanvas* canvas, int x, int y, SkIRect* subset,
105 int scrollX, int scrollY) {
106 for (int yMult=-1; yMult<=1; yMult++) {
107 for (int xMult=-1; xMult<=1; xMult++) {
108 // Figure out the (x,y) to draw this copy at
109 SkScalar bitmapX = SkIntToScalar(
110 x + quarterWidth * 5 * (xMult+1));
111 SkScalar bitmapY = SkIntToScalar(
112 y + quarterHeight * 5 * (yMult+1));
113
114 // Scroll a new copy of the bitmap, and then draw it.
115 // scrollRect() should always return true, even if it's a no-op
116 SkBitmap scrolledBitmap;
117 SkASSERT(origBitmap.copyTo(
118 &scrolledBitmap, origBitmap.config()));
119 SkASSERT(scrolledBitmap.scrollRect(
120 subset, scrollX * xMult, scrollY * yMult));
121 canvas->drawBitmap(scrolledBitmap, bitmapX, bitmapY);
122 }
123 }
124 }
125
126private:
127 typedef GM INHERITED;
128 static const int quarterWidth = 10;
129 static const int quarterHeight = 14;
130 SkBitmap origBitmap;
131};
132
133//////////////////////////////////////////////////////////////////////////////
134
135static GM* MyFactory(void*) { return new BitmapScrollGM; }
136static GMRegistry reg(MyFactory);
137
138}