| 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/canvas_context.h" | |
| 6 | |
| 7 #include <ctype.h> | |
| 8 #include <string.h> | |
| 9 #include "core/SkStream.h" | |
| 10 #include "embedders/openglui/common/image_cache.h" | |
| 11 #include "embedders/openglui/common/support.h" | |
| 12 | |
| 13 // TODO(gram): this should be dynamic. | |
| 14 #define MAX_CONTEXTS 256 | |
| 15 CanvasContext* contexts[MAX_CONTEXTS] = { 0 }; | |
| 16 | |
| 17 CanvasContext* Context2D(int handle) { | |
| 18 if (handle < 0 || handle >= MAX_CONTEXTS) { | |
| 19 LOGE("Request for out-of-range handle %d", handle); | |
| 20 return NULL; | |
| 21 } | |
| 22 if (contexts[handle] == NULL) { | |
| 23 LOGE("Warning: request for context with handle %d returns NULL", handle); | |
| 24 } | |
| 25 return contexts[handle]; | |
| 26 } | |
| 27 | |
| 28 void FreeContexts() { | |
| 29 extern CanvasContext* display_context; | |
| 30 for (int i = 0; i < MAX_CONTEXTS; i++) { | |
| 31 delete contexts[i]; | |
| 32 contexts[i] = NULL; | |
| 33 } | |
| 34 display_context = NULL; | |
| 35 } | |
| 36 | |
| 37 | |
| 38 CanvasContext::CanvasContext(int handle, int16_t widthp, int16_t heightp) | |
| 39 : canvas_(NULL), | |
| 40 width_(widthp), | |
| 41 height_(heightp), | |
| 42 imageSmoothingEnabled_(true), | |
| 43 state_(NULL) { | |
| 44 if (handle >= MAX_CONTEXTS) { | |
| 45 LOGE("Max contexts exceeded"); | |
| 46 exit(-1); | |
| 47 } | |
| 48 if (handle == 0) { | |
| 49 canvas_ = graphics->CreateDisplayCanvas(); | |
| 50 } else { | |
| 51 canvas_ = graphics->CreateBitmapCanvas(widthp, heightp); | |
| 52 } | |
| 53 state_ = new CanvasState(canvas_); | |
| 54 contexts[handle] = this; | |
| 55 LOGI("Created context with handle %d", handle); | |
| 56 } | |
| 57 | |
| 58 CanvasContext::~CanvasContext() { | |
| 59 delete state_; | |
| 60 delete canvas_; | |
| 61 } | |
| 62 | |
| 63 void CanvasContext::Save() { | |
| 64 state_ = state_->Save(); | |
| 65 } | |
| 66 | |
| 67 void CanvasContext::Restore() { | |
| 68 CanvasState* popped_state = state_; | |
| 69 state_ = state_->Restore(); | |
| 70 if (state_ == NULL) { | |
| 71 LOGE("Popping last state!"); | |
| 72 state_ = popped_state; | |
| 73 } | |
| 74 if (state_ != popped_state) { | |
| 75 // Only delete if the pop was successful. | |
| 76 delete popped_state; | |
| 77 } | |
| 78 } | |
| 79 | |
| 80 void CanvasContext::DrawImage(const char* src_url, | |
| 81 int sx, int sy, | |
| 82 bool has_src_dimensions, int sw, int sh, | |
| 83 int dx, int dy, | |
| 84 bool has_dst_dimensions, int dw, int dh) { | |
| 85 const SkBitmap* bm = ImageCache::GetImage(src_url); | |
| 86 if (bm == NULL) return; | |
| 87 if (!has_src_dimensions) { | |
| 88 sw = bm->width(); | |
| 89 sh = bm->height(); | |
| 90 } | |
| 91 if (!has_dst_dimensions) { | |
| 92 dw = bm->width(); | |
| 93 dh = bm->height(); | |
| 94 } | |
| 95 state_->DrawImage(*bm, sx, sy, sw, sh, dx, dy, dw, dh); | |
| 96 isDirty_ = true; | |
| 97 } | |
| 98 | |
| 99 void CanvasContext::ClearRect(float left, float top, | |
| 100 float width, float height) { | |
| 101 SkPaint paint; | |
| 102 paint.setStyle(SkPaint::kFill_Style); | |
| 103 paint.setColor(0xFFFFFFFF); | |
| 104 canvas_->drawRectCoords(left, top, left + width, top + height, paint); | |
| 105 isDirty_ = true; | |
| 106 } | |
| 107 | |
| OLD | NEW |