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