RenderScriptWallpaper.java revision 7eb6bcc2cbc062abea4f3c9d3e542215697c7b66
1/* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 18package com.android.musicvis; 19 20 21import android.service.wallpaper.WallpaperService; 22import android.os.Bundle; 23import android.renderscript.RenderScriptGL; 24import android.renderscript.RenderScript; 25import android.util.Log; 26import android.view.MotionEvent; 27import android.view.SurfaceHolder; 28import android.view.Surface; 29 30public abstract class RenderScriptWallpaper<T extends RenderScriptScene> extends WallpaperService { 31 public Engine onCreateEngine() { 32 return new RenderScriptEngine(); 33 } 34 35 protected abstract T createScene(int width, int height); 36 37 private class RenderScriptEngine extends Engine { 38 private RenderScriptGL mRs; 39 private T mRenderer; 40 41 @Override 42 public void onCreate(SurfaceHolder surfaceHolder) { 43 super.onCreate(surfaceHolder); 44 setTouchEventsEnabled(false); 45 surfaceHolder.setSizeFromLayout(); 46 } 47 48 @Override 49 public void onDestroy() { 50 super.onDestroy(); 51 destroyRenderer(); 52 } 53 54 private void destroyRenderer() { 55 if (mRenderer != null) { 56 mRenderer.stop(); 57 mRenderer = null; 58 } 59 if (mRs != null) { 60 mRs.destroy(); 61 mRs = null; 62 } 63 } 64 65 @Override 66 public void onVisibilityChanged(boolean visible) { 67 super.onVisibilityChanged(visible); 68 if (mRenderer != null) { 69 if (visible) { 70 mRenderer.start(); 71 } else { 72 mRenderer.stop(); 73 } 74 } 75 } 76 77 @Override 78 public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) { 79 super.onSurfaceChanged(holder, format, width, height); 80 if (mRs != null) { 81 mRs.contextSetSurface(width, height, holder.getSurface()); 82 } 83 if (mRenderer == null) { 84 mRenderer = createScene(width, height); 85 mRenderer.init(mRs, getResources(), isPreview()); 86 mRenderer.start(); 87 } else { 88 mRenderer.resize(width, height); 89 } 90 } 91 92 /*@Override 93 public void onTouchEvent(MotionEvent event) { 94 mRenderer.onTouchEvent(event); 95 }*/ 96 97 @Override 98 public void onOffsetsChanged(float xOffset, float yOffset, 99 float xStep, float yStep, int xPixels, int yPixels) { 100 mRenderer.setOffset(xOffset, yOffset, xPixels, yPixels); 101 } 102 103 @Override 104 public void onSurfaceCreated(SurfaceHolder holder) { 105 super.onSurfaceCreated(holder); 106 107 Surface surface = null; 108 while (surface == null) { 109 surface = holder.getSurface(); 110 } 111 mRs = new RenderScriptGL(false, false); 112 mRs.contextSetPriority(RenderScript.Priority.LOW); 113 } 114 115 @Override 116 public void onSurfaceDestroyed(SurfaceHolder holder) { 117 super.onSurfaceDestroyed(holder); 118 destroyRenderer(); 119 } 120 121 @Override 122 public Bundle onCommand(String action, int x, int y, int z, 123 Bundle extras, boolean resultRequested) { 124 return mRenderer.onCommand(action, x, y, z, extras, resultRequested); 125 } 126 127 } 128} 129