<commit_before>/* This file is part of magic-sdk, an sdk for the open source programming language magic.
 *
 * Copyright (C) 2016 magic-lang
 *
 * This software may be modified and distributed under the terms
 * of the MIT license.  See the LICENSE file for details.
 */

use draw-gpu
use collections
use draw
use geometry
use base
use concurrent
import OpenGLContext, GraphicBuffer, GraphicBufferYuv420Semiplanar, EGLYuv, OpenGLPacked, OpenGLMap, OpenGLPromise, OpenGLMonochrome

version(!gpuOff) {
NativeYuvContext: class extends OpenGLContext {
	defaultMap ::= this _yuvShader as Map
	_yuvShader: OpenGLMapTransform
	_monochromeToYuv: OpenGLMapTransform
	_eglBin := RecycleBin<EGLYuv> new(100, |image| image _recyclable = false; image free())
	init: func (other: This = null) {
		super(other)
		this _yuvShader = OpenGLMapTransform new(slurp("shaders/yuv.frag"), this)
		this _monochromeToYuv = OpenGLMapTransform new(slurp("shaders/monochromeToNativeYuv.frag"), this)
	}
	free: override func {
		this _backend makeCurrent()
		(this _eglBin, this _yuvShader) free()
		super()
	}
	createImage: override func (rasterImage: RasterImage) -> GpuImage {
		match (rasterImage) {
			case (graphicBufferImage: GraphicBufferYuv420Semiplanar) => this createEGLYuv(graphicBufferImage)
			case => super(rasterImage)
		}
	}
	preallocate: override func (resolution: IntVector2D) { this _eglBin clear() }
	preregister: override func (image: Image) {
		if (image instanceOf(GraphicBufferYuv420Semiplanar))
			this createEGLYuv(image as GraphicBufferYuv420Semiplanar) free()
	}
	createEGLYuv: func (source: GraphicBufferYuv420Semiplanar) -> EGLYuv {
		this _eglBin search(|eglYuv| source buffer handle == eglYuv handle) ?? EGLYuv new(source buffer, this)
	}
	recycle: override func (image: OpenGLPacked) {
		match (image) {
			case eglYuv: EGLYuv => this _eglBin add(eglYuv)
			case => super(image)
		}
	}
	getDefaultShader: override func (input, output: Image) -> OpenGLMap {
		result: OpenGLMap
		if (input instanceOf(EGLYuv) && output instanceOf(EGLYuv))
			result = this _yuvShader
		else if (output instanceOf(EGLYuv) && (input instanceOf(OpenGLMonochrome) || input instanceOf(RasterMonochrome)))
			result = this _monochromeToYuv
		else
			result = super(input, output)
		result
	}
}
}<commit_msg>Now supports drawing white lines to native YUV.<commit_after>/* This file is part of magic-sdk, an sdk for the open source programming language magic.
 *
 * Copyright (C) 2016 magic-lang
 *
 * This software may be modified and distributed under the terms
 * of the MIT license.  See the LICENSE file for details.
 */

use draw-gpu
use collections
use draw
use geometry
use base
use concurrent
import OpenGLContext, GraphicBuffer, GraphicBufferYuv420Semiplanar, EGLYuv, OpenGLPacked, OpenGLMap, OpenGLPromise, OpenGLMonochrome

version(!gpuOff) {
NativeYuvContext: class extends OpenGLContext {
	defaultMap ::= this _yuvShader as Map
	_yuvShader: OpenGLMapTransform
	_monochromeToYuv: OpenGLMapTransform
	_yuvLineShader: OpenGLMap
	_eglBin := RecycleBin<EGLYuv> new(100, |image| image _recyclable = false; image free())
	init: func (other: This = null) {
		super(other)
		this _yuvShader = OpenGLMapTransform new(slurp("shaders/yuv.frag"), this)
		this _monochromeToYuv = OpenGLMapTransform new(slurp("shaders/monochromeToNativeYuv.frag"), this)
		this _yuvLineShader = OpenGLMapTransform new(slurp("shaders/whiteToNativeYUV.frag"), this)
	}
	free: override func {
		this _backend makeCurrent()
		(this _eglBin, this _yuvShader, this _yuvLineShader) free()
		super()
	}
	createImage: override func (rasterImage: RasterImage) -> GpuImage {
		match (rasterImage) {
			case (graphicBufferImage: GraphicBufferYuv420Semiplanar) => this createEGLYuv(graphicBufferImage)
			case => super(rasterImage)
		}
	}
	preallocate: override func (resolution: IntVector2D) { this _eglBin clear() }
	preregister: override func (image: Image) {
		if (image instanceOf(GraphicBufferYuv420Semiplanar))
			this createEGLYuv(image as GraphicBufferYuv420Semiplanar) free()
	}
	createEGLYuv: func (source: GraphicBufferYuv420Semiplanar) -> EGLYuv {
		this _eglBin search(|eglYuv| source buffer handle == eglYuv handle) ?? EGLYuv new(source buffer, this)
	}
	recycle: override func (image: OpenGLPacked) {
		match (image) {
			case eglYuv: EGLYuv => this _eglBin add(eglYuv)
			case => super(image)
		}
	}
	getDefaultShader: override func (input, output: Image) -> OpenGLMap {
		result: OpenGLMap
		if (input instanceOf(EGLYuv) && output instanceOf(EGLYuv))
			result = this _yuvShader
		else if (output instanceOf(EGLYuv) && (input instanceOf(OpenGLMonochrome) || input instanceOf(RasterMonochrome)))
			result = this _monochromeToYuv
		else
			result = super(input, output)
		result
	}
	getLineShader: override func -> OpenGLMap { this _yuvLineShader }
}
}
{'fraction_non_alphanumeric': 0.09339975093399751, 'fraction_numerical': 0.009547530095475302, 'mean_word_length': 5.315858453473132, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4031956', 'n_tokens_mistral': 1738, 'n_tokens_neox': 1631, 'n_words': 448}