Use max texture size when loading images
[platform/core/uifw/dali-adaptor.git] / adaptors / common / pixel-buffer-impl.cpp
1 /*
2  * Copyright (c) 2017 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 "pixel-buffer-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <stdlib.h>
23 #include <cstring>
24
25 // INTERNAL INCLUDES
26 #include "pixel-manipulation.h"
27 #include "alpha-mask.h"
28
29 namespace Dali
30 {
31
32 namespace Internal
33 {
34
35 namespace Adaptor
36 {
37
38 PixelBuffer::PixelBuffer( unsigned char* buffer,
39                           unsigned int bufferSize,
40                           unsigned int width,
41                           unsigned int height,
42                           Dali::Pixel::Format pixelFormat )
43 : mBuffer( buffer ),
44   mBufferSize( bufferSize ),
45   mWidth( width ),
46   mHeight( height ),
47   mPixelFormat( pixelFormat )
48 {
49 }
50
51 PixelBuffer::~PixelBuffer()
52 {
53   ReleaseBuffer();
54 }
55
56 PixelBufferPtr PixelBuffer::New( unsigned int width,
57                                  unsigned int height,
58                                  Dali::Pixel::Format pixelFormat )
59 {
60   unsigned int bufferSize = width * height * Dali::Pixel::GetBytesPerPixel( pixelFormat );
61   unsigned char* buffer = NULL;
62   if( bufferSize > 0 )
63   {
64     buffer = static_cast<unsigned char*>( malloc ( bufferSize ) );
65   }
66   return new PixelBuffer( buffer, bufferSize, width, height, pixelFormat );
67 }
68
69 PixelBufferPtr PixelBuffer::New( unsigned char* buffer,
70                                  unsigned int bufferSize,
71                                  unsigned int width,
72                                  unsigned int height,
73                                  Dali::Pixel::Format pixelFormat )
74 {
75   return new PixelBuffer( buffer, bufferSize, width, height, pixelFormat );
76 }
77
78 Dali::PixelData PixelBuffer::Convert( PixelBuffer& pixelBuffer )
79 {
80   Dali::PixelData pixelData = Dali::PixelData::New( pixelBuffer.mBuffer,
81                                                     pixelBuffer.mBufferSize,
82                                                     pixelBuffer.mWidth,
83                                                     pixelBuffer.mHeight,
84                                                     pixelBuffer.mPixelFormat,
85                                                     Dali::PixelData::FREE );
86   pixelBuffer.mBuffer = NULL;
87   pixelBuffer.mWidth = 0;
88   pixelBuffer.mHeight = 0;
89   pixelBuffer.mBufferSize = 0;
90
91   return pixelData;
92 }
93
94 unsigned int PixelBuffer::GetWidth() const
95 {
96   return mWidth;
97 }
98
99 unsigned int PixelBuffer::GetHeight() const
100 {
101   return mHeight;
102 }
103
104 Dali::Pixel::Format PixelBuffer::GetPixelFormat() const
105 {
106   return mPixelFormat;
107 }
108
109 unsigned char* PixelBuffer::GetBuffer() const
110 {
111   return mBuffer;
112 }
113
114 unsigned int PixelBuffer::GetBufferSize() const
115 {
116   return mBufferSize;
117 }
118
119 Dali::PixelData PixelBuffer::CreatePixelData() const
120 {
121   unsigned char* destBuffer = NULL;
122
123   if( mBufferSize > 0 )
124   {
125     destBuffer = static_cast<unsigned char*>( malloc( mBufferSize ) );
126     memcpy( destBuffer, mBuffer, mBufferSize );
127   }
128
129   Dali::PixelData pixelData = Dali::PixelData::New( destBuffer, mBufferSize,
130                                                     mWidth, mHeight,
131                                                     mPixelFormat,
132                                                     Dali::PixelData::FREE );
133   return pixelData;
134 }
135
136 void PixelBuffer::ApplyMask( const PixelBuffer& mask )
137 {
138   int byteOffset=0;
139   int bitMask=0;
140
141   Dali::Pixel::GetAlphaOffsetAndMask(mPixelFormat, byteOffset, bitMask);
142
143   if( Dali::Pixel::HasAlpha( mPixelFormat ) && bitMask == 255 )
144   {
145     ApplyMaskToAlphaChannel( *this, mask );
146   }
147   else
148   {
149     PixelBufferPtr newPixelBuffer = CreateNewMaskedBuffer( *this, mask );
150     ReleaseBuffer();
151
152     // Take ownership of new buffer
153     mBuffer = newPixelBuffer->mBuffer;
154     newPixelBuffer->mBuffer = NULL;
155     mPixelFormat = newPixelBuffer->mPixelFormat;
156     mBufferSize = newPixelBuffer->mBufferSize;
157
158     // On leaving scope, newPixelBuffer will get destroyed.
159   }
160 }
161
162 void PixelBuffer::ReleaseBuffer()
163 {
164   if( mBuffer )
165   {
166     free( mBuffer );
167   }
168 }
169
170
171 }// namespace Adaptor
172 }// namespace Internal
173 }// namespace Dali