/
RockboxFramebuffer.java
296 lines (252 loc) · 9.98 KB
/
RockboxFramebuffer.java
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
/***************************************************************************
* __________ __ ___.
* 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.graphics.Matrix;
import android.graphics.Paint;
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=null;
private int srcWidth=0,srcHeight=0, desWidth, desHeight,paddingHeight=0,paddingWidth=0;
private int fixedWidth, fixedHeight;
private float scaleWidthFactor,scaleHeightFactor;
private boolean statusBarOn = false;
private boolean fastScale =false;
private boolean keepAspectRatio =false;
private Matrix myMatrix=null;
private Paint myPaint=null;
private boolean downscaled = false;
/* 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);
desWidth = metrics.widthPixels;
desHeight = metrics.heightPixels;
}
/* srcWidth, srcHeight: static value from Rockbox internal frambuffer */
/* desWidth, desHeight: read from the current running android device */
/* paddingWidth, paddingHeight: the size of black bar when keep scale aspect ratio.*/
/* fixedWidth, fixedHeight the sum of srcWidth+paddingWidth, srcHeight+paddingHeight */
private void initialize(int lcd_width, int lcd_height)
{
srcWidth = lcd_width;
srcHeight = lcd_height;
scaleWidthFactor = ((float)desWidth) / srcWidth;
scaleHeightFactor = ((float)desHeight) / srcHeight;
checkDisplayConfig();
getHolder().setFormat(4); /* RGB_565 */
btm = Bitmap.createBitmap(srcWidth, srcHeight, Bitmap.Config.RGB_565);
setEnabled(true);
}
private void checkDisplayConfig()
{
downscaled = (scaleWidthFactor < 1 || scaleHeightFactor < 1)?true:false;
fastScale = (!RockboxApp.getInstance().getRockboxFastScaleMode())?true:false;
statusBarOn = (!RockboxApp.getInstance().getTitlebarStatus())?true:false;
keepAspectRatio = (!RockboxApp.getInstance().getRockboxScaleKeepAspect())?true:false;
if (fastScale == false && keepAspectRatio ==true)
{
if (scaleWidthFactor > scaleHeightFactor && scaleWidthFactor >1)
scaleWidthFactor = scaleHeightFactor;
else
scaleHeightFactor = scaleWidthFactor;
}
if (keepAspectRatio == true)
{
if (scaleHeightFactor > scaleWidthFactor)
paddingHeight = (int)Math.ceil(srcWidth * desHeight / desWidth) - srcHeight;
if ( scaleWidthFactor > scaleHeightFactor)
paddingWidth = (int)Math.ceil(srcHeight * desWidth / desHeight) - srcWidth;
}
fixedWidth = srcWidth+paddingWidth;
fixedHeight = (statusBarOn == true)?srcHeight-(int)(getStatusBarHeight()/scaleHeightFactor)+paddingHeight
: srcHeight+paddingHeight;
if (fastScale == true) /* surface setFixedSize, emulate the screen resolution no matter what actual LCD resolution is */
{
getHolder().setFixedSize(fixedWidth,fixedHeight);
}
else
{
myMatrix = new Matrix();
myMatrix.postScale(scaleWidthFactor,scaleHeightFactor);
/* note. upscaling with Paint flags is very slow. */
myPaint =(scaleWidthFactor > 1 || scaleHeightFactor > 1)?null:
new Paint(Paint.ANTI_ALIAS_FLAG| Paint.DITHER_FLAG| Paint.FILTER_BITMAP_FLAG);
}
}
private boolean isBtmAlive()
{
if (btm.isRecycled() == true || btm == null)
{
if (srcWidth !=0 && srcHeight !=0)
btm = Bitmap.createBitmap(srcWidth, srcHeight, Bitmap.Config.RGB_565);
else
return false;
}
return true;
}
private void update(ByteBuffer framebuffer)
{
SurfaceHolder holder = getHolder();
Canvas c;
Rect dirty = new Rect();
if (isBtmAlive() == false)
return;
if (downscaled && fastScale == true) //downscale
{
/* (bug!)getHolder may returned a SurfaceHolder with wrong size, ignoring the previous setFixedSize call. (pre-Kitkat) */
/* needs to menually setFixedSize again, and lock update area according to source width and height. */
holder.setFixedSize(fixedWidth,fixedHeight);
dirty.set(0,0,fixedWidth,fixedHeight);
}
else
dirty = null;
c = holder.lockCanvas(dirty);
if (c == null)
return;
btm.copyPixelsFromBuffer(framebuffer);
synchronized (holder)
{ /* draw */
if (fastScale == true)
c.drawBitmap(btm, 0.0f, 0.0f, null);
else
c.drawBitmap(btm,myMatrix,myPaint);
}
holder.unlockCanvasAndPost(c);
}
private void update(ByteBuffer framebuffer, Rect dirty)
{
SurfaceHolder holder = getHolder();
Rect scaledDirty = new Rect();
if (fastScale == true)
{
scaledDirty = dirty;
if (downscaled) /* downscale */
holder.setFixedSize(fixedWidth,fixedHeight);
}
else
scaledDirty.set((int)(dirty.left * scaleWidthFactor), (int)(dirty.top * scaleHeightFactor),
(int)(dirty.right * scaleWidthFactor), (int)(dirty.bottom * scaleHeightFactor));
if (isBtmAlive() == false)
return;
Canvas c = holder.lockCanvas(scaledDirty);
if (c == null)
return;
/* can't copy a partial buffer, but it doesn't make a noticeable difference anyway */
btm.copyPixelsFromBuffer(framebuffer);
synchronized (holder)
{ /* draw */
if (fastScale == true)
c.drawBitmap(btm, 0.0f, 0.0f, null);
else
c.drawBitmap(btm,myMatrix,myPaint);
}
holder.unlockCanvasAndPost(c);
}
public boolean onTouchEvent(MotionEvent me)
{
int x,y;
if (statusBarOn) /*need to fix the xy touched coordinates */
{
int[] coords = new int[2];
this.getLocationInWindow(coords);
x = (int)me.getRawX() - coords[0];
y = (int)me.getRawY() - coords[1];
}
else
{
x = (int) me.getRawX();
y = (int) me.getRawY();
}
/*convert */
x = (paddingWidth > 0) ? (int)( x / scaleHeightFactor):(int)( x / scaleWidthFactor);
y = (paddingHeight > 0) ? (int)( y / scaleWidthFactor) :(int)( y / scaleHeightFactor);
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()
{
if (desHeight > srcHeight)
return (int)(metrics.densityDpi / scaleHeightFactor);
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)
{
}
public int getStatusBarHeight()
{
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
}