Multiple context support
[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 <dali/internal/graphics/common/graphics-interface.h>
27 #include "egl-graphics-controller.h"
28
29 namespace Dali::Graphics::GLES
30 {
31 Memory3::Memory3(const Graphics::MapBufferInfo& mapInfo, EglGraphicsController& controller)
32 : mController(controller)
33 {
34   mMapBufferInfo = mapInfo;
35   mMapObjectType = MapObjectType::BUFFER;
36 }
37
38 Memory3::Memory3(const Graphics::MapTextureInfo& mapInfo, EglGraphicsController& controller)
39 : mController(controller)
40 {
41   mMapTextureInfo = mapInfo;
42   mMapObjectType  = MapObjectType::TEXTURE;
43 }
44
45 Memory3::~Memory3()
46 {
47   Unlock(true);
48 }
49
50 void* Memory3::LockRegion(uint32_t offset, uint32_t size)
51 {
52   auto gl = mController.GetGL();
53
54   if(mMapObjectType == MapObjectType::BUFFER)
55   {
56     auto buffer = static_cast<GLES::Buffer*>(mMapBufferInfo.buffer);
57
58     if(buffer->IsCPUAllocated())
59     {
60       using Ptr = char*;
61       return Ptr(buffer->GetCPUAllocatedAddress()) + offset;
62     }
63     else
64     {
65       // switch to the shared context if necessary
66       auto graphics = mController.GetGraphicsInterface();
67       graphics->ActivateResourceContext();
68
69       // @TODO: trashing vertex binding, better find target that is rarely used
70       buffer->Bind(Graphics::BufferUsage::VERTEX_BUFFER);
71       void* ptr      = nullptr;
72       ptr            = gl->MapBufferRange(GL_ARRAY_BUFFER, mMapBufferInfo.offset, mMapBufferInfo.size, GL_MAP_WRITE_BIT);
73       mMappedPointer = ptr;
74     }
75     return mMappedPointer;
76   }
77
78   return nullptr;
79 }
80
81 void Memory3::Unlock(bool flush)
82 {
83   auto gl = mController.GetGL();
84
85   if(mMapObjectType == MapObjectType::BUFFER && mMappedPointer)
86   {
87     auto buffer = static_cast<GLES::Buffer*>(mMapBufferInfo.buffer);
88     if(!buffer->IsCPUAllocated())
89     {
90       // switch to the shared context if necessary
91       auto graphics = mController.GetGraphicsInterface();
92       graphics->ActivateResourceContext();
93
94       buffer->Bind(Graphics::BufferUsage::VERTEX_BUFFER);
95       gl->UnmapBuffer(GL_ARRAY_BUFFER);
96     }
97   }
98
99   if(flush)
100   {
101     Flush();
102   }
103 }
104
105 void Memory3::Flush()
106 {
107   // TODO:
108 }
109
110 } // namespace Dali::Graphics::GLES