| 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 |
| 10 #include "embedders/openglui/common/support.h" | 10 #include "embedders/openglui/common/support.h" |
| 11 | 11 |
| 12 CanvasContext::CanvasContext(int16_t widthp, int16_t heightp) | 12 // TODO(gram): this should be dynamic. |
| 13 #define MAX_CONTEXTS 16 |
| 14 CanvasContext* contexts[MAX_CONTEXTS] = { 0 }; |
| 15 |
| 16 CanvasContext* Context2D(int handle) { |
| 17 if (handle < 0 || handle >= MAX_CONTEXTS) { |
| 18 return NULL; |
| 19 } |
| 20 return contexts[handle]; |
| 21 } |
| 22 |
| 23 CanvasContext::CanvasContext(int handle, int16_t widthp, int16_t heightp) |
| 13 : canvas_(NULL), | 24 : canvas_(NULL), |
| 14 width_(widthp), | 25 width_(widthp), |
| 15 height_(heightp), | 26 height_(heightp), |
| 16 imageSmoothingEnabled_(true), | 27 imageSmoothingEnabled_(true), |
| 17 state_(NULL) { | 28 state_(NULL) { |
| 29 if (handle == 0) { |
| 30 canvas_ = graphics->CreateDisplayCanvas(); |
| 31 } else { |
| 32 canvas_ = graphics->CreateBitmapCanvas(widthp, heightp); |
| 33 } |
| 34 state_ = new CanvasState(canvas_); |
| 35 contexts[handle] = this; |
| 18 } | 36 } |
| 19 | 37 |
| 20 CanvasContext::~CanvasContext() { | 38 CanvasContext::~CanvasContext() { |
| 21 delete state_; | 39 delete state_; |
| 22 delete canvas_; | 40 delete canvas_; |
| 23 } | 41 } |
| 24 | 42 |
| 25 void CanvasContext::Create() { | |
| 26 canvas_ = graphics->CreateCanvas(); | |
| 27 state_ = new CanvasState(canvas_); | |
| 28 } | |
| 29 | |
| 30 void CanvasContext::DrawImage(const char* src_url, | 43 void CanvasContext::DrawImage(const char* src_url, |
| 31 int sx, int sy, | 44 int sx, int sy, |
| 32 bool has_src_dimensions, int sw, int sh, | 45 bool has_src_dimensions, int sw, int sh, |
| 33 int dx, int dy, | 46 int dx, int dy, |
| 34 bool has_dst_dimensions, int dw, int dh) { | 47 bool has_dst_dimensions, int dw, int dh) { |
| 35 SkBitmap bm; | 48 SkBitmap bm; |
| 36 // TODO(gram): We need a way to remap URLs to local file names. | 49 if (strncmp(src_url, "context2d://", 12) == 0) { |
| 37 // For now I am just using the characters after the last '/'. | 50 int handle = atoi(src_url + 12); |
| 38 // Note also that if we want to support URLs and network fetches, | 51 CanvasContext* otherContext = Context2D(handle); |
| 39 // then we introduce more complexity; this can't just be an URL. | 52 SkDevice* device = otherContext->canvas_->getDevice(); |
| 40 int pos = strlen(src_url); | 53 bm = device->accessBitmap(false); |
| 41 while (--pos >= 0 && src_url[pos] != '/'); | |
| 42 const char *path = src_url + pos + 1; | |
| 43 if (!SkImageDecoder::DecodeFile(path, &bm)) { | |
| 44 LOGI("Image decode of %s failed", path); | |
| 45 } else { | 54 } else { |
| 46 LOGI("Decode image: width=%d,height=%d", bm.width(), bm.height()); | 55 // TODO(gram): We need a way to remap URLs to local file names. |
| 47 if (!has_src_dimensions) { | 56 // For now I am just using the characters after the last '/'. |
| 48 sw = bm.width(); | 57 // Note also that if we want to support URLs and network fetches, |
| 49 sh = bm.height(); | 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 { |
| 66 LOGI("Decode image: width=%d,height=%d", bm.width(), bm.height()); |
| 50 } | 67 } |
| 51 if (!has_dst_dimensions) { | |
| 52 dw = bm.width(); | |
| 53 dh = bm.height(); | |
| 54 } | |
| 55 state_->DrawImage(bm, sx, sy, sw, sh, dx, dy, dw, dh); | |
| 56 } | 68 } |
| 69 if (!has_src_dimensions) { |
| 70 sw = bm.width(); |
| 71 sh = bm.height(); |
| 72 } |
| 73 if (!has_dst_dimensions) { |
| 74 dw = bm.width(); |
| 75 dh = bm.height(); |
| 76 } |
| 77 state_->DrawImage(bm, sx, sy, sw, sh, dx, dy, dw, dh); |
| 57 } | 78 } |
| 58 | 79 |
| 59 void CanvasContext::ClearRect(float left, float top, | 80 void CanvasContext::ClearRect(float left, float top, |
| 60 float width, float height) { | 81 float width, float height) { |
| 61 SkPaint paint; | 82 SkPaint paint; |
| 62 paint.setStyle(SkPaint::kFill_Style); | 83 paint.setStyle(SkPaint::kFill_Style); |
| 63 paint.setColor(0xFFFFFFFF); | 84 paint.setColor(0xFFFFFFFF); |
| 64 canvas_->drawRectCoords(left, top, left + width, top + height, paint); | 85 canvas_->drawRectCoords(left, top, left + width, top + height, paint); |
| 65 } | 86 } |
| 66 | 87 |
| OLD | NEW |