blob: 0603c3c4721d66fe82a2eff55c265654f45ea03c [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.
reed@google.comeb9a46c2014-01-25 16:46:20 +000029 bitmap->allocN32Pixels(quarterWidth*4, quarterHeight*4);
epoger@google.comd4af56c2011-07-25 16:27:59 +000030 SkCanvas canvas(*bitmap);
31
32 SkScalar w = SkIntToScalar(quarterWidth);
33 SkScalar h = SkIntToScalar(quarterHeight);
34 canvas.drawRectCoords( 0, 0, w*2, h*2, pRed);
35 canvas.drawRectCoords(w*2, 0, w*4, h*2, pGreen);
36 canvas.drawRectCoords( 0, h*2, w*2, h*4, pBlue);
37 canvas.drawRectCoords(w*2, h*2, w*4, h*4, pWhite);
epoger@google.com4067d7d2011-07-25 16:56:37 +000038 canvas.drawRectCoords(w, h, w*3, h*3, pAlphaGray);
epoger@google.comd4af56c2011-07-25 16:27:59 +000039 canvas.drawLine(w*2, 0, w*2, h*4, pLine);
40 canvas.drawLine( 0, h*2, w*4, h*2, pLine);
41 canvas.drawRectCoords(w, h, w*3, h*3, pLine);
42}
43
44class BitmapScrollGM : public GM {
reed@google.com9afc6562012-03-22 14:09:28 +000045 bool fInited;
46 void init() {
47 if (fInited) {
48 return;
49 }
50 fInited = true;
epoger@google.comd4af56c2011-07-25 16:27:59 +000051 // Create the original bitmap.
52 make_bitmap(quarterWidth, quarterHeight, &origBitmap);
reed@google.com9afc6562012-03-22 14:09:28 +000053 }
54
55public:
56 BitmapScrollGM() {
57 fInited = false;
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000058 this->setBGColor(0xFFDDDDDD);
epoger@google.comd4af56c2011-07-25 16:27:59 +000059 }
60
61protected:
mtklein36352bf2015-03-25 18:17:31 -070062 SkString onShortName() override {
epoger@google.comd4af56c2011-07-25 16:27:59 +000063 return SkString("bitmapscroll");
64 }
65
mtklein36352bf2015-03-25 18:17:31 -070066 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070067 return SkISize::Make(800, 600);
epoger@google.comd4af56c2011-07-25 16:27:59 +000068 }
69
mtklein36352bf2015-03-25 18:17:31 -070070 void onDraw(SkCanvas* canvas) override {
reed@google.com9afc6562012-03-22 14:09:28 +000071 this->init();
epoger@google.comd4af56c2011-07-25 16:27:59 +000072 SkIRect scrollCenterRegion = SkIRect::MakeXYWH(
epoger@google.com4067d7d2011-07-25 16:56:37 +000073 quarterWidth, quarterHeight, quarterWidth*2+1, quarterHeight*2+1);
epoger@google.comd4af56c2011-07-25 16:27:59 +000074 int x = quarterWidth;
75 int y = quarterHeight;
76 int xSpacing = quarterWidth * 20;
77 int ySpacing = quarterHeight * 16;
78
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000079 // Draw left-hand text labels.
epoger@google.comd4af56c2011-07-25 16:27:59 +000080 drawLabel(canvas, "scroll entire bitmap",
81 x, y, x, y + ySpacing);
82 drawLabel(canvas, "scroll part of bitmap",
83 x, y + ySpacing, x, y + ySpacing*2);
84 x += 30;
85
86 // Draw various permutations of scrolled bitmaps, scrolling a bit
87 // further each time.
88 draw9(canvas, x, y, NULL, quarterWidth*1/2, quarterHeight*1/2);
89 draw9(canvas, x, y+ySpacing, &scrollCenterRegion,
90 quarterWidth*1/2, quarterHeight*1/2);
91 x += xSpacing;
92 draw9(canvas, x, y, NULL, quarterWidth*3/2, quarterHeight*3/2);
93 draw9(canvas, x, y+ySpacing, &scrollCenterRegion,
94 quarterWidth*3/2, quarterHeight*3/2);
95 x += xSpacing;
96 draw9(canvas, x, y, NULL, quarterWidth*5/2, quarterHeight*5/2);
97 draw9(canvas, x, y+ySpacing, &scrollCenterRegion,
98 quarterWidth*5/2, quarterHeight*5/2);
99 x += xSpacing;
100 draw9(canvas, x, y, NULL, quarterWidth*9/2, quarterHeight*9/2);
101 draw9(canvas, x, y+ySpacing, &scrollCenterRegion,
102 quarterWidth*9/2, quarterHeight*9/2);
103 }
104
105 void drawLabel(SkCanvas* canvas, const char *text, int startX, int startY,
106 int endX, int endY) {
107 SkPaint paint;
caryclark5fb6bd42014-06-23 11:25:00 -0700108 sk_tool_utils::set_portable_typeface(&paint);
epoger@google.comd4af56c2011-07-25 16:27:59 +0000109 paint.setColor(0xFF000000);
110 SkPath path;
111 path.moveTo(SkIntToScalar(startX), SkIntToScalar(startY));
112 path.lineTo(SkIntToScalar(endX), SkIntToScalar(endY));
113 canvas->drawTextOnPath(text, strlen(text), path, NULL, paint);
114 }
115
116 /** Stamp out 9 copies of origBitmap, scrolled in each direction (and
117 * not scrolled at all).
118 */
119 void draw9(SkCanvas* canvas, int x, int y, SkIRect* subset,
120 int scrollX, int scrollY) {
121 for (int yMult=-1; yMult<=1; yMult++) {
122 for (int xMult=-1; xMult<=1; xMult++) {
123 // Figure out the (x,y) to draw this copy at
124 SkScalar bitmapX = SkIntToScalar(
125 x + quarterWidth * 5 * (xMult+1));
126 SkScalar bitmapY = SkIntToScalar(
127 y + quarterHeight * 5 * (yMult+1));
128
129 // Scroll a new copy of the bitmap, and then draw it.
130 // scrollRect() should always return true, even if it's a no-op
131 SkBitmap scrolledBitmap;
humper@google.com0e515772013-01-07 19:54:40 +0000132 SkDEBUGCODE(bool copyToReturnValue = )origBitmap.copyTo(
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000133 &scrolledBitmap, origBitmap.colorType());
epoger@google.com6b66c392011-08-22 18:14:07 +0000134 SkASSERT(copyToReturnValue);
humper@google.com0e515772013-01-07 19:54:40 +0000135 SkDEBUGCODE(bool scrollRectReturnValue = )scrolledBitmap.scrollRect(
epoger@google.com6b66c392011-08-22 18:14:07 +0000136 subset, scrollX * xMult, scrollY * yMult);
137 SkASSERT(scrollRectReturnValue);
epoger@google.comd4af56c2011-07-25 16:27:59 +0000138 canvas->drawBitmap(scrolledBitmap, bitmapX, bitmapY);
139 }
140 }
141 }
142
143private:
144 typedef GM INHERITED;
145 static const int quarterWidth = 10;
146 static const int quarterHeight = 14;
147 SkBitmap origBitmap;
148};
149
150//////////////////////////////////////////////////////////////////////////////
151
152static GM* MyFactory(void*) { return new BitmapScrollGM; }
153static GMRegistry reg(MyFactory);
154
155}