• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2013 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef SkBitmapDevice_DEFINED
9 #define SkBitmapDevice_DEFINED
10 
11 #include "include/core/SkBitmap.h"
12 #include "include/core/SkCanvas.h"
13 #include "include/core/SkColor.h"
14 #include "include/core/SkImageInfo.h"
15 #include "include/core/SkRect.h"
16 #include "include/core/SkScalar.h"
17 #include "include/core/SkSize.h"
18 #include "src/core/SkDevice.h"
19 #include "src/core/SkGlyphRunPainter.h"
20 #include "src/core/SkRasterClip.h"
21 #include "src/core/SkRasterClipStack.h"
22 
23 class SkImageFilterCache;
24 class SkMatrix;
25 class SkPaint;
26 class SkPath;
27 class SkPixmap;
28 class SkRasterHandleAllocator;
29 class SkRRect;
30 class SkSurface;
31 class SkSurfaceProps;
32 struct SkPoint;
33 struct SkCustomMesh;
34 
35 ///////////////////////////////////////////////////////////////////////////////
36 class SkBitmapDevice : public SkBaseDevice {
37 public:
38     /**
39      *  Construct a new device with the specified bitmap as its backend. It is
40      *  valid for the bitmap to have no pixels associated with it. In that case,
41      *  any drawing to this device will have no effect.
42      */
43     SkBitmapDevice(const SkBitmap& bitmap);
44 
45     /**
46      * Create a new device along with its requisite pixel memory using
47      * default SkSurfaceProps (i.e., kLegacyFontHost_InitType-style).
48      * Note: this entry point is slated for removal - no one should call it.
49      */
50     static SkBitmapDevice* Create(const SkImageInfo& info);
51 
52     /**
53      *  Construct a new device with the specified bitmap as its backend. It is
54      *  valid for the bitmap to have no pixels associated with it. In that case,
55      *  any drawing to this device will have no effect.
56      */
57     SkBitmapDevice(const SkBitmap& bitmap, const SkSurfaceProps& surfaceProps,
58                    void* externalHandle = nullptr);
59 
60     static SkBitmapDevice* Create(const SkImageInfo&, const SkSurfaceProps&,
61                                   SkRasterHandleAllocator* = nullptr);
62 
63 protected:
getRasterHandle()64     void* getRasterHandle() const override { return fRasterHandle; }
65 
66     /** These are called inside the per-device-layer loop for each draw call.
67      When these are called, we have already applied any saveLayer operations,
68      and are handling any looping from the paint.
69      */
70     void drawPaint(const SkPaint& paint) override;
71     void drawPoints(SkCanvas::PointMode mode, size_t count,
72                             const SkPoint[], const SkPaint& paint) override;
73     void drawRect(const SkRect& r, const SkPaint& paint) override;
74     void drawOval(const SkRect& oval, const SkPaint& paint) override;
75     void drawRRect(const SkRRect& rr, const SkPaint& paint) override;
76 
77     /**
78      *  If pathIsMutable, then the implementation is allowed to cast path to a
79      *  non-const pointer and modify it in place (as an optimization). Canvas
80      *  may do this to implement helpers such as drawOval, by placing a temp
81      *  path on the stack to hold the representation of the oval.
82      */
83     void drawPath(const SkPath&, const SkPaint&, bool pathIsMutable) override;
84 
85     void drawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
86                        const SkSamplingOptions&, const SkPaint&,
87                        SkCanvas::SrcRectConstraint) override;
88 
89     void drawVertices(const SkVertices*, sk_sp<SkBlender>, const SkPaint&) override;
90 #ifdef SK_ENABLE_SKSL
91     void drawCustomMesh(SkCustomMesh, sk_sp<SkBlender>, const SkPaint&) override;
92 #endif
93 
94     void drawAtlas(const SkRSXform[], const SkRect[], const SkColor[], int count, sk_sp<SkBlender>,
95                    const SkPaint&) override;
96 
97     ///////////////////////////////////////////////////////////////////////////
98 
99     void drawDevice(SkBaseDevice*, const SkSamplingOptions&, const SkPaint&) override;
100     void drawSpecial(SkSpecialImage*, const SkMatrix&, const SkSamplingOptions&,
101                      const SkPaint&) override;
102 
103     sk_sp<SkSpecialImage> makeSpecial(const SkBitmap&) override;
104     sk_sp<SkSpecialImage> makeSpecial(const SkImage*) override;
105     sk_sp<SkSpecialImage> snapSpecial(const SkIRect&, bool = false) override;
setImmutable()106     void setImmutable() override { fBitmap.setImmutable(); }
107 
108     ///////////////////////////////////////////////////////////////////////////
109 
110     void onDrawGlyphRunList(SkCanvas*, const SkGlyphRunList&, const SkPaint&) override;
111     bool onReadPixels(const SkPixmap&, int x, int y) override;
112     bool onWritePixels(const SkPixmap&, int, int) override;
113     bool onPeekPixels(SkPixmap*) override;
114     bool onAccessPixels(SkPixmap*) override;
115 
116     void onSave() override;
117     void onRestore() override;
118     void onClipRect(const SkRect& rect, SkClipOp, bool aa) override;
119     void onClipRRect(const SkRRect& rrect, SkClipOp, bool aa) override;
120     void onClipPath(const SkPath& path, SkClipOp, bool aa) override;
121     void onClipShader(sk_sp<SkShader>) override;
122     void onClipRegion(const SkRegion& deviceRgn, SkClipOp) override;
123     void onReplaceClip(const SkIRect& rect) override;
124     bool onClipIsAA() const override;
125     bool onClipIsWideOpen() const override;
126     void onAsRgnClip(SkRegion*) const override;
127     void validateDevBounds(const SkIRect& r) override;
128     ClipType onGetClipType() const override;
129     SkIRect onDevClipBounds() const override;
130 
131     void drawBitmap(const SkBitmap&, const SkMatrix&, const SkRect* dstOrNull,
132                     const SkSamplingOptions&, const SkPaint&);
133 
134 private:
135     friend class SkCanvas;
136     friend class SkDraw;
137     friend class SkDrawTiler;
138     friend class SkSurface_Raster;
139 
140     class BDDraw;
141 
142     // used to change the backend's pixels (and possibly config/rowbytes)
143     // but cannot change the width/height, so there should be no change to
144     // any clip information.
145     void replaceBitmapBackendForRasterSurface(const SkBitmap&) override;
146 
147     SkBaseDevice* onCreateDevice(const CreateInfo&, const SkPaint*) override;
148 
149     sk_sp<SkSurface> makeSurface(const SkImageInfo&, const SkSurfaceProps&) override;
150 
151     SkImageFilterCache* getImageFilterCache() override;
152 
153     SkBitmap    fBitmap;
154     void*       fRasterHandle = nullptr;
155     SkRasterClipStack  fRCStack;
156     SkGlyphRunListPainter fGlyphPainter;
157 
158 
159     using INHERITED = SkBaseDevice;
160 };
161 
162 #endif // SkBitmapDevice_DEFINED
163