1 /* 2 * Copyright (c) 2015 Guillaume Piolat 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * * Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * * Neither the names 'Derelict', 'DerelictSDL', nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 /** 33 Dynamic bindings to the CoreGraphics framework. 34 */ 35 module derelict.carbon.coregraphics; 36 37 import dplug.core.sharedlib; 38 import derelict.carbon.corefoundation; 39 import dplug.core.nogc; 40 41 version(OSX) 42 // because CoreGraphics.framework did not exist in OSX 10.6 43 enum libNames = "/System/Library/Frameworks/ApplicationServices.framework/ApplicationServices"; 44 else 45 enum libNames = ""; 46 47 48 class DerelictCoreGraphicsLoader : SharedLibLoader 49 { 50 public 51 { 52 nothrow @nogc: 53 this() 54 { 55 super(libNames); 56 } 57 58 override void loadSymbols() 59 { 60 bindFunc(cast(void**)&CGColorSpaceCreateDeviceRGB, "CGColorSpaceCreateDeviceRGB"); 61 bindFunc(cast(void**)&CGColorSpaceRelease, "CGColorSpaceRelease"); 62 bindFunc(cast(void**)&CGContextDrawImage, "CGContextDrawImage"); 63 bindFunc(cast(void**)&CGContextScaleCTM, "CGContextScaleCTM"); 64 bindFunc(cast(void**)&CGContextTranslateCTM, "CGContextTranslateCTM"); 65 bindFunc(cast(void**)&CGDataProviderCreateWithData, "CGDataProviderCreateWithData"); 66 bindFunc(cast(void**)&CGDataProviderRelease, "CGDataProviderRelease"); 67 bindFunc(cast(void**)&CGImageRelease, "CGImageRelease"); 68 bindFunc(cast(void**)&CGImageCreate, "CGImageCreate"); 69 bindFunc(cast(void**)&CGColorSpaceCreateWithName, "CGColorSpaceCreateWithName"); 70 } 71 } 72 } 73 74 private __gshared DerelictCoreGraphicsLoader DerelictCoreGraphics; 75 76 private __gshared loaderCounterCG = 0; 77 78 // Call this each time a novel owner uses these functions 79 // TODO: hold a mutex, because this isn't thread-safe 80 void acquireCoreGraphicsFunctions() nothrow @nogc 81 { 82 if (DerelictCoreGraphics is null) // You only live once 83 { 84 DerelictCoreGraphics = mallocNew!DerelictCoreGraphicsLoader(); 85 DerelictCoreGraphics.load(); 86 } 87 } 88 89 // Call this each time a novel owner releases a Cocoa functions 90 // TODO: hold a mutex, because this isn't thread-safe 91 void releaseCoreGraphicsFunctions() nothrow @nogc 92 { 93 /*if (--loaderCounterCG == 0) 94 { 95 DerelictCoreGraphics.unload(); 96 DerelictCoreGraphics.destroyFree(); 97 }*/ 98 } 99 100 unittest 101 { 102 version(OSX) 103 { 104 acquireCoreGraphicsFunctions(); 105 releaseCoreGraphicsFunctions(); 106 } 107 } 108 109 110 // <CoreGraphics/CGBase.h> 111 112 static if ((void*).sizeof > int.sizeof) // 64bit 113 alias CGFloat = double; 114 else 115 alias CGFloat = float; 116 117 118 // <CoreGraphics/CGColorSpace.h> 119 120 alias CGColorSpaceRef = void*; 121 122 alias CGColorRenderingIntent = int; 123 enum : CGColorRenderingIntent 124 { 125 kCGRenderingIntentDefault, 126 kCGRenderingIntentAbsoluteColorimetric, 127 kCGRenderingIntentRelativeColorimetric, 128 kCGRenderingIntentPerceptual, 129 kCGRenderingIntentSaturation 130 } 131 132 extern (C) nothrow @nogc 133 { 134 alias da_CGColorSpaceCreateDeviceRGB = CGColorSpaceRef function(); 135 alias da_CGColorSpaceRelease = void function(CGColorSpaceRef); 136 alias da_CGColorSpaceCreateWithName = CGColorSpaceRef function(CFStringRef); 137 } 138 139 __gshared 140 { 141 da_CGColorSpaceCreateDeviceRGB CGColorSpaceCreateDeviceRGB; 142 da_CGColorSpaceRelease CGColorSpaceRelease; 143 da_CGColorSpaceCreateWithName CGColorSpaceCreateWithName; 144 } 145 146 147 // <CoreGraphics/CGContext.h> 148 149 alias CGContextRef = void*; 150 151 extern (C) nothrow @nogc 152 { 153 alias da_CGContextDrawImage = void function(CGContextRef, CGRect, CGImageRef); 154 alias da_CGContextScaleCTM = void function(CGContextRef, CGFloat, CGFloat); 155 alias da_CGContextTranslateCTM = void function(CGContextRef c, CGFloat sx, CGFloat sy); 156 } 157 158 __gshared 159 { 160 da_CGContextDrawImage CGContextDrawImage; 161 da_CGContextScaleCTM CGContextScaleCTM; 162 da_CGContextTranslateCTM CGContextTranslateCTM; 163 } 164 165 166 167 // <CoreGraphics/CGDataProvider.h> 168 169 alias CGDataProviderRef = void*; 170 171 extern(C) nothrow 172 { 173 alias CGDataProviderReleaseDataCallback = void function(void* info, const(void)* data, size_t size); 174 } 175 176 extern(C) nothrow @nogc 177 { 178 alias da_CGDataProviderCreateWithData = CGDataProviderRef function(void*, const(void)*, size_t, CGDataProviderReleaseDataCallback); 179 alias da_CGDataProviderRelease = void function(CGDataProviderRef); 180 } 181 182 __gshared 183 { 184 da_CGDataProviderCreateWithData CGDataProviderCreateWithData; 185 da_CGDataProviderRelease CGDataProviderRelease; 186 } 187 188 189 190 // <CoreGraphics/CGGeometry.h> 191 192 struct CGPoint 193 { 194 CGFloat x; 195 CGFloat y; 196 } 197 198 struct CGSize 199 { 200 CGFloat width; 201 CGFloat height; 202 } 203 204 struct CGVector 205 { 206 CGFloat dx; 207 CGFloat dy; 208 } 209 210 struct CGRect 211 { 212 CGPoint origin; 213 CGSize size; 214 } 215 216 static immutable CGPoint CGPointZero = CGPoint(0, 0); 217 static immutable CGSize CGSizeZero = CGSize(0, 0); 218 static immutable CGRect CGRectZero = CGRect(CGPoint(0, 0), CGSize(0, 0)); 219 220 CGRect CGRectMake(CGFloat x, CGFloat y, CGFloat w, CGFloat h) nothrow @nogc 221 { 222 return CGRect(CGPoint(x, y), CGSize(w, h)); 223 } 224 225 226 227 // <CoreGraphics/CGImage.h> 228 229 alias CGImageRef = void*; 230 231 alias CGBitmapInfo = uint; 232 enum : CGBitmapInfo 233 { 234 kCGBitmapAlphaInfoMask = 0x1F, 235 kCGBitmapFloatComponents = (1 << 8), 236 kCGBitmapByteOrderMask = 0x7000, 237 kCGBitmapByteOrderDefault = (0 << 12), 238 kCGBitmapByteOrder16Little = (1 << 12), 239 kCGBitmapByteOrder32Little = (2 << 12), 240 kCGBitmapByteOrder16Big = (3 << 12), 241 kCGBitmapByteOrder32Big = (4 << 12) 242 } 243 244 extern (C) nothrow @nogc 245 { 246 alias da_CGImageRelease = void function(CGImageRef); 247 248 alias da_CGImageCreate = CGImageRef function(size_t width, size_t height, size_t bitsPerComponent, 249 size_t bitsPerPixel, size_t bytesPerRow, 250 CGColorSpaceRef space, CGBitmapInfo bitmapInfo, 251 CGDataProviderRef provider, const CGFloat *decode, 252 bool shouldInterpolate, CGColorRenderingIntent intent); 253 } 254 255 __gshared 256 { 257 da_CGImageRelease CGImageRelease; 258 da_CGImageCreate CGImageCreate; 259 } 260 261 262