blob: 1db0b2ad52301822bdd1c6ccf5d05bb76b7f3dcd [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@google.com67f11b12011-10-28 14:55:53 +000073 canvas.clipRect(clipR, SkRegion::kIntersect_Op, true);
reed@android.com8a1c16f2008-12-17 15:59:43 +000074
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 canvas.clipRegion(update.dirty());
76
reed@android.com845fdac2009-06-23 03:01:32 +000077 canvas.drawColor(0, SkXfermode::kClear_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000078 canvas.drawOval(oval, paint);
79 }
80 bounce(&x, &dx, WIDTH-OVALW);
81 bounce(&y, &dy, HEIGHT-OVALH);
reed@android.com8a1c16f2008-12-17 15:59:43 +000082 }
83 return NULL;
84}
85
86static const SkBitmap::Config gConfigs[] = {
87 SkBitmap::kARGB_8888_Config,
reed@android.com8a1c16f2008-12-17 15:59:43 +000088 SkBitmap::kRGB_565_Config,
89 SkBitmap::kARGB_4444_Config,
90 SkBitmap::kA8_Config
reed@android.com8a1c16f2008-12-17 15:59:43 +000091};
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