0d2003aacb3c4170804a02690a6cf7d18c24d518
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / gpu-buffer.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 // CLASS HEADER
19 #include <dali/internal/render/renderers/gpu-buffer.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/graphics-api/graphics-types.h>
23 #include <dali/public-api/common/dali-common.h>
24 #include <cstring>
25
26 namespace Dali
27 {
28 namespace Internal
29 {
30 namespace
31 {
32 } // namespace
33
34 GpuBuffer::GpuBuffer(Graphics::Controller& graphicsController, Graphics::BufferUsageFlags usage)
35 : mUsage(usage)
36 {
37 }
38
39 void GpuBuffer::UpdateDataBuffer(Graphics::Controller& graphicsController, uint32_t size, const void* data)
40 {
41   DALI_ASSERT_DEBUG(size > 0);
42   mSize = size;
43
44   if(!mGraphicsObject || size > mCapacity)
45   {
46     Graphics::BufferCreateInfo createInfo{};
47     createInfo.SetUsage(mUsage).SetSize(size);
48     mGraphicsObject = graphicsController.CreateBuffer(createInfo, std::move(mGraphicsObject));
49     mCapacity       = size;
50   }
51
52   Graphics::MapBufferInfo info{};
53   info.buffer = mGraphicsObject.get();
54   info.usage  = 0 | Graphics::MemoryUsageFlagBits::WRITE;
55   info.offset = 0;
56   info.size   = size;
57
58   auto  memory = graphicsController.MapBufferRange(info);
59   void* ptr    = memory->LockRegion(0, size);
60   memcpy(ptr, data, size);
61   memory->Unlock(true);
62   graphicsController.UnmapMemory(std::move(memory));
63 }
64
65 void GpuBuffer::Destroy()
66 {
67   mCapacity = 0;
68   mSize     = 0;
69   mGraphicsObject.reset();
70 }
71
72 } // namespace Internal
73
74 } //namespace Dali