License conversion from Flora to Apache 2.0
[platform/core/uifw/dali-adaptor.git] / adaptors / tizen / internal / 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 "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
27 namespace Dali
28 {
29
30 namespace Internal
31 {
32
33 namespace Adaptor
34 {
35
36 NativeBitmapBuffer::NativeBitmapBuffer( Adaptor* adaptor, unsigned int width, unsigned int height, Pixel::Format pFormat )
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   mGlAbstraction = &(adaptor->GetGlAbstraction());
45 }
46
47 NativeBitmapBuffer::~NativeBitmapBuffer()
48 {
49   delete mBuffer;
50 }
51
52 void NativeBitmapBuffer::PrepareTexture()
53 {
54   DALI_ASSERT_ALWAYS( mBuffer );
55   GLenum pixelFormat = GL_RGBA;
56   GLenum pixelDataType = GL_UNSIGNED_BYTE;
57
58   Integration::ConvertToGlFormat( mPixelFormat, pixelDataType, pixelFormat );
59
60   const unsigned char* buf = mBuffer->Read();
61
62   if( buf && buf != mLastReadBuffer ) // Prevent same buffer being uploaded multiple times
63   {
64     mLastReadBuffer = buf;
65
66     // The active texture has already been set to a sampler and bound.
67     mGlAbstraction->TexImage2D( GL_TEXTURE_2D, 0, pixelFormat, mWidth, mHeight, 0, pixelFormat, pixelDataType, buf );
68   }
69 }
70
71 void NativeBitmapBuffer::Write( const unsigned char *src, size_t size )
72 {
73   mBuffer->Write( src, size ); // Write will cause LocklessBuffer to switch to the other buffer
74 }
75
76 bool NativeBitmapBuffer::GlExtensionCreate()
77 {
78   return true;
79 }
80
81 void NativeBitmapBuffer::GlExtensionDestroy()
82 {
83 }
84
85 unsigned int NativeBitmapBuffer::TargetTexture()
86 {
87   return 0;
88 }
89
90 unsigned int NativeBitmapBuffer::GetWidth() const
91 {
92   return mWidth;
93 }
94
95 unsigned int NativeBitmapBuffer::GetHeight() const
96 {
97   return mHeight;
98 }
99
100 Pixel::Format NativeBitmapBuffer::GetPixelFormat() const
101 {
102   return mPixelFormat;
103 }
104
105 } // namespace Adaptor
106
107 } // namespace Internal
108
109 } // namespace Dali