commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | #include "gm.h" |
| 8 | |
| 9 | #include "SkBitmap.h" |
| 10 | #include "SkPaint.h" |
| 11 | #include "SkShader.h" |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 12 | |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 13 | #include "GrCaps.h" |
Yuqian Li | f3c1f09 | 2017-11-29 16:07:22 -0500 | [diff] [blame] | 14 | #include "GrContext.h" |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 15 | #include "GrContextPriv.h" |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 16 | |
| 17 | namespace skiagm { |
| 18 | |
| 19 | static void draw_bm(SkBitmap* bm) { |
skia.committer@gmail.com | 56710d1 | 2013-12-07 07:02:21 +0000 | [diff] [blame] | 20 | SkPaint bluePaint; |
| 21 | bluePaint.setColor(SK_ColorBLUE); |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 22 | |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 23 | bm->allocN32Pixels(20, 20); |
skia.committer@gmail.com | 56710d1 | 2013-12-07 07:02:21 +0000 | [diff] [blame] | 24 | bm->eraseColor(SK_ColorRED); |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 25 | |
skia.committer@gmail.com | 56710d1 | 2013-12-07 07:02:21 +0000 | [diff] [blame] | 26 | SkCanvas canvas(*bm); |
| 27 | canvas.drawCircle(10, 10, 5, bluePaint); |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | static void draw_mask(SkBitmap* bm) { |
skia.committer@gmail.com | 56710d1 | 2013-12-07 07:02:21 +0000 | [diff] [blame] | 31 | SkPaint circlePaint; |
| 32 | circlePaint.setColor(SK_ColorBLACK); |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 33 | |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 34 | bm->allocPixels(SkImageInfo::MakeA8(20, 20)); |
skia.committer@gmail.com | 56710d1 | 2013-12-07 07:02:21 +0000 | [diff] [blame] | 35 | bm->eraseColor(SK_ColorTRANSPARENT); |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 36 | |
skia.committer@gmail.com | 56710d1 | 2013-12-07 07:02:21 +0000 | [diff] [blame] | 37 | SkCanvas canvas(*bm); |
| 38 | canvas.drawCircle(10, 10, 10, circlePaint); |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | class BitmapShaderGM : public GM { |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 42 | |
caryclark | 63c684a | 2015-02-25 09:04:04 -0800 | [diff] [blame] | 43 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 44 | void onOnceBeforeDraw() override { |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 45 | this->setBGColor(SK_ColorGRAY); |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 46 | draw_bm(&fBitmap); |
| 47 | draw_mask(&fMask); |
| 48 | } |
| 49 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 50 | SkString onShortName() override { |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 51 | return SkString("bitmapshaders"); |
| 52 | } |
| 53 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 54 | SkISize onISize() override { |
commit-bot@chromium.org | 08f6d86 | 2014-03-24 02:49:48 +0000 | [diff] [blame] | 55 | return SkISize::Make(150, 100); |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 56 | } |
| 57 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 58 | void onDraw(SkCanvas* canvas) override { |
skia.committer@gmail.com | 56710d1 | 2013-12-07 07:02:21 +0000 | [diff] [blame] | 59 | SkPaint paint; |
commit-bot@chromium.org | e5957f6 | 2014-03-18 16:28:12 +0000 | [diff] [blame] | 60 | |
commit-bot@chromium.org | 08f6d86 | 2014-03-24 02:49:48 +0000 | [diff] [blame] | 61 | for (int i = 0; i < 2; i++) { |
| 62 | SkMatrix s; |
| 63 | s.reset(); |
| 64 | if (1 == i) { |
commit-bot@chromium.org | a723fee | 2014-03-24 14:54:04 +0000 | [diff] [blame] | 65 | s.setScale(1.5f, 1.5f); |
commit-bot@chromium.org | 08f6d86 | 2014-03-24 02:49:48 +0000 | [diff] [blame] | 66 | s.postTranslate(2, 2); |
| 67 | } |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 68 | |
commit-bot@chromium.org | 08f6d86 | 2014-03-24 02:49:48 +0000 | [diff] [blame] | 69 | canvas->save(); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 70 | paint.setShader(SkShader::MakeBitmapShader(fBitmap, SkShader::kClamp_TileMode, |
| 71 | SkShader::kClamp_TileMode, &s)); |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 72 | |
commit-bot@chromium.org | 08f6d86 | 2014-03-24 02:49:48 +0000 | [diff] [blame] | 73 | // draw the shader with a bitmap mask |
| 74 | canvas->drawBitmap(fMask, 0, 0, &paint); |
Michael Ludwig | b7d64b9 | 2019-02-11 11:09:15 -0500 | [diff] [blame] | 75 | // no blue circle expected (the bitmap shader's coordinates are aligned to CTM still) |
commit-bot@chromium.org | 08f6d86 | 2014-03-24 02:49:48 +0000 | [diff] [blame] | 76 | canvas->drawBitmap(fMask, 30, 0, &paint); |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 77 | |
commit-bot@chromium.org | 08f6d86 | 2014-03-24 02:49:48 +0000 | [diff] [blame] | 78 | canvas->translate(0, 25); |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 79 | |
commit-bot@chromium.org | 08f6d86 | 2014-03-24 02:49:48 +0000 | [diff] [blame] | 80 | canvas->drawCircle(10, 10, 10, paint); |
| 81 | canvas->drawCircle(40, 10, 10, paint); // no blue circle expected |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 82 | |
commit-bot@chromium.org | 08f6d86 | 2014-03-24 02:49:48 +0000 | [diff] [blame] | 83 | canvas->translate(0, 25); |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 84 | |
commit-bot@chromium.org | 08f6d86 | 2014-03-24 02:49:48 +0000 | [diff] [blame] | 85 | // clear the shader, colorized by a solid color with a bitmap mask |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 86 | paint.setShader(nullptr); |
commit-bot@chromium.org | 08f6d86 | 2014-03-24 02:49:48 +0000 | [diff] [blame] | 87 | paint.setColor(SK_ColorGREEN); |
| 88 | canvas->drawBitmap(fMask, 0, 0, &paint); |
| 89 | canvas->drawBitmap(fMask, 30, 0, &paint); |
| 90 | |
| 91 | canvas->translate(0, 25); |
| 92 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 93 | paint.setShader(SkShader::MakeBitmapShader(fMask, SkShader::kRepeat_TileMode, |
| 94 | SkShader::kRepeat_TileMode, &s)); |
commit-bot@chromium.org | 08f6d86 | 2014-03-24 02:49:48 +0000 | [diff] [blame] | 95 | paint.setColor(SK_ColorRED); |
| 96 | |
| 97 | // draw the mask using the shader and a color |
| 98 | canvas->drawRect(SkRect::MakeXYWH(0, 0, 20, 20), paint); |
| 99 | canvas->drawRect(SkRect::MakeXYWH(30, 0, 20, 20), paint); |
| 100 | canvas->restore(); |
| 101 | canvas->translate(60, 0); |
| 102 | } |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | private: |
| 106 | SkBitmap fBitmap; |
| 107 | SkBitmap fMask; |
| 108 | |
| 109 | typedef GM INHERITED; |
| 110 | }; |
| 111 | |
Yuqian Li | f3c1f09 | 2017-11-29 16:07:22 -0500 | [diff] [blame] | 112 | DEF_SIMPLE_GM(hugebitmapshader, canvas, 100, 100) { |
| 113 | SkPaint paint; |
| 114 | SkBitmap bitmap; |
| 115 | |
| 116 | // The huge height will exceed GL_MAX_TEXTURE_SIZE. We test that the GL backend will at least |
| 117 | // draw something with a default paint instead of drawing nothing. |
| 118 | // |
| 119 | // (See https://skia-review.googlesource.com/c/skia/+/73200) |
| 120 | int bitmapW = 1; |
| 121 | int bitmapH = 60000; |
| 122 | if (auto* ctx = canvas->getGrContext()) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 123 | bitmapH = ctx->priv().caps()->maxTextureSize() + 1; |
Yuqian Li | f3c1f09 | 2017-11-29 16:07:22 -0500 | [diff] [blame] | 124 | } |
| 125 | bitmap.setInfo(SkImageInfo::MakeA8(bitmapW, bitmapH), bitmapW); |
| 126 | uint8_t* pixels = new uint8_t[bitmapH]; |
| 127 | for(int i = 0; i < bitmapH; ++i) { |
| 128 | pixels[i] = i & 0xff; |
| 129 | } |
| 130 | bitmap.setPixels(pixels); |
| 131 | |
| 132 | paint.setShader(SkShader::MakeBitmapShader(bitmap, |
| 133 | SkShader::kMirror_TileMode, SkShader::kMirror_TileMode)); |
| 134 | paint.setColor(SK_ColorRED); |
| 135 | paint.setAntiAlias(true); |
| 136 | canvas->drawCircle(50, 50, 50, paint); |
| 137 | delete [] pixels; |
| 138 | } |
| 139 | |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 140 | ////////////////////////////////////////////////////////////////////////////// |
| 141 | |
Hal Canary | e964c18 | 2019-01-23 10:22:01 -0500 | [diff] [blame] | 142 | DEF_GM( return new BitmapShaderGM; ) |
commit-bot@chromium.org | 7842c81 | 2013-12-06 20:14:39 +0000 | [diff] [blame] | 143 | |
| 144 | } |