epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | */ |
epoger@google.com | d4af56c | 2011-07-25 16:27:59 +0000 | [diff] [blame] | 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 | */ |
| 18 | static void make_bitmap(int quarterWidth, int quarterHeight, SkBitmap *bitmap) { |
epoger@google.com | 4067d7d | 2011-07-25 16:56:37 +0000 | [diff] [blame] | 19 | SkPaint pRed, pWhite, pGreen, pBlue, pLine, pAlphaGray; |
epoger@google.com | d4af56c | 2011-07-25 16:27:59 +0000 | [diff] [blame] | 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); |
epoger@google.com | 4067d7d | 2011-07-25 16:56:37 +0000 | [diff] [blame] | 26 | pAlphaGray.setColor(0x66888888); |
epoger@google.com | d4af56c | 2011-07-25 16:27:59 +0000 | [diff] [blame] | 27 | |
| 28 | // Prepare bitmap, and a canvas that draws into it. |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 29 | bitmap->allocN32Pixels(quarterWidth*4, quarterHeight*4); |
epoger@google.com | d4af56c | 2011-07-25 16:27:59 +0000 | [diff] [blame] | 30 | 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.com | 4067d7d | 2011-07-25 16:56:37 +0000 | [diff] [blame] | 38 | canvas.drawRectCoords(w, h, w*3, h*3, pAlphaGray); |
epoger@google.com | d4af56c | 2011-07-25 16:27:59 +0000 | [diff] [blame] | 39 | 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 | |
| 44 | class BitmapScrollGM : public GM { |
reed@google.com | 9afc656 | 2012-03-22 14:09:28 +0000 | [diff] [blame] | 45 | bool fInited; |
| 46 | void init() { |
| 47 | if (fInited) { |
| 48 | return; |
| 49 | } |
| 50 | fInited = true; |
epoger@google.com | d4af56c | 2011-07-25 16:27:59 +0000 | [diff] [blame] | 51 | // Create the original bitmap. |
| 52 | make_bitmap(quarterWidth, quarterHeight, &origBitmap); |
reed@google.com | 9afc656 | 2012-03-22 14:09:28 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | public: |
| 56 | BitmapScrollGM() { |
| 57 | fInited = false; |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 58 | this->setBGColor(0xFFDDDDDD); |
epoger@google.com | d4af56c | 2011-07-25 16:27:59 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | protected: |
| 62 | virtual SkString onShortName() { |
| 63 | return SkString("bitmapscroll"); |
| 64 | } |
| 65 | |
Mike Klein | 9d7321c | 2014-07-16 14:19:54 -0400 | [diff] [blame] | 66 | virtual uint32_t onGetFlags() const SK_OVERRIDE { |
| 67 | return kSkipTiled_Flag; |
| 68 | } |
| 69 | |
epoger@google.com | d4af56c | 2011-07-25 16:27:59 +0000 | [diff] [blame] | 70 | virtual SkISize onISize() { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 71 | return SkISize::Make(800, 600); |
epoger@google.com | d4af56c | 2011-07-25 16:27:59 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | virtual void onDraw(SkCanvas* canvas) { |
reed@google.com | 9afc656 | 2012-03-22 14:09:28 +0000 | [diff] [blame] | 75 | this->init(); |
epoger@google.com | d4af56c | 2011-07-25 16:27:59 +0000 | [diff] [blame] | 76 | SkIRect scrollCenterRegion = SkIRect::MakeXYWH( |
epoger@google.com | 4067d7d | 2011-07-25 16:56:37 +0000 | [diff] [blame] | 77 | quarterWidth, quarterHeight, quarterWidth*2+1, quarterHeight*2+1); |
epoger@google.com | d4af56c | 2011-07-25 16:27:59 +0000 | [diff] [blame] | 78 | int x = quarterWidth; |
| 79 | int y = quarterHeight; |
| 80 | int xSpacing = quarterWidth * 20; |
| 81 | int ySpacing = quarterHeight * 16; |
| 82 | |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 83 | // Draw left-hand text labels. |
epoger@google.com | d4af56c | 2011-07-25 16:27:59 +0000 | [diff] [blame] | 84 | drawLabel(canvas, "scroll entire bitmap", |
| 85 | x, y, x, y + ySpacing); |
| 86 | drawLabel(canvas, "scroll part of bitmap", |
| 87 | x, y + ySpacing, x, y + ySpacing*2); |
| 88 | x += 30; |
| 89 | |
| 90 | // Draw various permutations of scrolled bitmaps, scrolling a bit |
| 91 | // further each time. |
| 92 | draw9(canvas, x, y, NULL, quarterWidth*1/2, quarterHeight*1/2); |
| 93 | draw9(canvas, x, y+ySpacing, &scrollCenterRegion, |
| 94 | quarterWidth*1/2, quarterHeight*1/2); |
| 95 | x += xSpacing; |
| 96 | draw9(canvas, x, y, NULL, quarterWidth*3/2, quarterHeight*3/2); |
| 97 | draw9(canvas, x, y+ySpacing, &scrollCenterRegion, |
| 98 | quarterWidth*3/2, quarterHeight*3/2); |
| 99 | x += xSpacing; |
| 100 | draw9(canvas, x, y, NULL, quarterWidth*5/2, quarterHeight*5/2); |
| 101 | draw9(canvas, x, y+ySpacing, &scrollCenterRegion, |
| 102 | quarterWidth*5/2, quarterHeight*5/2); |
| 103 | x += xSpacing; |
| 104 | draw9(canvas, x, y, NULL, quarterWidth*9/2, quarterHeight*9/2); |
| 105 | draw9(canvas, x, y+ySpacing, &scrollCenterRegion, |
| 106 | quarterWidth*9/2, quarterHeight*9/2); |
| 107 | } |
| 108 | |
| 109 | void drawLabel(SkCanvas* canvas, const char *text, int startX, int startY, |
| 110 | int endX, int endY) { |
| 111 | SkPaint paint; |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 112 | sk_tool_utils::set_portable_typeface(&paint); |
epoger@google.com | d4af56c | 2011-07-25 16:27:59 +0000 | [diff] [blame] | 113 | paint.setColor(0xFF000000); |
| 114 | SkPath path; |
| 115 | path.moveTo(SkIntToScalar(startX), SkIntToScalar(startY)); |
| 116 | path.lineTo(SkIntToScalar(endX), SkIntToScalar(endY)); |
| 117 | canvas->drawTextOnPath(text, strlen(text), path, NULL, paint); |
| 118 | } |
| 119 | |
| 120 | /** Stamp out 9 copies of origBitmap, scrolled in each direction (and |
| 121 | * not scrolled at all). |
| 122 | */ |
| 123 | void draw9(SkCanvas* canvas, int x, int y, SkIRect* subset, |
| 124 | int scrollX, int scrollY) { |
| 125 | for (int yMult=-1; yMult<=1; yMult++) { |
| 126 | for (int xMult=-1; xMult<=1; xMult++) { |
| 127 | // Figure out the (x,y) to draw this copy at |
| 128 | SkScalar bitmapX = SkIntToScalar( |
| 129 | x + quarterWidth * 5 * (xMult+1)); |
| 130 | SkScalar bitmapY = SkIntToScalar( |
| 131 | y + quarterHeight * 5 * (yMult+1)); |
| 132 | |
| 133 | // Scroll a new copy of the bitmap, and then draw it. |
| 134 | // scrollRect() should always return true, even if it's a no-op |
| 135 | SkBitmap scrolledBitmap; |
humper@google.com | 0e51577 | 2013-01-07 19:54:40 +0000 | [diff] [blame] | 136 | SkDEBUGCODE(bool copyToReturnValue = )origBitmap.copyTo( |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 137 | &scrolledBitmap, origBitmap.colorType()); |
epoger@google.com | 6b66c39 | 2011-08-22 18:14:07 +0000 | [diff] [blame] | 138 | SkASSERT(copyToReturnValue); |
humper@google.com | 0e51577 | 2013-01-07 19:54:40 +0000 | [diff] [blame] | 139 | SkDEBUGCODE(bool scrollRectReturnValue = )scrolledBitmap.scrollRect( |
epoger@google.com | 6b66c39 | 2011-08-22 18:14:07 +0000 | [diff] [blame] | 140 | subset, scrollX * xMult, scrollY * yMult); |
| 141 | SkASSERT(scrollRectReturnValue); |
epoger@google.com | d4af56c | 2011-07-25 16:27:59 +0000 | [diff] [blame] | 142 | canvas->drawBitmap(scrolledBitmap, bitmapX, bitmapY); |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | private: |
| 148 | typedef GM INHERITED; |
| 149 | static const int quarterWidth = 10; |
| 150 | static const int quarterHeight = 14; |
| 151 | SkBitmap origBitmap; |
| 152 | }; |
| 153 | |
| 154 | ////////////////////////////////////////////////////////////////////////////// |
| 155 | |
| 156 | static GM* MyFactory(void*) { return new BitmapScrollGM; } |
| 157 | static GMRegistry reg(MyFactory); |
| 158 | |
| 159 | } |