aboutsummaryrefslogtreecommitdiffhomepage
path: root/android_sample
diff options
context:
space:
mode:
authorGravatar djsollen@google.com <djsollen@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2011-06-13 16:58:40 +0000
committerGravatar djsollen@google.com <djsollen@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2011-06-13 16:58:40 +0000
commite32b5837c3f35b80502759f3de3e8cadf6625f4e (patch)
tree9073864ddc2d7ec2d5b83fb1c318218f41dc7d89 /android_sample
parentfc0685652ea1617b93aed84c7674fcafd55a30f6 (diff)
Updates to the Android SampleApp
Diffstat (limited to 'android_sample')
-rw-r--r--android_sample/SampleApp/Android.mk23
-rw-r--r--android_sample/SampleApp/AndroidManifest.xml2
-rw-r--r--android_sample/SampleApp/jni/sample-jni.cpp61
-rw-r--r--android_sample/SampleApp/res/menu/sample.xml30
-rw-r--r--android_sample/SampleApp/src/com/skia/sampleapp/SampleApp.java149
-rw-r--r--android_sample/SampleApp/src/com/skia/sampleapp/SampleView.java111
6 files changed, 304 insertions, 72 deletions
diff --git a/android_sample/SampleApp/Android.mk b/android_sample/SampleApp/Android.mk
index 4df3defe43..59f69936b0 100644
--- a/android_sample/SampleApp/Android.mk
+++ b/android_sample/SampleApp/Android.mk
@@ -30,14 +30,13 @@ LOCAL_C_INCLUDES += \
external/skia/include/effects \
external/skia/include/images \
external/skia/include/utils \
- $(LOCAL_PATH)/skia_extra/include/views \
- $(LOCAL_PATH)/skia_extra/samplecode \
- $(LOCAL_PATH)/skia_extra/include/xml \
+ external/skia/include/utils/android \
+ external/skia/include/views \
+ external/skia/samplecode \
+ external/skia/include/xml \
external/skia/include/gpu \
external/skia/src/core \
external/skia/gpu/include \
- frameworks/base/core/jni/android/graphics \
- frameworks/base/native/include/android \
$(LOCAL_PATH)/jni
LOCAL_SHARED_LIBRARIES := \
@@ -55,16 +54,16 @@ LOCAL_PRELINK_MODULE := false
LOCAL_MODULE := libskia-sample
LOCAL_SRC_FILES := \
- skia_extra/src/ports/SkXMLParser_empty.cpp \
+ ../../src/ports/SkXMLParser_empty.cpp \
jni/sample-jni.cpp
-include $(LOCAL_PATH)/skia_extra/src/views/views_files.mk
-LOCAL_SRC_FILES += $(addprefix skia_extra/src/views/, $(SOURCE))
+include external/skia/src/views/views_files.mk
+LOCAL_SRC_FILES += $(addprefix ../../src/views/, $(SOURCE))
-include $(LOCAL_PATH)/skia_extra/src/xml/xml_files.mk
-LOCAL_SRC_FILES += $(addprefix skia_extra/src/xml/, $(SOURCE))
+include external/skia/src/xml/xml_files.mk
+LOCAL_SRC_FILES += $(addprefix ../../src/xml/, $(SOURCE))
-include $(LOCAL_PATH)/skia_extra/samplecode/samplecode_files.mk
-LOCAL_SRC_FILES += $(addprefix skia_extra/samplecode/, $(SOURCE))
+include external/skia/samplecode/samplecode_files.mk
+LOCAL_SRC_FILES += $(addprefix ../../samplecode/, $(SOURCE))
include $(BUILD_SHARED_LIBRARY)
diff --git a/android_sample/SampleApp/AndroidManifest.xml b/android_sample/SampleApp/AndroidManifest.xml
index e3232468f6..071ae03390 100644
--- a/android_sample/SampleApp/AndroidManifest.xml
+++ b/android_sample/SampleApp/AndroidManifest.xml
@@ -21,7 +21,7 @@
<application android:label="@string/app_name"
android:debuggable="true">
<activity android:name=".SampleApp"
- android:theme="@android:style/Theme.Holo.NoActionBar"
+ android:theme="@android:style/Theme.Holo.Light"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
diff --git a/android_sample/SampleApp/jni/sample-jni.cpp b/android_sample/SampleApp/jni/sample-jni.cpp
index f8490b879a..86d07a85b8 100644
--- a/android_sample/SampleApp/jni/sample-jni.cpp
+++ b/android_sample/SampleApp/jni/sample-jni.cpp
@@ -18,11 +18,14 @@
#include <jni.h>
#include "SkCanvas.h"
-#include "GraphicsJNI.h"
#include "SkEvent.h"
#include "SkWindow.h"
#include "SkApplication.h"
-#include "AndroidKeyToSkKey.h"
+#include "utils/android/AndroidKeyToSkKey.h"
+
+#include "SkDevice.h"
+#include "SkGpuCanvas.h"
+#include "GrContext.h"
///////////////////////////////////////////
///////////////// Globals /////////////////
@@ -70,8 +73,7 @@ void SkOSWindow::onHandleInval(const SkIRect& rect)
if (!gActivityGlue.m_env || !gWindowGlue.m_inval || !gWindowGlue.m_obj) {
return;
}
- gActivityGlue.m_env->CallVoidMethod(gWindowGlue.m_obj, gWindowGlue.m_inval,
- rect.fLeft, rect.fTop, rect.fRight, rect.fBottom);
+ gActivityGlue.m_env->CallVoidMethod(gWindowGlue.m_obj, gWindowGlue.m_inval);
}
///////////////////////////////////////////
@@ -95,8 +97,8 @@ static jmethodID GetJMethod(JNIEnv* env, jclass clazz, const char name[],
}
extern "C" {
-JNIEXPORT void JNICALL Java_com_skia_sampleapp_SampleApp_drawToCanvas(
- JNIEnv* env, jobject thiz, jobject jcanvas);
+JNIEXPORT void JNICALL Java_com_skia_sampleapp_SampleApp_draw(
+ JNIEnv* env, jobject thiz);
JNIEXPORT void JNICALL Java_com_skia_sampleapp_SampleApp_init(
JNIEnv* env, jobject thiz);
JNIEXPORT void JNICALL Java_com_skia_sampleapp_SampleApp_term(
@@ -111,6 +113,8 @@ JNIEXPORT void JNICALL Java_com_skia_sampleapp_SampleApp_handleClick(
JNIEnv* env, jobject thiz, jint x, jint y, jint state);
JNIEXPORT void JNICALL Java_com_skia_sampleapp_SampleApp_createOSWindow(
JNIEnv* env, jobject thiz, jobject jsampleView);
+JNIEXPORT void JNICALL Java_com_skia_sampleapp_SampleApp_zoom(
+ JNIEnv* env, jobject thiz, jfloat factor);
};
JNIEXPORT bool JNICALL Java_com_skia_sampleapp_SampleApp_handleKeyDown(
@@ -160,10 +164,10 @@ JNIEXPORT void JNICALL Java_com_skia_sampleapp_SampleApp_createOSWindow(
{
gWindow = create_sk_window(NULL);
// Only using a method on View.
- jclass clazz = gActivityGlue.m_env->FindClass("android/view/View");
+ jclass clazz = gActivityGlue.m_env->FindClass("android/opengl/GLSurfaceView");
gWindowGlue.m_obj = gActivityGlue.m_env->NewWeakGlobalRef(jsampleView);
- gWindowGlue.m_inval = GetJMethod(gActivityGlue.m_env, clazz, "invalidate",
- "(IIII)V");
+ gWindowGlue.m_inval = GetJMethod(gActivityGlue.m_env, clazz, "requestRender",
+ "()V");
gActivityGlue.m_env->DeleteLocalRef(clazz);
}
@@ -198,11 +202,42 @@ JNIEXPORT void JNICALL Java_com_skia_sampleapp_SampleApp_term(JNIEnv* env,
}
-JNIEXPORT void JNICALL Java_com_skia_sampleapp_SampleApp_drawToCanvas(
- JNIEnv* env, jobject thiz, jobject jcanvas)
+JNIEXPORT void JNICALL Java_com_skia_sampleapp_SampleApp_draw(
+ JNIEnv* env, jobject thiz)
{
if (!gWindow) return;
gWindow->update(NULL);
- SkCanvas* canvas = GraphicsJNI::getNativeCanvas(env, jcanvas);
- canvas->drawBitmap(gWindow->getBitmap(), 0, 0);
+
+ // Copy the bitmap to the screen in raster mode
+ if (!gWindow->drawsToHardware()) {
+
+ SkBitmap bitmap = gWindow->getBitmap();
+
+ GrContext* context = gWindow->getGrContext();
+ if (!context) {
+ context = GrContext::Create(kOpenGL_Shaders_GrEngine, NULL);
+ if (!context || !gWindow->setGrContext(context)) {
+ return;
+ }
+ context->unref();
+ }
+ GrRenderTarget* renderTarget = context->createRenderTargetFrom3DApiState();
+ SkGpuCanvas* gpuCanvas = new SkGpuCanvas(context, renderTarget);
+
+ SkDevice* device = gpuCanvas->createDevice(SkBitmap::kARGB_8888_Config,
+ bitmap.width(), bitmap.height(),
+ false, false);
+ gpuCanvas->setDevice(device)->unref();
+ gpuCanvas->drawBitmap(bitmap, 0, 0);
+
+ SkSafeUnref(renderTarget);
+ SkSafeUnref(gpuCanvas);
+ }
}
+
+JNIEXPORT void JNICALL Java_com_skia_sampleapp_SampleApp_zoom(
+ JNIEnv* env, jobject thiz, jfloat factor)
+{
+ gWindow->changeZoomLevel(factor);
+}
+
diff --git a/android_sample/SampleApp/res/menu/sample.xml b/android_sample/SampleApp/res/menu/sample.xml
new file mode 100644
index 0000000000..49f7ebb091
--- /dev/null
+++ b/android_sample/SampleApp/res/menu/sample.xml
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2011 Skia
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+
+<menu xmlns:android="http://schemas.android.com/apk/res/android">
+ <item
+ android:id="@+id/next"
+ android:showAsAction="always"
+ android:icon="@*android:drawable/ic_btn_find_next"
+ />
+ <item
+ android:id="@+id/overview"
+ android:title="@string/overview"
+ />
+<!--
+ android:icon="@drawable/ic_menu_new_window"
+-->
+</menu>
diff --git a/android_sample/SampleApp/src/com/skia/sampleapp/SampleApp.java b/android_sample/SampleApp/src/com/skia/sampleapp/SampleApp.java
index b02c4d7fe4..fe733603eb 100644
--- a/android_sample/SampleApp/src/com/skia/sampleapp/SampleApp.java
+++ b/android_sample/SampleApp/src/com/skia/sampleapp/SampleApp.java
@@ -19,94 +19,151 @@ package com.skia.sampleapp;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
+import android.opengl.GLSurfaceView;
import android.os.Bundle;
+import android.os.Handler;
+import android.os.Message;
import android.util.AttributeSet;
import android.view.KeyEvent;
+import android.view.Menu;
+import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
+import javax.microedition.khronos.egl.EGLConfig;
+import javax.microedition.khronos.opengles.GL10;
+
public class SampleApp extends Activity
{
private TextView mTitle;
-
- public class SampleView extends View {
- public SampleView(Context context) {
- super(context);
- createOSWindow(this);
- }
-
- @Override
- protected void onDraw(Canvas canvas) {
- drawToCanvas(canvas);
- }
-
- @Override
- protected void onSizeChanged(int w, int h, int oldw, int oldh) {
- updateSize(w, h);
- }
-
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- final int x = (int) event.getX();
- final int y = (int) event.getY();
- final int action = event.getAction();
- handleClick(x, y, action);
- return true;
- }
- }
+ private SampleView mView;
@Override
- public void onCreate(Bundle savedInstanceState)
- {
+ public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
- init();
setContentView(R.layout.layout);
mTitle = (TextView) findViewById(R.id.title_view);
LinearLayout holder = (LinearLayout) findViewById(R.id.holder);
- View view = new SampleView(this);
- holder.addView(view, new LinearLayout.LayoutParams(
+ mView = new SampleView(this);
+
+ holder.addView(mView, new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
+
+ mTitle.setVisibility(View.GONE);
+ getActionBar().setDisplayShowHomeEnabled(false);
+
}
@Override
- public void onDestroy()
- {
- term();
+ public void onDestroy() {
+ mView.queueEvent(new Runnable() {
+ @Override
+ public void run() {
+ term();
+ }
+ });
super.onDestroy();
}
@Override
+ public boolean onCreateOptionsMenu(Menu menu) {
+ getMenuInflater().inflate(R.menu.sample, menu);
+ return true;
+ }
+
+ @Override
+ public boolean onOptionsItemSelected(MenuItem item) {
+ switch (item.getItemId()) {
+ case R.id.overview:
+ mView.queueEvent(new Runnable() {
+ @Override
+ public void run() {
+ handleKeyDown(KeyEvent.KEYCODE_BACK, 0);
+ }
+ });
+ return true;
+ case R.id.next:
+ mView.queueEvent(new Runnable() {
+ @Override
+ public void run() {
+ handleKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, 0);
+ }
+ });
+ return true;
+ default:
+ return false;
+ }
+ }
+
+ @Override
public boolean dispatchKeyEvent(KeyEvent event) {
+ final int keycode = event.getKeyCode();
+ if (keycode == KeyEvent.KEYCODE_BACK) {
+ if (event.getAction() == KeyEvent.ACTION_UP) {
+ finish();
+ }
+ return true;
+ }
switch (event.getAction()) {
case KeyEvent.ACTION_DOWN:
- int uni = event.getUnicodeChar(event.getMetaState());
- return handleKeyDown(event.getKeyCode(), uni);
+ final int uni = event.getUnicodeChar(event.getMetaState());
+ mView.queueEvent(new Runnable() {
+ @Override
+ public void run() {
+ handleKeyDown(keycode, uni);
+ }
+ });
+
+ return true;
case KeyEvent.ACTION_UP:
- return handleKeyUp(event.getKeyCode());
+ mView.queueEvent(new Runnable() {
+ @Override
+ public void run() {
+ handleKeyUp(keycode);
+ }
+ });
+ return true;
default:
return false;
}
}
+ private static final int SET_TITLE = 1;
+
+ private Handler mHandler = new Handler() {
+ @Override
+ public void handleMessage(Message msg) {
+ switch (msg.what) {
+ case SET_TITLE:
+ mTitle.setText((String) msg.obj);
+ SampleApp.this.getActionBar().setSubtitle((String) msg.obj);
+ break;
+ default:
+ break;
+ }
+ }
+ };
+
@Override
public void setTitle(CharSequence title) {
- mTitle.setText(title);
+ mHandler.obtainMessage(SET_TITLE, title).sendToTarget();
}
- private native void drawToCanvas(Canvas canvas);
- private native void init();
- private native void term();
+ native void draw();
+ native void init();
+ native void term();
// Currently depends on init having already been called.
- private native void createOSWindow(SampleView view);
- private native void updateSize(int w, int h);
- private native void handleClick(int x, int y, int state);
- private native boolean handleKeyDown(int key, int uni);
- private native boolean handleKeyUp(int key);
+ native void createOSWindow(GLSurfaceView view);
+ native void updateSize(int w, int h);
+ native void handleClick(int x, int y, int state);
+ native boolean handleKeyDown(int key, int uni);
+ native boolean handleKeyUp(int key);
+ native void zoom(float factor);
static {
System.loadLibrary("skia-sample");
diff --git a/android_sample/SampleApp/src/com/skia/sampleapp/SampleView.java b/android_sample/SampleApp/src/com/skia/sampleapp/SampleView.java
new file mode 100644
index 0000000000..0d76dcea93
--- /dev/null
+++ b/android_sample/SampleApp/src/com/skia/sampleapp/SampleView.java
@@ -0,0 +1,111 @@
+/*
+ * Copyright (C) 2011 Skia
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.skia.sampleapp;
+
+import android.app.Activity;
+import android.content.Context;
+import android.graphics.Canvas;
+import android.opengl.GLSurfaceView;
+import android.os.Bundle;
+import android.os.Handler;
+import android.os.Message;
+import android.util.AttributeSet;
+import android.view.KeyEvent;
+import android.view.MotionEvent;
+import android.view.ScaleGestureDetector;
+import android.view.ScaleGestureDetector.OnScaleGestureListener;
+
+import javax.microedition.khronos.egl.EGLConfig;
+import javax.microedition.khronos.opengles.GL10;
+
+public class SampleView extends GLSurfaceView implements OnScaleGestureListener {
+
+ private final SampleApp mApp;
+ private ScaleGestureDetector mDetector;
+ public SampleView(SampleApp app) {
+ super(app);
+ mApp = app;
+ setEGLContextClientVersion(2);
+ setEGLConfigChooser(8,8,8,8,0,8);
+ setRenderer(new SampleView.Renderer());
+ setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
+ mDetector = new ScaleGestureDetector(app, this);
+ }
+
+ @Override
+ public boolean onTouchEvent(MotionEvent event) {
+ mDetector.onTouchEvent(event);
+ if (mDetector.isInProgress()) {
+ return true;
+ }
+
+ final int x = (int) event.getX();
+ final int y = (int) event.getY();
+ final int action = event.getAction();
+ queueEvent(new Runnable() {
+ @Override
+ public void run() {
+ mApp.handleClick(x, y, action);
+ }
+ });
+
+ return true;
+ }
+ // ScaleGestureDetector.OnScaleGestureListener implementation
+ @Override
+ public boolean onScaleBegin(ScaleGestureDetector detector) {
+ return true;
+ }
+
+ @Override
+ public boolean onScale(ScaleGestureDetector detector) {
+ if (detector.getScaleFactor() != 1) {
+ final float difference = detector.getCurrentSpan() - detector.getPreviousSpan();
+ queueEvent(new Runnable() {
+ @Override
+ public void run() {
+ mApp.zoom(difference * .03f);
+ }
+ });
+
+ return true;
+ }
+ return false;
+ }
+
+ @Override
+ public void onScaleEnd(ScaleGestureDetector detector) {
+
+ }
+
+ private class Renderer implements GLSurfaceView.Renderer {
+ public void onDrawFrame(GL10 gl) {
+ mApp.draw();
+ }
+
+ public void onSurfaceChanged(GL10 gl, int width, int height) {
+ mApp.updateSize(width, height);
+ }
+
+ public void onSurfaceCreated(GL10 gl, EGLConfig config) {
+ gl.glClearStencil(0);
+ gl.glClear(gl.GL_STENCIL_BUFFER_BIT);
+ mApp.init();
+ mApp.createOSWindow(SampleView.this);
+ }
+ }
+} \ No newline at end of file