Merge branch 'tizen' into new_text
[platform/core/uifw/dali-adaptor.git] / adaptors / wayland / pixmap-image-impl-wl.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 "pixmap-image-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23 #include <render-surface.h>
24
25 // INTERNAL INCLUDES
26 #include <gl/egl-image-extensions.h>
27 #include <gl/egl-factory.h>
28 #include <adaptor-impl.h>
29
30 // Allow this to be encoded and saved:
31 #include <platform-abstractions/slp/resource-loader/resource-loader.h>
32 #include <bitmap-saver.h>
33
34 namespace Dali
35 {
36
37 namespace Internal
38 {
39
40 namespace Adaptor
41 {
42 using Dali::Integration::PixelBuffer;
43
44 PixmapImage* PixmapImage::New(unsigned int width, unsigned int height, Dali::PixmapImage::ColorDepth depth, Any pixmap )
45 {
46   PixmapImage* image = new PixmapImage( width, height, depth, pixmap );
47   DALI_ASSERT_DEBUG( image && "PixmapImage allocation failed." );
48
49   // 2nd phase construction
50   if(image) //< Defensive in case we ever compile without exceptions.
51   {
52     image->Initialize();
53   }
54
55   return image;
56 }
57
58 PixmapImage::PixmapImage( unsigned int width, unsigned int height, Dali::PixmapImage::ColorDepth depth, Any pixmap )
59 : mWidth( width ),
60   mHeight( height ),
61   mOwnPixmap( true ),
62   mPixelFormat( Pixel::RGB888 ),
63   mColorDepth( depth ),
64   mEglImageKHR( NULL ),
65   mEglImageExtensions( NULL )
66 {
67   DALI_ASSERT_ALWAYS( Adaptor::IsAvailable() );
68   EglFactory& eglFactory = Adaptor::GetImplementation( Adaptor::Get() ).GetEGLFactory();
69   mEglImageExtensions = eglFactory.GetImageExtensions();
70   DALI_ASSERT_DEBUG( mEglImageExtensions );
71 }
72
73 void PixmapImage::Initialize()
74 {
75 }
76
77 PixmapImage::~PixmapImage()
78 {
79 }
80
81 bool PixmapImage::GetPixels(std::vector<unsigned char>& pixbuf, unsigned& width, unsigned& height, Pixel::Format& pixelFormat) const
82 {
83     return false;
84 }
85
86 bool PixmapImage::EncodeToFile(const std::string& filename) const
87 {
88   std::vector< unsigned char > pixbuf;
89   unsigned int width(0), height(0);
90   Pixel::Format pixelFormat;
91
92   if(GetPixels(pixbuf, width, height, pixelFormat))
93   {
94     return Dali::EncodeToFile(&pixbuf[0], filename, pixelFormat, width, height);
95   }
96   return false;
97 }
98
99 bool PixmapImage::GlExtensionCreate()
100 {
101     return false;
102 }
103
104 void PixmapImage::GlExtensionDestroy()
105 {
106   mEglImageExtensions->DestroyImageKHR(mEglImageKHR);
107
108   mEglImageKHR = NULL;
109 }
110
111 unsigned int PixmapImage::TargetTexture()
112 {
113   mEglImageExtensions->TargetTextureKHR(mEglImageKHR);
114
115   return 0;
116 }
117
118 int PixmapImage::GetPixelDepth(Dali::PixmapImage::ColorDepth depth) const
119 {
120   switch (depth)
121   {
122     case Dali::PixmapImage::COLOR_DEPTH_8:
123     {
124       return 8;
125     }
126     case Dali::PixmapImage::COLOR_DEPTH_16:
127     {
128       return 16;
129     }
130     case Dali::PixmapImage::COLOR_DEPTH_24:
131     {
132       return 24;
133     }
134     case Dali::PixmapImage::COLOR_DEPTH_32:
135     {
136       return 32;
137     }
138     default:
139     {
140       DALI_ASSERT_DEBUG(0 && "unknown color enum");
141       return 0;
142     }
143   }
144 }
145
146 void PixmapImage::SetPixelFormat(int depth)
147 {
148   // store the pixel format based on the depth
149   switch (depth)
150   {
151     case 8:
152     {
153       mPixelFormat = Pixel::A8;
154       break;
155     }
156     case 16:
157     {
158       mPixelFormat = Pixel::RGB565;
159       break;
160     }
161     case 32:
162     {
163       mPixelFormat = Pixel::RGBA8888;
164       break;
165     }
166     case 24:
167     default:
168     {
169       mPixelFormat = Pixel::RGB888;
170       break;
171     }
172   }
173 }
174
175 void PixmapImage::GetPixmapDetails()
176 {
177 }
178
179 } // namespace Adaptor
180
181 } // namespace internal
182
183 } // namespace Dali