SampleTextureDomain.cpp revision ab13167410206c5371508101949213d46e8dded9
1/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SampleCode.h"
9#include "SkBlurMask.h"
10#include "SkBlurMaskFilter.h"
11#include "SkCanvas.h"
12#include "SkDevice.h"
13
14static SkBitmap make_bitmap() {
15    SkBitmap bm;
16    bm.setConfig(SkBitmap::kARGB_8888_Config , 5, 5);
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
29class TextureDomainView : public SampleView {
30    SkBitmap    fBM;
31
32public:
33    TextureDomainView(){
34        fBM = make_bitmap();
35    }
36
37protected:
38    // overrides from SkEventSink
39    virtual bool onQuery(SkEvent* evt) {
40        if (SampleCode::TitleQ(*evt)) {
41            SampleCode::TitleR(evt, "Texture Domain");
42            return true;
43        }
44        return this->INHERITED::onQuery(evt);
45    }
46
47    virtual void onDrawContent(SkCanvas* canvas) {
48        SkIRect srcRect;
49        SkRect dstRect;
50        SkPaint paint;
51        paint.setFilterBitmap(true);
52
53        // Test that bitmap draws from malloc-backed bitmaps respect
54        // the constrained texture domain.
55        srcRect.setXYWH(1, 1, 3, 3);
56        dstRect.setXYWH(5.0f, 5.0f, 305.0f, 305.0f);
57        canvas->drawBitmapRect(fBM, &srcRect, dstRect, &paint);
58
59        // Test that bitmap draws across separate devices also respect
60        // the constrainted texture domain.
61        // Note:  GPU-backed bitmaps follow a different rendering path
62        // when copying from one GPU device to another.
63        SkAutoTUnref<SkBaseDevice> secondDevice(canvas->createCompatibleDevice(
64                SkBitmap::kARGB_8888_Config, 5, 5, true));
65        SkCanvas secondCanvas(secondDevice.get());
66
67        srcRect.setXYWH(1, 1, 3, 3);
68        dstRect.setXYWH(1.0f, 1.0f, 3.0f, 3.0f);
69        secondCanvas.drawBitmapRect(fBM, &srcRect, dstRect, &paint);
70
71        SkBitmap deviceBitmap = secondDevice->accessBitmap(false);
72
73        srcRect.setXYWH(1, 1, 3, 3);
74        dstRect.setXYWH(405.0f, 5.0f, 305.0f, 305.0f);
75        canvas->drawBitmapRect(deviceBitmap, &srcRect, dstRect, &paint);
76
77        // Test that bitmap blurring using a subrect
78        // renders correctly
79        srcRect.setXYWH(1, 1, 3, 3);
80        dstRect.setXYWH(5.0f, 405.0f, 305.0f, 305.0f);
81        SkMaskFilter* mf = SkBlurMaskFilter::Create(
82            SkBlurMaskFilter::kNormal_BlurStyle,
83            SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)),
84            SkBlurMaskFilter::kHighQuality_BlurFlag |
85            SkBlurMaskFilter::kIgnoreTransform_BlurFlag);
86        paint.setMaskFilter(mf)->unref();
87        canvas->drawBitmapRect(deviceBitmap, &srcRect, dstRect, &paint);
88
89        // Blur and a rotation + NULL src rect
90        // This should not trigger the texture domain code
91        // but it will test a code path in SkGpuDevice::drawBitmap
92        // that handles blurs with rects transformed to non-
93        // orthogonal rects. It also tests the NULL src rect handling
94    mf = SkBlurMaskFilter::Create(
95            SkBlurMaskFilter::kNormal_BlurStyle,
96            SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)),
97            SkBlurMaskFilter::kHighQuality_BlurFlag);
98        paint.setMaskFilter(mf)->unref();
99
100        dstRect.setXYWH(-150.0f, -150.0f, 300.0f, 300.0f);
101        canvas->translate(550, 550);
102        canvas->rotate(45);
103        canvas->drawBitmapRect(fBM, NULL, dstRect, &paint);
104    }
105private:
106    typedef SkView INHERITED;
107};
108
109//////////////////////////////////////////////////////////////////////////////
110
111static SkView* MyFactory() { return new TextureDomainView; }
112static SkViewRegister reg(MyFactory);
113