OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <dlfcn.h> |
| 6 |
| 7 #include "base/singleton.h" |
| 8 #include "chrome/common/io_surface_support_mac.h" |
| 9 |
| 10 typedef CFTypeRef (*IOSurfaceCreateProcPtr)(CFDictionaryRef properties); |
| 11 typedef uint32 (*IOSurfaceGetIDProcPtr)(CFTypeRef io_surface); |
| 12 typedef CFTypeRef (*IOSurfaceLookupProcPtr)(uint32 io_surface_id); |
| 13 typedef mach_port_t (*IOSurfaceCreateMachPortProcPtr)(CFTypeRef io_surface); |
| 14 typedef CFTypeRef (*IOSurfaceLookupFromMachPortProcPtr)(mach_port_t port); |
| 15 typedef CGLError (*CGLTexImageIOSurface2DProcPtr)(CGLContextObj ctx, |
| 16 GLenum target, |
| 17 GLenum internal_format, |
| 18 GLsizei width, |
| 19 GLsizei height, |
| 20 GLenum format, |
| 21 GLenum type, |
| 22 CFTypeRef io_surface, |
| 23 GLuint plane); |
| 24 |
| 25 class IOSurfaceSupportImpl : public IOSurfaceSupport { |
| 26 public: |
| 27 static IOSurfaceSupportImpl* Initialize(); |
| 28 |
| 29 bool InitializedSuccessfully() { |
| 30 return initialized_successfully_; |
| 31 } |
| 32 |
| 33 virtual CFStringRef GetKIOSurfaceWidth(); |
| 34 virtual CFStringRef GetKIOSurfaceHeight(); |
| 35 virtual CFStringRef GetKIOSurfaceBytesPerElement(); |
| 36 virtual CFStringRef GetKIOSurfaceIsGlobal(); |
| 37 |
| 38 virtual CFTypeRef IOSurfaceCreate(CFDictionaryRef properties); |
| 39 virtual uint32 IOSurfaceGetID(CFTypeRef io_surface); |
| 40 virtual CFTypeRef IOSurfaceLookup(uint32 io_surface_id); |
| 41 virtual mach_port_t IOSurfaceCreateMachPort(CFTypeRef io_surface); |
| 42 virtual CFTypeRef IOSurfaceLookupFromMachPort(mach_port_t port); |
| 43 |
| 44 virtual CGLError CGLTexImageIOSurface2D(CGLContextObj ctx, |
| 45 GLenum target, |
| 46 GLenum internal_format, |
| 47 GLsizei width, |
| 48 GLsizei height, |
| 49 GLenum format, |
| 50 GLenum type, |
| 51 CFTypeRef io_surface, |
| 52 GLuint plane); |
| 53 |
| 54 private: |
| 55 IOSurfaceSupportImpl(); |
| 56 ~IOSurfaceSupportImpl(); |
| 57 |
| 58 void* iosurface_handle_; |
| 59 void* opengl_handle_; |
| 60 CFStringRef k_io_surface_width_; |
| 61 CFStringRef k_io_surface_height_; |
| 62 CFStringRef k_io_surface_bytes_per_element_; |
| 63 CFStringRef k_io_surface_is_global_; |
| 64 IOSurfaceCreateProcPtr io_surface_create_; |
| 65 IOSurfaceGetIDProcPtr io_surface_get_id_; |
| 66 IOSurfaceLookupProcPtr io_surface_lookup_; |
| 67 IOSurfaceCreateMachPortProcPtr io_surface_create_mach_port_; |
| 68 IOSurfaceLookupFromMachPortProcPtr io_surface_lookup_from_mach_port_; |
| 69 CGLTexImageIOSurface2DProcPtr cgl_tex_image_io_surface_2d_; |
| 70 bool initialized_successfully_; |
| 71 |
| 72 friend struct DefaultSingletonTraits<IOSurfaceSupportImpl>; |
| 73 DISALLOW_EVIL_CONSTRUCTORS(IOSurfaceSupportImpl); |
| 74 }; |
| 75 |
| 76 static Singleton<IOSurfaceSupportImpl> sole_instance_; |
| 77 |
| 78 IOSurfaceSupportImpl* IOSurfaceSupportImpl::Initialize() { |
| 79 IOSurfaceSupportImpl* impl = sole_instance_.get(); |
| 80 if (impl->InitializedSuccessfully()) |
| 81 return impl; |
| 82 return NULL; |
| 83 } |
| 84 |
| 85 CFStringRef IOSurfaceSupportImpl::GetKIOSurfaceWidth() { |
| 86 return k_io_surface_width_; |
| 87 } |
| 88 |
| 89 CFStringRef IOSurfaceSupportImpl::GetKIOSurfaceHeight() { |
| 90 return k_io_surface_height_; |
| 91 } |
| 92 |
| 93 CFStringRef IOSurfaceSupportImpl::GetKIOSurfaceBytesPerElement() { |
| 94 return k_io_surface_bytes_per_element_; |
| 95 } |
| 96 |
| 97 CFStringRef IOSurfaceSupportImpl::GetKIOSurfaceIsGlobal() { |
| 98 return k_io_surface_is_global_; |
| 99 } |
| 100 |
| 101 CFTypeRef IOSurfaceSupportImpl::IOSurfaceCreate(CFDictionaryRef properties) { |
| 102 return io_surface_create_(properties); |
| 103 } |
| 104 |
| 105 uint32 IOSurfaceSupportImpl::IOSurfaceGetID( |
| 106 CFTypeRef io_surface) { |
| 107 return io_surface_get_id_(io_surface); |
| 108 } |
| 109 |
| 110 CFTypeRef IOSurfaceSupportImpl::IOSurfaceLookup(uint32 io_surface_id) { |
| 111 return io_surface_lookup_(io_surface_id); |
| 112 } |
| 113 |
| 114 mach_port_t IOSurfaceSupportImpl::IOSurfaceCreateMachPort( |
| 115 CFTypeRef io_surface) { |
| 116 return io_surface_create_mach_port_(io_surface); |
| 117 } |
| 118 |
| 119 CFTypeRef IOSurfaceSupportImpl::IOSurfaceLookupFromMachPort(mach_port_t port) { |
| 120 return io_surface_lookup_from_mach_port_(port); |
| 121 } |
| 122 |
| 123 CGLError IOSurfaceSupportImpl::CGLTexImageIOSurface2D(CGLContextObj ctx, |
| 124 GLenum target, |
| 125 GLenum internal_format, |
| 126 GLsizei width, |
| 127 GLsizei height, |
| 128 GLenum format, |
| 129 GLenum type, |
| 130 CFTypeRef io_surface, |
| 131 GLuint plane) { |
| 132 return cgl_tex_image_io_surface_2d_(ctx, |
| 133 target, |
| 134 internal_format, |
| 135 width, |
| 136 height, |
| 137 format, |
| 138 type, |
| 139 io_surface, |
| 140 plane); |
| 141 } |
| 142 |
| 143 IOSurfaceSupportImpl::IOSurfaceSupportImpl() |
| 144 : iosurface_handle_(NULL), |
| 145 opengl_handle_(NULL), |
| 146 k_io_surface_width_(NULL), |
| 147 k_io_surface_height_(NULL), |
| 148 k_io_surface_bytes_per_element_(NULL), |
| 149 k_io_surface_is_global_(NULL), |
| 150 io_surface_create_(NULL), |
| 151 io_surface_get_id_(NULL), |
| 152 io_surface_lookup_(NULL), |
| 153 io_surface_create_mach_port_(NULL), |
| 154 io_surface_lookup_from_mach_port_(NULL), |
| 155 cgl_tex_image_io_surface_2d_(NULL), |
| 156 initialized_successfully_(false) { |
| 157 iosurface_handle_ = dlopen( |
| 158 "/System/Library/Frameworks/IOSurface.framework/IOSurface", |
| 159 RTLD_LAZY | RTLD_LOCAL); |
| 160 if (!iosurface_handle_) |
| 161 return; |
| 162 opengl_handle_ = dlopen( |
| 163 "/System/Library/Frameworks/OpenGL.framework/OpenGL", |
| 164 RTLD_LAZY | RTLD_LOCAL); |
| 165 if (!opengl_handle_) { |
| 166 dlclose(iosurface_handle_); |
| 167 iosurface_handle_ = NULL; |
| 168 return; |
| 169 } |
| 170 |
| 171 void* surface_width_ptr = dlsym(iosurface_handle_, "kIOSurfaceWidth"); |
| 172 void* surface_height_ptr = dlsym(iosurface_handle_, "kIOSurfaceHeight"); |
| 173 void* surface_bytes_per_element_ptr = |
| 174 dlsym(iosurface_handle_, "kIOSurfaceBytesPerElement"); |
| 175 void* surface_is_global_ptr = |
| 176 dlsym(iosurface_handle_, "kIOSurfaceIsGlobal"); |
| 177 void* surface_create_ptr = dlsym(iosurface_handle_, "IOSurfaceCreate"); |
| 178 void* surface_get_id_ptr = dlsym(iosurface_handle_, "IOSurfaceGetID"); |
| 179 void* surface_lookup_ptr = dlsym(iosurface_handle_, "IOSurfaceLookup"); |
| 180 void* surface_create_mach_port_ptr = |
| 181 dlsym(iosurface_handle_, "IOSurfaceCreateMachPort"); |
| 182 void* surface_lookup_from_mach_port_ptr = |
| 183 dlsym(iosurface_handle_, "IOSurfaceLookupFromMachPort"); |
| 184 void* tex_image_io_surface_2d_ptr = |
| 185 dlsym(opengl_handle_, "CGLTexImageIOSurface2D"); |
| 186 if (!surface_width_ptr || |
| 187 !surface_height_ptr || |
| 188 !surface_bytes_per_element_ptr || |
| 189 !surface_is_global_ptr || |
| 190 !surface_create_ptr || |
| 191 !surface_get_id_ptr || |
| 192 !surface_lookup_ptr || |
| 193 !surface_create_mach_port_ptr || |
| 194 !surface_lookup_from_mach_port_ptr || |
| 195 !tex_image_io_surface_2d_ptr) { |
| 196 dlclose(iosurface_handle_); |
| 197 iosurface_handle_ = NULL; |
| 198 dlclose(opengl_handle_); |
| 199 opengl_handle_ = NULL; |
| 200 return; |
| 201 } |
| 202 |
| 203 k_io_surface_width_ = *static_cast<CFStringRef*>(surface_width_ptr); |
| 204 k_io_surface_height_ = *static_cast<CFStringRef*>(surface_height_ptr); |
| 205 k_io_surface_bytes_per_element_ = |
| 206 *static_cast<CFStringRef*>(surface_bytes_per_element_ptr); |
| 207 k_io_surface_is_global_ = *static_cast<CFStringRef*>(surface_is_global_ptr); |
| 208 io_surface_create_ = reinterpret_cast<IOSurfaceCreateProcPtr>( |
| 209 surface_create_ptr); |
| 210 io_surface_get_id_ = |
| 211 reinterpret_cast<IOSurfaceGetIDProcPtr>(surface_get_id_ptr); |
| 212 io_surface_lookup_ = |
| 213 reinterpret_cast<IOSurfaceLookupProcPtr>(surface_lookup_ptr); |
| 214 io_surface_create_mach_port_ = |
| 215 reinterpret_cast<IOSurfaceCreateMachPortProcPtr>( |
| 216 surface_create_mach_port_ptr); |
| 217 io_surface_lookup_from_mach_port_ = |
| 218 reinterpret_cast<IOSurfaceLookupFromMachPortProcPtr>( |
| 219 surface_lookup_from_mach_port_ptr); |
| 220 cgl_tex_image_io_surface_2d_ = |
| 221 reinterpret_cast<CGLTexImageIOSurface2DProcPtr>( |
| 222 tex_image_io_surface_2d_ptr); |
| 223 initialized_successfully_ = true; |
| 224 } |
| 225 |
| 226 IOSurfaceSupportImpl::~IOSurfaceSupportImpl() { |
| 227 if (iosurface_handle_) |
| 228 dlclose(iosurface_handle_); |
| 229 if (opengl_handle_) |
| 230 dlclose(opengl_handle_); |
| 231 } |
| 232 |
| 233 IOSurfaceSupport* IOSurfaceSupport::Initialize() { |
| 234 return IOSurfaceSupportImpl::Initialize(); |
| 235 } |
| 236 |
| 237 IOSurfaceSupport::IOSurfaceSupport() { |
| 238 } |
| 239 |
| 240 IOSurfaceSupport::~IOSurfaceSupport() { |
| 241 } |
| 242 |
OLD | NEW |