skia2/gm/bitmapshader.cpp
reed@google.com eb9a46cbbb add legacy/helper allocN32Pixels, and convert gm to use it
This is an intermediate api, but might help us quickly get to a point where
no one is creating bitmaps in a 2-step process (setConfig + alloc).

BUG=skia:
R=halcanary@google.com

Review URL: https://codereview.chromium.org/140593005

git-svn-id: http://skia.googlecode.com/svn/trunk@13182 2bbb7eff-a529-9590-31e7-b0007b416f81
2014-01-25 16:46:20 +00:00

102 lines
2.6 KiB
C++

/*
* Copyright 2013 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "gm.h"
#include "SkBitmap.h"
#include "SkPaint.h"
#include "SkShader.h"
namespace skiagm {
static void draw_bm(SkBitmap* bm) {
SkPaint bluePaint;
bluePaint.setColor(SK_ColorBLUE);
bm->allocN32Pixels(20, 20);
bm->eraseColor(SK_ColorRED);
SkCanvas canvas(*bm);
canvas.drawCircle(10, 10, 5, bluePaint);
}
static void draw_mask(SkBitmap* bm) {
SkPaint circlePaint;
circlePaint.setColor(SK_ColorBLACK);
bm->allocConfigPixels(SkBitmap::kA8_Config, 20, 20);
bm->eraseColor(SK_ColorTRANSPARENT);
SkCanvas canvas(*bm);
canvas.drawCircle(10, 10, 10, circlePaint);
}
class BitmapShaderGM : public GM {
public:
BitmapShaderGM() {
this->setBGColor(SK_ColorGRAY);
draw_bm(&fBitmap);
draw_mask(&fMask);
}
protected:
virtual SkString onShortName() {
return SkString("bitmapshaders");
}
virtual SkISize onISize() {
return make_isize(75, 100);
}
virtual void onDraw(SkCanvas* canvas) {
SkShader* shader = SkShader::CreateBitmapShader(fBitmap,
SkShader::kClamp_TileMode,
SkShader::kClamp_TileMode);
SkPaint paint;
paint.setShader(shader);
// release the shader ref as the paint now holds a reference
shader->unref();
// draw the shader with a bitmap mask
canvas->drawBitmap(fMask, 0, 0, &paint);
canvas->drawBitmap(fMask, 30, 0, &paint);
canvas->translate(0, 25);
// draw the shader with standard geometry
canvas->drawCircle(10, 10, 10, paint);
canvas->drawCircle(40, 10, 10, paint); // no blue circle expected
canvas->translate(0, 25);
shader = SkShader::CreateBitmapShader(fMask,
SkShader::kRepeat_TileMode,
SkShader::kRepeat_TileMode);
paint.setShader(shader);
paint.setColor(SK_ColorRED);
shader->unref();
// draw the mask using the shader and a color
canvas->drawRect(SkRect::MakeXYWH(0, 0, 20, 20), paint);
canvas->drawRect(SkRect::MakeXYWH(30, 0, 20, 20), paint);
}
private:
SkBitmap fBitmap;
SkBitmap fMask;
typedef GM INHERITED;
};
//////////////////////////////////////////////////////////////////////////////
static GM* MyFactory(void*) { return new BitmapShaderGM; }
static GMRegistry reg(MyFactory);
}