blob: 3de6cee1b1e1b990ef71febd9363e2868d6d8585 [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
Ben Wagnerb2c4ea62018-08-08 11:36:17 -04008#include "Sample.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
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:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040036 virtual bool onQuery(Sample::Event* evt) {
37 if (Sample::TitleQ(*evt)) {
38 Sample::TitleR(evt, "Texture Domain");
junov@google.com869d6d92011-05-17 15:47:10 +000039 return true;
40 }
41 return this->INHERITED::onQuery(evt);
42 }
43
44 virtual void onDrawContent(SkCanvas* canvas) {
reed@google.com76f10a32014-02-05 15:32:21 +000045 SkRect srcRect;
junov@google.com869d6d92011-05-17 15:47:10 +000046 SkRect dstRect;
47 SkPaint paint;
reed93a12152015-03-16 10:08:34 -070048 paint.setFilterQuality(kLow_SkFilterQuality);
twiz@google.com76b82742011-06-02 20:30:02 +000049
50 // Test that bitmap draws from malloc-backed bitmaps respect
51 // the constrained texture domain.
52 srcRect.setXYWH(1, 1, 3, 3);
reed@google.com76f10a32014-02-05 15:32:21 +000053 dstRect.setXYWH(5, 5, 305, 305);
reede47829b2015-08-06 10:02:53 -070054 canvas->drawBitmapRect(fBM, srcRect, dstRect, &paint, SkCanvas::kStrict_SrcRectConstraint);
twiz@google.com76b82742011-06-02 20:30:02 +000055
56 // Test that bitmap draws across separate devices also respect
57 // the constrainted texture domain.
58 // Note: GPU-backed bitmaps follow a different rendering path
59 // when copying from one GPU device to another.
reed@google.com76f10a32014-02-05 15:32:21 +000060 SkImageInfo info = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType);
reede8f30622016-03-23 18:59:25 -070061 auto surface(canvas->makeSurface(info));
twiz@google.com76b82742011-06-02 20:30:02 +000062
63 srcRect.setXYWH(1, 1, 3, 3);
reed@google.com76f10a32014-02-05 15:32:21 +000064 dstRect.setXYWH(1, 1, 3, 3);
reede47829b2015-08-06 10:02:53 -070065 surface->getCanvas()->drawBitmapRect(fBM, srcRect, dstRect, &paint,
reed84984ef2015-07-17 07:09:43 -070066 SkCanvas::kStrict_SrcRectConstraint);
twiz@google.com76b82742011-06-02 20:30:02 +000067
reed9ce9d672016-03-17 10:51:11 -070068 sk_sp<SkImage> image(surface->makeImageSnapshot());
twiz@google.com76b82742011-06-02 20:30:02 +000069
70 srcRect.setXYWH(1, 1, 3, 3);
reed@google.com76f10a32014-02-05 15:32:21 +000071 dstRect.setXYWH(405, 5, 305, 305);
reede47829b2015-08-06 10:02:53 -070072 canvas->drawImageRect(image, srcRect, dstRect, &paint);
junov@google.comd935cfb2011-06-27 20:48:23 +000073
74 // Test that bitmap blurring using a subrect
rmistry@google.comae933ce2012-08-23 18:19:56 +000075 // renders correctly
junov@google.comd935cfb2011-06-27 20:48:23 +000076 srcRect.setXYWH(1, 1, 3, 3);
reed@google.com76f10a32014-02-05 15:32:21 +000077 dstRect.setXYWH(5, 405, 305, 305);
Mike Reed1be1f8d2018-03-14 13:01:17 -040078 paint.setMaskFilter(SkMaskFilter::MakeBlur(
79 kNormal_SkBlurStyle, SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)), false));
reede47829b2015-08-06 10:02:53 -070080 canvas->drawImageRect(image, srcRect, dstRect, &paint);
junov@google.comd935cfb2011-06-27 20:48:23 +000081
halcanary96fcdcc2015-08-27 07:41:13 -070082 // Blur and a rotation + nullptr src rect
junov@google.comd935cfb2011-06-27 20:48:23 +000083 // This should not trigger the texture domain code
84 // but it will test a code path in SkGpuDevice::drawBitmap
85 // that handles blurs with rects transformed to non-
halcanary96fcdcc2015-08-27 07:41:13 -070086 // orthogonal rects. It also tests the nullptr src rect handling
Mike Reed1be1f8d2018-03-14 13:01:17 -040087 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle,
88 SkBlurMask::ConvertRadiusToSigma(5)));
junov@google.com1d329782011-07-28 20:10:09 +000089
reed@google.com76f10a32014-02-05 15:32:21 +000090 dstRect.setXYWH(-150, -150, 300, 300);
junov@google.comd935cfb2011-06-27 20:48:23 +000091 canvas->translate(550, 550);
92 canvas->rotate(45);
reed84984ef2015-07-17 07:09:43 -070093 canvas->drawBitmapRect(fBM, dstRect, &paint);
junov@google.com869d6d92011-05-17 15:47:10 +000094 }
95private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040096 typedef Sample INHERITED;
junov@google.com869d6d92011-05-17 15:47:10 +000097};
98
99//////////////////////////////////////////////////////////////////////////////
100
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400101DEF_SAMPLE( return new TextureDomainView(); )