blob: 4ad28a76e4e2bc332a29c335cb7f09d147f54903 [file] [log] [blame]
commit-bot@chromium.org7842c812013-12-06 20:14:39 +00001/*
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 Wagner6a34f3a2019-05-01 10:59:30 -04007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/core/SkBitmap.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040010#include "include/core/SkCanvas.h"
11#include "include/core/SkColor.h"
12#include "include/core/SkImageInfo.h"
13#include "include/core/SkMatrix.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/core/SkPaint.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040015#include "include/core/SkRect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/core/SkShader.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040017#include "include/core/SkSize.h"
18#include "include/core/SkString.h"
19#include "include/core/SkTileMode.h"
20#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "include/gpu/GrContext.h"
22#include "src/gpu/GrCaps.h"
23#include "src/gpu/GrContextPriv.h"
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000024
25namespace skiagm {
26
27static void draw_bm(SkBitmap* bm) {
skia.committer@gmail.com56710d12013-12-07 07:02:21 +000028 SkPaint bluePaint;
29 bluePaint.setColor(SK_ColorBLUE);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000030
reed@google.comeb9a46c2014-01-25 16:46:20 +000031 bm->allocN32Pixels(20, 20);
skia.committer@gmail.com56710d12013-12-07 07:02:21 +000032 bm->eraseColor(SK_ColorRED);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000033
skia.committer@gmail.com56710d12013-12-07 07:02:21 +000034 SkCanvas canvas(*bm);
35 canvas.drawCircle(10, 10, 5, bluePaint);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000036}
37
38static void draw_mask(SkBitmap* bm) {
skia.committer@gmail.com56710d12013-12-07 07:02:21 +000039 SkPaint circlePaint;
40 circlePaint.setColor(SK_ColorBLACK);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000041
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000042 bm->allocPixels(SkImageInfo::MakeA8(20, 20));
skia.committer@gmail.com56710d12013-12-07 07:02:21 +000043 bm->eraseColor(SK_ColorTRANSPARENT);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000044
skia.committer@gmail.com56710d12013-12-07 07:02:21 +000045 SkCanvas canvas(*bm);
46 canvas.drawCircle(10, 10, 10, circlePaint);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000047}
48
49class BitmapShaderGM : public GM {
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000050
caryclark63c684a2015-02-25 09:04:04 -080051protected:
mtklein36352bf2015-03-25 18:17:31 -070052 void onOnceBeforeDraw() override {
Mike Kleind46dce32018-08-16 10:17:03 -040053 this->setBGColor(SK_ColorGRAY);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000054 draw_bm(&fBitmap);
55 draw_mask(&fMask);
56 }
57
mtkleinf0599002015-07-13 06:18:39 -070058 SkString onShortName() override {
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000059 return SkString("bitmapshaders");
60 }
61
mtkleinf0599002015-07-13 06:18:39 -070062 SkISize onISize() override {
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000063 return SkISize::Make(150, 100);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000064 }
65
mtkleinf0599002015-07-13 06:18:39 -070066 void onDraw(SkCanvas* canvas) override {
skia.committer@gmail.com56710d12013-12-07 07:02:21 +000067 SkPaint paint;
commit-bot@chromium.orge5957f62014-03-18 16:28:12 +000068
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000069 for (int i = 0; i < 2; i++) {
70 SkMatrix s;
71 s.reset();
72 if (1 == i) {
commit-bot@chromium.orga723fee2014-03-24 14:54:04 +000073 s.setScale(1.5f, 1.5f);
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000074 s.postTranslate(2, 2);
75 }
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000076
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000077 canvas->save();
Mike Reed50acf8f2019-04-08 13:20:23 -040078 paint.setShader(fBitmap.makeShader(&s));
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000079
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000080 // draw the shader with a bitmap mask
81 canvas->drawBitmap(fMask, 0, 0, &paint);
Michael Ludwigb7d64b92019-02-11 11:09:15 -050082 // no blue circle expected (the bitmap shader's coordinates are aligned to CTM still)
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000083 canvas->drawBitmap(fMask, 30, 0, &paint);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000084
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000085 canvas->translate(0, 25);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000086
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000087 canvas->drawCircle(10, 10, 10, paint);
88 canvas->drawCircle(40, 10, 10, paint); // no blue circle expected
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000089
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000090 canvas->translate(0, 25);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000091
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000092 // clear the shader, colorized by a solid color with a bitmap mask
halcanary96fcdcc2015-08-27 07:41:13 -070093 paint.setShader(nullptr);
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000094 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 Reed50acf8f2019-04-08 13:20:23 -0400100 paint.setShader(fMask.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, &s));
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +0000101 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.org7842c812013-12-06 20:14:39 +0000109 }
110
111private:
112 SkBitmap fBitmap;
113 SkBitmap fMask;
114
115 typedef GM INHERITED;
116};
117
Yuqian Lif3c1f092017-11-29 16:07:22 -0500118DEF_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 Phillips9da87e02019-02-04 13:26:26 -0500129 bitmapH = ctx->priv().caps()->maxTextureSize() + 1;
Yuqian Lif3c1f092017-11-29 16:07:22 -0500130 }
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 Reed50acf8f2019-04-08 13:20:23 -0400138 paint.setShader(bitmap.makeShader(SkTileMode::kMirror, SkTileMode::kMirror));
Yuqian Lif3c1f092017-11-29 16:07:22 -0500139 paint.setColor(SK_ColorRED);
140 paint.setAntiAlias(true);
141 canvas->drawCircle(50, 50, 50, paint);
142 delete [] pixels;
143}
144
commit-bot@chromium.org7842c812013-12-06 20:14:39 +0000145//////////////////////////////////////////////////////////////////////////////
146
Hal Canarye964c182019-01-23 10:22:01 -0500147DEF_GM( return new BitmapShaderGM; )
commit-bot@chromium.org7842c812013-12-06 20:14:39 +0000148
149}