blob: 2609af67cd05ec0132fc4016839e85cc4fdf910c [file] [log] [blame]
Derek Sollenbergerb8947ee2009-10-05 14:40:18 -04001/*
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 APPLE COMPUTER, INC. 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 */
25
26#include "VideoPlugin.h"
27#include "android_npapi.h"
28
29#include <stdio.h>
30#include <sys/time.h>
31#include <time.h>
32#include <math.h>
33#include <string.h>
34
35extern NPNetscapeFuncs* browser;
36extern ANPBitmapInterfaceV0 gBitmapI;
37extern ANPCanvasInterfaceV0 gCanvasI;
38extern ANPLogInterfaceV0 gLogI;
39extern ANPPaintInterfaceV0 gPaintI;
40extern ANPSurfaceInterfaceV0 gSurfaceI;
41extern ANPTypefaceInterfaceV0 gTypefaceI;
42extern ANPWindowInterfaceV0 gWindowI;
43
44///////////////////////////////////////////////////////////////////////////////
45
46VideoPlugin::VideoPlugin(NPP inst) : SurfaceSubPlugin(inst) {
47
48 // initialize the drawing surface
49 m_surface = NULL;
50
51 //register for touch events
52 ANPEventFlags flags = kTouch_ANPEventFlag;
53 NPError err = browser->setvalue(inst, kAcceptEvents_ANPSetValue, &flags);
54 if (err != NPERR_NO_ERROR) {
55 gLogI.log(inst, kError_ANPLogType, "Error selecting input events.");
56 }
57}
58
59VideoPlugin::~VideoPlugin() {
60 surfaceDestroyed();
61}
62
63bool VideoPlugin::supportsDrawingModel(ANPDrawingModel model) {
64 return (model == kSurface_ANPDrawingModel);
65}
66
67bool VideoPlugin::isFixedSurface() {
68 return true;
69}
70
71void VideoPlugin::surfaceCreated(jobject surface) {
72 m_surface = surface;
73 drawPlugin();
74}
75
76void VideoPlugin::surfaceChanged(int format, int width, int height) {
77 gLogI.log(inst(), kDebug_ANPLogType, "----%p SurfaceChanged Event: %d",
78 inst(), format);
79 drawPlugin();
80}
81
82void VideoPlugin::surfaceDestroyed() {
83 JNIEnv* env = NULL;
84 if (m_surface && gVM->GetEnv((void**) &env, JNI_VERSION_1_4) == JNI_OK) {
85 env->DeleteGlobalRef(m_surface);
86 m_surface = NULL;
87 }
88}
89
90void VideoPlugin::drawPlugin() {
91
92 ANPBitmap bitmap;
93 JNIEnv* env = NULL;
94 if (!m_surface || gVM->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK ||
95 !gSurfaceI.lock(env, m_surface, &bitmap, NULL)) {
96 gLogI.log(inst(), kError_ANPLogType, "----%p Unable to Lock Surface", inst());
97 return;
98 }
99
100 ANPCanvas* canvas = gCanvasI.newCanvas(&bitmap);
101
102 // get the plugin's dimensions according to the DOM
103 PluginObject *obj = (PluginObject*) inst()->pdata;
104 const int pW = obj->window->width;
105 const int pH = obj->window->height;
106
107 // compare DOM dimensions to the plugin's surface dimensions
108 if (pW != bitmap.width || pH != bitmap.height)
109 gLogI.log(inst(), kError_ANPLogType,
110 "----%p Invalid Surface Dimensions (%d,%d):(%d,%d)",
111 inst(), pW, pH, bitmap.width, bitmap.height);
112
113 // set constants
114 const int fontSize = 16;
115 const int leftMargin = 10;
116
117 gCanvasI.drawColor(canvas, 0xFFCDCDCD);
118
119 ANPPaint* paint = gPaintI.newPaint();
120 gPaintI.setFlags(paint, gPaintI.getFlags(paint) | kAntiAlias_ANPPaintFlag);
121 gPaintI.setColor(paint, 0xFFFF0000);
122 gPaintI.setTextSize(paint, fontSize);
123
124 ANPTypeface* tf = gTypefaceI.createFromName("serif", kItalic_ANPTypefaceStyle);
125 gPaintI.setTypeface(paint, tf);
126 gTypefaceI.unref(tf);
127
128 ANPFontMetrics fm;
129 gPaintI.getFontMetrics(paint, &fm);
130
131 gPaintI.setColor(paint, 0xFF0000FF);
132 const char c[] = "Touch anywhere on the plugin to begin video playback!";
133 gCanvasI.drawText(canvas, c, sizeof(c)-1, leftMargin, -fm.fTop, paint);
134
135 // clean up variables and unlock the surface
136 gPaintI.deletePaint(paint);
137 gCanvasI.deleteCanvas(canvas);
138 gSurfaceI.unlock(env, m_surface);
139}
140
141int16 VideoPlugin::handleEvent(const ANPEvent* evt) {
142 switch (evt->eventType) {
143 case kDraw_ANPEventType:
144 gLogI.log(inst(), kError_ANPLogType, " ------ %p the plugin did not request draw events", inst());
145 break;
146 case kTouch_ANPEventType:
147 if (kDown_ANPTouchAction == evt->data.touch.action) {
148 gLogI.log(inst(), kDebug_ANPLogType, " ------ %p requesting fullscreen mode", inst());
149 gWindowI.requestFullScreen(inst());
150 }
151 return 1;
152 case kKey_ANPEventType:
153 gLogI.log(inst(), kError_ANPLogType, " ------ %p the plugin did not request key events", inst());
154 break;
155 default:
156 break;
157 }
158 return 0; // unknown or unhandled event
159}