90640e5f7c71d826274a17bdde23ccd4eb0eac07
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / test-graphics-buffer.cpp
1 /*
2  * Copyright (c) 2021 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 #include "test-graphics-buffer.h"
18 #include <sstream>
19 #include "dali-test-suite-utils.h"
20
21 namespace Dali
22 {
23 TestGraphicsBuffer::TestGraphicsBuffer(TraceCallStack& callStack, TestGlAbstraction& glAbstraction, uint32_t size, Graphics::BufferUsageFlags usage)
24 : mCallStack(callStack),
25   mGl(glAbstraction),
26   mUsage(usage)
27 {
28   memory.reserve(size);
29   mGl.GetBufferTrace().EnableLogging(true);
30 }
31
32 void TestGraphicsBuffer::Bind()
33 {
34   mCallStack.PushCall("Buffer::Bind", "");
35   if(!mId)
36   {
37     mGl.GenBuffers(1, &mId);
38   }
39   mGl.BindBuffer(GetTarget(), mId);
40 }
41
42 void TestGraphicsBuffer::Unbind()
43 {
44   mCallStack.PushCall("Buffer::Unbind", "");
45   if(mId)
46   {
47     mGl.BindBuffer(GetTarget(), 0);
48   }
49 }
50
51 void TestGraphicsBuffer::Upload(uint32_t offset, uint32_t size)
52 {
53   std::ostringstream o;
54   o << offset << ", " << size;
55   TraceCallStack::NamedParams namedParams;
56   namedParams["offset"] << offset;
57   namedParams["size"] << size;
58   mCallStack.PushCall("Buffer::Upload", o.str(), namedParams);
59   if(size <= memory.size() && mCreated)
60   {
61     // Use subData to avoid re-allocation
62     mGl.BufferSubData(GetTarget(), offset, size, &memory[offset]);
63   }
64   else
65   {
66     mGl.BufferData(GetTarget(), size, &memory[0], GL_STATIC_DRAW); //@todo Query - do we need other usages?
67     mCreated = true;
68   }
69 }
70
71 GLenum TestGraphicsBuffer::GetTarget()
72 {
73   GLenum target = GL_ARRAY_BUFFER;
74   if((mUsage & static_cast<Graphics::BufferUsageFlags>(Graphics::BufferUsage::INDEX_BUFFER)) != 0)
75   {
76     target = GL_ELEMENT_ARRAY_BUFFER;
77   }
78   return target;
79 }
80
81 } // namespace Dali