| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 #include "embedders/openglui/common/image_cache.h" | |
| 6 | |
| 7 #include <ctype.h> | |
| 8 #include <string.h> | |
| 9 #include "core/SkStream.h" | |
| 10 #include "embedders/openglui/common/canvas_context.h" | |
| 11 | |
| 12 ImageCache* ImageCache::instance_ = NULL; | |
| 13 | |
| 14 extern CanvasContext* Context2D(int handle); | |
| 15 | |
| 16 ImageCache::ImageCache(const char* resource_path) | |
| 17 : images(), resource_path_(resource_path) { | |
| 18 } | |
| 19 | |
| 20 const SkBitmap* ImageCache::GetImage_(const char* src_url) { | |
| 21 if (strncmp(src_url, "context2d://", 12) == 0) { | |
| 22 int handle = atoi(src_url + 12); | |
| 23 CanvasContext* otherContext = Context2D(handle); | |
| 24 return otherContext->GetBitmap(); | |
| 25 } else if (images.find(src_url) == images.end()) { | |
| 26 SkBitmap* bm = Load(src_url); | |
| 27 if (bm != NULL) { | |
| 28 images[src_url] = bm; | |
| 29 } | |
| 30 return bm; | |
| 31 } else { | |
| 32 return images[src_url]; | |
| 33 } | |
| 34 } | |
| 35 | |
| 36 int ImageCache::GetWidth_(const char* src_url) { | |
| 37 const SkBitmap* image = GetImage(src_url); | |
| 38 if (image == NULL) return 0; | |
| 39 return image->width(); | |
| 40 } | |
| 41 | |
| 42 int ImageCache::GetHeight_(const char* src_url) { | |
| 43 const SkBitmap* image = GetImage(src_url); | |
| 44 if (image == NULL) return 0; | |
| 45 return image->height(); | |
| 46 } | |
| 47 | |
| 48 SkBitmap* ImageCache::Load(const char* src_url) { | |
| 49 SkBitmap *bm = NULL; | |
| 50 const char* filepath; | |
| 51 if (strncmp(src_url, "file://", 7) == 0) { | |
| 52 filepath = src_url + 7; | |
| 53 } else { | |
| 54 // TODO(gram): We need a way to remap URLs to local file names. | |
| 55 // For now I am just using the characters after the last '/'. | |
| 56 // Note also that if we want to support URLs and network fetches, | |
| 57 // then we introduce more complexity; this can't just be an URL. | |
| 58 int pos = strlen(src_url); | |
| 59 while (--pos >= 0 && src_url[pos] != '/'); | |
| 60 filepath = src_url + pos + 1; | |
| 61 } | |
| 62 char* path; | |
| 63 if (filepath[0] == '/') { | |
| 64 path = const_cast<char*>(filepath); | |
| 65 } else { | |
| 66 size_t len1 = strlen(resource_path_); | |
| 67 size_t len2 = strlen(filepath); | |
| 68 path = new char[len1 + 1 + len2 + 1]; | |
| 69 strncpy(path, resource_path_, len1+1); | |
| 70 strncat(path, "/", 1); | |
| 71 strncat(path, filepath, len2); | |
| 72 } | |
| 73 | |
| 74 SkFILEStream stream(path); | |
| 75 if (stream.isValid()) { | |
| 76 // We could use DecodeFile and pass the path, but by creating the | |
| 77 // SkStream here we can produce better error log messages. | |
| 78 bm = new SkBitmap(); | |
| 79 if (!SkImageDecoder::DecodeStream(&stream, bm)) { | |
| 80 LOGI("Image decode of %s failed", path); | |
| 81 return NULL; | |
| 82 } else { | |
| 83 LOGI("Decode image %s: width=%d,height=%d", | |
| 84 path, bm->width(), bm->height()); | |
| 85 } | |
| 86 } else { | |
| 87 LOGI("Path %s is invalid", path); | |
| 88 } | |
| 89 | |
| 90 if (path != filepath) { | |
| 91 delete[] path; | |
| 92 } | |
| 93 return bm; | |
| 94 } | |
| 95 | |
| OLD | NEW |