15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Copyright 2011 Google Inc.
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Use of this source code is governed by a BSD-style license that can be
5d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) * found in the LICENSE file.
6d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) */
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "SampleCode.h"
9d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "SkBlurMask.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "SkBlurMaskFilter.h"
11d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "SkCanvas.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "SkSurface.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static SkBitmap make_bitmap() {
15d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    SkBitmap bm;
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bm.allocN32Pixels(5, 5);
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    for (int y = 0; y < bm.height(); y++) {
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        uint32_t* p = bm.getAddr32(0, y);
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        for (int x = 0; x < bm.width(); x++) {
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            p[x] = ((x + y) & 1) ? SK_ColorWHITE : SK_ColorBLACK;
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        }
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bm.unlockPixels();
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return bm;
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class TextureDomainView : public SampleView {
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SkBitmap    fBM;
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)public:
32d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    TextureDomainView(){
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        fBM = make_bitmap();
34d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    }
35
36protected:
37    // overrides from SkEventSink
38    virtual bool onQuery(SkEvent* evt) {
39        if (SampleCode::TitleQ(*evt)) {
40            SampleCode::TitleR(evt, "Texture Domain");
41            return true;
42        }
43        return this->INHERITED::onQuery(evt);
44    }
45
46    virtual void onDrawContent(SkCanvas* canvas) {
47        SkRect srcRect;
48        SkRect dstRect;
49        SkPaint paint;
50        paint.setFilterLevel(SkPaint::kLow_FilterLevel);
51
52        // Test that bitmap draws from malloc-backed bitmaps respect
53        // the constrained texture domain.
54        srcRect.setXYWH(1, 1, 3, 3);
55        dstRect.setXYWH(5, 5, 305, 305);
56        canvas->drawBitmapRectToRect(fBM, &srcRect, dstRect, &paint);
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.
62        SkImageInfo info = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType);
63        SkAutoTUnref<SkSurface> surface(canvas->newSurface(info));
64
65        srcRect.setXYWH(1, 1, 3, 3);
66        dstRect.setXYWH(1, 1, 3, 3);
67        surface->getCanvas()->drawBitmapRectToRect(fBM, &srcRect, dstRect,
68                                                   &paint);
69
70        SkAutoTUnref<SkImage> image(surface->newImageSnapshot());
71
72        srcRect.setXYWH(1, 1, 3, 3);
73        dstRect.setXYWH(405, 5, 305, 305);
74        canvas->drawImageRect(image, &srcRect, dstRect, &paint);
75
76        // Test that bitmap blurring using a subrect
77        // renders correctly
78        srcRect.setXYWH(1, 1, 3, 3);
79        dstRect.setXYWH(5, 405, 305, 305);
80        SkMaskFilter* mf = SkBlurMaskFilter::Create(
81            kNormal_SkBlurStyle,
82            SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)),
83            SkBlurMaskFilter::kHighQuality_BlurFlag |
84            SkBlurMaskFilter::kIgnoreTransform_BlurFlag);
85        paint.setMaskFilter(mf)->unref();
86        canvas->drawImageRect(image, &srcRect, dstRect, &paint);
87
88        // Blur and a rotation + NULL src rect
89        // This should not trigger the texture domain code
90        // but it will test a code path in SkGpuDevice::drawBitmap
91        // that handles blurs with rects transformed to non-
92        // orthogonal rects. It also tests the NULL src rect handling
93        mf = SkBlurMaskFilter::Create(kNormal_SkBlurStyle,
94                                      SkBlurMask::ConvertRadiusToSigma(5),
95                                      SkBlurMaskFilter::kHighQuality_BlurFlag);
96        paint.setMaskFilter(mf)->unref();
97
98        dstRect.setXYWH(-150, -150, 300, 300);
99        canvas->translate(550, 550);
100        canvas->rotate(45);
101        canvas->drawBitmapRectToRect(fBM, NULL, dstRect, &paint);
102    }
103private:
104    typedef SkView INHERITED;
105};
106
107//////////////////////////////////////////////////////////////////////////////
108
109static SkView* MyFactory() { return new TextureDomainView; }
110static SkViewRegister reg(MyFactory);
111