70852761ff7cbca8edc132921ebce831adffd104
[platform/core/uifw/dali-adaptor.git] / dali / internal / graphics / gles-impl / gles3-graphics-memory.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
18 // CLASS HEADER
19 #include "gles3-graphics-memory.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/gl-abstraction.h>
23 #include <dali/integration-api/gl-defines.h>
24
25 // INTERNAL INCLUDES
26 #include "egl-graphics-controller.h"
27
28 namespace Dali::Graphics::GLES
29 {
30 Memory3::Memory3(const Graphics::MapBufferInfo& mapInfo, EglGraphicsController& controller)
31 : mController(controller)
32 {
33   mMapBufferInfo = mapInfo;
34   mMapObjectType = MapObjectType::BUFFER;
35 }
36
37 Memory3::Memory3(const Graphics::MapTextureInfo& mapInfo, EglGraphicsController& controller)
38 : mController(controller)
39 {
40   mMapTextureInfo = mapInfo;
41   mMapObjectType  = MapObjectType::TEXTURE;
42 }
43
44 Memory3::~Memory3()
45 {
46   Unlock(true);
47 }
48
49 void* Memory3::LockRegion(uint32_t offset, uint32_t size)
50 {
51   auto gl = mController.GetGL();
52
53   if(mMapObjectType == MapObjectType::BUFFER)
54   {
55     auto buffer = static_cast<GLES::Buffer*>(mMapBufferInfo.buffer);
56
57     if(buffer->IsCPUAllocated())
58     {
59       using Ptr = char*;
60       return Ptr(buffer->GetCPUAllocatedAddress()) + offset;
61     }
62     else
63     {
64       // @TODO: trashing vertex binding, better find target that is rarely used
65       buffer->Bind(Graphics::BufferUsage::VERTEX_BUFFER);
66       void* ptr      = nullptr;
67       ptr            = gl->MapBufferRange(GL_ARRAY_BUFFER, mMapBufferInfo.offset, mMapBufferInfo.size, GL_MAP_WRITE_BIT);
68       mMappedPointer = ptr;
69     }
70     return mMappedPointer;
71   }
72
73   return nullptr;
74 }
75
76 void Memory3::Unlock(bool flush)
77 {
78   auto gl = mController.GetGL();
79
80   if(mMapObjectType == MapObjectType::BUFFER && mMappedPointer)
81   {
82     auto buffer = static_cast<GLES::Buffer*>(mMapBufferInfo.buffer);
83     if(!buffer->IsCPUAllocated())
84     {
85       buffer->Bind(Graphics::BufferUsage::VERTEX_BUFFER);
86       gl->UnmapBuffer(GL_ARRAY_BUFFER);
87     }
88   }
89
90   if(flush)
91   {
92     Flush();
93   }
94 }
95
96 void Memory3::Flush()
97 {
98   // TODO:
99 }
100
101 } // namespace Dali::Graphics::GLES