blob: 3bac799901dafe97ae662be56e0ce671cd8f125d [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkSurface.h"
10#include "include/effects/SkBlurMaskFilter.h"
11#include "samplecode/Sample.h"
12#include "src/core/SkBlurMask.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
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040027class TextureDomainView : public Sample {
junov@google.com869d6d92011-05-17 15:47:10 +000028 SkBitmap fBM;
29
30public:
31 TextureDomainView(){
32 fBM = make_bitmap();
33 }
34
35protected:
Hal Canary8a027312019-07-03 10:55:44 -040036 virtual SkString name() { return SkString("Texture Domain"); }
junov@google.com869d6d92011-05-17 15:47:10 +000037
38 virtual void onDrawContent(SkCanvas* canvas) {
reed@google.com76f10a32014-02-05 15:32:21 +000039 SkRect srcRect;
junov@google.com869d6d92011-05-17 15:47:10 +000040 SkRect dstRect;
41 SkPaint paint;
reed93a12152015-03-16 10:08:34 -070042 paint.setFilterQuality(kLow_SkFilterQuality);
twiz@google.com76b82742011-06-02 20:30:02 +000043
44 // Test that bitmap draws from malloc-backed bitmaps respect
45 // the constrained texture domain.
46 srcRect.setXYWH(1, 1, 3, 3);
reed@google.com76f10a32014-02-05 15:32:21 +000047 dstRect.setXYWH(5, 5, 305, 305);
reede47829b2015-08-06 10:02:53 -070048 canvas->drawBitmapRect(fBM, srcRect, dstRect, &paint, SkCanvas::kStrict_SrcRectConstraint);
twiz@google.com76b82742011-06-02 20:30:02 +000049
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.
reed@google.com76f10a32014-02-05 15:32:21 +000054 SkImageInfo info = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType);
reede8f30622016-03-23 18:59:25 -070055 auto surface(canvas->makeSurface(info));
twiz@google.com76b82742011-06-02 20:30:02 +000056
57 srcRect.setXYWH(1, 1, 3, 3);
reed@google.com76f10a32014-02-05 15:32:21 +000058 dstRect.setXYWH(1, 1, 3, 3);
reede47829b2015-08-06 10:02:53 -070059 surface->getCanvas()->drawBitmapRect(fBM, srcRect, dstRect, &paint,
reed84984ef2015-07-17 07:09:43 -070060 SkCanvas::kStrict_SrcRectConstraint);
twiz@google.com76b82742011-06-02 20:30:02 +000061
reed9ce9d672016-03-17 10:51:11 -070062 sk_sp<SkImage> image(surface->makeImageSnapshot());
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(405, 5, 305, 305);
reede47829b2015-08-06 10:02:53 -070066 canvas->drawImageRect(image, srcRect, dstRect, &paint);
junov@google.comd935cfb2011-06-27 20:48:23 +000067
68 // Test that bitmap blurring using a subrect
rmistry@google.comae933ce2012-08-23 18:19:56 +000069 // renders correctly
junov@google.comd935cfb2011-06-27 20:48:23 +000070 srcRect.setXYWH(1, 1, 3, 3);
reed@google.com76f10a32014-02-05 15:32:21 +000071 dstRect.setXYWH(5, 405, 305, 305);
Mike Reed1be1f8d2018-03-14 13:01:17 -040072 paint.setMaskFilter(SkMaskFilter::MakeBlur(
73 kNormal_SkBlurStyle, SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)), false));
reede47829b2015-08-06 10:02:53 -070074 canvas->drawImageRect(image, srcRect, dstRect, &paint);
junov@google.comd935cfb2011-06-27 20:48:23 +000075
halcanary96fcdcc2015-08-27 07:41:13 -070076 // Blur and a rotation + nullptr src rect
junov@google.comd935cfb2011-06-27 20:48:23 +000077 // 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-
halcanary96fcdcc2015-08-27 07:41:13 -070080 // orthogonal rects. It also tests the nullptr src rect handling
Mike Reed1be1f8d2018-03-14 13:01:17 -040081 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle,
82 SkBlurMask::ConvertRadiusToSigma(5)));
junov@google.com1d329782011-07-28 20:10:09 +000083
reed@google.com76f10a32014-02-05 15:32:21 +000084 dstRect.setXYWH(-150, -150, 300, 300);
junov@google.comd935cfb2011-06-27 20:48:23 +000085 canvas->translate(550, 550);
86 canvas->rotate(45);
reed84984ef2015-07-17 07:09:43 -070087 canvas->drawBitmapRect(fBM, dstRect, &paint);
junov@google.com869d6d92011-05-17 15:47:10 +000088 }
89private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040090 typedef Sample INHERITED;
junov@google.com869d6d92011-05-17 15:47:10 +000091};
92
93//////////////////////////////////////////////////////////////////////////////
94
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040095DEF_SAMPLE( return new TextureDomainView(); )