dmitri.shuralyov.com/gpu/mtl/...

example/movingtriangle/internal/{ns,ca}: rename to appkit, coreanim

This came up during code review in https://golang.org/cl/171025.
Backport the improvements to the upstream copies of those packages.

These are better names for these Go packages. The previous names were
an attempt at leveraging the familiarity with Apple's API prefixes
(i.e., AppKit identifiers tend to have "NS" prefix, Core Animation API
identifiers tend to have "CA" prefixes). However, that isn't as useful
here because those prefixes are not very consistent or recognizable
when turned into Go package names.

Create a helper to convert from bool to C.BOOL and use it instead of
more verbose switches. Other people seem to like this style better.
dmitshur committed 5 years ago commit 11308bd612348edeca0d8fddca07bc5a4dfbb35c
Collapse all
example/movingtriangle/internal/ns/ns.go → example/movingtriangle/internal/appkit/appkit.go
@@ -1,22 +1,23 @@
// +build darwin

// Package ns provides access to Apple's AppKit API (https://developer.apple.com/documentation/appkit).
// Package appkit provides access to Apple's AppKit API
// (https://developer.apple.com/documentation/appkit).
//
// This package is in very early stages of development.
// It's a minimal implementation with scope limited to
// supporting the movingtriangle example.
package ns
package appkit

import (
	"unsafe"

	"dmitri.shuralyov.com/gpu/mtl/example/movingtriangle/internal/ca"
	"dmitri.shuralyov.com/gpu/mtl/example/movingtriangle/internal/coreanim"
)

/*
#include "ns.h"
#include "appkit.h"
*/
import "C"

// Window is a window that an app displays on the screen.
//
@@ -46,20 +47,22 @@ type View struct {
}

// SetLayer sets v.layer to l.
//
// Reference: https://developer.apple.com/documentation/appkit/nsview/1483298-layer.
func (v View) SetLayer(l ca.Layer) {
func (v View) SetLayer(l coreanim.Layer) {
	C.View_SetLayer(v.view, l.Layer())
}

// SetWantsLayer sets v.wantsLayer to wantsLayer.
//
// Reference: https://developer.apple.com/documentation/appkit/nsview/1483695-wantslayer.
func (v View) SetWantsLayer(wantsLayer bool) {
	switch wantsLayer {
	case true:
		C.View_SetWantsLayer(v.view, 1)
	case false:
		C.View_SetWantsLayer(v.view, 0)
	C.View_SetWantsLayer(v.view, toCBool(wantsLayer))
}

func toCBool(b bool) C.BOOL {
	if b {
		return 1
	}
	return 0
}
example/movingtriangle/internal/ns/ns.h → example/movingtriangle/internal/appkit/appkit.h
No modification.
example/movingtriangle/internal/ns/ns.m → example/movingtriangle/internal/appkit/appkit.m
@@ -1,9 +1,9 @@
// +build darwin

#import <Cocoa/Cocoa.h>
#include "ns.h"
#include "appkit.h"

void * Window_ContentView(void * window) {
	return ((NSWindow *)window).contentView;
}

example/movingtriangle/internal/ca/ca.go → example/movingtriangle/internal/coreanim/coreanim.go
@@ -1,24 +1,25 @@
// +build darwin

// Package ca provides access to Apple's Core Animation API (https://developer.apple.com/documentation/quartzcore).
// Package coreanim provides access to Apple's Core Animation API
// (https://developer.apple.com/documentation/quartzcore).
//
// This package is in very early stages of development.
// It's a minimal implementation with scope limited to
// supporting the movingtriangle example.
package ca
package coreanim

import (
	"errors"
	"unsafe"

	"dmitri.shuralyov.com/gpu/mtl"
)

/*
#cgo LDFLAGS: -framework QuartzCore -framework Foundation
#include "ca.h"
#include "coreanim.h"
*/
import "C"

// Layer is an object that manages image-based content and
// allows you to perform animations on that content.
@@ -90,16 +91,11 @@ func (ml MetalLayer) SetMaximumDrawableCount(count int) {
// SetDisplaySyncEnabled controls whether the Metal layer and its drawables
// are synchronized with the display's refresh rate.
//
// Reference: https://developer.apple.com/documentation/quartzcore/cametallayer/2887087-displaysyncenabled.
func (ml MetalLayer) SetDisplaySyncEnabled(enabled bool) {
	switch enabled {
	case true:
		C.MetalLayer_SetDisplaySyncEnabled(ml.metalLayer, 1)
	case false:
		C.MetalLayer_SetDisplaySyncEnabled(ml.metalLayer, 0)
	}
	C.MetalLayer_SetDisplaySyncEnabled(ml.metalLayer, toCBool(enabled))
}

// SetDrawableSize sets the size, in pixels, of textures for rendering layer content.
//
// Reference: https://developer.apple.com/documentation/quartzcore/cametallayer/1478174-drawablesize.
@@ -133,5 +129,12 @@ func (md MetalDrawable) Drawable() unsafe.Pointer { return md.metalDrawable }
//
// Reference: https://developer.apple.com/documentation/quartzcore/cametaldrawable/1478159-texture.
func (md MetalDrawable) Texture() mtl.Texture {
	return mtl.NewTexture(C.MetalDrawable_Texture(md.metalDrawable))
}

func toCBool(b bool) C.BOOL {
	if b {
		return 1
	}
	return 0
}
example/movingtriangle/internal/ca/ca.h → example/movingtriangle/internal/coreanim/coreanim.h
No modification.
example/movingtriangle/internal/ca/ca.m → example/movingtriangle/internal/coreanim/coreanim.m
@@ -1,9 +1,9 @@
// +build darwin

#import <QuartzCore/QuartzCore.h>
#include "ca.h"
#include "coreanim.h"

void * MakeMetalLayer() {
	return [[CAMetalLayer alloc] init];
}

example/movingtriangle/main.go
@@ -12,12 +12,12 @@ import (
	"runtime"
	"time"
	"unsafe"

	"dmitri.shuralyov.com/gpu/mtl"
	"dmitri.shuralyov.com/gpu/mtl/example/movingtriangle/internal/ca"
	"dmitri.shuralyov.com/gpu/mtl/example/movingtriangle/internal/ns"
	"dmitri.shuralyov.com/gpu/mtl/example/movingtriangle/internal/appkit"
	"dmitri.shuralyov.com/gpu/mtl/example/movingtriangle/internal/coreanim"
	"github.com/go-gl/glfw/v3.3/glfw"
	"golang.org/x/image/math/f32"
)

func init() {
@@ -55,17 +55,17 @@ func run() error {
	if err != nil {
		return err
	}
	defer window.Destroy()

	ml := ca.MakeMetalLayer()
	ml := coreanim.MakeMetalLayer()
	ml.SetDevice(device)
	ml.SetPixelFormat(mtl.PixelFormatBGRA8UNorm)
	ml.SetDrawableSize(window.GetFramebufferSize())
	ml.SetMaximumDrawableCount(3)
	ml.SetDisplaySyncEnabled(true)
	cv := ns.NewWindow(window.GetCocoaWindow()).ContentView()
	cv := appkit.NewWindow(window.GetCocoaWindow()).ContentView()
	cv.SetLayer(ml)
	cv.SetWantsLayer(true)

	// Set callbacks.
	window.SetFramebufferSizeCallback(func(_ *glfw.Window, width, height int) {