| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | #include "SampleCode.h" | 
|  | 2 | #include "SkView.h" | 
|  | 3 | #include "SkCanvas.h" | 
|  | 4 | #include "SkGraphics.h" | 
|  | 5 | #include "SkRandom.h" | 
|  | 6 | #include "SkFlipPixelRef.h" | 
|  | 7 | #include "SkPageFlipper.h" | 
|  | 8 |  | 
|  | 9 | #include <pthread.h> | 
|  | 10 |  | 
|  | 11 | #define WIDTH   200 | 
|  | 12 | #define HEIGHT  200 | 
|  | 13 |  | 
|  | 14 | static bool gDone; | 
|  | 15 |  | 
|  | 16 | static void bounce(SkScalar* x, SkScalar* dx, const int max) { | 
|  | 17 | *x += *dx; | 
|  | 18 | if (*x < 0) { | 
|  | 19 | *x = 0; | 
|  | 20 | if (*dx < 0) { | 
|  | 21 | *dx = -*dx; | 
|  | 22 | } | 
|  | 23 | } else if (*x > SkIntToScalar(max)) { | 
|  | 24 | *x = SkIntToScalar(max); | 
|  | 25 | if (*dx > 0) { | 
|  | 26 | *dx = -*dx; | 
|  | 27 | } | 
|  | 28 | } | 
|  | 29 | } | 
|  | 30 |  | 
|  | 31 | static void* draw_proc(void* context) { | 
|  | 32 | const int OVALW = 32; | 
|  | 33 | const int OVALH = 32; | 
|  | 34 |  | 
|  | 35 | const SkBitmap* bm = static_cast<const SkBitmap*>(context); | 
|  | 36 | SkFlipPixelRef* ref = static_cast<SkFlipPixelRef*>(bm->pixelRef()); | 
|  | 37 |  | 
|  | 38 | const int DSCALE = 1; | 
|  | 39 | SkScalar    dx = SkIntToScalar(7) / DSCALE; | 
|  | 40 | SkScalar    dy = SkIntToScalar(5) / DSCALE; | 
|  | 41 | SkScalar    x = 0; | 
|  | 42 | SkScalar    y = 0; | 
|  | 43 |  | 
|  | 44 | SkPaint paint; | 
|  | 45 |  | 
|  | 46 | paint.setAntiAlias(true); | 
| reed@android.com | 44a6312 | 2009-05-30 02:40:28 +0000 | [diff] [blame] | 47 | paint.setColor(SK_ColorRED); | 
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 48 |  | 
|  | 49 | SkRect oval; | 
|  | 50 | oval.setEmpty(); | 
|  | 51 |  | 
|  | 52 | while (!gDone) { | 
|  | 53 | ref->inval(oval, true); | 
|  | 54 | oval.set(x, y, x + SkIntToScalar(OVALW), y + SkIntToScalar(OVALH)); | 
|  | 55 | ref->inval(oval, true); | 
|  | 56 |  | 
|  | 57 | SkAutoFlipUpdate    update(ref); | 
|  | 58 |  | 
|  | 59 | if (!update.dirty().isEmpty()) { | 
|  | 60 | // this must be local to the loop, since it needs to forget the pixels | 
|  | 61 | // its writing to after each iteration, since we do the swap | 
|  | 62 | SkCanvas    canvas(update.bitmap()); | 
|  | 63 |  | 
|  | 64 | //            SkDebugf("----- dirty [%d %d %d %d]\n", dirty.getBounds().fLeft, dirty.getBounds().fTop, dirty.getBounds().width(), dirty.getBounds().height()); | 
|  | 65 | canvas.clipRegion(update.dirty()); | 
|  | 66 |  | 
| reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 67 | canvas.drawColor(0, SkXfermode::kClear_Mode); | 
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 68 | canvas.drawOval(oval, paint); | 
|  | 69 | } | 
|  | 70 | bounce(&x, &dx, WIDTH-OVALW); | 
|  | 71 | bounce(&y, &dy, HEIGHT-OVALH); | 
|  | 72 |  | 
|  | 73 | #if 1 | 
|  | 74 | for (int i = 0; i < 1000; i++) { | 
|  | 75 | for (int j = 0; j < 10000; j++) { | 
|  | 76 | SkFixedMul(j, 10); | 
|  | 77 | } | 
|  | 78 | } | 
|  | 79 | #endif | 
|  | 80 | } | 
|  | 81 | return NULL; | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | static const SkBitmap::Config gConfigs[] = { | 
|  | 85 | SkBitmap::kARGB_8888_Config, | 
|  | 86 | #if 1 | 
|  | 87 | SkBitmap::kRGB_565_Config, | 
|  | 88 | SkBitmap::kARGB_4444_Config, | 
|  | 89 | SkBitmap::kA8_Config | 
|  | 90 | #endif | 
|  | 91 | }; | 
|  | 92 |  | 
|  | 93 | class PageFlipView : public SkView { | 
|  | 94 | public: | 
|  | 95 |  | 
|  | 96 | enum { N = SK_ARRAY_COUNT(gConfigs) }; | 
|  | 97 |  | 
|  | 98 | pthread_t   fThreads[N]; | 
|  | 99 | SkBitmap    fBitmaps[N]; | 
|  | 100 |  | 
|  | 101 | PageFlipView() { | 
|  | 102 | gDone = false; | 
|  | 103 | for (int i = 0; i < N; i++) { | 
|  | 104 | int             status; | 
|  | 105 | pthread_attr_t  attr; | 
|  | 106 |  | 
|  | 107 | status = pthread_attr_init(&attr); | 
|  | 108 | SkASSERT(0 == status); | 
|  | 109 |  | 
|  | 110 | fBitmaps[i].setConfig(gConfigs[i], WIDTH, HEIGHT); | 
|  | 111 | SkFlipPixelRef* pr = new SkFlipPixelRef(gConfigs[i], WIDTH, HEIGHT); | 
|  | 112 | fBitmaps[i].setPixelRef(pr)->unref(); | 
|  | 113 | fBitmaps[i].eraseColor(0); | 
|  | 114 |  | 
|  | 115 | status = pthread_create(&fThreads[i], &attr,  draw_proc, &fBitmaps[i]); | 
|  | 116 | SkASSERT(0 == status); | 
|  | 117 | } | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 | virtual ~PageFlipView() { | 
|  | 121 | gDone = true; | 
|  | 122 | for (int i = 0; i < N; i++) { | 
|  | 123 | void* ret; | 
|  | 124 | int status = pthread_join(fThreads[i], &ret); | 
|  | 125 | SkASSERT(0 == status); | 
|  | 126 | } | 
|  | 127 | } | 
|  | 128 |  | 
|  | 129 | protected: | 
|  | 130 | // overrides from SkEventSink | 
|  | 131 | virtual bool onQuery(SkEvent* evt) { | 
|  | 132 | if (SampleCode::TitleQ(*evt)) { | 
|  | 133 | SampleCode::TitleR(evt, "PageFlip"); | 
|  | 134 | return true; | 
|  | 135 | } | 
|  | 136 | return this->INHERITED::onQuery(evt); | 
|  | 137 | } | 
|  | 138 |  | 
|  | 139 | void drawBG(SkCanvas* canvas) { | 
|  | 140 | canvas->drawColor(0xFFDDDDDD); | 
|  | 141 | //        canvas->drawColor(SK_ColorWHITE); | 
|  | 142 | } | 
|  | 143 |  | 
|  | 144 | virtual void onDraw(SkCanvas* canvas) { | 
|  | 145 | this->drawBG(canvas); | 
|  | 146 |  | 
|  | 147 | SkScalar x = SkIntToScalar(10); | 
|  | 148 | SkScalar y = SkIntToScalar(10); | 
|  | 149 | for (int i = 0; i < N; i++) { | 
|  | 150 | canvas->drawBitmap(fBitmaps[i], x, y); | 
|  | 151 | x += SkIntToScalar(fBitmaps[i].width() + 20); | 
|  | 152 | } | 
|  | 153 | this->inval(NULL); | 
|  | 154 | } | 
|  | 155 |  | 
|  | 156 | virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) { | 
|  | 157 | this->inval(NULL); | 
|  | 158 | return this->INHERITED::onFindClickHandler(x, y); | 
|  | 159 | } | 
|  | 160 |  | 
|  | 161 | virtual bool onClick(Click* click) { | 
|  | 162 | return this->INHERITED::onClick(click); | 
|  | 163 | } | 
|  | 164 |  | 
|  | 165 | private: | 
|  | 166 | typedef SkView INHERITED; | 
|  | 167 | }; | 
|  | 168 |  | 
|  | 169 | ////////////////////////////////////////////////////////////////////////////// | 
|  | 170 |  | 
|  | 171 | static SkView* MyFactory() { return new PageFlipView; } | 
|  | 172 | static SkViewRegister reg(MyFactory); | 
|  | 173 |  |