OSDN Git Service

Merge "Add logging to help debug an ANR" into ics-mr1
[android-x86/external-webkit.git] / Source / WebCore / platform / graphics / android / MediaListener.h
1 /*
2  * Copyright (C) 2011 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 #ifndef MediaListener_h
18 #define MediaListener_h
19
20 #if USE(ACCELERATED_COMPOSITING)
21
22 #include <gui/SurfaceTexture.h>
23 #include <gui/SurfaceTextureClient.h>
24 #include <jni.h>
25 #include <JNIUtility.h>
26 #include "MediaTexture.h"
27 #include "WebCoreJni.h"
28
29 #ifdef DEBUG
30
31 #include <cutils/log.h>
32 #include <wtf/text/CString.h>
33
34 #undef XLOG
35 #define XLOG(...) android_printLog(ANDROID_LOG_DEBUG, "MediaListener", __VA_ARGS__)
36
37 #else
38
39 #undef XLOG
40 #define XLOG(...)
41
42 #endif // DEBUG
43
44 namespace WebCore {
45
46 class MediaListener : public android::SurfaceTexture::FrameAvailableListener {
47
48 public:
49     MediaListener(jobject weakWebViewRef,
50                   const sp<android::SurfaceTexture>& surfaceTexture,
51                   const sp<ANativeWindow>& nativeWindow)
52         : m_weakWebViewRef(weakWebViewRef)
53         , m_postInvalMethod(0)
54         , m_frameAvailable(false)
55         , m_surfaceTexture(surfaceTexture)
56         , m_nativeWindow(nativeWindow)
57         , m_framerateCallback(0)
58     {
59         if (!m_weakWebViewRef)
60             return;
61
62         JNIEnv* env = JSC::Bindings::getJNIEnv();
63         jobject localWebViewRef = env->NewLocalRef(m_weakWebViewRef);
64         if (localWebViewRef) {
65             jclass wvClass = env->GetObjectClass(localWebViewRef);
66             m_postInvalMethod = env->GetMethodID(wvClass, "postInvalidate", "()V");
67             env->DeleteLocalRef(wvClass);
68             env->DeleteLocalRef(localWebViewRef);
69         }
70         checkException(env);
71     }
72
73     virtual void onFrameAvailable()
74     {
75         JNIEnv* env = JSC::Bindings::getJNIEnv();
76         jobject localWebViewRef = env->NewLocalRef(m_weakWebViewRef);
77         if (localWebViewRef) {
78             env->CallVoidMethod(localWebViewRef, m_postInvalMethod);
79             env->DeleteLocalRef(localWebViewRef);
80         }
81         checkException(env);
82         if (!m_frameAvailable) {
83             m_frameAvailable = true;
84         }
85         if (m_framerateCallback)
86             m_framerateCallback(m_nativeWindow.get(), m_surfaceTexture->getTimestamp());
87     }
88
89     bool isFrameAvailable() { return m_frameAvailable; }
90     void setFramerateCallback(FramerateCallbackProc callback) { m_framerateCallback = callback; }
91
92 private:
93     jobject m_weakWebViewRef;
94     jmethodID m_postInvalMethod;
95     bool m_frameAvailable;
96     sp<android::SurfaceTexture> m_surfaceTexture;
97     sp<ANativeWindow> m_nativeWindow;
98     FramerateCallbackProc m_framerateCallback;
99 };
100
101 } // namespace WebCore
102
103 #endif // USE(ACCELERATED_COMPOSITING)
104 #endif  // MediaListener_h