| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "embedders/openglui/common/canvas_context.h" | 5 #include "embedders/openglui/common/canvas_context.h" |
| 6 | 6 |
| 7 #include <ctype.h> | 7 #include <ctype.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 #include "core/SkStream.h" |
| 10 #include "embedders/openglui/common/support.h" | 10 #include "embedders/openglui/common/support.h" |
| 11 | 11 |
| 12 // TODO(gram): this should be dynamic. | 12 // TODO(gram): this should be dynamic. |
| 13 #define MAX_CONTEXTS 16 | 13 #define MAX_CONTEXTS 16 |
| 14 CanvasContext* contexts[MAX_CONTEXTS] = { 0 }; | 14 CanvasContext* contexts[MAX_CONTEXTS] = { 0 }; |
| 15 | 15 |
| 16 CanvasContext* Context2D(int handle) { | 16 CanvasContext* Context2D(int handle) { |
| 17 if (handle < 0 || handle >= MAX_CONTEXTS) { | 17 if (handle < 0 || handle >= MAX_CONTEXTS) { |
| 18 LOGE("Request for out-of-range handle %d", handle); |
| 18 return NULL; | 19 return NULL; |
| 19 } | 20 } |
| 21 if (contexts[handle] == NULL) { |
| 22 LOGE("Warning: request for context with handle %d returns NULL", handle); |
| 23 } |
| 20 return contexts[handle]; | 24 return contexts[handle]; |
| 21 } | 25 } |
| 22 | 26 |
| 27 void FreeContexts() { |
| 28 extern CanvasContext* display_context; |
| 29 for (int i = 0; i < MAX_CONTEXTS; i++) { |
| 30 delete contexts[i]; |
| 31 contexts[i] = NULL; |
| 32 } |
| 33 display_context = NULL; |
| 34 } |
| 35 |
| 36 |
| 23 CanvasContext::CanvasContext(int handle, int16_t widthp, int16_t heightp) | 37 CanvasContext::CanvasContext(int handle, int16_t widthp, int16_t heightp) |
| 24 : canvas_(NULL), | 38 : canvas_(NULL), |
| 25 width_(widthp), | 39 width_(widthp), |
| 26 height_(heightp), | 40 height_(heightp), |
| 27 imageSmoothingEnabled_(true), | 41 imageSmoothingEnabled_(true), |
| 28 state_(NULL) { | 42 state_(NULL) { |
| 29 if (handle == 0) { | 43 if (handle == 0) { |
| 30 canvas_ = graphics->CreateDisplayCanvas(); | 44 canvas_ = graphics->CreateDisplayCanvas(); |
| 31 } else { | 45 } else { |
| 32 canvas_ = graphics->CreateBitmapCanvas(widthp, heightp); | 46 canvas_ = graphics->CreateBitmapCanvas(widthp, heightp); |
| 33 } | 47 } |
| 34 state_ = new CanvasState(canvas_); | 48 state_ = new CanvasState(canvas_); |
| 35 contexts[handle] = this; | 49 contexts[handle] = this; |
| 50 LOGI("Created context with handle %d", handle); |
| 36 } | 51 } |
| 37 | 52 |
| 38 CanvasContext::~CanvasContext() { | 53 CanvasContext::~CanvasContext() { |
| 39 delete state_; | 54 delete state_; |
| 40 delete canvas_; | 55 delete canvas_; |
| 41 } | 56 } |
| 42 | 57 |
| 43 void CanvasContext::DrawImage(const char* src_url, | 58 void CanvasContext::DrawImage(const char* src_url, |
| 44 int sx, int sy, | 59 int sx, int sy, |
| 45 bool has_src_dimensions, int sw, int sh, | 60 bool has_src_dimensions, int sw, int sh, |
| 46 int dx, int dy, | 61 int dx, int dy, |
| 47 bool has_dst_dimensions, int dw, int dh) { | 62 bool has_dst_dimensions, int dw, int dh) { |
| 48 SkBitmap bm; | 63 SkBitmap bm; |
| 49 if (strncmp(src_url, "context2d://", 12) == 0) { | 64 if (strncmp(src_url, "context2d://", 12) == 0) { |
| 50 int handle = atoi(src_url + 12); | 65 int handle = atoi(src_url + 12); |
| 51 CanvasContext* otherContext = Context2D(handle); | 66 CanvasContext* otherContext = Context2D(handle); |
| 52 SkDevice* device = otherContext->canvas_->getDevice(); | 67 SkDevice* device = otherContext->canvas_->getDevice(); |
| 53 bm = device->accessBitmap(false); | 68 bm = device->accessBitmap(false); |
| 54 } else { | 69 } else { |
| 55 // TODO(gram): We need a way to remap URLs to local file names. | 70 const char* filepath; |
| 56 // For now I am just using the characters after the last '/'. | 71 if (strncmp(src_url, "file://", 7) == 0) { |
| 57 // Note also that if we want to support URLs and network fetches, | 72 filepath = src_url + 7; |
| 58 // then we introduce more complexity; this can't just be an URL. | |
| 59 int pos = strlen(src_url); | |
| 60 while (--pos >= 0 && src_url[pos] != '/'); | |
| 61 const char *path = src_url + pos + 1; | |
| 62 if (!SkImageDecoder::DecodeFile(path, &bm)) { | |
| 63 LOGI("Image decode of %s failed", path); | |
| 64 return; | |
| 65 } else { | 73 } else { |
| 66 LOGI("Decode image: width=%d,height=%d", bm.width(), bm.height()); | 74 // TODO(gram): We need a way to remap URLs to local file names. |
| 75 // For now I am just using the characters after the last '/'. |
| 76 // Note also that if we want to support URLs and network fetches, |
| 77 // then we introduce more complexity; this can't just be an URL. |
| 78 int pos = strlen(src_url); |
| 79 while (--pos >= 0 && src_url[pos] != '/'); |
| 80 filepath = src_url + pos + 1; |
| 81 } |
| 82 char* path; |
| 83 if (filepath[0] == '/') { |
| 84 path = const_cast<char*>(filepath); |
| 85 } else { |
| 86 size_t len1 = strlen(graphics->resource_path()); |
| 87 size_t len2 = strlen(filepath); |
| 88 path = new char[len1 + 1 + len2 + 1]; |
| 89 strncpy(path, graphics->resource_path(), len1+1); |
| 90 strncat(path, "/", 1); |
| 91 strncat(path, filepath, len2); |
| 92 } |
| 93 SkFILEStream stream(path); |
| 94 if (stream.isValid()) { |
| 95 // We could use DecodeFile and pass the path, but by creating the |
| 96 // SkStream here we can produce better error log messages. |
| 97 if (!SkImageDecoder::DecodeStream(&stream, &bm)) { |
| 98 LOGI("Image decode of %s failed", path); |
| 99 return; |
| 100 } else { |
| 101 LOGI("Decode image %s: width=%d,height=%d", |
| 102 path, bm.width(), bm.height()); |
| 103 } |
| 104 } else { |
| 105 LOGI("Path %s is invalid", path); |
| 106 } |
| 107 if (path != filepath) { |
| 108 delete[] path; |
| 67 } | 109 } |
| 68 } | 110 } |
| 69 if (!has_src_dimensions) { | 111 if (!has_src_dimensions) { |
| 70 sw = bm.width(); | 112 sw = bm.width(); |
| 71 sh = bm.height(); | 113 sh = bm.height(); |
| 72 } | 114 } |
| 73 if (!has_dst_dimensions) { | 115 if (!has_dst_dimensions) { |
| 74 dw = bm.width(); | 116 dw = bm.width(); |
| 75 dh = bm.height(); | 117 dh = bm.height(); |
| 76 } | 118 } |
| 77 state_->DrawImage(bm, sx, sy, sw, sh, dx, dy, dw, dh); | 119 state_->DrawImage(bm, sx, sy, sw, sh, dx, dy, dw, dh); |
| 120 isDirty_ = true; |
| 78 } | 121 } |
| 79 | 122 |
| 80 void CanvasContext::ClearRect(float left, float top, | 123 void CanvasContext::ClearRect(float left, float top, |
| 81 float width, float height) { | 124 float width, float height) { |
| 82 SkPaint paint; | 125 SkPaint paint; |
| 83 paint.setStyle(SkPaint::kFill_Style); | 126 paint.setStyle(SkPaint::kFill_Style); |
| 84 paint.setColor(0xFFFFFFFF); | 127 paint.setColor(0xFFFFFFFF); |
| 85 canvas_->drawRectCoords(left, top, left + width, top + height, paint); | 128 canvas_->drawRectCoords(left, top, left + width, top + height, paint); |
| 129 isDirty_ = true; |
| 86 } | 130 } |
| 87 | 131 |
| OLD | NEW |