1 #include "SampleCode.h" 2 #include "SkColorPriv.h" 3 #include "SkGradientShader.h" 4 #include "SkView.h" 5 #include "SkCanvas.h" 6 #include "SkUtils.h" 7 8 static void draw_rect(SkCanvas* canvas, const SkRect& r, const SkPaint& p) { 9 canvas->drawRect(r, p); 10 11 SkPaint frame(p); 12 frame.setShader(NULL); 13 frame.setStyle(SkPaint::kStroke_Style); 14 canvas->drawRect(r, frame); 15 } 16 17 static void draw_gradient(SkCanvas* canvas) { 18 SkRect r = { 0, 0, SkIntToScalar(256), SkIntToScalar(32) }; 19 SkPoint pts[] = { { r.fLeft, r.fTop }, { r.fRight, r.fTop } }; 20 SkColor colors[] = { 0xFF000000, 0xFFFF0000 }; 21 SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL, 2, 22 SkShader::kClamp_TileMode); 23 24 SkPaint p; 25 p.setShader(s)->unref(); 26 draw_rect(canvas, r, p); 27 28 canvas->translate(0, SkIntToScalar(40)); 29 p.setDither(true); 30 draw_rect(canvas, r, p); 31 } 32 33 static void test_pathregion() { 34 SkPath path; 35 SkRegion region; 36 path.moveTo(25071800.f, -141823808.f); 37 path.lineTo(25075500.f, -141824000.f); 38 path.lineTo(25075400.f, -141827712.f); 39 path.lineTo(25071810.f, -141827600.f); 40 path.close(); 41 42 SkIRect bounds; 43 path.getBounds().round(&bounds); 44 SkRegion clip(bounds); 45 bool result = region.setPath(path, clip); // <-- !! DOWN !! 46 SkDebugf("----- result %d\n", result); 47 } 48 49 static SkBitmap make_bitmap() { 50 SkBitmap bm; 51 SkColorTable* ctable = new SkColorTable(256); 52 53 SkPMColor* c = ctable->lockColors(); 54 for (int i = 0; i < 256; i++) { 55 c[i] = SkPackARGB32(0xFF, i, 0, 0); 56 } 57 ctable->unlockColors(true); 58 bm.setConfig(SkBitmap::kIndex8_Config, 256, 32); 59 bm.allocPixels(ctable); 60 ctable->unref(); 61 62 bm.lockPixels(); 63 for (int y = 0; y < bm.height(); y++) { 64 uint8_t* p = bm.getAddr8(0, y); 65 for (int x = 0; x < 256; x++) { 66 p[x] = x; 67 } 68 } 69 bm.unlockPixels(); 70 return bm; 71 } 72 73 class DitherBitmapView : public SampleView { 74 SkBitmap fBM8; 75 SkBitmap fBM32; 76 public: 77 DitherBitmapView() { 78 test_pathregion(); 79 fBM8 = make_bitmap(); 80 fBM8.copyTo(&fBM32, SkBitmap::kARGB_8888_Config); 81 82 this->setBGColor(0xFFDDDDDD); 83 } 84 85 protected: 86 // overrides from SkEventSink 87 virtual bool onQuery(SkEvent* evt) { 88 if (SampleCode::TitleQ(*evt)) { 89 SampleCode::TitleR(evt, "DitherBitmap"); 90 return true; 91 } 92 return this->INHERITED::onQuery(evt); 93 } 94 95 static void setBitmapOpaque(SkBitmap* bm, bool isOpaque) { 96 SkAutoLockPixels alp(*bm); // needed for ctable 97 bm->setIsOpaque(isOpaque); 98 SkColorTable* ctable = bm->getColorTable(); 99 if (ctable) { 100 ctable->setIsOpaque(isOpaque); 101 } 102 } 103 104 static void draw2(SkCanvas* canvas, const SkBitmap& bm) { 105 SkPaint paint; 106 SkBitmap bitmap(bm); 107 108 setBitmapOpaque(&bitmap, false); 109 paint.setDither(false); 110 canvas->drawBitmap(bitmap, 0, 0, &paint); 111 paint.setDither(true); 112 canvas->drawBitmap(bitmap, 0, SkIntToScalar(bm.height() + 10), &paint); 113 114 setBitmapOpaque(&bitmap, true); 115 SkScalar x = SkIntToScalar(bm.width() + 10); 116 paint.setDither(false); 117 canvas->drawBitmap(bitmap, x, 0, &paint); 118 paint.setDither(true); 119 canvas->drawBitmap(bitmap, x, SkIntToScalar(bm.height() + 10), &paint); 120 } 121 122 virtual void onDrawContent(SkCanvas* canvas) { 123 canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); 124 125 draw2(canvas, fBM8); 126 canvas->translate(0, SkIntToScalar(fBM8.height() *3)); 127 draw2(canvas, fBM32); 128 129 canvas->translate(0, SkIntToScalar(fBM8.height() *3)); 130 draw_gradient(canvas); 131 } 132 133 private: 134 typedef SampleView INHERITED; 135 }; 136 137 ////////////////////////////////////////////////////////////////////////////// 138 139 static SkView* MyFactory() { return new DitherBitmapView; } 140 static SkViewRegister reg(MyFactory); 141 142