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.
|
|
|
|
*/
|
|
|
|
|
2019-05-01 14:59:30 +00:00
|
|
|
#include "gm/gm.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
2019-05-01 14:59:30 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkImageInfo.h"
|
|
|
|
#include "include/core/SkMatrix.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkPaint.h"
|
2019-05-01 14:59:30 +00:00
|
|
|
#include "include/core/SkRect.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkShader.h"
|
2019-05-01 14:59:30 +00:00
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/core/SkTileMode.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2020-07-01 16:55:01 +00:00
|
|
|
#include "include/gpu/GrRecordingContext.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/gpu/GrCaps.h"
|
2020-06-29 19:36:12 +00:00
|
|
|
#include "src/gpu/GrRecordingContextPriv.h"
|
2013-12-06 20:14:39 +00:00
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
2021-01-25 00:49:21 +00:00
|
|
|
static sk_sp<SkImage> draw_bm() {
|
2013-12-07 07:02:21 +00:00
|
|
|
SkPaint bluePaint;
|
|
|
|
bluePaint.setColor(SK_ColorBLUE);
|
2013-12-06 20:14:39 +00:00
|
|
|
|
2021-01-25 00:49:21 +00:00
|
|
|
SkBitmap bm;
|
|
|
|
bm.allocN32Pixels(20, 20);
|
|
|
|
bm.eraseColor(SK_ColorRED);
|
|
|
|
SkCanvas(bm).drawCircle(10, 10, 5, bluePaint);
|
|
|
|
return bm.asImage();
|
2013-12-06 20:14:39 +00:00
|
|
|
}
|
|
|
|
|
2021-01-25 00:49:21 +00:00
|
|
|
static sk_sp<SkImage> draw_mask() {
|
2013-12-07 07:02:21 +00:00
|
|
|
SkPaint circlePaint;
|
|
|
|
circlePaint.setColor(SK_ColorBLACK);
|
2013-12-06 20:14:39 +00:00
|
|
|
|
2021-01-25 00:49:21 +00:00
|
|
|
SkBitmap bm;
|
|
|
|
bm.allocPixels(SkImageInfo::MakeA8(20, 20));
|
|
|
|
bm.eraseColor(SK_ColorTRANSPARENT);
|
|
|
|
SkCanvas(bm).drawCircle(10, 10, 10, circlePaint);
|
|
|
|
return bm.asImage();
|
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);
|
2021-01-25 00:49:21 +00:00
|
|
|
fImage = draw_bm();
|
|
|
|
fMask = draw_mask();
|
2013-12-06 20:14:39 +00:00
|
|
|
}
|
|
|
|
|
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();
|
2021-01-25 00:49:21 +00:00
|
|
|
paint.setShader(fImage->makeShader(SkSamplingOptions(), s));
|
2013-12-07 07:02:21 +00:00
|
|
|
|
2014-03-24 02:49:48 +00:00
|
|
|
// draw the shader with a bitmap mask
|
2021-01-25 00:49:21 +00:00
|
|
|
canvas->drawImage(fMask, 0, 0, SkSamplingOptions(), &paint);
|
2019-02-11 16:09:15 +00:00
|
|
|
// no blue circle expected (the bitmap shader's coordinates are aligned to CTM still)
|
2021-01-25 00:49:21 +00:00
|
|
|
canvas->drawImage(fMask, 30, 0, SkSamplingOptions(), &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);
|
2021-01-25 00:49:21 +00:00
|
|
|
canvas->drawImage(fMask, 0, 0, SkSamplingOptions(), &paint);
|
|
|
|
canvas->drawImage(fMask, 30, 0, SkSamplingOptions(), &paint);
|
2014-03-24 02:49:48 +00:00
|
|
|
|
|
|
|
canvas->translate(0, 25);
|
|
|
|
|
2021-01-25 00:49:21 +00:00
|
|
|
paint.setShader(fMask->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
|
|
|
|
SkSamplingOptions(), 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:
|
2021-01-25 00:49:21 +00:00
|
|
|
sk_sp<SkImage> fImage, fMask;
|
2013-12-06 20:14:39 +00:00
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2013-12-06 20:14:39 +00:00
|
|
|
};
|
|
|
|
|
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;
|
2020-06-29 19:36:12 +00:00
|
|
|
if (auto ctx = canvas->recordingContext()) {
|
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);
|
|
|
|
|
2020-12-12 16:18:31 +00:00
|
|
|
paint.setShader(bitmap.makeShader(SkTileMode::kMirror, SkTileMode::kMirror,
|
|
|
|
SkSamplingOptions()));
|
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
|
|
|
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace skiagm
|