1 /***************************************************************************
3 * Copyright 2010 BMW Car IT GmbH
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 ****************************************************************************/
20 #ifndef _BEAGLEEGLIMAGE_H_
21 #define _BEAGLEEGLIMAGE_H_
23 #include "TextureBinders/ITextureBinder.h"
25 #include "EGL/eglext.h"
26 #include <GLES2/gl2.h>
27 #include <GLES2/gl2ext.h>
29 #include "PlatformSurfaces/BeaglePlatformSurface.h"
31 class BeagleEglImage : public ITextureBinder {
34 LOG_INFO("BeagleEglImage", "Query EGL Extensions ...");
36 m_pfGLEglImageTargetTexture2DOES = (PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)
37 eglGetProcAddress("glEGLImageTargetTexture2DOES");
38 m_pfEglCreateImageKHR = (PFNEGLCREATEIMAGEKHRPROC)
39 eglGetProcAddress("eglCreateImageKHR");
40 m_pfEglDestroyImageKHR = (PFNEGLDESTROYIMAGEKHRPROC)
41 eglGetProcAddress("eglDestroyImageKHR");
43 if (!m_pfEglCreateImageKHR || !m_pfEglDestroyImageKHR || !m_pfGLEglImageTargetTexture2DOES)
45 LOG_ERROR("BeagleEglImage", "Query EGL Extensions failed");
48 m_eglDisplay = eglGetDisplay(0);
50 void bindSurfaceTexture(Surface*s){
51 // LOG_INFO("BeagleEglImage", "bindSurfaceTexture");
52 BeaglePlatformSurface* nativeSurface = (BeaglePlatformSurface*)s->platform;
53 glBindTexture(GL_TEXTURE_2D, nativeSurface->texture);
54 if (nativeSurface->eglImage != NULL){
55 // LOG_INFO("BeagleEglImage", "bindSurfaceTexture");
56 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
57 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
58 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
59 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
60 eglWaitNative(EGL_CORE_NATIVE_ENGINE);
61 m_pfGLEglImageTargetTexture2DOES(GL_TEXTURE_2D, nativeSurface->eglImage);
65 void unbindSurfaceTexture(Surface*s){
69 PlatformSurface* createPlatformSurface(Surface* s){
70 return new BeaglePlatformSurface(s);
73 void createClientBuffer(Surface* surface){
74 LOG_DEBUG("BeagleEglImage", "createClientBuffer");
75 BeaglePlatformSurface* nativeSurface = (BeaglePlatformSurface*)surface->platform;
76 NATIVE_PIXMAP_STRUCT* pNativePixmap = (NATIVE_PIXMAP_STRUCT*)malloc(sizeof(NATIVE_PIXMAP_STRUCT));
77 pNativePixmap->ePixelFormat = 2;
78 pNativePixmap->eRotation = 0;
79 pNativePixmap->lWidth = 256;//surface->OriginalSourceWidth;//480;
80 pNativePixmap->lHeight = 256;//surface->OriginalSourceHeight;//640;
81 pNativePixmap->lStride = pNativePixmap->lWidth* 32/8; //bitwidth/8
82 pNativePixmap->lSizeInBytes = pNativePixmap->lHeight * pNativePixmap->lStride;
83 pNativePixmap->lAddress = (long)CMEM_registerAlloc(surface->nativeHandle);
84 if(!pNativePixmap->lAddress)
86 LOG_ERROR("BeagleEglImage","CMEM_alloc returned NULL\n");
89 //Get the physical page corresponding to the above cmem buffer
90 pNativePixmap->pvAddress = surface->nativeHandle; //;CMEM_getPhys((void*)pNativePixmap->lAddress);
91 LOG_DEBUG("BeagleEglImage","Physical address = "<< pNativePixmap->pvAddress);
92 LOG_DEBUG("BeagleEglImage","Logical address = "<< pNativePixmap->lAddress);
94 nativeSurface->eglImage = m_pfEglCreateImageKHR(
96 EGL_NO_CONTEXT, //eglContext,
97 EGL_NATIVE_PIXMAP_KHR, //EGL_GL_TEXTURE_2D_KHR,
98 pNativePixmap, //(void*)textureId0,//
99 NULL //miplevel 0 is fine, thank you
102 if (nativeSurface->eglImage == 0)
104 LOG_DEBUG("BeagleEglImage", "could not allocate EGL Image for window");
106 glGenTextures(1,&nativeSurface->texture);
110 void destroyClientBuffer(Surface* surface){
111 LOG_DEBUG("BeagleEglImage", "destroyClientBuffer");
112 BeaglePlatformSurface* nativeSurface = (BeaglePlatformSurface*)surface->platform;
113 if (NULL!=nativeSurface && 0!=nativeSurface->eglImage){
114 EGLBoolean status = m_pfEglDestroyImageKHR(m_eglDisplay,nativeSurface->eglImage);
116 LOG_ERROR("BeagleEglImage", "could not delete EGLImage");
117 nativeSurface->eglImage = 0;
121 PFNEGLCREATEIMAGEKHRPROC m_pfEglCreateImageKHR;
122 PFNEGLDESTROYIMAGEKHRPROC m_pfEglDestroyImageKHR;
123 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC m_pfGLEglImageTargetTexture2DOES;
124 EGLDisplay m_eglDisplay;
127 #endif /* _BEAGLEEGLIMAGE_H_ */