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