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 */ 7 8 #include "gm.h" 9 #include "SkGradientShader.h" 10 11 #include "SkTypeface.h" 12 #include "SkImageDecoder.h" 13 #include "SkStream.h" 14 15 static void setTypeface(SkPaint* paint, const char name[], SkTypeface::Style style) { 16 SkSafeUnref(paint->setTypeface(SkTypeface::CreateFromName(name, style))); 17 } 18 19 static SkSize computeSize(const SkBitmap& bm, const SkMatrix& mat) { 20 SkRect bounds = SkRect::MakeWH(SkIntToScalar(bm.width()), 21 SkIntToScalar(bm.height())); 22 mat.mapRect(&bounds); 23 return SkSize::Make(bounds.width(), bounds.height()); 24 } 25 26 static void draw_col(SkCanvas* canvas, const SkBitmap& bm, const SkMatrix& mat, 27 SkScalar dx) { 28 SkPaint paint; 29 30 SkAutoCanvasRestore acr(canvas, true); 31 32 canvas->drawBitmapMatrix(bm, mat, &paint); 33 34 paint.setFilterLevel(SkPaint::kLow_FilterLevel); 35 canvas->translate(dx, 0); 36 canvas->drawBitmapMatrix(bm, mat, &paint); 37 38 paint.setFilterLevel(SkPaint::kHigh_FilterLevel); 39 canvas->translate(dx, 0); 40 canvas->drawBitmapMatrix(bm, mat, &paint); 41 } 42 43 class FilterBitmapGM : public skiagm::GM { 44 void onOnceBeforeDraw() { 45 46 make_bitmap(); 47 48 SkScalar cx = SkScalarHalf(fBM.width()); 49 SkScalar cy = SkScalarHalf(fBM.height()); 50 SkScalar scale = get_scale(); 51 52 53 fMatrix[0].setScale(scale, scale); 54 fMatrix[1].setRotate(30, cx, cy); fMatrix[1].postScale(scale, scale); 55 } 56 57 public: 58 SkBitmap fBM; 59 SkMatrix fMatrix[2]; 60 SkString fName; 61 62 FilterBitmapGM() 63 { 64 this->setBGColor(0xFFDDDDDD); 65 } 66 67 protected: 68 virtual SkString onShortName() SK_OVERRIDE { 69 return fName; 70 } 71 72 virtual SkISize onISize() SK_OVERRIDE { 73 return SkISize::Make(920, 480); 74 } 75 76 virtual void make_bitmap() = 0; 77 virtual SkScalar get_scale() = 0; 78 79 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { 80 81 canvas->translate(10, 10); 82 for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrix); ++i) { 83 SkSize size = computeSize(fBM, fMatrix[i]); 84 size.fWidth += 20; 85 size.fHeight += 20; 86 87 draw_col(canvas, fBM, fMatrix[i], size.fWidth); 88 canvas->translate(0, size.fHeight); 89 } 90 } 91 92 private: 93 typedef skiagm::GM INHERITED; 94 }; 95 96 class FilterBitmapTextGM: public FilterBitmapGM { 97 public: 98 FilterBitmapTextGM(float textSize) 99 : fTextSize(textSize) 100 { 101 fName.printf("filterbitmap_text_%.2fpt", fTextSize); 102 } 103 104 protected: 105 float fTextSize; 106 107 SkScalar get_scale() SK_OVERRIDE { 108 return 32.f/fTextSize; 109 } 110 111 void make_bitmap() SK_OVERRIDE { 112 fBM.setConfig(SkBitmap::kARGB_8888_Config, int(fTextSize * 8), int(fTextSize * 6)); 113 fBM.allocPixels(); 114 SkCanvas canvas(fBM); 115 canvas.drawColor(SK_ColorWHITE); 116 117 SkPaint paint; 118 paint.setAntiAlias(true); 119 paint.setSubpixelText(true); 120 paint.setTextSize(fTextSize); 121 122 setTypeface(&paint, "Times", SkTypeface::kNormal); 123 canvas.drawText("Hamburgefons", 12, fTextSize/2, 1.2f*fTextSize, paint); 124 setTypeface(&paint, "Times", SkTypeface::kBold); 125 canvas.drawText("Hamburgefons", 12, fTextSize/2, 2.4f*fTextSize, paint); 126 setTypeface(&paint, "Times", SkTypeface::kItalic); 127 canvas.drawText("Hamburgefons", 12, fTextSize/2, 3.6f*fTextSize, paint); 128 setTypeface(&paint, "Times", SkTypeface::kBoldItalic); 129 canvas.drawText("Hamburgefons", 12, fTextSize/2, 4.8f*fTextSize, paint); 130 } 131 private: 132 typedef FilterBitmapGM INHERITED; 133 }; 134 135 class FilterBitmapCheckerboardGM: public FilterBitmapGM { 136 public: 137 FilterBitmapCheckerboardGM(int size, int num_checks) 138 : fSize(size), fNumChecks(num_checks) 139 { 140 fName.printf("filterbitmap_checkerboard_%d_%d", fSize, fNumChecks); 141 } 142 143 protected: 144 int fSize; 145 int fNumChecks; 146 147 SkScalar get_scale() SK_OVERRIDE { 148 return 192.f/fSize; 149 } 150 151 void make_bitmap() SK_OVERRIDE { 152 fBM.setConfig(SkBitmap::kARGB_8888_Config, fSize, fSize); 153 fBM.allocPixels(); 154 SkAutoLockPixels lock(fBM); 155 for (int y = 0; y < fSize; y ++) { 156 for (int x = 0; x < fSize; x ++) { 157 SkPMColor* s = fBM.getAddr32(x, y); 158 int cx = (x * fNumChecks) / fSize; 159 int cy = (y * fNumChecks) / fSize; 160 if ((cx+cy)%2) { 161 *s = 0xFFFFFFFF; 162 } else { 163 *s = 0xFF000000; 164 } 165 } 166 } 167 } 168 private: 169 typedef FilterBitmapGM INHERITED; 170 }; 171 172 class FilterBitmapImageGM: public FilterBitmapGM { 173 public: 174 FilterBitmapImageGM(const char filename[]) 175 : fFilename(filename) 176 { 177 fName.printf("filterbitmap_image_%s", filename); 178 } 179 180 protected: 181 SkString fFilename; 182 int fSize; 183 184 SkScalar get_scale() SK_OVERRIDE { 185 return 192.f/fSize; 186 } 187 188 void make_bitmap() SK_OVERRIDE { 189 SkString path(skiagm::GM::gResourcePath); 190 path.append("/"); 191 path.append(fFilename); 192 193 SkImageDecoder *codec = NULL; 194 SkFILEStream stream(path.c_str()); 195 if (stream.isValid()) { 196 codec = SkImageDecoder::Factory(&stream); 197 } 198 if (codec) { 199 stream.rewind(); 200 codec->decode(&stream, &fBM, SkBitmap::kARGB_8888_Config, 201 SkImageDecoder::kDecodePixels_Mode); 202 SkDELETE(codec); 203 } else { 204 fBM.setConfig(SkBitmap::kARGB_8888_Config, 1, 1); 205 fBM.allocPixels(); 206 *(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad 207 } 208 fSize = fBM.height(); 209 } 210 private: 211 typedef FilterBitmapGM INHERITED; 212 }; 213 214 ////////////////////////////////////////////////////////////////////////////// 215 216 DEF_GM( return new FilterBitmapTextGM(3); ) 217 DEF_GM( return new FilterBitmapTextGM(7); ) 218 DEF_GM( return new FilterBitmapTextGM(10); ) 219 DEF_GM( return new FilterBitmapCheckerboardGM(4,4); ) 220 DEF_GM( return new FilterBitmapCheckerboardGM(32,32); ) 221 DEF_GM( return new FilterBitmapCheckerboardGM(32,8); ) 222 DEF_GM( return new FilterBitmapCheckerboardGM(32,2); ) 223 DEF_GM( return new FilterBitmapCheckerboardGM(192,192); ) 224 DEF_GM( return new FilterBitmapImageGM("mandrill_16.png"); ) 225 DEF_GM( return new FilterBitmapImageGM("mandrill_32.png"); ) 226 DEF_GM( return new FilterBitmapImageGM("mandrill_64.png"); ) 227 DEF_GM( return new FilterBitmapImageGM("mandrill_128.png"); ) 228 DEF_GM( return new FilterBitmapImageGM("mandrill_256.png"); ) 229 DEF_GM( return new FilterBitmapImageGM("mandrill_512.png"); ) 230