DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (d8847129d134)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
#ifdef MOZ_WIDGET_ANDROID

#include "AndroidNativeWindow.h"
#include "prlink.h"

// #define ANDROID_NATIVE_WINDOW_DEBUG

#if defined(ANDROID_NATIVE_WINDOW_DEBUG) || defined(DEBUG)
#define ALOG(args...)  __android_log_print(ANDROID_LOG_INFO, "AndroidNativeWindow" , ## args)
#else
#define ALOG(args...) ((void)0)
#endif

using namespace mozilla::gfx;
using namespace mozilla::gl;
using namespace mozilla;

class NativeWindowLibrary
{
public:

  NativeWindowLibrary()
    : fANativeWindow_fromSurface(nullptr)
    , fANativeWindow_release(nullptr)
    , fANativeWindow_setBuffersGeometry(nullptr)
    , fANativeWindow_lock(nullptr)
    , fANativeWindow_unlockAndPost(nullptr)
    , fANativeWindow_getFormat(nullptr)
    , fANativeWindow_getWidth(nullptr)
    , fANativeWindow_getHeight(nullptr)
  {
    PRLibrary* lib = PR_LoadLibrary("libandroid.so");

    fANativeWindow_fromSurface = (pfnANativeWindow_fromSurface)PR_FindSymbol(lib, "ANativeWindow_fromSurface");
    fANativeWindow_release = (pfnANativeWindow_release)PR_FindSymbol(lib, "ANativeWindow_release");
    fANativeWindow_setBuffersGeometry = (pfnANativeWindow_setBuffersGeometry)PR_FindSymbol(lib, "ANativeWindow_setBuffersGeometry");
    fANativeWindow_lock = (pfnANativeWindow_lock)PR_FindSymbol(lib, "ANativeWindow_lock");
    fANativeWindow_unlockAndPost = (pfnANativeWindow_unlockAndPost)PR_FindSymbol(lib, "ANativeWindow_unlockAndPost");
    fANativeWindow_getFormat = (pfnANativeWindow_getFormat)PR_FindSymbol(lib, "ANativeWindow_getFormat");
    fANativeWindow_getWidth = (pfnANativeWindow_getWidth)PR_FindSymbol(lib, "ANativeWindow_getWidth");
    fANativeWindow_getHeight = (pfnANativeWindow_getHeight)PR_FindSymbol(lib, "ANativeWindow_getHeight");
  }

  void* ANativeWindow_fromSurface(JNIEnv* aEnv, jobject aSurface) {
    ALOG("%s: env=%p, surface=%p\n", __PRETTY_FUNCTION__, aEnv, aSurface);
    if (!Initialized()) {
      return nullptr;
    }

    return fANativeWindow_fromSurface(aEnv, aSurface);
  }

  void ANativeWindow_release(void* aWindow) {
    ALOG("%s: window=%p\n", __PRETTY_FUNCTION__, aWindow);
    if (!Initialized()) {
      return;
    }

    fANativeWindow_release(aWindow);
  }

  bool ANativeWindow_setBuffersGeometry(void* aWindow, int32_t aWidth, int32_t aHeight, int32_t aFormat) {
    ALOG("%s: window=%p, width=%d, height=%d, format=%d\n", __PRETTY_FUNCTION__, aWindow, aWidth, aHeight, aFormat);
    if (!Initialized()) {
      return nullptr;
    }

    return fANativeWindow_setBuffersGeometry(aWindow, aWidth, aHeight, (int32_t)aFormat) == 0;
  }

  bool ANativeWindow_lock(void* aWindow, void* out_buffer, void*in_out_dirtyBounds) {
    ALOG("%s: window=%p, out_buffer=%p, in_out_dirtyBounds=%p\n", __PRETTY_FUNCTION__,
         aWindow, out_buffer, in_out_dirtyBounds);
    if (!Initialized()) {
      return false;
    }

    return fANativeWindow_lock(aWindow, out_buffer, in_out_dirtyBounds) == 0;
  }

  bool ANativeWindow_unlockAndPost(void* aWindow) {
    ALOG("%s: window=%p\n", __PRETTY_FUNCTION__, aWindow);
    if (!Initialized()) {
      return false;
    }

    return fANativeWindow_unlockAndPost(aWindow) == 0;
  }

  AndroidWindowFormat ANativeWindow_getFormat(void* aWindow) {
    ALOG("%s: window=%p\n", __PRETTY_FUNCTION__, aWindow);
    if (!Initialized()) {
      return AndroidWindowFormat::Unknown;
    }

    return (AndroidWindowFormat)fANativeWindow_getFormat(aWindow);
  }

  int32_t ANativeWindow_getWidth(void* aWindow) {
    ALOG("%s: window=%p\n", __PRETTY_FUNCTION__, aWindow);
    if (!Initialized()) {
      return -1;
    }

    return fANativeWindow_getWidth(aWindow);
  }

  int32_t ANativeWindow_getHeight(void* aWindow) {
    ALOG("%s: window=%p\n", __PRETTY_FUNCTION__, aWindow);
    if (!Initialized()) {
      return -1;
    }

    return fANativeWindow_getHeight(aWindow);
  }

  bool Initialized() {
    return fANativeWindow_fromSurface && fANativeWindow_release && fANativeWindow_setBuffersGeometry
      && fANativeWindow_lock && fANativeWindow_unlockAndPost && fANativeWindow_getFormat && fANativeWindow_getWidth
      && fANativeWindow_getHeight;
  }

private:

  typedef void* (*pfnANativeWindow_fromSurface)(JNIEnv* env, jobject surface);
  pfnANativeWindow_fromSurface fANativeWindow_fromSurface;

  typedef void (*pfnANativeWindow_release)(void* window);
  pfnANativeWindow_release fANativeWindow_release;

  typedef int32_t (*pfnANativeWindow_setBuffersGeometry)(void* window, int32_t width, int32_t height, int32_t format);
  pfnANativeWindow_setBuffersGeometry fANativeWindow_setBuffersGeometry;

  typedef int32_t (*pfnANativeWindow_lock)(void *window, void *out_buffer, void *in_out_dirtyBounds);
  pfnANativeWindow_lock fANativeWindow_lock;

  typedef int32_t (*pfnANativeWindow_unlockAndPost)(void *window);
  pfnANativeWindow_unlockAndPost fANativeWindow_unlockAndPost;

  typedef AndroidWindowFormat (*pfnANativeWindow_getFormat)(void* window);
  pfnANativeWindow_getFormat fANativeWindow_getFormat;

  typedef int32_t (*pfnANativeWindow_getWidth)(void* window);
  pfnANativeWindow_getWidth fANativeWindow_getWidth;

  typedef int32_t (*pfnANativeWindow_getHeight)(void* window);
  pfnANativeWindow_getHeight fANativeWindow_getHeight;
};

static NativeWindowLibrary* sLibrary = nullptr;

static bool
EnsureInit()
{
  static bool initialized = false;
  if (!initialized) {
    if (!sLibrary) {
      sLibrary = new NativeWindowLibrary();
    }
    initialized = sLibrary->Initialized();
  }

  return initialized;
}


namespace mozilla {

/* static */ AndroidNativeWindow*
AndroidNativeWindow::CreateFromSurface(JNIEnv* aEnv, jobject aSurface)
{
  if (!EnsureInit()) {
    ALOG("Not initialized");
    return nullptr;
  }

  void* window = sLibrary->ANativeWindow_fromSurface(aEnv, aSurface);
  if (!window) {
    ALOG("Failed to create window from surface");
    return nullptr;
  }

  return new AndroidNativeWindow(window);
}

AndroidNativeWindow::~AndroidNativeWindow()
{
  if (EnsureInit() && mWindow) {
    sLibrary->ANativeWindow_release(mWindow);
    mWindow = nullptr;
  }
}

IntSize
AndroidNativeWindow::Size()
{
  MOZ_ASSERT(mWindow);
  if (!EnsureInit()) {
    return IntSize(0, 0);
  }

  return IntSize(sLibrary->ANativeWindow_getWidth(mWindow), sLibrary->ANativeWindow_getHeight(mWindow));
}

AndroidWindowFormat
AndroidNativeWindow::Format()
{
  MOZ_ASSERT(mWindow);
  if (!EnsureInit()) {
    return AndroidWindowFormat::Unknown;
  }

  return sLibrary->ANativeWindow_getFormat(mWindow);
}

bool
AndroidNativeWindow::SetBuffersGeometry(int32_t aWidth, int32_t aHeight, AndroidWindowFormat aFormat)
{
  MOZ_ASSERT(mWindow);
  if (!EnsureInit())
    return false;

  return sLibrary->ANativeWindow_setBuffersGeometry(mWindow, aWidth, aHeight, (int32_t)aFormat);
}

bool
AndroidNativeWindow::Lock(void** out_bits,int32_t* out_width, int32_t* out_height,
                          int32_t* out_stride, AndroidWindowFormat* out_format)
{
  /* Copied from native_window.h in Android NDK (platform-9) */
  typedef struct ANativeWindow_Buffer {
      // The number of pixels that are show horizontally.
      int32_t width;

      // The number of pixels that are shown vertically.
      int32_t height;

      // The number of *pixels* that a line in the buffer takes in
      // memory.  This may be >= width.
      int32_t stride;

      // The format of the buffer.  One of WINDOW_FORMAT_*
      int32_t format;

      // The actual bits.
      void* bits;

      // Do not touch.
      uint32_t reserved[6];
  } ANativeWindow_Buffer; 


  ANativeWindow_Buffer buffer;

  if (!sLibrary->ANativeWindow_lock(mWindow, &buffer, nullptr)) {
    ALOG("Failed to lock");
    return false;
  }

  *out_bits = buffer.bits;
  *out_width = buffer.width;
  *out_height = buffer.height;
  *out_stride = buffer.stride;
  *out_format = (AndroidWindowFormat)buffer.format;
  return true;
}

bool
AndroidNativeWindow::UnlockAndPost()
{
  if (!EnsureInit()) {
    ALOG("Not initialized");
    return false;
  }

  return sLibrary->ANativeWindow_unlockAndPost(mWindow);
}

}

#endif // MOZ_WIDGET_ANDROID