1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "EGLImageHelpers.h"
8 #include "GLContext.h"
9 #include "GLLibraryEGL.h"
10 
11 namespace mozilla {
12 namespace layers {
13 
14 using namespace gl;
15 
EGLImageCreateFromNativeBuffer(GLContext * aGL,void * aBuffer,const gfx::IntSize & aCropSize)16 EGLImage EGLImageCreateFromNativeBuffer(GLContext* aGL, void* aBuffer,
17                                         const gfx::IntSize& aCropSize) {
18   EGLint attrs[] = {
19       LOCAL_EGL_IMAGE_PRESERVED,
20       LOCAL_EGL_TRUE,
21       LOCAL_EGL_NONE,
22       LOCAL_EGL_NONE,
23   };
24 
25   EGLint cropAttrs[] = {
26       LOCAL_EGL_IMAGE_PRESERVED,
27       LOCAL_EGL_TRUE,
28       LOCAL_EGL_IMAGE_CROP_LEFT_ANDROID,
29       0,
30       LOCAL_EGL_IMAGE_CROP_TOP_ANDROID,
31       0,
32       LOCAL_EGL_IMAGE_CROP_RIGHT_ANDROID,
33       aCropSize.width,
34       LOCAL_EGL_IMAGE_CROP_BOTTOM_ANDROID,
35       aCropSize.height,
36       LOCAL_EGL_NONE,
37       LOCAL_EGL_NONE,
38   };
39 
40   auto* egl = gl::GLLibraryEGL::Get();
41   bool hasCropRect = (aCropSize.width != 0 && aCropSize.height != 0);
42   EGLint* usedAttrs = attrs;
43   if (hasCropRect &&
44       egl->IsExtensionSupported(GLLibraryEGL::EGL_ANDROID_image_crop)) {
45     usedAttrs = cropAttrs;
46   }
47 
48   return egl->fCreateImage(egl->Display(), EGL_NO_CONTEXT,
49                            LOCAL_EGL_NATIVE_BUFFER_ANDROID, aBuffer, usedAttrs);
50 }
51 
EGLImageDestroy(GLContext * aGL,EGLImage aImage)52 void EGLImageDestroy(GLContext* aGL, EGLImage aImage) {
53   auto* egl = gl::GLLibraryEGL::Get();
54   egl->fDestroyImage(egl->Display(), aImage);
55 }
56 
57 }  // namespace layers
58 }  // namespace mozilla
59