add onOnceBeforeDraw() virtual, so gm's can easily perform lazy initialization

git-svn-id: http://skia.googlecode.com/svn/trunk@6559 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
reed@google.com 2012-11-27 15:15:58 +00:00
parent 631940c8c4
commit 7775d66aa0
5 changed files with 26 additions and 13 deletions

View File

@ -13,6 +13,7 @@ SkString GM::gResourcePath;
GM::GM() {
fBGColor = SK_ColorWHITE;
fCanvasIsDeferred = false;
fHaveCalledOnceBeforeDraw = false;
}
GM::~GM() {}
@ -22,10 +23,18 @@ void GM::draw(SkCanvas* canvas) {
}
void GM::drawContent(SkCanvas* canvas) {
if (!fHaveCalledOnceBeforeDraw) {
fHaveCalledOnceBeforeDraw = true;
this->onOnceBeforeDraw();
}
this->onDraw(canvas);
}
void GM::drawBackground(SkCanvas* canvas) {
if (!fHaveCalledOnceBeforeDraw) {
fHaveCalledOnceBeforeDraw = true;
this->onOnceBeforeDraw();
}
this->onDrawBackground(canvas);
}

View File

@ -79,6 +79,7 @@ namespace skiagm {
protected:
static SkString gResourcePath;
virtual void onOnceBeforeDraw() {}
virtual void onDraw(SkCanvas*) = 0;
virtual void onDrawBackground(SkCanvas*);
virtual SkISize onISize() = 0;
@ -90,6 +91,7 @@ namespace skiagm {
SkString fShortName;
SkColor fBGColor;
bool fCanvasIsDeferred; // work-around problem in srcmode.cpp
bool fHaveCalledOnceBeforeDraw;
};
typedef SkTRegistry<GM*, void*> GMRegistry;

View File

@ -79,20 +79,20 @@ namespace skiagm {
class HairModesGM : public GM {
SkPaint fBGPaint;
public:
HairModesGM() {
fBGPaint.setShader(make_bg_shader())->unref();
}
protected:
virtual SkString onShortName() {
virtual SkString onShortName() SK_OVERRIDE {
return SkString("hairmodes");
}
virtual SkISize onISize() { return make_isize(640, 480); }
virtual void onDraw(SkCanvas* canvas) {
virtual void onOnceBeforeDraw() SK_OVERRIDE {
fBGPaint.setShader(make_bg_shader())->unref();
}
virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
const SkRect bounds = SkRect::MakeWH(W, H);
static const SkAlpha gAlphaValue[] = { 0xFF, 0x88, 0x88 };

View File

@ -64,9 +64,6 @@ public:
TilingGM()
: fLooper(SkIntToScalar(1), SkIntToScalar(2), SkIntToScalar(2),
0x88000000) {
for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) {
makebm(&fTexture[i], gConfigs[i], gWidth, gHeight);
}
}
SkBitmap fTexture[SK_ARRAY_COUNT(gConfigs)];
@ -78,7 +75,13 @@ protected:
SkISize onISize() { return SkISize::Make(880, 560); }
virtual void onDraw(SkCanvas* canvas) {
virtual void onOnceBeforeDraw() SK_OVERRIDE {
for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) {
makebm(&fTexture[i], gConfigs[i], gWidth, gHeight);
}
}
virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
SkRect r = { 0, 0, SkIntToScalar(gWidth*2), SkIntToScalar(gHeight*2) };

View File

@ -32,11 +32,9 @@ static SkBitmap make_bitmap() {
}
class TinyBitmapGM : public GM {
SkBitmap fBM;
public:
TinyBitmapGM() {
this->setBGColor(0xFFDDDDDD);
fBM = make_bitmap();
}
protected:
@ -47,8 +45,9 @@ protected:
virtual SkISize onISize() { return make_isize(100, 100); }
virtual void onDraw(SkCanvas* canvas) {
SkBitmap bm = make_bitmap();
SkShader* s =
SkShader::CreateBitmapShader(fBM, SkShader::kRepeat_TileMode,
SkShader::CreateBitmapShader(bm, SkShader::kRepeat_TileMode,
SkShader::kMirror_TileMode);
SkPaint paint;
paint.setAlpha(0x80);