• 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 "SkBitmap.h"
12 #include "SkCanvas.h"
13 #include "SkColor.h"
14 #include "SkDevice.h"
15 #include "SkGlyphRunPainter.h"
16 #include "SkImageInfo.h"
17 #include "SkRasterClip.h"
18 #include "SkRasterClipStack.h"
19 #include "SkRect.h"
20 #include "SkScalar.h"
21 #include "SkSize.h"
22 #include "SkSurfaceProps.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     void drawSprite(const SkBitmap&, int x, int y, const SkPaint&) override;
93 
94     /**
95      *  The default impl. will create a bitmap-shader from the bitmap,
96      *  and call drawRect with it.
97      */
98     void drawBitmapRect(const SkBitmap&, const SkRect*, const SkRect&,
99                         const SkPaint&, SkCanvas::SrcRectConstraint) override;
100 
101     void drawGlyphRunList(const SkGlyphRunList& glyphRunList) override;
102     void drawVertices(const SkVertices*, const SkVertices::Bone bones[], int boneCount, SkBlendMode,
103                       const SkPaint& paint) override;
104     void drawDevice(SkBaseDevice*, int x, int y, const SkPaint&) override;
105 
106     ///////////////////////////////////////////////////////////////////////////
107 
108     void drawSpecial(SkSpecialImage*, int x, int y, const SkPaint&,
109                      SkImage*, const SkMatrix&) override;
110     sk_sp<SkSpecialImage> makeSpecial(const SkBitmap&) override;
111     sk_sp<SkSpecialImage> makeSpecial(const SkImage*) override;
112     sk_sp<SkSpecialImage> snapSpecial() override;
setImmutable()113     void setImmutable() override { fBitmap.setImmutable(); }
114 
115     sk_sp<SkSpecialImage> snapBackImage(const SkIRect&) override;
116 
117     ///////////////////////////////////////////////////////////////////////////
118 
119     bool onReadPixels(const SkPixmap&, int x, int y) override;
120     bool onWritePixels(const SkPixmap&, int, int) override;
121     bool onPeekPixels(SkPixmap*) override;
122     bool onAccessPixels(SkPixmap*) override;
123 
124     void onSave() override;
125     void onRestore() override;
126     void onClipRect(const SkRect& rect, SkClipOp, bool aa) override;
127     void onClipRRect(const SkRRect& rrect, SkClipOp, bool aa) override;
128     void onClipPath(const SkPath& path, SkClipOp, bool aa) override;
129     void onClipRegion(const SkRegion& deviceRgn, SkClipOp) override;
130     void onSetDeviceClipRestriction(SkIRect* mutableClipRestriction) override;
131     bool onClipIsAA() const override;
132     void onAsRgnClip(SkRegion*) const override;
133     void validateDevBounds(const SkIRect& r) override;
134     ClipType onGetClipType() const override;
135 
136     virtual void drawBitmap(const SkBitmap&, const SkMatrix&, const SkRect* dstOrNull,
137                             const SkPaint&);
138 
139 private:
140     friend class SkCanvas;
141     friend struct DeviceCM; //for setMatrixClip
142     friend class SkDraw;
143     friend class SkDrawIter;
144     friend class SkDrawTiler;
145     friend class SkSurface_Raster;
146 
147     class BDDraw;
148 
149     // used to change the backend's pixels (and possibly config/rowbytes)
150     // but cannot change the width/height, so there should be no change to
151     // any clip information.
152     void replaceBitmapBackendForRasterSurface(const SkBitmap&) override;
153 
154     SkBaseDevice* onCreateDevice(const CreateInfo&, const SkPaint*) override;
155 
156     sk_sp<SkSurface> makeSurface(const SkImageInfo&, const SkSurfaceProps&) override;
157 
158     SkImageFilterCache* getImageFilterCache() override;
159 
160     SkBitmap    fBitmap;
161     void*       fRasterHandle = nullptr;
162     SkRasterClipStack  fRCStack;
163     std::unique_ptr<SkBitmap> fCoverage;    // if non-null, will have the same dimensions as fBitmap
164     SkGlyphRunListPainter fGlyphPainter;
165 
166 
167     typedef SkBaseDevice INHERITED;
168 };
169 
170 class SkBitmapDeviceFilteredSurfaceProps {
171 public:
SkBitmapDeviceFilteredSurfaceProps(const SkBitmap & bitmap,const SkPaint & paint,const SkSurfaceProps & surfaceProps)172     SkBitmapDeviceFilteredSurfaceProps(const SkBitmap& bitmap, const SkPaint& paint,
173                                        const SkSurfaceProps& surfaceProps)
174         : fSurfaceProps((kN32_SkColorType != bitmap.colorType() || !paint.isSrcOver())
175                         ? fLazy.init(surfaceProps.flags(), kUnknown_SkPixelGeometry)
176                         : &surfaceProps)
177     { }
178 
179     SkBitmapDeviceFilteredSurfaceProps(const SkBitmapDeviceFilteredSurfaceProps&) = delete;
180     SkBitmapDeviceFilteredSurfaceProps& operator=(const SkBitmapDeviceFilteredSurfaceProps&) = delete;
181     SkBitmapDeviceFilteredSurfaceProps(SkBitmapDeviceFilteredSurfaceProps&&) = delete;
182     SkBitmapDeviceFilteredSurfaceProps& operator=(SkBitmapDeviceFilteredSurfaceProps&&) = delete;
183 
operator()184     const SkSurfaceProps& operator()() const { return *fSurfaceProps; }
185 
186 private:
187     SkTLazy<SkSurfaceProps> fLazy;
188     SkSurfaceProps const * const fSurfaceProps;
189 };
190 
191 #endif // SkBitmapDevice_DEFINED
192