(Automated Tests) Sync with core tests
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / test-actor-utils.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include "test-actor-utils.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/dali-core.h>
23 #include <dali/devel-api/images/texture-set-image.h>
24
25 // INTERNAL INCLUDES
26 #include "mesh-builder.h"
27
28 namespace Dali
29 {
30
31 namespace
32 {
33
34 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
35   attribute mediump vec2 aPosition;\n
36   uniform mediump mat4 uMvpMatrix;\n
37   uniform mediump vec3 uSize;\n
38   \n
39   void main()\n
40   {\n
41     mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);\n
42     vertexPosition.xyz *= uSize;\n
43     gl_Position = uMvpMatrix * vertexPosition;\n
44   }\n
45 );
46
47 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
48   uniform lowp vec4 uColor;\n
49   \n
50   void main()\n
51   {\n
52     gl_FragColor = uColor;\n
53   }\n
54 );
55
56 } // unnamed namespace
57
58 Actor CreateRenderableActor()
59 {
60   return CreateRenderableActor( Image(), VERTEX_SHADER, FRAGMENT_SHADER );
61 }
62
63 Actor CreateRenderableActor( Image texture )
64 {
65   return CreateRenderableActor( texture, VERTEX_SHADER, FRAGMENT_SHADER );
66 }
67
68 Actor CreateRenderableActor( Image texture, const std::string& vertexShader, const std::string& fragmentShader )
69 {
70   // Create the geometry
71   Geometry geometry = CreateQuadGeometry();
72
73   // Create Shader
74   Shader shader = Shader::New( vertexShader, fragmentShader );
75
76   // Create renderer from geometry and material
77   Renderer renderer = Renderer::New( geometry, shader );
78
79   // Create actor and set renderer
80   Actor actor = Actor::New();
81   actor.AddRenderer( renderer );
82
83   // If we a texture, then create a texture-set and add to renderer
84   if( texture )
85   {
86     TextureSet textureSet = TextureSet::New();
87     TextureSetImage( textureSet, 0u, texture );
88     renderer.SetTextures( textureSet );
89
90     // Set actor to the size of the texture if set
91     actor.SetSize( texture.GetWidth(), texture.GetHeight() );
92   }
93
94   return actor;
95 }
96
97 Image GetTexture( Actor actor )
98 {
99   Image image;
100   if( actor && actor.GetRendererCount() )
101   {
102     Renderer renderer = actor.GetRendererAt( 0u );
103     if( renderer )
104     {
105       TextureSet textureSet = renderer.GetTextures();
106       if( textureSet && textureSet.GetTextureCount() )
107       {
108         image = TextureGetImage( textureSet, 0u );
109       }
110     }
111   }
112
113   return image;
114 }
115
116 } // namespace Dali