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 | |
junov@google.com | 869d6d9 | 2011-05-17 15:47:10 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 9 | #include "SkBlurMask.h" |
| 10 | #include "SkBlurMaskFilter.h" |
junov@google.com | 869d6d9 | 2011-05-17 15:47:10 +0000 | [diff] [blame] | 11 | #include "SkCanvas.h" |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 12 | #include "SkSurface.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 | } |
| 24 | bm.unlockPixels(); |
| 25 | return bm; |
| 26 | } |
junov@google.com | 869d6d9 | 2011-05-17 15:47:10 +0000 | [diff] [blame] | 27 | |
| 28 | class TextureDomainView : public SampleView { |
| 29 | SkBitmap fBM; |
| 30 | |
| 31 | public: |
| 32 | TextureDomainView(){ |
| 33 | fBM = make_bitmap(); |
| 34 | } |
| 35 | |
| 36 | protected: |
| 37 | // overrides from SkEventSink |
| 38 | virtual bool onQuery(SkEvent* evt) { |
| 39 | if (SampleCode::TitleQ(*evt)) { |
junov@google.com | 1d32978 | 2011-07-28 20:10:09 +0000 | [diff] [blame] | 40 | SampleCode::TitleR(evt, "Texture Domain"); |
junov@google.com | 869d6d9 | 2011-05-17 15:47:10 +0000 | [diff] [blame] | 41 | return true; |
| 42 | } |
| 43 | return this->INHERITED::onQuery(evt); |
| 44 | } |
| 45 | |
| 46 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 47 | SkRect srcRect; |
junov@google.com | 869d6d9 | 2011-05-17 15:47:10 +0000 | [diff] [blame] | 48 | SkRect dstRect; |
| 49 | SkPaint paint; |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 50 | paint.setFilterQuality(kLow_SkFilterQuality); |
twiz@google.com | 76b8274 | 2011-06-02 20:30:02 +0000 | [diff] [blame] | 51 | |
| 52 | // Test that bitmap draws from malloc-backed bitmaps respect |
| 53 | // the constrained texture domain. |
| 54 | srcRect.setXYWH(1, 1, 3, 3); |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 55 | dstRect.setXYWH(5, 5, 305, 305); |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 56 | canvas->drawBitmapRect(fBM, srcRect, dstRect, &paint, SkCanvas::kStrict_SrcRectConstraint); |
twiz@google.com | 76b8274 | 2011-06-02 20:30:02 +0000 | [diff] [blame] | 57 | |
| 58 | // Test that bitmap draws across separate devices also respect |
| 59 | // the constrainted texture domain. |
| 60 | // Note: GPU-backed bitmaps follow a different rendering path |
| 61 | // when copying from one GPU device to another. |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 62 | SkImageInfo info = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 63 | auto surface(canvas->makeSurface(info)); |
twiz@google.com | 76b8274 | 2011-06-02 20:30:02 +0000 | [diff] [blame] | 64 | |
| 65 | srcRect.setXYWH(1, 1, 3, 3); |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 66 | dstRect.setXYWH(1, 1, 3, 3); |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 67 | surface->getCanvas()->drawBitmapRect(fBM, srcRect, dstRect, &paint, |
reed | 84984ef | 2015-07-17 07:09:43 -0700 | [diff] [blame] | 68 | SkCanvas::kStrict_SrcRectConstraint); |
twiz@google.com | 76b8274 | 2011-06-02 20:30:02 +0000 | [diff] [blame] | 69 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 70 | sk_sp<SkImage> image(surface->makeImageSnapshot()); |
twiz@google.com | 76b8274 | 2011-06-02 20:30:02 +0000 | [diff] [blame] | 71 | |
| 72 | srcRect.setXYWH(1, 1, 3, 3); |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 73 | dstRect.setXYWH(405, 5, 305, 305); |
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 | |
| 76 | // Test that bitmap blurring using a subrect |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 77 | // renders correctly |
junov@google.com | d935cfb | 2011-06-27 20:48:23 +0000 | [diff] [blame] | 78 | srcRect.setXYWH(1, 1, 3, 3); |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 79 | dstRect.setXYWH(5, 405, 305, 305); |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 80 | paint.setMaskFilter(SkBlurMaskFilter::Make( |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 81 | kNormal_SkBlurStyle, |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 82 | SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)), |
junov@google.com | 1d32978 | 2011-07-28 20:10:09 +0000 | [diff] [blame] | 83 | SkBlurMaskFilter::kHighQuality_BlurFlag | |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 84 | SkBlurMaskFilter::kIgnoreTransform_BlurFlag)); |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 85 | canvas->drawImageRect(image, srcRect, dstRect, &paint); |
junov@google.com | d935cfb | 2011-06-27 20:48:23 +0000 | [diff] [blame] | 86 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 87 | // Blur and a rotation + nullptr src rect |
junov@google.com | d935cfb | 2011-06-27 20:48:23 +0000 | [diff] [blame] | 88 | // This should not trigger the texture domain code |
| 89 | // but it will test a code path in SkGpuDevice::drawBitmap |
| 90 | // that handles blurs with rects transformed to non- |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 91 | // orthogonal rects. It also tests the nullptr src rect handling |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 92 | paint.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle, |
| 93 | SkBlurMask::ConvertRadiusToSigma(5), |
| 94 | SkBlurMaskFilter::kHighQuality_BlurFlag)); |
junov@google.com | 1d32978 | 2011-07-28 20:10:09 +0000 | [diff] [blame] | 95 | |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 96 | dstRect.setXYWH(-150, -150, 300, 300); |
junov@google.com | d935cfb | 2011-06-27 20:48:23 +0000 | [diff] [blame] | 97 | canvas->translate(550, 550); |
| 98 | canvas->rotate(45); |
reed | 84984ef | 2015-07-17 07:09:43 -0700 | [diff] [blame] | 99 | canvas->drawBitmapRect(fBM, dstRect, &paint); |
junov@google.com | 869d6d9 | 2011-05-17 15:47:10 +0000 | [diff] [blame] | 100 | } |
| 101 | private: |
| 102 | typedef SkView INHERITED; |
| 103 | }; |
| 104 | |
| 105 | ////////////////////////////////////////////////////////////////////////////// |
| 106 | |
| 107 | static SkView* MyFactory() { return new TextureDomainView; } |
| 108 | static SkViewRegister reg(MyFactory); |