blob: 1f895e5d91045c37a0766c2a4958f4d6e405292d [file] [log] [blame]
junov@google.com869d6d92011-05-17 15:47:10 +00001#include "SampleCode.h"
2#include "SkCanvas.h"
twiz@google.com76b82742011-06-02 20:30:02 +00003#include "SkDevice.h"
junov@google.comd935cfb2011-06-27 20:48:23 +00004#include "SkBlurMaskFilter.h"
junov@google.com869d6d92011-05-17 15:47:10 +00005
6namespace {
7SkBitmap make_bitmap() {
8 SkBitmap bm;
twiz@google.com76b82742011-06-02 20:30:02 +00009 bm.setConfig(SkBitmap::kARGB_8888_Config , 5, 5);
junov@google.com869d6d92011-05-17 15:47:10 +000010 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
23class TextureDomainView : public SampleView {
24 SkBitmap fBM;
25
26public:
27 TextureDomainView(){
28 fBM = make_bitmap();
29 }
30
31protected:
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.com76b82742011-06-02 20:30:02 +000046
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.com869d6d92011-05-17 15:47:10 +000051 canvas->drawBitmapRect(fBM, &srcRect, dstRect, &paint);
twiz@google.com76b82742011-06-02 20:30:02 +000052
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.come97f0852011-06-17 13:10:25 +000058 SkRefPtr<SkDevice> secondDevice(canvas->createCompatibleDevice(
reed@google.comaf951c92011-06-16 19:10:39 +000059 SkBitmap::kARGB_8888_Config, 5, 5, true));
twiz@google.com76b82742011-06-02 20:30:02 +000060 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.comd935cfb2011-06-27 20:48:23 +000072
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.com9ef2d832011-07-01 21:12:20 +000080 SkBlurMaskFilter::kHighQuality_BlurFlag);
junov@google.comd935cfb2011-06-27 20:48:23 +000081 paint.setMaskFilter(mf)->unref();
82 canvas->drawBitmapRect(deviceBitmap, &srcRect, dstRect, &paint);
83
epoger@google.com9ef2d832011-07-01 21:12:20 +000084 // Blur and a rotation + NULL src rect
junov@google.comd935cfb2011-06-27 20:48:23 +000085 // 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.com869d6d92011-05-17 15:47:10 +000093 }
94private:
95 typedef SkView INHERITED;
96};
97
98//////////////////////////////////////////////////////////////////////////////
99
100static SkView* MyFactory() { return new TextureDomainView; }
101static SkViewRegister reg(MyFactory);
102