blob: a4f7f322904eaebeb5b682f6b42689c66b19c829 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +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 */
epoger@google.comd4af56c2011-07-25 16:27:59 +00008#include "gm.h"
9
10namespace 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 */
18static void make_bitmap(int quarterWidth, int quarterHeight, SkBitmap *bitmap) {
epoger@google.com4067d7d2011-07-25 16:56:37 +000019 SkPaint pRed, pWhite, pGreen, pBlue, pLine, pAlphaGray;
epoger@google.comd4af56c2011-07-25 16:27:59 +000020 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);
epoger@google.com4067d7d2011-07-25 16:56:37 +000026 pAlphaGray.setColor(0x66888888);
epoger@google.comd4af56c2011-07-25 16:27:59 +000027
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);
epoger@google.com4067d7d2011-07-25 16:56:37 +000041 canvas.drawRectCoords(w, h, w*3, h*3, pAlphaGray);
epoger@google.comd4af56c2011-07-25 16:27:59 +000042 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
47class BitmapScrollGM : public GM {
48public:
49 BitmapScrollGM() {
50 // Create the original bitmap.
51 make_bitmap(quarterWidth, quarterHeight, &origBitmap);
52 }
53
54protected:
55 virtual SkString onShortName() {
56 return SkString("bitmapscroll");
57 }
58
59 virtual SkISize onISize() {
60 return make_isize(800, 600);
61 }
62
63 virtual void onDraw(SkCanvas* canvas) {
64 SkIRect scrollCenterRegion = SkIRect::MakeXYWH(
epoger@google.com4067d7d2011-07-25 16:56:37 +000065 quarterWidth, quarterHeight, quarterWidth*2+1, quarterHeight*2+1);
epoger@google.comd4af56c2011-07-25 16:27:59 +000066 int x = quarterWidth;
67 int y = quarterHeight;
68 int xSpacing = quarterWidth * 20;
69 int ySpacing = quarterHeight * 16;
70
71 // Draw background and left-hand text labels.
72 canvas->drawColor(0xFFDDDDDD);
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
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 */
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 SkASSERT(origBitmap.copyTo(
125 &scrolledBitmap, origBitmap.config()));
126 SkASSERT(scrolledBitmap.scrollRect(
127 subset, scrollX * xMult, scrollY * yMult));
128 canvas->drawBitmap(scrolledBitmap, bitmapX, bitmapY);
129 }
130 }
131 }
132
133private:
134 typedef GM INHERITED;
135 static const int quarterWidth = 10;
136 static const int quarterHeight = 14;
137 SkBitmap origBitmap;
138};
139
140//////////////////////////////////////////////////////////////////////////////
141
142static GM* MyFactory(void*) { return new BitmapScrollGM; }
143static GMRegistry reg(MyFactory);
144
145}