2013-12-06 20:14:39 +00:00
|
|
|
/*
|
|
|
|
* 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"
|
2018-05-11 14:14:21 +00:00
|
|
|
|
|
|
|
#include "GrCaps.h"
|
2017-11-29 21:07:22 +00:00
|
|
|
#include "GrContext.h"
|
2018-05-11 14:14:21 +00:00
|
|
|
#include "GrContextPriv.h"
|
2013-12-06 20:14:39 +00:00
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
static void draw_bm(SkBitmap* bm) {
|
2013-12-07 07:02:21 +00:00
|
|
|
SkPaint bluePaint;
|
|
|
|
bluePaint.setColor(SK_ColorBLUE);
|
2013-12-06 20:14:39 +00:00
|
|
|
|
2014-01-25 16:46:20 +00:00
|
|
|
bm->allocN32Pixels(20, 20);
|
2013-12-07 07:02:21 +00:00
|
|
|
bm->eraseColor(SK_ColorRED);
|
2013-12-06 20:14:39 +00:00
|
|
|
|
2013-12-07 07:02:21 +00:00
|
|
|
SkCanvas canvas(*bm);
|
|
|
|
canvas.drawCircle(10, 10, 5, bluePaint);
|
2013-12-06 20:14:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_mask(SkBitmap* bm) {
|
2013-12-07 07:02:21 +00:00
|
|
|
SkPaint circlePaint;
|
|
|
|
circlePaint.setColor(SK_ColorBLACK);
|
2013-12-06 20:14:39 +00:00
|
|
|
|
2014-02-17 21:21:46 +00:00
|
|
|
bm->allocPixels(SkImageInfo::MakeA8(20, 20));
|
2013-12-07 07:02:21 +00:00
|
|
|
bm->eraseColor(SK_ColorTRANSPARENT);
|
2013-12-06 20:14:39 +00:00
|
|
|
|
2013-12-07 07:02:21 +00:00
|
|
|
SkCanvas canvas(*bm);
|
|
|
|
canvas.drawCircle(10, 10, 10, circlePaint);
|
2013-12-06 20:14:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class BitmapShaderGM : public GM {
|
|
|
|
|
2015-02-25 17:04:04 +00:00
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2018-08-16 14:17:03 +00:00
|
|
|
this->setBGColor(SK_ColorGRAY);
|
2013-12-06 20:14:39 +00:00
|
|
|
draw_bm(&fBitmap);
|
|
|
|
draw_mask(&fMask);
|
|
|
|
}
|
|
|
|
|
2015-07-13 13:18:39 +00:00
|
|
|
SkString onShortName() override {
|
2013-12-06 20:14:39 +00:00
|
|
|
return SkString("bitmapshaders");
|
|
|
|
}
|
|
|
|
|
2015-07-13 13:18:39 +00:00
|
|
|
SkISize onISize() override {
|
2014-03-24 02:49:48 +00:00
|
|
|
return SkISize::Make(150, 100);
|
2013-12-06 20:14:39 +00:00
|
|
|
}
|
|
|
|
|
2015-07-13 13:18:39 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2013-12-07 07:02:21 +00:00
|
|
|
SkPaint paint;
|
2014-03-18 16:28:12 +00:00
|
|
|
|
2014-03-24 02:49:48 +00:00
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
SkMatrix s;
|
|
|
|
s.reset();
|
|
|
|
if (1 == i) {
|
2014-03-24 14:54:04 +00:00
|
|
|
s.setScale(1.5f, 1.5f);
|
2014-03-24 02:49:48 +00:00
|
|
|
s.postTranslate(2, 2);
|
|
|
|
}
|
2013-12-07 07:02:21 +00:00
|
|
|
|
2014-03-24 02:49:48 +00:00
|
|
|
canvas->save();
|
2019-04-08 17:20:23 +00:00
|
|
|
paint.setShader(fBitmap.makeShader(&s));
|
2013-12-07 07:02:21 +00:00
|
|
|
|
2014-03-24 02:49:48 +00:00
|
|
|
// draw the shader with a bitmap mask
|
|
|
|
canvas->drawBitmap(fMask, 0, 0, &paint);
|
2019-02-11 16:09:15 +00:00
|
|
|
// no blue circle expected (the bitmap shader's coordinates are aligned to CTM still)
|
2014-03-24 02:49:48 +00:00
|
|
|
canvas->drawBitmap(fMask, 30, 0, &paint);
|
2013-12-07 07:02:21 +00:00
|
|
|
|
2014-03-24 02:49:48 +00:00
|
|
|
canvas->translate(0, 25);
|
2013-12-07 07:02:21 +00:00
|
|
|
|
2014-03-24 02:49:48 +00:00
|
|
|
canvas->drawCircle(10, 10, 10, paint);
|
|
|
|
canvas->drawCircle(40, 10, 10, paint); // no blue circle expected
|
2013-12-07 07:02:21 +00:00
|
|
|
|
2014-03-24 02:49:48 +00:00
|
|
|
canvas->translate(0, 25);
|
2013-12-07 07:02:21 +00:00
|
|
|
|
2014-03-24 02:49:48 +00:00
|
|
|
// clear the shader, colorized by a solid color with a bitmap mask
|
2015-08-27 14:41:13 +00:00
|
|
|
paint.setShader(nullptr);
|
2014-03-24 02:49:48 +00:00
|
|
|
paint.setColor(SK_ColorGREEN);
|
|
|
|
canvas->drawBitmap(fMask, 0, 0, &paint);
|
|
|
|
canvas->drawBitmap(fMask, 30, 0, &paint);
|
|
|
|
|
|
|
|
canvas->translate(0, 25);
|
|
|
|
|
2019-04-08 17:20:23 +00:00
|
|
|
paint.setShader(fMask.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, &s));
|
2014-03-24 02:49:48 +00:00
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(60, 0);
|
|
|
|
}
|
2013-12-06 20:14:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SkBitmap fBitmap;
|
|
|
|
SkBitmap fMask;
|
|
|
|
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
2017-11-29 21:07:22 +00:00
|
|
|
DEF_SIMPLE_GM(hugebitmapshader, canvas, 100, 100) {
|
|
|
|
SkPaint paint;
|
|
|
|
SkBitmap bitmap;
|
|
|
|
|
|
|
|
// The huge height will exceed GL_MAX_TEXTURE_SIZE. We test that the GL backend will at least
|
|
|
|
// draw something with a default paint instead of drawing nothing.
|
|
|
|
//
|
|
|
|
// (See https://skia-review.googlesource.com/c/skia/+/73200)
|
|
|
|
int bitmapW = 1;
|
|
|
|
int bitmapH = 60000;
|
|
|
|
if (auto* ctx = canvas->getGrContext()) {
|
2019-02-04 18:26:26 +00:00
|
|
|
bitmapH = ctx->priv().caps()->maxTextureSize() + 1;
|
2017-11-29 21:07:22 +00:00
|
|
|
}
|
|
|
|
bitmap.setInfo(SkImageInfo::MakeA8(bitmapW, bitmapH), bitmapW);
|
|
|
|
uint8_t* pixels = new uint8_t[bitmapH];
|
|
|
|
for(int i = 0; i < bitmapH; ++i) {
|
|
|
|
pixels[i] = i & 0xff;
|
|
|
|
}
|
|
|
|
bitmap.setPixels(pixels);
|
|
|
|
|
2019-04-08 17:20:23 +00:00
|
|
|
paint.setShader(bitmap.makeShader(SkTileMode::kMirror, SkTileMode::kMirror));
|
2017-11-29 21:07:22 +00:00
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
canvas->drawCircle(50, 50, 50, paint);
|
|
|
|
delete [] pixels;
|
|
|
|
}
|
|
|
|
|
2013-12-06 20:14:39 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2019-01-23 15:22:01 +00:00
|
|
|
DEF_GM( return new BitmapShaderGM; )
|
2013-12-06 20:14:39 +00:00
|
|
|
|
|
|
|
}
|