blob: 06aca1c021b4484f0ed3128ca9b337f7e453ea66 [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
18static void check_isabitmap(skiatest::Reporter* reporter, SkShader* shader,
19 int expectedW, int expectedH,
20 SkShader::TileMode expectedX, SkShader::TileMode expectedY,
reed320a40d2016-08-02 06:12:06 -070021 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
25#ifdef SK_SUPPORT_LEGACY_SHADER_ISABITMAP
26 SkBitmap bm;
reedf1ac1822016-08-01 11:24:14 -070027 REPORTER_ASSERT(reporter, shader->isABitmap(&bm, &localM, tileModes));
28 REPORTER_ASSERT(reporter, bm.width() == expectedW);
29 REPORTER_ASSERT(reporter, bm.height() == expectedH);
30 REPORTER_ASSERT(reporter, localM == expectedM);
31 REPORTER_ASSERT(reporter, tileModes[0] == expectedX);
32 REPORTER_ASSERT(reporter, tileModes[1] == expectedY);
Mike Reed627778d2016-09-28 17:13:38 -040033#endif
reedf1ac1822016-08-01 11:24:14 -070034
35 // wack these so we don't get a false positive
36 localM.setScale(9999, -9999);
37 tileModes[0] = tileModes[1] = (SkShader::TileMode)99;
38
39 SkImage* image = shader->isAImage(&localM, tileModes);
reed320a40d2016-08-02 06:12:06 -070040 REPORTER_ASSERT(reporter, image);
41 REPORTER_ASSERT(reporter, image->width() == expectedW);
42 REPORTER_ASSERT(reporter, image->height() == expectedH);
43 REPORTER_ASSERT(reporter, localM == expectedM);
44 REPORTER_ASSERT(reporter, tileModes[0] == expectedX);
45 REPORTER_ASSERT(reporter, tileModes[1] == expectedY);
reedf1ac1822016-08-01 11:24:14 -070046}
47
48DEF_TEST(Shader_isABitmap, reporter) {
49 const int W = 100;
50 const int H = 100;
51 SkBitmap bm;
52 bm.allocN32Pixels(W, H);
53 auto img = SkImage::MakeFromBitmap(bm);
54 const SkMatrix localM = SkMatrix::MakeScale(2, 3);
55 const SkShader::TileMode tmx = SkShader::kRepeat_TileMode;
56 const SkShader::TileMode tmy = SkShader::kMirror_TileMode;
57
58 auto shader0 = SkShader::MakeBitmapShader(bm, tmx, tmy, &localM);
59 auto shader1 = SkImage::MakeFromBitmap(bm)->makeShader(tmx, tmy, &localM);
60
reed320a40d2016-08-02 06:12:06 -070061 check_isabitmap(reporter, shader0.get(), W, H, tmx, tmy, localM);
62 check_isabitmap(reporter, shader1.get(), W, H, tmx, tmy, localM);
reedf1ac1822016-08-01 11:24:14 -070063}
Herb Derbye556bf72017-02-28 11:14:47 -050064
65// Make sure things are ok with just a single leg.
66DEF_TEST(ComposeShaderSingle, reporter) {
67 SkBitmap srcBitmap;
68 srcBitmap.allocN32Pixels(10, 10);
69 srcBitmap.eraseColor(SK_ColorRED);
70 SkCanvas canvas(srcBitmap);
71 SkPaint p;
72 p.setShader(
73 SkShader::MakeComposeShader(
74 SkShader::MakeEmptyShader(),
75 SkPerlinNoiseShader::MakeFractalNoise(1.0f, 1.0f, 2, 0.0f),
76 SkBlendMode::kClear));
77 SkRRect rr;
78 SkVector rd[] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
79 rr.setRectRadii({0, 0, 0, 0}, rd);
80 canvas.drawRRect(rr, p);
81}