canvas.cc 12.0 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "flutter/lib/ui/painting/canvas.h"

#include <math.h>

9
#include "flutter/flow/layers/physical_model_layer.h"
10 11
#include "flutter/lib/ui/painting/image.h"
#include "flutter/lib/ui/painting/matrix.h"
A
Adam Barth 已提交
12
#include "lib/tonic/converter/dart_converter.h"
13 14 15
#include "lib/tonic/dart_args.h"
#include "lib/tonic/dart_binding_macros.h"
#include "lib/tonic/dart_library_natives.h"
16 17 18 19 20 21 22 23 24 25 26
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkCanvas.h"

namespace blink {

static void Canvas_constructor(Dart_NativeArguments args) {
  DartCallConstructor(&Canvas::Create, args);
}

IMPLEMENT_WRAPPERTYPEINFO(ui, Canvas);

27 28
#define FOR_EACH_BINDING(V)         \
  V(Canvas, save)                   \
29
  V(Canvas, saveLayerWithoutBounds) \
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
  V(Canvas, saveLayer)              \
  V(Canvas, restore)                \
  V(Canvas, getSaveCount)           \
  V(Canvas, translate)              \
  V(Canvas, scale)                  \
  V(Canvas, rotate)                 \
  V(Canvas, skew)                   \
  V(Canvas, transform)              \
  V(Canvas, clipRect)               \
  V(Canvas, clipRRect)              \
  V(Canvas, clipPath)               \
  V(Canvas, drawColor)              \
  V(Canvas, drawLine)               \
  V(Canvas, drawPaint)              \
  V(Canvas, drawRect)               \
  V(Canvas, drawRRect)              \
  V(Canvas, drawDRRect)             \
  V(Canvas, drawOval)               \
  V(Canvas, drawCircle)             \
49
  V(Canvas, drawArc)                \
50 51 52 53 54 55 56
  V(Canvas, drawPath)               \
  V(Canvas, drawImage)              \
  V(Canvas, drawImageRect)          \
  V(Canvas, drawImageNine)          \
  V(Canvas, drawPicture)            \
  V(Canvas, drawPoints)             \
  V(Canvas, drawVertices)           \
57 58
  V(Canvas, drawAtlas)              \
  V(Canvas, drawShadow)
59 60 61

FOR_EACH_BINDING(DART_NATIVE_CALLBACK)

62
void Canvas::RegisterNatives(tonic::DartLibraryNatives* natives) {
63 64
  natives->Register({{"Canvas_constructor", Canvas_constructor, 6, true},
                     FOR_EACH_BINDING(DART_REGISTER_NATIVE)});
65 66
}

67 68 69 70 71 72 73 74
ftl::RefPtr<Canvas> Canvas::Create(PictureRecorder* recorder,
                                   double left,
                                   double top,
                                   double right,
                                   double bottom) {
  FTL_DCHECK(recorder);
  FTL_DCHECK(!recorder->isRecording());
  ftl::RefPtr<Canvas> canvas = ftl::MakeRefCounted<Canvas>(
75
      recorder->BeginRecording(SkRect::MakeLTRB(left, top, right, bottom)));
76 77
  recorder->set_canvas(canvas);
  return canvas;
78 79
}

80
Canvas::Canvas(SkCanvas* canvas) : canvas_(canvas) {}
81

82
Canvas::~Canvas() {}
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

void Canvas::save() {
  if (!canvas_)
    return;
  canvas_->save();
}

void Canvas::saveLayerWithoutBounds(const Paint& paint,
                                    const PaintData& paint_data) {
  if (!canvas_)
    return;
  canvas_->saveLayer(nullptr, paint.paint());
}

void Canvas::saveLayer(double left,
                       double top,
                       double right,
                       double bottom,
                       const Paint& paint,
                       const PaintData& paint_data) {
  if (!canvas_)
    return;
  SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
  canvas_->saveLayer(&bounds, paint.paint());
}

void Canvas::restore() {
  if (!canvas_)
    return;
  canvas_->restore();
}

int Canvas::getSaveCount() {
  if (!canvas_)
    return 0;
  return canvas_->getSaveCount();
}

void Canvas::translate(double dx, double dy) {
  if (!canvas_)
    return;
  canvas_->translate(dx, dy);
}

void Canvas::scale(double sx, double sy) {
  if (!canvas_)
    return;
  canvas_->scale(sx, sy);
}

void Canvas::rotate(double radians) {
  if (!canvas_)
    return;
136
  canvas_->rotate(radians * 180.0 / M_PI);
137 138 139 140 141 142 143 144
}

void Canvas::skew(double sx, double sy) {
  if (!canvas_)
    return;
  canvas_->skew(sx, sy);
}

145
void Canvas::transform(const tonic::Float64List& matrix4) {
146 147 148 149 150
  if (!canvas_)
    return;
  canvas_->concat(ToSkMatrix(matrix4));
}

151
void Canvas::clipRect(double left, double top, double right, double bottom) {
152 153 154 155 156 157 158 159
  if (!canvas_)
    return;
  canvas_->clipRect(SkRect::MakeLTRB(left, top, right, bottom));
}

void Canvas::clipRRect(const RRect& rrect) {
  if (!canvas_)
    return;
A
Adam Barth 已提交
160
  canvas_->clipRRect(rrect.sk_rrect, true);
161 162 163 164 165
}

void Canvas::clipPath(const CanvasPath* path) {
  if (!canvas_)
    return;
A
Adam Barth 已提交
166
  canvas_->clipPath(path->path(), true);
167 168
}

A
Adam Barth 已提交
169
void Canvas::drawColor(SkColor color, SkBlendMode blend_mode) {
170 171
  if (!canvas_)
    return;
A
Adam Barth 已提交
172
  canvas_->drawColor(color, blend_mode);
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
}

void Canvas::drawLine(double x1,
                      double y1,
                      double x2,
                      double y2,
                      const Paint& paint,
                      const PaintData& paint_data) {
  if (!canvas_)
    return;
  canvas_->drawLine(x1, y1, x2, y2, *paint.paint());
}

void Canvas::drawPaint(const Paint& paint, const PaintData& paint_data) {
  if (!canvas_)
    return;
  canvas_->drawPaint(*paint.paint());
}

void Canvas::drawRect(double left,
                      double top,
                      double right,
                      double bottom,
                      const Paint& paint,
                      const PaintData& paint_data) {
  if (!canvas_)
    return;
  canvas_->drawRect(SkRect::MakeLTRB(left, top, right, bottom), *paint.paint());
}

void Canvas::drawRRect(const RRect& rrect,
                       const Paint& paint,
                       const PaintData& paint_data) {
  if (!canvas_)
    return;
  canvas_->drawRRect(rrect.sk_rrect, *paint.paint());
}

void Canvas::drawDRRect(const RRect& outer,
                        const RRect& inner,
                        const Paint& paint,
                        const PaintData& paint_data) {
  if (!canvas_)
    return;
  canvas_->drawDRRect(outer.sk_rrect, inner.sk_rrect, *paint.paint());
}

void Canvas::drawOval(double left,
                      double top,
                      double right,
                      double bottom,
                      const Paint& paint,
                      const PaintData& paint_data) {
  if (!canvas_)
    return;
  canvas_->drawOval(SkRect::MakeLTRB(left, top, right, bottom), *paint.paint());
}

void Canvas::drawCircle(double x,
                        double y,
                        double radius,
                        const Paint& paint,
                        const PaintData& paint_data) {
  if (!canvas_)
    return;
  canvas_->drawCircle(x, y, radius, *paint.paint());
}

241
void Canvas::drawArc(double left,
A
Adam Barth 已提交
242 243 244 245 246 247 248 249
                     double top,
                     double right,
                     double bottom,
                     double startAngle,
                     double sweepAngle,
                     bool useCenter,
                     const Paint& paint,
                     const PaintData& paint_data) {
250 251 252
  if (!canvas_)
    return;
  canvas_->drawArc(SkRect::MakeLTRB(left, top, right, bottom),
A
Adam Barth 已提交
253 254
                   startAngle * 180.0 / M_PI, sweepAngle * 180.0 / M_PI,
                   useCenter, *paint.paint());
255 256
}

257 258 259 260 261
void Canvas::drawPath(const CanvasPath* path,
                      const Paint& paint,
                      const PaintData& paint_data) {
  if (!canvas_)
    return;
262
  FTL_DCHECK(path);
263 264 265 266 267 268 269 270 271 272
  canvas_->drawPath(path->path(), *paint.paint());
}

void Canvas::drawImage(const CanvasImage* image,
                       double x,
                       double y,
                       const Paint& paint,
                       const PaintData& paint_data) {
  if (!canvas_)
    return;
273
  FTL_DCHECK(image);
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
  canvas_->drawImage(image->image(), x, y, paint.paint());
}

void Canvas::drawImageRect(const CanvasImage* image,
                           double src_left,
                           double src_top,
                           double src_right,
                           double src_bottom,
                           double dst_left,
                           double dst_top,
                           double dst_right,
                           double dst_bottom,
                           const Paint& paint,
                           const PaintData& paint_data) {
  if (!canvas_)
    return;
290
  FTL_DCHECK(image);
291 292
  SkRect src = SkRect::MakeLTRB(src_left, src_top, src_right, src_bottom);
  SkRect dst = SkRect::MakeLTRB(dst_left, dst_top, dst_right, dst_bottom);
293
  canvas_->drawImageRect(image->image(), src, dst, paint.paint(),
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
                         SkCanvas::kFast_SrcRectConstraint);
}

void Canvas::drawImageNine(const CanvasImage* image,
                           double center_left,
                           double center_top,
                           double center_right,
                           double center_bottom,
                           double dst_left,
                           double dst_top,
                           double dst_right,
                           double dst_bottom,
                           const Paint& paint,
                           const PaintData& paint_data) {
  if (!canvas_)
    return;
310
  FTL_DCHECK(image);
311 312
  SkRect center =
      SkRect::MakeLTRB(center_left, center_top, center_right, center_bottom);
313 314 315 316 317 318 319 320 321
  SkIRect icenter;
  center.round(&icenter);
  SkRect dst = SkRect::MakeLTRB(dst_left, dst_top, dst_right, dst_bottom);
  canvas_->drawImageNine(image->image(), icenter, dst, paint.paint());
}

void Canvas::drawPicture(Picture* picture) {
  if (!canvas_)
    return;
322
  FTL_DCHECK(picture);
323 324 325 326 327 328
  canvas_->drawPicture(picture->picture().get());
}

void Canvas::drawPoints(const Paint& paint,
                        const PaintData& paint_data,
                        SkCanvas::PointMode point_mode,
329
                        const tonic::Float32List& points) {
330 331 332 333 334 335 336 337 338 339 340 341
  if (!canvas_)
    return;

  static_assert(sizeof(SkPoint) == sizeof(float) * 2,
                "SkPoint doesn't use floats.");

  canvas_->drawPoints(point_mode,
                      points.num_elements() / 2,  // SkPoints have two floats.
                      reinterpret_cast<const SkPoint*>(points.data()),
                      *paint.paint());
}

342
void Canvas::drawVertices(const Vertices* vertices,
343
                          SkBlendMode blend_mode,
344 345
                          const Paint& paint,
                          const PaintData& paint_data) {
346 347 348
  if (!canvas_)
    return;

349 350 351
  canvas_->drawVertices(vertices->vertices(),
                        blend_mode,
                        *paint.paint());
352 353
}

354 355 356 357 358 359
void Canvas::drawAtlas(const Paint& paint,
                       const PaintData& paint_data,
                       CanvasImage* atlas,
                       const tonic::Float32List& transforms,
                       const tonic::Float32List& rects,
                       const tonic::Int32List& colors,
360
                       SkBlendMode blend_mode,
361
                       const tonic::Float32List& cull_rect) {
362 363 364 365 366 367 368 369 370 371 372
  if (!canvas_)
    return;

  sk_sp<SkImage> skImage = atlas->image();

  static_assert(sizeof(SkRSXform) == sizeof(float) * 4,
                "SkRSXform doesn't use floats.");
  static_assert(sizeof(SkRect) == sizeof(float) * 4,
                "SkRect doesn't use floats.");

  canvas_->drawAtlas(
373
      skImage.get(), reinterpret_cast<const SkRSXform*>(transforms.data()),
374 375 376
      reinterpret_cast<const SkRect*>(rects.data()),
      reinterpret_cast<const SkColor*>(colors.data()),
      rects.num_elements() / 4,  // SkRect have four floats.
377
      blend_mode, reinterpret_cast<const SkRect*>(cull_rect.data()),
378 379 380
      paint.paint());
}

381 382
void Canvas::drawShadow(const CanvasPath* path,
                        SkColor color,
383
                        double elevation,
384 385 386 387 388 389 390 391
                        bool transparentOccluder) {
  flow::PhysicalModelLayer::DrawShadow(canvas_,
                                       path->path(),
                                       color,
                                       elevation,
                                       transparentOccluder);
}

392 393 394 395 396 397 398 399
void Canvas::Clear() {
  canvas_ = nullptr;
}

bool Canvas::IsRecording() const {
  return !!canvas_;
}

400
}  // namespace blink