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 | 869d6d9 | 2011-05-17 15:47:10 +0000 | [diff] [blame] | 4 | |
| 5 | namespace { |
| 6 | SkBitmap make_bitmap() { |
| 7 | SkBitmap bm; |
twiz@google.com | 76b8274 | 2011-06-02 20:30:02 +0000 | [diff] [blame] | 8 | bm.setConfig(SkBitmap::kARGB_8888_Config , 5, 5); |
junov@google.com | 869d6d9 | 2011-05-17 15:47:10 +0000 | [diff] [blame] | 9 | bm.allocPixels(); |
| 10 | |
| 11 | for (int y = 0; y < bm.height(); y++) { |
| 12 | uint32_t* p = bm.getAddr32(0, y); |
| 13 | for (int x = 0; x < bm.width(); x++) { |
| 14 | p[x] = ((x + y) & 1) ? SK_ColorWHITE : SK_ColorBLACK; |
| 15 | } |
| 16 | } |
| 17 | bm.unlockPixels(); |
| 18 | return bm; |
| 19 | } |
| 20 | } // unnamed namespace |
| 21 | |
| 22 | class TextureDomainView : public SampleView { |
| 23 | SkBitmap fBM; |
| 24 | |
| 25 | public: |
| 26 | TextureDomainView(){ |
| 27 | fBM = make_bitmap(); |
| 28 | } |
| 29 | |
| 30 | protected: |
| 31 | // overrides from SkEventSink |
| 32 | virtual bool onQuery(SkEvent* evt) { |
| 33 | if (SampleCode::TitleQ(*evt)) { |
| 34 | SampleCode::TitleR(evt, "Texture Domian"); |
| 35 | return true; |
| 36 | } |
| 37 | return this->INHERITED::onQuery(evt); |
| 38 | } |
| 39 | |
| 40 | virtual void onDrawContent(SkCanvas* canvas) { |
| 41 | SkIRect srcRect; |
| 42 | SkRect dstRect; |
| 43 | SkPaint paint; |
| 44 | paint.setFilterBitmap(true); |
twiz@google.com | 76b8274 | 2011-06-02 20:30:02 +0000 | [diff] [blame] | 45 | |
| 46 | // Test that bitmap draws from malloc-backed bitmaps respect |
| 47 | // the constrained texture domain. |
| 48 | srcRect.setXYWH(1, 1, 3, 3); |
| 49 | dstRect.setXYWH(5.0f, 5.0f, 305.0f, 305.0f); |
junov@google.com | 869d6d9 | 2011-05-17 15:47:10 +0000 | [diff] [blame] | 50 | canvas->drawBitmapRect(fBM, &srcRect, dstRect, &paint); |
twiz@google.com | 76b8274 | 2011-06-02 20:30:02 +0000 | [diff] [blame] | 51 | |
| 52 | // Test that bitmap draws across separate devices also respect |
| 53 | // the constrainted texture domain. |
| 54 | // Note: GPU-backed bitmaps follow a different rendering path |
| 55 | // when copying from one GPU device to another. |
| 56 | SkRefPtr<SkDevice> primaryDevice(canvas->getDevice()); |
| 57 | SkRefPtr<SkDevice> secondDevice(canvas->createDevice( |
| 58 | SkBitmap::kARGB_8888_Config, 5, 5, true, true)); |
| 59 | secondDevice->unref(); |
| 60 | SkCanvas secondCanvas(secondDevice.get()); |
| 61 | |
| 62 | srcRect.setXYWH(1, 1, 3, 3); |
| 63 | dstRect.setXYWH(1.0f, 1.0f, 3.0f, 3.0f); |
| 64 | secondCanvas.drawBitmapRect(fBM, &srcRect, dstRect, &paint); |
| 65 | |
| 66 | SkBitmap deviceBitmap = secondDevice->accessBitmap(false); |
| 67 | |
| 68 | srcRect.setXYWH(1, 1, 3, 3); |
| 69 | dstRect.setXYWH(405.0f, 5.0f, 305.0f, 305.0f); |
| 70 | canvas->drawBitmapRect(deviceBitmap, &srcRect, dstRect, &paint); |
junov@google.com | 869d6d9 | 2011-05-17 15:47:10 +0000 | [diff] [blame] | 71 | } |
| 72 | private: |
| 73 | typedef SkView INHERITED; |
| 74 | }; |
| 75 | |
| 76 | ////////////////////////////////////////////////////////////////////////////// |
| 77 | |
| 78 | static SkView* MyFactory() { return new TextureDomainView; } |
| 79 | static SkViewRegister reg(MyFactory); |
| 80 | |