1// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef SKIA_EXT_BITMAP_PLATFORM_DEVICE_DATA_H_
6#define SKIA_EXT_BITMAP_PLATFORM_DEVICE_DATA_H_
7
8#include "skia/ext/bitmap_platform_device.h"
9
10namespace skia {
11
12class BitmapPlatformDevice::BitmapPlatformDeviceData :
13#if defined(WIN32) || defined(__APPLE__)
14    public SkRefCnt {
15#elif defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__sun)
16    // These objects are reference counted and own a Cairo surface. The surface
17    // is the backing store for a Skia bitmap and we reference count it so that
18    // we can copy BitmapPlatformDevice objects without having to copy all the
19    // image data.
20    public base::RefCounted<BitmapPlatformDeviceData> {
21#endif
22
23 public:
24#if defined(WIN32)
25  typedef HBITMAP PlatformContext;
26#elif defined(__APPLE__)
27  typedef CGContextRef PlatformContext;
28#elif defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__sun)
29  typedef cairo_t* PlatformContext;
30#endif
31
32#if defined(WIN32) || defined(__APPLE__)
33  explicit BitmapPlatformDeviceData(PlatformContext bitmap);
34#elif defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__sun)
35  explicit BitmapPlatformDeviceData(cairo_surface_t* surface);
36#endif
37
38#if defined(WIN32)
39  // Create/destroy hdc_, which is the memory DC for our bitmap data.
40  HDC GetBitmapDC();
41  void ReleaseBitmapDC();
42  bool IsBitmapDCCreated() const;
43#endif
44
45#if defined(__APPLE__)
46  void ReleaseBitmapContext();
47#endif  // defined(__APPLE__)
48
49  // Sets the transform and clip operations. This will not update the CGContext,
50  // but will mark the config as dirty. The next call of LoadConfig will
51  // pick up these changes.
52  void SetMatrixClip(const SkMatrix& transform, const SkRegion& region);
53
54  // Loads the current transform and clip into the context. Can be called even
55  // when |bitmap_context_| is NULL (will be a NOP).
56  void LoadConfig();
57
58  const SkMatrix& transform() const {
59    return transform_;
60  }
61
62  PlatformContext bitmap_context() {
63    return bitmap_context_;
64  }
65
66 private:
67#if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__sun)
68  friend class base::RefCounted<BitmapPlatformDeviceData>;
69#endif
70  virtual ~BitmapPlatformDeviceData();
71
72  // Lazily-created graphics context used to draw into the bitmap.
73  PlatformContext bitmap_context_;
74
75#if defined(WIN32)
76  // Lazily-created DC used to draw into the bitmap, see GetBitmapDC().
77  HDC hdc_;
78#elif defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__sun)
79  cairo_surface_t *const surface_;
80#endif
81
82  // True when there is a transform or clip that has not been set to the
83  // context.  The context is retrieved for every text operation, and the
84  // transform and clip do not change as much. We can save time by not loading
85  // the clip and transform for every one.
86  bool config_dirty_;
87
88  // Translation assigned to the context: we need to keep track of this
89  // separately so it can be updated even if the context isn't created yet.
90  SkMatrix transform_;
91
92  // The current clipping
93  SkRegion clip_region_;
94
95  // Disallow copy & assign.
96  BitmapPlatformDeviceData(const BitmapPlatformDeviceData&);
97  BitmapPlatformDeviceData& operator=(const BitmapPlatformDeviceData&);
98};
99
100}  // namespace skia
101
102#endif  // SKIA_EXT_BITMAP_PLATFORM_DEVICE_DATA_H_
103