2014-09-09 23:51:04 +00:00
|
|
|
// Copyright 2014 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package app
|
|
|
|
|
|
|
|
/*
|
|
|
|
#cgo android LDFLAGS: -llog -landroid -lEGL -lGLESv2
|
|
|
|
#include <android/log.h>
|
|
|
|
#include <android/native_activity.h>
|
2014-09-11 23:39:16 +00:00
|
|
|
#include <android/input.h>
|
2014-09-09 23:51:04 +00:00
|
|
|
#include <EGL/egl.h>
|
|
|
|
#include <GLES/gl.h>
|
|
|
|
|
|
|
|
// TODO(crawshaw): Test configuration on more devices.
|
|
|
|
const EGLint RGB_888[] = {
|
|
|
|
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
|
|
|
|
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
|
|
|
|
EGL_BLUE_SIZE, 8,
|
|
|
|
EGL_GREEN_SIZE, 8,
|
|
|
|
EGL_RED_SIZE, 8,
|
|
|
|
EGL_DEPTH_SIZE, 16,
|
|
|
|
EGL_CONFIG_CAVEAT, EGL_NONE,
|
|
|
|
EGL_NONE
|
|
|
|
};
|
|
|
|
|
|
|
|
EGLint windowWidth;
|
|
|
|
EGLint windowHeight;
|
|
|
|
EGLDisplay display;
|
|
|
|
EGLSurface surface;
|
|
|
|
|
|
|
|
#define LOG_ERROR(...) __android_log_print(ANDROID_LOG_ERROR, "Go", __VA_ARGS__)
|
|
|
|
|
2015-04-09 19:00:55 +00:00
|
|
|
void querySurfaceWidthAndHeight() {
|
|
|
|
eglQuerySurface(display, surface, EGL_WIDTH, &windowWidth);
|
|
|
|
eglQuerySurface(display, surface, EGL_HEIGHT, &windowHeight);
|
|
|
|
}
|
|
|
|
|
2014-09-09 23:51:04 +00:00
|
|
|
void createEGLWindow(ANativeWindow* window) {
|
|
|
|
EGLint numConfigs, format;
|
|
|
|
EGLConfig config;
|
|
|
|
EGLContext context;
|
|
|
|
|
|
|
|
display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
|
|
|
|
if (!eglInitialize(display, 0, 0)) {
|
|
|
|
LOG_ERROR("EGL initialize failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!eglChooseConfig(display, RGB_888, &config, 1, &numConfigs)) {
|
|
|
|
LOG_ERROR("EGL choose RGB_888 config failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (numConfigs <= 0) {
|
|
|
|
LOG_ERROR("EGL no config found");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);
|
|
|
|
if (ANativeWindow_setBuffersGeometry(window, 0, 0, format) != 0) {
|
|
|
|
LOG_ERROR("EGL set buffers geometry failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
surface = eglCreateWindowSurface(display, config, window, NULL);
|
|
|
|
if (surface == EGL_NO_SURFACE) {
|
|
|
|
LOG_ERROR("EGL create surface failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const EGLint contextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
|
|
|
|
context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttribs);
|
|
|
|
|
|
|
|
if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) {
|
|
|
|
LOG_ERROR("eglMakeCurrent failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-09 19:00:55 +00:00
|
|
|
querySurfaceWidthAndHeight();
|
2014-09-09 23:51:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef LOG_ERROR
|
|
|
|
*/
|
|
|
|
import "C"
|
2014-09-11 23:39:16 +00:00
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
2014-11-09 21:55:57 +00:00
|
|
|
"golang.org/x/mobile/event"
|
|
|
|
"golang.org/x/mobile/geom"
|
|
|
|
"golang.org/x/mobile/gl"
|
2014-09-11 23:39:16 +00:00
|
|
|
)
|
|
|
|
|
2015-05-05 13:52:19 +00:00
|
|
|
func windowDraw(callbacks []Callbacks, w *C.ANativeWindow, queue *C.AInputQueue) {
|
2015-05-28 10:08:52 +00:00
|
|
|
go gl.Start(func() {
|
|
|
|
C.createEGLWindow(w)
|
|
|
|
})
|
2014-09-09 23:51:04 +00:00
|
|
|
|
2014-10-12 22:24:05 +00:00
|
|
|
// TODO: is the library or the app responsible for clearing the buffers?
|
2014-09-11 23:39:16 +00:00
|
|
|
gl.ClearColor(0, 0, 0, 1)
|
|
|
|
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
|
2015-05-28 10:08:52 +00:00
|
|
|
gl.Do(func() { C.eglSwapBuffers(C.display, C.surface) })
|
2014-09-11 23:39:16 +00:00
|
|
|
|
|
|
|
if errv := gl.GetError(); errv != gl.NO_ERROR {
|
|
|
|
log.Printf("GL initialization error: %s", errv)
|
|
|
|
}
|
|
|
|
|
2015-05-05 13:52:19 +00:00
|
|
|
configAlt.Width = geom.Pt(float32(C.windowWidth) / geom.PixelsPerPt)
|
|
|
|
configAlt.Height = geom.Pt(float32(C.windowHeight) / geom.PixelsPerPt)
|
|
|
|
configSwap(callbacks)
|
2014-09-09 23:51:04 +00:00
|
|
|
|
2015-02-22 20:27:20 +00:00
|
|
|
// Wait until geometry and GL is initialized before cb.Start.
|
2015-05-05 13:52:19 +00:00
|
|
|
stateStart(callbacks)
|
2015-02-22 20:27:20 +00:00
|
|
|
|
2014-09-09 23:51:04 +00:00
|
|
|
for {
|
2015-05-05 13:52:19 +00:00
|
|
|
processEvents(callbacks, queue)
|
2014-09-09 23:51:04 +00:00
|
|
|
select {
|
2015-04-09 19:00:55 +00:00
|
|
|
case <-windowRedrawNeeded:
|
|
|
|
// Re-query the width and height.
|
|
|
|
C.querySurfaceWidthAndHeight()
|
2015-05-05 13:52:19 +00:00
|
|
|
configAlt.Width = geom.Pt(float32(C.windowWidth) / geom.PixelsPerPt)
|
|
|
|
configAlt.Height = geom.Pt(float32(C.windowHeight) / geom.PixelsPerPt)
|
|
|
|
gl.Viewport(0, 0, int(C.windowWidth), int(C.windowHeight))
|
|
|
|
configSwap(callbacks)
|
2014-09-09 23:51:04 +00:00
|
|
|
case <-windowDestroyed:
|
2015-05-05 13:52:19 +00:00
|
|
|
stateStop(callbacks)
|
2015-05-28 10:08:52 +00:00
|
|
|
gl.Stop()
|
2014-09-09 23:51:04 +00:00
|
|
|
return
|
|
|
|
default:
|
2015-05-05 13:52:19 +00:00
|
|
|
for _, cb := range callbacks {
|
|
|
|
if cb.Draw != nil {
|
|
|
|
cb.Draw()
|
|
|
|
}
|
2014-10-12 22:24:05 +00:00
|
|
|
}
|
2015-05-28 10:08:52 +00:00
|
|
|
gl.Do(func() { C.eglSwapBuffers(C.display, C.surface) })
|
2014-09-09 23:51:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-11 23:39:16 +00:00
|
|
|
|
2015-05-05 13:52:19 +00:00
|
|
|
func processEvents(callbacks []Callbacks, queue *C.AInputQueue) {
|
2014-09-11 23:39:16 +00:00
|
|
|
var event *C.AInputEvent
|
|
|
|
for C.AInputQueue_getEvent(queue, &event) >= 0 {
|
|
|
|
if C.AInputQueue_preDispatchEvent(queue, event) != 0 {
|
|
|
|
continue
|
|
|
|
}
|
2015-05-05 13:52:19 +00:00
|
|
|
processEvent(callbacks, event)
|
2014-09-11 23:39:16 +00:00
|
|
|
C.AInputQueue_finishEvent(queue, event, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-05 13:52:19 +00:00
|
|
|
func processEvent(callbacks []Callbacks, e *C.AInputEvent) {
|
2014-09-11 23:39:16 +00:00
|
|
|
switch C.AInputEvent_getType(e) {
|
|
|
|
case C.AINPUT_EVENT_TYPE_KEY:
|
|
|
|
log.Printf("TODO input event: key")
|
|
|
|
case C.AINPUT_EVENT_TYPE_MOTION:
|
2015-05-05 13:52:19 +00:00
|
|
|
// TODO: calculate hasTouch once in run
|
|
|
|
hasTouch := false
|
|
|
|
for _, cb := range callbacks {
|
|
|
|
if cb.Touch != nil {
|
|
|
|
hasTouch = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !hasTouch {
|
2014-09-11 23:39:16 +00:00
|
|
|
return
|
|
|
|
}
|
2014-12-20 15:44:54 +00:00
|
|
|
|
|
|
|
// At most one of the events in this batch is an up or down event; get its index and type.
|
|
|
|
upDownIndex := C.size_t(C.AMotionEvent_getAction(e)&C.AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> C.AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT
|
|
|
|
upDownTyp := event.TouchMove
|
|
|
|
switch C.AMotionEvent_getAction(e) & C.AMOTION_EVENT_ACTION_MASK {
|
|
|
|
case C.AMOTION_EVENT_ACTION_DOWN, C.AMOTION_EVENT_ACTION_POINTER_DOWN:
|
|
|
|
upDownTyp = event.TouchStart
|
|
|
|
case C.AMOTION_EVENT_ACTION_UP, C.AMOTION_EVENT_ACTION_POINTER_UP:
|
|
|
|
upDownTyp = event.TouchEnd
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, n := C.size_t(0), C.AMotionEvent_getPointerCount(e); i < n; i++ {
|
|
|
|
typ := event.TouchMove
|
|
|
|
if i == upDownIndex {
|
|
|
|
typ = upDownTyp
|
|
|
|
}
|
2015-05-05 13:52:19 +00:00
|
|
|
t := event.Touch{
|
2014-12-21 17:00:15 +00:00
|
|
|
ID: event.TouchSequenceID(C.AMotionEvent_getPointerId(e, i)),
|
2014-12-20 15:44:54 +00:00
|
|
|
Type: typ,
|
|
|
|
Loc: geom.Point{
|
2014-12-21 17:00:15 +00:00
|
|
|
X: geom.Pt(float32(C.AMotionEvent_getX(e, i)) / geom.PixelsPerPt),
|
|
|
|
Y: geom.Pt(float32(C.AMotionEvent_getY(e, i)) / geom.PixelsPerPt),
|
2014-12-20 15:44:54 +00:00
|
|
|
},
|
2015-05-05 13:52:19 +00:00
|
|
|
}
|
|
|
|
for _, cb := range callbacks {
|
|
|
|
if cb.Touch != nil {
|
|
|
|
cb.Touch(t)
|
|
|
|
}
|
|
|
|
}
|
2014-09-11 23:39:16 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
log.Printf("unknown input event, type=%d", C.AInputEvent_getType(e))
|
|
|
|
}
|
|
|
|
}
|