epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
junov@google.com | 869d6d9 | 2011-05-17 15:47:10 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
| 9 | #include "SkCanvas.h" |
twiz@google.com | 76b8274 | 2011-06-02 20:30:02 +0000 | [diff] [blame] | 10 | #include "SkDevice.h" |
junov@google.com | d935cfb | 2011-06-27 20:48:23 +0000 | [diff] [blame] | 11 | #include "SkBlurMaskFilter.h" |
junov@google.com | 869d6d9 | 2011-05-17 15:47:10 +0000 | [diff] [blame] | 12 | |
| 13 | namespace { |
| 14 | SkBitmap make_bitmap() { |
| 15 | SkBitmap bm; |
twiz@google.com | 76b8274 | 2011-06-02 20:30:02 +0000 | [diff] [blame] | 16 | bm.setConfig(SkBitmap::kARGB_8888_Config , 5, 5); |
junov@google.com | 869d6d9 | 2011-05-17 15:47:10 +0000 | [diff] [blame] | 17 | bm.allocPixels(); |
| 18 | |
| 19 | for (int y = 0; y < bm.height(); y++) { |
| 20 | uint32_t* p = bm.getAddr32(0, y); |
| 21 | for (int x = 0; x < bm.width(); x++) { |
| 22 | p[x] = ((x + y) & 1) ? SK_ColorWHITE : SK_ColorBLACK; |
| 23 | } |
| 24 | } |
| 25 | bm.unlockPixels(); |
| 26 | return bm; |
| 27 | } |
| 28 | } // unnamed namespace |
| 29 | |
| 30 | class TextureDomainView : public SampleView { |
| 31 | SkBitmap fBM; |
| 32 | |
| 33 | public: |
| 34 | TextureDomainView(){ |
| 35 | fBM = make_bitmap(); |
| 36 | } |
| 37 | |
| 38 | protected: |
| 39 | // overrides from SkEventSink |
| 40 | virtual bool onQuery(SkEvent* evt) { |
| 41 | if (SampleCode::TitleQ(*evt)) { |
junov@google.com | 1d32978 | 2011-07-28 20:10:09 +0000 | [diff] [blame] | 42 | SampleCode::TitleR(evt, "Texture Domain"); |
junov@google.com | 869d6d9 | 2011-05-17 15:47:10 +0000 | [diff] [blame] | 43 | return true; |
| 44 | } |
| 45 | return this->INHERITED::onQuery(evt); |
| 46 | } |
| 47 | |
| 48 | virtual void onDrawContent(SkCanvas* canvas) { |
| 49 | SkIRect srcRect; |
| 50 | SkRect dstRect; |
| 51 | SkPaint paint; |
| 52 | paint.setFilterBitmap(true); |
twiz@google.com | 76b8274 | 2011-06-02 20:30:02 +0000 | [diff] [blame] | 53 | |
| 54 | // Test that bitmap draws from malloc-backed bitmaps respect |
| 55 | // the constrained texture domain. |
| 56 | srcRect.setXYWH(1, 1, 3, 3); |
| 57 | dstRect.setXYWH(5.0f, 5.0f, 305.0f, 305.0f); |
junov@google.com | 869d6d9 | 2011-05-17 15:47:10 +0000 | [diff] [blame] | 58 | canvas->drawBitmapRect(fBM, &srcRect, dstRect, &paint); |
twiz@google.com | 76b8274 | 2011-06-02 20:30:02 +0000 | [diff] [blame] | 59 | |
| 60 | // Test that bitmap draws across separate devices also respect |
| 61 | // the constrainted texture domain. |
| 62 | // Note: GPU-backed bitmaps follow a different rendering path |
| 63 | // when copying from one GPU device to another. |
| 64 | SkRefPtr<SkDevice> primaryDevice(canvas->getDevice()); |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 65 | SkRefPtr<SkDevice> secondDevice(canvas->createCompatibleDevice( |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 66 | SkBitmap::kARGB_8888_Config, 5, 5, true)); |
twiz@google.com | 76b8274 | 2011-06-02 20:30:02 +0000 | [diff] [blame] | 67 | secondDevice->unref(); |
| 68 | SkCanvas secondCanvas(secondDevice.get()); |
| 69 | |
| 70 | srcRect.setXYWH(1, 1, 3, 3); |
| 71 | dstRect.setXYWH(1.0f, 1.0f, 3.0f, 3.0f); |
| 72 | secondCanvas.drawBitmapRect(fBM, &srcRect, dstRect, &paint); |
| 73 | |
| 74 | SkBitmap deviceBitmap = secondDevice->accessBitmap(false); |
| 75 | |
| 76 | srcRect.setXYWH(1, 1, 3, 3); |
| 77 | dstRect.setXYWH(405.0f, 5.0f, 305.0f, 305.0f); |
| 78 | canvas->drawBitmapRect(deviceBitmap, &srcRect, dstRect, &paint); |
junov@google.com | d935cfb | 2011-06-27 20:48:23 +0000 | [diff] [blame] | 79 | |
| 80 | // Test that bitmap blurring using a subrect |
| 81 | // renders correctly |
| 82 | srcRect.setXYWH(1, 1, 3, 3); |
| 83 | dstRect.setXYWH(5.0f, 405.0f, 305.0f, 305.0f); |
| 84 | SkMaskFilter* mf = SkBlurMaskFilter::Create( |
| 85 | 5, |
| 86 | SkBlurMaskFilter::kNormal_BlurStyle, |
junov@google.com | 1d32978 | 2011-07-28 20:10:09 +0000 | [diff] [blame] | 87 | SkBlurMaskFilter::kHighQuality_BlurFlag | |
| 88 | SkBlurMaskFilter::kIgnoreTransform_BlurFlag); |
junov@google.com | d935cfb | 2011-06-27 20:48:23 +0000 | [diff] [blame] | 89 | paint.setMaskFilter(mf)->unref(); |
| 90 | canvas->drawBitmapRect(deviceBitmap, &srcRect, dstRect, &paint); |
| 91 | |
epoger@google.com | 9ef2d83 | 2011-07-01 21:12:20 +0000 | [diff] [blame] | 92 | // Blur and a rotation + NULL src rect |
junov@google.com | d935cfb | 2011-06-27 20:48:23 +0000 | [diff] [blame] | 93 | // This should not trigger the texture domain code |
| 94 | // but it will test a code path in SkGpuDevice::drawBitmap |
| 95 | // that handles blurs with rects transformed to non- |
| 96 | // orthogonal rects. It also tests the NULL src rect handling |
junov@google.com | 1d32978 | 2011-07-28 20:10:09 +0000 | [diff] [blame] | 97 | mf = SkBlurMaskFilter::Create( |
| 98 | 5, |
| 99 | SkBlurMaskFilter::kNormal_BlurStyle, |
| 100 | SkBlurMaskFilter::kHighQuality_BlurFlag); |
| 101 | paint.setMaskFilter(mf)->unref(); |
| 102 | |
junov@google.com | d935cfb | 2011-06-27 20:48:23 +0000 | [diff] [blame] | 103 | dstRect.setXYWH(-150.0f, -150.0f, 300.0f, 300.0f); |
| 104 | canvas->translate(550, 550); |
| 105 | canvas->rotate(45); |
| 106 | canvas->drawBitmapRect(fBM, NULL, dstRect, &paint); |
junov@google.com | 869d6d9 | 2011-05-17 15:47:10 +0000 | [diff] [blame] | 107 | } |
| 108 | private: |
| 109 | typedef SkView INHERITED; |
| 110 | }; |
| 111 | |
| 112 | ////////////////////////////////////////////////////////////////////////////// |
| 113 | |
| 114 | static SkView* MyFactory() { return new TextureDomainView; } |
| 115 | static SkViewRegister reg(MyFactory); |
| 116 | |