OLD | NEW |
1 | |
2 /* | 1 /* |
3 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
4 * | 3 * |
5 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 5 * found in the LICENSE file. |
7 */ | 6 */ |
| 7 |
8 #include "gm.h" | 8 #include "gm.h" |
9 #include "SkBlurMask.h" | 9 #include "SkBlurMask.h" |
10 #include "SkBlurMaskFilter.h" | 10 #include "SkBlurMaskFilter.h" |
11 #include "SkColorPriv.h" | 11 #include "SkColorPriv.h" |
12 #include "SkGradientShader.h" | 12 #include "SkGradientShader.h" |
13 #include "SkShader.h" | 13 #include "SkShader.h" |
14 | 14 #include "SkImage.h" |
15 namespace skiagm { | |
16 | 15 |
17 static SkBitmap make_chessbm(int w, int h) { | 16 static SkBitmap make_chessbm(int w, int h) { |
18 SkBitmap bm; | 17 SkBitmap bm; |
19 bm.allocN32Pixels(w, h); | 18 bm.allocN32Pixels(w, h); |
20 | 19 |
21 for (int y = 0; y < bm.height(); y++) { | 20 for (int y = 0; y < bm.height(); y++) { |
22 uint32_t* p = bm.getAddr32(0, y); | 21 uint32_t* p = bm.getAddr32(0, y); |
23 for (int x = 0; x < bm.width(); x++) { | 22 for (int x = 0; x < bm.width(); x++) { |
24 p[x] = ((x + y) & 1) ? SK_ColorWHITE : SK_ColorBLACK; | 23 p[x] = ((x + y) & 1) ? SK_ColorWHITE : SK_ColorBLACK; |
25 } | 24 } |
26 } | 25 } |
27 bm.unlockPixels(); | 26 bm.unlockPixels(); |
28 return bm; | 27 return bm; |
29 } | 28 } |
30 | 29 |
31 static void makebm(SkBitmap* bm, int w, int h) { | 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) { |
32 bm->allocN32Pixels(w, h); | 37 bm->allocN32Pixels(w, h); |
33 bm->eraseColor(SK_ColorTRANSPARENT); | 38 bm->eraseColor(SK_ColorTRANSPARENT); |
34 | 39 |
35 SkCanvas canvas(*bm); | 40 SkCanvas canvas(*bm); |
36 | 41 |
37 SkScalar wScalar = SkIntToScalar(w); | 42 SkScalar wScalar = SkIntToScalar(w); |
38 SkScalar hScalar = SkIntToScalar(h); | 43 SkScalar hScalar = SkIntToScalar(h); |
39 | 44 |
40 SkPoint pt = { wScalar / 2, hScalar / 2 }; | 45 SkPoint pt = { wScalar / 2, hScalar / 2 }; |
41 | 46 |
(...skipping 21 matching lines...) Expand all Loading... |
63 colors, pos, | 68 colors, pos, |
64 SK_ARRAY_COUNT(colors), | 69 SK_ARRAY_COUNT(colors), |
65 SkShader::kRepeat_TileMode, | 70 SkShader::kRepeat_TileMode, |
66 0, &mat))->unref(); | 71 0, &mat))->unref(); |
67 canvas.drawRect(rect, paint); | 72 canvas.drawRect(rect, paint); |
68 rect.inset(wScalar / 8, hScalar / 8); | 73 rect.inset(wScalar / 8, hScalar / 8); |
69 mat.postScale(SK_Scalar1 / 4, SK_Scalar1 / 4); | 74 mat.postScale(SK_Scalar1 / 4, SK_Scalar1 / 4); |
70 } | 75 } |
71 // Let backends know we won't change this, so they don't have to deep copy i
t defensively. | 76 // Let backends know we won't change this, so they don't have to deep copy i
t defensively. |
72 bm->setImmutable(); | 77 bm->setImmutable(); |
| 78 |
| 79 return image_from_bitmap(*bm); |
73 } | 80 } |
74 | 81 |
| 82 static void canvasproc(SkCanvas* canvas, SkImage*, const SkBitmap& bm, const SkI
Rect* srcR, |
| 83 const SkRect& dstR) { |
| 84 canvas->drawBitmapRect(bm, srcR, dstR); |
| 85 } |
| 86 |
| 87 static void imageproc(SkCanvas* canvas, SkImage* image, const SkBitmap&, const S
kIRect* srcIR, |
| 88 const SkRect& dstR) { |
| 89 SkRect storage, *srcR = NULL; |
| 90 if (srcIR) { |
| 91 storage.set(*srcIR); |
| 92 srcR = &storage; |
| 93 } |
| 94 canvas->drawImageRect(image, srcR, dstR); |
| 95 } |
| 96 |
| 97 static void imagescaleproc(SkCanvas* canvas, SkImage* image, const SkBitmap&, co
nst SkIRect* srcIR, |
| 98 const SkRect& dstR) { |
| 99 const int newW = SkScalarRoundToInt(dstR.width()); |
| 100 const int newH = SkScalarRoundToInt(dstR.height()); |
| 101 SkAutoTUnref<SkImage> newImage(image->newImage(newW, newH, srcIR)); |
| 102 |
| 103 #ifdef SK_DEBUG |
| 104 const SkIRect baseR = SkIRect::MakeWH(image->width(), image->height()); |
| 105 const bool containsSubset = !srcIR || baseR.contains(*srcIR); |
| 106 #endif |
| 107 |
| 108 if (newImage) { |
| 109 SkASSERT(containsSubset); |
| 110 canvas->drawImage(newImage, dstR.x(), dstR.y()); |
| 111 } else { |
| 112 // newImage() does not support subsets that are not contained by the ori
ginal |
| 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); |
| 116 SkPaint paint; |
| 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); |
| 121 } |
| 122 } |
| 123 |
| 124 typedef void DrawRectRectProc(SkCanvas*, SkImage*, const SkBitmap&, const SkIRec
t*, const SkRect&); |
| 125 |
75 static const int gSize = 1024; | 126 static const int gSize = 1024; |
| 127 static const int gBmpSize = 2048; |
76 | 128 |
77 class DrawBitmapRectGM : public GM { | 129 class DrawBitmapRectGM : public skiagm::GM { |
78 public: | 130 public: |
79 DrawBitmapRectGM() { | 131 DrawBitmapRectGM(DrawRectRectProc proc, const char suffix[]) : fProc(proc) { |
| 132 fName.set("drawbitmaprect"); |
| 133 if (suffix) { |
| 134 fName.append(suffix); |
| 135 } |
80 } | 136 } |
81 | 137 |
82 SkBitmap fLargeBitmap; | 138 DrawRectRectProc* fProc; |
| 139 SkBitmap fLargeBitmap; |
| 140 SkAutoTUnref<SkImage> fImage; |
| 141 SkString fName; |
83 | 142 |
84 protected: | 143 protected: |
85 SkString onShortName() { | 144 SkString onShortName() SK_OVERRIDE { return fName; } |
86 return SkString("drawbitmaprect"); | 145 |
| 146 SkISize onISize() SK_OVERRIDE { return SkISize::Make(gSize, gSize); } |
| 147 |
| 148 void onOnceBeforeDraw() SK_OVERRIDE { |
| 149 fImage.reset(makebm(&fLargeBitmap, gBmpSize, gBmpSize)); |
87 } | 150 } |
88 | 151 |
89 SkISize onISize() { return SkISize::Make(gSize, gSize); } | 152 void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
90 | |
91 virtual void onDraw(SkCanvas* canvas) { | |
92 static const int kBmpSize = 2048; | |
93 if (fLargeBitmap.isNull()) { | |
94 makebm(&fLargeBitmap, kBmpSize, kBmpSize); | |
95 } | |
96 SkRect dstRect = { 0, 0, SkIntToScalar(64), SkIntToScalar(64)}; | 153 SkRect dstRect = { 0, 0, SkIntToScalar(64), SkIntToScalar(64)}; |
97 static const int kMaxSrcRectSize = 1 << (SkNextLog2(kBmpSize) + 2); | 154 static const int kMaxSrcRectSize = 1 << (SkNextLog2(gBmpSize) + 2); |
98 | 155 |
99 static const int kPadX = 30; | 156 static const int kPadX = 30; |
100 static const int kPadY = 40; | 157 static const int kPadY = 40; |
101 SkPaint paint; | 158 SkPaint paint; |
102 paint.setAlpha(0x20); | 159 paint.setAlpha(0x20); |
103 canvas->drawBitmapRect(fLargeBitmap, NULL, | 160 canvas->drawBitmapRect(fLargeBitmap, NULL, |
104 SkRect::MakeWH(gSize * SK_Scalar1, | 161 SkRect::MakeWH(gSize * SK_Scalar1, |
105 gSize * SK_Scalar1), | 162 gSize * SK_Scalar1), |
106 &paint); | 163 &paint); |
107 canvas->translate(SK_Scalar1 * kPadX / 2, | 164 canvas->translate(SK_Scalar1 * kPadX / 2, |
108 SK_Scalar1 * kPadY / 2); | 165 SK_Scalar1 * kPadY / 2); |
109 SkPaint blackPaint; | 166 SkPaint blackPaint; |
110 SkScalar titleHeight = SK_Scalar1 * 24; | 167 SkScalar titleHeight = SK_Scalar1 * 24; |
111 blackPaint.setColor(SK_ColorBLACK); | 168 blackPaint.setColor(SK_ColorBLACK); |
112 blackPaint.setTextSize(titleHeight); | 169 blackPaint.setTextSize(titleHeight); |
113 blackPaint.setAntiAlias(true); | 170 blackPaint.setAntiAlias(true); |
114 sk_tool_utils::set_portable_typeface(&blackPaint); | 171 sk_tool_utils::set_portable_typeface(&blackPaint); |
115 SkString title; | 172 SkString title; |
116 title.printf("Bitmap size: %d x %d", kBmpSize, kBmpSize); | 173 title.printf("Bitmap size: %d x %d", gBmpSize, gBmpSize); |
117 canvas->drawText(title.c_str(), title.size(), 0, | 174 canvas->drawText(title.c_str(), title.size(), 0, |
118 titleHeight, blackPaint); | 175 titleHeight, blackPaint); |
119 | 176 |
120 canvas->translate(0, SK_Scalar1 * kPadY / 2 + titleHeight); | 177 canvas->translate(0, SK_Scalar1 * kPadY / 2 + titleHeight); |
121 int rowCount = 0; | 178 int rowCount = 0; |
122 canvas->save(); | 179 canvas->save(); |
123 for (int w = 1; w <= kMaxSrcRectSize; w *= 4) { | 180 for (int w = 1; w <= kMaxSrcRectSize; w *= 4) { |
124 for (int h = 1; h <= kMaxSrcRectSize; h *= 4) { | 181 for (int h = 1; h <= kMaxSrcRectSize; h *= 4) { |
125 | 182 |
126 SkIRect srcRect = SkIRect::MakeXYWH((kBmpSize - w) / 2, | 183 SkIRect srcRect = SkIRect::MakeXYWH((gBmpSize - w) / 2, (gBmpSiz
e - h) / 2, w, h); |
127 (kBmpSize - h) / 2, | 184 fProc(canvas, fImage, fLargeBitmap, &srcRect, dstRect); |
128 w, h); | |
129 canvas->drawBitmapRect(fLargeBitmap, &srcRect, dstRect); | |
130 | 185 |
131 SkString label; | 186 SkString label; |
132 label.appendf("%d x %d", w, h); | 187 label.appendf("%d x %d", w, h); |
133 blackPaint.setAntiAlias(true); | 188 blackPaint.setAntiAlias(true); |
134 blackPaint.setStyle(SkPaint::kFill_Style); | 189 blackPaint.setStyle(SkPaint::kFill_Style); |
135 blackPaint.setTextSize(SK_Scalar1 * 10); | 190 blackPaint.setTextSize(SK_Scalar1 * 10); |
136 SkScalar baseline = dstRect.height() + | 191 SkScalar baseline = dstRect.height() + |
137 blackPaint.getTextSize() + SK_Scalar1 * 3; | 192 blackPaint.getTextSize() + SK_Scalar1 * 3; |
138 canvas->drawText(label.c_str(), label.size(), | 193 canvas->drawText(label.c_str(), label.size(), |
139 0, baseline, | 194 0, baseline, |
(...skipping 29 matching lines...) Expand all Loading... |
169 kNormal_SkBlurStyle, | 224 kNormal_SkBlurStyle, |
170 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)), | 225 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)), |
171 SkBlurMaskFilter::kHighQuality_BlurFlag | | 226 SkBlurMaskFilter::kHighQuality_BlurFlag | |
172 SkBlurMaskFilter::kIgnoreTransform_BlurFlag); | 227 SkBlurMaskFilter::kIgnoreTransform_BlurFlag); |
173 paint.setMaskFilter(mf)->unref(); | 228 paint.setMaskFilter(mf)->unref(); |
174 canvas->drawBitmapRect(bm, &srcRect, dstRect, &paint); | 229 canvas->drawBitmapRect(bm, &srcRect, dstRect, &paint); |
175 } | 230 } |
176 } | 231 } |
177 | 232 |
178 private: | 233 private: |
179 typedef GM INHERITED; | 234 typedef skiagm::GM INHERITED; |
180 }; | 235 }; |
181 | 236 |
182 ////////////////////////////////////////////////////////////////////////////// | 237 DEF_GM( return new DrawBitmapRectGM(canvasproc, NULL); ) |
183 | 238 DEF_GM( return new DrawBitmapRectGM(imageproc, "-imagerect"); ) |
184 static GM* MyFactory(void*) { return new DrawBitmapRectGM; } | 239 DEF_GM( return new DrawBitmapRectGM(imagescaleproc, "-imagescale"); ) |
185 static GMRegistry reg(MyFactory); | |
186 } | |
OLD | NEW |