blob: 9c7f3de7a39457de960e7a4c42c30365fd59a2cd [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 }
junov@google.com869d6d92011-05-17 15:47:10 +000024 return bm;
25}
junov@google.com869d6d92011-05-17 15:47:10 +000026
27class TextureDomainView : public SampleView {
28 SkBitmap fBM;
29
30public:
31 TextureDomainView(){
32 fBM = make_bitmap();
33 }
34
35protected:
36 // overrides from SkEventSink
37 virtual bool onQuery(SkEvent* evt) {
38 if (SampleCode::TitleQ(*evt)) {
junov@google.com1d329782011-07-28 20:10:09 +000039 SampleCode::TitleR(evt, "Texture Domain");
junov@google.com869d6d92011-05-17 15:47:10 +000040 return true;
41 }
42 return this->INHERITED::onQuery(evt);
43 }
44
45 virtual void onDrawContent(SkCanvas* canvas) {
reed@google.com76f10a32014-02-05 15:32:21 +000046 SkRect srcRect;
junov@google.com869d6d92011-05-17 15:47:10 +000047 SkRect dstRect;
48 SkPaint paint;
reed93a12152015-03-16 10:08:34 -070049 paint.setFilterQuality(kLow_SkFilterQuality);
twiz@google.com76b82742011-06-02 20:30:02 +000050
51 // Test that bitmap draws from malloc-backed bitmaps respect
52 // the constrained texture domain.
53 srcRect.setXYWH(1, 1, 3, 3);
reed@google.com76f10a32014-02-05 15:32:21 +000054 dstRect.setXYWH(5, 5, 305, 305);
reede47829b2015-08-06 10:02:53 -070055 canvas->drawBitmapRect(fBM, srcRect, dstRect, &paint, SkCanvas::kStrict_SrcRectConstraint);
twiz@google.com76b82742011-06-02 20:30:02 +000056
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.
reed@google.com76f10a32014-02-05 15:32:21 +000061 SkImageInfo info = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType);
reede8f30622016-03-23 18:59:25 -070062 auto surface(canvas->makeSurface(info));
twiz@google.com76b82742011-06-02 20:30:02 +000063
64 srcRect.setXYWH(1, 1, 3, 3);
reed@google.com76f10a32014-02-05 15:32:21 +000065 dstRect.setXYWH(1, 1, 3, 3);
reede47829b2015-08-06 10:02:53 -070066 surface->getCanvas()->drawBitmapRect(fBM, srcRect, dstRect, &paint,
reed84984ef2015-07-17 07:09:43 -070067 SkCanvas::kStrict_SrcRectConstraint);
twiz@google.com76b82742011-06-02 20:30:02 +000068
reed9ce9d672016-03-17 10:51:11 -070069 sk_sp<SkImage> image(surface->makeImageSnapshot());
twiz@google.com76b82742011-06-02 20:30:02 +000070
71 srcRect.setXYWH(1, 1, 3, 3);
reed@google.com76f10a32014-02-05 15:32:21 +000072 dstRect.setXYWH(405, 5, 305, 305);
reede47829b2015-08-06 10:02:53 -070073 canvas->drawImageRect(image, srcRect, dstRect, &paint);
junov@google.comd935cfb2011-06-27 20:48:23 +000074
75 // Test that bitmap blurring using a subrect
rmistry@google.comae933ce2012-08-23 18:19:56 +000076 // renders correctly
junov@google.comd935cfb2011-06-27 20:48:23 +000077 srcRect.setXYWH(1, 1, 3, 3);
reed@google.com76f10a32014-02-05 15:32:21 +000078 dstRect.setXYWH(5, 405, 305, 305);
reedefdfd512016-04-04 10:02:58 -070079 paint.setMaskFilter(SkBlurMaskFilter::Make(
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000080 kNormal_SkBlurStyle,
robertphillips@google.comb7061172013-09-06 14:16:12 +000081 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)),
junov@google.com1d329782011-07-28 20:10:09 +000082 SkBlurMaskFilter::kHighQuality_BlurFlag |
reedefdfd512016-04-04 10:02:58 -070083 SkBlurMaskFilter::kIgnoreTransform_BlurFlag));
reede47829b2015-08-06 10:02:53 -070084 canvas->drawImageRect(image, srcRect, dstRect, &paint);
junov@google.comd935cfb2011-06-27 20:48:23 +000085
halcanary96fcdcc2015-08-27 07:41:13 -070086 // Blur and a rotation + nullptr src rect
junov@google.comd935cfb2011-06-27 20:48:23 +000087 // 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-
halcanary96fcdcc2015-08-27 07:41:13 -070090 // orthogonal rects. It also tests the nullptr src rect handling
reedefdfd512016-04-04 10:02:58 -070091 paint.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle,
92 SkBlurMask::ConvertRadiusToSigma(5),
93 SkBlurMaskFilter::kHighQuality_BlurFlag));
junov@google.com1d329782011-07-28 20:10:09 +000094
reed@google.com76f10a32014-02-05 15:32:21 +000095 dstRect.setXYWH(-150, -150, 300, 300);
junov@google.comd935cfb2011-06-27 20:48:23 +000096 canvas->translate(550, 550);
97 canvas->rotate(45);
reed84984ef2015-07-17 07:09:43 -070098 canvas->drawBitmapRect(fBM, dstRect, &paint);
junov@google.com869d6d92011-05-17 15:47:10 +000099 }
100private:
101 typedef SkView INHERITED;
102};
103
104//////////////////////////////////////////////////////////////////////////////
105
106static SkView* MyFactory() { return new TextureDomainView; }
107static SkViewRegister reg(MyFactory);