[dali_2.3.26] Merge branch 'devel/master'
[platform/core/uifw/dali-adaptor.git] / dali / internal / imaging / common / native-bitmap-buffer-impl.cpp
1 /*
2  * Copyright (c) 2014 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 <dali/internal/imaging/common/native-bitmap-buffer-impl.h>
20
21 // EXTERNAL HEADERS
22 #include <dali/integration-api/bitmap.h>
23 #include <dali/integration-api/debug.h>
24
25 // INTERNAL HEADERS
26 #include <dali/internal/graphics/gles/egl-graphics.h>
27 #include <dali/internal/graphics/gles/gl-implementation.h>
28
29 namespace Dali
30 {
31 namespace Internal
32 {
33 namespace Adaptor
34 {
35 NativeBitmapBuffer::NativeBitmapBuffer(Adaptor* adaptor, uint32_t width, uint32_t height, Pixel::Format pFormat)
36 : mGlAbstraction(nullptr),
37   mWidth(width),
38   mHeight(height),
39   mPixelFormat(pFormat),
40   mLastReadBuffer(NULL)
41 {
42   DALI_ASSERT_ALWAYS(adaptor);
43   mBuffer = new Integration::LocklessBuffer(width * height * Pixel::GetBytesPerPixel(pFormat));
44
45   GraphicsInterface* graphics    = &(adaptor->GetGraphicsInterface());
46   auto               eglGraphics = static_cast<EglGraphics*>(graphics);
47   mGlAbstraction                 = &(eglGraphics->GetGlAbstraction());
48 }
49
50 NativeBitmapBuffer::~NativeBitmapBuffer()
51 {
52   delete mBuffer;
53 }
54
55 void NativeBitmapBuffer::PrepareTexture()
56 {
57   DALI_ASSERT_ALWAYS(mBuffer);
58   GLenum pixelFormat   = GL_RGBA;
59   GLenum pixelDataType = GL_UNSIGNED_BYTE;
60
61   Integration::ConvertToGlFormat(mPixelFormat, pixelDataType, pixelFormat);
62
63   const uint8_t* buf = mBuffer->Read();
64
65   if(buf && buf != mLastReadBuffer) // Prevent same buffer being uploaded multiple times
66   {
67     mLastReadBuffer = buf;
68
69     // The active texture has already been set to a sampler and bound.
70     mGlAbstraction->TexImage2D(GL_TEXTURE_2D, 0, pixelFormat, mWidth, mHeight, 0, pixelFormat, pixelDataType, buf);
71   }
72 }
73
74 void NativeBitmapBuffer::Write(const uint8_t* src, size_t size)
75 {
76   mBuffer->Write(src, size); // Write will cause LocklessBuffer to switch to the other buffer
77 }
78
79 bool NativeBitmapBuffer::CreateResource()
80 {
81   return true;
82 }
83
84 void NativeBitmapBuffer::DestroyResource()
85 {
86 }
87
88 uint32_t NativeBitmapBuffer::TargetTexture()
89 {
90   return 0;
91 }
92
93 uint32_t NativeBitmapBuffer::GetWidth() const
94 {
95   return mWidth;
96 }
97
98 uint32_t NativeBitmapBuffer::GetHeight() const
99 {
100   return mHeight;
101 }
102
103 bool NativeBitmapBuffer::RequiresBlending() const
104 {
105   return Pixel::HasAlpha(mPixelFormat);
106 }
107
108 int NativeBitmapBuffer::GetTextureTarget() const
109 {
110   return 0;
111 }
112
113 bool NativeBitmapBuffer::ApplyNativeFragmentShader(std::string& shader)
114 {
115   return false;
116 }
117
118 const char* NativeBitmapBuffer::GetCustomSamplerTypename() const
119 {
120   return nullptr;
121 }
122
123 Any NativeBitmapBuffer::GetNativeImageHandle() const
124 {
125   return nullptr;
126 }
127
128 bool NativeBitmapBuffer::SourceChanged() const
129 {
130   return true;
131 }
132
133 } // namespace Adaptor
134
135 } // namespace Internal
136
137 } // namespace Dali