| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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/memory/singleton.h" | |
| 8 #include "ui/gfx/surface/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 size_t (*IOSurfaceGetWidthPtr)(CFTypeRef io_surface); | |
| 16 typedef size_t (*IOSurfaceGetHeightPtr)(CFTypeRef io_surface); | |
| 17 typedef CGLError (*CGLTexImageIOSurface2DProcPtr)(CGLContextObj ctx, | |
| 18 GLenum target, | |
| 19 GLenum internal_format, | |
| 20 GLsizei width, | |
| 21 GLsizei height, | |
| 22 GLenum format, | |
| 23 GLenum type, | |
| 24 CFTypeRef io_surface, | |
| 25 GLuint plane); | |
| 26 | |
| 27 class IOSurfaceSupportImpl : public IOSurfaceSupport { | |
| 28 public: | |
| 29 static IOSurfaceSupportImpl* GetInstance(); | |
| 30 | |
| 31 bool InitializedSuccessfully() { | |
| 32 return initialized_successfully_; | |
| 33 } | |
| 34 | |
| 35 virtual CFStringRef GetKIOSurfaceWidth(); | |
| 36 virtual CFStringRef GetKIOSurfaceHeight(); | |
| 37 virtual CFStringRef GetKIOSurfaceBytesPerElement(); | |
| 38 virtual CFStringRef GetKIOSurfaceIsGlobal(); | |
| 39 | |
| 40 virtual CFTypeRef IOSurfaceCreate(CFDictionaryRef properties); | |
| 41 virtual uint32 IOSurfaceGetID(CFTypeRef io_surface); | |
| 42 virtual CFTypeRef IOSurfaceLookup(uint32 io_surface_id); | |
| 43 virtual mach_port_t IOSurfaceCreateMachPort(CFTypeRef io_surface); | |
| 44 virtual CFTypeRef IOSurfaceLookupFromMachPort(mach_port_t port); | |
| 45 | |
| 46 virtual size_t IOSurfaceGetWidth(CFTypeRef io_surface); | |
| 47 virtual size_t IOSurfaceGetHeight(CFTypeRef io_surface); | |
| 48 | |
| 49 virtual CGLError CGLTexImageIOSurface2D(CGLContextObj ctx, | |
| 50 GLenum target, | |
| 51 GLenum internal_format, | |
| 52 GLsizei width, | |
| 53 GLsizei height, | |
| 54 GLenum format, | |
| 55 GLenum type, | |
| 56 CFTypeRef io_surface, | |
| 57 GLuint plane); | |
| 58 | |
| 59 private: | |
| 60 IOSurfaceSupportImpl(); | |
| 61 ~IOSurfaceSupportImpl(); | |
| 62 | |
| 63 void* iosurface_handle_; | |
| 64 void* opengl_handle_; | |
| 65 CFStringRef k_io_surface_width_; | |
| 66 CFStringRef k_io_surface_height_; | |
| 67 CFStringRef k_io_surface_bytes_per_element_; | |
| 68 CFStringRef k_io_surface_is_global_; | |
| 69 IOSurfaceCreateProcPtr io_surface_create_; | |
| 70 IOSurfaceGetIDProcPtr io_surface_get_id_; | |
| 71 IOSurfaceLookupProcPtr io_surface_lookup_; | |
| 72 IOSurfaceCreateMachPortProcPtr io_surface_create_mach_port_; | |
| 73 IOSurfaceLookupFromMachPortProcPtr io_surface_lookup_from_mach_port_; | |
| 74 IOSurfaceGetWidthPtr io_surface_get_width_; | |
| 75 IOSurfaceGetHeightPtr io_surface_get_height_; | |
| 76 CGLTexImageIOSurface2DProcPtr cgl_tex_image_io_surface_2d_; | |
| 77 bool initialized_successfully_; | |
| 78 | |
| 79 friend struct DefaultSingletonTraits<IOSurfaceSupportImpl>; | |
| 80 DISALLOW_COPY_AND_ASSIGN(IOSurfaceSupportImpl); | |
| 81 }; | |
| 82 | |
| 83 IOSurfaceSupportImpl* IOSurfaceSupportImpl::GetInstance() { | |
| 84 IOSurfaceSupportImpl* impl = Singleton<IOSurfaceSupportImpl>::get(); | |
| 85 if (impl->InitializedSuccessfully()) | |
| 86 return impl; | |
| 87 return NULL; | |
| 88 } | |
| 89 | |
| 90 CFStringRef IOSurfaceSupportImpl::GetKIOSurfaceWidth() { | |
| 91 return k_io_surface_width_; | |
| 92 } | |
| 93 | |
| 94 CFStringRef IOSurfaceSupportImpl::GetKIOSurfaceHeight() { | |
| 95 return k_io_surface_height_; | |
| 96 } | |
| 97 | |
| 98 CFStringRef IOSurfaceSupportImpl::GetKIOSurfaceBytesPerElement() { | |
| 99 return k_io_surface_bytes_per_element_; | |
| 100 } | |
| 101 | |
| 102 CFStringRef IOSurfaceSupportImpl::GetKIOSurfaceIsGlobal() { | |
| 103 return k_io_surface_is_global_; | |
| 104 } | |
| 105 | |
| 106 CFTypeRef IOSurfaceSupportImpl::IOSurfaceCreate(CFDictionaryRef properties) { | |
| 107 return io_surface_create_(properties); | |
| 108 } | |
| 109 | |
| 110 uint32 IOSurfaceSupportImpl::IOSurfaceGetID( | |
| 111 CFTypeRef io_surface) { | |
| 112 return io_surface_get_id_(io_surface); | |
| 113 } | |
| 114 | |
| 115 CFTypeRef IOSurfaceSupportImpl::IOSurfaceLookup(uint32 io_surface_id) { | |
| 116 return io_surface_lookup_(io_surface_id); | |
| 117 } | |
| 118 | |
| 119 mach_port_t IOSurfaceSupportImpl::IOSurfaceCreateMachPort( | |
| 120 CFTypeRef io_surface) { | |
| 121 return io_surface_create_mach_port_(io_surface); | |
| 122 } | |
| 123 | |
| 124 CFTypeRef IOSurfaceSupportImpl::IOSurfaceLookupFromMachPort(mach_port_t port) { | |
| 125 return io_surface_lookup_from_mach_port_(port); | |
| 126 } | |
| 127 | |
| 128 size_t IOSurfaceSupportImpl::IOSurfaceGetWidth(CFTypeRef io_surface) { | |
| 129 return io_surface_get_width_(io_surface); | |
| 130 } | |
| 131 | |
| 132 size_t IOSurfaceSupportImpl::IOSurfaceGetHeight(CFTypeRef io_surface) { | |
| 133 return io_surface_get_height_(io_surface); | |
| 134 } | |
| 135 | |
| 136 | |
| 137 CGLError IOSurfaceSupportImpl::CGLTexImageIOSurface2D(CGLContextObj ctx, | |
| 138 GLenum target, | |
| 139 GLenum internal_format, | |
| 140 GLsizei width, | |
| 141 GLsizei height, | |
| 142 GLenum format, | |
| 143 GLenum type, | |
| 144 CFTypeRef io_surface, | |
| 145 GLuint plane) { | |
| 146 return cgl_tex_image_io_surface_2d_(ctx, | |
| 147 target, | |
| 148 internal_format, | |
| 149 width, | |
| 150 height, | |
| 151 format, | |
| 152 type, | |
| 153 io_surface, | |
| 154 plane); | |
| 155 } | |
| 156 | |
| 157 IOSurfaceSupportImpl::IOSurfaceSupportImpl() | |
| 158 : iosurface_handle_(NULL), | |
| 159 opengl_handle_(NULL), | |
| 160 k_io_surface_width_(NULL), | |
| 161 k_io_surface_height_(NULL), | |
| 162 k_io_surface_bytes_per_element_(NULL), | |
| 163 k_io_surface_is_global_(NULL), | |
| 164 io_surface_create_(NULL), | |
| 165 io_surface_get_id_(NULL), | |
| 166 io_surface_lookup_(NULL), | |
| 167 io_surface_create_mach_port_(NULL), | |
| 168 io_surface_lookup_from_mach_port_(NULL), | |
| 169 io_surface_get_width_(NULL), | |
| 170 io_surface_get_height_(NULL), | |
| 171 cgl_tex_image_io_surface_2d_(NULL), | |
| 172 initialized_successfully_(false) { | |
| 173 iosurface_handle_ = dlopen( | |
| 174 "/System/Library/Frameworks/IOSurface.framework/IOSurface", | |
| 175 RTLD_LAZY | RTLD_LOCAL); | |
| 176 if (!iosurface_handle_) | |
| 177 return; | |
| 178 opengl_handle_ = dlopen( | |
| 179 "/System/Library/Frameworks/OpenGL.framework/OpenGL", | |
| 180 RTLD_LAZY | RTLD_LOCAL); | |
| 181 if (!opengl_handle_) { | |
| 182 dlclose(iosurface_handle_); | |
| 183 iosurface_handle_ = NULL; | |
| 184 return; | |
| 185 } | |
| 186 | |
| 187 void* surface_width_ptr = dlsym(iosurface_handle_, "kIOSurfaceWidth"); | |
| 188 void* surface_height_ptr = dlsym(iosurface_handle_, "kIOSurfaceHeight"); | |
| 189 void* surface_bytes_per_element_ptr = | |
| 190 dlsym(iosurface_handle_, "kIOSurfaceBytesPerElement"); | |
| 191 void* surface_is_global_ptr = | |
| 192 dlsym(iosurface_handle_, "kIOSurfaceIsGlobal"); | |
| 193 void* surface_create_ptr = dlsym(iosurface_handle_, "IOSurfaceCreate"); | |
| 194 void* surface_get_id_ptr = dlsym(iosurface_handle_, "IOSurfaceGetID"); | |
| 195 void* surface_lookup_ptr = dlsym(iosurface_handle_, "IOSurfaceLookup"); | |
| 196 void* surface_create_mach_port_ptr = | |
| 197 dlsym(iosurface_handle_, "IOSurfaceCreateMachPort"); | |
| 198 void* surface_lookup_from_mach_port_ptr = | |
| 199 dlsym(iosurface_handle_, "IOSurfaceLookupFromMachPort"); | |
| 200 void* io_surface_get_width_ptr = | |
| 201 dlsym(iosurface_handle_, "IOSurfaceGetWidth"); | |
| 202 void* io_surface_get_height_ptr = | |
| 203 dlsym(iosurface_handle_, "IOSurfaceGetHeight"); | |
| 204 void* tex_image_io_surface_2d_ptr = | |
| 205 dlsym(opengl_handle_, "CGLTexImageIOSurface2D"); | |
| 206 if (!surface_width_ptr || | |
| 207 !surface_height_ptr || | |
| 208 !surface_bytes_per_element_ptr || | |
| 209 !surface_is_global_ptr || | |
| 210 !surface_create_ptr || | |
| 211 !surface_get_id_ptr || | |
| 212 !surface_lookup_ptr || | |
| 213 !surface_create_mach_port_ptr || | |
| 214 !surface_lookup_from_mach_port_ptr || | |
| 215 !io_surface_get_width_ptr || | |
| 216 !io_surface_get_height_ptr || | |
| 217 !tex_image_io_surface_2d_ptr) { | |
| 218 dlclose(iosurface_handle_); | |
| 219 iosurface_handle_ = NULL; | |
| 220 dlclose(opengl_handle_); | |
| 221 opengl_handle_ = NULL; | |
| 222 return; | |
| 223 } | |
| 224 | |
| 225 k_io_surface_width_ = *static_cast<CFStringRef*>(surface_width_ptr); | |
| 226 k_io_surface_height_ = *static_cast<CFStringRef*>(surface_height_ptr); | |
| 227 k_io_surface_bytes_per_element_ = | |
| 228 *static_cast<CFStringRef*>(surface_bytes_per_element_ptr); | |
| 229 k_io_surface_is_global_ = *static_cast<CFStringRef*>(surface_is_global_ptr); | |
| 230 io_surface_create_ = reinterpret_cast<IOSurfaceCreateProcPtr>( | |
| 231 surface_create_ptr); | |
| 232 io_surface_get_id_ = | |
| 233 reinterpret_cast<IOSurfaceGetIDProcPtr>(surface_get_id_ptr); | |
| 234 io_surface_lookup_ = | |
| 235 reinterpret_cast<IOSurfaceLookupProcPtr>(surface_lookup_ptr); | |
| 236 io_surface_create_mach_port_ = | |
| 237 reinterpret_cast<IOSurfaceCreateMachPortProcPtr>( | |
| 238 surface_create_mach_port_ptr); | |
| 239 io_surface_lookup_from_mach_port_ = | |
| 240 reinterpret_cast<IOSurfaceLookupFromMachPortProcPtr>( | |
| 241 surface_lookup_from_mach_port_ptr); | |
| 242 io_surface_get_width_ = | |
| 243 reinterpret_cast<IOSurfaceGetWidthPtr>( | |
| 244 io_surface_get_width_ptr); | |
| 245 io_surface_get_height_ = | |
| 246 reinterpret_cast<IOSurfaceGetHeightPtr>( | |
| 247 io_surface_get_height_ptr); | |
| 248 cgl_tex_image_io_surface_2d_ = | |
| 249 reinterpret_cast<CGLTexImageIOSurface2DProcPtr>( | |
| 250 tex_image_io_surface_2d_ptr); | |
| 251 initialized_successfully_ = true; | |
| 252 } | |
| 253 | |
| 254 IOSurfaceSupportImpl::~IOSurfaceSupportImpl() { | |
| 255 if (iosurface_handle_) | |
| 256 dlclose(iosurface_handle_); | |
| 257 if (opengl_handle_) | |
| 258 dlclose(opengl_handle_); | |
| 259 } | |
| 260 | |
| 261 IOSurfaceSupport* IOSurfaceSupport::Initialize() { | |
| 262 return IOSurfaceSupportImpl::GetInstance(); | |
| 263 } | |
| 264 | |
| 265 IOSurfaceSupport::IOSurfaceSupport() { | |
| 266 } | |
| 267 | |
| 268 IOSurfaceSupport::~IOSurfaceSupport() { | |
| 269 } | |
| 270 | |
| OLD | NEW |