OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2013 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 "SkBitmap.h" |
| 10 #include "SkCanvas.h" |
| 11 #include "SkColorPriv.h" |
| 12 |
| 13 /** |
| 14 * This GM checks that bitmap pixels are unpremultiplied before being exported |
| 15 * to other formats. If unpremultiplication is implemented properly, this |
| 16 * GM should come out completely white. If not, this GM looks like a row of two |
| 17 * greyscale gradients above a row of grey lines. |
| 18 * This tests both the ARGB4444 and ARGB8888 bitmap configurations. |
| 19 */ |
| 20 |
| 21 static const int SLIDE_SIZE = 256; |
| 22 static const int PIXEL_SIZE_8888 = SLIDE_SIZE / 256; |
| 23 static const int PIXEL_SIZE_4444 = SLIDE_SIZE / 16; |
| 24 |
| 25 static SkBitmap init_bitmap(SkBitmap::Config config) { |
| 26 SkBitmap bitmap; |
| 27 bitmap.setConfig(config, SLIDE_SIZE, SLIDE_SIZE); |
| 28 bitmap.allocPixels(); |
| 29 bitmap.eraseColor(SK_ColorWHITE); |
| 30 return bitmap; |
| 31 } |
| 32 |
| 33 static SkBitmap make_argb8888_gradient() { |
| 34 SkBitmap bitmap = init_bitmap(SkBitmap::kARGB_8888_Config); |
| 35 uint8_t rowColor = 0; |
| 36 for (int y = 0; y < SLIDE_SIZE; y++) { |
| 37 uint32_t* dst = bitmap.getAddr32(0, y); |
| 38 for (int x = 0; x < SLIDE_SIZE; x++) { |
| 39 dst[x] = SkPackARGB32(rowColor, rowColor, |
| 40 rowColor, rowColor); |
| 41 } |
| 42 if (y % PIXEL_SIZE_8888 == PIXEL_SIZE_8888 - 1) { |
| 43 rowColor++; |
| 44 } |
| 45 } |
| 46 return bitmap; |
| 47 } |
| 48 |
| 49 static SkBitmap make_argb4444_gradient() { |
| 50 SkBitmap bitmap = init_bitmap(SkBitmap::kARGB_4444_Config); |
| 51 uint8_t rowColor = 0; |
| 52 for (int y = 0; y < SLIDE_SIZE; y++) { |
| 53 uint16_t* dst = bitmap.getAddr16(0, y); |
| 54 for (int x = 0; x < SLIDE_SIZE; x++) { |
| 55 dst[x] = SkPackARGB4444(rowColor, rowColor, |
| 56 rowColor, rowColor); |
| 57 } |
| 58 if (y % PIXEL_SIZE_4444 == PIXEL_SIZE_4444 - 1) { |
| 59 rowColor++; |
| 60 } |
| 61 } |
| 62 return bitmap; |
| 63 } |
| 64 |
| 65 static SkBitmap make_argb8888_stripes() { |
| 66 SkBitmap bitmap = init_bitmap(SkBitmap::kARGB_8888_Config); |
| 67 uint8_t rowColor = 0; |
| 68 for (int y = 0; y < SLIDE_SIZE; y++) { |
| 69 uint32_t* dst = bitmap.getAddr32(0, y); |
| 70 for (int x = 0; x < SLIDE_SIZE; x++) { |
| 71 dst[x] = SkPackARGB32(rowColor, rowColor, |
| 72 rowColor, rowColor); |
| 73 } |
| 74 if (rowColor == 0) { |
| 75 rowColor = 255; |
| 76 } else { |
| 77 rowColor = 0; |
| 78 } |
| 79 } |
| 80 return bitmap; |
| 81 } |
| 82 |
| 83 static SkBitmap make_argb4444_stripes() { |
| 84 SkBitmap bitmap = init_bitmap(SkBitmap::kARGB_4444_Config); |
| 85 uint8_t rowColor = 0;; |
| 86 for (int y = 0; y < SLIDE_SIZE; y++) { |
| 87 uint16_t* dst = bitmap.getAddr16(0, y); |
| 88 for (int x = 0; x < SLIDE_SIZE; x++) { |
| 89 dst[x] = SkPackARGB4444(rowColor, rowColor, |
| 90 rowColor, rowColor); |
| 91 } |
| 92 if (rowColor == 0) { |
| 93 rowColor = 15; |
| 94 } else { |
| 95 rowColor = 0; |
| 96 } |
| 97 } |
| 98 return bitmap; |
| 99 } |
| 100 |
| 101 namespace skiagm { |
| 102 |
| 103 class BitmapPremulGM : public GM { |
| 104 public: |
| 105 BitmapPremulGM() { |
| 106 this->setBGColor(SK_ColorWHITE); |
| 107 } |
| 108 |
| 109 protected: |
| 110 SkString onShortName() SK_OVERRIDE { |
| 111 return SkString("bitmap_premul"); |
| 112 } |
| 113 |
| 114 virtual SkISize onISize() SK_OVERRIDE { |
| 115 return SkISize::Make(SLIDE_SIZE * 2, SLIDE_SIZE * 2); |
| 116 } |
| 117 |
| 118 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 119 SkScalar slideSize = SkIntToScalar(SLIDE_SIZE); |
| 120 canvas->drawBitmap(make_argb8888_gradient(), 0, 0); |
| 121 canvas->drawBitmap(make_argb4444_gradient(), slideSize, 0); |
| 122 canvas->drawBitmap(make_argb8888_stripes(), 0, slideSize); |
| 123 canvas->drawBitmap(make_argb4444_stripes(), slideSize, slideSize); |
| 124 } |
| 125 |
| 126 private: |
| 127 typedef GM INHERITED; |
| 128 }; |
| 129 |
| 130 DEF_GM( return new BitmapPremulGM; ) |
| 131 } |
OLD | NEW |