ca.m (3217B)
1 // Copyright 2018 The Ebiten Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // +build darwin 16 17 #include "ca.h" 18 #import <QuartzCore/QuartzCore.h> 19 20 void *MakeMetalLayer() { 21 CAMetalLayer *layer = [[CAMetalLayer alloc] init]; 22 // TODO: Expose a function to set color space. 23 // TODO: Enable colorspace on iOS: this will be available as of iOS 13.0. 24 #if !TARGET_OS_IPHONE 25 CGColorSpaceRef colorspace = 26 CGColorSpaceCreateWithName(kCGColorSpaceDisplayP3); 27 layer.colorspace = colorspace; 28 CGColorSpaceRelease(colorspace); 29 #endif 30 return layer; 31 } 32 33 uint16_t MetalLayer_PixelFormat(void *metalLayer) { 34 return ((CAMetalLayer *)metalLayer).pixelFormat; 35 } 36 37 void MetalLayer_SetDevice(void *metalLayer, void *device) { 38 ((CAMetalLayer *)metalLayer).device = (id<MTLDevice>)device; 39 } 40 41 void MetalLayer_SetOpaque(void *metalLayer, unsigned char opaque) { 42 ((CAMetalLayer *)metalLayer).opaque = (BOOL)opaque; 43 } 44 45 const char *MetalLayer_SetPixelFormat(void *metalLayer, uint16_t pixelFormat) { 46 @try { 47 ((CAMetalLayer *)metalLayer).pixelFormat = (MTLPixelFormat)pixelFormat; 48 } @catch (NSException *exception) { 49 return exception.reason.UTF8String; 50 } 51 return NULL; 52 } 53 54 const char *MetalLayer_SetMaximumDrawableCount(void *metalLayer, 55 uint_t maximumDrawableCount) { 56 // @available syntax is not available for old Xcode (#781) 57 // 58 // If possible, we'd want to write the guard like: 59 // 60 // if (@available(macOS 10.13.2, *)) { ... 61 62 @try { 63 if ([(CAMetalLayer *)metalLayer 64 respondsToSelector:@selector(setMaximumDrawableCount:)]) { 65 [((CAMetalLayer *)metalLayer) 66 setMaximumDrawableCount:(NSUInteger)maximumDrawableCount]; 67 } 68 } @catch (NSException *exception) { 69 return exception.reason.UTF8String; 70 } 71 return NULL; 72 } 73 74 void MetalLayer_SetDisplaySyncEnabled(void *metalLayer, 75 uint8_t displaySyncEnabled) { 76 // @available syntax is not available for old Xcode (#781) 77 // 78 // If possible, we'd want to write the guard like: 79 // 80 // if (@available(macOS 10.13, *)) { ... 81 82 #if !TARGET_OS_IPHONE 83 if ([(CAMetalLayer *)metalLayer 84 respondsToSelector:@selector(setDisplaySyncEnabled:)]) { 85 [((CAMetalLayer *)metalLayer) setDisplaySyncEnabled:displaySyncEnabled]; 86 } 87 #endif 88 } 89 90 void MetalLayer_SetDrawableSize(void *metalLayer, double width, double height) { 91 ((CAMetalLayer *)metalLayer).drawableSize = (CGSize){width, height}; 92 } 93 94 void *MetalLayer_NextDrawable(void *metalLayer) { 95 return [(CAMetalLayer *)metalLayer nextDrawable]; 96 } 97 98 void *MetalDrawable_Texture(void *metalDrawable) { 99 return ((id<CAMetalDrawable>)metalDrawable).texture; 100 }