Matt Sarett | 547a727 | 2017-04-12 11:52:47 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
| 9 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 10 | #include "include/core/SkColor.h" |
| 11 | #include "include/core/SkFilterQuality.h" |
| 12 | #include "include/core/SkImage.h" |
| 13 | #include "include/core/SkImageInfo.h" |
| 14 | #include "include/core/SkMatrix.h" |
| 15 | #include "include/core/SkPaint.h" |
| 16 | #include "include/core/SkRect.h" |
| 17 | #include "include/core/SkRefCnt.h" |
| 18 | #include "include/core/SkShader.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "include/core/SkSurface.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 20 | #include "include/core/SkTileMode.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 21 | #include "tools/Resources.h" |
| 22 | #include "tools/ToolUtils.h" |
Matt Sarett | 547a727 | 2017-04-12 11:52:47 -0400 | [diff] [blame] | 23 | |
| 24 | static sk_sp<SkImage> make_image(SkCanvas* rootCanvas, SkColor color) { |
| 25 | SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 26 | auto surface(ToolUtils::makeSurface(rootCanvas, info)); |
Matt Sarett | 547a727 | 2017-04-12 11:52:47 -0400 | [diff] [blame] | 27 | |
| 28 | SkPaint paint; |
| 29 | paint.setAntiAlias(true); |
| 30 | paint.setColor(color); |
| 31 | surface->getCanvas()->drawIRect(SkIRect::MakeXYWH(25, 25, 50, 50), paint); |
| 32 | return surface->makeImageSnapshot(); |
| 33 | } |
| 34 | |
| 35 | DEF_SIMPLE_GM(localmatriximageshader, canvas, 250, 250) { |
| 36 | sk_sp<SkImage> redImage = make_image(canvas, SK_ColorRED); |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 37 | SkMatrix translate = SkMatrix::Translate(100.0f, 0.0f); |
Matt Sarett | 547a727 | 2017-04-12 11:52:47 -0400 | [diff] [blame] | 38 | SkMatrix rotate; |
| 39 | rotate.setRotate(45.0f); |
Mike Reed | 0acd795 | 2017-04-28 11:12:19 -0400 | [diff] [blame] | 40 | sk_sp<SkShader> redImageShader = redImage->makeShader(&translate); |
Matt Sarett | 547a727 | 2017-04-12 11:52:47 -0400 | [diff] [blame] | 41 | sk_sp<SkShader> redLocalMatrixShader = redImageShader->makeWithLocalMatrix(rotate); |
| 42 | |
| 43 | // Rotate about the origin will happen first. |
| 44 | SkPaint paint; |
| 45 | paint.setShader(redLocalMatrixShader); |
| 46 | canvas->drawIRect(SkIRect::MakeWH(250, 250), paint); |
| 47 | |
| 48 | sk_sp<SkImage> blueImage = make_image(canvas, SK_ColorBLUE); |
Mike Reed | 0acd795 | 2017-04-28 11:12:19 -0400 | [diff] [blame] | 49 | sk_sp<SkShader> blueImageShader = blueImage->makeShader(&rotate); |
Matt Sarett | 547a727 | 2017-04-12 11:52:47 -0400 | [diff] [blame] | 50 | sk_sp<SkShader> blueLocalMatrixShader = blueImageShader->makeWithLocalMatrix(translate); |
| 51 | |
| 52 | // Translate will happen first. |
| 53 | paint.setShader(blueLocalMatrixShader); |
| 54 | canvas->drawIRect(SkIRect::MakeWH(250, 250), paint); |
| 55 | |
| 56 | canvas->translate(100.0f, 0.0f); |
| 57 | |
| 58 | // Use isAImage() and confirm that the shaders will draw exactly the same (to the right by 100). |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 59 | SkTileMode mode[2]; |
Matt Sarett | 547a727 | 2017-04-12 11:52:47 -0400 | [diff] [blame] | 60 | SkMatrix matrix; |
| 61 | SkImage* image = redLocalMatrixShader->isAImage(&matrix, mode); |
| 62 | paint.setShader(image->makeShader(mode[0], mode[1], &matrix)); |
| 63 | canvas->drawIRect(SkIRect::MakeWH(250, 250), paint); |
| 64 | image = blueLocalMatrixShader->isAImage(&matrix, mode); |
| 65 | paint.setShader(image->makeShader(mode[0], mode[1], &matrix)); |
| 66 | canvas->drawIRect(SkIRect::MakeWH(250, 250), paint); |
| 67 | } |
Brian Osman | 5b6fa82 | 2018-02-14 10:16:32 -0500 | [diff] [blame] | 68 | |
| 69 | DEF_SIMPLE_GM(localmatriximageshader_filtering, canvas, 256, 256) { |
| 70 | // Test that filtering decisions (eg bicubic for upscale) are made correctly when the scale |
| 71 | // comes from a local matrix shader. |
| 72 | auto image = GetResourceAsImage("images/mandrill_256.png"); |
| 73 | SkPaint p; |
| 74 | p.setFilterQuality(kHigh_SkFilterQuality); |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 75 | SkMatrix m = SkMatrix::Scale(2, 2); |
Brian Osman | 5b6fa82 | 2018-02-14 10:16:32 -0500 | [diff] [blame] | 76 | p.setShader(image->makeShader()->makeWithLocalMatrix(m)); |
| 77 | |
| 78 | canvas->drawRect(SkRect::MakeXYWH(0, 0, 256, 256), p); |
| 79 | } |