bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 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 | */ |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 7 | |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 8 | #include "gm.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 9 | #include "SkBlurMask.h" |
bsalomon@google.com | 7d30a21 | 2012-04-25 15:52:27 +0000 | [diff] [blame] | 10 | #include "SkBlurMaskFilter.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 11 | #include "SkColorPriv.h" |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 12 | #include "SkGradientShader.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 13 | #include "SkShader.h" |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 14 | #include "SkImage.h" |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 15 | |
bsalomon@google.com | 7d30a21 | 2012-04-25 15:52:27 +0000 | [diff] [blame] | 16 | static SkBitmap make_chessbm(int w, int h) { |
| 17 | SkBitmap bm; |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 18 | bm.allocN32Pixels(w, h); |
bsalomon@google.com | 7d30a21 | 2012-04-25 15:52:27 +0000 | [diff] [blame] | 19 | |
| 20 | for (int y = 0; y < bm.height(); y++) { |
| 21 | uint32_t* p = bm.getAddr32(0, y); |
| 22 | for (int x = 0; x < bm.width(); x++) { |
| 23 | p[x] = ((x + y) & 1) ? SK_ColorWHITE : SK_ColorBLACK; |
| 24 | } |
| 25 | } |
| 26 | bm.unlockPixels(); |
| 27 | return bm; |
| 28 | } |
| 29 | |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 30 | static SkImage* image_from_bitmap(const SkBitmap& bm) { |
| 31 | SkBitmap b(bm); |
| 32 | b.lockPixels(); |
| 33 | return SkImage::NewRasterCopy(b.info(), b.getPixels(), b.rowBytes()); |
| 34 | } |
| 35 | |
| 36 | static SkImage* makebm(SkBitmap* bm, int w, int h) { |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 37 | bm->allocN32Pixels(w, h); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 38 | bm->eraseColor(SK_ColorTRANSPARENT); |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 39 | |
| 40 | SkCanvas canvas(*bm); |
| 41 | |
| 42 | SkScalar wScalar = SkIntToScalar(w); |
| 43 | SkScalar hScalar = SkIntToScalar(h); |
| 44 | |
| 45 | SkPoint pt = { wScalar / 2, hScalar / 2 }; |
| 46 | |
| 47 | SkScalar radius = 4 * SkMaxScalar(wScalar, hScalar); |
| 48 | |
| 49 | SkColor colors[] = { SK_ColorRED, SK_ColorYELLOW, |
| 50 | SK_ColorGREEN, SK_ColorMAGENTA, |
| 51 | SK_ColorBLUE, SK_ColorCYAN, |
| 52 | SK_ColorRED}; |
| 53 | |
| 54 | SkScalar pos[] = {0, |
| 55 | SK_Scalar1 / 6, |
| 56 | 2 * SK_Scalar1 / 6, |
| 57 | 3 * SK_Scalar1 / 6, |
| 58 | 4 * SK_Scalar1 / 6, |
| 59 | 5 * SK_Scalar1 / 6, |
| 60 | SK_Scalar1}; |
| 61 | |
| 62 | SkPaint paint; |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 63 | SkRect rect = SkRect::MakeWH(wScalar, hScalar); |
| 64 | SkMatrix mat = SkMatrix::I(); |
| 65 | for (int i = 0; i < 4; ++i) { |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 66 | paint.setShader(SkGradientShader::CreateRadial( |
| 67 | pt, radius, |
| 68 | colors, pos, |
| 69 | SK_ARRAY_COUNT(colors), |
| 70 | SkShader::kRepeat_TileMode, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 71 | 0, &mat))->unref(); |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 72 | canvas.drawRect(rect, paint); |
| 73 | rect.inset(wScalar / 8, hScalar / 8); |
| 74 | mat.postScale(SK_Scalar1 / 4, SK_Scalar1 / 4); |
| 75 | } |
commit-bot@chromium.org | 5d41507 | 2014-05-02 14:22:38 +0000 | [diff] [blame] | 76 | // Let backends know we won't change this, so they don't have to deep copy it defensively. |
| 77 | bm->setImmutable(); |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 78 | |
| 79 | return image_from_bitmap(*bm); |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 80 | } |
| 81 | |
reed | 84984ef | 2015-07-17 07:09:43 -0700 | [diff] [blame] | 82 | static void canvasproc(SkCanvas* canvas, SkImage*, const SkBitmap& bm, const SkIRect& srcR, |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 83 | const SkRect& dstR) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 84 | canvas->drawBitmapRect(bm, srcR, dstR, nullptr); |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 85 | } |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 86 | |
reed | 84984ef | 2015-07-17 07:09:43 -0700 | [diff] [blame] | 87 | static void imageproc(SkCanvas* canvas, SkImage* image, const SkBitmap&, const SkIRect& srcR, |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 88 | const SkRect& dstR) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 89 | canvas->drawImageRect(image, srcR, dstR, nullptr); |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 90 | } |
| 91 | |
reed | 84984ef | 2015-07-17 07:09:43 -0700 | [diff] [blame] | 92 | typedef void DrawRectRectProc(SkCanvas*, SkImage*, const SkBitmap&, const SkIRect&, const SkRect&); |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 93 | |
| 94 | static const int gSize = 1024; |
| 95 | static const int gBmpSize = 2048; |
| 96 | |
| 97 | class DrawBitmapRectGM : public skiagm::GM { |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 98 | public: |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 99 | DrawBitmapRectGM(DrawRectRectProc proc, const char suffix[]) : fProc(proc) { |
| 100 | fName.set("drawbitmaprect"); |
| 101 | if (suffix) { |
| 102 | fName.append(suffix); |
| 103 | } |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 104 | } |
| 105 | |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 106 | DrawRectRectProc* fProc; |
| 107 | SkBitmap fLargeBitmap; |
| 108 | SkAutoTUnref<SkImage> fImage; |
| 109 | SkString fName; |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 110 | |
| 111 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 112 | SkString onShortName() override { return fName; } |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 113 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 114 | SkISize onISize() override { return SkISize::Make(gSize, gSize); } |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 115 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 116 | void onOnceBeforeDraw() override { |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 117 | fImage.reset(makebm(&fLargeBitmap, gBmpSize, gBmpSize)); |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 118 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 119 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 120 | void onDraw(SkCanvas* canvas) override { |
vandebo@chromium.org | 663515b | 2012-01-05 18:45:27 +0000 | [diff] [blame] | 121 | SkRect dstRect = { 0, 0, SkIntToScalar(64), SkIntToScalar(64)}; |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 122 | static const int kMaxSrcRectSize = 1 << (SkNextLog2(gBmpSize) + 2); |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 123 | |
| 124 | static const int kPadX = 30; |
| 125 | static const int kPadY = 40; |
| 126 | SkPaint paint; |
| 127 | paint.setAlpha(0x20); |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 128 | canvas->drawBitmapRect(fLargeBitmap, SkRect::MakeIWH(gSize, gSize), &paint); |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 129 | canvas->translate(SK_Scalar1 * kPadX / 2, |
| 130 | SK_Scalar1 * kPadY / 2); |
| 131 | SkPaint blackPaint; |
| 132 | SkScalar titleHeight = SK_Scalar1 * 24; |
| 133 | blackPaint.setColor(SK_ColorBLACK); |
| 134 | blackPaint.setTextSize(titleHeight); |
| 135 | blackPaint.setAntiAlias(true); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 136 | sk_tool_utils::set_portable_typeface(&blackPaint); |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 137 | SkString title; |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 138 | title.printf("Bitmap size: %d x %d", gBmpSize, gBmpSize); |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 139 | canvas->drawText(title.c_str(), title.size(), 0, |
| 140 | titleHeight, blackPaint); |
| 141 | |
| 142 | canvas->translate(0, SK_Scalar1 * kPadY / 2 + titleHeight); |
| 143 | int rowCount = 0; |
| 144 | canvas->save(); |
| 145 | for (int w = 1; w <= kMaxSrcRectSize; w *= 4) { |
| 146 | for (int h = 1; h <= kMaxSrcRectSize; h *= 4) { |
| 147 | |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 148 | SkIRect srcRect = SkIRect::MakeXYWH((gBmpSize - w) / 2, (gBmpSize - h) / 2, w, h); |
reed | 84984ef | 2015-07-17 07:09:43 -0700 | [diff] [blame] | 149 | fProc(canvas, fImage, fLargeBitmap, srcRect, dstRect); |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 150 | |
| 151 | SkString label; |
| 152 | label.appendf("%d x %d", w, h); |
| 153 | blackPaint.setAntiAlias(true); |
| 154 | blackPaint.setStyle(SkPaint::kFill_Style); |
| 155 | blackPaint.setTextSize(SK_Scalar1 * 10); |
| 156 | SkScalar baseline = dstRect.height() + |
| 157 | blackPaint.getTextSize() + SK_Scalar1 * 3; |
| 158 | canvas->drawText(label.c_str(), label.size(), |
| 159 | 0, baseline, |
| 160 | blackPaint); |
| 161 | blackPaint.setStyle(SkPaint::kStroke_Style); |
| 162 | blackPaint.setStrokeWidth(SK_Scalar1); |
| 163 | blackPaint.setAntiAlias(false); |
| 164 | canvas->drawRect(dstRect, blackPaint); |
| 165 | |
| 166 | canvas->translate(dstRect.width() + SK_Scalar1 * kPadX, 0); |
| 167 | ++rowCount; |
| 168 | if ((dstRect.width() + kPadX) * rowCount > gSize) { |
| 169 | canvas->restore(); |
| 170 | canvas->translate(0, dstRect.height() + SK_Scalar1 * kPadY); |
| 171 | canvas->save(); |
| 172 | rowCount = 0; |
| 173 | } |
| 174 | } |
| 175 | } |
bsalomon@google.com | 7d30a21 | 2012-04-25 15:52:27 +0000 | [diff] [blame] | 176 | |
| 177 | { |
| 178 | // test the following code path: |
| 179 | // SkGpuDevice::drawPath() -> SkGpuDevice::drawWithMaskFilter() |
| 180 | SkIRect srcRect; |
| 181 | SkPaint paint; |
| 182 | SkBitmap bm; |
| 183 | |
| 184 | bm = make_chessbm(5, 5); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 185 | paint.setFilterQuality(kLow_SkFilterQuality); |
bsalomon@google.com | 7d30a21 | 2012-04-25 15:52:27 +0000 | [diff] [blame] | 186 | |
| 187 | srcRect.setXYWH(1, 1, 3, 3); |
| 188 | SkMaskFilter* mf = SkBlurMaskFilter::Create( |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 189 | kNormal_SkBlurStyle, |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 190 | SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)), |
bsalomon@google.com | 7d30a21 | 2012-04-25 15:52:27 +0000 | [diff] [blame] | 191 | SkBlurMaskFilter::kHighQuality_BlurFlag | |
| 192 | SkBlurMaskFilter::kIgnoreTransform_BlurFlag); |
| 193 | paint.setMaskFilter(mf)->unref(); |
reed | 84984ef | 2015-07-17 07:09:43 -0700 | [diff] [blame] | 194 | canvas->drawBitmapRect(bm, srcRect, dstRect, &paint); |
bsalomon@google.com | 7d30a21 | 2012-04-25 15:52:27 +0000 | [diff] [blame] | 195 | } |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | private: |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 199 | typedef skiagm::GM INHERITED; |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 200 | }; |
| 201 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 202 | DEF_GM( return new DrawBitmapRectGM(canvasproc, nullptr); ) |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 203 | DEF_GM( return new DrawBitmapRectGM(imageproc, "-imagerect"); ) |