##########################################################################
#
#  Copyright (c) 2020, Image Engine Design Inc. All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions are
#  met:
#
#      * Redistributions of source code must retain the above
#        copyright notice, this list of conditions and the following
#        disclaimer.
#
#      * Redistributions in binary form must reproduce the above
#        copyright notice, this list of conditions and the following
#        disclaimer in the documentation and/or other materials provided with
#        the distribution.
#
#      * Neither the name of John Haddon nor the names of
#        any other contributors to this software may be used to endorse or
#        promote products derived from this software without specific prior
#        written permission.
#
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
#  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
#  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
#  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
#  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
#  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
#  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
#  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
#  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
#  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
##########################################################################

import imath
import math
import os
import unittest

import IECore
import IECoreScene

import Gaffer
import GafferArnold
import GafferImage
import GafferImageUI
import GafferScene
import GafferSceneTest
import GafferTest
import GafferUI
import GafferUITest
import GafferImageTest

from Qt import QtCore

@unittest.skipIf( GafferTest.inCI(), "Performance not relevant on CI platform" )
class InteractiveArnoldRenderPerformanceTest( GafferUITest.TestCase ) :

	# Arnold outputs licensing warnings that would cause failures
	failureMessageLevel = IECore.MessageHandler.Level.Error

	def runInteractive( self, useUI, useBlur, resolution ):

		script = Gaffer.ScriptNode()

		script["Camera"] = GafferScene.Camera()
		script["Camera"]["transform"]["translate"]["z"].setValue( 6 )

		script["Sphere"] = GafferScene.Sphere( "Sphere" )
		script["Sphere"]["radius"].setValue( 10 )

		script["ImageShader"] = GafferArnold.ArnoldShader()
		script["ImageShader"].loadShader( "image" )
		script["ImageShader"]["parameters"]["filename"].setValue( "${GAFFER_ROOT}/python/GafferImageTest/images/GafferChecker.exr" )
		script["ImageShader"]["parameters"]["sscale"].setValue( 16 )
		script["ImageShader"]["parameters"]["tscale"].setValue( 16 )

		script["ShaderAssignment"] = GafferScene.ShaderAssignment()
		script["ShaderAssignment"]["in"].setInput( script["Sphere"]["out"] )
		script["ShaderAssignment"]["shader"].setInput( script["ImageShader"]["out"] )

		script["Group"] = GafferScene.Group()
		script["Group"]["in"][0].setInput( script["Camera"]["out"] )
		script["Group"]["in"][1].setInput( script["ShaderAssignment"]["out"] )

		script["StandardOptions"] = GafferScene.StandardOptions()
		script["StandardOptions"]["in"].setInput( script["Group"]["out"] )
		script["StandardOptions"]["options"]["renderCamera"]["value"].setValue( '/group/camera' )
		script["StandardOptions"]["options"]["renderCamera"]["enabled"].setValue( True )
		script["StandardOptions"]["options"]["renderResolution"]["value"].setValue( imath.V2i( resolution, resolution ) )
		script["StandardOptions"]["options"]["renderResolution"]["enabled"].setValue( True )

		script["ArnoldOptions"] = GafferArnold.ArnoldOptions( "ArnoldOptions" )
		script["ArnoldOptions"]["in"].setInput( script["StandardOptions"]["out"] )
		# Make sure we leave some CPU available for Gaffer
		script["ArnoldOptions"]["options"]["threads"]["value"].setValue( -1 )
		script["ArnoldOptions"]["options"]["threads"]["enabled"].setValue( True )

		script["Outputs"] = GafferScene.Outputs()
		script["Outputs"].addOutput(
			"beauty",
			IECoreScene.Output(
				"Interactive/Beauty",
				"ieDisplay",
				"rgba",
				{
					"quantize" : IECore.IntVectorData( [ 0, 0, 0, 0 ] ),
					"driverType" : 'ClientDisplayDriver',
					"displayHost" : 'localhost',
					"displayPort" : str( GafferImage.Catalogue.displayDriverServer().portNumber() ),
					"remoteDisplayType" : 'GafferImage::GafferDisplayDriver',
					"filter" : 'box',
				}
			)
		)
		script["Outputs"]["in"].setInput( script["ArnoldOptions"]["out"] )

		script["InteractiveArnoldRender"] = GafferArnold.InteractiveArnoldRender()
		script["InteractiveArnoldRender"]["in"].setInput( script["Outputs"]["out"] )

		script["Catalogue"] = GafferImage.Catalogue( "Catalogue" )