d6392fb03d41b7dda305b34df73c0d6676a7a2df
[platform/core/uifw/dali-core.git] / dali / internal / event / rendering / texture-impl.cpp
1 /*
2  * Copyright (c) 2015 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 NewTexturePtr NewTexture::New(TextureType::Type type, Pixel::Format format, unsigned int width, unsigned int height )
31 {
32   NewTexturePtr texture( new NewTexture( type, format, width, height ) );
33   texture->Initialize();
34   return texture;
35 }
36
37 NewTexturePtr NewTexture::New( NativeImageInterface& nativeImageInterface )
38 {
39   NewTexturePtr texture( new NewTexture( &nativeImageInterface ) );
40   texture->Initialize();
41   return texture;
42 }
43
44 Render::NewTexture* NewTexture::GetRenderObject() const
45 {
46   return mRenderObject;
47 }
48
49 NewTexture::NewTexture(TextureType::Type type, Pixel::Format format, unsigned int width, unsigned int height )
50 : mEventThreadServices( *Stage::GetCurrent() ),
51   mRenderObject( NULL ),
52   mNativeImage(),
53   mType( type ),
54   mFormat( format ),
55   mWidth( width ),
56   mHeight( height )
57 {
58 }
59
60 NewTexture::NewTexture( NativeImageInterfacePtr nativeImageInterface )
61 : mEventThreadServices( *Stage::GetCurrent() ),
62   mRenderObject( NULL ),
63   mNativeImage( nativeImageInterface ),
64   mType( TextureType::TEXTURE_2D ),
65   mFormat( Pixel::RGB888 ),
66   mWidth( nativeImageInterface->GetWidth() ),
67   mHeight( nativeImageInterface->GetHeight() )
68 {
69 }
70
71 void NewTexture::Initialize()
72 {
73   if( mNativeImage )
74   {
75     mRenderObject = new Render::NewTexture( mNativeImage );
76   }
77   else
78   {
79     mRenderObject = new Render::NewTexture( mType, mFormat, mWidth, mHeight );
80   }
81
82   AddTexture( mEventThreadServices.GetUpdateManager(), *mRenderObject );
83 }
84
85 NewTexture::~NewTexture()
86 {
87   if( EventThreadServices::IsCoreRunning() && mRenderObject )
88   {
89     RemoveTexture( mEventThreadServices.GetUpdateManager(), *mRenderObject );
90   }
91 }
92
93 bool NewTexture::Upload( PixelDataPtr pixelData )
94 {
95   return Upload( pixelData, 0u, 0u, 0u, 0u, mWidth, mHeight );
96 }
97
98 bool NewTexture::Upload( PixelDataPtr pixelData,
99                          unsigned int layer, unsigned int mipmap,
100                          unsigned int xOffset, unsigned int yOffset,
101                          unsigned int width, unsigned int height )
102 {
103   bool result(false);
104   if( mNativeImage )
105   {
106     DALI_LOG_ERROR( "OpenGLES does not support uploading data to native texture");
107   }
108   else
109   {
110     unsigned int pixelDataSize = pixelData->GetWidth()*pixelData->GetHeight();
111     if( pixelData->GetBuffer() == NULL || pixelDataSize == 0 )
112     {
113       DALI_LOG_ERROR( "PixelData is empty");
114     }
115     else
116     {
117       Pixel::Format pixelDataFormat = pixelData->GetPixelFormat();
118       if( ( pixelDataFormat == mFormat ) || ( (pixelDataFormat == Pixel::RGB888 ) && ( mFormat == Pixel::RGBA8888 ) ) )
119       {
120         if( pixelDataSize < width * height )
121         {
122           DALI_LOG_ERROR( "Pixel data of an incorrect size when trying to update texture");
123         }
124         else if( ( xOffset + width  > ( mWidth  / (1<<mipmap) ) ) ||
125                  ( yOffset + height > ( mHeight / (1<<mipmap) ) ) )
126         {
127           DALI_LOG_ERROR( "Texture update area out of bounds");
128         }
129         else
130         {
131           //Parameters are correct. Send message to upload data to the texture
132           UploadParams params = { layer, mipmap, xOffset, yOffset, width, height };
133           UploadTextureMessage(mEventThreadServices.GetUpdateManager(), *mRenderObject, pixelData, params );
134           result = true;
135         }
136       }
137       else
138       {
139         DALI_LOG_ERROR( "Bad format");
140       }
141     }
142   }
143
144   return result;
145 }
146
147 void NewTexture::GenerateMipmaps()
148 {
149   GenerateMipmapsMessage(mEventThreadServices.GetUpdateManager(), *mRenderObject );
150 }
151
152 unsigned int NewTexture::GetWidth() const
153 {
154   return mWidth;
155 }
156
157 unsigned int NewTexture::GetHeight() const
158 {
159   return mHeight;
160 }
161
162 } // namespace Internal
163 } // namespace Dali