Merge "Reduce Render::Texture size by 30% and texture upload message size by 50%...
[platform/core/uifw/dali-core.git] / dali / internal / event / rendering / texture-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 <dali/internal/event/rendering/texture-impl.h> // Dali::Internal::Texture
20
21 // INTERNAL INCLUDES
22 #include <dali/internal/update/manager/update-manager.h>
23 #include <dali/internal/event/common/stage-impl.h>
24
25 namespace Dali
26 {
27 namespace Internal
28 {
29
30 TexturePtr Texture::New(TextureType::Type type, Pixel::Format format, unsigned int width, unsigned int height )
31 {
32   constexpr auto max_value = std::numeric_limits< uint16_t >::max();
33   DALI_ASSERT_ALWAYS( ( width < max_value )&&( height < max_value )&& "Size out of range" );
34   TexturePtr texture( new Texture( type, format, ImageDimensions( width, height) ) );
35   texture->Initialize();
36   return texture;
37 }
38
39 TexturePtr Texture::New( NativeImageInterface& nativeImageInterface )
40 {
41   TexturePtr texture( new Texture( &nativeImageInterface ) );
42   texture->Initialize();
43   return texture;
44 }
45
46 Render::Texture* Texture::GetRenderObject() const
47 {
48   return mRenderObject;
49 }
50
51 Texture::Texture(TextureType::Type type, Pixel::Format format, ImageDimensions size )
52 : mEventThreadServices( *Stage::GetCurrent() ),
53   mRenderObject( NULL ),
54   mNativeImage(),
55   mSize( size ),
56   mType( type ),
57   mFormat( format )
58 {
59 }
60
61 Texture::Texture( NativeImageInterfacePtr nativeImageInterface )
62 : mEventThreadServices( *Stage::GetCurrent() ),
63   mRenderObject( NULL ),
64   mNativeImage( nativeImageInterface ),
65   mSize( nativeImageInterface->GetWidth(), nativeImageInterface->GetHeight() ),
66   mType( TextureType::TEXTURE_2D ),
67   mFormat( Pixel::RGB888 )
68 {
69 }
70
71 void Texture::Initialize()
72 {
73   if( EventThreadServices::IsCoreRunning() )
74   {
75     if( mNativeImage )
76     {
77       mRenderObject = new Render::Texture( mNativeImage );
78     }
79     else
80     {
81       mRenderObject = new Render::Texture( mType, mFormat, mSize );
82     }
83
84     OwnerPointer< Render::Texture > transferOwnership( mRenderObject );
85     AddTexture( mEventThreadServices.GetUpdateManager(), transferOwnership );
86   }
87 }
88
89 Texture::~Texture()
90 {
91   if( EventThreadServices::IsCoreRunning() && mRenderObject )
92   {
93     RemoveTexture( mEventThreadServices.GetUpdateManager(), *mRenderObject );
94   }
95 }
96
97 bool Texture::Upload( PixelDataPtr pixelData )
98 {
99   return Upload( pixelData, 0u, 0u, 0u, 0u, pixelData->GetWidth(), pixelData->GetHeight() );
100 }
101
102 bool Texture::Upload( PixelDataPtr pixelData,
103                       unsigned int layer, unsigned int mipmap,
104                       unsigned int xOffset, unsigned int yOffset,
105                       unsigned int width, unsigned int height )
106 {
107   constexpr auto max_value = std::numeric_limits< uint16_t >::max();
108   DALI_ASSERT_ALWAYS( layer < max_value &&
109                       mipmap < max_value &&
110                       xOffset < max_value &&
111                       yOffset < max_value &&
112                       width < max_value &&
113                       height < max_value &&
114                       "Parameter value out of range" );
115
116   bool result(false);
117   if( EventThreadServices::IsCoreRunning() && mRenderObject )
118   {
119     if( mNativeImage )
120     {
121       DALI_LOG_ERROR( "OpenGL ES does not support uploading data to native texture\n");
122     }
123     else
124     {
125       unsigned int pixelDataSize = pixelData->GetWidth()*pixelData->GetHeight();
126       if( pixelData->GetBuffer() == NULL || pixelDataSize == 0 )
127       {
128         DALI_LOG_ERROR( "PixelData is empty\n");
129       }
130       else
131       {
132         Pixel::Format pixelDataFormat = pixelData->GetPixelFormat();
133         if( ( pixelDataFormat == mFormat ) || ( (pixelDataFormat == Pixel::RGB888 ) && ( mFormat == Pixel::RGBA8888 ) ) )
134         {
135           if( pixelDataSize < width * height )
136           {
137             DALI_LOG_ERROR( "PixelData of an incorrect size when trying to update texture\n");
138           }
139           else if( ( xOffset + width  > ( mSize.GetWidth()  / (1u << mipmap) ) ) ||
140               ( yOffset + height > ( mSize.GetHeight() / (1u << mipmap) ) ) )
141           {
142             DALI_LOG_ERROR( "Texture update area out of bounds\n");
143           }
144           else
145           {
146             //Parameters are correct. Send message to upload data to the texture
147             UploadParams params = { static_cast< uint16_t >( layer ),
148                                     static_cast< uint16_t >( mipmap ),
149                                     static_cast< uint16_t >( xOffset ),
150                                     static_cast< uint16_t >( yOffset ),
151                                     static_cast< uint16_t >( width ),
152                                     static_cast< uint16_t >( height ) };
153             UploadTextureMessage( mEventThreadServices.GetUpdateManager(), *mRenderObject, pixelData, params );
154             result = true;
155           }
156         }
157         else
158         {
159           DALI_LOG_ERROR( "Bad format\n");
160         }
161       }
162     }
163   }
164
165   return result;
166 }
167
168 void Texture::GenerateMipmaps()
169 {
170   if( EventThreadServices::IsCoreRunning() && mRenderObject )
171   {
172     GenerateMipmapsMessage(mEventThreadServices.GetUpdateManager(), *mRenderObject );
173   }
174 }
175
176 unsigned int Texture::GetWidth() const
177 {
178   return mSize.GetWidth();
179 }
180
181 unsigned int Texture::GetHeight() const
182 {
183   return mSize.GetHeight();
184 }
185
186 } // namespace Internal
187 } // namespace Dali