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