Class: Skia::RuntimeEffect

Inherits:
Base
  • Object
show all
Defined in:
lib/skia/runtime_effect.rb

Instance Attribute Summary

Attributes inherited from Base

#ptr

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#null?, release_callback, #to_ptr

Constructor Details

#initialize(ptr) ⇒ RuntimeEffect

Returns a new instance of RuntimeEffect.



5
6
7
# File 'lib/skia/runtime_effect.rb', line 5

def initialize(ptr)
  super(ptr, :sk_runtimeeffect_unref)
end

Class Method Details

.ensure_native_function!(function_name, api_name) ⇒ Object



81
82
83
84
85
86
# File 'lib/skia/runtime_effect.rb', line 81

def self.ensure_native_function!(function_name, api_name)
  return if Native.function_available?(function_name)

  raise UnsupportedOperationError,
        "RuntimeEffect#{api_name} is not supported by the current libSkiaSharp (missing #{function_name})"
end

.ensure_runtimeeffect_available!Object



72
73
74
75
# File 'lib/skia/runtime_effect.rb', line 72

def self.ensure_runtimeeffect_available!
  ensure_native_function!(:sk_runtimeeffect_make_for_shader, '.make_for_shader')
  ensure_native_function!(:sk_runtimeeffect_unref, '.make_for_shader')
end

.make_for_shader(sksl) ⇒ Object

Raises:



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/skia/runtime_effect.rb', line 9

def self.make_for_shader(sksl)
  ensure_runtimeeffect_available!
  source = sk_string_from(sksl)
  error = Native.sk_string_new_empty
  raise Error, 'Failed to allocate runtime effect error string' if error.nil? || error.null?

  begin
    ptr = Native.sk_runtimeeffect_make_for_shader(source, error)
    error_text = read_sk_string(error)
    raise Error, "Failed to compile runtime shader: #{error_text}" if ptr.nil? || ptr.null?

    new(ptr)
  ensure
    Native.sk_string_destructor(source) if source && !source.null?
    Native.sk_string_destructor(error) if error && !error.null?
  end
end

.read_sk_string(ptr) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/skia/runtime_effect.rb', line 49

def self.read_sk_string(ptr)
  return '' if ptr.nil? || ptr.null?

  cstr = Native.sk_string_get_c_str(ptr)
  size = Native.sk_string_get_size(ptr)
  return '' if cstr.nil? || cstr.null? || size.to_i.zero?

  cstr.read_string(size)
end

.sk_string_from(value) ⇒ Object

Raises:



39
40
41
42
43
44
45
46
47
# File 'lib/skia/runtime_effect.rb', line 39

def self.sk_string_from(value)
  bytes = value.to_s.b
  buffer = FFI::MemoryPointer.new(:char, bytes.bytesize)
  buffer.put_bytes(0, bytes)
  ptr = Native.sk_string_new_with_copy(buffer, bytes.bytesize)
  raise Error, 'Failed to allocate native string' if ptr.nil? || ptr.null?

  ptr
end

Instance Method Details

#make_shader(uniforms: nil, matrix: nil) ⇒ Object

Raises:



27
28
29
30
31
32
33
34
35
# File 'lib/skia/runtime_effect.rb', line 27

def make_shader(uniforms: nil, matrix: nil)
  ensure_native_function!(:sk_runtimeeffect_make_shader, '#make_shader')
  uniforms_data = coerce_uniform_data(uniforms)
  matrix_struct = matrix&.to_struct
  ptr = Native.sk_runtimeeffect_make_shader(@ptr, uniforms_data&.ptr, nil, 0, matrix_struct)
  raise Error, 'Failed to create shader from runtime effect' if ptr.nil? || ptr.null?

  Shader.new(ptr)
end