1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
|
#include "SkCanvas.h"
#include "SkDevice.h"
#include "SkGLCanvas.h"
#include "SkGraphics.h"
#include "SkImageEncoder.h"
#include "SkPaint.h"
#include "SkPicture.h"
#include "SkStream.h"
#include "SkWindow.h"
#include "SampleCode.h"
//#define SK_SUPPORT_GL
#ifdef SK_SUPPORT_GL
#include <AGL/agl.h>
#include <OpenGL/gl.h>
#endif
#define ANIMATING_EVENTTYPE "nextSample"
#define ANIMATING_DELAY 750
#define USE_OFFSCREEN
SkViewRegister* SkViewRegister::gHead;
SkViewRegister::SkViewRegister(SkViewFactory fact) : fFact(fact) {
static bool gOnce;
if (!gOnce) {
gHead = NULL;
gOnce = true;
}
fChain = gHead;
gHead = this;
}
#ifdef SK_SUPPORT_GL
static AGLContext gAGLContext;
static void init_gl(WindowRef wref) {
GLint major, minor;
aglGetVersion(&major, &minor);
SkDebugf("---- agl version %d %d\n", major, minor);
const GLint pixelAttrs[] = {
AGL_RGBA,
AGL_DEPTH_SIZE, 32,
AGL_OFFSCREEN,
AGL_NONE
};
AGLPixelFormat format = aglCreatePixelFormat(pixelAttrs);
SkDebugf("----- agl format %p\n", format);
gAGLContext = aglCreateContext(format, NULL);
SkDebugf("----- agl context %p\n", gAGLContext);
aglDestroyPixelFormat(format);
aglEnable(gAGLContext, GL_BLEND);
aglEnable(gAGLContext, GL_LINE_SMOOTH);
aglEnable(gAGLContext, GL_POINT_SMOOTH);
aglEnable(gAGLContext, GL_POLYGON_SMOOTH);
aglSetCurrentContext(gAGLContext);
}
static void setup_offscreen_gl(const SkBitmap& offscreen, WindowRef wref) {
GLboolean success = true;
#ifdef USE_OFFSCREEN
success = aglSetOffScreen(gAGLContext,
offscreen.width(),
offscreen.height(),
offscreen.rowBytes(),
offscreen.getPixels());
#else
success = aglSetWindowRef(gAGLContext, wref);
#endif
GLenum err = aglGetError();
if (err) {
SkDebugf("---- setoffscreen %d %d %s [%d %d]\n", success, err,
aglErrorString(err), offscreen.width(), offscreen.height());
}
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
glEnable(GL_TEXTURE_2D);
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
}
#endif
//////////////////////////////////////////////////////////////////////////////
static const char gTitleEvtName[] = "SampleCode_Title_Event";
static const char gPrefSizeEvtName[] = "SampleCode_PrefSize_Event";
bool SampleCode::TitleQ(const SkEvent& evt) {
return evt.isType(gTitleEvtName, sizeof(gTitleEvtName) - 1);
}
void SampleCode::TitleR(SkEvent* evt, const char title[]) {
SkASSERT(evt && TitleQ(*evt));
evt->setString(gTitleEvtName, title);
}
bool SampleCode::PrefSizeQ(const SkEvent& evt) {
return evt.isType(gPrefSizeEvtName, sizeof(gPrefSizeEvtName) - 1);
}
void SampleCode::PrefSizeR(SkEvent* evt, SkScalar width, SkScalar height) {
SkASSERT(evt && PrefSizeQ(*evt));
SkScalar size[2];
size[0] = width;
size[1] = height;
evt->setScalars(gPrefSizeEvtName, 2, size);
}
//////////////////////////////////////////////////////////////////////////////
class SampleWindow : public SkOSWindow {
public:
SampleWindow(void* hwnd);
virtual ~SampleWindow();
protected:
virtual void onDraw(SkCanvas* canvas);
virtual bool onHandleKey(SkKey key);
virtual bool onHandleChar(SkUnichar);
virtual void onSizeChange();
virtual SkCanvas* beforeChildren(SkCanvas*);
virtual void afterChildren(SkCanvas*);
virtual bool onEvent(const SkEvent& evt);
#if 0
virtual bool handleChar(SkUnichar uni);
virtual bool handleEvent(const SkEvent& evt);
virtual bool handleKey(SkKey key);
virtual bool handleKeyUp(SkKey key);
virtual bool onClick(Click* click);
virtual Click* onFindClickHandler(SkScalar x, SkScalar y);
virtual bool onHandleKeyUp(SkKey key);
#endif
private:
const SkViewRegister* fCurr;
SkPicture* fPicture;
SkGLCanvas* fGLCanvas;
SkPath fClipPath;
enum CanvasType {
kRaster_CanvasType,
kPicture_CanvasType,
kOpenGL_CanvasType
};
CanvasType fCanvasType;
bool fUseClip;
bool fRepeatDrawing;
bool fAnimating;
int fScrollTestX, fScrollTestY;
void loadView(SkView*);
void updateTitle();
bool nextSample();
void postAnimatingEvent() {
if (fAnimating) {
SkEvent* evt = new SkEvent(ANIMATING_EVENTTYPE);
evt->post(this->getSinkID(), ANIMATING_DELAY);
}
}
static CanvasType cycle_canvastype(CanvasType);
typedef SkOSWindow INHERITED;
};
SampleWindow::CanvasType SampleWindow::cycle_canvastype(CanvasType ct) {
static const CanvasType gCT[] = {
kPicture_CanvasType,
kOpenGL_CanvasType,
kRaster_CanvasType
};
return gCT[ct];
}
SampleWindow::SampleWindow(void* hwnd) : INHERITED(hwnd) {
#ifdef SK_SUPPORT_GL
init_gl((WindowRef)hwnd);
#endif
fPicture = NULL;
fGLCanvas = NULL;
fCanvasType = kRaster_CanvasType;
fUseClip = false;
fRepeatDrawing = false;
fAnimating = false;
fScrollTestX = fScrollTestY = 0;
// this->setConfig(SkBitmap::kRGB_565_Config);
this->setConfig(SkBitmap::kARGB_8888_Config);
this->setVisibleP(true);
fCurr = SkViewRegister::Head();
this->loadView(fCurr->factory()());
}
SampleWindow::~SampleWindow() {
delete fPicture;
delete fGLCanvas;
}
void SampleWindow::onDraw(SkCanvas* canvas) {
if (fRepeatDrawing) {
this->inval(NULL);
}
}
#include "SkColorPriv.h"
static void reverseRedAndBlue(const SkBitmap& bm) {
SkASSERT(bm.config() == SkBitmap::kARGB_8888_Config);
uint8_t* p = (uint8_t*)bm.getPixels();
uint8_t* stop = p + bm.getSize();
while (p < stop) {
// swap red/blue (to go from ARGB(int) to RGBA(memory) and premultiply
unsigned scale = SkAlpha255To256(p[3]);
unsigned r = p[2];
unsigned b = p[0];
p[0] = SkAlphaMul(r, scale);
p[1] = SkAlphaMul(p[1], scale);
p[2] = SkAlphaMul(b, scale);
p += 4;
}
}
SkCanvas* SampleWindow::beforeChildren(SkCanvas* canvas) {
#ifdef SK_SUPPORT_GL
#ifndef USE_OFFSCREEN
aglSetWindowRef(gAGLContext, NULL);
#endif
#endif
switch (fCanvasType) {
case kRaster_CanvasType:
canvas = this->INHERITED::beforeChildren(canvas);
break;
case kPicture_CanvasType:
fPicture = new SkPicture;
canvas = fPicture->beginRecording(9999, 9999);
break;
#ifdef SK_SUPPORT_GL
case kOpenGL_CanvasType: {
//SkGLCanvas::DeleteAllTextures(); // just for testing
SkDevice* device = canvas->getDevice();
const SkBitmap& bitmap = device->accessBitmap(true);
// first clear the raster bitmap, so we don't see any leftover bits
bitmap.eraseColor(0);
// now setup our glcanvas
setup_offscreen_gl(bitmap, (WindowRef)this->getHWND());
fGLCanvas = new SkGLCanvas;
fGLCanvas->setViewport(bitmap.width(), bitmap.height());
canvas = fGLCanvas;
break;
}
#endif
}
if (fUseClip) {
canvas->drawColor(0xFFFF88FF);
canvas->clipPath(fClipPath);
}
return canvas;
}
static void paint_rgn(const SkBitmap& bm, const SkIRect& r,
const SkRegion& rgn) {
SkCanvas canvas(bm);
SkRegion inval(rgn);
inval.translate(r.fLeft, r.fTop);
canvas.clipRegion(inval);
canvas.drawColor(0xFFFF8080);
}
void SampleWindow::afterChildren(SkCanvas* orig) {
switch (fCanvasType) {
case kRaster_CanvasType:
break;
case kPicture_CanvasType:
if (false) {
SkPicture* pict = new SkPicture(*fPicture);
fPicture->unref();
orig->drawPicture(*pict);
pict->unref();
} if (true) {
SkDynamicMemoryWStream ostream;
fPicture->serialize(&ostream);
fPicture->unref();
SkMemoryStream istream(ostream.getStream(), ostream.getOffset());
SkPicture pict(&istream);
orig->drawPicture(pict);
} else {
fPicture->draw(orig);
fPicture->unref();
}
fPicture = NULL;
break;
#ifdef SK_SUPPORT_GL
case kOpenGL_CanvasType:
glFlush();
delete fGLCanvas;
fGLCanvas = NULL;
#ifdef USE_OFFSCREEN
reverseRedAndBlue(orig->getDevice()->accessBitmap(true));
#endif
break;
#endif
}
// if ((fScrollTestX | fScrollTestY) != 0)
{
const SkBitmap& bm = orig->getDevice()->accessBitmap(true);
int dx = fScrollTestX * 7;
int dy = fScrollTestY * 7;
SkIRect r;
SkRegion inval;
r.set(50, 50, 50+100, 50+100);
bm.scrollRect(&r, dx, dy, &inval);
paint_rgn(bm, r, inval);
}
}
static SkBitmap::Config gConfigCycle[] = {
SkBitmap::kNo_Config, // none -> none
SkBitmap::kNo_Config, // a1 -> none
SkBitmap::kNo_Config, // a8 -> none
SkBitmap::kNo_Config, // index8 -> none
SkBitmap::kARGB_4444_Config, // 565 -> 4444
SkBitmap::kARGB_8888_Config, // 4444 -> 8888
SkBitmap::kRGB_565_Config // 8888 -> 565
};
static SkBitmap::Config cycle_configs(SkBitmap::Config c) {
return gConfigCycle[c];
}
bool SampleWindow::nextSample() {
if (fCurr) {
fCurr = fCurr->next();
if (NULL == fCurr) {
fCurr = SkViewRegister::Head();
}
this->loadView(fCurr->factory()());
return true;
}
return false;
}
bool SampleWindow::onEvent(const SkEvent& evt) {
if (evt.isType(ANIMATING_EVENTTYPE)) {
if (fAnimating) {
this->nextSample();
this->postAnimatingEvent();
}
return true;
}
return this->INHERITED::onEvent(evt);
}
static void cleanup_for_filename(SkString* name) {
char* str = name->writable_str();
for (int i = 0; i < name->size(); i++) {
switch (str[i]) {
case ':': str[i] = '-'; break;
case '/': str[i] = '-'; break;
case ' ': str[i] = '_'; break;
default: break;
}
}
}
bool SampleWindow::onHandleChar(SkUnichar uni) {
int dx = 0xFF;
int dy = 0xFF;
switch (uni) {
case '5': dx = 0; dy = 0; break;
case '8': dx = 0; dy = -1; break;
case '6': dx = 1; dy = 0; break;
case '2': dx = 0; dy = 1; break;
case '4': dx = -1; dy = 0; break;
case '7': dx = -1; dy = -1; break;
case '9': dx = 1; dy = -1; break;
case '3': dx = 1; dy = 1; break;
case '1': dx = -1; dy = 1; break;
default:
break;
}
if (0xFF != dx && 0xFF != dy) {
if ((dx | dy) == 0) {
fScrollTestX = fScrollTestY = 0;
} else {
fScrollTestX += dx;
fScrollTestY += dy;
}
this->inval(NULL);
return true;
}
switch (uni) {
case 'a':
fAnimating = !fAnimating;
this->postAnimatingEvent();
this->updateTitle();
return true;
case 'f': {
const char* title = this->getTitle();
if (title[0] == 0) {
title = "sampleapp";
}
SkString name(title);
cleanup_for_filename(&name);
name.append(".png");
if (SkImageEncoder::EncodeFile(name.c_str(), this->getBitmap(),
SkImageEncoder::kPNG_Type, 100)) {
SkDebugf("Created %s\n", name.c_str());
}
return true;
}
default:
break;
}
return this->INHERITED::onHandleChar(uni);
}
#include "SkDumpCanvas.h"
bool SampleWindow::onHandleKey(SkKey key) {
switch (key) {
case kRight_SkKey:
if (this->nextSample()) {
return true;
}
break;
case kLeft_SkKey:
fCanvasType = cycle_canvastype(fCanvasType);
this->updateTitle();
this->inval(NULL);
return true;
case kUp_SkKey:
fUseClip = !fUseClip;
this->updateTitle();
this->inval(NULL);
return true;
case kDown_SkKey:
this->setConfig(cycle_configs(this->getBitmap().config()));
this->updateTitle();
return true;
case kOK_SkKey:
if (true) {
SkDebugfDumper dumper;
SkDumpCanvas dc(&dumper);
this->draw(&dc);
} else {
fRepeatDrawing = !fRepeatDrawing;
if (fRepeatDrawing) {
this->inval(NULL);
}
}
return true;
default:
break;
}
return this->INHERITED::onHandleKey(key);
}
void SampleWindow::loadView(SkView* view) {
SkView::F2BIter iter(this);
SkView* prev = iter.next();
if (prev) {
prev->detachFromParent();
}
view->setVisibleP(true);
this->attachChildToFront(view)->unref();
view->setSize(this->width(), this->height());
this->updateTitle();
}
static const char* gConfigNames[] = {
"unknown config",
"A1",
"A8",
"Index8",
"565",
"4444",
"8888"
};
static const char* configToString(SkBitmap::Config c) {
return gConfigNames[c];
}
static const char* gCanvasTypePrefix[] = {
"raster: ",
"picture: ",
"opengl: "
};
void SampleWindow::updateTitle() {
SkString title;
SkView::F2BIter iter(this);
SkView* view = iter.next();
SkEvent evt(gTitleEvtName);
if (view->doQuery(&evt)) {
title.set(evt.findString(gTitleEvtName));
}
if (title.size() == 0) {
title.set("<unknown>");
}
title.prepend(gCanvasTypePrefix[fCanvasType]);
title.prepend(" ");
title.prepend(configToString(this->getBitmap().config()));
if (fAnimating) {
title.prepend("<A> ");
}
this->setTitle(title.c_str());
}
void SampleWindow::onSizeChange() {
this->INHERITED::onSizeChange();
SkView::F2BIter iter(this);
SkView* view = iter.next();
view->setSize(this->width(), this->height());
// rebuild our clippath
{
const SkScalar W = this->width();
const SkScalar H = this->height();
fClipPath.reset();
#if 0
for (SkScalar y = SK_Scalar1; y < H; y += SkIntToScalar(32)) {
SkRect r;
r.set(SK_Scalar1, y, SkIntToScalar(30), y + SkIntToScalar(30));
for (; r.fLeft < W; r.offset(SkIntToScalar(32), 0))
fClipPath.addRect(r);
}
#else
SkRect r;
r.set(0, 0, W, H);
fClipPath.addRect(r, SkPath::kCCW_Direction);
r.set(W/4, H/4, W*3/4, H*3/4);
fClipPath.addRect(r, SkPath::kCW_Direction);
#endif
}
this->updateTitle(); // to refresh our config
}
///////////////////////////////////////////////////////////////////////////////
SkOSWindow* create_sk_window(void* hwnd) {
return new SampleWindow(hwnd);
}
void get_preferred_size(int* x, int* y, int* width, int* height) {
*x = 10;
*y = 50;
*width = 640;
*height = 480;
}
void application_init() {
// setenv("ANDROID_ROOT", "../../../data", 0);
setenv("ANDROID_ROOT", "/android/device/data", 0);
SkGraphics::Init(true);
SkEvent::Init();
}
void application_term() {
SkEvent::Term();
SkGraphics::Term();
}
|