Merge remote-tracking branch 'origin/tizen' into new_text
[platform/core/uifw/dali-core.git] / automated-tests / src / dali-internal / utc-Dali-Internal-Mesh.cpp
1 /*
2  * Copyright (c) 2014 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 #include <iostream>
19
20 #include <stdlib.h>
21 #include <dali/public-api/dali-core.h>
22 #include <dali-test-suite-utils.h>
23
24 // Internal headers are allowed here
25
26 #include <dali/internal/event/common/thread-local-storage.h>
27 #include <dali/internal/update/resources/resource-manager.h>
28 #include <dali/internal/event/resources/resource-client.h>
29 #include <dali/internal/event/resources/resource-ticket.h>
30 #include <dali/internal/update/modeling/scene-graph-mesh.h>
31
32 using namespace Dali;
33 #include "mesh-builder.h"
34
35 void utc_dali_mesh_startup(void)
36 {
37   test_return_value = TET_UNDEF;
38 }
39
40 void utc_dali_mesh_cleanup(void)
41 {
42   test_return_value = TET_PASS;
43 }
44
45 namespace
46 {
47
48 unsigned int GetNextResourceId(TestApplication& application)
49 {
50   // Attempt to determine what the next resource ID will be
51   Internal::ResourceClient& resourceClient  = Internal::ThreadLocalStorage::Get().GetResourceClient();
52   Internal::ResourceTicketPtr resourceTicket = resourceClient.AllocateTexture( 80, 80, Pixel::RGBA8888 );
53   unsigned int nextResourceId = resourceTicket->GetId();
54   nextResourceId++;
55
56   // Clear allocation request through the system
57   application.SendNotification();
58   application.Render();
59   application.SendNotification();
60
61   return nextResourceId;
62 }
63
64 Mesh CreateMesh( TestApplication& application )
65 {
66   Dali::MeshData publicMeshData;
67   {
68     MeshData::VertexContainer    publicVertices;
69     MeshData::FaceIndices        publicFaces;
70     BoneContainer                bones;
71     ConstructVertices(publicVertices, 60);
72     ConstructFaces(publicVertices, publicFaces);
73     Dali::Material material  = ConstructMaterial();
74     publicMeshData.SetData(publicVertices, publicFaces, bones, material);
75   }
76   Mesh publicMesh = Mesh::New(publicMeshData); // Will generate new request
77
78   application.SendNotification();
79   application.Render(); // Should allocate mesh, pass ownership to ResourceManager
80   application.SendNotification();
81   return publicMesh;
82 }
83
84 Internal::MeshData& GetStagedMeshData( unsigned int resourceId )
85 {
86     // ResourceManager::GetMesh() will give us the scene graph mesh from resource id
87   Internal::ResourceManager& resourceManager  = Internal::ThreadLocalStorage::Get().GetResourceManager();
88   Internal::SceneGraph::Mesh* internalMesh = resourceManager.GetMesh(resourceId);
89   DALI_TEST_CHECK( internalMesh != NULL );
90   return internalMesh->GetMeshData(Internal::SceneGraph::Mesh::UPDATE_THREAD);
91 }
92
93
94 void TestMeshDiscard( ResourcePolicy::DataRetention policy, bool expectedDiscardResult )
95 {
96   TestApplication application( TestApplication::DEFAULT_SURFACE_WIDTH,
97                                TestApplication::DEFAULT_SURFACE_HEIGHT,
98                                TestApplication::DEFAULT_HORIZONTAL_DPI,
99                                TestApplication::DEFAULT_VERTICAL_DPI,
100                                policy );
101
102   // run through startup, clearing all requests/messages
103   application.SendNotification();
104   application.Render();
105   application.SendNotification();
106
107   unsigned int nextResourceId = GetNextResourceId(application);
108   Mesh publicMesh = CreateMesh(application);
109   Internal::MeshData& meshData = GetStagedMeshData(nextResourceId);
110
111   // Check that the vertex data is allocated
112   const Internal::MeshData::VertexContainer& internalVertices = meshData.GetVertices();
113   const Internal::MeshData::FaceIndices& internalFaces = meshData.GetFaces();
114   DALI_TEST_CHECK( meshData.GetVertexCount() > 0 );
115   DALI_TEST_CHECK( meshData.GetFaceCount() > 0 );
116   DALI_TEST_CHECK( internalVertices.size() > 0 );
117   DALI_TEST_CHECK( internalFaces.size() > 0 );
118
119   // Create an actor that will render the mesh
120   MeshActor actor = MeshActor::New( publicMesh );
121   std::string name = "AMeshActor";
122   actor.SetName(name);
123   Stage::GetCurrent().Add(actor);
124
125   // render it
126   application.SendNotification();
127   application.Render();
128   application.SendNotification();
129
130   // Check that the vertex data has been discarded
131   DALI_TEST_CHECK( meshData.GetVertexCount() > 0 );
132   DALI_TEST_CHECK( meshData.GetFaceCount() > 0 );
133   if( expectedDiscardResult == false )
134   {
135     DALI_TEST_CHECK( internalVertices.size() > 0 );
136     DALI_TEST_CHECK( internalFaces.size() > 0 );
137   }
138   else
139   {
140     DALI_TEST_CHECK( internalVertices.size() == 0 );
141     DALI_TEST_CHECK( internalFaces.size() == 0 );
142   }
143 }
144
145 } // anon namespace
146
147
148 int utcDaliInternalMeshDiscard01(void)
149 {
150   tet_infoline("Test that internal mesh data is discarded after rendering with policy=DISCARD_ALL");
151   TestMeshDiscard( ResourcePolicy::DALI_DISCARDS_ALL_DATA, true /* Not discarded */);
152   END_TEST;
153 }
154
155 int utcDaliInternalMeshDiscard02(void)
156 {
157   tet_infoline("Test that internal mesh data is not discarded after rendering with policy=RETAIN_ALL");
158   TestMeshDiscard( ResourcePolicy::DALI_RETAINS_ALL_DATA, false /* Not discarded */);
159   END_TEST;
160 }
161
162 int utcDaliInternalMeshDiscard03(void)
163 {
164   tet_infoline("Test that internal mesh data is not discarded after rendering with policy=RETAIN_MESH");
165   TestMeshDiscard( ResourcePolicy::DALI_RETAINS_MESH_DATA, false /* Not discarded */);
166   END_TEST;
167 }