| 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 fprintf(stderr, "ImageCache::GetImage(%s)\n", src_url); |
| 22 if (strncmp(src_url, "context2d://", 12) == 0) { |
| 23 int handle = atoi(src_url + 12); |
| 24 CanvasContext* otherContext = Context2D(handle); |
| 25 return otherContext->GetBitmap(); |
| 26 } else if (images.find(src_url) == images.end()) { |
| 27 SkBitmap* bm = Load(src_url); |
| 28 if (bm != NULL) { |
| 29 images[src_url] = bm; |
| 30 } |
| 31 return bm; |
| 32 } else { |
| 33 return images[src_url]; |
| 34 } |
| 35 } |
| 36 |
| 37 int ImageCache::GetWidth_(const char* src_url) { |
| 38 fprintf(stderr, "ImageCache::GetWidth(%s)\n", src_url); |
| 39 const SkBitmap* image = GetImage(src_url); |
| 40 if (image == NULL) return 0; |
| 41 return image->width(); |
| 42 } |
| 43 |
| 44 int ImageCache::GetHeight_(const char* src_url) { |
| 45 fprintf(stderr, "ImageCache::GetHeight(%s)\n", src_url); |
| 46 const SkBitmap* image = GetImage(src_url); |
| 47 if (image == NULL) return 0; |
| 48 return image->height(); |
| 49 } |
| 50 |
| 51 SkBitmap* ImageCache::Load(const char* src_url) { |
| 52 fprintf(stderr, "ImageCache::Load(%s)\n", src_url); |
| 53 SkBitmap *bm = NULL; |
| 54 const char* filepath; |
| 55 if (strncmp(src_url, "file://", 7) == 0) { |
| 56 filepath = src_url + 7; |
| 57 } else { |
| 58 // TODO(gram): We need a way to remap URLs to local file names. |
| 59 // For now I am just using the characters after the last '/'. |
| 60 // Note also that if we want to support URLs and network fetches, |
| 61 // then we introduce more complexity; this can't just be an URL. |
| 62 int pos = strlen(src_url); |
| 63 while (--pos >= 0 && src_url[pos] != '/'); |
| 64 filepath = src_url + pos + 1; |
| 65 } |
| 66 char* path; |
| 67 if (filepath[0] == '/') { |
| 68 path = const_cast<char*>(filepath); |
| 69 } else { |
| 70 size_t len1 = strlen(resource_path_); |
| 71 size_t len2 = strlen(filepath); |
| 72 path = new char[len1 + 1 + len2 + 1]; |
| 73 strncpy(path, resource_path_, len1+1); |
| 74 strncat(path, "/", 1); |
| 75 strncat(path, filepath, len2); |
| 76 } |
| 77 |
| 78 SkFILEStream stream(path); |
| 79 if (stream.isValid()) { |
| 80 // We could use DecodeFile and pass the path, but by creating the |
| 81 // SkStream here we can produce better error log messages. |
| 82 bm = new SkBitmap(); |
| 83 if (!SkImageDecoder::DecodeStream(&stream, bm)) { |
| 84 LOGI("Image decode of %s failed", path); |
| 85 return NULL; |
| 86 } else { |
| 87 LOGI("Decode image %s: width=%d,height=%d", |
| 88 path, bm->width(), bm->height()); |
| 89 } |
| 90 } else { |
| 91 LOGI("Path %s is invalid", path); |
| 92 } |
| 93 |
| 94 if (path != filepath) { |
| 95 delete[] path; |
| 96 } |
| 97 return bm; |
| 98 } |
| 99 |
| OLD | NEW |