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
17package com.android.wallpaper;
18
19import android.graphics.PixelFormat;
20import android.service.wallpaper.WallpaperService;
21import android.os.Bundle;
22import android.renderscript.RenderScriptGL;
23import android.renderscript.RenderScript;
24import android.view.SurfaceHolder;
25import android.view.Surface;
26
27public abstract class RenderScriptWallpaper<T extends RenderScriptScene> extends WallpaperService {
28    public Engine onCreateEngine() {
29        return new RenderScriptEngine();
30    }
31
32    protected abstract T createScene(int width, int height);
33
34    private class RenderScriptEngine extends Engine {
35        private RenderScriptGL mRs;
36        private T mRenderer;
37
38        @Override
39        public void onCreate(SurfaceHolder surfaceHolder) {
40            super.onCreate(surfaceHolder);
41            setTouchEventsEnabled(false);
42            surfaceHolder.setSizeFromLayout();
43
44            if (getResources().getBoolean(R.bool.use_32bit)) {
45                surfaceHolder.setFormat(PixelFormat.RGBX_8888);
46            }
47        }
48
49        @Override
50        public void onDestroy() {
51            super.onDestroy();
52            destroyRenderer();
53        }
54
55        private void destroyRenderer() {
56            if (mRenderer != null) {
57                mRenderer.stop();
58                mRenderer = null;
59            }
60            if (mRs != null) {
61                mRs.destroy();
62                mRs = null;
63            }
64        }
65
66        @Override
67        public void onVisibilityChanged(boolean visible) {
68            super.onVisibilityChanged(visible);
69            if (mRenderer != null) {
70                if (visible) {
71                    mRenderer.start();
72                } else {
73                    mRenderer.stop();
74                }
75            }
76        }
77
78        @Override
79        public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
80            super.onSurfaceChanged(holder, format, width, height);
81            if (mRs != null) {
82                mRs.contextSetSurface(width, height, holder.getSurface());
83            }
84            if (mRenderer == null) {
85                mRenderer = createScene(width, height);
86                mRenderer.init(mRs, getResources(), isPreview());
87                mRenderer.start();
88            } else {
89                mRenderer.resize(width, height);
90            }
91        }
92
93        @Override
94        public void onOffsetsChanged(float xOffset, float yOffset,
95                float xStep, float yStep, int xPixels, int yPixels) {
96            mRenderer.setOffset(xOffset, yOffset, xPixels, yPixels);
97        }
98
99        @Override
100        public void onSurfaceCreated(SurfaceHolder holder) {
101            super.onSurfaceCreated(holder);
102
103            Surface surface = null;
104            while (surface == null) {
105                surface = holder.getSurface();
106            }
107            mRs = new RenderScriptGL(false, false);
108            mRs.contextSetPriority(RenderScript.Priority.LOW);
109        }
110
111        @Override
112        public void onSurfaceDestroyed(SurfaceHolder holder) {
113            super.onSurfaceDestroyed(holder);
114            destroyRenderer();
115        }
116
117        @Override
118        public Bundle onCommand(String action, int x, int y, int z,
119                Bundle extras, boolean resultRequested) {
120            return mRenderer.onCommand(action, x, y, z, extras, resultRequested);
121        }
122
123    }
124}
125