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  */
25 
26 /*  Defines the android-specific types and functions as part of npapi
27 
28     In particular, defines the window and event types that are passed to
29     NPN_GetValue, NPP_SetWindow and NPP_HandleEvent.
30 
31     To minimize what native libraries the plugin links against, some
32     functionality is provided via function-ptrs (e.g. time, sound)
33  */
34 
35 #ifndef android_npapi_H
36 #define android_npapi_H
37 
38 #include <stdint.h>
39 #include <jni.h>
40 #include "npapi.h"
41 #include "GLDefs.h"
42 
43 ///////////////////////////////////////////////////////////////////////////////
44 // General types
45 
46 enum ANPBitmapFormats {
47     kUnknown_ANPBitmapFormat    = 0,
48     kRGBA_8888_ANPBitmapFormat  = 1,
49     kRGB_565_ANPBitmapFormat    = 2
50 };
51 typedef int32_t ANPBitmapFormat;
52 
53 struct ANPPixelPacking {
54     uint8_t AShift;
55     uint8_t ABits;
56     uint8_t RShift;
57     uint8_t RBits;
58     uint8_t GShift;
59     uint8_t GBits;
60     uint8_t BShift;
61     uint8_t BBits;
62 };
63 
64 struct ANPBitmap {
65     void*           baseAddr;
66     ANPBitmapFormat format;
67     int32_t         width;
68     int32_t         height;
69     int32_t         rowBytes;
70 };
71 
72 struct ANPRectF {
73     float   left;
74     float   top;
75     float   right;
76     float   bottom;
77 };
78 
79 struct ANPRectI {
80     int32_t left;
81     int32_t top;
82     int32_t right;
83     int32_t bottom;
84 };
85 
86 struct ANPCanvas;
87 struct ANPMatrix;
88 struct ANPPaint;
89 struct ANPPath;
90 struct ANPTypeface;
91 
92 ///////////////////////////////////////////////////////////////////////////////
93 // NPN_GetValue
94 
95 /** queries for a specific ANPInterface.
96 
97     Maybe called with NULL for the NPP instance
98 
99     NPN_GetValue(inst, interface_enum, ANPInterface*)
100  */
101 #define kLogInterfaceV0_ANPGetValue         ((NPNVariable)1000)
102 #define kAudioTrackInterfaceV0_ANPGetValue  ((NPNVariable)1001)
103 #define kCanvasInterfaceV0_ANPGetValue      ((NPNVariable)1002)
104 #define kMatrixInterfaceV0_ANPGetValue      ((NPNVariable)1003)
105 #define kPaintInterfaceV0_ANPGetValue       ((NPNVariable)1004)
106 #define kPathInterfaceV0_ANPGetValue        ((NPNVariable)1005)
107 #define kTypefaceInterfaceV0_ANPGetValue    ((NPNVariable)1006)
108 #define kWindowInterfaceV0_ANPGetValue      ((NPNVariable)1007)
109 #define kBitmapInterfaceV0_ANPGetValue      ((NPNVariable)1008)
110 #define kSurfaceInterfaceV0_ANPGetValue     ((NPNVariable)1009)
111 #define kSystemInterfaceV0_ANPGetValue      ((NPNVariable)1010)
112 #define kEventInterfaceV0_ANPGetValue       ((NPNVariable)1011)
113 
114 #define kAudioTrackInterfaceV1_ANPGetValue  ((NPNVariable)1012)
115 #define kOpenGLInterfaceV0_ANPGetValue      ((NPNVariable)1013)
116 #define kWindowInterfaceV1_ANPGetValue      ((NPNVariable)1014)
117 #define kVideoInterfaceV0_ANPGetValue       ((NPNVariable)1015)
118 #define kSystemInterfaceV1_ANPGetValue      ((NPNVariable)1016)
119 #define kSystemInterfaceV2_ANPGetValue      ((NPNVariable)1017)
120 #define kWindowInterfaceV2_ANPGetValue      ((NPNVariable)1018)
121 #define kNativeWindowInterfaceV0_ANPGetValue ((NPNVariable)1019)
122 #define kVideoInterfaceV1_ANPGetValue       ((NPNVariable)1020)
123 
124 /** queries for the drawing models supported on this device.
125 
126     NPN_GetValue(inst, kSupportedDrawingModel_ANPGetValue, uint32_t* bits)
127  */
128 #define kSupportedDrawingModel_ANPGetValue  ((NPNVariable)2000)
129 
130 /** queries for the context (android.content.Context) of the plugin. If no
131     instance is specified the application's context is returned. If the instance
132     is given then the context returned is identical to the context used to
133     create the webview in which that instance resides.
134 
135     NOTE: Holding onto a non-application context after your instance has been
136     destroyed will cause a memory leak.  Refer to the android documentation to
137     determine what context is best suited for your particular scenario.
138 
139     NPN_GetValue(inst, kJavaContext_ANPGetValue, jobject context)
140  */
141 #define kJavaContext_ANPGetValue            ((NPNVariable)2001)
142 
143 ///////////////////////////////////////////////////////////////////////////////
144 // NPN_SetValue
145 
146 /** Request to set the drawing model. SetValue will return false if the drawing
147     model is not supported or has insufficient information for configuration.
148 
149     NPN_SetValue(inst, kRequestDrawingModel_ANPSetValue, (void*)foo_ANPDrawingModel)
150  */
151 #define kRequestDrawingModel_ANPSetValue    ((NPPVariable)1000)
152 
153 /** These are used as bitfields in ANPSupportedDrawingModels_EnumValue,
154     and as-is in ANPRequestDrawingModel_EnumValue. The drawing model determines
155     how to interpret the ANPDrawingContext provided in the Draw event and how
156     to interpret the NPWindow->window field.
157  */
158 enum ANPDrawingModels {
159     /** Draw into a bitmap from the browser thread in response to a Draw event.
160         NPWindow->window is reserved (ignore)
161      */
162     kBitmap_ANPDrawingModel  = 1 << 0,
163     /** Draw into a surface (e.g. raster, openGL, etc.) using the Java surface
164         interface. When this model is used the browser will invoke the Java
165         class specified in the plugin's apk manifest. From that class the browser
166         will invoke the appropriate method to return an an instance of a android
167         Java View. The instance is then embedded in the html. The plugin can then
168         manipulate the view as it would any normal Java View in android.
169 
170         Unlike the bitmap model, a surface model is opaque so no html content
171         behind the plugin will be  visible. Unless the plugin needs to be
172         transparent the surface model should be chosen over the bitmap model as
173         it will have better performance.
174 
175         Further, a plugin can manipulate some surfaces in native code using the
176         ANPSurfaceInterface.  This interface can be used to manipulate Java
177         objects that extend Surface.class by allowing them to access the
178         surface's underlying bitmap in native code.  For instance, if a raster
179         surface is used the plugin can lock, draw directly into the bitmap, and
180         unlock the surface in native code without making JNI calls to the Java
181         surface object.
182      */
183     kSurface_ANPDrawingModel = 1 << 1,
184     kOpenGL_ANPDrawingModel  = 1 << 2,
185 };
186 typedef int32_t ANPDrawingModel;
187 
188 /** Request to receive/disable events. If the pointer is NULL then all flags will
189     be disabled. Otherwise, the event type will be enabled iff its corresponding
190     bit in the EventFlags bit field is set.
191 
192     NPN_SetValue(inst, ANPAcceptEvents, (void*)EventFlags)
193  */
194 #define kAcceptEvents_ANPSetValue           ((NPPVariable)1001)
195 
196 /** The EventFlags are a set of bits used to determine which types of events the
197     plugin wishes to receive. For example, if the value is 0x03 then both key
198     and touch events will be provided to the plugin.
199  */
200 enum ANPEventFlag {
201     kKey_ANPEventFlag               = 0x01,
202     kTouch_ANPEventFlag             = 0x02,
203 };
204 typedef uint32_t ANPEventFlags;
205 
206 ///////////////////////////////////////////////////////////////////////////////
207 // NPP_GetValue
208 
209 /** Requests that the plugin return a java surface to be displayed. This will
210     only be used if the plugin has choosen the kSurface_ANPDrawingModel.
211 
212     NPP_GetValue(inst, kJavaSurface_ANPGetValue, jobject surface)
213  */
214 #define kJavaSurface_ANPGetValue            ((NPPVariable)2000)
215 
216 
217 ///////////////////////////////////////////////////////////////////////////////
218 // ANDROID INTERFACE DEFINITIONS
219 
220 /** Interfaces provide additional functionality to the plugin via function ptrs.
221     Once an interface is retrieved, it is valid for the lifetime of the plugin
222     (just like browserfuncs).
223 
224     All ANPInterfaces begin with an inSize field, which must be set by the
225     caller (plugin) with the number of bytes allocated for the interface.
226     e.g. SomeInterface si; si.inSize = sizeof(si); browser->getvalue(..., &si);
227  */
228 struct ANPInterface {
229     uint32_t    inSize;     // size (in bytes) of this struct
230 };
231 
232 enum ANPLogTypes {
233     kError_ANPLogType   = 0,    // error
234     kWarning_ANPLogType = 1,    // warning
235     kDebug_ANPLogType   = 2     // debug only (informational)
236 };
237 typedef int32_t ANPLogType;
238 
239 struct ANPLogInterfaceV0 : ANPInterface {
240     /** dumps printf messages to the log file
241         e.g. interface->log(instance, kWarning_ANPLogType, "value is %d", value);
242      */
243     void (*log)(ANPLogType, const char format[], ...);
244 };
245 
246 /** ANPColor is always defined to have the same packing on all platforms, and
247     it is always unpremultiplied.
248 
249     This is in contrast to 32bit format(s) in bitmaps, which are premultiplied,
250     and their packing may vary depending on the platform, hence the need for
251     ANPBitmapInterface::getPixelPacking()
252  */
253 typedef uint32_t ANPColor;
254 #define ANPColor_ASHIFT     24
255 #define ANPColor_RSHIFT     16
256 #define ANPColor_GSHIFT     8
257 #define ANPColor_BSHIFT     0
258 #define ANP_MAKE_COLOR(a, r, g, b)  \
259                    (((a) << ANPColor_ASHIFT) |  \
260                     ((r) << ANPColor_RSHIFT) |  \
261                     ((g) << ANPColor_GSHIFT) |  \
262                     ((b) << ANPColor_BSHIFT))
263 
264 enum ANPPaintFlag {
265     kAntiAlias_ANPPaintFlag         = 1 << 0,
266     kFilterBitmap_ANPPaintFlag      = 1 << 1,
267     kDither_ANPPaintFlag            = 1 << 2,
268     kUnderlineText_ANPPaintFlag     = 1 << 3,
269     kStrikeThruText_ANPPaintFlag    = 1 << 4,
270     kFakeBoldText_ANPPaintFlag      = 1 << 5,
271 };
272 typedef uint32_t ANPPaintFlags;
273 
274 enum ANPPaintStyles {
275     kFill_ANPPaintStyle             = 0,
276     kStroke_ANPPaintStyle           = 1,
277     kFillAndStroke_ANPPaintStyle    = 2
278 };
279 typedef int32_t ANPPaintStyle;
280 
281 enum ANPPaintCaps {
282     kButt_ANPPaintCap   = 0,
283     kRound_ANPPaintCap  = 1,
284     kSquare_ANPPaintCap = 2
285 };
286 typedef int32_t ANPPaintCap;
287 
288 enum ANPPaintJoins {
289     kMiter_ANPPaintJoin = 0,
290     kRound_ANPPaintJoin = 1,
291     kBevel_ANPPaintJoin = 2
292 };
293 typedef int32_t ANPPaintJoin;
294 
295 enum ANPPaintAligns {
296     kLeft_ANPPaintAlign     = 0,
297     kCenter_ANPPaintAlign   = 1,
298     kRight_ANPPaintAlign    = 2
299 };
300 typedef int32_t ANPPaintAlign;
301 
302 enum ANPTextEncodings {
303     kUTF8_ANPTextEncoding   = 0,
304     kUTF16_ANPTextEncoding  = 1,
305 };
306 typedef int32_t ANPTextEncoding;
307 
308 enum ANPTypefaceStyles {
309     kBold_ANPTypefaceStyle      = 1 << 0,
310     kItalic_ANPTypefaceStyle    = 1 << 1
311 };
312 typedef uint32_t ANPTypefaceStyle;
313 
314 typedef uint32_t ANPFontTableTag;
315 
316 struct ANPFontMetrics {
317     /** The greatest distance above the baseline for any glyph (will be <= 0) */
318     float   fTop;
319     /** The recommended distance above the baseline (will be <= 0) */
320     float   fAscent;
321     /** The recommended distance below the baseline (will be >= 0) */
322     float   fDescent;
323     /** The greatest distance below the baseline for any glyph (will be >= 0) */
324     float   fBottom;
325     /** The recommended distance to add between lines of text (will be >= 0) */
326     float   fLeading;
327 };
328 
329 struct ANPTypefaceInterfaceV0 : ANPInterface {
330     /** Return a new reference to the typeface that most closely matches the
331         requested name and style. Pass null as the name to return
332         the default font for the requested style. Will never return null
333 
334         The 5 generic font names "serif", "sans-serif", "monospace", "cursive",
335         "fantasy" are recognized, and will be mapped to their logical font
336         automatically by this call.
337 
338         @param name     May be NULL. The name of the font family.
339         @param style    The style (normal, bold, italic) of the typeface.
340         @return reference to the closest-matching typeface. Caller must call
341                 unref() when they are done with the typeface.
342      */
343     ANPTypeface* (*createFromName)(const char name[], ANPTypefaceStyle);
344 
345     /** Return a new reference to the typeface that most closely matches the
346         requested typeface and specified Style. Use this call if you want to
347         pick a new style from the same family of the existing typeface.
348         If family is NULL, this selects from the default font's family.
349 
350         @param family  May be NULL. The name of the existing type face.
351         @param s       The style (normal, bold, italic) of the type face.
352         @return reference to the closest-matching typeface. Call must call
353                 unref() when they are done.
354      */
355     ANPTypeface* (*createFromTypeface)(const ANPTypeface* family,
356                                        ANPTypefaceStyle);
357 
358     /** Return the owner count of the typeface. A newly created typeface has an
359         owner count of 1. When the owner count is reaches 0, the typeface is
360         deleted.
361      */
362     int32_t (*getRefCount)(const ANPTypeface*);
363 
364     /** Increment the owner count on the typeface
365      */
366     void (*ref)(ANPTypeface*);
367 
368     /** Decrement the owner count on the typeface. When the count goes to 0,
369         the typeface is deleted.
370      */
371     void (*unref)(ANPTypeface*);
372 
373     /** Return the style bits for the specified typeface
374      */
375     ANPTypefaceStyle (*getStyle)(const ANPTypeface*);
376 
377     /** Some fonts are stored in files. If that is true for the fontID, then
378         this returns the byte length of the full file path. If path is not null,
379         then the full path is copied into path (allocated by the caller), up to
380         length bytes. If index is not null, then it is set to the truetype
381         collection index for this font, or 0 if the font is not in a collection.
382 
383         Note: getFontPath does not assume that path is a null-terminated string,
384         so when it succeeds, it only copies the bytes of the file name and
385         nothing else (i.e. it copies exactly the number of bytes returned by the
386         function. If the caller wants to treat path[] as a C string, it must be
387         sure that it is allocated at least 1 byte larger than the returned size,
388         and it must copy in the terminating 0.
389 
390         If the fontID does not correspond to a file, then the function returns
391         0, and the path and index parameters are ignored.
392 
393         @param fontID  The font whose file name is being queried
394         @param path    Either NULL, or storage for receiving up to length bytes
395                        of the font's file name. Allocated by the caller.
396         @param length  The maximum space allocated in path (by the caller).
397                        Ignored if path is NULL.
398         @param index   Either NULL, or receives the TTC index for this font.
399                        If the font is not a TTC, then will be set to 0.
400         @return The byte length of th font's file name, or 0 if the font is not
401                 baked by a file.
402      */
403     int32_t (*getFontPath)(const ANPTypeface*, char path[], int32_t length,
404                            int32_t* index);
405 
406     /** Return a UTF8 encoded path name for the font directory, or NULL if not
407         supported. If returned, this string address will be valid for the life
408         of the plugin instance. It will always end with a '/' character.
409      */
410     const char* (*getFontDirectoryPath)();
411 };
412 
413 struct ANPPaintInterfaceV0 : ANPInterface {
414     /** Return a new paint object, which holds all of the color and style
415         attributes that affect how things (geometry, text, bitmaps) are drawn
416         in a ANPCanvas.
417 
418         The paint that is returned is not tied to any particular plugin
419         instance, but it must only be accessed from one thread at a time.
420      */
421     ANPPaint*   (*newPaint)();
422     void        (*deletePaint)(ANPPaint*);
423 
424     ANPPaintFlags (*getFlags)(const ANPPaint*);
425     void        (*setFlags)(ANPPaint*, ANPPaintFlags);
426 
427     ANPColor    (*getColor)(const ANPPaint*);
428     void        (*setColor)(ANPPaint*, ANPColor);
429 
430     ANPPaintStyle (*getStyle)(const ANPPaint*);
431     void        (*setStyle)(ANPPaint*, ANPPaintStyle);
432 
433     float       (*getStrokeWidth)(const ANPPaint*);
434     float       (*getStrokeMiter)(const ANPPaint*);
435     ANPPaintCap (*getStrokeCap)(const ANPPaint*);
436     ANPPaintJoin (*getStrokeJoin)(const ANPPaint*);
437     void        (*setStrokeWidth)(ANPPaint*, float);
438     void        (*setStrokeMiter)(ANPPaint*, float);
439     void        (*setStrokeCap)(ANPPaint*, ANPPaintCap);
440     void        (*setStrokeJoin)(ANPPaint*, ANPPaintJoin);
441 
442     ANPTextEncoding (*getTextEncoding)(const ANPPaint*);
443     ANPPaintAlign (*getTextAlign)(const ANPPaint*);
444     float       (*getTextSize)(const ANPPaint*);
445     float       (*getTextScaleX)(const ANPPaint*);
446     float       (*getTextSkewX)(const ANPPaint*);
447     void        (*setTextEncoding)(ANPPaint*, ANPTextEncoding);
448     void        (*setTextAlign)(ANPPaint*, ANPPaintAlign);
449     void        (*setTextSize)(ANPPaint*, float);
450     void        (*setTextScaleX)(ANPPaint*, float);
451     void        (*setTextSkewX)(ANPPaint*, float);
452 
453     /** Return the typeface ine paint, or null if there is none. This does not
454         modify the owner count of the returned typeface.
455      */
456     ANPTypeface* (*getTypeface)(const ANPPaint*);
457 
458     /** Set the paint's typeface. If the paint already had a non-null typeface,
459         its owner count is decremented. If the new typeface is non-null, its
460         owner count is incremented.
461      */
462     void (*setTypeface)(ANPPaint*, ANPTypeface*);
463 
464     /** Return the width of the text. If bounds is not null, return the bounds
465         of the text in that rectangle.
466      */
467     float (*measureText)(ANPPaint*, const void* text, uint32_t byteLength,
468                          ANPRectF* bounds);
469 
470     /** Return the number of unichars specifed by the text.
471         If widths is not null, returns the array of advance widths for each
472             unichar.
473         If bounds is not null, returns the array of bounds for each unichar.
474      */
475     int (*getTextWidths)(ANPPaint*, const void* text, uint32_t byteLength,
476                          float widths[], ANPRectF bounds[]);
477 
478     /** Return in metrics the spacing values for text, respecting the paint's
479         typeface and pointsize, and return the spacing between lines
480         (descent - ascent + leading). If metrics is NULL, it will be ignored.
481      */
482     float (*getFontMetrics)(ANPPaint*, ANPFontMetrics* metrics);
483 };
484 
485 struct ANPCanvasInterfaceV0 : ANPInterface {
486     /** Return a canvas that will draw into the specified bitmap. Note: the
487         canvas copies the fields of the bitmap, so it need not persist after
488         this call, but the canvas DOES point to the same pixel memory that the
489         bitmap did, so the canvas should not be used after that pixel memory
490         goes out of scope. In the case of creating a canvas to draw into the
491         pixels provided by kDraw_ANPEventType, those pixels are only while
492         handling that event.
493 
494         The canvas that is returned is not tied to any particular plugin
495         instance, but it must only be accessed from one thread at a time.
496      */
497     ANPCanvas*  (*newCanvas)(const ANPBitmap*);
498     void        (*deleteCanvas)(ANPCanvas*);
499 
500     void        (*save)(ANPCanvas*);
501     void        (*restore)(ANPCanvas*);
502     void        (*translate)(ANPCanvas*, float tx, float ty);
503     void        (*scale)(ANPCanvas*, float sx, float sy);
504     void        (*rotate)(ANPCanvas*, float degrees);
505     void        (*skew)(ANPCanvas*, float kx, float ky);
506     void        (*concat)(ANPCanvas*, const ANPMatrix*);
507     void        (*clipRect)(ANPCanvas*, const ANPRectF*);
508     void        (*clipPath)(ANPCanvas*, const ANPPath*);
509 
510     /** Return the current matrix on the canvas
511      */
512     void        (*getTotalMatrix)(ANPCanvas*, ANPMatrix*);
513     /** Return the current clip bounds in local coordinates, expanding it to
514         account for antialiasing edge effects if aa is true. If the
515         current clip is empty, return false and ignore the bounds argument.
516      */
517     bool        (*getLocalClipBounds)(ANPCanvas*, ANPRectF* bounds, bool aa);
518     /** Return the current clip bounds in device coordinates in bounds. If the
519         current clip is empty, return false and ignore the bounds argument.
520      */
521     bool        (*getDeviceClipBounds)(ANPCanvas*, ANPRectI* bounds);
522 
523     void        (*drawColor)(ANPCanvas*, ANPColor);
524     void        (*drawPaint)(ANPCanvas*, const ANPPaint*);
525     void        (*drawLine)(ANPCanvas*, float x0, float y0, float x1, float y1,
526                             const ANPPaint*);
527     void        (*drawRect)(ANPCanvas*, const ANPRectF*, const ANPPaint*);
528     void        (*drawOval)(ANPCanvas*, const ANPRectF*, const ANPPaint*);
529     void        (*drawPath)(ANPCanvas*, const ANPPath*, const ANPPaint*);
530     void        (*drawText)(ANPCanvas*, const void* text, uint32_t byteLength,
531                             float x, float y, const ANPPaint*);
532     void       (*drawPosText)(ANPCanvas*, const void* text, uint32_t byteLength,
533                                const float xy[], const ANPPaint*);
534     void        (*drawBitmap)(ANPCanvas*, const ANPBitmap*, float x, float y,
535                               const ANPPaint*);
536     void        (*drawBitmapRect)(ANPCanvas*, const ANPBitmap*,
537                                   const ANPRectI* src, const ANPRectF* dst,
538                                   const ANPPaint*);
539 };
540 
541 struct ANPWindowInterfaceV0 : ANPInterface {
542     /** Registers a set of rectangles that the plugin would like to keep on
543         screen. The rectangles are listed in order of priority with the highest
544         priority rectangle in location rects[0].  The browser will attempt to keep
545         as many of the rectangles on screen as possible and will scroll them into
546         view in response to the invocation of this method and other various events.
547         The count specifies how many rectangles are in the array. If the count is
548         zero it signals the browser that any existing rectangles should be cleared
549         and no rectangles will be tracked.
550      */
551     void (*setVisibleRects)(NPP instance, const ANPRectI rects[], int32_t count);
552     /** Clears any rectangles that are being tracked as a result of a call to
553         setVisibleRects. This call is equivalent to setVisibleRect(inst, NULL, 0).
554      */
555     void    (*clearVisibleRects)(NPP instance);
556     /** Given a boolean value of true the device will be requested to provide
557         a keyboard. A value of false will result in a request to hide the
558         keyboard. Further, the on-screen keyboard will not be displayed if a
559         physical keyboard is active.
560      */
561     void    (*showKeyboard)(NPP instance, bool value);
562     /** Called when a plugin wishes to enter into full screen mode. The plugin's
563         Java class (defined in the plugin's apk manifest) will be called
564         asynchronously to provide a View object to be displayed full screen.
565      */
566     void    (*requestFullScreen)(NPP instance);
567     /** Called when a plugin wishes to exit from full screen mode. As a result,
568         the plugin's full screen view will be discarded by the view system.
569      */
570     void    (*exitFullScreen)(NPP instance);
571     /** Called when a plugin wishes to be zoomed and centered in the current view.
572      */
573     void    (*requestCenterFitZoom)(NPP instance);
574 };
575 
576 enum ANPScreenOrientations {
577     /** No preference specified: let the system decide the best orientation.
578      */
579     kDefault_ANPScreenOrientation        = 0,
580     /** Would like to have the screen in a landscape orientation, but it will
581         not allow for 180 degree rotations.
582      */
583     kFixedLandscape_ANPScreenOrientation = 1,
584     /** Would like to have the screen in a portrait orientation, but it will
585         not allow for 180 degree rotations.
586      */
587     kFixedPortrait_ANPScreenOrientation  = 2,
588     /** Would like to have the screen in landscape orientation, but can use the
589         sensor to change which direction the screen is facing.
590      */
591     kLandscape_ANPScreenOrientation      = 3,
592     /** Would like to have the screen in portrait orientation, but can use the
593         sensor to change which direction the screen is facing.
594      */
595     kPortrait_ANPScreenOrientation       = 4
596 };
597 
598 typedef int32_t ANPScreenOrientation;
599 
600 struct ANPWindowInterfaceV2 : ANPWindowInterfaceV0 {
601     /** Returns a rectangle representing the visible area of the plugin on
602         screen. The coordinates are relative to the size of the plugin in the
603         document and therefore will never be negative or exceed the plugin's size.
604      */
605     ANPRectI (*visibleRect)(NPP instance);
606 
607     /** Called when the plugin wants to specify a particular screen orientation
608         when entering into full screen mode. The orientation must be set prior
609         to entering into full screen.  After entering full screen any subsequent
610         changes will be updated the next time the plugin goes full screen.
611      */
612     void (*requestFullScreenOrientation)(NPP instance, ANPScreenOrientation orientation);
613 };
614 
615 ///////////////////////////////////////////////////////////////////////////////
616 
617 enum ANPSampleFormats {
618     kUnknown_ANPSamleFormat     = 0,
619     kPCM16Bit_ANPSampleFormat   = 1,
620     kPCM8Bit_ANPSampleFormat    = 2
621 };
622 typedef int32_t ANPSampleFormat;
623 
624 /** The audio buffer is passed to the callback proc to request more samples.
625     It is owned by the system, and the callback may read it, but should not
626     maintain a pointer to it outside of the scope of the callback proc.
627  */
628 struct ANPAudioBuffer {
629     // RO - repeat what was specified in newTrack()
630     int32_t     channelCount;
631     // RO - repeat what was specified in newTrack()
632     ANPSampleFormat  format;
633     /** This buffer is owned by the caller. Inside the callback proc, up to
634         "size" bytes of sample data should be written into this buffer. The
635         address is only valid for the scope of a single invocation of the
636         callback proc.
637      */
638     void*       bufferData;
639     /** On input, specifies the maximum number of bytes that can be written
640         to "bufferData". On output, specifies the actual number of bytes that
641         the callback proc wrote into "bufferData".
642      */
643     uint32_t    size;
644 };
645 
646 enum ANPAudioEvents {
647     /** This event is passed to the callback proc when the audio-track needs
648         more sample data written to the provided buffer parameter.
649      */
650     kMoreData_ANPAudioEvent = 0,
651     /** This event is passed to the callback proc if the audio system runs out
652         of sample data. In this event, no buffer parameter will be specified
653         (i.e. NULL will be passed to the 3rd parameter).
654      */
655     kUnderRun_ANPAudioEvent = 1
656 };
657 typedef int32_t ANPAudioEvent;
658 
659 /** Called to feed sample data to the track. This will be called in a separate
660     thread. However, you may call trackStop() from the callback (but you
661     cannot delete the track).
662 
663     For example, when you have written the last chunk of sample data, you can
664     immediately call trackStop(). This will take effect after the current
665     buffer has been played.
666 
667     The "user" parameter is the same value that was passed to newTrack()
668  */
669 typedef void (*ANPAudioCallbackProc)(ANPAudioEvent event, void* user,
670                                      ANPAudioBuffer* buffer);
671 
672 struct ANPAudioTrack;   // abstract type for audio tracks
673 
674 struct ANPAudioTrackInterfaceV0 : ANPInterface {
675     /** Create a new audio track, or NULL on failure. The track is initially in
676         the stopped state and therefore ANPAudioCallbackProc will not be called
677         until the track is started.
678      */
679     ANPAudioTrack*  (*newTrack)(uint32_t sampleRate,    // sampling rate in Hz
680                                 ANPSampleFormat,
681                                 int channelCount,       // MONO=1, STEREO=2
682                                 ANPAudioCallbackProc,
683                                 void* user);
684     /** Deletes a track that was created using newTrack.  The track can be
685         deleted in any state and it waits for the ANPAudioCallbackProc thread
686         to exit before returning.
687      */
688     void (*deleteTrack)(ANPAudioTrack*);
689 
690     void (*start)(ANPAudioTrack*);
691     void (*pause)(ANPAudioTrack*);
692     void (*stop)(ANPAudioTrack*);
693     /** Returns true if the track is not playing (e.g. pause or stop was called,
694         or start was never called.
695      */
696     bool (*isStopped)(ANPAudioTrack*);
697 };
698 
699 struct ANPAudioTrackInterfaceV1 : ANPAudioTrackInterfaceV0 {
700     /** Returns the track's latency in milliseconds. */
701     uint32_t (*trackLatency)(ANPAudioTrack*);
702 };
703 
704 
705 ///////////////////////////////////////////////////////////////////////////////
706 // DEFINITION OF VALUES PASSED THROUGH NPP_HandleEvent
707 
708 enum ANPEventTypes {
709     kNull_ANPEventType          = 0,
710     kKey_ANPEventType           = 1,
711     /** Mouse events are triggered by either clicking with the navigational pad
712         or by tapping the touchscreen (if the kDown_ANPTouchAction is handled by
713         the plugin then no mouse event is generated).  The kKey_ANPEventFlag has
714         to be set to true in order to receive these events.
715      */
716     kMouse_ANPEventType         = 2,
717     /** Touch events are generated when the user touches on the screen. The
718         kTouch_ANPEventFlag has to be set to true in order to receive these
719         events.
720      */
721     kTouch_ANPEventType         = 3,
722     /** Only triggered by a plugin using the kBitmap_ANPDrawingModel. This event
723         signals that the plugin needs to redraw itself into the provided bitmap.
724      */
725     kDraw_ANPEventType          = 4,
726     kLifecycle_ANPEventType     = 5,
727 
728     /** This event type is completely defined by the plugin.
729         When creating an event, the caller must always set the first
730         two fields, the remaining data is optional.
731             ANPEvent evt;
732             evt.inSize = sizeof(ANPEvent);
733             evt.eventType = kCustom_ANPEventType
734             // other data slots are optional
735             evt.other[] = ...;
736         To post a copy of the event, call
737             eventInterface->postEvent(myNPPInstance, &evt);
738         That call makes a copy of the event struct, and post that on the event
739         queue for the plugin.
740      */
741     kCustom_ANPEventType   = 6,
742 };
743 typedef int32_t ANPEventType;
744 
745 enum ANPKeyActions {
746     kDown_ANPKeyAction  = 0,
747     kUp_ANPKeyAction    = 1,
748 };
749 typedef int32_t ANPKeyAction;
750 
751 #include "ANPKeyCodes.h"
752 typedef int32_t ANPKeyCode;
753 
754 enum ANPKeyModifiers {
755     kAlt_ANPKeyModifier     = 1 << 0,
756     kShift_ANPKeyModifier   = 1 << 1,
757 };
758 // bit-field containing some number of ANPKeyModifier bits
759 typedef uint32_t ANPKeyModifier;
760 
761 enum ANPMouseActions {
762     kDown_ANPMouseAction  = 0,
763     kUp_ANPMouseAction    = 1,
764 };
765 typedef int32_t ANPMouseAction;
766 
767 enum ANPTouchActions {
768     /** This occurs when the user first touches on the screen. As such, this
769         action will always occur prior to any of the other touch actions. If
770         the plugin chooses to not handle this action then no other events
771         related to that particular touch gesture will be generated.
772      */
773     kDown_ANPTouchAction        = 0,
774     kUp_ANPTouchAction          = 1,
775     kMove_ANPTouchAction        = 2,
776     kCancel_ANPTouchAction      = 3,
777     // The web view will ignore the return value from the following actions
778     kLongPress_ANPTouchAction   = 4,
779     kDoubleTap_ANPTouchAction   = 5,
780 };
781 typedef int32_t ANPTouchAction;
782 
783 enum ANPLifecycleActions {
784     /** The web view containing this plugin has been paused.  See documentation
785         on the android activity lifecycle for more information.
786      */
787     kPause_ANPLifecycleAction           = 0,
788     /** The web view containing this plugin has been resumed. See documentation
789         on the android activity lifecycle for more information.
790      */
791     kResume_ANPLifecycleAction          = 1,
792     /** The plugin has focus and is now the recipient of input events (e.g. key,
793         touch, etc.)
794      */
795     kGainFocus_ANPLifecycleAction       = 2,
796     /** The plugin has lost focus and will not receive any input events until it
797         regains focus. This event is always preceded by a GainFocus action.
798      */
799     kLoseFocus_ANPLifecycleAction       = 3,
800     /** The browser is running low on available memory and is requesting that
801         the plugin free any unused/inactive resources to prevent a performance
802         degradation.
803      */
804     kFreeMemory_ANPLifecycleAction      = 4,
805     /** The page has finished loading. This happens when the page's top level
806         frame reports that it has completed loading.
807      */
808     kOnLoad_ANPLifecycleAction          = 5,
809     /** The browser is honoring the plugin's request to go full screen. Upon
810         returning from this event the browser will resize the plugin's java
811         surface to full-screen coordinates.
812      */
813     kEnterFullScreen_ANPLifecycleAction = 6,
814     /** The browser has exited from full screen mode. Immediately prior to
815         sending this event the browser has resized the plugin's java surface to
816         its original coordinates.
817      */
818     kExitFullScreen_ANPLifecycleAction  = 7,
819     /** The plugin is visible to the user on the screen. This event will always
820         occur after a kOffScreen_ANPLifecycleAction event.
821      */
822     kOnScreen_ANPLifecycleAction        = 8,
823     /** The plugin is no longer visible to the user on the screen. This event
824         will always occur prior to an kOnScreen_ANPLifecycleAction event.
825      */
826     kOffScreen_ANPLifecycleAction       = 9,
827 };
828 typedef uint32_t ANPLifecycleAction;
829 
830 /* This is what is passed to NPP_HandleEvent() */
831 struct ANPEvent {
832     uint32_t        inSize;  // size of this struct in bytes
833     ANPEventType    eventType;
834     // use based on the value in eventType
835     union {
836         struct {
837             ANPKeyAction    action;
838             ANPKeyCode      nativeCode;
839             int32_t         virtualCode;    // windows virtual key code
840             ANPKeyModifier  modifiers;
841             int32_t         repeatCount;    // 0 for initial down (or up)
842             int32_t         unichar;        // 0 if there is no value
843         } key;
844         struct {
845             ANPMouseAction  action;
846             int32_t         x;  // relative to your "window" (0...width)
847             int32_t         y;  // relative to your "window" (0...height)
848         } mouse;
849         struct {
850             ANPTouchAction  action;
851             ANPKeyModifier  modifiers;
852             int32_t         x;  // relative to your "window" (0...width)
853             int32_t         y;  // relative to your "window" (0...height)
854         } touch;
855         struct {
856             ANPLifecycleAction  action;
857         } lifecycle;
858         struct {
859             ANPDrawingModel model;
860             // relative to (0,0) in top-left of your plugin
861             ANPRectI        clip;
862             // use based on the value in model
863             union {
864                 ANPBitmap   bitmap;
865                 struct {
866                     int32_t width;
867                     int32_t height;
868                 } surfaceSize;
869             } data;
870         } draw;
871     } data;
872 };
873 
874 
875 struct ANPEventInterfaceV0 : ANPInterface {
876     /** Post a copy of the specified event to the plugin. The event will be
877         delivered to the plugin in its main thread (the thread that receives
878         other ANPEvents). If, after posting before delivery, the NPP instance
879         is torn down, the event will be discarded.
880      */
881     void (*postEvent)(NPP inst, const ANPEvent* event);
882 };
883 
884 struct ANPSurfaceInterfaceV0 : ANPInterface {
885   /** Locks the surface from manipulation by other threads and provides a bitmap
886         to be written to.  The dirtyRect param specifies which portion of the
887         bitmap will be written to.  If the dirtyRect is NULL then the entire
888         surface will be considered dirty.  If the lock was successful the function
889         will return true and the bitmap will be set to point to a valid bitmap.
890         If not the function will return false and the bitmap will be set to NULL.
891   */
892   bool (*lock)(JNIEnv* env, jobject surface, ANPBitmap* bitmap, ANPRectI* dirtyRect);
893   /** Given a locked surface handle (i.e. result of a successful call to lock)
894         the surface is unlocked and the contents of the bitmap, specifically
895         those inside the dirtyRect are written to the screen.
896   */
897   void (*unlock)(JNIEnv* env, jobject surface);
898 };
899 
900 /**
901  * TODO should we not use EGL and GL data types for ABI safety?
902  */
903 struct ANPTextureInfo {
904     GLuint      textureId;
905     uint32_t    width;
906     uint32_t    height;
907     GLenum      internalFormat;
908 };
909 
910 typedef void* ANPEGLContext;
911 
912 struct ANPOpenGLInterfaceV0 : ANPInterface {
913     ANPEGLContext (*acquireContext)(NPP instance);
914 
915     ANPTextureInfo (*lockTexture)(NPP instance);
916 
917     void (*releaseTexture)(NPP instance, const ANPTextureInfo*);
918 
919     /**
920      * Invert the contents of the plugin on the y-axis.
921      * default is to not be inverted (i.e. use OpenGL coordinates)
922      */
923     void (*invertPluginContent)(NPP instance, bool isContentInverted);
924 };
925 
926 enum ANPPowerStates {
927     kDefault_ANPPowerState  = 0,
928     kScreenOn_ANPPowerState = 1
929 };
930 typedef int32_t ANPPowerState;
931 
932 struct ANPSystemInterfaceV1 : ANPInterface {
933     /** Return the path name for the current Application's plugin data directory,
934         or NULL if not supported
935      */
936     const char* (*getApplicationDataDirectory)();
937 
938     /** A helper function to load java classes from the plugin's apk.  The
939         function looks for a class given the fully qualified and null terminated
940         string representing the className. For example,
941 
942         const char* className = "com.android.mypackage.MyClass";
943 
944         If the class cannot be found or there is a problem loading the class
945         NULL will be returned.
946      */
947     jclass (*loadJavaClass)(NPP instance, const char* className);
948 
949     void (*setPowerState)(NPP instance, ANPPowerState powerState);
950 };
951 
952 struct ANPSystemInterfaceV2 : ANPInterface {
953     /** Return the path name for the current Application's plugin data directory,
954         or NULL if not supported. This directory will change depending on whether
955         or not the plugin is found within an incognito tab.
956      */
957     const char* (*getApplicationDataDirectory)(NPP instance);
958 
959     // redeclaration of existing features
960     jclass (*loadJavaClass)(NPP instance, const char* className);
961     void (*setPowerState)(NPP instance, ANPPowerState powerState);
962 };
963 
964 typedef void* ANPNativeWindow;
965 
966 /** Called to notify the plugin that a video frame has been composited by the
967 *  browser for display.  This will be called in a separate thread and as such
968 *  you cannot call releaseNativeWindow from the callback.
969 *
970 *  The timestamp is in nanoseconds, and is monotonically increasing.
971 */
972 typedef void (*ANPVideoFrameCallbackProc)(ANPNativeWindow* window, int64_t timestamp);
973 
974 struct ANPVideoInterfaceV1 : ANPInterface {
975 
976     /**
977      * Constructs a new native window to be used for rendering video content.
978      *
979      * Subsequent calls will produce new windows, but may also return NULL after
980      * n attempts if the browser has reached it's limit. Further, if the browser
981      * is unable to acquire the window quickly it may also return NULL in order
982      * to not prevent the plugin from executing. A subsequent call will then
983      * return the window if it is avaiable.
984      *
985      * NOTE: The hardware may fail if you try to decode more than the allowable
986      * number of videos supported on that device.
987      */
988     ANPNativeWindow (*acquireNativeWindow)(NPP instance);
989 
990     /**
991      * Sets the rectangle that specifies where the video content is to be drawn.
992      * The dimensions are in document space. Further, if the rect is NULL the
993      * browser will not attempt to draw the window, therefore do not set the
994      * dimensions until you queue the first buffer in the window.
995      */
996     void (*setWindowDimensions)(NPP instance, const ANPNativeWindow window, const ANPRectF* dimensions);
997 
998     /**
999      */
1000     void (*releaseNativeWindow)(NPP instance, ANPNativeWindow window);
1001 
1002     /** Set a callback to be notified when an ANPNativeWindow is composited by
1003      *  the browser.
1004      */
1005     void (*setFramerateCallback)(NPP instance, const ANPNativeWindow window, ANPVideoFrameCallbackProc);
1006 };
1007 
1008 struct ANPNativeWindowInterfaceV0 : ANPInterface {
1009     /**
1010      * Constructs a new native window to be used for rendering plugin content.
1011      *
1012      * Subsequent calls will return the original constructed window. Further, if
1013      * the browser is unable to acquire the window quickly it may return NULL in
1014      * order to not block the plugin indefinitely. A subsequent call will then
1015      * return the window if it is available.
1016      */
1017     ANPNativeWindow (*acquireNativeWindow)(NPP instance);
1018 
1019     /**
1020      * Invert the contents of the plugin on the y-axis.
1021      * default is to not be inverted (e.g. use OpenGL coordinates)
1022      */
1023     void (*invertPluginContent)(NPP instance, bool isContentInverted);
1024 };
1025 
1026 
1027 #endif
1028