| 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 #ifndef EMBEDDERS_OPENGLUI_COMMON_CANVAS_CONTEXT_H_ | |
| 6 #define EMBEDDERS_OPENGLUI_COMMON_CANVAS_CONTEXT_H_ | |
| 7 | |
| 8 #include "embedders/openglui/common/canvas_state.h" | |
| 9 #include "embedders/openglui/common/graphics_handler.h" | |
| 10 #include "embedders/openglui/common/log.h" | |
| 11 #include "embedders/openglui/common/opengl.h" | |
| 12 #include "embedders/openglui/common/support.h" | |
| 13 | |
| 14 typedef struct ImageData { | |
| 15 int width; | |
| 16 int height; | |
| 17 GLubyte* pixels; | |
| 18 | |
| 19 ImageData(int wp, int hp, GLubyte* pp) | |
| 20 : width(wp), height(hp), pixels(pp) { | |
| 21 } | |
| 22 ImageData() | |
| 23 : width(0), height(0), pixels(NULL) { | |
| 24 } | |
| 25 } ImageData; | |
| 26 | |
| 27 class CanvasContext { | |
| 28 protected: | |
| 29 SkCanvas* canvas_; | |
| 30 int16_t width_, height_; | |
| 31 bool imageSmoothingEnabled_; | |
| 32 bool isDirty_; | |
| 33 CanvasState* state_; | |
| 34 | |
| 35 static inline float Radians2Degrees(float angle) { | |
| 36 return 180.0 * angle / M_PI; | |
| 37 } | |
| 38 | |
| 39 inline void NotImplemented(const char* method) { | |
| 40 LOGE("Method CanvasContext::%s is not yet implemented", method); | |
| 41 } | |
| 42 | |
| 43 public: | |
| 44 CanvasContext(int handle, int16_t width, int16_t height); | |
| 45 virtual ~CanvasContext(); | |
| 46 | |
| 47 inline bool isDirty() { | |
| 48 return isDirty_; | |
| 49 } | |
| 50 | |
| 51 inline void clearDirty() { | |
| 52 isDirty_ = false; | |
| 53 } | |
| 54 | |
| 55 inline void setGlobalAlpha(float alpha) { | |
| 56 state_->setGlobalAlpha(alpha); | |
| 57 } | |
| 58 | |
| 59 inline void setFillColor(const char* color) { | |
| 60 state_->setFillColor(color); | |
| 61 } | |
| 62 | |
| 63 inline void setStrokeColor(const char* color) { | |
| 64 state_->setStrokeColor(color); | |
| 65 } | |
| 66 | |
| 67 inline void setShadowBlur(float blur) { | |
| 68 state_->setShadowBlur(blur); | |
| 69 } | |
| 70 | |
| 71 inline void setShadowColor(const char* color) { | |
| 72 state_->setShadowColor(color); | |
| 73 } | |
| 74 | |
| 75 inline void setShadowOffsetX(float offset) { | |
| 76 state_->setShadowOffsetX(offset); | |
| 77 } | |
| 78 | |
| 79 inline void setShadowOffsetY(float offset) { | |
| 80 state_->setShadowOffsetY(offset); | |
| 81 } | |
| 82 | |
| 83 // For now, we don't allow resizing. | |
| 84 // TODO(gram): fix this or remove these. | |
| 85 inline int setWidth(int widthp) { | |
| 86 return width_; | |
| 87 } | |
| 88 | |
| 89 inline int setHeight(int heightp) { | |
| 90 return height_; | |
| 91 } | |
| 92 | |
| 93 inline bool imageSmoothingEnabled() { | |
| 94 return imageSmoothingEnabled_; | |
| 95 } | |
| 96 | |
| 97 inline void setImageSmoothingEnabled(bool enabled) { | |
| 98 // TODO(gram): We're not actually doing anything with this yet. | |
| 99 imageSmoothingEnabled_ = enabled; | |
| 100 } | |
| 101 | |
| 102 inline void setGlobalCompositeOperation(const char* op) { | |
| 103 state_->setGlobalCompositeOperation(op); | |
| 104 } | |
| 105 | |
| 106 void Save(); | |
| 107 void Restore(); | |
| 108 | |
| 109 inline void Rotate(float angle) { | |
| 110 canvas_->rotate(Radians2Degrees(angle)); | |
| 111 } | |
| 112 | |
| 113 inline void Translate(float x, float y) { | |
| 114 canvas_->translate(x, y); | |
| 115 } | |
| 116 | |
| 117 inline void Scale(float x, float y) { | |
| 118 canvas_->scale(x, y); | |
| 119 } | |
| 120 | |
| 121 inline void Transform(float a, float b, | |
| 122 float c, float d, | |
| 123 float e, float f) { | |
| 124 SkMatrix t; | |
| 125 // Our params are for a 3 x 2 matrix in column order: | |
| 126 // | |
| 127 // a c e | |
| 128 // b d f | |
| 129 // | |
| 130 // We need to turn this into a 3x3 matrix: | |
| 131 // | |
| 132 // a c e | |
| 133 // b d f | |
| 134 // 0 0 1 | |
| 135 // | |
| 136 // and pass the params in row order: | |
| 137 t.setAll(a, c, e, b, d, f, 0, 0, 1); | |
| 138 canvas_->concat(t); | |
| 139 } | |
| 140 | |
| 141 inline void setTransform(float a, float b, | |
| 142 float c, float d, | |
| 143 float e, float f) { | |
| 144 SkMatrix t; | |
| 145 t.setAll(a, c, e, b, d, f, 0, 0, 1); | |
| 146 canvas_->setMatrix(t); | |
| 147 } | |
| 148 | |
| 149 ImageData* GetImageData(float sx, float sy, float sw, float sh) { | |
| 150 NotImplemented("GetImageData"); | |
| 151 return NULL; | |
| 152 } | |
| 153 | |
| 154 void PutImageData(ImageData* imageData, float dx, float dy) { | |
| 155 NotImplemented("PutImageData"); | |
| 156 } | |
| 157 | |
| 158 void DrawImage(const char* src_url, | |
| 159 int sx, int sy, bool has_src_dimensions, int sw, int sh, | |
| 160 int dx, int dy, bool has_dst_dimensions, int dw, int dh); | |
| 161 | |
| 162 inline void Clear() { | |
| 163 canvas_->drawColor(0xFFFFFFFF); | |
| 164 isDirty_ = true; | |
| 165 } | |
| 166 | |
| 167 void ClearRect(float left, float top, float width, float height); | |
| 168 | |
| 169 inline void FillRect(float left, float top, float width, float height) { | |
| 170 // Does not affect the path. | |
| 171 state_->FillRect(left, top, width, height); | |
| 172 isDirty_ = true; | |
| 173 } | |
| 174 | |
| 175 inline void StrokeRect(float left, float top, float width, float height) { | |
| 176 // Does not affect the path. | |
| 177 state_->StrokeRect(left, top, width, height); | |
| 178 isDirty_ = true; | |
| 179 } | |
| 180 | |
| 181 inline void BeginPath() { | |
| 182 state_->BeginPath(); | |
| 183 } | |
| 184 | |
| 185 inline void Fill() { | |
| 186 state_->Fill(); | |
| 187 isDirty_ = true; | |
| 188 } | |
| 189 | |
| 190 inline void Stroke() { | |
| 191 state_->Stroke(); | |
| 192 isDirty_ = true; | |
| 193 } | |
| 194 | |
| 195 inline void ClosePath() { | |
| 196 state_->ClosePath(); | |
| 197 } | |
| 198 | |
| 199 inline void MoveTo(float x, float y) { | |
| 200 state_->MoveTo(x, y); | |
| 201 } | |
| 202 | |
| 203 inline void LineTo(float x, float y) { | |
| 204 state_->LineTo(x, y); | |
| 205 } | |
| 206 | |
| 207 inline void QuadraticCurveTo(float cpx, float cpy, float x, float y) { | |
| 208 state_->QuadraticCurveTo(cpx, cpy, x, y); | |
| 209 } | |
| 210 | |
| 211 inline void BezierCurveTo(float cp1x, float cp1y, float cp2x, float cp2y, | |
| 212 float x, float y) { | |
| 213 state_->BezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y); | |
| 214 } | |
| 215 | |
| 216 inline void ArcTo(float x1, float y1, float x2, float y2, float radius) { | |
| 217 state_->ArcTo(x1, y1, x2, y2, radius); | |
| 218 } | |
| 219 | |
| 220 inline void Rect(float x, float y, float w, float h) { | |
| 221 state_->Rect(x, y, w, h); | |
| 222 } | |
| 223 | |
| 224 inline void Arc(float x, float y, float radius, | |
| 225 float startAngle, float endAngle, | |
| 226 bool antiClockwise) { | |
| 227 state_->Arc(x, y, radius, startAngle, endAngle, antiClockwise); | |
| 228 } | |
| 229 | |
| 230 inline void setLineWidth(double w) { | |
| 231 state_->setLineWidth(w); | |
| 232 } | |
| 233 | |
| 234 inline void setLineCap(const char* lc) { | |
| 235 state_->setLineCap(lc); | |
| 236 } | |
| 237 | |
| 238 inline void setLineDash(float* dashes, int len) { | |
| 239 state_->setLineDash(dashes, len); | |
| 240 } | |
| 241 | |
| 242 inline void setLineDashOffset(float offset) { | |
| 243 state_->setLineDashOffset(offset); | |
| 244 } | |
| 245 | |
| 246 inline void setLineJoin(const char* lj) { | |
| 247 state_->setLineJoin(lj); | |
| 248 } | |
| 249 | |
| 250 inline void setMiterLimit(float limit) { | |
| 251 state_->setMiterLimit(limit); | |
| 252 } | |
| 253 | |
| 254 inline const char* setFont(const char* font) { | |
| 255 return state_->setFont(font); | |
| 256 } | |
| 257 | |
| 258 inline const char* setTextAlign(const char* align) { | |
| 259 return state_->setTextAlign(align); | |
| 260 } | |
| 261 | |
| 262 const char* setTextBaseline(const char* baseline) { | |
| 263 return state_->setTextBaseline(baseline); | |
| 264 } | |
| 265 | |
| 266 inline const char* setDirection(const char* direction) { | |
| 267 return state_->setTextDirection(direction); | |
| 268 } | |
| 269 | |
| 270 inline void FillText(const char* text, float x, float y, | |
| 271 float maxWidth = -1) { | |
| 272 state_->FillText(text, x, y, maxWidth); | |
| 273 isDirty_ = true; | |
| 274 } | |
| 275 | |
| 276 inline void StrokeText(const char* text, float x, float y, | |
| 277 float maxWidth = -1) { | |
| 278 state_->StrokeText(text, x, y, maxWidth); | |
| 279 isDirty_ = true; | |
| 280 } | |
| 281 | |
| 282 inline float MeasureText(const char *text) { | |
| 283 return state_->MeasureText(text); | |
| 284 } | |
| 285 | |
| 286 inline void Clip() { | |
| 287 state_->Clip(); | |
| 288 } | |
| 289 | |
| 290 inline void ResetClip() { | |
| 291 // TODO(gram): Check this. Is it affected by the transform? | |
| 292 canvas_->clipRect(SkRect::MakeLTRB(0, 0, width_, height_), | |
| 293 SkRegion::kReplace_Op); | |
| 294 } | |
| 295 | |
| 296 virtual void Flush() { | |
| 297 canvas_->flush(); | |
| 298 } | |
| 299 | |
| 300 inline void SetFillGradient(bool is_radial, double x0, double y0, double r0, | |
| 301 double x1, double y1, double r1, | |
| 302 int stops, float* positions, char** colors) { | |
| 303 state_->SetFillGradient(is_radial, x0, y0, r0, x1, y1, r1, | |
| 304 stops, positions, colors); | |
| 305 } | |
| 306 | |
| 307 inline void SetStrokeGradient(bool is_radial, double x0, double y0, double r0, | |
| 308 double x1, double y1, double r1, | |
| 309 int stops, float* positions, char** colors) { | |
| 310 state_->SetStrokeGradient(is_radial, x0, y0, r0, x1, y1, r1, | |
| 311 stops, positions, colors); | |
| 312 } | |
| 313 | |
| 314 inline const SkBitmap* GetBitmap() { | |
| 315 SkDevice* device = canvas_->getDevice(); | |
| 316 return &device->accessBitmap(false); | |
| 317 } | |
| 318 }; | |
| 319 | |
| 320 CanvasContext* Context2D(int handle); | |
| 321 void FreeContexts(); | |
| 322 | |
| 323 #endif // EMBEDDERS_OPENGLUI_COMMON_CANVAS_CONTEXT_H_ | |
| 324 | |
| OLD | NEW |