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