blob: 99670bd2357cc081bac1e34945de6f8cf986340c [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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.com8a1c16f2008-12-17 15:59:43 +00008#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.com67f11b12011-10-28 14:55:53 +000018#define WIDTH 160
reed@android.com8a1c16f2008-12-17 15:59:43 +000019#define HEIGHT 200
20
21static bool gDone;
22
23static 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
38static 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.com44a63122009-05-30 02:40:28 +000054 paint.setColor(SK_ColorRED);
reed@android.com8a1c16f2008-12-17 15:59:43 +000055
56 SkRect oval;
57 oval.setEmpty();
58
reed@google.com67f11b12011-10-28 14:55:53 +000059 SkRect clipR = SkRect::MakeWH(SkIntToScalar(bm->width()), SkIntToScalar(bm->height()));
60 clipR.inset(SK_Scalar1/4, SK_Scalar1/4);
61
reed@android.com8a1c16f2008-12-17 15:59:43 +000062 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.com8a1c16f2008-12-17 15:59:43 +000073 canvas.clipRegion(update.dirty());
reed@android.com845fdac2009-06-23 03:01:32 +000074 canvas.drawColor(0, SkXfermode::kClear_Mode);
mike@reedtribe.org6151d1d2011-10-30 17:06:24 +000075 canvas.clipRect(clipR, SkRegion::kIntersect_Op, true);
76
reed@android.com8a1c16f2008-12-17 15:59:43 +000077 canvas.drawOval(oval, paint);
78 }
79 bounce(&x, &dx, WIDTH-OVALW);
80 bounce(&y, &dy, HEIGHT-OVALH);
reed@android.com8a1c16f2008-12-17 15:59:43 +000081 }
82 return NULL;
83}
84
85static const SkBitmap::Config gConfigs[] = {
86 SkBitmap::kARGB_8888_Config,
reed@android.com8a1c16f2008-12-17 15:59:43 +000087 SkBitmap::kRGB_565_Config,
88 SkBitmap::kARGB_4444_Config,
89 SkBitmap::kA8_Config
reed@android.com8a1c16f2008-12-17 15:59:43 +000090};
91
reed@google.com81e3d7f2011-06-01 12:42:36 +000092class PageFlipView : public SampleView {
reed@google.com7b185902012-03-12 21:13:48 +000093 bool fOnce;
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;
reed@google.com7b185902012-03-12 21:13:48 +0000103 fOnce = false;
104 this->setBGColor(0xFFDDDDDD);
105 }
106
107 void init() {
108 if (fOnce) {
109 return;
110 }
111 fOnce = true;
112
reed@android.com8a1c16f2008-12-17 15:59:43 +0000113 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.com7b185902012-03-12 21:13:48 +0000119
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120 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.com7b185902012-03-12 21:13:48 +0000124
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125 status = pthread_create(&fThreads[i], &attr, draw_proc, &fBitmaps[i]);
126 SkASSERT(0 == status);
127 }
128 }
129
130 virtual ~PageFlipView() {
reed@google.com7b185902012-03-12 21:13:48 +0000131 if (!fOnce) {
132 return;
133 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000134 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
142protected:
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.com81e3d7f2011-06-01 12:42:36 +0000151
152 virtual void onDrawContent(SkCanvas* canvas) {
reed@google.com7b185902012-03-12 21:13:48 +0000153 this->init();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000154 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
172private:
reed@google.com81e3d7f2011-06-01 12:42:36 +0000173 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000174};
175
176//////////////////////////////////////////////////////////////////////////////
177
178static SkView* MyFactory() { return new PageFlipView; }
179static SkViewRegister reg(MyFactory);
180