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 "include/core/SkCanvas.h"
9 #include "include/core/SkMaskFilter.h"
10 #include "include/core/SkSurface.h"
11 #include "samplecode/Sample.h"
12 #include "src/core/SkBlurMask.h"
13 
make_bitmap()14 static SkBitmap make_bitmap() {
15     SkBitmap bm;
16     bm.allocN32Pixels(5, 5);
17 
18     for (int y = 0; y < bm.height(); y++) {
19         uint32_t* p = bm.getAddr32(0, y);
20         for (int x = 0; x < bm.width(); x++) {
21             p[x] = ((x + y) & 1) ? SK_ColorWHITE : SK_ColorBLACK;
22         }
23     }
24     return bm;
25 }
26 
27 class TextureDomainView : public Sample {
28     SkBitmap    fBM;
29 
30 public:
TextureDomainView()31     TextureDomainView(){
32         fBM = make_bitmap();
33     }
34 
35 protected:
name()36     virtual SkString name() { return SkString("Texture Domain"); }
37 
onDrawContent(SkCanvas * canvas)38     virtual void onDrawContent(SkCanvas* canvas) {
39         SkRect srcRect;
40         SkRect dstRect;
41         SkPaint paint;
42         paint.setFilterQuality(kLow_SkFilterQuality);
43 
44         // Test that bitmap draws from malloc-backed bitmaps respect
45         // the constrained texture domain.
46         srcRect.setXYWH(1, 1, 3, 3);
47         dstRect.setXYWH(5, 5, 305, 305);
48         canvas->drawBitmapRect(fBM, srcRect, dstRect, &paint, SkCanvas::kStrict_SrcRectConstraint);
49 
50         // Test that bitmap draws across separate devices also respect
51         // the constrainted texture domain.
52         // Note:  GPU-backed bitmaps follow a different rendering path
53         // when copying from one GPU device to another.
54         SkImageInfo info = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType);
55         auto surface(canvas->makeSurface(info));
56 
57         srcRect.setXYWH(1, 1, 3, 3);
58         dstRect.setXYWH(1, 1, 3, 3);
59         surface->getCanvas()->drawBitmapRect(fBM, srcRect, dstRect, &paint,
60                                              SkCanvas::kStrict_SrcRectConstraint);
61 
62         sk_sp<SkImage> image(surface->makeImageSnapshot());
63 
64         srcRect.setXYWH(1, 1, 3, 3);
65         dstRect.setXYWH(405, 5, 305, 305);
66         canvas->drawImageRect(image, srcRect, dstRect, &paint);
67 
68         // Test that bitmap blurring using a subrect
69         // renders correctly
70         srcRect.setXYWH(1, 1, 3, 3);
71         dstRect.setXYWH(5, 405, 305, 305);
72         paint.setMaskFilter(SkMaskFilter::MakeBlur(
73             kNormal_SkBlurStyle, SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)), false));
74         canvas->drawImageRect(image, srcRect, dstRect, &paint);
75 
76         // Blur and a rotation + nullptr src rect
77         // This should not trigger the texture domain code
78         // but it will test a code path in SkGpuDevice::drawBitmap
79         // that handles blurs with rects transformed to non-
80         // orthogonal rects. It also tests the nullptr src rect handling
81         paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle,
82                                                    SkBlurMask::ConvertRadiusToSigma(5)));
83 
84         dstRect.setXYWH(-150, -150, 300, 300);
85         canvas->translate(550, 550);
86         canvas->rotate(45);
87         canvas->drawBitmapRect(fBM, dstRect, &paint);
88     }
89 private:
90     typedef Sample INHERITED;
91 };
92 
93 //////////////////////////////////////////////////////////////////////////////
94 
95 DEF_SAMPLE( return new TextureDomainView(); )
96