blob: 7c5bf480e843ac3e73351e3b692acb61e40202aa [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001#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
14static bool gDone;
15
16static 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
31static 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.com44a63122009-05-30 02:40:28 +000047 paint.setColor(SK_ColorRED);
reed@android.com8a1c16f2008-12-17 15:59:43 +000048
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.com845fdac2009-06-23 03:01:32 +000067 canvas.drawColor(0, SkXfermode::kClear_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000068 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
84static 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
reed@google.com81e3d7f2011-06-01 12:42:36 +000093class PageFlipView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +000094public:
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 }
reed@google.com81e3d7f2011-06-01 12:42:36 +0000118 this->setBGColor(0xFFDDDDDD);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000119 }
120
121 virtual ~PageFlipView() {
122 gDone = true;
123 for (int i = 0; i < N; i++) {
124 void* ret;
125 int status = pthread_join(fThreads[i], &ret);
126 SkASSERT(0 == status);
127 }
128 }
129
130protected:
131 // overrides from SkEventSink
132 virtual bool onQuery(SkEvent* evt) {
133 if (SampleCode::TitleQ(*evt)) {
134 SampleCode::TitleR(evt, "PageFlip");
135 return true;
136 }
137 return this->INHERITED::onQuery(evt);
138 }
reed@google.com81e3d7f2011-06-01 12:42:36 +0000139
140 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141 SkScalar x = SkIntToScalar(10);
142 SkScalar y = SkIntToScalar(10);
143 for (int i = 0; i < N; i++) {
144 canvas->drawBitmap(fBitmaps[i], x, y);
145 x += SkIntToScalar(fBitmaps[i].width() + 20);
146 }
147 this->inval(NULL);
148 }
149
150 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
151 this->inval(NULL);
152 return this->INHERITED::onFindClickHandler(x, y);
153 }
154
155 virtual bool onClick(Click* click) {
156 return this->INHERITED::onClick(click);
157 }
158
159private:
reed@google.com81e3d7f2011-06-01 12:42:36 +0000160 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000161};
162
163//////////////////////////////////////////////////////////////////////////////
164
165static SkView* MyFactory() { return new PageFlipView; }
166static SkViewRegister reg(MyFactory);
167