blob: e615235a578334fc039b56e84011fb357dc6bea3 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 */
commit-bot@chromium.orgab131672013-09-13 12:39:55 +00007
junov@google.com869d6d92011-05-17 15:47:10 +00008#include "SampleCode.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +00009#include "SkBlurMask.h"
10#include "SkBlurMaskFilter.h"
junov@google.com869d6d92011-05-17 15:47:10 +000011#include "SkCanvas.h"
reed@google.com76f10a32014-02-05 15:32:21 +000012#include "SkSurface.h"
junov@google.com869d6d92011-05-17 15:47:10 +000013
commit-bot@chromium.orgab131672013-09-13 12:39:55 +000014static SkBitmap make_bitmap() {
junov@google.com869d6d92011-05-17 15:47:10 +000015 SkBitmap bm;
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000016 bm.allocN32Pixels(5, 5);
junov@google.com869d6d92011-05-17 15:47:10 +000017
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 bm.unlockPixels();
25 return bm;
26}
junov@google.com869d6d92011-05-17 15:47:10 +000027
28class TextureDomainView : public SampleView {
29 SkBitmap fBM;
30
31public:
32 TextureDomainView(){
33 fBM = make_bitmap();
34 }
35
36protected:
37 // overrides from SkEventSink
38 virtual bool onQuery(SkEvent* evt) {
39 if (SampleCode::TitleQ(*evt)) {
junov@google.com1d329782011-07-28 20:10:09 +000040 SampleCode::TitleR(evt, "Texture Domain");
junov@google.com869d6d92011-05-17 15:47:10 +000041 return true;
42 }
43 return this->INHERITED::onQuery(evt);
44 }
45
46 virtual void onDrawContent(SkCanvas* canvas) {
reed@google.com76f10a32014-02-05 15:32:21 +000047 SkRect srcRect;
junov@google.com869d6d92011-05-17 15:47:10 +000048 SkRect dstRect;
49 SkPaint paint;
reed93a12152015-03-16 10:08:34 -070050 paint.setFilterQuality(kLow_SkFilterQuality);
twiz@google.com76b82742011-06-02 20:30:02 +000051
52 // Test that bitmap draws from malloc-backed bitmaps respect
53 // the constrained texture domain.
54 srcRect.setXYWH(1, 1, 3, 3);
reed@google.com76f10a32014-02-05 15:32:21 +000055 dstRect.setXYWH(5, 5, 305, 305);
reede47829b2015-08-06 10:02:53 -070056 canvas->drawBitmapRect(fBM, srcRect, dstRect, &paint, SkCanvas::kStrict_SrcRectConstraint);
twiz@google.com76b82742011-06-02 20:30:02 +000057
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.
reed@google.com76f10a32014-02-05 15:32:21 +000062 SkImageInfo info = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType);
reede8f30622016-03-23 18:59:25 -070063 auto surface(canvas->makeSurface(info));
twiz@google.com76b82742011-06-02 20:30:02 +000064
65 srcRect.setXYWH(1, 1, 3, 3);
reed@google.com76f10a32014-02-05 15:32:21 +000066 dstRect.setXYWH(1, 1, 3, 3);
reede47829b2015-08-06 10:02:53 -070067 surface->getCanvas()->drawBitmapRect(fBM, srcRect, dstRect, &paint,
reed84984ef2015-07-17 07:09:43 -070068 SkCanvas::kStrict_SrcRectConstraint);
twiz@google.com76b82742011-06-02 20:30:02 +000069
reed9ce9d672016-03-17 10:51:11 -070070 sk_sp<SkImage> image(surface->makeImageSnapshot());
twiz@google.com76b82742011-06-02 20:30:02 +000071
72 srcRect.setXYWH(1, 1, 3, 3);
reed@google.com76f10a32014-02-05 15:32:21 +000073 dstRect.setXYWH(405, 5, 305, 305);
reede47829b2015-08-06 10:02:53 -070074 canvas->drawImageRect(image, srcRect, dstRect, &paint);
junov@google.comd935cfb2011-06-27 20:48:23 +000075
76 // Test that bitmap blurring using a subrect
rmistry@google.comae933ce2012-08-23 18:19:56 +000077 // renders correctly
junov@google.comd935cfb2011-06-27 20:48:23 +000078 srcRect.setXYWH(1, 1, 3, 3);
reed@google.com76f10a32014-02-05 15:32:21 +000079 dstRect.setXYWH(5, 405, 305, 305);
reedefdfd512016-04-04 10:02:58 -070080 paint.setMaskFilter(SkBlurMaskFilter::Make(
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000081 kNormal_SkBlurStyle,
robertphillips@google.comb7061172013-09-06 14:16:12 +000082 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)),
junov@google.com1d329782011-07-28 20:10:09 +000083 SkBlurMaskFilter::kHighQuality_BlurFlag |
reedefdfd512016-04-04 10:02:58 -070084 SkBlurMaskFilter::kIgnoreTransform_BlurFlag));
reede47829b2015-08-06 10:02:53 -070085 canvas->drawImageRect(image, srcRect, dstRect, &paint);
junov@google.comd935cfb2011-06-27 20:48:23 +000086
halcanary96fcdcc2015-08-27 07:41:13 -070087 // Blur and a rotation + nullptr src rect
junov@google.comd935cfb2011-06-27 20:48:23 +000088 // This should not trigger the texture domain code
89 // but it will test a code path in SkGpuDevice::drawBitmap
90 // that handles blurs with rects transformed to non-
halcanary96fcdcc2015-08-27 07:41:13 -070091 // orthogonal rects. It also tests the nullptr src rect handling
reedefdfd512016-04-04 10:02:58 -070092 paint.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle,
93 SkBlurMask::ConvertRadiusToSigma(5),
94 SkBlurMaskFilter::kHighQuality_BlurFlag));
junov@google.com1d329782011-07-28 20:10:09 +000095
reed@google.com76f10a32014-02-05 15:32:21 +000096 dstRect.setXYWH(-150, -150, 300, 300);
junov@google.comd935cfb2011-06-27 20:48:23 +000097 canvas->translate(550, 550);
98 canvas->rotate(45);
reed84984ef2015-07-17 07:09:43 -070099 canvas->drawBitmapRect(fBM, dstRect, &paint);
junov@google.com869d6d92011-05-17 15:47:10 +0000100 }
101private:
102 typedef SkView INHERITED;
103};
104
105//////////////////////////////////////////////////////////////////////////////
106
107static SkView* MyFactory() { return new TextureDomainView; }
108static SkViewRegister reg(MyFactory);