OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * The overlay displaying the image. | 6 * The overlay displaying the image. |
7 */ | 7 */ |
8 function ImageView(container, viewport) { | 8 function ImageView(container, viewport) { |
9 this.container_ = container; | 9 this.container_ = container; |
10 this.viewport_ = viewport; | 10 this.viewport_ = viewport; |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 | 173 |
174 var self = this; | 174 var self = this; |
175 | 175 |
176 this.contentID_ = id; | 176 this.contentID_ = id; |
177 | 177 |
178 if (FileType.getMediaType(source) == 'video') { | 178 if (FileType.getMediaType(source) == 'video') { |
179 var video = this.document_.createElement('video'); | 179 var video = this.document_.createElement('video'); |
180 if (metadata.thumbnailURL) { | 180 if (metadata.thumbnailURL) { |
181 video.setAttribute('poster', metadata.thumbnailURL); | 181 video.setAttribute('poster', metadata.thumbnailURL); |
182 } | 182 } |
183 if (!metadata.thumbnailOnly) { | 183 video.src = metadata.contentURL || source; |
184 video.src = source; | 184 video.load(); |
185 video.load(); | 185 displayMainImage(ImageView.LOAD_TYPE_TOTAL, slide, video); |
186 } | |
187 displayMainImage(ImageView.LOAD_TYPE_TOTAL, video); | |
188 return; | 186 return; |
189 } | 187 } |
190 var readyContent = this.getReadyContent(id, source); | 188 var readyContent = this.getReadyContent(id, source); |
191 if (readyContent) { | 189 if (readyContent) { |
192 displayMainImage(ImageView.LOAD_TYPE_CACHED_FULL, readyContent); | 190 displayMainImage(ImageView.LOAD_TYPE_CACHED_FULL, slide, readyContent); |
193 } else { | 191 } else { |
194 var cachedScreen = this.screenCache_.getItem(id); | 192 var cachedScreen = this.screenCache_.getItem(id); |
195 if (cachedScreen) { | 193 if (cachedScreen) { |
196 // We have a cached screen-scale canvas, use it instead of a thumbnail. | 194 // We have a cached screen-scale canvas, use it instead of a thumbnail. |
197 displayThumbnail(ImageView.LOAD_TYPE_CACHED_SCREEN, cachedScreen); | 195 displayThumbnail(ImageView.LOAD_TYPE_CACHED_SCREEN, slide, cachedScreen); |
198 // As far as the user can tell the image is loaded. We still need to load | 196 // As far as the user can tell the image is loaded. We still need to load |
199 // the full res image to make editing possible, but we can report now. | 197 // the full res image to make editing possible, but we can report now. |
200 ImageUtil.metrics.recordInterval(ImageUtil.getMetricName('DisplayTime')); | 198 ImageUtil.metrics.recordInterval(ImageUtil.getMetricName('DisplayTime')); |
201 } else if (metadata.thumbnailURL) { | 199 } else if (metadata.thumbnailURL) { |
202 this.imageLoader_.load( | 200 this.imageLoader_.load( |
203 metadata.thumbnailURL, | 201 metadata.thumbnailURL, |
204 metadata.thumbnailTransform, | 202 metadata.thumbnailTransform, |
205 displayThumbnail.bind(null, ImageView.LOAD_TYPE_FILE)); | 203 displayThumbnail.bind(null, ImageView.LOAD_TYPE_FILE, slide)); |
206 } else { | 204 } else { |
207 loadMainImage(ImageView.LOAD_TYPE_FILE, 0); | 205 loadMainImage(ImageView.LOAD_TYPE_FILE, slide, source, 0); |
208 } | 206 } |
209 } | 207 } |
210 | 208 |
211 function displayThumbnail(loadType, canvas) { | 209 function displayThumbnail(loadType, slide, canvas) { |
212 // The thumbnail may have different aspect ratio than the main image. | 210 // The thumbnail may have different aspect ratio than the main image. |
213 // Force the main image proportions to avoid flicker. | 211 // Force the main image proportions to avoid flicker. |
214 var time = Date.now(); | 212 var time = Date.now(); |
215 | 213 |
216 var mainImageLoadDelay = ImageView.ANIMATION_WAIT_INTERVAL; | 214 var mainImageLoadDelay = ImageView.ANIMATION_WAIT_INTERVAL; |
| 215 var mainImageSlide = slide; |
217 | 216 |
218 // Do not do slide-in animation when scrolling very fast. | 217 // Do not do slide-in animation when scrolling very fast. |
219 if (self.lastLoadTime_ && | 218 if (self.lastLoadTime_ && |
220 (time - self.lastLoadTime_) < ImageView.FAST_SCROLL_INTERVAL) { | 219 (time - self.lastLoadTime_) < ImageView.FAST_SCROLL_INTERVAL) { |
221 slide = 0; | 220 mainImageSlide = 0; |
222 } | 221 } |
223 self.lastLoadTime_ = time; | 222 self.lastLoadTime_ = time; |
224 | 223 |
225 if (metadata.thumbnailOnly) { | 224 var contentURL; |
| 225 if (metadata.contentURL) { |
| 226 contentURL = metadata.contentURL; |
226 // We do not know the main image size, but chances are that it is large | 227 // We do not know the main image size, but chances are that it is large |
227 // enough. Show the thumbnail at the maximum possible scale. | 228 // enough. Show the thumbnail at the maximum possible scale. |
228 var bounds = self.viewport_.getScreenBounds(); | 229 var bounds = self.viewport_.getScreenBounds(); |
229 var scale = Math.min (bounds.width / canvas.width, | 230 var scale = Math.min (bounds.width / canvas.width, |
230 bounds.height / canvas.height); | 231 bounds.height / canvas.height); |
231 self.replace(canvas, slide, canvas.width * scale, canvas.height * scale); | 232 self.replace(canvas, slide, canvas.width * scale, canvas.height * scale); |
232 if (opt_callback) opt_callback(ImageView.LOAD_TYPE_TOTAL); | 233 } else { |
233 return; | 234 contentURL = source; |
| 235 self.replace(canvas, slide, metadata.width, metadata.height); |
234 } | 236 } |
235 | 237 if (!mainImageSlide) mainImageLoadDelay = 0; |
236 self.replace(canvas, slide, metadata.width, metadata.height); | 238 mainImageSlide = 0; |
237 if (!slide) mainImageLoadDelay = 0; | 239 loadMainImage(loadType, mainImageSlide, contentURL, mainImageLoadDelay); |
238 slide = 0; | |
239 loadMainImage(loadType, mainImageLoadDelay); | |
240 } | 240 } |
241 | 241 |
242 function loadMainImage(loadType, delay) { | 242 function loadMainImage(loadType, slide, contentURL, delay) { |
243 if (self.prefetchLoader_.isLoading(source)) { | 243 if (self.prefetchLoader_.isLoading(contentURL)) { |
244 // The image we need is already being prefetched. Initiating another load | 244 // The image we need is already being prefetched. Initiating another load |
245 // would be a waste. Hijack the load instead by overriding the callback. | 245 // would be a waste. Hijack the load instead by overriding the callback. |
246 self.prefetchLoader_.setCallback(displayMainImage.bind(null, loadType)); | 246 self.prefetchLoader_.setCallback( |
| 247 displayMainImage.bind(null, loadType, slide)); |
247 | 248 |
248 // Swap the loaders so that the self.isLoading works correctly. | 249 // Swap the loaders so that the self.isLoading works correctly. |
249 var temp = self.prefetchLoader_; | 250 var temp = self.prefetchLoader_; |
250 self.prefetchLoader_ = self.imageLoader_; | 251 self.prefetchLoader_ = self.imageLoader_; |
251 self.imageLoader_ = temp; | 252 self.imageLoader_ = temp; |
252 return; | 253 return; |
253 } | 254 } |
254 self.prefetchLoader_.cancel(); // The prefetch was doing something useless. | 255 self.prefetchLoader_.cancel(); // The prefetch was doing something useless. |
255 | 256 |
256 self.imageLoader_.load( | 257 self.imageLoader_.load( |
257 source, | 258 contentURL, |
258 metadata.imageTransform, | 259 metadata.imageTransform, |
259 displayMainImage.bind(null, loadType), | 260 displayMainImage.bind(null, loadType, slide), |
260 delay); | 261 delay); |
261 } | 262 } |
262 | 263 |
263 function displayMainImage(loadType, content) { | 264 function displayMainImage(loadType, slide, content) { |
264 self.replace(content, slide); | 265 self.replace(content, slide); |
265 ImageUtil.metrics.recordEnum(ImageUtil.getMetricName('LoadMode'), | 266 ImageUtil.metrics.recordEnum(ImageUtil.getMetricName('LoadMode'), |
266 loadType, ImageView.LOAD_TYPE_TOTAL); | 267 loadType, ImageView.LOAD_TYPE_TOTAL); |
267 if (loadType != ImageView.LOAD_TYPE_CACHED_SCREEN) { | 268 if (loadType != ImageView.LOAD_TYPE_CACHED_SCREEN) { |
268 ImageUtil.metrics.recordInterval(ImageUtil.getMetricName('DisplayTime')); | 269 ImageUtil.metrics.recordInterval(ImageUtil.getMetricName('DisplayTime')); |
269 } | 270 } |
270 if (opt_callback) opt_callback(loadType); | 271 if (opt_callback) opt_callback(loadType); |
271 } | 272 } |
272 }; | 273 }; |
273 | 274 |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
522 if (this.order_.length > this.capacity_) | 523 if (this.order_.length > this.capacity_) |
523 throw new Error('Exceeded cache capacity'); | 524 throw new Error('Exceeded cache capacity'); |
524 }; | 525 }; |
525 | 526 |
526 ImageView.Cache.prototype.evictLRU = function() { | 527 ImageView.Cache.prototype.evictLRU = function() { |
527 if (this.order_.length == this.capacity_) { | 528 if (this.order_.length == this.capacity_) { |
528 var id = this.order_.shift(); | 529 var id = this.order_.shift(); |
529 delete this.map_[id]; | 530 delete this.map_[id]; |
530 } | 531 } |
531 }; | 532 }; |
OLD | NEW |