2011-07-28 14:26:00 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2011-07-25 16:27:59 +00:00
|
|
|
#include "gm.h"
|
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
/** Create a bitmap image suitable for testing SkBitmap::scrollRect().
|
|
|
|
*
|
|
|
|
* @param quarterWidth bitmap will be 4x this many pixels wide
|
|
|
|
* @param quarterHeight bitmap will be 4x this many pixels tall
|
|
|
|
* @param bitmap the bitmap data is written into this object
|
|
|
|
*/
|
|
|
|
static void make_bitmap(int quarterWidth, int quarterHeight, SkBitmap *bitmap) {
|
2011-07-25 16:56:37 +00:00
|
|
|
SkPaint pRed, pWhite, pGreen, pBlue, pLine, pAlphaGray;
|
2011-07-25 16:27:59 +00:00
|
|
|
pRed.setColor(0xFFFF9999);
|
|
|
|
pWhite.setColor(0xFFFFFFFF);
|
|
|
|
pGreen.setColor(0xFF99FF99);
|
|
|
|
pBlue.setColor(0xFF9999FF);
|
|
|
|
pLine.setColor(0xFF000000);
|
|
|
|
pLine.setStyle(SkPaint::kStroke_Style);
|
2011-07-25 16:56:37 +00:00
|
|
|
pAlphaGray.setColor(0x66888888);
|
2011-07-25 16:27:59 +00:00
|
|
|
|
|
|
|
// Prepare bitmap, and a canvas that draws into it.
|
2014-01-25 16:46:20 +00:00
|
|
|
bitmap->allocN32Pixels(quarterWidth*4, quarterHeight*4);
|
2011-07-25 16:27:59 +00:00
|
|
|
SkCanvas canvas(*bitmap);
|
|
|
|
|
|
|
|
SkScalar w = SkIntToScalar(quarterWidth);
|
|
|
|
SkScalar h = SkIntToScalar(quarterHeight);
|
|
|
|
canvas.drawRectCoords( 0, 0, w*2, h*2, pRed);
|
|
|
|
canvas.drawRectCoords(w*2, 0, w*4, h*2, pGreen);
|
|
|
|
canvas.drawRectCoords( 0, h*2, w*2, h*4, pBlue);
|
|
|
|
canvas.drawRectCoords(w*2, h*2, w*4, h*4, pWhite);
|
2011-07-25 16:56:37 +00:00
|
|
|
canvas.drawRectCoords(w, h, w*3, h*3, pAlphaGray);
|
2011-07-25 16:27:59 +00:00
|
|
|
canvas.drawLine(w*2, 0, w*2, h*4, pLine);
|
|
|
|
canvas.drawLine( 0, h*2, w*4, h*2, pLine);
|
|
|
|
canvas.drawRectCoords(w, h, w*3, h*3, pLine);
|
|
|
|
}
|
|
|
|
|
|
|
|
class BitmapScrollGM : public GM {
|
2012-03-22 14:09:28 +00:00
|
|
|
bool fInited;
|
|
|
|
void init() {
|
|
|
|
if (fInited) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fInited = true;
|
2011-07-25 16:27:59 +00:00
|
|
|
// Create the original bitmap.
|
|
|
|
make_bitmap(quarterWidth, quarterHeight, &origBitmap);
|
2012-03-22 14:09:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
BitmapScrollGM() {
|
|
|
|
fInited = false;
|
2011-10-31 14:18:20 +00:00
|
|
|
this->setBGColor(0xFFDDDDDD);
|
2011-07-25 16:27:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2011-07-25 16:27:59 +00:00
|
|
|
return SkString("bitmapscroll");
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2014-06-10 06:59:03 +00:00
|
|
|
return SkISize::Make(800, 600);
|
2011-07-25 16:27:59 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2012-03-22 14:09:28 +00:00
|
|
|
this->init();
|
2011-07-25 16:27:59 +00:00
|
|
|
SkIRect scrollCenterRegion = SkIRect::MakeXYWH(
|
2011-07-25 16:56:37 +00:00
|
|
|
quarterWidth, quarterHeight, quarterWidth*2+1, quarterHeight*2+1);
|
2011-07-25 16:27:59 +00:00
|
|
|
int x = quarterWidth;
|
|
|
|
int y = quarterHeight;
|
|
|
|
int xSpacing = quarterWidth * 20;
|
|
|
|
int ySpacing = quarterHeight * 16;
|
|
|
|
|
2011-10-31 14:18:20 +00:00
|
|
|
// Draw left-hand text labels.
|
2011-07-25 16:27:59 +00:00
|
|
|
drawLabel(canvas, "scroll entire bitmap",
|
|
|
|
x, y, x, y + ySpacing);
|
|
|
|
drawLabel(canvas, "scroll part of bitmap",
|
|
|
|
x, y + ySpacing, x, y + ySpacing*2);
|
|
|
|
x += 30;
|
|
|
|
|
|
|
|
// Draw various permutations of scrolled bitmaps, scrolling a bit
|
|
|
|
// further each time.
|
|
|
|
draw9(canvas, x, y, NULL, quarterWidth*1/2, quarterHeight*1/2);
|
|
|
|
draw9(canvas, x, y+ySpacing, &scrollCenterRegion,
|
|
|
|
quarterWidth*1/2, quarterHeight*1/2);
|
|
|
|
x += xSpacing;
|
|
|
|
draw9(canvas, x, y, NULL, quarterWidth*3/2, quarterHeight*3/2);
|
|
|
|
draw9(canvas, x, y+ySpacing, &scrollCenterRegion,
|
|
|
|
quarterWidth*3/2, quarterHeight*3/2);
|
|
|
|
x += xSpacing;
|
|
|
|
draw9(canvas, x, y, NULL, quarterWidth*5/2, quarterHeight*5/2);
|
|
|
|
draw9(canvas, x, y+ySpacing, &scrollCenterRegion,
|
|
|
|
quarterWidth*5/2, quarterHeight*5/2);
|
|
|
|
x += xSpacing;
|
|
|
|
draw9(canvas, x, y, NULL, quarterWidth*9/2, quarterHeight*9/2);
|
|
|
|
draw9(canvas, x, y+ySpacing, &scrollCenterRegion,
|
|
|
|
quarterWidth*9/2, quarterHeight*9/2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void drawLabel(SkCanvas* canvas, const char *text, int startX, int startY,
|
|
|
|
int endX, int endY) {
|
|
|
|
SkPaint paint;
|
2014-06-23 18:25:00 +00:00
|
|
|
sk_tool_utils::set_portable_typeface(&paint);
|
2011-07-25 16:27:59 +00:00
|
|
|
paint.setColor(0xFF000000);
|
|
|
|
SkPath path;
|
|
|
|
path.moveTo(SkIntToScalar(startX), SkIntToScalar(startY));
|
|
|
|
path.lineTo(SkIntToScalar(endX), SkIntToScalar(endY));
|
|
|
|
canvas->drawTextOnPath(text, strlen(text), path, NULL, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Stamp out 9 copies of origBitmap, scrolled in each direction (and
|
|
|
|
* not scrolled at all).
|
|
|
|
*/
|
|
|
|
void draw9(SkCanvas* canvas, int x, int y, SkIRect* subset,
|
|
|
|
int scrollX, int scrollY) {
|
|
|
|
for (int yMult=-1; yMult<=1; yMult++) {
|
|
|
|
for (int xMult=-1; xMult<=1; xMult++) {
|
|
|
|
// Figure out the (x,y) to draw this copy at
|
|
|
|
SkScalar bitmapX = SkIntToScalar(
|
|
|
|
x + quarterWidth * 5 * (xMult+1));
|
|
|
|
SkScalar bitmapY = SkIntToScalar(
|
|
|
|
y + quarterHeight * 5 * (yMult+1));
|
|
|
|
|
|
|
|
// Scroll a new copy of the bitmap, and then draw it.
|
|
|
|
// scrollRect() should always return true, even if it's a no-op
|
|
|
|
SkBitmap scrolledBitmap;
|
2013-01-07 19:54:40 +00:00
|
|
|
SkDEBUGCODE(bool copyToReturnValue = )origBitmap.copyTo(
|
2014-02-23 03:59:35 +00:00
|
|
|
&scrolledBitmap, origBitmap.colorType());
|
2011-08-22 18:14:07 +00:00
|
|
|
SkASSERT(copyToReturnValue);
|
2013-01-07 19:54:40 +00:00
|
|
|
SkDEBUGCODE(bool scrollRectReturnValue = )scrolledBitmap.scrollRect(
|
2011-08-22 18:14:07 +00:00
|
|
|
subset, scrollX * xMult, scrollY * yMult);
|
|
|
|
SkASSERT(scrollRectReturnValue);
|
2011-07-25 16:27:59 +00:00
|
|
|
canvas->drawBitmap(scrolledBitmap, bitmapX, bitmapY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef GM INHERITED;
|
|
|
|
static const int quarterWidth = 10;
|
|
|
|
static const int quarterHeight = 14;
|
|
|
|
SkBitmap origBitmap;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static GM* MyFactory(void*) { return new BitmapScrollGM; }
|
|
|
|
static GMRegistry reg(MyFactory);
|
|
|
|
|
|
|
|
}
|