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