ce8222e8fb050d3de48ab5828145a1f4d6c8e85f
[profile/ivi/layer-management.git] / LayerManagerPlugins / Renderers / Graphic / include / TextureBinders / BeagleEglImage.h
1 /***************************************************************************
2 *
3 * Copyright 2010 BMW Car IT GmbH
4 *
5 *
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
9 *
10 *               http://www.apache.org/licenses/LICENSE-2.0
11 *
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.
17 *
18 ****************************************************************************/
19
20 #ifndef _BEAGLEEGLIMAGE_H_
21 #define _BEAGLEEGLIMAGE_H_
22
23 #include "TextureBinders/ITextureBinder.h"
24 #include <EGL/egl.h>
25 #include "EGL/eglext.h"
26 #include <GLES2/gl2.h>
27 #include <GLES2/gl2ext.h>
28 #include <cmem.h>
29 #include "PlatformSurfaces/BeaglePlatformSurface.h"
30
31 class BeagleEglImage : public ITextureBinder {
32 public:
33         BeagleEglImage(){
34                 LOG_INFO("BeagleEglImage", "Query EGL Extensions ...");
35                 CMEM_init();
36                 m_pfGLEglImageTargetTexture2DOES = (PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)
37                                         eglGetProcAddress("glEGLImageTargetTexture2DOES");
38                 m_pfEglCreateImageKHR = (PFNEGLCREATEIMAGEKHRPROC)
39                                         eglGetProcAddress("eglCreateImageKHR");
40                 m_pfEglDestroyImageKHR = (PFNEGLDESTROYIMAGEKHRPROC)
41                                         eglGetProcAddress("eglDestroyImageKHR");
42
43                 if (!m_pfEglCreateImageKHR || !m_pfEglDestroyImageKHR || !m_pfGLEglImageTargetTexture2DOES)
44                 {
45                         LOG_ERROR("BeagleEglImage", "Query EGL Extensions failed");
46                 }
47
48                 m_eglDisplay = eglGetDisplay(0);
49         }
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);
62                 }
63         }
64
65         void unbindSurfaceTexture(Surface*s){
66
67         }
68
69         PlatformSurface* createPlatformSurface(Surface* s){
70                 return new BeaglePlatformSurface(s);
71         }
72
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)
85                 {
86                         LOG_ERROR("BeagleEglImage","CMEM_alloc returned NULL\n");
87                         exit(-1);
88                 }
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);
93
94                 nativeSurface->eglImage = m_pfEglCreateImageKHR(
95                                 m_eglDisplay,
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
100                 );
101
102                 if (nativeSurface->eglImage == 0)
103                 {
104                         LOG_DEBUG("BeagleEglImage", "could not allocate EGL Image for window");
105                 } else {
106                         glGenTextures(1,&nativeSurface->texture);
107                 }
108         }
109
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);
115                         if (!status)
116                                 LOG_ERROR("BeagleEglImage", "could not delete EGLImage");
117                         nativeSurface->eglImage = 0;
118                 }
119         }
120 private:
121         PFNEGLCREATEIMAGEKHRPROC                m_pfEglCreateImageKHR;
122         PFNEGLDESTROYIMAGEKHRPROC           m_pfEglDestroyImageKHR;
123         PFNGLEGLIMAGETARGETTEXTURE2DOESPROC m_pfGLEglImageTargetTexture2DOES;
124         EGLDisplay m_eglDisplay;
125 };
126
127 #endif /* _BEAGLEEGLIMAGE_H_ */