1 #include "SampleCode.h" 2 #include "SkView.h" 3 #include "SkCanvas.h" 4 #include "SkColorFilter.h" 5 #include "SkDevice.h" 6 #include "SkPaint.h" 7 #include "SkShader.h" 8 9 static int inflate5To8(int x) { 10 return (x << 3) | (x >> 2); 11 } 12 13 static int trunc5(int x) { 14 return x >> 3; 15 } 16 17 #define SK_R16_BITS 5 18 19 static int round5_slow(int x) { 20 int orig = x & 7; 21 int fake = x >> 5; 22 int trunc = x >> 3; 23 24 int diff = fake - orig; 25 26 int bias = 0; 27 if (diff > 4) { 28 bias = -1; 29 } else if (diff < -4) { 30 bias = 1; 31 } 32 return trunc + bias; 33 } 34 35 static int round5_fast(int x) { 36 int result = x + 3 - (x >> 5) + (x >> 7); 37 result >>= 3; 38 39 { 40 int r2 = round5_slow(x); 41 SkASSERT(r2 == result); 42 } 43 return result; 44 } 45 46 static void test_5bits() { 47 int e0 = 0; 48 int e1 = 0; 49 int e2 = 0; 50 int ae0 = 0; 51 int ae1 = 0; 52 int ae2 = 0; 53 for (int i = 0; i < 256; i++) { 54 int t0 = trunc5(i); 55 int t1 = round5_fast(i); 56 int t2 = trunc5(i); 57 int v0 = inflate5To8(t0); 58 int v1 = inflate5To8(t1); 59 int v2 = inflate5To8(t2); 60 int err0 = i - v0; 61 int err1 = i - v1; 62 int err2 = i - v2; 63 SkDebugf("--- %3d : trunc=%3d (%2d) round:%3d (%2d) \n"/*new:%d (%2d)\n"*/, i, 64 v0, err0, v1, err1, v2, err2); 65 66 67 e0 += err0; 68 e1 += err1; 69 e2 += err2; 70 ae0 += SkAbs32(err0); 71 ae1 += SkAbs32(err1); 72 ae2 += SkAbs32(err2); 73 } 74 SkDebugf("--- trunc: %d %d round: %d %d new: %d %d\n", e0, ae0, e1, ae1, e2, ae2); 75 } 76 77 static SkShader* createChecker() { 78 SkBitmap bm; 79 bm.setConfig(SkBitmap::kARGB_8888_Config, 2, 2); 80 bm.allocPixels(); 81 bm.lockPixels(); 82 *bm.getAddr32(0, 0) = *bm.getAddr32(1, 1) = SkPreMultiplyColor(0xFFFFFFFF); 83 *bm.getAddr32(0, 1) = *bm.getAddr32(1, 0) = SkPreMultiplyColor(0xFFCCCCCC); 84 SkShader* s = SkShader::CreateBitmapShader(bm, SkShader::kRepeat_TileMode, 85 SkShader::kRepeat_TileMode); 86 87 SkMatrix m; 88 m.setScale(12, 12); 89 s->setLocalMatrix(m); 90 return s; 91 } 92 93 static SkBitmap createBitmap(int n) { 94 SkBitmap bitmap; 95 bitmap.setConfig(SkBitmap::kARGB_8888_Config, n, n); 96 bitmap.allocPixels(); 97 bitmap.eraseColor(0); 98 99 SkCanvas canvas(bitmap); 100 SkRect r; 101 r.set(0, 0, SkIntToScalar(n), SkIntToScalar(n)); 102 r.inset(SK_Scalar1, SK_Scalar1); 103 104 SkPaint paint; 105 paint.setAntiAlias(true); 106 107 paint.setColor(SK_ColorRED); 108 canvas.drawOval(r, paint); 109 110 r.inset(SK_Scalar1*n/4, SK_Scalar1*n/4); 111 paint.setXfermodeMode(SkXfermode::kSrc_Mode); 112 paint.setColor(0x800000FF); 113 canvas.drawOval(r, paint); 114 115 return bitmap; 116 } 117 118 class ColorFilterView : public SampleView { 119 SkBitmap fBitmap; 120 SkShader* fShader; 121 enum { 122 N = 64 123 }; 124 public: 125 ColorFilterView() { 126 fBitmap = createBitmap(N); 127 fShader = createChecker(); 128 129 // test_5bits(); 130 } 131 132 virtual ~ColorFilterView() { 133 fShader->unref(); 134 } 135 136 protected: 137 // overrides from SkEventSink 138 virtual bool onQuery(SkEvent* evt) { 139 if (SampleCode::TitleQ(*evt)) { 140 SampleCode::TitleR(evt, "ColorFilter"); 141 return true; 142 } 143 return this->INHERITED::onQuery(evt); 144 } 145 146 virtual void onDrawBackground(SkCanvas* canvas) { 147 SkPaint paint; 148 paint.setShader(fShader); 149 canvas->drawPaint(paint); 150 } 151 152 virtual void onDrawContent(SkCanvas* canvas) { 153 if (false) { 154 SkPaint p; 155 p.setAntiAlias(true); 156 SkRect r = { 20.4f, 10, 20.6f, 20 }; 157 canvas->drawRect(r, p); 158 r.set(30.9f, 10, 31.1f, 20); 159 canvas->drawRect(r, p); 160 return; 161 } 162 163 static const SkXfermode::Mode gModes[] = { 164 SkXfermode::kClear_Mode, 165 SkXfermode::kSrc_Mode, 166 SkXfermode::kDst_Mode, 167 SkXfermode::kSrcOver_Mode, 168 SkXfermode::kDstOver_Mode, 169 SkXfermode::kSrcIn_Mode, 170 SkXfermode::kDstIn_Mode, 171 SkXfermode::kSrcOut_Mode, 172 SkXfermode::kDstOut_Mode, 173 SkXfermode::kSrcATop_Mode, 174 SkXfermode::kDstATop_Mode, 175 SkXfermode::kXor_Mode, 176 SkXfermode::kPlus_Mode, 177 SkXfermode::kMultiply_Mode, 178 }; 179 180 static const SkColor gColors[] = { 181 0xFF000000, 182 0x80000000, 183 0xFF00FF00, 184 0x8000FF00, 185 0x00000000, 186 }; 187 188 float scale = 1.5f; 189 SkPaint paint; 190 canvas->translate(N / 8, N / 8); 191 192 for (size_t y = 0; y < SK_ARRAY_COUNT(gColors); y++) { 193 for (size_t x = 0; x < SK_ARRAY_COUNT(gModes); x++) { 194 SkColorFilter* cf = SkColorFilter::CreateModeFilter(gColors[y], gModes[x]); 195 SkSafeUnref(paint.setColorFilter(cf)); 196 canvas->drawBitmap(fBitmap, x * N * 1.25f, y * N * scale, &paint); 197 } 198 } 199 200 } 201 202 private: 203 typedef SampleView INHERITED; 204 }; 205 206 ////////////////////////////////////////////////////////////////////////////// 207 208 static SkView* MyFactory() { return new ColorFilterView; } 209 static SkViewRegister reg(MyFactory); 210 211