zorldo

Goofing around with Ebiten
git clone git://bsandro.tech/zorldo
Log | Files | Refs | README

ca_darwin.m (3825B)


      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 #include "ca_darwin.h"
     16 #import <QuartzCore/QuartzCore.h>
     17 
     18 void *MakeMetalLayer() {
     19   CAMetalLayer *layer = [[CAMetalLayer alloc] init];
     20   // TODO: Expose a function to set color space.
     21   // TODO: Enable colorspace on iOS: this will be available as of iOS 13.0.
     22 #if !TARGET_OS_IPHONE
     23   CGColorSpaceRef colorspace =
     24       CGColorSpaceCreateWithName(kCGColorSpaceDisplayP3);
     25   layer.colorspace = colorspace;
     26   CGColorSpaceRelease(colorspace);
     27 #endif
     28   return layer;
     29 }
     30 
     31 uint16_t MetalLayer_PixelFormat(void *metalLayer) {
     32   return ((CAMetalLayer *)metalLayer).pixelFormat;
     33 }
     34 
     35 void MetalLayer_SetDevice(void *metalLayer, void *device) {
     36   ((CAMetalLayer *)metalLayer).device = (id<MTLDevice>)device;
     37 }
     38 
     39 void MetalLayer_SetOpaque(void *metalLayer, unsigned char opaque) {
     40   ((CAMetalLayer *)metalLayer).opaque = (BOOL)opaque;
     41 }
     42 
     43 const char *MetalLayer_SetPixelFormat(void *metalLayer, uint16_t pixelFormat) {
     44   @try {
     45     ((CAMetalLayer *)metalLayer).pixelFormat = (MTLPixelFormat)pixelFormat;
     46   } @catch (NSException *exception) {
     47     return exception.reason.UTF8String;
     48   }
     49   return NULL;
     50 }
     51 
     52 const char *MetalLayer_SetMaximumDrawableCount(void *metalLayer,
     53                                                uint_t maximumDrawableCount) {
     54   // @available syntax is not available for old Xcode (#781)
     55   //
     56   // If possible, we'd want to write the guard like:
     57   //
     58   //     if (@available(macOS 10.13.2, *)) { ...
     59 
     60   @try {
     61     if ([(CAMetalLayer *)metalLayer
     62             respondsToSelector:@selector(setMaximumDrawableCount:)]) {
     63       [((CAMetalLayer *)metalLayer)
     64           setMaximumDrawableCount:(NSUInteger)maximumDrawableCount];
     65     }
     66   } @catch (NSException *exception) {
     67     return exception.reason.UTF8String;
     68   }
     69   return NULL;
     70 }
     71 
     72 void MetalLayer_SetDisplaySyncEnabled(void *metalLayer,
     73                                       uint8_t displaySyncEnabled) {
     74   // @available syntax is not available for old Xcode (#781)
     75   //
     76   // If possible, we'd want to write the guard like:
     77   //
     78   //     if (@available(macOS 10.13, *)) { ...
     79 
     80 #if !TARGET_OS_IPHONE
     81   if ([(CAMetalLayer *)metalLayer
     82           respondsToSelector:@selector(setDisplaySyncEnabled:)]) {
     83     [((CAMetalLayer *)metalLayer) setDisplaySyncEnabled:displaySyncEnabled];
     84   }
     85 #endif
     86 }
     87 
     88 void MetalLayer_SetDrawableSize(void *metalLayer, double width, double height) {
     89   ((CAMetalLayer *)metalLayer).drawableSize = (CGSize){width, height};
     90 }
     91 
     92 void MetalLayer_SetPresentsWithTransaction(void *metalLayer,
     93                                            uint8_t presentsWithTransaction) {
     94   [((CAMetalLayer *)metalLayer)
     95       setPresentsWithTransaction:presentsWithTransaction];
     96 }
     97 
     98 void *MetalLayer_NextDrawable(void *metalLayer) {
     99   return [(CAMetalLayer *)metalLayer nextDrawable];
    100 }
    101 
    102 void *MetalDrawable_Texture(void *metalDrawable) {
    103   return ((id<CAMetalDrawable>)metalDrawable).texture;
    104 }
    105 
    106 void MetalDrawable_Present(void *metalDrawable) {
    107   [((id<CAMetalDrawable>)metalDrawable) present];
    108 }
    109 
    110 void MetalLayer_SetFramebufferOnly(void *metalLayer, uint8_t framebufferOnly) {
    111   [((CAMetalLayer *)metalLayer) setFramebufferOnly:framebufferOnly];
    112 }
    113 
    114 uint8_t MetalLayer_PresentsWithTransaction(void *metalLayer) {
    115   return [((CAMetalLayer *)metalLayer) presentsWithTransaction];
    116 }