Example #1
0
func NewPlayer(sampleRate, channelNum, bytesPerSample int) (*Player, error) {
	p := &Player{
		sampleRate:     sampleRate,
		channelNum:     channelNum,
		bytesPerSample: bytesPerSample,
		buffer:         []byte{},
		chErr:          make(chan error),
		chBuffer:       make(chan []byte, 8),
	}
	if err := jni.RunOnJVM(func(vm, env, ctx uintptr) error {
		audioTrack := C.jobject(nil)
		bufferSize := C.int(0)
		if msg := C.initAudioTrack(C.uintptr_t(vm), C.uintptr_t(env),
			C.int(sampleRate), C.int(channelNum), C.int(bytesPerSample),
			&audioTrack, &bufferSize); msg != nil {
			return errors.New("driver: " + C.GoString(msg))
		}
		p.audioTrack = audioTrack
		p.bufferSize = int(bufferSize)
		return nil
	}); err != nil {
		return nil, err
	}
	go p.loop()
	return p, nil
}
Example #2
0
File: gl.go Project: arnold8/mobile
func (ctx *context) TexImage2D(target Enum, level int, width, height int, format Enum, ty Enum, data []byte) {
	// It is common to pass TexImage2D a nil data, indicating that a
	// bound GL buffer is being used as the source. In that case, it
	// is not necessary to block.
	parg := unsafe.Pointer(nil)
	if len(data) > 0 {
		parg = unsafe.Pointer(&data[0])
	}

	ctx.enqueue(call{
		args: C.struct_fnargs{
			fn: C.glfnTexImage2D,
			// TODO(crawshaw): GLES3 offset for PIXEL_UNPACK_BUFFER and PIXEL_PACK_BUFFER.
			a0: target.c(),
			a1: C.uintptr_t(level),
			a2: C.uintptr_t(format),
			a3: C.uintptr_t(width),
			a4: C.uintptr_t(height),
			a5: format.c(),
			a6: ty.c(),
		},
		parg:     parg,
		blocking: parg != nil,
	})
}
Example #3
0
func (p *Player) loop() {
	for bufInBytes := range p.chBuffer {
		var bufInShorts []int16
		if p.bytesPerSample == 2 {
			bufInShorts = make([]int16, len(bufInBytes)/2)
			for i := 0; i < len(bufInShorts); i++ {
				bufInShorts[i] = int16(bufInBytes[2*i]) | (int16(bufInBytes[2*i+1]) << 8)
			}
		}

		if err := jni.RunOnJVM(func(vm, env, ctx uintptr) error {
			msg := (*C.char)(nil)
			switch p.bytesPerSample {
			case 1:
				msg = C.writeToAudioTrack(C.uintptr_t(vm), C.uintptr_t(env),
					p.audioTrack, C.int(p.bytesPerSample),
					unsafe.Pointer(&bufInBytes[0]), C.int(len(bufInBytes)))
			case 2:
				msg = C.writeToAudioTrack(C.uintptr_t(vm), C.uintptr_t(env),
					p.audioTrack, C.int(p.bytesPerSample),
					unsafe.Pointer(&bufInShorts[0]), C.int(len(bufInShorts)))
			}
			if msg != nil {
				return errors.New(C.GoString(msg))
			}
			return nil
		}); err != nil {
			p.chErr <- err
			return
		}
	}
}
Example #4
0
// drawLoop is the primary drawing loop.
//
// After Cocoa has created an NSWindow and called prepareOpenGL,
// it starts drawLoop on a locked goroutine to handle OpenGL calls.
//
// The screen is drawn every time a paint.Event is received, which can be
// triggered either by the user or by Cocoa via drawgl (for example, when
// the window is resized).
func drawLoop(w *windowImpl, vba uintptr) {
	runtime.LockOSThread()
	C.makeCurrentContext(C.uintptr_t(w.ctx))

	// Starting in OS X 10.11 (El Capitan), the vertex array is
	// occasionally getting unbound when the context changes threads.
	//
	// Avoid this by binding it again.
	C.glBindVertexArray(C.GLuint(vba))
	if errno := C.glGetError(); errno != 0 {
		panic(fmt.Sprintf("gldriver: glBindVertexArray failed: %d", errno))
	}

	workAvailable := w.worker.WorkAvailable()

	// TODO(crawshaw): exit this goroutine on Release.
	for {
		select {
		case <-workAvailable:
			w.worker.DoWork()
		case <-w.publish:
		loop:
			for {
				select {
				case <-workAvailable:
					w.worker.DoWork()
				default:
					break loop
				}
			}
			C.flushContext(C.uintptr_t(w.ctx))
			w.publishDone <- screen.PublishResult{}
		}
	}
}
Example #5
0
func (ctx *context) GetAttachedShaders(p Program) []Shader {
	shadersLen := ctx.GetProgrami(p, ATTACHED_SHADERS)
	if shadersLen == 0 {
		return nil
	}
	var n C.GLsizei
	buf := make([]C.GLuint, shadersLen)

	ctx.enqueue(call{
		args: C.struct_fnargs{
			fn: C.glfnGetAttachedShaders,
			a0: p.c(),
			a1: C.uintptr_t(shadersLen),
			a2: C.uintptr_t(uintptr(unsafe.Pointer(&n))),
			a3: C.uintptr_t(uintptr(unsafe.Pointer(&buf[0]))),
		},
		blocking: true,
	})

	buf = buf[:int(n)]
	shaders := make([]Shader, len(buf))
	for i, s := range buf {
		shaders[i] = Shader{Value: uint32(s)}
	}
	return shaders
}
Example #6
0
//export tableGetCell
func tableGetCell(data unsafe.Pointer, tnm *C.tableNM) C.LRESULT {
	t := (*table)(data)
	t.RLock()
	defer t.RUnlock()
	d := reflect.Indirect(reflect.ValueOf(t.data))
	datum := d.Index(int(tnm.row)).Field(int(tnm.column))
	switch {
	case datum.Type() == reflect.TypeOf((*image.RGBA)(nil)):
		i := datum.Interface().(*image.RGBA)
		hbitmap := C.toBitmap(unsafe.Pointer(i), C.intptr_t(i.Rect.Dx()), C.intptr_t(i.Rect.Dy()))
		bitmap := C.uintptr_t(uintptr(unsafe.Pointer(hbitmap)))
		t.freeLock.Lock()
		t.free[bitmap] = true // bitmap freed with C.freeBitmap()
		t.freeLock.Unlock()
		return C.LRESULT(bitmap)
	case datum.Kind() == reflect.Bool:
		if datum.Bool() == true {
			return C.TRUE
		}
		return C.FALSE
	default:
		s := fmt.Sprintf("%v", datum)
		text := C.uintptr_t(uintptr(unsafe.Pointer(toUTF16(s))))
		t.freeLock.Lock()
		t.free[text] = false // text freed with C.free()
		t.freeLock.Unlock()
		return C.LRESULT(text)
	}
}
Example #7
0
func (arg1 SwigcptrIp) Belong(arg2 Ip, arg3 Ip) (_swig_ret bool) {
	var swig_r bool
	_swig_i_0 := arg1
	_swig_i_1 := arg2.Swigcptr()
	_swig_i_2 := arg3.Swigcptr()
	swig_r = (bool)(C._wrap_ip_belong_iplookup_cd10677ac3a321f0(C.uintptr_t(_swig_i_0), C.uintptr_t(_swig_i_1), C.uintptr_t(_swig_i_2)))
	return swig_r
}
Example #8
0
func initAL() {
	err := mobileinit.RunOnJVM(func(vm, env, ctx uintptr) error {
		C.al_init(C.uintptr_t(vm), C.uintptr_t(env), C.jobject(ctx), &alHandle)
		if alHandle == nil {
			return errors.New("al: cannot load libopenal.so")
		}
		return nil
	})
	if err != nil {
		log.Fatalf("al: %v", err)
	}

	alEnableFunc = C.LPALENABLE(fn("alEnable"))
	alDisableFunc = C.LPALDISABLE(fn("alDisable"))
	alIsEnabledFunc = C.LPALISENABLED(fn("alIsEnabled"))
	alGetIntegerFunc = C.LPALGETINTEGER(fn("alGetInteger"))
	alGetIntegervFunc = C.LPALGETINTEGERV(fn("alGetIntegerv"))
	alGetFloatFunc = C.LPALGETFLOAT(fn("alGetFloat"))
	alGetFloatvFunc = C.LPALGETFLOATV(fn("alGetFloatv"))
	alGetBooleanFunc = C.LPALGETBOOLEAN(fn("alGetBoolean"))
	alGetBooleanvFunc = C.LPALGETBOOLEANV(fn("alGetBooleanv"))
	alGetStringFunc = C.LPALGETSTRING(fn("alGetString"))
	alDistanceModelFunc = C.LPALDISTANCEMODEL(fn("alDistanceModel"))
	alDopplerFactorFunc = C.LPALDOPPLERFACTOR(fn("alDopplerFactor"))
	alDopplerVelocityFunc = C.LPALDOPPLERVELOCITY(fn("alDopplerVelocity"))
	alSpeedOfSoundFunc = C.LPALSPEEDOFSOUND(fn("alSpeedOfSound"))
	alGetErrorFunc = C.LPALGETERROR(fn("alGetError"))
	alGenSourcesFunc = C.LPALGENSOURCES(fn("alGenSources"))
	alSourcePlayvFunc = C.LPALSOURCEPLAYV(fn("alSourcePlayv"))
	alSourcePausevFunc = C.LPALSOURCEPAUSEV(fn("alSourcePausev"))
	alSourceStopvFunc = C.LPALSOURCESTOPV(fn("alSourceStopv"))
	alSourceRewindvFunc = C.LPALSOURCEREWINDV(fn("alSourceRewindv"))
	alDeleteSourcesFunc = C.LPALDELETESOURCES(fn("alDeleteSources"))
	alGetSourceiFunc = C.LPALGETSOURCEI(fn("alGetSourcei"))
	alGetSourcefFunc = C.LPALGETSOURCEF(fn("alGetSourcef"))
	alGetSourcefvFunc = C.LPALGETSOURCEFV(fn("alGetSourcefv"))
	alSourceiFunc = C.LPALSOURCEI(fn("alSourcei"))
	alSourcefFunc = C.LPALSOURCEF(fn("alSourcef"))
	alSourcefvFunc = C.LPALSOURCEFV(fn("alSourcefv"))
	alSourceQueueBuffersFunc = C.LPALSOURCEQUEUEBUFFERS(fn("alSourceQueueBuffers"))
	alSourceUnqueueBuffersFunc = C.LPALSOURCEUNQUEUEBUFFERS(fn("alSourceUnqueueBuffers"))
	alGetListenerfFunc = C.LPALGETLISTENERF(fn("alGetListenerf"))
	alGetListenerfvFunc = C.LPALGETLISTENERFV(fn("alGetListenerfv"))
	alListenerfFunc = C.LPALLISTENERF(fn("alListenerf"))
	alListenerfvFunc = C.LPALLISTENERFV(fn("alListenerfv"))
	alGenBuffersFunc = C.LPALGENBUFFERS(fn("alGenBuffers"))
	alDeleteBuffersFunc = C.LPALDELETEBUFFERS(fn("alDeleteBuffers"))
	alGetBufferiFunc = C.LPALGETBUFFERI(fn("alGetBufferi"))
	alBufferDataFunc = C.LPALBUFFERDATA(fn("alBufferData"))
	alIsBufferFunc = C.LPALISBUFFER(fn("alIsBuffer"))

	alcGetErrorFunc = C.LPALCGETERROR(fn("alcGetError"))
	alcOpenDeviceFunc = C.LPALCOPENDEVICE(fn("alcOpenDevice"))
	alcCloseDeviceFunc = C.LPALCCLOSEDEVICE(fn("alcCloseDevice"))
	alcCreateContextFunc = C.LPALCCREATECONTEXT(fn("alcCreateContext"))
	alcMakeContextCurrentFunc = C.LPALCMAKECONTEXTCURRENT(fn("alcMakeContextCurrent"))
	alcDestroyContextFunc = C.LPALCDESTROYCONTEXT(fn("alcDestroyContext"))
}
Example #9
0
func (ctx *context) PolygonOffset(factor, units float32) {
	ctx.enqueue(call{
		args: C.struct_fnargs{
			fn: C.glfnPolygonOffset,
			a0: C.uintptr_t(math.Float32bits(factor)),
			a1: C.uintptr_t(math.Float32bits(units)),
		},
	})
}
Example #10
0
func (ctx *context) DepthRangef(n, f float32) {
	ctx.enqueue(call{
		args: C.struct_fnargs{
			fn: C.glfnDepthRangef,
			a0: C.uintptr_t(math.Float32bits(n)),
			a1: C.uintptr_t(math.Float32bits(f)),
		},
	})
}
Example #11
0
func (an *androidNotification) Close() {
	err := mobileinit.RunOnJVM(func(vm, env, ctx uintptr) error {
		C.notification_close(C.uintptr_t(vm), C.uintptr_t(env), C.uintptr_t(ctx), C.jint(an.id))
		return nil
	})
	if err != nil {
		log.Fatalf("Notification.Close: %v", err)
	}
}
Example #12
0
func assetInit() {
	err := mobileinit.RunOnJVM(func(vm, env, ctx uintptr) error {
		C.asset_manager_init(C.uintptr_t(vm), C.uintptr_t(env), C.jobject(ctx))
		return nil
	})
	if err != nil {
		log.Fatalf("asset: %v", err)
	}
}
Example #13
0
func (ctx *context) VertexAttrib2f(dst Attrib, x, y float32) {
	ctx.enqueue(call{
		args: C.struct_fnargs{
			fn: C.glfnVertexAttrib2f,
			a0: dst.c(),
			a1: C.uintptr_t(math.Float32bits(x)),
			a2: C.uintptr_t(math.Float32bits(y)),
		},
	})
}
Example #14
0
func (ctx *context) DrawArrays(mode Enum, first, count int) {
	ctx.enqueue(call{
		args: C.struct_fnargs{
			fn: C.glfnDrawArrays,
			a0: mode.c(),
			a1: C.uintptr_t(first),
			a2: C.uintptr_t(count),
		},
	})
}
Example #15
0
func (ctx *context) Uniform2i(dst Uniform, v0, v1 int) {
	ctx.enqueue(call{
		args: C.struct_fnargs{
			fn: C.glfnUniform2i,
			a0: dst.c(),
			a1: C.uintptr_t(v0),
			a2: C.uintptr_t(v1),
		},
	})
}
Example #16
0
func (ctx *context) Uniform2f(dst Uniform, v0, v1 float32) {
	ctx.enqueue(call{
		args: C.struct_fnargs{
			fn: C.glfnUniform2f,
			a0: dst.c(),
			a1: C.uintptr_t(math.Float32bits(v0)),
			a2: C.uintptr_t(math.Float32bits(v1)),
		},
	})
}
Example #17
0
func (ctx *context) StencilFunc(fn Enum, ref int, mask uint32) {
	ctx.enqueue(call{
		args: C.struct_fnargs{
			fn: C.glfnStencilFunc,
			a0: fn.c(),
			a1: C.uintptr_t(ref),
			a2: C.uintptr_t(mask),
		},
	})
}
Example #18
0
func stopLocationUpdates() {
	err := mobileinit.RunOnJVM(func(vm, env, ctx uintptr) error {
		C.location_manager_stopLocationUpdates(C.uintptr_t(vm), C.uintptr_t(env), C.uintptr_t(ctx))
		return nil
	})
	if err != nil {
		log.Fatalf("stopLocationUpdates: %v", err)
	}
	log.Print("stopLocationUpdates succeeded")
}
Example #19
0
func (ctx *context) Viewport(x, y, width, height int) {
	ctx.enqueue(call{
		args: C.struct_fnargs{
			fn: C.glfnViewport,
			a0: C.uintptr_t(x),
			a1: C.uintptr_t(y),
			a2: C.uintptr_t(width),
			a3: C.uintptr_t(height),
		},
	})
}
Example #20
0
func (ctx *context) UniformMatrix4fv(dst Uniform, src []float32) {
	ctx.enqueue(call{
		args: C.struct_fnargs{
			fn: C.glfnUniformMatrix4fv,
			a0: dst.c(),
			a1: C.uintptr_t(len(src) / 16),
			a2: C.uintptr_t(uintptr(unsafe.Pointer(&src[0]))),
		},
		blocking: true,
	})
}
Example #21
0
func hsCloseStream(stream hsStream, scratch hsScratch, onEvent hsMatchEventHandler, context interface{}) error {
	ctxt := &hsMatchEventContext{onEvent, context}

	ret := C.hs_close_stream_cgo(C.uintptr_t(uintptr(unsafe.Pointer(stream))), C.uintptr_t(uintptr(unsafe.Pointer(scratch))), C.uintptr_t(uintptr(unsafe.Pointer(ctxt))))

	if ret != C.HS_SUCCESS {
		return HsError(ret)
	}

	return nil
}
Example #22
0
func (u *uc) HookAdd(htype int, cb interface{}, extra ...uint64) (Hook, error) {
	var callback unsafe.Pointer
	var iarg1 C.int
	var uarg1, uarg2 C.uint64_t
	rangeMode := false
	switch htype {
	case HOOK_BLOCK, HOOK_CODE:
		rangeMode = true
		callback = C.hookCode_cgo
	case HOOK_MEM_READ, HOOK_MEM_WRITE, HOOK_MEM_READ | HOOK_MEM_WRITE:
		rangeMode = true
		callback = C.hookMemAccess_cgo
	case HOOK_INTR:
		callback = C.hookInterrupt_cgo
	case HOOK_INSN:
		iarg1 = C.int(extra[0])
		switch iarg1 {
		case X86_INS_IN:
			callback = C.hookX86In_cgo
		case X86_INS_OUT:
			callback = C.hookX86Out_cgo
		case X86_INS_SYSCALL, X86_INS_SYSENTER:
			callback = C.hookX86Syscall_cgo
		default:
			return 0, errors.New("Unknown instruction type.")
		}
	default:
		// special case for mask
		if htype&(HOOK_MEM_READ_UNMAPPED|HOOK_MEM_WRITE_UNMAPPED|HOOK_MEM_FETCH_UNMAPPED|
			HOOK_MEM_READ_PROT|HOOK_MEM_WRITE_PROT|HOOK_MEM_FETCH_PROT) != 0 {
			rangeMode = true
			callback = C.hookMemInvalid_cgo
		} else {
			return 0, errors.New("Unknown hook type.")
		}
	}
	var h2 C.uc_hook
	data := &HookData{u, cb}
	uptr := uintptr(unsafe.Pointer(data))
	if rangeMode {
		if len(extra) == 2 {
			uarg1 = C.uint64_t(extra[0])
			uarg2 = C.uint64_t(extra[1])
		} else {
			uarg1, uarg2 = 1, 0
		}
		C.uc_hook_add_u2(u.handle, &h2, C.uc_hook_type(htype), callback, C.uintptr_t(uptr), uarg1, uarg2)
	} else {
		C.uc_hook_add_i1(u.handle, &h2, C.uc_hook_type(htype), callback, C.uintptr_t(uptr), iarg1)
	}
	hookDataMap[uptr] = data
	hookToUintptr[Hook(h2)] = uptr
	return Hook(h2), nil
}
Example #23
0
func (ctx *context) RenderbufferStorage(target, internalFormat Enum, width, height int) {
	ctx.enqueue(call{
		args: C.struct_fnargs{
			fn: C.glfnRenderbufferStorage,
			a0: target.c(),
			a1: internalFormat.c(),
			a2: C.uintptr_t(width),
			a3: C.uintptr_t(height),
		},
	})
}
Example #24
0
func locationManagerInit() {
	err := mobileinit.RunOnJVM(func(vm, env, ctx uintptr) error {
		C.location_manager_init(C.uintptr_t(vm), C.uintptr_t(env), C.uintptr_t(ctx))
		return nil
	})
	if err != nil {
		log.Fatalf("location: %v", err)
	}
	locationManager = &androidLocationManager{}
	log.Print("location initialized")
}
Example #25
0
func (ctx *context) BlendColor(red, green, blue, alpha float32) {
	ctx.enqueue(call{
		args: C.struct_fnargs{
			fn: C.glfnBlendColor,
			a0: C.uintptr_t(math.Float32bits(red)),
			a1: C.uintptr_t(math.Float32bits(green)),
			a2: C.uintptr_t(math.Float32bits(blue)),
			a3: C.uintptr_t(math.Float32bits(alpha)),
		},
	})
}
Example #26
0
func (ctx *context) Scissor(x, y, width, height int32) {
	ctx.enqueue(call{
		args: C.struct_fnargs{
			fn: C.glfnScissor,
			a0: C.uintptr_t(x),
			a1: C.uintptr_t(y),
			a2: C.uintptr_t(width),
			a3: C.uintptr_t(height),
		},
	})
}
Example #27
0
func (ctx *context) DrawElements(mode Enum, count int, ty Enum, offset int) {
	ctx.enqueue(call{
		args: C.struct_fnargs{
			fn: C.glfnDrawElements,
			a0: mode.c(),
			a1: C.uintptr_t(count),
			a2: ty.c(),
			a3: C.uintptr_t(offset),
		},
	})
}
Example #28
0
func (ctx *context) Uniform4i(dst Uniform, v0, v1, v2, v3 int32) {
	ctx.enqueue(call{
		args: C.struct_fnargs{
			fn: C.glfnUniform4i,
			a0: dst.c(),
			a1: C.uintptr_t(v0),
			a2: C.uintptr_t(v1),
			a3: C.uintptr_t(v2),
			a4: C.uintptr_t(v3),
		},
	})
}
Example #29
0
func (ctx *context) UniformMatrix2fv(dst Uniform, src []float32) {
	ctx.enqueue(call{
		args: C.struct_fnargs{
			fn: C.glfnUniformMatrix2fv,
			// OpenGL ES 2 does not support transpose.
			a0: dst.c(),
			a1: C.uintptr_t(len(src) / 4),
			a2: C.uintptr_t(uintptr(unsafe.Pointer(&src[0]))),
		},
		blocking: true,
	})
}
Example #30
0
func (ctx *context) BufferSubData(target Enum, offset int, data []byte) {
	ctx.enqueue(call{
		args: C.struct_fnargs{
			fn: C.glfnBufferSubData,
			a0: target.c(),
			a1: C.uintptr_t(offset),
			a2: C.uintptr_t(len(data)),
			a3: (C.uintptr_t)(uintptr(unsafe.Pointer(&data[0]))),
		},
		blocking: true,
	})
}