epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | */ |
commit-bot@chromium.org | ab13167 | 2013-09-13 12:39:55 +0000 | [diff] [blame] | 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
Mike Reed | fe2d3c7 | 2019-12-30 10:09:43 -0500 | [diff] [blame] | 9 | #include "include/core/SkMaskFilter.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkSurface.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "samplecode/Sample.h" |
| 12 | #include "src/core/SkBlurMask.h" |
junov@google.com | 869d6d9 | 2011-05-17 15:47:10 +0000 | [diff] [blame] | 13 | |
commit-bot@chromium.org | ab13167 | 2013-09-13 12:39:55 +0000 | [diff] [blame] | 14 | static SkBitmap make_bitmap() { |
junov@google.com | 869d6d9 | 2011-05-17 15:47:10 +0000 | [diff] [blame] | 15 | SkBitmap bm; |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 16 | bm.allocN32Pixels(5, 5); |
junov@google.com | 869d6d9 | 2011-05-17 15:47:10 +0000 | [diff] [blame] | 17 | |
| 18 | for (int y = 0; y < bm.height(); y++) { |
| 19 | uint32_t* p = bm.getAddr32(0, y); |
| 20 | for (int x = 0; x < bm.width(); x++) { |
| 21 | p[x] = ((x + y) & 1) ? SK_ColorWHITE : SK_ColorBLACK; |
| 22 | } |
| 23 | } |
junov@google.com | 869d6d9 | 2011-05-17 15:47:10 +0000 | [diff] [blame] | 24 | return bm; |
| 25 | } |
junov@google.com | 869d6d9 | 2011-05-17 15:47:10 +0000 | [diff] [blame] | 26 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 27 | class TextureDomainView : public Sample { |
junov@google.com | 869d6d9 | 2011-05-17 15:47:10 +0000 | [diff] [blame] | 28 | SkBitmap fBM; |
| 29 | |
| 30 | public: |
| 31 | TextureDomainView(){ |
| 32 | fBM = make_bitmap(); |
| 33 | } |
| 34 | |
| 35 | protected: |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 36 | virtual SkString name() { return SkString("Texture Domain"); } |
junov@google.com | 869d6d9 | 2011-05-17 15:47:10 +0000 | [diff] [blame] | 37 | |
| 38 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 39 | SkRect srcRect; |
junov@google.com | 869d6d9 | 2011-05-17 15:47:10 +0000 | [diff] [blame] | 40 | SkRect dstRect; |
| 41 | SkPaint paint; |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 42 | paint.setFilterQuality(kLow_SkFilterQuality); |
twiz@google.com | 76b8274 | 2011-06-02 20:30:02 +0000 | [diff] [blame] | 43 | |
| 44 | // Test that bitmap draws from malloc-backed bitmaps respect |
| 45 | // the constrained texture domain. |
| 46 | srcRect.setXYWH(1, 1, 3, 3); |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 47 | dstRect.setXYWH(5, 5, 305, 305); |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 48 | canvas->drawBitmapRect(fBM, srcRect, dstRect, &paint, SkCanvas::kStrict_SrcRectConstraint); |
twiz@google.com | 76b8274 | 2011-06-02 20:30:02 +0000 | [diff] [blame] | 49 | |
| 50 | // Test that bitmap draws across separate devices also respect |
| 51 | // the constrainted texture domain. |
| 52 | // Note: GPU-backed bitmaps follow a different rendering path |
| 53 | // when copying from one GPU device to another. |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 54 | SkImageInfo info = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 55 | auto surface(canvas->makeSurface(info)); |
twiz@google.com | 76b8274 | 2011-06-02 20:30:02 +0000 | [diff] [blame] | 56 | |
| 57 | srcRect.setXYWH(1, 1, 3, 3); |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 58 | dstRect.setXYWH(1, 1, 3, 3); |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 59 | surface->getCanvas()->drawBitmapRect(fBM, srcRect, dstRect, &paint, |
reed | 84984ef | 2015-07-17 07:09:43 -0700 | [diff] [blame] | 60 | SkCanvas::kStrict_SrcRectConstraint); |
twiz@google.com | 76b8274 | 2011-06-02 20:30:02 +0000 | [diff] [blame] | 61 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 62 | sk_sp<SkImage> image(surface->makeImageSnapshot()); |
twiz@google.com | 76b8274 | 2011-06-02 20:30:02 +0000 | [diff] [blame] | 63 | |
| 64 | srcRect.setXYWH(1, 1, 3, 3); |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 65 | dstRect.setXYWH(405, 5, 305, 305); |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 66 | canvas->drawImageRect(image, srcRect, dstRect, &paint); |
junov@google.com | d935cfb | 2011-06-27 20:48:23 +0000 | [diff] [blame] | 67 | |
| 68 | // Test that bitmap blurring using a subrect |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 69 | // renders correctly |
junov@google.com | d935cfb | 2011-06-27 20:48:23 +0000 | [diff] [blame] | 70 | srcRect.setXYWH(1, 1, 3, 3); |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 71 | dstRect.setXYWH(5, 405, 305, 305); |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 72 | paint.setMaskFilter(SkMaskFilter::MakeBlur( |
| 73 | kNormal_SkBlurStyle, SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)), false)); |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 74 | canvas->drawImageRect(image, srcRect, dstRect, &paint); |
junov@google.com | d935cfb | 2011-06-27 20:48:23 +0000 | [diff] [blame] | 75 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 76 | // Blur and a rotation + nullptr src rect |
junov@google.com | d935cfb | 2011-06-27 20:48:23 +0000 | [diff] [blame] | 77 | // This should not trigger the texture domain code |
| 78 | // but it will test a code path in SkGpuDevice::drawBitmap |
| 79 | // that handles blurs with rects transformed to non- |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 80 | // orthogonal rects. It also tests the nullptr src rect handling |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 81 | paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, |
| 82 | SkBlurMask::ConvertRadiusToSigma(5))); |
junov@google.com | 1d32978 | 2011-07-28 20:10:09 +0000 | [diff] [blame] | 83 | |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 84 | dstRect.setXYWH(-150, -150, 300, 300); |
junov@google.com | d935cfb | 2011-06-27 20:48:23 +0000 | [diff] [blame] | 85 | canvas->translate(550, 550); |
| 86 | canvas->rotate(45); |
reed | 84984ef | 2015-07-17 07:09:43 -0700 | [diff] [blame] | 87 | canvas->drawBitmapRect(fBM, dstRect, &paint); |
junov@google.com | 869d6d9 | 2011-05-17 15:47:10 +0000 | [diff] [blame] | 88 | } |
| 89 | private: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 90 | typedef Sample INHERITED; |
junov@google.com | 869d6d9 | 2011-05-17 15:47:10 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | ////////////////////////////////////////////////////////////////////////////// |
| 94 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 95 | DEF_SAMPLE( return new TextureDomainView(); ) |