blob: a1c0ce4702dcc760ba04b63a6fd641633900a58f [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 {
reed@google.com9afc6562012-03-22 14:09:28 +000048 bool fInited;
49 void init() {
50 if (fInited) {
51 return;
52 }
53 fInited = true;
epoger@google.comd4af56c2011-07-25 16:27:59 +000054 // Create the original bitmap.
55 make_bitmap(quarterWidth, quarterHeight, &origBitmap);
reed@google.com9afc6562012-03-22 14:09:28 +000056 }
57
58public:
59 BitmapScrollGM() {
60 fInited = false;
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000061 this->setBGColor(0xFFDDDDDD);
epoger@google.comd4af56c2011-07-25 16:27:59 +000062 }
63
64protected:
65 virtual SkString onShortName() {
66 return SkString("bitmapscroll");
67 }
68
69 virtual SkISize onISize() {
70 return make_isize(800, 600);
71 }
72
73 virtual void onDraw(SkCanvas* canvas) {
reed@google.com9afc6562012-03-22 14:09:28 +000074 this->init();
epoger@google.comd4af56c2011-07-25 16:27:59 +000075 SkIRect scrollCenterRegion = SkIRect::MakeXYWH(
epoger@google.com4067d7d2011-07-25 16:56:37 +000076 quarterWidth, quarterHeight, quarterWidth*2+1, quarterHeight*2+1);
epoger@google.comd4af56c2011-07-25 16:27:59 +000077 int x = quarterWidth;
78 int y = quarterHeight;
79 int xSpacing = quarterWidth * 20;
80 int ySpacing = quarterHeight * 16;
81
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000082 // Draw left-hand text labels.
epoger@google.comd4af56c2011-07-25 16:27:59 +000083 drawLabel(canvas, "scroll entire bitmap",
84 x, y, x, y + ySpacing);
85 drawLabel(canvas, "scroll part of bitmap",
86 x, y + ySpacing, x, y + ySpacing*2);
87 x += 30;
88
89 // Draw various permutations of scrolled bitmaps, scrolling a bit
90 // further each time.
91 draw9(canvas, x, y, NULL, quarterWidth*1/2, quarterHeight*1/2);
92 draw9(canvas, x, y+ySpacing, &scrollCenterRegion,
93 quarterWidth*1/2, quarterHeight*1/2);
94 x += xSpacing;
95 draw9(canvas, x, y, NULL, quarterWidth*3/2, quarterHeight*3/2);
96 draw9(canvas, x, y+ySpacing, &scrollCenterRegion,
97 quarterWidth*3/2, quarterHeight*3/2);
98 x += xSpacing;
99 draw9(canvas, x, y, NULL, quarterWidth*5/2, quarterHeight*5/2);
100 draw9(canvas, x, y+ySpacing, &scrollCenterRegion,
101 quarterWidth*5/2, quarterHeight*5/2);
102 x += xSpacing;
103 draw9(canvas, x, y, NULL, quarterWidth*9/2, quarterHeight*9/2);
104 draw9(canvas, x, y+ySpacing, &scrollCenterRegion,
105 quarterWidth*9/2, quarterHeight*9/2);
106 }
107
108 void drawLabel(SkCanvas* canvas, const char *text, int startX, int startY,
109 int endX, int endY) {
110 SkPaint paint;
111 paint.setColor(0xFF000000);
112 SkPath path;
113 path.moveTo(SkIntToScalar(startX), SkIntToScalar(startY));
114 path.lineTo(SkIntToScalar(endX), SkIntToScalar(endY));
115 canvas->drawTextOnPath(text, strlen(text), path, NULL, paint);
116 }
117
118 /** Stamp out 9 copies of origBitmap, scrolled in each direction (and
119 * not scrolled at all).
120 */
121 void draw9(SkCanvas* canvas, int x, int y, SkIRect* subset,
122 int scrollX, int scrollY) {
123 for (int yMult=-1; yMult<=1; yMult++) {
124 for (int xMult=-1; xMult<=1; xMult++) {
125 // Figure out the (x,y) to draw this copy at
126 SkScalar bitmapX = SkIntToScalar(
127 x + quarterWidth * 5 * (xMult+1));
128 SkScalar bitmapY = SkIntToScalar(
129 y + quarterHeight * 5 * (yMult+1));
130
131 // Scroll a new copy of the bitmap, and then draw it.
132 // scrollRect() should always return true, even if it's a no-op
133 SkBitmap scrolledBitmap;
humper@google.com0e515772013-01-07 19:54:40 +0000134 SkDEBUGCODE(bool copyToReturnValue = )origBitmap.copyTo(
epoger@google.com6b66c392011-08-22 18:14:07 +0000135 &scrolledBitmap, origBitmap.config());
136 SkASSERT(copyToReturnValue);
humper@google.com0e515772013-01-07 19:54:40 +0000137 SkDEBUGCODE(bool scrollRectReturnValue = )scrolledBitmap.scrollRect(
epoger@google.com6b66c392011-08-22 18:14:07 +0000138 subset, scrollX * xMult, scrollY * yMult);
139 SkASSERT(scrollRectReturnValue);
epoger@google.comd4af56c2011-07-25 16:27:59 +0000140 canvas->drawBitmap(scrolledBitmap, bitmapX, bitmapY);
141 }
142 }
143 }
144
145private:
146 typedef GM INHERITED;
147 static const int quarterWidth = 10;
148 static const int quarterHeight = 14;
149 SkBitmap origBitmap;
150};
151
152//////////////////////////////////////////////////////////////////////////////
153
154static GM* MyFactory(void*) { return new BitmapScrollGM; }
155static GMRegistry reg(MyFactory);
156
157}