summaryrefslogtreecommitdiffstats
path: root/skia/ext/platform_device_mac.h
blob: a32667f43d3497a5aedaef03b7adf9debca1a5d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright (c) 2006-2008 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.

#ifndef SKIA_EXT_PLATFORM_DEVICE_MAC_H_
#define SKIA_EXT_PLATFORM_DEVICE_MAC_H_
#pragma once

#import <ApplicationServices/ApplicationServices.h>
#include "third_party/skia/include/core/SkDevice.h"

class SkMatrix;
class SkPath;
class SkRegion;

namespace skia {

// A device is basically a wrapper around SkBitmap that provides a surface for
// SkCanvas to draw into. Our device provides a surface CoreGraphics can also
// write to. It also provides functionality to play well with CG drawing
// functions.
// This class is abstract and must be subclassed. It provides the basic
// interface to implement it either with or without a bitmap backend.
class PlatformDevice : public SkDevice {
 public:
  typedef CGContextRef PlatformSurface;

  // The CGContext that corresponds to the bitmap, used for CoreGraphics
  // operations drawing into the bitmap. This is possibly heavyweight, so it
  // should exist only during one pass of rendering.
  virtual CGContextRef GetBitmapContext() = 0;

  // Draws to the given graphics context. If the bitmap context doesn't exist,
  // this will temporarily create it. However, if you have created the bitmap
  // context, it will be more efficient if you don't free it until after this
  // call so it doesn't have to be created twice.  If src_rect is null, then
  // the entirety of the source device will be copied.
  virtual void DrawToContext(CGContextRef context, int x, int y,
                             const CGRect* src_rect) = 0;

  // Returns if the preferred rendering engine is vectorial or bitmap based.
  virtual bool IsVectorial() = 0;

  // Initializes the default settings and colors in a device context.
  static void InitializeCGContext(CGContextRef context);

  // Loads a SkPath into the CG context. The path can there after be used for
  // clipping or as a stroke.
  static void LoadPathToCGContext(CGContextRef context, const SkPath& path);

  // Loads a SkRegion into the CG context.
  static void LoadClippingRegionToCGContext(CGContextRef context,
                                            const SkRegion& region,
                                            const SkMatrix& transformation);

 protected:
  // Forwards |bitmap| to SkDevice's constructor.
  PlatformDevice(const SkBitmap& bitmap);

  // Loads the specified Skia transform into the device context
  static void LoadTransformToCGContext(CGContextRef context,
                                       const SkMatrix& matrix);

  // Function pointer used by the processPixels method for setting the alpha
  // value of a particular pixel.
  typedef void (*adjustAlpha)(uint32_t* pixel);

  // Loops through each of the pixels in the specified range, invoking
  // adjustor for the alpha value of each pixel.
  virtual void processPixels(int x,
                             int y,
                             int width,
                             int height,
                             adjustAlpha adjustor) = 0;
};

}  // namespace skia

#endif  // SKIA_EXT_PLATFORM_DEVICE_MAC_H_