[Tizen] Fix SVACE issue at gles2-graphics-memory
[platform/core/uifw/dali-adaptor.git] / dali / internal / graphics / gles-impl / gles2-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 "gles2-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 <dali/internal/graphics/common/graphics-interface.h>
27 #include "egl-graphics-controller.h"
28 #include "gles-graphics-buffer.h"
29
30 namespace Dali::Graphics::GLES
31 {
32 Memory2::Memory2(const Graphics::MapBufferInfo& mapInfo, EglGraphicsController& controller)
33 : mController(controller)
34 {
35   mMapBufferInfo = mapInfo;
36   mMapObjectType = MapObjectType::BUFFER;
37 }
38
39 Memory2::Memory2(const Graphics::MapTextureInfo& mapInfo, EglGraphicsController& controller)
40 : mController(controller)
41 {
42   mMapTextureInfo = mapInfo;
43   mMapObjectType  = MapObjectType::TEXTURE;
44 }
45
46 Memory2::~Memory2()
47 {
48   Unlock(true);
49 }
50
51 void* Memory2::LockRegion(uint32_t offset, uint32_t size)
52 {
53   // allocate temporary buffer (reading back may not be supported)
54   // emulated buffer is always mapped from beginning
55   if(mMapObjectType == MapObjectType::BUFFER)
56   {
57     auto buffer = static_cast<GLES::Buffer*>(mMapBufferInfo.buffer);
58     if(buffer->IsCPUAllocated())
59     {
60       using Ptr           = char*;
61       mMappedPointer      = Ptr(buffer->GetCPUAllocatedAddress()) + offset;
62       mIsAllocatedLocally = false;
63     }
64     else
65     {
66       auto retval         = malloc(size);
67       mMappedPointer      = retval;
68       mIsAllocatedLocally = true;
69     }
70   }
71
72   return mMappedPointer;
73 }
74
75 void Memory2::Unlock(bool flush)
76 {
77   auto gl = mController.GetGL();
78
79   // for buffer...
80   if(mMapObjectType == MapObjectType::BUFFER && mMappedPointer)
81   {
82     auto buffer = static_cast<GLES::Buffer*>(mMapBufferInfo.buffer);
83     if(!buffer->IsCPUAllocated())
84     {
85       buffer->Bind(BufferUsage::VERTEX_BUFFER);
86       gl->BufferSubData(GL_ARRAY_BUFFER, GLintptr(mMapBufferInfo.offset), GLsizeiptr(mMapBufferInfo.size), mMappedPointer);
87     }
88   }
89
90   if(mIsAllocatedLocally)
91   {
92     free(mMappedPointer);
93     mMappedPointer = nullptr;
94   }
95
96   if(flush)
97   {
98     Flush();
99   }
100 }
101
102 void Memory2::Flush()
103 {
104   // TODO:
105 }
106
107 } // namespace Dali::Graphics::GLES