blob: 650eb5d918325389d80337b9b97f94d70033eb2d [file] [log] [blame]
reedf1ac1822016-08-01 11:24:14 -07001/*
2 * Copyright 2016 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
8#include "Test.h"
9#include "SkBitmap.h"
Herb Derbye556bf72017-02-28 11:14:47 -050010#include "SkCanvas.h"
reedf1ac1822016-08-01 11:24:14 -070011#include "SkImage.h"
Herb Derbye556bf72017-02-28 11:14:47 -050012#include "SkPerlinNoiseShader.h"
13#include "SkRRect.h"
reedf1ac1822016-08-01 11:24:14 -070014#include "SkShader.h"
15#include "SkSurface.h"
16#include "SkData.h"
17
Mike Reed09bc78b2018-10-15 12:39:03 -040018static void check_isaimage(skiatest::Reporter* reporter, SkShader* shader,
19 int expectedW, int expectedH,
20 SkShader::TileMode expectedX, SkShader::TileMode expectedY,
21 const SkMatrix& expectedM) {
reedf1ac1822016-08-01 11:24:14 -070022 SkShader::TileMode tileModes[2];
23 SkMatrix localM;
Mike Reed627778d2016-09-28 17:13:38 -040024
reedf1ac1822016-08-01 11:24:14 -070025 // wack these so we don't get a false positive
26 localM.setScale(9999, -9999);
27 tileModes[0] = tileModes[1] = (SkShader::TileMode)99;
28
29 SkImage* image = shader->isAImage(&localM, tileModes);
reed320a40d2016-08-02 06:12:06 -070030 REPORTER_ASSERT(reporter, image);
31 REPORTER_ASSERT(reporter, image->width() == expectedW);
32 REPORTER_ASSERT(reporter, image->height() == expectedH);
33 REPORTER_ASSERT(reporter, localM == expectedM);
34 REPORTER_ASSERT(reporter, tileModes[0] == expectedX);
35 REPORTER_ASSERT(reporter, tileModes[1] == expectedY);
reedf1ac1822016-08-01 11:24:14 -070036}
37
Mike Reed09bc78b2018-10-15 12:39:03 -040038DEF_TEST(Shader_isAImage, reporter) {
reedf1ac1822016-08-01 11:24:14 -070039 const int W = 100;
40 const int H = 100;
41 SkBitmap bm;
42 bm.allocN32Pixels(W, H);
43 auto img = SkImage::MakeFromBitmap(bm);
44 const SkMatrix localM = SkMatrix::MakeScale(2, 3);
45 const SkShader::TileMode tmx = SkShader::kRepeat_TileMode;
46 const SkShader::TileMode tmy = SkShader::kMirror_TileMode;
47
48 auto shader0 = SkShader::MakeBitmapShader(bm, tmx, tmy, &localM);
49 auto shader1 = SkImage::MakeFromBitmap(bm)->makeShader(tmx, tmy, &localM);
50
Mike Reed09bc78b2018-10-15 12:39:03 -040051 check_isaimage(reporter, shader0.get(), W, H, tmx, tmy, localM);
52 check_isaimage(reporter, shader1.get(), W, H, tmx, tmy, localM);
reedf1ac1822016-08-01 11:24:14 -070053}
Herb Derbye556bf72017-02-28 11:14:47 -050054
55// Make sure things are ok with just a single leg.
56DEF_TEST(ComposeShaderSingle, reporter) {
57 SkBitmap srcBitmap;
58 srcBitmap.allocN32Pixels(10, 10);
59 srcBitmap.eraseColor(SK_ColorRED);
60 SkCanvas canvas(srcBitmap);
61 SkPaint p;
62 p.setShader(
63 SkShader::MakeComposeShader(
64 SkShader::MakeEmptyShader(),
65 SkPerlinNoiseShader::MakeFractalNoise(1.0f, 1.0f, 2, 0.0f),
66 SkBlendMode::kClear));
67 SkRRect rr;
68 SkVector rd[] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
69 rr.setRectRadii({0, 0, 0, 0}, rd);
70 canvas.drawRRect(rr, p);
71}