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 */
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
18 #define WIDTH 160
19 #define HEIGHT 200
20
21 static bool gDone;
22
bounce(SkScalar * x,SkScalar * dx,const int max)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
draw_proc(void * context)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);
54 paint.setColor(SK_ColorRED);
55
56 SkRect oval;
57 oval.setEmpty();
58
59 SkRect clipR = SkRect::MakeWH(SkIntToScalar(bm->width()), SkIntToScalar(bm->height()));
60 clipR.inset(SK_Scalar1/4, SK_Scalar1/4);
61
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());
73 canvas.clipRegion(update.dirty());
74 canvas.drawColor(0, SkXfermode::kClear_Mode);
75 canvas.clipRect(clipR, SkRegion::kIntersect_Op, true);
76
77 canvas.drawOval(oval, paint);
78 }
79 bounce(&x, &dx, WIDTH-OVALW);
80 bounce(&y, &dy, HEIGHT-OVALH);
81 }
82 return NULL;
83 }
84
85 static const SkBitmap::Config gConfigs[] = {
86 SkBitmap::kARGB_8888_Config,
87 SkBitmap::kRGB_565_Config,
88 SkBitmap::kARGB_4444_Config,
89 SkBitmap::kA8_Config
90 };
91
92 class PageFlipView : public SampleView {
93 public:
94
95 enum { N = SK_ARRAY_COUNT(gConfigs) };
96
97 pthread_t fThreads[N];
98 SkBitmap fBitmaps[N];
99
PageFlipView()100 PageFlipView() {
101 gDone = false;
102 for (int i = 0; i < N; i++) {
103 int status;
104 pthread_attr_t attr;
105
106 status = pthread_attr_init(&attr);
107 SkASSERT(0 == status);
108
109 fBitmaps[i].setConfig(gConfigs[i], WIDTH, HEIGHT);
110 SkFlipPixelRef* pr = new SkFlipPixelRef(gConfigs[i], WIDTH, HEIGHT);
111 fBitmaps[i].setPixelRef(pr)->unref();
112 fBitmaps[i].eraseColor(0);
113
114 status = pthread_create(&fThreads[i], &attr, draw_proc, &fBitmaps[i]);
115 SkASSERT(0 == status);
116 }
117 this->setBGColor(0xFFDDDDDD);
118 }
119
~PageFlipView()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
onQuery(SkEvent * evt)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
onDrawContent(SkCanvas * canvas)139 virtual void onDrawContent(SkCanvas* canvas) {
140 SkScalar x = SkIntToScalar(10);
141 SkScalar y = SkIntToScalar(10);
142 for (int i = 0; i < N; i++) {
143 canvas->drawBitmap(fBitmaps[i], x, y);
144 x += SkIntToScalar(fBitmaps[i].width() + 20);
145 }
146 this->inval(NULL);
147 }
148
onFindClickHandler(SkScalar x,SkScalar y)149 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
150 this->inval(NULL);
151 return this->INHERITED::onFindClickHandler(x, y);
152 }
153
onClick(Click * click)154 virtual bool onClick(Click* click) {
155 return this->INHERITED::onClick(click);
156 }
157
158 private:
159 typedef SampleView INHERITED;
160 };
161
162 //////////////////////////////////////////////////////////////////////////////
163
MyFactory()164 static SkView* MyFactory() { return new PageFlipView; }
165 static SkViewRegister reg(MyFactory);
166
167