blob: b2d96f0812a3612a2e057872adcca6ea1ab57c8f [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@android.com8a1c16f2008-12-17 15:59:43 +000093public:
94
95 enum { N = SK_ARRAY_COUNT(gConfigs) };
96
97 pthread_t fThreads[N];
98 SkBitmap fBitmaps[N];
99
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 }
reed@google.com81e3d7f2011-06-01 12:42:36 +0000117 this->setBGColor(0xFFDDDDDD);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118 }
119
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
129protected:
130 // overrides from SkEventSink
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 }
reed@google.com81e3d7f2011-06-01 12:42:36 +0000138
139 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000140 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
149 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
150 this->inval(NULL);
151 return this->INHERITED::onFindClickHandler(x, y);
152 }
153
154 virtual bool onClick(Click* click) {
155 return this->INHERITED::onClick(click);
156 }
157
158private:
reed@google.com81e3d7f2011-06-01 12:42:36 +0000159 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160};
161
162//////////////////////////////////////////////////////////////////////////////
163
164static SkView* MyFactory() { return new PageFlipView; }
165static SkViewRegister reg(MyFactory);
166