1
0
Fork 0
forked from len0rd/rockbox
foxbox/android/src/org/rockbox/RockboxFramebuffer.java
Thomas Martitz 901521d6de android: lcd_update/_rect() changes
* rename some java methods (to update(), initialize())
* re-create the ByteBuffer object from the framebuffer on every update.
  This is needed now since 2c71aa9 added the possiblity for lcd_framebuffer to change.
* do so, along with the creation of the dirty Rect object, in native code.

Change-Id: Id39ea8e4b6148987c5f216a87e0ff3c8e7babe92
2012-03-22 23:26:50 +01:00

142 lines
4.4 KiB
Java

/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (C) 2010 Thomas Martitz
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
package org.rockbox;
import java.nio.ByteBuffer;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.ViewConfiguration;
public class RockboxFramebuffer extends SurfaceView
implements SurfaceHolder.Callback
{
private final DisplayMetrics metrics;
private final ViewConfiguration view_config;
private Bitmap btm;
/* first stage init; needs to run from a thread that has a Looper
* setup stuff that needs a Context */
public RockboxFramebuffer(Context c)
{
super(c);
metrics = c.getResources().getDisplayMetrics();
view_config = ViewConfiguration.get(c);
getHolder().addCallback(this);
/* Needed so we can catch KeyEvents */
setFocusable(true);
setFocusableInTouchMode(true);
setClickable(true);
/* don't draw until native is ready (2nd stage) */
setEnabled(false);
}
/* second stage init; called from Rockbox with information about the
* display framebuffer */
private void initialize(int lcd_width, int lcd_height)
{
btm = Bitmap.createBitmap(lcd_width, lcd_height, Bitmap.Config.RGB_565);
setEnabled(true);
}
private void update(ByteBuffer framebuffer)
{
SurfaceHolder holder = getHolder();
Canvas c = holder.lockCanvas();
btm.copyPixelsFromBuffer(framebuffer);
synchronized (holder)
{ /* draw */
c.drawBitmap(btm, 0.0f, 0.0f, null);
}
holder.unlockCanvasAndPost(c);
}
private void update(ByteBuffer framebuffer, Rect dirty)
{
SurfaceHolder holder = getHolder();
Canvas c = holder.lockCanvas(dirty);
/* can't copy a partial buffer, but it doesn't make a noticeable difference anyway */
btm.copyPixelsFromBuffer(framebuffer);
synchronized (holder)
{ /* draw */
c.drawBitmap(btm, dirty, dirty, null);
}
holder.unlockCanvasAndPost(c);
}
public boolean onTouchEvent(MotionEvent me)
{
int x = (int) me.getX();
int y = (int) me.getY();
switch (me.getAction())
{
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
touchHandler(false, x, y);
return true;
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_DOWN:
touchHandler(true, x, y);
return true;
}
return false;
}
public boolean onKeyDown(int keyCode, KeyEvent event)
{
return buttonHandler(keyCode, true);
}
public boolean onKeyUp(int keyCode, KeyEvent event)
{
return buttonHandler(keyCode, false);
}
private int getDpi()
{
return metrics.densityDpi;
}
private int getScrollThreshold()
{
return view_config.getScaledTouchSlop();
}
private native void touchHandler(boolean down, int x, int y);
public native static boolean buttonHandler(int keycode, boolean state);
public native void surfaceCreated(SurfaceHolder holder);
public native void surfaceDestroyed(SurfaceHolder holder);
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
}
}