2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "gles2-graphics-memory.h"
22 #include <dali/integration-api/gl-abstraction.h>
23 #include <dali/integration-api/gl-defines.h>
26 #include "egl-graphics-controller.h"
27 #include "gles-graphics-buffer.h"
29 namespace Dali::Graphics::GLES
31 Memory2::Memory2(const Graphics::MapBufferInfo& mapInfo, EglGraphicsController& controller)
32 : mController(controller)
34 mMapBufferInfo = mapInfo;
35 mMapObjectType = MapObjectType::BUFFER;
38 Memory2::Memory2(const Graphics::MapTextureInfo& mapInfo, EglGraphicsController& controller)
39 : mController(controller)
41 mMapTextureInfo = mapInfo;
42 mMapObjectType = MapObjectType::TEXTURE;
50 void* Memory2::LockRegion(uint32_t offset, uint32_t size)
52 // allocate temporary buffer (reading back may not be supported)
53 // emulated buffer is always mapped from beginning
54 if(mMapObjectType == MapObjectType::BUFFER)
56 auto buffer = static_cast<GLES::Buffer*>(mMapBufferInfo.buffer);
57 if(buffer->IsCPUAllocated())
60 mMappedPointer = Ptr(buffer->GetCPUAllocatedAddress()) + offset;
61 mIsAllocatedLocally = false;
65 auto retval = malloc(size);
66 mMappedPointer = retval;
67 mIsAllocatedLocally = true;
71 return mMappedPointer;
74 void Memory2::Unlock(bool flush)
76 auto gl = mController.GetGL();
79 if(mMapObjectType == MapObjectType::BUFFER&& mMappedPointer)
81 auto buffer = static_cast<GLES::Buffer*>(mMapBufferInfo.buffer);
82 if(!buffer->IsCPUAllocated())
84 buffer->Bind(BufferUsage::VERTEX_BUFFER);
85 gl->BufferSubData(GL_ARRAY_BUFFER, mMapBufferInfo.offset, mMapBufferInfo.size, mMappedPointer);
89 if(mIsAllocatedLocally)
92 mMappedPointer = nullptr;
101 void Memory2::Flush()
106 } // namespace Dali::Graphics::GLES