<commit_before>//
// Copyright (c) 2011-2014 <NAME> <<EMAIL>>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use ooc-math
use ooc-draw
use ooc-draw-gpu
use ooc-base
use ooc-opengl
use ooc-android-debug
import math, EglRgba, AndroidContext

GpuPacker: class {
	_renderTarget: Fbo
	_targetTexture: EglRgba
	_context: AndroidContext
	_size: IntSize2D
	size: IntSize2D { get { this _size } }
	_internalSize: IntSize2D
	_bytesPerPixel: UInt
	bytesPerPixel: UInt { get { this _bytesPerPixel } }
	init: func (size: IntSize2D, bytesPerPixel: UInt, context: AndroidContext) {
		this _bytesPerPixel = bytesPerPixel
		this _context = context
		this _size = size
		this _internalSize = IntSize2D new(size width * bytesPerPixel / 4, size height)
		this _bytesPerPixel = bytesPerPixel
		this _targetTexture = context createEglRgba(this _internalSize)
		this _renderTarget = Fbo create(this _targetTexture texture, this _internalSize width, this _internalSize height)
	}
	recycle: func {
		this _context recycle(this)
	}
	dispose: func {
		this _targetTexture dispose()
		this _renderTarget dispose()
	}
	pack: func ~ByteBuffer (image: GpuImage, map: OpenGLES3MapDefault) -> ByteBuffer {
		map transform = FloatTransform2D identity
		map imageSize = image size
		map screenSize = image size
		this _renderTarget bind()
		this _renderTarget clear()
		surface := this _context createSurface()
		surface draw(image, map, Viewport new(this _internalSize))
		surface recycle()
		Fbo finish()
		this _renderTarget unbind()
		sourcePointer := this _targetTexture lock()
		buffer := ByteBuffer new(this _targetTexture stride * image size height, sourcePointer,
			func (buffer: ByteBuffer){ this _targetTexture unlock()
						 this recycle()
						})
		buffer
	}
	pack: func (image: GpuImage, map: OpenGLES3MapDefault, destination: RasterImage) {
		map transform = FloatTransform2D identity
		map imageSize = image size
		map screenSize = image size
		this _renderTarget bind()
		this _renderTarget clear()
		surface := this _context createSurface()
		surface draw(image, map, Viewport new(this _internalSize))
		surface recycle()
		Fbo finish()
		this _renderTarget unbind()
		sourcePointer := this _targetTexture lock()
		destinationPointer := destination pointer
		destinationStride := destination stride
		sourceStride := this _targetTexture stride
		/*
		if (this _targetTexture isPadded()) {
			printAndroid("Warning: Copying row by row")
			printAndroid("Stride: " + this _targetTexture stride toString())
			for(row in 0..image size height) {
				sourceRow := sourcePointer + row * sourceStride
				destinationRow := destinationPointer + row * destinationStride
				memcpy(destinationRow, sourceRow, destinationStride)
			}
		}
		else {
			memcpy(destinationPointer, sourcePointer, destinationStride * destination size height)
		}
		*/
		memcpy(destinationPointer, sourcePointer, destinationStride * destination size height)

		this _targetTexture unlock()
	}
}<commit_msg>Separated drawing and reading for optimization<commit_after>//
// Copyright (c) 2011-2014 <NAME> <<EMAIL>>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use ooc-math
use ooc-draw
use ooc-draw-gpu
use ooc-base
use ooc-opengl
use ooc-android-debug
import math, EglRgba, AndroidContext

GpuPacker: class {
	_renderTarget: Fbo
	_targetTexture: EglRgba
	_context: AndroidContext
	_size: IntSize2D
	size: IntSize2D { get { this _size } }
	_internalSize: IntSize2D
	_bytesPerPixel: UInt
	bytesPerPixel: UInt { get { this _bytesPerPixel } }
	init: func (size: IntSize2D, bytesPerPixel: UInt, context: AndroidContext) {
		this _bytesPerPixel = bytesPerPixel
		this _context = context
		this _size = size
		this _internalSize = IntSize2D new(size width * bytesPerPixel / 4, size height)
		this _bytesPerPixel = bytesPerPixel
		this _targetTexture = context createEglRgba(this _internalSize)
		this _renderTarget = Fbo create(this _targetTexture texture, this _internalSize width, this _internalSize height)
	}
	recycle: func {
		this _context recycle(this)
	}
	dispose: func {
		this _targetTexture dispose()
		this _renderTarget dispose()
	}
	pack: func (image: GpuImage, map: OpenGLES3MapDefault) {
		map transform = FloatTransform2D identity
		map imageSize = image size
		map screenSize = image size
		this _renderTarget bind()
		this _renderTarget clear()
		surface := this _context createSurface()
		surface draw(image, map, Viewport new(this _internalSize))
		surface recycle()
		this _renderTarget unbind()
	}
	read: func ~ByteBuffer -> ByteBuffer {
		sourcePointer := this _targetTexture lock()
		buffer := ByteBuffer new(this _targetTexture stride * this _targetTexture size height, sourcePointer,
			func (buffer: ByteBuffer){ this _targetTexture unlock()
						 this recycle()
						})
		buffer
	}
	finish: static func {
		Fbo finish()
	}
	flush: static func {
		Fbo flush()
	}
	read: func (destination: RasterImage) {
		sourcePointer := this _targetTexture lock()
		destinationPointer := destination pointer
		destinationStride := destination stride
		sourceStride := this _targetTexture stride
		/*
		if (this _targetTexture isPadded()) {
			printAndroid("Warning: Copying row by row")
			printAndroid("Stride: " + this _targetTexture stride toString())
			for(row in 0..image size height) {
				sourceRow := sourcePointer + row * sourceStride
				destinationRow := destinationPointer + row * destinationStride
				memcpy(destinationRow, sourceRow, destinationStride)
			}
		}
		else {
			memcpy(destinationPointer, sourcePointer, destinationStride * destination size height)
		}
		*/
		memcpy(destinationPointer, sourcePointer, destinationStride * destination size height)
		this _targetTexture unlock()
	}
}
{'fraction_non_alphanumeric': 0.0803932909196067, 'fraction_numerical': 0.005494505494505495, 'mean_word_length': 4.349574632637277, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4212824', 'n_tokens_mistral': 2140, 'n_tokens_neox': 1967, 'n_words': 727}