Merge remote-tracking branch 'origin/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 Any PixmapImage::GetPixmap() const
82 {
83   DALI_ASSERT_ALWAYS( false && "PixmapImage::GetPixmap() is not supported for Wayland." );
84   return Any();
85 }
86
87 bool PixmapImage::GetPixels(std::vector<unsigned char>& pixbuf, unsigned& width, unsigned& height, Pixel::Format& pixelFormat) const
88 {
89     return false;
90 }
91
92 bool PixmapImage::EncodeToFile(const std::string& filename) const
93 {
94   std::vector< unsigned char > pixbuf;
95   unsigned int width(0), height(0);
96   Pixel::Format pixelFormat;
97
98   if(GetPixels(pixbuf, width, height, pixelFormat))
99   {
100     return Dali::EncodeToFile(&pixbuf[0], filename, pixelFormat, width, height);
101   }
102   return false;
103 }
104
105 bool PixmapImage::GlExtensionCreate()
106 {
107     return false;
108 }
109
110 void PixmapImage::GlExtensionDestroy()
111 {
112   mEglImageExtensions->DestroyImageKHR(mEglImageKHR);
113
114   mEglImageKHR = NULL;
115 }
116
117 unsigned int PixmapImage::TargetTexture()
118 {
119   mEglImageExtensions->TargetTextureKHR(mEglImageKHR);
120
121   return 0;
122 }
123
124 int PixmapImage::GetPixelDepth(Dali::PixmapImage::ColorDepth depth) const
125 {
126   switch (depth)
127   {
128     case Dali::PixmapImage::COLOR_DEPTH_8:
129     {
130       return 8;
131     }
132     case Dali::PixmapImage::COLOR_DEPTH_16:
133     {
134       return 16;
135     }
136     case Dali::PixmapImage::COLOR_DEPTH_24:
137     {
138       return 24;
139     }
140     case Dali::PixmapImage::COLOR_DEPTH_32:
141     {
142       return 32;
143     }
144     default:
145     {
146       DALI_ASSERT_DEBUG(0 && "unknown color enum");
147       return 0;
148     }
149   }
150 }
151
152 void PixmapImage::SetPixelFormat(int depth)
153 {
154   // store the pixel format based on the depth
155   switch (depth)
156   {
157     case 8:
158     {
159       mPixelFormat = Pixel::A8;
160       break;
161     }
162     case 16:
163     {
164       mPixelFormat = Pixel::RGB565;
165       break;
166     }
167     case 32:
168     {
169       mPixelFormat = Pixel::RGBA8888;
170       break;
171     }
172     case 24:
173     default:
174     {
175       mPixelFormat = Pixel::RGB888;
176       break;
177     }
178   }
179 }
180
181 void PixmapImage::GetPixmapDetails()
182 {
183 }
184
185 } // namespace Adaptor
186
187 } // namespace internal
188
189 } // namespace Dali