New Texture API clean-up
[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
38 Render::NewTexture* NewTexture::GetRenderObject() const
39 {
40   return mRenderObject;
41 }
42
43 NewTexture::NewTexture(TextureType::Type type, Pixel::Format format, unsigned int width, unsigned int height )
44 : mEventThreadServices( *Stage::GetCurrent() ),
45   mRenderObject( NULL ),
46   mType( type ),
47   mFormat( format ),
48   mWidth( width ),
49   mHeight( height )
50 {
51 }
52
53 void NewTexture::Initialize()
54 {
55   mRenderObject = new Render::NewTexture( mType, mFormat, mWidth, mHeight );
56   AddTexture( mEventThreadServices.GetUpdateManager(), *mRenderObject );
57 }
58
59 NewTexture::~NewTexture()
60 {
61   if( EventThreadServices::IsCoreRunning() && mRenderObject )
62   {
63     RemoveTexture( mEventThreadServices.GetUpdateManager(), *mRenderObject );
64   }
65 }
66
67 bool NewTexture::CheckUploadParametres( const Vector<unsigned char>& buffer, const UploadParams& parameters ) const
68 {
69   if(  buffer.Size() < GetBytesPerPixel( mFormat ) * parameters.width * parameters.height )
70   {
71     DALI_LOG_ERROR( "Error: Buffer of an incorrect size when trying to update texture");
72     return false;
73   }
74   else if( ( parameters.xOffset + parameters.width  > static_cast<unsigned int>( mWidth/(1<<parameters.mipmap ))) ||
75            ( parameters.yOffset + parameters.height > static_cast<unsigned int>( mHeight/(1<<parameters.mipmap ))))
76   {
77     DALI_LOG_ERROR( "Error: Out of bounds texture update");
78     return false;
79   }
80   else
81   {
82     return true;
83   }
84 }
85
86 void NewTexture::Upload( Vector<unsigned char>& buffer,
87                          unsigned int layer, unsigned int mipmap,
88                          unsigned int xOffset, unsigned int yOffset,
89                          unsigned int width, unsigned int height )
90 {
91   UploadParams params = { layer, mipmap, xOffset, yOffset, width, height };
92   if( CheckUploadParametres( buffer, params ) )
93   {
94     UploadTextureMessage(mEventThreadServices.GetUpdateManager(), *mRenderObject, buffer, params );
95   }
96 }
97
98 void NewTexture::Upload( Vector<unsigned char>& buffer )
99 {
100   UploadParams params = {0u,0u,0u,0u,mWidth,mHeight};
101   if( CheckUploadParametres( buffer, params ) )
102   {
103     UploadTextureMessage(mEventThreadServices.GetUpdateManager(), *mRenderObject, buffer, params );
104   }
105 }
106
107 void NewTexture::GenerateMipmaps()
108 {
109   GenerateMipmapsMessage(mEventThreadServices.GetUpdateManager(), *mRenderObject );
110 }
111
112 unsigned int NewTexture::GetWidth() const
113 {
114   return mWidth;
115 }
116
117 unsigned int NewTexture::GetHeight() const
118 {
119   return mHeight;
120 }
121
122 } // namespace Internal
123 } // namespace Dali