2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "SkBlurMask.h"
10 #include "SkBlurMaskFilter.h"
11 #include "SkColorPriv.h"
12 #include "SkGradientShader.h"
16 static SkBitmap make_chessbm(int w, int h) {
18 bm.allocN32Pixels(w, h);
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;
30 static SkImage* image_from_bitmap(const SkBitmap& bm) {
33 return SkImage::NewRasterCopy(b.info(), b.getPixels(), b.rowBytes());
36 static SkImage* makebm(SkBitmap* bm, int w, int h) {
37 bm->allocN32Pixels(w, h);
38 bm->eraseColor(SK_ColorTRANSPARENT);
42 SkScalar wScalar = SkIntToScalar(w);
43 SkScalar hScalar = SkIntToScalar(h);
45 SkPoint pt = { wScalar / 2, hScalar / 2 };
47 SkScalar radius = 4 * SkMaxScalar(wScalar, hScalar);
49 SkColor colors[] = { SK_ColorRED, SK_ColorYELLOW,
50 SK_ColorGREEN, SK_ColorMAGENTA,
51 SK_ColorBLUE, SK_ColorCYAN,
63 SkRect rect = SkRect::MakeWH(wScalar, hScalar);
64 SkMatrix mat = SkMatrix::I();
65 for (int i = 0; i < 4; ++i) {
66 paint.setShader(SkGradientShader::CreateRadial(
69 SK_ARRAY_COUNT(colors),
70 SkShader::kRepeat_TileMode,
72 canvas.drawRect(rect, paint);
73 rect.inset(wScalar / 8, hScalar / 8);
74 mat.postScale(SK_Scalar1 / 4, SK_Scalar1 / 4);
76 // Let backends know we won't change this, so they don't have to deep copy it defensively.
79 return image_from_bitmap(*bm);
82 static void canvasproc(SkCanvas* canvas, SkImage*, const SkBitmap& bm, const SkIRect* srcR,
84 canvas->drawBitmapRect(bm, srcR, dstR);
87 static void imageproc(SkCanvas* canvas, SkImage* image, const SkBitmap&, const SkIRect* srcIR,
89 SkRect storage, *srcR = NULL;
94 canvas->drawImageRect(image, srcR, dstR);
97 static void imagescaleproc(SkCanvas* canvas, SkImage* image, const SkBitmap&, const SkIRect* srcIR,
99 const int newW = SkScalarRoundToInt(dstR.width());
100 const int newH = SkScalarRoundToInt(dstR.height());
101 SkAutoTUnref<SkImage> newImage(image->newImage(newW, newH, srcIR));
104 const SkIRect baseR = SkIRect::MakeWH(image->width(), image->height());
105 const bool containsSubset = !srcIR || baseR.contains(*srcIR);
109 SkASSERT(containsSubset);
110 canvas->drawImage(newImage, dstR.x(), dstR.y());
112 // newImage() does not support subsets that are not contained by the original
113 // but drawImageRect does, so we just draw an X in its place to indicate that we are
114 // deliberately not drawing here.
115 SkASSERT(!containsSubset);
117 paint.setStyle(SkPaint::kStroke_Style);
118 paint.setStrokeWidth(4);
119 canvas->drawLine(4, 4, newW - 4.0f, newH - 4.0f, paint);
120 canvas->drawLine(4, newH - 4.0f, newW - 4.0f, 4, paint);
124 typedef void DrawRectRectProc(SkCanvas*, SkImage*, const SkBitmap&, const SkIRect*, const SkRect&);
126 static const int gSize = 1024;
127 static const int gBmpSize = 2048;
129 class DrawBitmapRectGM : public skiagm::GM {
131 DrawBitmapRectGM(DrawRectRectProc proc, const char suffix[]) : fProc(proc) {
132 fName.set("drawbitmaprect");
134 fName.append(suffix);
138 DrawRectRectProc* fProc;
139 SkBitmap fLargeBitmap;
140 SkAutoTUnref<SkImage> fImage;
144 SkString onShortName() override { return fName; }
146 SkISize onISize() override { return SkISize::Make(gSize, gSize); }
148 void onOnceBeforeDraw() override {
149 fImage.reset(makebm(&fLargeBitmap, gBmpSize, gBmpSize));
152 void onDraw(SkCanvas* canvas) override {
153 SkRect dstRect = { 0, 0, SkIntToScalar(64), SkIntToScalar(64)};
154 static const int kMaxSrcRectSize = 1 << (SkNextLog2(gBmpSize) + 2);
156 static const int kPadX = 30;
157 static const int kPadY = 40;
159 paint.setAlpha(0x20);
160 canvas->drawBitmapRect(fLargeBitmap, NULL,
161 SkRect::MakeWH(gSize * SK_Scalar1,
164 canvas->translate(SK_Scalar1 * kPadX / 2,
165 SK_Scalar1 * kPadY / 2);
167 SkScalar titleHeight = SK_Scalar1 * 24;
168 blackPaint.setColor(SK_ColorBLACK);
169 blackPaint.setTextSize(titleHeight);
170 blackPaint.setAntiAlias(true);
171 sk_tool_utils::set_portable_typeface(&blackPaint);
173 title.printf("Bitmap size: %d x %d", gBmpSize, gBmpSize);
174 canvas->drawText(title.c_str(), title.size(), 0,
175 titleHeight, blackPaint);
177 canvas->translate(0, SK_Scalar1 * kPadY / 2 + titleHeight);
180 for (int w = 1; w <= kMaxSrcRectSize; w *= 4) {
181 for (int h = 1; h <= kMaxSrcRectSize; h *= 4) {
183 SkIRect srcRect = SkIRect::MakeXYWH((gBmpSize - w) / 2, (gBmpSize - h) / 2, w, h);
184 fProc(canvas, fImage, fLargeBitmap, &srcRect, dstRect);
187 label.appendf("%d x %d", w, h);
188 blackPaint.setAntiAlias(true);
189 blackPaint.setStyle(SkPaint::kFill_Style);
190 blackPaint.setTextSize(SK_Scalar1 * 10);
191 SkScalar baseline = dstRect.height() +
192 blackPaint.getTextSize() + SK_Scalar1 * 3;
193 canvas->drawText(label.c_str(), label.size(),
196 blackPaint.setStyle(SkPaint::kStroke_Style);
197 blackPaint.setStrokeWidth(SK_Scalar1);
198 blackPaint.setAntiAlias(false);
199 canvas->drawRect(dstRect, blackPaint);
201 canvas->translate(dstRect.width() + SK_Scalar1 * kPadX, 0);
203 if ((dstRect.width() + kPadX) * rowCount > gSize) {
205 canvas->translate(0, dstRect.height() + SK_Scalar1 * kPadY);
213 // test the following code path:
214 // SkGpuDevice::drawPath() -> SkGpuDevice::drawWithMaskFilter()
219 bm = make_chessbm(5, 5);
220 paint.setFilterQuality(kLow_SkFilterQuality);
222 srcRect.setXYWH(1, 1, 3, 3);
223 SkMaskFilter* mf = SkBlurMaskFilter::Create(
225 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)),
226 SkBlurMaskFilter::kHighQuality_BlurFlag |
227 SkBlurMaskFilter::kIgnoreTransform_BlurFlag);
228 paint.setMaskFilter(mf)->unref();
229 canvas->drawBitmapRect(bm, &srcRect, dstRect, &paint);
234 typedef skiagm::GM INHERITED;
237 DEF_GM( return new DrawBitmapRectGM(canvasproc, NULL); )
238 DEF_GM( return new DrawBitmapRectGM(imageproc, "-imagerect"); )
239 DEF_GM( return new DrawBitmapRectGM(imagescaleproc, "-imagescale"); )