2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
10 #include "Resources.h"
11 #include "SkGradientShader.h"
12 #include "SkTypeface.h"
13 #include "SkImageDecoder.h"
17 static void make_checker(SkBitmap* bm, int size, int numChecks) {
18 bm->allocN32Pixels(size, size);
19 for (int y = 0; y < size; ++y) {
20 for (int x = 0; x < size; ++x) {
21 SkPMColor* s = bm->getAddr32(x, y);
22 int cx = (x * numChecks) / size;
23 int cy = (y * numChecks) / size;
33 static void setTypeface(SkPaint* paint, const char name[], SkTypeface::Style style) {
34 sk_tool_utils::set_portable_typeface(paint, name, style);
37 class DownsampleBitmapGM : public skiagm::GM {
42 SkFilterQuality fFilterQuality;
44 DownsampleBitmapGM(SkFilterQuality filterQuality)
45 : fFilterQuality(filterQuality)
47 this->setBGColor(0xFFDDDDDD);
51 const char* filterQualityToString() {
52 static const char *filterQualityNames[] = {
53 "none", "low", "medium", "high"
55 return filterQualityNames[fFilterQuality];
60 SkString onShortName() SK_OVERRIDE {
64 SkISize onISize() SK_OVERRIDE {
65 make_bitmap_wrapper();
66 return SkISize::Make(fBM.width(), 4 * fBM.height());
69 void make_bitmap_wrapper() {
76 virtual void make_bitmap() = 0;
78 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
79 make_bitmap_wrapper();
87 matrix.setScale( curScale, curScale );
90 paint.setFilterQuality(fFilterQuality);
93 canvas->translate(0, (SkScalar)curY);
94 canvas->concat(matrix);
95 canvas->drawBitmap(fBM, 0, 0, &paint);
98 curHeight = (int) (fBM.height() * curScale + 2);
101 } while (curHeight >= 2 && curY < 4*fBM.height());
105 typedef skiagm::GM INHERITED;
108 class DownsampleBitmapTextGM: public DownsampleBitmapGM {
110 DownsampleBitmapTextGM(float textSize, SkFilterQuality filterQuality)
111 : INHERITED(filterQuality), fTextSize(textSize)
113 fName.printf("downsamplebitmap_text_%s_%.2fpt", this->filterQualityToString(), fTextSize);
119 void make_bitmap() SK_OVERRIDE {
120 fBM.allocN32Pixels(int(fTextSize * 8), int(fTextSize * 6));
121 SkCanvas canvas(fBM);
122 canvas.drawColor(SK_ColorWHITE);
125 paint.setAntiAlias(true);
126 paint.setSubpixelText(true);
127 paint.setTextSize(fTextSize);
129 setTypeface(&paint, "Times", SkTypeface::kNormal);
130 canvas.drawText("Hamburgefons", 12, fTextSize/2, 1.2f*fTextSize, paint);
131 setTypeface(&paint, "Times", SkTypeface::kBold);
132 canvas.drawText("Hamburgefons", 12, fTextSize/2, 2.4f*fTextSize, paint);
133 setTypeface(&paint, "Times", SkTypeface::kItalic);
134 canvas.drawText("Hamburgefons", 12, fTextSize/2, 3.6f*fTextSize, paint);
135 setTypeface(&paint, "Times", SkTypeface::kBoldItalic);
136 canvas.drawText("Hamburgefons", 12, fTextSize/2, 4.8f*fTextSize, paint);
139 typedef DownsampleBitmapGM INHERITED;
142 class DownsampleBitmapCheckerboardGM: public DownsampleBitmapGM {
144 DownsampleBitmapCheckerboardGM(int size, int numChecks, SkFilterQuality filterQuality)
145 : INHERITED(filterQuality), fSize(size), fNumChecks(numChecks)
147 fName.printf("downsamplebitmap_checkerboard_%s_%d_%d", this->filterQualityToString(), fSize, fNumChecks);
154 void make_bitmap() SK_OVERRIDE {
155 make_checker(&fBM, fSize, fNumChecks);
158 typedef DownsampleBitmapGM INHERITED;
161 class DownsampleBitmapImageGM: public DownsampleBitmapGM {
163 DownsampleBitmapImageGM(const char filename[], SkFilterQuality filterQuality)
164 : INHERITED(filterQuality), fFilename(filename)
166 fName.printf("downsamplebitmap_image_%s_%s", this->filterQualityToString(), filename);
173 void make_bitmap() SK_OVERRIDE {
174 SkImageDecoder* codec = NULL;
175 SkString resourcePath = GetResourcePath(fFilename.c_str());
176 SkFILEStream stream(resourcePath.c_str());
177 if (stream.isValid()) {
178 codec = SkImageDecoder::Factory(&stream);
182 codec->decode(&stream, &fBM, kN32_SkColorType, SkImageDecoder::kDecodePixels_Mode);
185 fBM.allocN32Pixels(1, 1);
186 *(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad
188 fSize = fBM.height();
191 typedef DownsampleBitmapGM INHERITED;
194 #include "SkMipMap.h"
196 static void release_mipmap(void*, void* context) {
197 ((SkMipMap*)context)->unref();
200 class ShowMipLevels : public skiagm::GM {
205 this->setBGColor(0xFFDDDDDD);
206 make_checker(&fBM, 512, 256);
211 SkString onShortName() SK_OVERRIDE {
212 return SkString("showmiplevels");
215 SkISize onISize() SK_OVERRIDE {
216 return SkISize::Make(fBM.width() + 8, 2 * fBM.height() + 80);
219 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
222 canvas->drawBitmap(fBM, x, y, NULL);
223 y += fBM.height() + 4;
225 SkAutoTUnref<SkMipMap> mm(SkMipMap::Build(fBM, NULL));
227 SkMipMap::Level level;
228 SkScalar scale = 0.5f;
229 while (mm->extractLevel(scale, &level)) {
230 SkImageInfo info = SkImageInfo::MakeN32Premul(level.fWidth, level.fHeight);
232 bm.installPixels(info, level.fPixels, level.fRowBytes, NULL,
233 &release_mipmap, (void*)(SkRef(mm.get())));
235 canvas->drawBitmap(bm, x, y, NULL);
236 y += bm.height() + 4;
238 if (info.width() == 1 || info.height() == 1) {
245 typedef skiagm::GM INHERITED;
247 DEF_GM( return new ShowMipLevels; )
249 //////////////////////////////////////////////////////////////////////////////
251 DEF_GM( return new DownsampleBitmapTextGM(72, kHigh_SkFilterQuality); )
252 DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256, kHigh_SkFilterQuality); )
253 DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png", kHigh_SkFilterQuality); )
254 DEF_GM( return new DownsampleBitmapImageGM("mandrill_132x132_12x12.astc",
255 kHigh_SkFilterQuality); )
257 DEF_GM( return new DownsampleBitmapTextGM(72, kMedium_SkFilterQuality); )
258 DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256, kMedium_SkFilterQuality); )
259 DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png", kMedium_SkFilterQuality); )
260 DEF_GM( return new DownsampleBitmapImageGM("mandrill_132x132_12x12.astc",
261 kMedium_SkFilterQuality); )
263 DEF_GM( return new DownsampleBitmapTextGM(72, kLow_SkFilterQuality); )
264 DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256, kLow_SkFilterQuality); )
265 DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png", kLow_SkFilterQuality); )
266 DEF_GM( return new DownsampleBitmapImageGM("mandrill_132x132_12x12.astc",
267 kLow_SkFilterQuality); )
269 DEF_GM( return new DownsampleBitmapTextGM(72, kNone_SkFilterQuality); )
270 DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256, kNone_SkFilterQuality); )
271 DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png", kNone_SkFilterQuality); )
272 DEF_GM( return new DownsampleBitmapImageGM("mandrill_132x132_12x12.astc",
273 kNone_SkFilterQuality); )