Plugin Power Saver: Fix a nullptr crash with plugin placeholder resize.
[chromium-blink-merge.git] / skia / ext / bitmap_platform_device_win.h
blob8acfd42bbabe32dc32ff3837c1b26ae9c50ca0ff
1 // Copyright (c) 2012 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.
5 #ifndef SKIA_EXT_BITMAP_PLATFORM_DEVICE_WIN_H_
6 #define SKIA_EXT_BITMAP_PLATFORM_DEVICE_WIN_H_
8 #include "base/basictypes.h"
9 #include "base/compiler_specific.h"
10 #include "skia/ext/platform_device.h"
11 #include "skia/ext/refptr.h"
13 namespace skia {
15 // A device is basically a wrapper around SkBitmap that provides a surface for
16 // SkCanvas to draw into. Our device provides a surface Windows can also write
17 // to. BitmapPlatformDevice creates a bitmap using CreateDIBSection() in a
18 // format that Skia supports and can then use this to draw ClearType into, etc.
19 // This pixel data is provided to the bitmap that the device contains so that it
20 // can be shared.
22 // The GDI bitmap created for drawing is actually owned by a
23 // PlatformBitmapPixelRef, and stored in an SkBitmap via the normal skia
24 // SkPixelRef refcounting mechanism. In this way, the GDI bitmap can outlive
25 // the device created to draw into it. So it is safe to call accessBitmap() on
26 // the device, and retain the returned SkBitmap.
27 class SK_API BitmapPlatformDevice : public SkBitmapDevice, public PlatformDevice {
28 public:
29 // Factory function. is_opaque should be set if the caller knows the bitmap
30 // will be completely opaque and allows some optimizations.
32 // The |shared_section| parameter is optional (pass NULL for default
33 // behavior). If |shared_section| is non-null, then it must be a handle to a
34 // file-mapping object returned by CreateFileMapping. See CreateDIBSection
35 // for details. If |shared_section| is null, the bitmap backing store is not
36 // initialized.
37 static BitmapPlatformDevice* Create(int width, int height,
38 bool is_opaque, HANDLE shared_section,
39 bool do_clear = false);
41 // Create a BitmapPlatformDevice with no shared section. The bitmap is not
42 // initialized to 0.
43 static BitmapPlatformDevice* Create(int width, int height, bool is_opaque);
45 ~BitmapPlatformDevice() override;
47 // PlatformDevice overrides
48 // Retrieves the bitmap DC, which is the memory DC for our bitmap data. The
49 // bitmap DC is lazy created.
50 PlatformSurface BeginPlatformPaint() override;
51 void EndPlatformPaint() override;
53 // Loads the given transform and clipping region into the HDC. This is
54 // overridden from SkBaseDevice.
55 void setMatrixClip(const SkMatrix& transform,
56 const SkRegion& region,
57 const SkClipStack&) override;
59 void DrawToHDC(HDC dc, int x, int y, const RECT* src_rect) override;
61 protected:
62 // Flushes the Windows device context so that the pixel data can be accessed
63 // directly by Skia. Overridden from SkBaseDevice, this is called when Skia
64 // starts accessing pixel data.
65 const SkBitmap& onAccessBitmap() override;
67 SkBaseDevice* onCreateDevice(const CreateInfo&, const SkPaint*) override;
69 private:
70 // Private constructor.
71 BitmapPlatformDevice(HBITMAP hbitmap, const SkBitmap& bitmap);
73 // Bitmap into which the drawing will be done. This bitmap not owned by this
74 // class, but by the BitmapPlatformPixelRef inside the device's SkBitmap.
75 // It's only stored here in order to lazy-create the DC (below).
76 HBITMAP hbitmap_;
78 // Previous bitmap held by the DC. This will be selected back before the
79 // DC is destroyed.
80 HBITMAP old_hbitmap_;
82 // Lazily-created DC used to draw into the bitmap; see GetBitmapDC().
83 HDC hdc_;
85 // True when there is a transform or clip that has not been set to the
86 // context. The context is retrieved for every text operation, and the
87 // transform and clip do not change as much. We can save time by not loading
88 // the clip and transform for every one.
89 bool config_dirty_;
91 // Translation assigned to the context: we need to keep track of this
92 // separately so it can be updated even if the context isn't created yet.
93 SkMatrix transform_;
95 // The current clipping region.
96 SkRegion clip_region_;
98 // Create/destroy hdc_, which is the memory DC for our bitmap data.
99 HDC GetBitmapDC();
100 void ReleaseBitmapDC();
101 bool IsBitmapDCCreated() const;
103 // Sets the transform and clip operations. This will not update the DC,
104 // but will mark the config as dirty. The next call of LoadConfig will
105 // pick up these changes.
106 void SetMatrixClip(const SkMatrix& transform, const SkRegion& region);
108 // Loads the current transform and clip into the context. Can be called even
109 // when |hbitmap_| is NULL (will be a NOP).
110 void LoadConfig();
112 #ifdef SK_DEBUG
113 int begin_paint_count_;
114 #endif
116 DISALLOW_COPY_AND_ASSIGN(BitmapPlatformDevice);
119 } // namespace skia
121 #endif // SKIA_EXT_BITMAP_PLATFORM_DEVICE_WIN_H_