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 <dali/internal/graphics/common/graphics-interface.h>
27 #include "egl-graphics-controller.h"
28 #include "gles-graphics-buffer.h"
30 namespace Dali::Graphics::GLES
32 Memory2::Memory2(const Graphics::MapBufferInfo& mapInfo, EglGraphicsController& controller)
33 : mController(controller)
35 mMapBufferInfo = mapInfo;
36 mMapObjectType = MapObjectType::BUFFER;
39 Memory2::Memory2(const Graphics::MapTextureInfo& mapInfo, EglGraphicsController& controller)
40 : mController(controller)
42 mMapTextureInfo = mapInfo;
43 mMapObjectType = MapObjectType::TEXTURE;
51 void* Memory2::LockRegion(uint32_t offset, uint32_t size)
53 // allocate temporary buffer (reading back may not be supported)
54 // emulated buffer is always mapped from beginning
55 if(mMapObjectType == MapObjectType::BUFFER)
57 auto buffer = static_cast<GLES::Buffer*>(mMapBufferInfo.buffer);
58 if(buffer->IsCPUAllocated())
61 mMappedPointer = Ptr(buffer->GetCPUAllocatedAddress()) + offset;
62 mIsAllocatedLocally = false;
66 auto retval = malloc(size);
67 mMappedPointer = retval;
68 mIsAllocatedLocally = true;
72 return mMappedPointer;
75 void Memory2::Unlock(bool flush)
77 auto gl = mController.GetGL();
80 if(mMapObjectType == MapObjectType::BUFFER && mMappedPointer)
82 auto buffer = static_cast<GLES::Buffer*>(mMapBufferInfo.buffer);
83 if(!buffer->IsCPUAllocated())
85 buffer->Bind(BufferUsage::VERTEX_BUFFER);
86 gl->BufferSubData(GL_ARRAY_BUFFER, GLintptr(mMapBufferInfo.offset), GLsizeiptr(mMapBufferInfo.size), mMappedPointer);
90 if(mIsAllocatedLocally)
93 mMappedPointer = nullptr;
102 void Memory2::Flush()
107 } // namespace Dali::Graphics::GLES