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