| OLD | NEW | 
|---|
| 1 | 1 | 
| 2 /* | 2 /* | 
| 3  * Copyright 2011 Google Inc. | 3  * Copyright 2011 Google Inc. | 
| 4  * | 4  * | 
| 5  * Use of this source code is governed by a BSD-style license that can be | 5  * Use of this source code is governed by a BSD-style license that can be | 
| 6  * found in the LICENSE file. | 6  * found in the LICENSE file. | 
| 7  */ | 7  */ | 
| 8 #include "SkBenchmark.h" | 8 #include "SkBenchmark.h" | 
| 9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" | 
| 10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" | 
| 11 #include "SkColorPriv.h" | 11 #include "SkColorPriv.h" | 
| 12 #include "SkPaint.h" | 12 #include "SkPaint.h" | 
| 13 #include "SkShader.h" | 13 #include "SkShader.h" | 
| 14 #include "SkString.h" | 14 #include "SkString.h" | 
| 15 | 15 | 
| 16 static const char* gConfigName[] = { | 16 static const char* gConfigName[] = { | 
| 17     "ERROR", "a1", "a8", "index8", "565", "4444", "8888" | 17     "ERROR", "a1", "a8", "index8", "565", "4444", "8888" | 
| 18 }; | 18 }; | 
| 19 | 19 | 
| 20 static void drawIntoBitmap(const SkBitmap& bm) { | 20 static void draw_into_bitmap(const SkBitmap& bm) { | 
| 21     const int w = bm.width(); | 21     const int w = bm.width(); | 
| 22     const int h = bm.height(); | 22     const int h = bm.height(); | 
| 23 | 23 | 
| 24     SkCanvas canvas(bm); | 24     SkCanvas canvas(bm); | 
| 25     SkPaint p; | 25     SkPaint p; | 
| 26     p.setAntiAlias(true); | 26     p.setAntiAlias(true); | 
| 27     p.setColor(SK_ColorRED); | 27     p.setColor(SK_ColorRED); | 
| 28     canvas.drawCircle(SkIntToScalar(w)/2, SkIntToScalar(h)/2, | 28     canvas.drawCircle(SkIntToScalar(w)/2, SkIntToScalar(h)/2, | 
| 29                       SkIntToScalar(SkMin32(w, h))*3/8, p); | 29                       SkIntToScalar(SkMin32(w, h))*3/8, p); | 
| 30 | 30 | 
| 31     SkRect r; | 31     SkRect r; | 
| 32     r.set(0, 0, SkIntToScalar(w), SkIntToScalar(h)); | 32     r.set(0, 0, SkIntToScalar(w), SkIntToScalar(h)); | 
| 33     p.setStyle(SkPaint::kStroke_Style); | 33     p.setStyle(SkPaint::kStroke_Style); | 
| 34     p.setStrokeWidth(SkIntToScalar(4)); | 34     p.setStrokeWidth(SkIntToScalar(4)); | 
| 35     p.setColor(SK_ColorBLUE); | 35     p.setColor(SK_ColorBLUE); | 
| 36     canvas.drawRect(r, p); | 36     canvas.drawRect(r, p); | 
| 37 } | 37 } | 
| 38 | 38 | 
| 39 static int conv6ToByte(int x) { | 39 static int conv_6_to_byte(int x) { | 
| 40     return x * 0xFF / 5; | 40     return x * 0xFF / 5; | 
| 41 } | 41 } | 
| 42 | 42 | 
| 43 static int convByteTo6(int x) { | 43 static int conv_byte_to_6(int x) { | 
| 44     return x * 5 / 255; | 44     return x * 5 / 255; | 
| 45 } | 45 } | 
| 46 | 46 | 
| 47 static uint8_t compute666Index(SkPMColor c) { | 47 static uint8_t compute_666_index(SkPMColor c) { | 
| 48     int r = SkGetPackedR32(c); | 48     int r = SkGetPackedR32(c); | 
| 49     int g = SkGetPackedG32(c); | 49     int g = SkGetPackedG32(c); | 
| 50     int b = SkGetPackedB32(c); | 50     int b = SkGetPackedB32(c); | 
| 51 | 51 | 
| 52     return convByteTo6(r) * 36 + convByteTo6(g) * 6 + convByteTo6(b); | 52     return conv_byte_to_6(r) * 36 + conv_byte_to_6(g) * 6 + conv_byte_to_6(b); | 
| 53 } | 53 } | 
| 54 | 54 | 
| 55 static void convertToIndex666(const SkBitmap& src, SkBitmap* dst) { | 55 static void convert_to_index666(const SkBitmap& src, SkBitmap* dst) { | 
| 56     SkColorTable* ctable = new SkColorTable(216); | 56     SkColorTable* ctable = new SkColorTable(216); | 
| 57     SkPMColor* colors = ctable->lockColors(); | 57     SkPMColor* colors = ctable->lockColors(); | 
| 58     // rrr ggg bbb | 58     // rrr ggg bbb | 
| 59     for (int r = 0; r < 6; r++) { | 59     for (int r = 0; r < 6; r++) { | 
| 60         int rr = conv6ToByte(r); | 60         int rr = conv_6_to_byte(r); | 
| 61         for (int g = 0; g < 6; g++) { | 61         for (int g = 0; g < 6; g++) { | 
| 62             int gg = conv6ToByte(g); | 62             int gg = conv_6_to_byte(g); | 
| 63             for (int b = 0; b < 6; b++) { | 63             for (int b = 0; b < 6; b++) { | 
| 64                 int bb = conv6ToByte(b); | 64                 int bb = conv_6_to_byte(b); | 
| 65                 *colors++ = SkPreMultiplyARGB(0xFF, rr, gg, bb); | 65                 *colors++ = SkPreMultiplyARGB(0xFF, rr, gg, bb); | 
| 66             } | 66             } | 
| 67         } | 67         } | 
| 68     } | 68     } | 
| 69     ctable->unlockColors(true); | 69     ctable->unlockColors(true); | 
| 70     dst->setConfig(SkBitmap::kIndex8_Config, src.width(), src.height()); | 70     dst->setConfig(SkBitmap::kIndex8_Config, src.width(), src.height()); | 
| 71     dst->allocPixels(ctable); | 71     dst->allocPixels(ctable); | 
| 72     ctable->unref(); | 72     ctable->unref(); | 
| 73 | 73 | 
| 74     SkAutoLockPixels alps(src); | 74     SkAutoLockPixels alps(src); | 
| 75     SkAutoLockPixels alpd(*dst); | 75     SkAutoLockPixels alpd(*dst); | 
| 76 | 76 | 
| 77     for (int y = 0; y < src.height(); y++) { | 77     for (int y = 0; y < src.height(); y++) { | 
| 78         const SkPMColor* srcP = src.getAddr32(0, y); | 78         const SkPMColor* srcP = src.getAddr32(0, y); | 
| 79         uint8_t* dstP = dst->getAddr8(0, y); | 79         uint8_t* dstP = dst->getAddr8(0, y); | 
| 80         for (int x = src.width() - 1; x >= 0; --x) { | 80         for (int x = src.width() - 1; x >= 0; --x) { | 
| 81             *dstP++ = compute666Index(*srcP++); | 81             *dstP++ = compute_666_index(*srcP++); | 
| 82         } | 82         } | 
| 83     } | 83     } | 
| 84 } | 84 } | 
| 85 | 85 | 
| 86 class RepeatTileBench : public SkBenchmark { | 86 class RepeatTileBench : public SkBenchmark { | 
| 87     SkPaint     fPaint; | 87     SkPaint          fPaint; | 
| 88     SkString    fName; | 88     SkString         fName; | 
|  | 89     SkBitmap         fBitmap; | 
|  | 90     bool             fIsOpaque; | 
|  | 91     SkBitmap::Config fConfig; | 
| 89     enum { N = SkBENCHLOOP(20) }; | 92     enum { N = SkBENCHLOOP(20) }; | 
| 90 public: | 93 public: | 
| 91     RepeatTileBench(void* param, SkBitmap::Config c, bool isOpaque = false) : IN
     HERITED(param) { | 94     RepeatTileBench(void* param, SkBitmap::Config c, bool isOpaque = false) : IN
     HERITED(param) { | 
| 92         const int w = 50; | 95         const int w = 50; | 
| 93         const int h = 50; | 96         const int h = 50; | 
| 94         SkBitmap bm; | 97         fConfig = c; | 
|  | 98         fIsOpaque = isOpaque; | 
| 95 | 99 | 
| 96         if (SkBitmap::kIndex8_Config == c) { | 100         if (SkBitmap::kIndex8_Config == fConfig) { | 
| 97             bm.setConfig(SkBitmap::kARGB_8888_Config, w, h); | 101             fBitmap.setConfig(SkBitmap::kARGB_8888_Config, w, h); | 
| 98         } else { | 102         } else { | 
| 99             bm.setConfig(c, w, h); | 103             fBitmap.setConfig(fConfig, w, h); | 
| 100         } | 104         } | 
| 101         bm.allocPixels(); | 105         fName.printf("repeatTile_%s_%c", | 
| 102         bm.eraseColor(isOpaque ? SK_ColorWHITE : 0); | 106                      gConfigName[fBitmap.config()], isOpaque ? 'X' : 'A'); | 
| 103         bm.setIsOpaque(isOpaque); | 107     } | 
| 104 | 108 | 
| 105         drawIntoBitmap(bm); | 109 protected: | 
|  | 110     virtual const char* onGetName() SK_OVERRIDE { | 
|  | 111         return fName.c_str(); | 
|  | 112     } | 
| 106 | 113 | 
| 107         if (SkBitmap::kIndex8_Config == c) { | 114     virtual void onPreDraw() SK_OVERRIDE { | 
|  | 115         fBitmap.allocPixels(); | 
|  | 116         fBitmap.eraseColor(fIsOpaque ? SK_ColorWHITE : 0); | 
|  | 117         fBitmap.setIsOpaque(fIsOpaque); | 
|  | 118 | 
|  | 119         draw_into_bitmap(fBitmap); | 
|  | 120 | 
|  | 121         if (SkBitmap::kIndex8_Config == fConfig) { | 
| 108             SkBitmap tmp; | 122             SkBitmap tmp; | 
| 109             convertToIndex666(bm, &tmp); | 123             convert_to_index666(fBitmap, &tmp); | 
| 110             bm = tmp; | 124             fBitmap = tmp; | 
| 111         } | 125         } | 
| 112 | 126 | 
| 113         SkShader* s = SkShader::CreateBitmapShader(bm, | 127         SkShader* s = SkShader::CreateBitmapShader(fBitmap, | 
| 114                                                    SkShader::kRepeat_TileMode, | 128                                                    SkShader::kRepeat_TileMode, | 
| 115                                                    SkShader::kRepeat_TileMode); | 129                                                    SkShader::kRepeat_TileMode); | 
| 116         fPaint.setShader(s)->unref(); | 130         fPaint.setShader(s)->unref(); | 
| 117         fName.printf("repeatTile_%s_%c", gConfigName[bm.config()], isOpaque ? 'X
     ' : 'A'); |  | 
| 118     } | 131     } | 
| 119 | 132 | 
| 120 protected: |  | 
| 121     virtual const char* onGetName() { |  | 
| 122         return fName.c_str(); |  | 
| 123     } |  | 
| 124 | 133 | 
| 125     virtual void onDraw(SkCanvas* canvas) { | 134     virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { | 
| 126         SkPaint paint(fPaint); | 135         SkPaint paint(fPaint); | 
| 127         this->setupPaint(&paint); | 136         this->setupPaint(&paint); | 
| 128 | 137 | 
| 129         for (int i = 0; i < N; i++) { | 138         for (int i = 0; i < N; i++) { | 
| 130             canvas->drawPaint(paint); | 139             canvas->drawPaint(paint); | 
| 131         } | 140         } | 
| 132     } | 141     } | 
| 133 | 142 | 
| 134 private: | 143 private: | 
| 135     typedef SkBenchmark INHERITED; | 144     typedef SkBenchmark INHERITED; | 
| 136 }; | 145 }; | 
| 137 | 146 | 
| 138 DEF_BENCH(return new RepeatTileBench(p, SkBitmap::kARGB_8888_Config, true)) | 147 DEF_BENCH(return new RepeatTileBench(p, SkBitmap::kARGB_8888_Config, true)) | 
| 139 DEF_BENCH(return new RepeatTileBench(p, SkBitmap::kARGB_8888_Config, false)) | 148 DEF_BENCH(return new RepeatTileBench(p, SkBitmap::kARGB_8888_Config, false)) | 
| 140 DEF_BENCH(return new RepeatTileBench(p, SkBitmap::kRGB_565_Config)) | 149 DEF_BENCH(return new RepeatTileBench(p, SkBitmap::kRGB_565_Config)) | 
| 141 DEF_BENCH(return new RepeatTileBench(p, SkBitmap::kIndex8_Config)) | 150 DEF_BENCH(return new RepeatTileBench(p, SkBitmap::kIndex8_Config)) | 
| OLD | NEW | 
|---|