2012-01-31 06:40:58 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2012-05-21 04:12:37 -07:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2012-01-31 06:40:58 -08:00
|
|
|
|
|
|
|
#include <android/log.h>
|
|
|
|
#include "AndroidMediaLayer.h"
|
|
|
|
#include "AndroidBridge.h"
|
|
|
|
|
|
|
|
#define LOG(args...) __android_log_print(ANDROID_LOG_INFO, "AndroidMediaLayer" , ## args)
|
|
|
|
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
AndroidMediaLayer::AndroidMediaLayer()
|
2012-02-15 12:34:31 -08:00
|
|
|
: mInverted(false), mVisible(true) {
|
2012-01-31 06:40:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
AndroidMediaLayer::~AndroidMediaLayer() {
|
2012-04-27 13:04:47 -07:00
|
|
|
if (mContentData.window && AndroidBridge::Bridge()) {
|
2012-01-31 06:40:58 -08:00
|
|
|
AndroidBridge::Bridge()->ReleaseNativeWindow(mContentData.window);
|
|
|
|
mContentData.window = NULL;
|
|
|
|
}
|
|
|
|
|
2012-04-27 13:04:47 -07:00
|
|
|
if (mContentData.surface && AndroidBridge::Bridge()) {
|
2012-01-31 06:40:58 -08:00
|
|
|
AndroidBridge::Bridge()->DestroySurface(mContentData.surface);
|
|
|
|
mContentData.surface = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::map<void*, SurfaceData*>::iterator it;
|
|
|
|
|
|
|
|
for (it = mVideoSurfaces.begin(); it != mVideoSurfaces.end(); it++) {
|
|
|
|
SurfaceData* data = it->second;
|
|
|
|
|
2012-04-27 13:04:47 -07:00
|
|
|
if (AndroidBridge::Bridge()) {
|
|
|
|
AndroidBridge::Bridge()->ReleaseNativeWindow(data->window);
|
|
|
|
AndroidBridge::Bridge()->DestroySurface(data->surface);
|
|
|
|
}
|
|
|
|
|
2012-01-31 06:40:58 -08:00
|
|
|
delete data;
|
|
|
|
}
|
|
|
|
|
|
|
|
mVideoSurfaces.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AndroidMediaLayer::EnsureContentSurface() {
|
2012-04-27 13:04:47 -07:00
|
|
|
if (!mContentData.surface && AndroidBridge::Bridge()) {
|
2012-01-31 06:40:58 -08:00
|
|
|
mContentData.surface = AndroidBridge::Bridge()->CreateSurface();
|
|
|
|
if (mContentData.surface) {
|
2012-04-27 13:04:47 -07:00
|
|
|
mContentData.window = AndroidBridge::Bridge()->AcquireNativeWindow(AndroidBridge::GetJNIEnv(), mContentData.surface);
|
2012-01-31 06:40:58 -08:00
|
|
|
AndroidBridge::Bridge()->SetNativeWindowFormat(mContentData.window, 0, 0, AndroidBridge::WINDOW_FORMAT_RGBA_8888);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mContentData.surface && mContentData.window;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* AndroidMediaLayer::GetNativeWindowForContent() {
|
|
|
|
if (!EnsureContentSurface())
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return mContentData.window;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* AndroidMediaLayer::RequestNativeWindowForVideo() {
|
2012-04-27 13:04:47 -07:00
|
|
|
if (!AndroidBridge::Bridge())
|
|
|
|
return NULL;
|
|
|
|
|
2012-01-31 06:40:58 -08:00
|
|
|
jobject surface = AndroidBridge::Bridge()->CreateSurface();
|
|
|
|
if (surface) {
|
2012-04-27 13:04:47 -07:00
|
|
|
void* window = AndroidBridge::Bridge()->AcquireNativeWindow(AndroidBridge::GetJNIEnv(), surface);
|
2012-01-31 06:40:58 -08:00
|
|
|
if (window) {
|
|
|
|
AndroidBridge::Bridge()->SetNativeWindowFormat(window, 0, 0, AndroidBridge::WINDOW_FORMAT_RGBA_8888);
|
|
|
|
mVideoSurfaces[window] = new SurfaceData(surface, window);
|
|
|
|
return window;
|
|
|
|
} else {
|
|
|
|
LOG("Failed to create native window from surface");
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
AndroidBridge::Bridge()->DestroySurface(surface);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AndroidMediaLayer::ReleaseNativeWindowForVideo(void* aWindow) {
|
2012-04-27 13:04:47 -07:00
|
|
|
if (mVideoSurfaces.find(aWindow) == mVideoSurfaces.end() || !AndroidBridge::Bridge())
|
2012-01-31 06:40:58 -08:00
|
|
|
return;
|
|
|
|
|
|
|
|
SurfaceData* data = mVideoSurfaces[aWindow];
|
|
|
|
|
|
|
|
AndroidBridge::Bridge()->ReleaseNativeWindow(data->window);
|
|
|
|
AndroidBridge::Bridge()->DestroySurface(data->surface);
|
|
|
|
|
|
|
|
mVideoSurfaces.erase(aWindow);
|
|
|
|
delete data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AndroidMediaLayer::SetNativeWindowDimensions(void* aWindow, const gfxRect& aDimensions) {
|
|
|
|
if (mVideoSurfaces.find(aWindow) == mVideoSurfaces.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
SurfaceData* data = mVideoSurfaces[aWindow];
|
|
|
|
data->dimensions = aDimensions;
|
|
|
|
}
|
|
|
|
|
2012-04-27 13:04:47 -07:00
|
|
|
void AndroidMediaLayer::UpdatePosition(const gfxRect& aRect) {
|
|
|
|
if (!mVisible || !AndroidBridge::Bridge())
|
2012-02-15 12:34:31 -08:00
|
|
|
return;
|
2012-01-31 06:40:58 -08:00
|
|
|
|
|
|
|
std::map<void*, SurfaceData*>::iterator it;
|
|
|
|
|
|
|
|
for (it = mVideoSurfaces.begin(); it != mVideoSurfaces.end(); it++) {
|
|
|
|
SurfaceData* data = it->second;
|
|
|
|
|
2012-04-27 13:04:47 -07:00
|
|
|
gfxRect videoRect(aRect.x + data->dimensions.x, aRect.y + data->dimensions.y,
|
|
|
|
data->dimensions.width, data->dimensions.height);
|
2012-01-31 06:40:58 -08:00
|
|
|
|
|
|
|
AndroidBridge::Bridge()->ShowSurface(data->surface, videoRect, mInverted, false);
|
|
|
|
}
|
2012-03-12 10:03:54 -07:00
|
|
|
|
|
|
|
if (EnsureContentSurface()) {
|
|
|
|
AndroidBridge::Bridge()->ShowSurface(mContentData.surface, aRect, mInverted, true);
|
|
|
|
}
|
2012-01-31 06:40:58 -08:00
|
|
|
}
|
|
|
|
|
2012-02-15 12:34:31 -08:00
|
|
|
void AndroidMediaLayer::SetVisible(bool aVisible) {
|
2012-04-27 13:04:47 -07:00
|
|
|
if (aVisible == mVisible || !AndroidBridge::Bridge())
|
2012-02-15 12:34:31 -08:00
|
|
|
return;
|
|
|
|
|
|
|
|
mVisible = aVisible;
|
|
|
|
if (mVisible)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Hide all surfaces
|
|
|
|
std::map<void*, SurfaceData*>::iterator it;
|
|
|
|
|
|
|
|
if (EnsureContentSurface())
|
|
|
|
AndroidBridge::Bridge()->HideSurface(mContentData.surface);
|
|
|
|
|
|
|
|
for (it = mVideoSurfaces.begin(); it != mVideoSurfaces.end(); it++) {
|
|
|
|
SurfaceData* data = it->second;
|
|
|
|
AndroidBridge::Bridge()->HideSurface(data->surface);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-31 06:40:58 -08:00
|
|
|
} /* mozilla */
|