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