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 "SkSurface.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 SampleView {
28 SkBitmap fBM;
29
30 public:
TextureDomainView()31 TextureDomainView(){
32 fBM = make_bitmap();
33 }
34
35 protected:
36 // overrides from SkEventSink
onQuery(SkEvent * evt)37 virtual bool onQuery(SkEvent* evt) {
38 if (SampleCode::TitleQ(*evt)) {
39 SampleCode::TitleR(evt, "Texture Domain");
40 return true;
41 }
42 return this->INHERITED::onQuery(evt);
43 }
44
onDrawContent(SkCanvas * canvas)45 virtual void onDrawContent(SkCanvas* canvas) {
46 SkRect srcRect;
47 SkRect dstRect;
48 SkPaint paint;
49 paint.setFilterQuality(kLow_SkFilterQuality);
50
51 // Test that bitmap draws from malloc-backed bitmaps respect
52 // the constrained texture domain.
53 srcRect.setXYWH(1, 1, 3, 3);
54 dstRect.setXYWH(5, 5, 305, 305);
55 canvas->drawBitmapRect(fBM, srcRect, dstRect, &paint, SkCanvas::kStrict_SrcRectConstraint);
56
57 // Test that bitmap draws across separate devices also respect
58 // the constrainted texture domain.
59 // Note: GPU-backed bitmaps follow a different rendering path
60 // when copying from one GPU device to another.
61 SkImageInfo info = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType);
62 auto surface(canvas->makeSurface(info));
63
64 srcRect.setXYWH(1, 1, 3, 3);
65 dstRect.setXYWH(1, 1, 3, 3);
66 surface->getCanvas()->drawBitmapRect(fBM, srcRect, dstRect, &paint,
67 SkCanvas::kStrict_SrcRectConstraint);
68
69 sk_sp<SkImage> image(surface->makeImageSnapshot());
70
71 srcRect.setXYWH(1, 1, 3, 3);
72 dstRect.setXYWH(405, 5, 305, 305);
73 canvas->drawImageRect(image, srcRect, dstRect, &paint);
74
75 // Test that bitmap blurring using a subrect
76 // renders correctly
77 srcRect.setXYWH(1, 1, 3, 3);
78 dstRect.setXYWH(5, 405, 305, 305);
79 paint.setMaskFilter(SkBlurMaskFilter::Make(
80 kNormal_SkBlurStyle,
81 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)),
82 SkBlurMaskFilter::kHighQuality_BlurFlag |
83 SkBlurMaskFilter::kIgnoreTransform_BlurFlag));
84 canvas->drawImageRect(image, srcRect, dstRect, &paint);
85
86 // Blur and a rotation + nullptr src rect
87 // This should not trigger the texture domain code
88 // but it will test a code path in SkGpuDevice::drawBitmap
89 // that handles blurs with rects transformed to non-
90 // orthogonal rects. It also tests the nullptr src rect handling
91 paint.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle,
92 SkBlurMask::ConvertRadiusToSigma(5),
93 SkBlurMaskFilter::kHighQuality_BlurFlag));
94
95 dstRect.setXYWH(-150, -150, 300, 300);
96 canvas->translate(550, 550);
97 canvas->rotate(45);
98 canvas->drawBitmapRect(fBM, dstRect, &paint);
99 }
100 private:
101 typedef SkView INHERITED;
102 };
103
104 //////////////////////////////////////////////////////////////////////////////
105
MyFactory()106 static SkView* MyFactory() { return new TextureDomainView; }
107 static SkViewRegister reg(MyFactory);
108