1/*
2 * Copyright 2009, The Android Open Source Project
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *  * Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 *  * Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25package com.android.sampleplugin;
26
27import android.content.Context;
28import android.graphics.PixelFormat;
29import android.view.SurfaceHolder;
30import android.view.SurfaceView;
31import android.view.SurfaceHolder.Callback;
32
33public class PaintSurface extends SurfaceView {
34
35    static {
36        //needed for jni calls
37        System.loadLibrary("sampleplugin");
38    }
39
40    private final int npp;
41
42    private boolean validNPP = true;
43    private Object nppLock = new Object();
44
45    public PaintSurface(Context context, int NPP, int width, int height) {
46        super(context);
47
48        this.npp = NPP;
49
50        this.getHolder().setFormat(PixelFormat.RGBA_8888);
51        this.getHolder().addCallback(new Callback() {
52
53            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
54                synchronized (nppLock) {
55                    if (validNPP) {
56                        nativeSurfaceChanged(npp, format, width, height);
57                    }
58                }
59            }
60
61            public void surfaceCreated(SurfaceHolder holder) {
62                synchronized (nppLock) {
63                    if (validNPP) {
64                        nativeSurfaceCreated(npp);
65                    }
66                }
67            }
68
69            public void surfaceDestroyed(SurfaceHolder holder) {
70                synchronized (nppLock) {
71                    if (validNPP) {
72                        nativeSurfaceDestroyed(npp);
73                    }
74                }
75            }
76        });
77
78        // sets the plugin's surface to a fixed size
79        this.getHolder().setFixedSize(width, height);
80
81        // ensure that the view system is aware that we will be drawing
82        this.setWillNotDraw(false);
83    }
84
85    // called by JNI
86    private void invalidateNPP() {
87        synchronized (nppLock) {
88            validNPP = false;
89        }
90    }
91
92    private native void nativeSurfaceCreated(int npp);
93    private native void nativeSurfaceChanged(int npp, int format, int width, int height);
94    private native void nativeSurfaceDestroyed(int npp);
95}
96