bool
TaskContourGL::run(RunParams & /* params */) const
{
	gl::Context::Lock lock(env().context);

	SurfaceGL::Handle target =
		SurfaceGL::Handle::cast_dynamic(target_surface);

	// transformation

	Vector rect_size = get_source_rect_rb() - get_source_rect_lt();
	Matrix bounds_transfromation;
	bounds_transfromation.m00 = fabs(rect_size[0]) > 1e-10 ? 2.0/rect_size[0] : 0.0;
	bounds_transfromation.m11 = fabs(rect_size[1]) > 1e-10 ? 2.0/rect_size[1] : 0.0;
	bounds_transfromation.m20 = -1.0 - get_source_rect_lt()[0]*bounds_transfromation.m00;
	bounds_transfromation.m21 = -1.0 - get_source_rect_lt()[1]*bounds_transfromation.m11;

	Matrix matrix = transformation * bounds_transfromation;

	if (valid_target())
	{
		// check bounds
		std::vector<Vector> polygon;
		Rect bounds(-1.0, -1.0, 1.0, 1.0);
		Vector pixel_size(
			2.0/(Real)(get_target_rect().maxx - get_target_rect().minx),
			2.0/(Real)(get_target_rect().maxy - get_target_rect().miny) );
		contour->split(polygon, bounds, matrix, pixel_size);

		// bind framebuffer

		gl::Framebuffers::RenderbufferLock renderbuffer = env().framebuffers.get_renderbuffer(GL_STENCIL_INDEX8, target->get_width(), target->get_height());
		gl::Framebuffers::FramebufferLock framebuffer = env().framebuffers.get_framebuffer();
		glBindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer.get_id());
		glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, renderbuffer.get_id());
		glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, target->get_id(), 0);
		env().framebuffers.check("TaskContourGL::run bind framebuffer");
		glViewport(
			get_target_rect().minx,
			get_target_rect().miny,
			get_target_rect().maxx - get_target_rect().minx,
			get_target_rect().maxy - get_target_rect().miny );
		env().context.check("TaskContourGL::run viewport");

		// render

		render_polygon(
			polygon,
			bounds,
			contour->invert,
			contour->antialias,
			contour->winding_style,
			contour->color );

		// release framebuffer

		glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
		glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0);
		glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
		env().context.check("TaskContourGL::run release contour");
	}

	return true;
}
bool
TaskSurfaceResampleGL::run(RunParams & /* params */) const
{
	gl::Context::Lock lock(env().context);

	SurfaceGL::Handle a =
		SurfaceGL::Handle::cast_dynamic(sub_task()->target_surface);
	SurfaceGL::Handle target =
		SurfaceGL::Handle::cast_dynamic(target_surface);

	// TODO: gamma

	Vector rect_size = get_source_rect_rb() - get_source_rect_lt();
	Matrix bounds_transfromation;
	bounds_transfromation.m00 = fabs(rect_size[0]) > 1e-10 ? 2.0/rect_size[0] : 0.0;
	bounds_transfromation.m11 = fabs(rect_size[1]) > 1e-10 ? 2.0/rect_size[1] : 0.0;
	bounds_transfromation.m20 = -1.0 - get_source_rect_lt()[0] * bounds_transfromation.m00;
	bounds_transfromation.m21 = -1.0 - get_source_rect_lt()[1] * bounds_transfromation.m11;

	Matrix matrix = transformation * bounds_transfromation;

	// prepare arrays
	Vector k(target->get_width(), target->get_height());
	Vector d( matrix.get_axis_x().multiply_coords(k).norm().divide_coords(k).mag() / matrix.get_axis_x().mag(),
			  matrix.get_axis_y().multiply_coords(k).norm().divide_coords(k).mag() / matrix.get_axis_y().mag() );
	d *= 4.0;
	Vector coords[4][3];
	for(int i = 0; i < 4; ++i)
	{
		coords[i][2] = Vector(i%2 ? 1.0 : -1.0, i/2 ? 1.0 : -1.0).multiply_coords(Vector(1.0, 1.0) + d);
		coords[i][0] = matrix.get_transformed(coords[i][2]*0.5 + Vector(0.5, 0.5));
		coords[i][1] = (coords[i][2] + Vector(1.0, 1.0))*0.5;
	}
	Vector aascale = d.one_divide_coords();

	gl::Framebuffers::FramebufferLock framebuffer = env().framebuffers.get_framebuffer();
	glBindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer.get_id());
	glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, target->get_id(), 0);
	glViewport(
		get_target_rect().minx,
		get_target_rect().miny,
		get_target_rect().maxx - get_target_rect().minx,
		get_target_rect().maxy - get_target_rect().miny );
	env().context.check();

	glBindTexture(GL_TEXTURE_2D, a->get_id());
	glBindSampler(0, env().samplers.get_interpolation(interpolation));
	env().context.check();

	gl::Buffers::BufferLock buf = env().buffers.get_array_buffer(coords);
	gl::Buffers::VertexArrayLock va = env().buffers.get_vertex_array();
	env().context.check();

	glBindVertexArray(va.get_id());
	glBindBuffer(GL_ARRAY_BUFFER, buf.get_id());
	glEnableVertexAttribArray(0);
	glEnableVertexAttribArray(1);
	glEnableVertexAttribArray(2);
	glVertexAttribPointer(0, 2, GL_DOUBLE, GL_TRUE, sizeof(coords[0]), (const char*)buf.get_pointer() + 0*sizeof(coords[0][0]));
	glVertexAttribPointer(1, 2, GL_DOUBLE, GL_TRUE, sizeof(coords[0]), (const char*)buf.get_pointer() + 1*sizeof(coords[0][0]));
	glVertexAttribPointer(2, 2, GL_DOUBLE, GL_TRUE, sizeof(coords[0]), (const char*)buf.get_pointer() + 2*sizeof(coords[0][0]));
	glBindBuffer(GL_ARRAY_BUFFER, 0);
	env().context.check();

	env().shaders.antialiased_textured_rect(interpolation, aascale);
	glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
	env().context.check();

	glDisableVertexAttribArray(0);
	glDisableVertexAttribArray(1);
	glDisableVertexAttribArray(2);
	glBindVertexArray(0);
	env().context.check();

	glBindSampler(0, 0);
	glBindTexture(GL_TEXTURE_2D, 0);
	env().context.check();

	glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
	glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
	env().context.check();

	return true;
}