Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(151)

Side by Side Diff: runtime/embedders/openglui/common/canvas_context.cc

Issue 13042015: Various OpenGLUI changes: (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 #include "core/SkStream.h" 9 #include "core/SkStream.h"
10 #include "embedders/openglui/common/image_cache.h"
10 #include "embedders/openglui/common/support.h" 11 #include "embedders/openglui/common/support.h"
11 12
12 // TODO(gram): this should be dynamic. 13 // TODO(gram): this should be dynamic.
13 #define MAX_CONTEXTS 16 14 #define MAX_CONTEXTS 256
14 CanvasContext* contexts[MAX_CONTEXTS] = { 0 }; 15 CanvasContext* contexts[MAX_CONTEXTS] = { 0 };
15 16
16 CanvasContext* Context2D(int handle) { 17 CanvasContext* Context2D(int handle) {
17 if (handle < 0 || handle >= MAX_CONTEXTS) { 18 if (handle < 0 || handle >= MAX_CONTEXTS) {
18 LOGE("Request for out-of-range handle %d", handle); 19 LOGE("Request for out-of-range handle %d", handle);
19 return NULL; 20 return NULL;
20 } 21 }
21 if (contexts[handle] == NULL) { 22 if (contexts[handle] == NULL) {
22 LOGE("Warning: request for context with handle %d returns NULL", handle); 23 LOGE("Warning: request for context with handle %d returns NULL", handle);
23 } 24 }
24 return contexts[handle]; 25 return contexts[handle];
25 } 26 }
26 27
27 void FreeContexts() { 28 void FreeContexts() {
28 extern CanvasContext* display_context; 29 extern CanvasContext* display_context;
29 for (int i = 0; i < MAX_CONTEXTS; i++) { 30 for (int i = 0; i < MAX_CONTEXTS; i++) {
30 delete contexts[i]; 31 delete contexts[i];
31 contexts[i] = NULL; 32 contexts[i] = NULL;
32 } 33 }
33 display_context = NULL; 34 display_context = NULL;
34 } 35 }
35 36
36 37
37 CanvasContext::CanvasContext(int handle, int16_t widthp, int16_t heightp) 38 CanvasContext::CanvasContext(int handle, int16_t widthp, int16_t heightp)
38 : canvas_(NULL), 39 : canvas_(NULL),
39 width_(widthp), 40 width_(widthp),
40 height_(heightp), 41 height_(heightp),
41 imageSmoothingEnabled_(true), 42 imageSmoothingEnabled_(true),
42 state_(NULL) { 43 state_(NULL) {
44 if (handle >= MAX_CONTEXTS) {
45 LOGE("Max contexts exceeded");
46 exit(-1);
47 }
43 if (handle == 0) { 48 if (handle == 0) {
44 canvas_ = graphics->CreateDisplayCanvas(); 49 canvas_ = graphics->CreateDisplayCanvas();
45 } else { 50 } else {
46 canvas_ = graphics->CreateBitmapCanvas(widthp, heightp); 51 canvas_ = graphics->CreateBitmapCanvas(widthp, heightp);
47 } 52 }
48 state_ = new CanvasState(canvas_); 53 state_ = new CanvasState(canvas_);
49 contexts[handle] = this; 54 contexts[handle] = this;
50 LOGI("Created context with handle %d", handle); 55 LOGI("Created context with handle %d", handle);
51 } 56 }
52 57
53 CanvasContext::~CanvasContext() { 58 CanvasContext::~CanvasContext() {
54 delete state_; 59 delete state_;
55 delete canvas_; 60 delete canvas_;
56 } 61 }
57 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
58 void CanvasContext::DrawImage(const char* src_url, 80 void CanvasContext::DrawImage(const char* src_url,
59 int sx, int sy, 81 int sx, int sy,
60 bool has_src_dimensions, int sw, int sh, 82 bool has_src_dimensions, int sw, int sh,
61 int dx, int dy, 83 int dx, int dy,
62 bool has_dst_dimensions, int dw, int dh) { 84 bool has_dst_dimensions, int dw, int dh) {
63 SkBitmap bm; 85 const SkBitmap* bm = ImageCache::GetImage(src_url);
64 if (strncmp(src_url, "context2d://", 12) == 0) { 86 if (bm == NULL) return;
65 int handle = atoi(src_url + 12);
66 CanvasContext* otherContext = Context2D(handle);
67 SkDevice* device = otherContext->canvas_->getDevice();
68 bm = device->accessBitmap(false);
69 } else {
70 const char* filepath;
71 if (strncmp(src_url, "file://", 7) == 0) {
72 filepath = src_url + 7;
73 } else {
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;
109 }
110 }
111 if (!has_src_dimensions) { 87 if (!has_src_dimensions) {
112 sw = bm.width(); 88 sw = bm->width();
113 sh = bm.height(); 89 sh = bm->height();
114 } 90 }
115 if (!has_dst_dimensions) { 91 if (!has_dst_dimensions) {
116 dw = bm.width(); 92 dw = bm->width();
117 dh = bm.height(); 93 dh = bm->height();
118 } 94 }
119 state_->DrawImage(bm, sx, sy, sw, sh, dx, dy, dw, dh); 95 state_->DrawImage(*bm, sx, sy, sw, sh, dx, dy, dw, dh);
120 isDirty_ = true; 96 isDirty_ = true;
121 } 97 }
122 98
123 void CanvasContext::ClearRect(float left, float top, 99 void CanvasContext::ClearRect(float left, float top,
124 float width, float height) { 100 float width, float height) {
125 SkPaint paint; 101 SkPaint paint;
126 paint.setStyle(SkPaint::kFill_Style); 102 paint.setStyle(SkPaint::kFill_Style);
127 paint.setColor(0xFFFFFFFF); 103 paint.setColor(0xFFFFFFFF);
128 canvas_->drawRectCoords(left, top, left + width, top + height, paint); 104 canvas_->drawRectCoords(left, top, left + width, top + height, paint);
129 isDirty_ = true; 105 isDirty_ = true;
130 } 106 }
131 107
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698