Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | // vim:set ts=2 sts=2 sw=2 et cin: |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifdef MOZ_WIDGET_ANDROID |
michael@0 | 8 | |
michael@0 | 9 | #include <set> |
michael@0 | 10 | #include <map> |
michael@0 | 11 | #include <android/log.h> |
michael@0 | 12 | #include "nsSurfaceTexture.h" |
michael@0 | 13 | #include "AndroidBridge.h" |
michael@0 | 14 | #include "nsThreadUtils.h" |
michael@0 | 15 | #include "mozilla/gfx/Matrix.h" |
michael@0 | 16 | |
michael@0 | 17 | using namespace mozilla; |
michael@0 | 18 | |
michael@0 | 19 | // UGH |
michael@0 | 20 | static std::map<int, nsSurfaceTexture*> sInstances; |
michael@0 | 21 | static int sNextID = 0; |
michael@0 | 22 | |
michael@0 | 23 | static class JNIFunctions { |
michael@0 | 24 | public: |
michael@0 | 25 | |
michael@0 | 26 | JNIFunctions() : mInitialized(false) |
michael@0 | 27 | { |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | bool EnsureInitialized() |
michael@0 | 31 | { |
michael@0 | 32 | if (mInitialized) |
michael@0 | 33 | return true; |
michael@0 | 34 | |
michael@0 | 35 | JNIEnv* env = GetJNIForThread(); |
michael@0 | 36 | |
michael@0 | 37 | AutoLocalJNIFrame jniFrame(env); |
michael@0 | 38 | |
michael@0 | 39 | jSurfaceTextureClass = (jclass)env->NewGlobalRef(env->FindClass("android/graphics/SurfaceTexture")); |
michael@0 | 40 | jSurfaceTexture_Ctor = env->GetMethodID(jSurfaceTextureClass, "<init>", "(I)V"); |
michael@0 | 41 | jSurfaceTexture_updateTexImage = env->GetMethodID(jSurfaceTextureClass, "updateTexImage", "()V"); |
michael@0 | 42 | jSurfaceTexture_getTransformMatrix = env->GetMethodID(jSurfaceTextureClass, "getTransformMatrix", "([F)V"); |
michael@0 | 43 | |
michael@0 | 44 | mInitialized = true; |
michael@0 | 45 | return true; |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | jobject CreateSurfaceTexture(GLuint aTexture) |
michael@0 | 49 | { |
michael@0 | 50 | if (!EnsureInitialized()) |
michael@0 | 51 | return nullptr; |
michael@0 | 52 | |
michael@0 | 53 | JNIEnv* env = GetJNIForThread(); |
michael@0 | 54 | |
michael@0 | 55 | AutoLocalJNIFrame jniFrame(env); |
michael@0 | 56 | |
michael@0 | 57 | return env->NewGlobalRef(env->NewObject(jSurfaceTextureClass, jSurfaceTexture_Ctor, (int) aTexture)); |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | void ReleaseSurfaceTexture(jobject aSurfaceTexture) |
michael@0 | 61 | { |
michael@0 | 62 | JNIEnv* env = GetJNIForThread(); |
michael@0 | 63 | |
michael@0 | 64 | env->DeleteGlobalRef(aSurfaceTexture); |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | void UpdateTexImage(jobject aSurfaceTexture) |
michael@0 | 68 | { |
michael@0 | 69 | JNIEnv* env = GetJNIForThread(); |
michael@0 | 70 | |
michael@0 | 71 | AutoLocalJNIFrame jniFrame(env); |
michael@0 | 72 | env->CallVoidMethod(aSurfaceTexture, jSurfaceTexture_updateTexImage); |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | bool GetTransformMatrix(jobject aSurfaceTexture, gfx::Matrix4x4& aMatrix) |
michael@0 | 76 | { |
michael@0 | 77 | JNIEnv* env = GetJNIForThread(); |
michael@0 | 78 | |
michael@0 | 79 | AutoLocalJNIFrame jniFrame(env); |
michael@0 | 80 | |
michael@0 | 81 | jfloatArray jarray = env->NewFloatArray(16); |
michael@0 | 82 | env->CallVoidMethod(aSurfaceTexture, jSurfaceTexture_getTransformMatrix, jarray); |
michael@0 | 83 | |
michael@0 | 84 | jfloat* array = env->GetFloatArrayElements(jarray, nullptr); |
michael@0 | 85 | |
michael@0 | 86 | aMatrix._11 = array[0]; |
michael@0 | 87 | aMatrix._12 = array[1]; |
michael@0 | 88 | aMatrix._13 = array[2]; |
michael@0 | 89 | aMatrix._14 = array[3]; |
michael@0 | 90 | |
michael@0 | 91 | aMatrix._21 = array[4]; |
michael@0 | 92 | aMatrix._22 = array[5]; |
michael@0 | 93 | aMatrix._23 = array[6]; |
michael@0 | 94 | aMatrix._24 = array[7]; |
michael@0 | 95 | |
michael@0 | 96 | aMatrix._31 = array[8]; |
michael@0 | 97 | aMatrix._32 = array[9]; |
michael@0 | 98 | aMatrix._33 = array[10]; |
michael@0 | 99 | aMatrix._34 = array[11]; |
michael@0 | 100 | |
michael@0 | 101 | aMatrix._41 = array[12]; |
michael@0 | 102 | aMatrix._42 = array[13]; |
michael@0 | 103 | aMatrix._43 = array[14]; |
michael@0 | 104 | aMatrix._44 = array[15]; |
michael@0 | 105 | |
michael@0 | 106 | env->ReleaseFloatArrayElements(jarray, array, 0); |
michael@0 | 107 | |
michael@0 | 108 | return false; |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | private: |
michael@0 | 112 | bool mInitialized; |
michael@0 | 113 | |
michael@0 | 114 | jclass jSurfaceTextureClass; |
michael@0 | 115 | jmethodID jSurfaceTexture_Ctor; |
michael@0 | 116 | jmethodID jSurfaceTexture_updateTexImage; |
michael@0 | 117 | jmethodID jSurfaceTexture_getTransformMatrix; |
michael@0 | 118 | |
michael@0 | 119 | } sJNIFunctions; |
michael@0 | 120 | |
michael@0 | 121 | nsSurfaceTexture* |
michael@0 | 122 | nsSurfaceTexture::Create(GLuint aTexture) |
michael@0 | 123 | { |
michael@0 | 124 | // Right now we only support creating this on the main thread because |
michael@0 | 125 | // of the JNIEnv assumptions in JNIHelper and elsewhere |
michael@0 | 126 | if (!NS_IsMainThread()) |
michael@0 | 127 | return nullptr; |
michael@0 | 128 | |
michael@0 | 129 | nsSurfaceTexture* st = new nsSurfaceTexture(); |
michael@0 | 130 | if (!st->Init(aTexture)) { |
michael@0 | 131 | printf_stderr("Failed to initialize nsSurfaceTexture"); |
michael@0 | 132 | delete st; |
michael@0 | 133 | st = nullptr; |
michael@0 | 134 | } |
michael@0 | 135 | |
michael@0 | 136 | return st; |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | nsSurfaceTexture* |
michael@0 | 140 | nsSurfaceTexture::Find(int id) |
michael@0 | 141 | { |
michael@0 | 142 | std::map<int, nsSurfaceTexture*>::iterator it; |
michael@0 | 143 | |
michael@0 | 144 | it = sInstances.find(id); |
michael@0 | 145 | if (it == sInstances.end()) |
michael@0 | 146 | return nullptr; |
michael@0 | 147 | |
michael@0 | 148 | return it->second; |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | bool |
michael@0 | 152 | nsSurfaceTexture::Check() |
michael@0 | 153 | { |
michael@0 | 154 | return sJNIFunctions.EnsureInitialized(); |
michael@0 | 155 | } |
michael@0 | 156 | |
michael@0 | 157 | bool |
michael@0 | 158 | nsSurfaceTexture::Init(GLuint aTexture) |
michael@0 | 159 | { |
michael@0 | 160 | if (!sJNIFunctions.EnsureInitialized()) |
michael@0 | 161 | return false; |
michael@0 | 162 | |
michael@0 | 163 | JNIEnv* env = GetJNIForThread(); |
michael@0 | 164 | |
michael@0 | 165 | mSurfaceTexture = sJNIFunctions.CreateSurfaceTexture(aTexture); |
michael@0 | 166 | if (!mSurfaceTexture) |
michael@0 | 167 | return false; |
michael@0 | 168 | |
michael@0 | 169 | mNativeWindow = AndroidBridge::Bridge()->AcquireNativeWindowFromSurfaceTexture(env, mSurfaceTexture); |
michael@0 | 170 | |
michael@0 | 171 | mID = ++sNextID; |
michael@0 | 172 | sInstances.insert(std::pair<int, nsSurfaceTexture*>(mID, this)); |
michael@0 | 173 | |
michael@0 | 174 | return true; |
michael@0 | 175 | } |
michael@0 | 176 | |
michael@0 | 177 | nsSurfaceTexture::nsSurfaceTexture() |
michael@0 | 178 | : mSurfaceTexture(nullptr), mNativeWindow(nullptr) |
michael@0 | 179 | { |
michael@0 | 180 | } |
michael@0 | 181 | |
michael@0 | 182 | nsSurfaceTexture::~nsSurfaceTexture() |
michael@0 | 183 | { |
michael@0 | 184 | sInstances.erase(mID); |
michael@0 | 185 | |
michael@0 | 186 | mFrameAvailableCallback = nullptr; |
michael@0 | 187 | |
michael@0 | 188 | if (mNativeWindow) { |
michael@0 | 189 | AndroidBridge::Bridge()->ReleaseNativeWindowForSurfaceTexture(mSurfaceTexture); |
michael@0 | 190 | mNativeWindow = nullptr; |
michael@0 | 191 | } |
michael@0 | 192 | |
michael@0 | 193 | JNIEnv* env = GetJNIForThread(); |
michael@0 | 194 | |
michael@0 | 195 | if (mSurfaceTexture) { |
michael@0 | 196 | mozilla::widget::android::GeckoAppShell::UnregisterSurfaceTextureFrameListener(mSurfaceTexture); |
michael@0 | 197 | |
michael@0 | 198 | env->DeleteGlobalRef(mSurfaceTexture); |
michael@0 | 199 | mSurfaceTexture = nullptr; |
michael@0 | 200 | } |
michael@0 | 201 | } |
michael@0 | 202 | |
michael@0 | 203 | void* |
michael@0 | 204 | nsSurfaceTexture::GetNativeWindow() |
michael@0 | 205 | { |
michael@0 | 206 | return mNativeWindow; |
michael@0 | 207 | } |
michael@0 | 208 | |
michael@0 | 209 | void |
michael@0 | 210 | nsSurfaceTexture::UpdateTexImage() |
michael@0 | 211 | { |
michael@0 | 212 | sJNIFunctions.UpdateTexImage(mSurfaceTexture); |
michael@0 | 213 | } |
michael@0 | 214 | |
michael@0 | 215 | bool |
michael@0 | 216 | nsSurfaceTexture::GetTransformMatrix(gfx::Matrix4x4& aMatrix) |
michael@0 | 217 | { |
michael@0 | 218 | return sJNIFunctions.GetTransformMatrix(mSurfaceTexture, aMatrix); |
michael@0 | 219 | } |
michael@0 | 220 | |
michael@0 | 221 | void |
michael@0 | 222 | nsSurfaceTexture::SetFrameAvailableCallback(nsIRunnable* aRunnable) |
michael@0 | 223 | { |
michael@0 | 224 | if (aRunnable) |
michael@0 | 225 | mozilla::widget::android::GeckoAppShell::RegisterSurfaceTextureFrameListener(mSurfaceTexture, mID); |
michael@0 | 226 | else |
michael@0 | 227 | mozilla::widget::android::GeckoAppShell::UnregisterSurfaceTextureFrameListener(mSurfaceTexture); |
michael@0 | 228 | |
michael@0 | 229 | mFrameAvailableCallback = aRunnable; |
michael@0 | 230 | } |
michael@0 | 231 | |
michael@0 | 232 | void |
michael@0 | 233 | nsSurfaceTexture::NotifyFrameAvailable() |
michael@0 | 234 | { |
michael@0 | 235 | if (mFrameAvailableCallback) { |
michael@0 | 236 | // Proxy to main thread if we aren't on it |
michael@0 | 237 | if (!NS_IsMainThread()) { |
michael@0 | 238 | // Proxy to main thread |
michael@0 | 239 | nsCOMPtr<nsIRunnable> event = NS_NewRunnableMethod(this, &nsSurfaceTexture::NotifyFrameAvailable); |
michael@0 | 240 | NS_DispatchToCurrentThread(event); |
michael@0 | 241 | } else { |
michael@0 | 242 | mFrameAvailableCallback->Run(); |
michael@0 | 243 | } |
michael@0 | 244 | } |
michael@0 | 245 | } |
michael@0 | 246 | |
michael@0 | 247 | #endif // MOZ_WIDGET_ANDROID |