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 module derelict.carbon.coregraphics; 33 34 import dplug.core.sharedlib; 35 import derelict.carbon.corefoundation; 36 import dplug.core.nogc; 37 38 version(OSX) 39 // because CoreGraphics.framework did not exist in OSX 10.6 40 enum libNames = "/System/Library/Frameworks/ApplicationServices.framework/ApplicationServices"; 41 else 42 enum libNames = ""; 43 44 45 class DerelictCoreGraphicsLoader : SharedLibLoader 46 { 47 public 48 { 49 nothrow @nogc: 50 this() 51 { 52 super(libNames); 53 } 54 55 override void loadSymbols() 56 { 57 bindFunc(cast(void**)&CGColorSpaceCreateDeviceRGB, "CGColorSpaceCreateDeviceRGB"); 58 bindFunc(cast(void**)&CGColorSpaceRelease, "CGColorSpaceRelease"); 59 bindFunc(cast(void**)&CGContextDrawImage, "CGContextDrawImage"); 60 bindFunc(cast(void**)&CGContextScaleCTM, "CGContextScaleCTM"); 61 bindFunc(cast(void**)&CGContextTranslateCTM, "CGContextTranslateCTM"); 62 bindFunc(cast(void**)&CGDataProviderCreateWithData, "CGDataProviderCreateWithData"); 63 bindFunc(cast(void**)&CGDataProviderRelease, "CGDataProviderRelease"); 64 bindFunc(cast(void**)&CGImageRelease, "CGImageRelease"); 65 bindFunc(cast(void**)&CGImageCreate, "CGImageCreate"); 66 bindFunc(cast(void**)&CGColorSpaceCreateWithName, "CGColorSpaceCreateWithName"); 67 } 68 } 69 } 70 71 private __gshared DerelictCoreGraphicsLoader DerelictCoreGraphics; 72 73 private __gshared loaderCounterCG = 0; 74 75 // Call this each time a novel owner uses these functions 76 // TODO: hold a mutex, because this isn't thread-safe 77 void acquireCoreGraphicsFunctions() nothrow @nogc 78 { 79 if (DerelictCoreGraphics is null) // You only live once 80 { 81 DerelictCoreGraphics = mallocEmplace!DerelictCoreGraphicsLoader(); 82 DerelictCoreGraphics.load(); 83 } 84 } 85 86 // Call this each time a novel owner releases a Cocoa functions 87 // TODO: hold a mutex, because this isn't thread-safe 88 void releaseCoreGraphicsFunctions() nothrow @nogc 89 { 90 /*if (--loaderCounterCG == 0) 91 { 92 DerelictCoreGraphics.unload(); 93 DerelictCoreGraphics.destroyFree(); 94 }*/ 95 } 96 97 unittest 98 { 99 version(OSX) 100 { 101 acquireCoreGraphicsFunctions(); 102 releaseCoreGraphicsFunctions(); 103 } 104 } 105 106 107 // <CoreGraphics/CGBase.h> 108 109 static if ((void*).sizeof > int.sizeof) // 64bit 110 alias CGFloat = double; 111 else 112 alias CGFloat = float; 113 114 115 // <CoreGraphics/CGColorSpace.h> 116 117 alias CGColorSpaceRef = void*; 118 119 alias CGColorRenderingIntent = int; 120 enum : CGColorRenderingIntent 121 { 122 kCGRenderingIntentDefault, 123 kCGRenderingIntentAbsoluteColorimetric, 124 kCGRenderingIntentRelativeColorimetric, 125 kCGRenderingIntentPerceptual, 126 kCGRenderingIntentSaturation 127 } 128 129 extern (C) nothrow @nogc 130 { 131 alias da_CGColorSpaceCreateDeviceRGB = CGColorSpaceRef function(); 132 alias da_CGColorSpaceRelease = void function(CGColorSpaceRef); 133 alias da_CGColorSpaceCreateWithName = CGColorSpaceRef function(CFStringRef); 134 } 135 136 __gshared 137 { 138 da_CGColorSpaceCreateDeviceRGB CGColorSpaceCreateDeviceRGB; 139 da_CGColorSpaceRelease CGColorSpaceRelease; 140 da_CGColorSpaceCreateWithName CGColorSpaceCreateWithName; 141 } 142 143 144 // <CoreGraphics/CGContext.h> 145 146 alias CGContextRef = void*; 147 148 extern (C) nothrow @nogc 149 { 150 alias da_CGContextDrawImage = void function(CGContextRef, CGRect, CGImageRef); 151 alias da_CGContextScaleCTM = void function(CGContextRef, CGFloat, CGFloat); 152 alias da_CGContextTranslateCTM = void function(CGContextRef c, CGFloat sx, CGFloat sy); 153 } 154 155 __gshared 156 { 157 da_CGContextDrawImage CGContextDrawImage; 158 da_CGContextScaleCTM CGContextScaleCTM; 159 da_CGContextTranslateCTM CGContextTranslateCTM; 160 } 161 162 163 164 // <CoreGraphics/CGDataProvider.h> 165 166 alias CGDataProviderRef = void*; 167 168 extern(C) nothrow 169 { 170 alias CGDataProviderReleaseDataCallback = void function(void* info, const(void)* data, size_t size); 171 } 172 173 extern(C) nothrow @nogc 174 { 175 alias da_CGDataProviderCreateWithData = CGDataProviderRef function(void*, const(void)*, size_t, CGDataProviderReleaseDataCallback); 176 alias da_CGDataProviderRelease = void function(CGDataProviderRef); 177 } 178 179 __gshared 180 { 181 da_CGDataProviderCreateWithData CGDataProviderCreateWithData; 182 da_CGDataProviderRelease CGDataProviderRelease; 183 } 184 185 186 187 // <CoreGraphics/CGGeometry.h> 188 189 struct CGPoint 190 { 191 CGFloat x; 192 CGFloat y; 193 } 194 195 struct CGSize 196 { 197 CGFloat width; 198 CGFloat height; 199 } 200 201 struct CGVector 202 { 203 CGFloat dx; 204 CGFloat dy; 205 } 206 207 struct CGRect 208 { 209 CGPoint origin; 210 CGSize size; 211 } 212 213 static immutable CGPoint CGPointZero = CGPoint(0, 0); 214 static immutable CGSize CGSizeZero = CGSize(0, 0); 215 static immutable CGRect CGRectZero = CGRect(CGPoint(0, 0), CGSize(0, 0)); 216 217 CGRect CGRectMake(CGFloat x, CGFloat y, CGFloat w, CGFloat h) nothrow @nogc 218 { 219 return CGRect(CGPoint(x, y), CGSize(w, h)); 220 } 221 222 223 224 // <CoreGraphics/CGImage.h> 225 226 alias CGImageRef = void*; 227 228 alias CGBitmapInfo = uint; 229 enum : CGBitmapInfo 230 { 231 kCGBitmapAlphaInfoMask = 0x1F, 232 kCGBitmapFloatComponents = (1 << 8), 233 kCGBitmapByteOrderMask = 0x7000, 234 kCGBitmapByteOrderDefault = (0 << 12), 235 kCGBitmapByteOrder16Little = (1 << 12), 236 kCGBitmapByteOrder32Little = (2 << 12), 237 kCGBitmapByteOrder16Big = (3 << 12), 238 kCGBitmapByteOrder32Big = (4 << 12) 239 } 240 241 extern (C) nothrow @nogc 242 { 243 alias da_CGImageRelease = void function(CGImageRef); 244 245 alias da_CGImageCreate = CGImageRef function(size_t width, size_t height, size_t bitsPerComponent, 246 size_t bitsPerPixel, size_t bytesPerRow, 247 CGColorSpaceRef space, CGBitmapInfo bitmapInfo, 248 CGDataProviderRef provider, const CGFloat *decode, 249 bool shouldInterpolate, CGColorRenderingIntent intent); 250 } 251 252 __gshared 253 { 254 da_CGImageRelease CGImageRelease; 255 da_CGImageCreate CGImageCreate; 256 } 257 258 259