• 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 "include/core/SkSurfaceProps.h"
19 #include "src/core/SkDevice.h"
20 #include "src/core/SkGlyphRunPainter.h"
21 #include "src/core/SkRasterClip.h"
22 #include "src/core/SkRasterClipStack.h"
23 
24 class SkImageFilterCache;
25 class SkMatrix;
26 class SkPaint;
27 class SkPath;
28 class SkPixmap;
29 class SkRasterHandleAllocator;
30 class SkRRect;
31 class SkSurface;
32 struct SkPoint;
33 
34 ///////////////////////////////////////////////////////////////////////////////
35 class SkBitmapDevice : public SkBaseDevice {
36 public:
37     /**
38      *  Construct a new device with the specified bitmap as its backend. It is
39      *  valid for the bitmap to have no pixels associated with it. In that case,
40      *  any drawing to this device will have no effect.
41      */
42     SkBitmapDevice(const SkBitmap& bitmap);
43 
44     /**
45      * Create a new device along with its requisite pixel memory using
46      * default SkSurfaceProps (i.e., kLegacyFontHost_InitType-style).
47      * Note: this entry point is slated for removal - no one should call it.
48      */
49     static SkBitmapDevice* Create(const SkImageInfo& info);
50 
51     /**
52      *  Construct a new device with the specified bitmap as its backend. It is
53      *  valid for the bitmap to have no pixels associated with it. In that case,
54      *  any drawing to this device will have no effect.
55      */
56     SkBitmapDevice(const SkBitmap& bitmap, const SkSurfaceProps& surfaceProps,
57                    void* externalHandle, const SkBitmap* coverage);
58 
59     static SkBitmapDevice* Create(const SkImageInfo&, const SkSurfaceProps&,
60                                   bool trackCoverage,
61                                   SkRasterHandleAllocator*);
62 
Create(const SkImageInfo & info,const SkSurfaceProps & props)63     static SkBitmapDevice* Create(const SkImageInfo& info, const SkSurfaceProps& props) {
64         return Create(info, props, false, nullptr);
65     }
66 
accessCoverage()67     const SkPixmap* accessCoverage() const {
68         return fCoverage ? &fCoverage->pixmap() : nullptr;
69     }
70 
71 protected:
getRasterHandle()72     void* getRasterHandle() const override { return fRasterHandle; }
73 
74     /** These are called inside the per-device-layer loop for each draw call.
75      When these are called, we have already applied any saveLayer operations,
76      and are handling any looping from the paint.
77      */
78     void drawPaint(const SkPaint& paint) override;
79     void drawPoints(SkCanvas::PointMode mode, size_t count,
80                             const SkPoint[], const SkPaint& paint) override;
81     void drawRect(const SkRect& r, const SkPaint& paint) override;
82     void drawOval(const SkRect& oval, const SkPaint& paint) override;
83     void drawRRect(const SkRRect& rr, const SkPaint& paint) override;
84 
85     /**
86      *  If pathIsMutable, then the implementation is allowed to cast path to a
87      *  non-const pointer and modify it in place (as an optimization). Canvas
88      *  may do this to implement helpers such as drawOval, by placing a temp
89      *  path on the stack to hold the representation of the oval.
90      */
91     void drawPath(const SkPath&, const SkPaint&, bool pathIsMutable) override;
92 
93     void drawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
94                        const SkSamplingOptions&, const SkPaint&,
95                        SkCanvas::SrcRectConstraint) override;
96 
97     void drawVertices(const SkVertices*, SkBlendMode, const SkPaint&) override;
98     void drawAtlas(const SkRSXform[], const SkRect[], const SkColor[], int count, SkBlendMode,
99                    const SkPaint&) override;
100 
101     ///////////////////////////////////////////////////////////////////////////
102 
103     void drawDevice(SkBaseDevice*, const SkSamplingOptions&, const SkPaint&) override;
104     void drawSpecial(SkSpecialImage*, const SkMatrix&, const SkSamplingOptions&,
105                      const SkPaint&) override;
106 
107     sk_sp<SkSpecialImage> makeSpecial(const SkBitmap&) override;
108     sk_sp<SkSpecialImage> makeSpecial(const SkImage*) override;
109     sk_sp<SkSpecialImage> snapSpecial(const SkIRect&, bool = false) override;
setImmutable()110     void setImmutable() override { fBitmap.setImmutable(); }
111 
112     ///////////////////////////////////////////////////////////////////////////
113 
114     void onDrawGlyphRunList(const SkGlyphRunList& glyphRunList, const SkPaint& paint) override;
115     bool onReadPixels(const SkPixmap&, int x, int y) override;
116     bool onWritePixels(const SkPixmap&, int, int) override;
117     bool onPeekPixels(SkPixmap*) override;
118     bool onAccessPixels(SkPixmap*) override;
119 
120     void onSave() override;
121     void onRestore() override;
122     void onClipRect(const SkRect& rect, SkClipOp, bool aa) override;
123     void onClipRRect(const SkRRect& rrect, SkClipOp, bool aa) override;
124     void onClipPath(const SkPath& path, SkClipOp, bool aa) override;
125     void onClipShader(sk_sp<SkShader>) override;
126     void onClipRegion(const SkRegion& deviceRgn, SkClipOp) override;
127     void onReplaceClip(const SkIRect& rect) override;
128     bool onClipIsAA() const override;
129     bool onClipIsWideOpen() const override;
130     void onAsRgnClip(SkRegion*) const override;
131     void validateDevBounds(const SkIRect& r) override;
132     ClipType onGetClipType() const override;
133     SkIRect onDevClipBounds() const override;
134 
135     void drawBitmap(const SkBitmap&, const SkMatrix&, const SkRect* dstOrNull,
136                     const SkSamplingOptions&, const SkPaint&);
137 
138 private:
139     friend class SkCanvas;
140     friend class SkDraw;
141     friend class SkDrawTiler;
142     friend class SkSurface_Raster;
143 
144     class BDDraw;
145 
146     // used to change the backend's pixels (and possibly config/rowbytes)
147     // but cannot change the width/height, so there should be no change to
148     // any clip information.
149     void replaceBitmapBackendForRasterSurface(const SkBitmap&) override;
150 
151     SkBaseDevice* onCreateDevice(const CreateInfo&, const SkPaint*) override;
152 
153     sk_sp<SkSurface> makeSurface(const SkImageInfo&, const SkSurfaceProps&) override;
154 
155     SkImageFilterCache* getImageFilterCache() override;
156 
157     SkBitmap    fBitmap;
158     void*       fRasterHandle = nullptr;
159     SkRasterClipStack  fRCStack;
160     std::unique_ptr<SkBitmap> fCoverage;    // if non-null, will have the same dimensions as fBitmap
161     SkGlyphRunListPainter fGlyphPainter;
162 
163 
164     using INHERITED = SkBaseDevice;
165 };
166 
167 class SkBitmapDeviceFilteredSurfaceProps {
168 public:
SkBitmapDeviceFilteredSurfaceProps(const SkBitmap & bitmap,const SkPaint & paint,const SkSurfaceProps & surfaceProps)169     SkBitmapDeviceFilteredSurfaceProps(const SkBitmap& bitmap, const SkPaint& paint,
170                                        const SkSurfaceProps& surfaceProps)
171         : fSurfaceProps((kN32_SkColorType != bitmap.colorType() || !paint.isSrcOver())
172                         ? fLazy.init(surfaceProps.flags(), kUnknown_SkPixelGeometry)
173                         : &surfaceProps)
174     { }
175 
176     SkBitmapDeviceFilteredSurfaceProps(const SkBitmapDeviceFilteredSurfaceProps&) = delete;
177     SkBitmapDeviceFilteredSurfaceProps& operator=(const SkBitmapDeviceFilteredSurfaceProps&) = delete;
178     SkBitmapDeviceFilteredSurfaceProps(SkBitmapDeviceFilteredSurfaceProps&&) = delete;
179     SkBitmapDeviceFilteredSurfaceProps& operator=(SkBitmapDeviceFilteredSurfaceProps&&) = delete;
180 
operator()181     const SkSurfaceProps& operator()() const { return *fSurfaceProps; }
182 
183 private:
184     SkTLazy<SkSurfaceProps> fLazy;
185     SkSurfaceProps const * const fSurfaceProps;
186 };
187 
188 #endif // SkBitmapDevice_DEFINED
189