zorldo

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

devicescale.go (1649B)


      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 package devicescale
     16 
     17 import (
     18 	"sync"
     19 )
     20 
     21 type pos struct {
     22 	x, y int
     23 }
     24 
     25 var (
     26 	m     sync.Mutex
     27 	cache = map[pos]float64{}
     28 )
     29 
     30 // GetAt returns the device scale at (x, y), i.e. the number of device-dependent pixels per device-independent pixel.
     31 // x and y are in device-dependent pixels and must be the top-left coordinate of a monitor, or 0,0 to request a "global scale".
     32 func GetAt(x, y int) float64 {
     33 	m.Lock()
     34 	defer m.Unlock()
     35 	if s, ok := cache[pos{x, y}]; ok {
     36 		return s
     37 	}
     38 	s := impl(x, y)
     39 	cache[pos{x, y}] = s
     40 	return s
     41 }
     42 
     43 // CelarCache clears the cache.
     44 // This should be called when monitors are changed by connecting or disconnecting.
     45 func ClearCache() {
     46 	// TODO: This should be called not only when monitors are changed but also a monitor's scales are changed.
     47 	// The device scale can vary even for the same monitor.
     48 	// The only known case is when the application works on macOS, with OpenGL, with a wider screen mode,
     49 	// and in the fullscreen mode (#1573).
     50 
     51 	m.Lock()
     52 	defer m.Unlock()
     53 	for k := range cache {
     54 		delete(cache, k)
     55 	}
     56 }