Dali 3D User Interface Engine
resource-thread-image.cpp
Go to the documentation of this file.
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 "resource-thread-image.h"
20 
21 // EXTERNAL INCLUDES
27 
28 // INTERNAL INCLUDES
29 #include "portable/file-closer.h"
31 #include "network/file-download.h"
32 
33 using namespace Dali::Integration;
34 
35 namespace Dali
36 {
37 
38 namespace TizenPlatform
39 {
40 
41 namespace
42 {
43 
44 // limit maximum image down load size to 50 MB
45 const size_t MAXIMUM_DOWNLOAD_IMAGE_SIZE = 50 * 1024 * 1024 ;
46 }
47 
48 ResourceThreadImage::ResourceThreadImage(ResourceLoader& resourceLoader)
49 : ResourceThreadBase(resourceLoader)
50 {
51 }
52 
54 {
55 }
56 
58 {
59  DALI_LOG_TRACE_METHOD( mLogFilter );
60  DALI_LOG_INFO( mLogFilter, Debug::Verbose, "%s(%s)\n", __FUNCTION__, request.GetPath().c_str() );
61 
62  LoadImageFromLocalFile(request);
63 }
64 
66 {
67  bool succeeded;
68 
69  DALI_LOG_TRACE_METHOD( mLogFilter );
70  DALI_LOG_INFO( mLogFilter, Debug::Verbose, "%s(%s)\n", __FUNCTION__, request.GetPath().c_str() );
71 
72  Dali::Vector<uint8_t> dataBuffer;
73  size_t dataSize;
74  succeeded = DownloadRemoteImageIntoMemory( request, dataBuffer, dataSize );
75  if( succeeded )
76  {
77  DecodeImageFromMemory(static_cast<void*>(&dataBuffer[0]), dataBuffer.Size(), request);
78  }
79 }
80 
82 {
83  DALI_LOG_TRACE_METHOD( mLogFilter );
84  DALI_LOG_INFO(mLogFilter, Debug::Verbose, "%s(%s)\n", __FUNCTION__, request.GetPath().c_str());
85 
86  // Get the blob of binary data that we need to decode:
87  DALI_ASSERT_DEBUG( request.GetResource() );
88 
89  DALI_ASSERT_DEBUG( 0 != dynamic_cast<Dali::RefCountedVector<uint8_t>*>( request.GetResource().Get() ) && "Only blobs of binary data can be decoded." );
90  Dali::RefCountedVector<uint8_t>* const encodedBlob = reinterpret_cast<Dali::RefCountedVector<uint8_t>*>( request.GetResource().Get() );
91 
92  if( 0 != encodedBlob )
93  {
94  const size_t blobSize = encodedBlob->GetVector().Size();
95  uint8_t * const blobBytes = &(encodedBlob->GetVector()[0]);
96  DecodeImageFromMemory(blobBytes, blobSize, request);
97  }
98  else
99  {
100  FailedResource resource(request.GetId(), FailureUnknown);
101  mResourceLoader.AddFailedLoad(resource);
102  }
103 }
104 
106 {
107  bool ok = Network::DownloadRemoteFileIntoMemory( request.GetPath(), dataBuffer, dataSize, MAXIMUM_DOWNLOAD_IMAGE_SIZE );
108  if( !ok )
109  {
110  FailedResource resource(request.GetId(), FailureUnknown);
111  mResourceLoader.AddFailedLoad(resource);
112  }
113  return ok;
114 }
115 
117 {
118  bool fileNotFound = false;
119  BitmapPtr bitmap = 0;
120  bool result = false;
121 
122  Dali::Internal::Platform::FileCloser fileCloser( request.GetPath().c_str(), "rb" );
123  FILE * const fp = fileCloser.GetFile();
124 
125  if( NULL != fp )
126  {
127  result = ImageLoader::ConvertStreamToBitmap( *request.GetType(), request.GetPath(), fp, *this, bitmap );
128  // Last chance to interrupt a cancelled load before it is reported back to clients
129  // which have already stopped tracking it:
130  InterruptionPoint(); // Note: This can throw an exception.
131  if( result && bitmap )
132  {
133  // Construct LoadedResource and ResourcePointer for image data
134  LoadedResource resource( request.GetId(), request.GetType()->id, ResourcePointer( bitmap.Get() ) );
135  // Queue the loaded resource
137  }
138  else
139  {
140  DALI_LOG_WARNING( "Unable to decode %s\n", request.GetPath().c_str() );
141  }
142  }
143  else
144  {
145  DALI_LOG_WARNING( "Failed to open file to load \"%s\"\n", request.GetPath().c_str() );
146  fileNotFound = true;
147  }
148 
149  if ( !bitmap )
150  {
151  if( fileNotFound )
152  {
153  FailedResource resource(request.GetId(), FailureFileNotFound );
154  mResourceLoader.AddFailedLoad(resource);
155  }
156  else
157  {
158  FailedResource resource(request.GetId(), FailureUnknown);
159  mResourceLoader.AddFailedLoad(resource);
160  }
161  }
162 }
163 
164 void ResourceThreadImage::DecodeImageFromMemory(void* blobBytes, size_t blobSize, const Integration::ResourceRequest& request)
165 {
166  BitmapPtr bitmap = 0;
167 
168  DALI_ASSERT_DEBUG( blobSize > 0U );
169  DALI_ASSERT_DEBUG( blobBytes != 0U );
170 
171  if( blobBytes != 0 && blobSize > 0U )
172  {
173  // Open a file handle on the memory buffer:
174  Dali::Internal::Platform::FileCloser fileCloser( blobBytes, blobSize, "rb" );
175  FILE * const fp = fileCloser.GetFile();
176  if ( NULL != fp )
177  {
178  bool result = ImageLoader::ConvertStreamToBitmap( *request.GetType(), request.GetPath(), fp, StubbedResourceLoadingClient(), bitmap );
179  if ( result && bitmap )
180  {
181  // Construct LoadedResource and ResourcePointer for image data
182  LoadedResource resource( request.GetId(), request.GetType()->id, ResourcePointer( bitmap.Get() ) );
183  // Queue the loaded resource
185  }
186  else
187  {
188  DALI_LOG_WARNING( "Unable to decode bitmap supplied as in-memory blob.\n" );
189  }
190  }
191  }
192 
193  if (!bitmap)
194  {
195  FailedResource resource(request.GetId(), FailureUnknown);
196  mResourceLoader.AddFailedLoad(resource);
197  }
198 }
199 
200 } // namespace TizenPlatform
201 
202 } // namespace Dali
Dali Docs Home
Read more about Dali