[dali_2.3.20] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / test-actor-utils.cpp
1 /*
2  * Copyright (c) 2023 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
24 // INTERNAL INCLUDES
25 #include <dali-test-suite-utils.h>
26 #include "mesh-builder.h"
27
28 namespace Dali
29 {
30 namespace
31 {
32 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
33   attribute mediump vec2   aPosition;\n
34     uniform mediump mat4   uMvpMatrix;\n
35       uniform mediump vec3 uSize;\n
36   \n void main()\n {
37         \n
38           mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);
39         \n
40           vertexPosition.xyz *= uSize;
41         \n
42           gl_Position = uMvpMatrix * vertexPosition;
43         \n
44       }\n);
45
46 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
47   uniform lowp vec4 uColor;\n
48   \n void main()\n {
49     \n
50       gl_FragColor = uColor;
51     \n
52   }\n);
53
54 } // unnamed namespace
55
56 Actor CreateRenderableActor()
57 {
58   return CreateRenderableActor(Texture(), VERTEX_SHADER, FRAGMENT_SHADER);
59 }
60
61 Actor CreateRenderableActor(Texture texture)
62 {
63   return CreateRenderableActor(texture, VERTEX_SHADER, FRAGMENT_SHADER);
64 }
65
66 Actor CreateRenderableActor(Texture texture, const std::string& vertexShader, const std::string& fragmentShader)
67 {
68   // Create the geometry
69   Geometry geometry = CreateQuadGeometry();
70
71   // Create Shader
72   Shader shader = Shader::New(vertexShader, fragmentShader);
73
74   // Create renderer from geometry and material
75   Renderer renderer = Renderer::New(geometry, shader);
76
77   // Create actor and set renderer
78   Actor actor = Actor::New();
79   actor.AddRenderer(renderer);
80
81   // If we a texture, then create a texture-set and add to renderer
82   if(texture)
83   {
84     TextureSet textureSet = TextureSet::New();
85     textureSet.SetTexture(0u, texture);
86     renderer.SetTextures(textureSet);
87
88     // Set actor to the size of the texture if set
89     actor.SetProperty(Actor::Property::SIZE, Vector2(texture.GetWidth(), texture.GetHeight()));
90   }
91
92   return actor;
93 }
94
95 Actor CreateRenderableActor2(TextureSet textures, const std::string& vertexShader, const std::string& fragmentShader)
96 {
97   // Create the geometry
98   Geometry geometry = CreateQuadGeometry();
99
100   // Create Shader
101   Shader shader = Shader::New(vertexShader, fragmentShader);
102
103   // Create renderer from geometry and material
104   Renderer renderer = Renderer::New(geometry, shader);
105
106   // Create actor and set renderer
107   Actor actor = Actor::New();
108   actor.AddRenderer(renderer);
109
110   // If we a texture, then create a texture-set and add to renderer
111   if(textures)
112   {
113     renderer.SetTextures(textures);
114
115     auto texture = textures.GetTexture(0);
116
117     // Set actor to the size of the texture if set
118     actor.SetProperty(Actor::Property::SIZE, Vector2(texture.GetWidth(), texture.GetHeight()));
119   }
120
121   return actor;
122 }
123
124 Texture CreateTexture(TextureType::Type type, Pixel::Format format, int width, int height)
125 {
126   Texture texture = Texture::New(type, format, width, height);
127
128   int       bufferSize = width * height * GetBytesPerPixel(format);
129   uint8_t*  buffer     = reinterpret_cast<uint8_t*>(malloc(bufferSize));
130   PixelData pixelData  = PixelData::New(buffer, bufferSize, width, height, format, PixelData::FREE);
131   texture.Upload(pixelData, 0u, 0u, 0u, 0u, width, height);
132   return texture;
133 }
134
135 TextureSet CreateTextureSet(Pixel::Format format, int width, int height)
136 {
137   TextureSet textureSet = TextureSet::New();
138   textureSet.SetTexture(0u, CreateTexture(TextureType::TEXTURE_2D, format, width, height));
139   return textureSet;
140 }
141
142 void DirtyRectChecker(const std::vector<Rect<int>>& damagedRects, std::multiset<Rect<int>, RectSorter> expectedRectList, bool checkRectsExact, const char* testLocation)
143 {
144   // Just check damagedRect contain all expectRectList.
145   DALI_TEST_GREATER(damagedRects.size() + 1u, expectedRectList.size(), testLocation);
146
147   for(auto& rect : damagedRects)
148   {
149     auto iter = expectedRectList.find(rect);
150     if(iter != expectedRectList.end())
151     {
152       expectedRectList.erase(iter);
153     }
154     else if(checkRectsExact)
155     {
156       std::ostringstream o;
157       o << rect << " exist in expectRectList" << std::endl;
158       fprintf(stderr, "Test failed in %s, checking %s", testLocation, o.str().c_str());
159       tet_result(TET_FAIL);
160     }
161   }
162
163   // Check all rects are matched
164   DALI_TEST_EQUALS(expectedRectList.empty(), true, testLocation);
165 }
166
167 } // namespace Dali