1 /* 2 * Copyright 2010, 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 #ifndef SharedTexture_h 27 #define SharedTexture_h 28 29 #include "TextureInfo.h" 30 #include <EGL/egl.h> 31 #include <EGL/eglext.h> 32 #include <GLES2/gl2.h> 33 #include <GLES2/gl2ext.h> 34 #include <utils/threads.h> 35 36 namespace WebCore { 37 38 /** 39 * SharedTexture is a class that encapsulates all the necessary variables 40 * needed to share a single texture across threads. In the case that threads 41 * communicate using EGL's sharedContext mechanism or do not support the proper 42 * EGLImage extensions the targetTexture, eglImage, and isNewImage variables are 43 * not used. 44 */ 45 class SharedTexture { 46 public: 47 // consumer thread functions 48 SharedTexture(SharedTextureMode mode); 49 ~SharedTexture(); 50 51 TextureInfo* lockSource(); 52 void releaseSource(); 53 54 TextureInfo* lockTarget(); 55 void releaseTarget(); 56 57 // these locks are only used for the methods below lock()58 void lock() { m_lock.lock(); } unlock()59 void unlock() { m_lock.unlock(); } 60 61 void initSourceTexture(); // producer thread only 62 void deleteSourceTexture(); // producer thread only 63 void deleteTargetTexture(); // consumer thread only getSourceTextureId()64 GLuint getSourceTextureId() { return m_sourceTexture->m_textureId; } getTargetTextureId()65 GLuint getTargetTextureId() { return m_targetTexture->m_textureId; } getEGLImage()66 EGLImageKHR getEGLImage() { return m_eglImage; } 67 68 private: 69 /** 70 * The mutex is used to ensure that the contents of the struct are current across 71 * threads and that only one thread is manipulating the texture at a given time. 72 */ 73 android::Mutex m_lock; 74 /** 75 * The texture and its associated metadata that is used by the producer. The 76 * texture is created in the producer's thread and can only be read by the 77 * consumer when the consumer shares the same context as the producer. The 78 * metadata is used to track changes to the texture that would orphan the 79 * target texture and require a new EGLImage to be constructed. 80 */ 81 TextureInfo* m_sourceTexture; 82 /** 83 * The target texture stores the id and metadata of the texture that is to be 84 * used by the consumer. In the case where EGLImages are supported this hold 85 * the current eglImage target. 86 */ 87 TextureInfo* m_targetTexture; 88 /** 89 * The EGLImage is used to share the texture between EGLContexts on two 90 * different threads. This serves as an alternative to sharing the contexts 91 * but is only used if GL and EGL support the required extensions. 92 */ 93 EGLImageKHR m_eglImage; 94 /** 95 * This flag is used to determine if the eglImage has been updated. This 96 * signals the consumer thread to rebind the targetTexture to the new image. 97 */ 98 bool m_isNewImage; 99 /** 100 * The sync allows the consumer to release the lock prior to the commands 101 * executing on the GPU. Prior to releasing the lock the consumer creates 102 * a sync object and stores it here. After locking the texture the client 103 * must check that the sync has completed prior to manipulating the texture. 104 * This value is only used if the proper EGL extensions are supported. 105 */ 106 EGLSyncKHR m_syncObject; 107 108 EGLDisplay m_display; 109 110 bool m_supportsEGLImage; 111 bool m_supportsEGLFenceSyncKHR; 112 113 SharedTextureMode m_sharedTextureMode; 114 }; 115 116 } // namespace WebCore 117 118 #endif // SharedTexture_h 119