xref: /aosp_15_r20/external/skia/src/core/SkBitmapDevice.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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/SkRect.h"
15 #include "include/core/SkRefCnt.h"
16 #include "include/core/SkSamplingOptions.h"
17 #include "src/core/SkDevice.h"
18 #include "src/core/SkGlyphRunPainter.h"
19 #include "src/core/SkRasterClipStack.h"
20 
21 #include <cstddef>
22 
23 class SkBlender;
24 class SkImage;
25 class SkMatrix;
26 class SkMesh;
27 class SkPaint;
28 class SkPath;
29 class SkPixmap;
30 class SkRRect;
31 class SkRasterHandleAllocator;
32 class SkRegion;
33 class SkShader;
34 class SkSpecialImage;
35 class SkSurface;
36 class SkSurfaceProps;
37 class SkVertices;
38 enum class SkClipOp;
39 namespace sktext { class GlyphRunList; }
40 struct SkImageInfo;
41 struct SkPoint;
42 struct SkRSXform;
43 
44 ///////////////////////////////////////////////////////////////////////////////
45 class SkBitmapDevice : public SkDevice {
46 public:
47     /**
48      *  Construct a new device with the specified bitmap as its backend. It is
49      *  valid for the bitmap to have no pixels associated with it. In that case,
50      *  any drawing to this device will have no effect.
51      */
52     SkBitmapDevice(const SkBitmap& bitmap);
53 
54     /**
55      *  Construct a new device with the specified bitmap as its backend. It is
56      *  valid for the bitmap to have no pixels associated with it. In that case,
57      *  any drawing to this device will have no effect.
58      */
59     SkBitmapDevice(const SkBitmap& bitmap, const SkSurfaceProps& surfaceProps,
60                    void* externalHandle = nullptr);
61 
62     static sk_sp<SkBitmapDevice> Create(const SkImageInfo&, const SkSurfaceProps&,
63                                         SkRasterHandleAllocator* = nullptr);
64 
65     void drawPaint(const SkPaint& paint) override;
66     void drawPoints(SkCanvas::PointMode mode, size_t count,
67                             const SkPoint[], const SkPaint& paint) override;
68     void drawRect(const SkRect& r, const SkPaint& paint) override;
69     void drawOval(const SkRect& oval, const SkPaint& paint) override;
70     void drawRRect(const SkRRect& rr, const SkPaint& paint) override;
71 
72     void drawPath(const SkPath&, const SkPaint&, bool pathIsMutable) override;
73 
74     void drawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
75                        const SkSamplingOptions&, const SkPaint&,
76                        SkCanvas::SrcRectConstraint) override;
77 
78     void drawVertices(const SkVertices*, sk_sp<SkBlender>, const SkPaint&, bool) override;
79     // Implemented in src/sksl/SkBitmapDevice_mesh.cpp
80     void drawMesh(const SkMesh&, sk_sp<SkBlender>, const SkPaint&) override;
81 
82     void drawAtlas(const SkRSXform[], const SkRect[], const SkColor[], int count, sk_sp<SkBlender>,
83                    const SkPaint&) override;
84 
85     ///////////////////////////////////////////////////////////////////////////
86 
87     void pushClipStack() override;
88     void popClipStack() override;
89     void clipRect(const SkRect& rect, SkClipOp, bool aa) override;
90     void clipRRect(const SkRRect& rrect, SkClipOp, bool aa) override;
91     void clipPath(const SkPath& path, SkClipOp, bool aa) override;
92     void clipRegion(const SkRegion& deviceRgn, SkClipOp) override;
93     void replaceClip(const SkIRect& rect) override;
94     bool isClipAntiAliased() const override;
95     bool isClipEmpty() const override;
96     bool isClipRect() const override;
97     bool isClipWideOpen() const override;
98     void android_utils_clipAsRgn(SkRegion*) const override;
99     SkIRect devClipBounds() const override;
100 
101     ///////////////////////////////////////////////////////////////////////////
102 
103     void drawSpecial(SkSpecialImage*, const SkMatrix&, const SkSamplingOptions&,
104                      const SkPaint&, SkCanvas::SrcRectConstraint) override;
105 
106     sk_sp<SkSpecialImage> makeSpecial(const SkBitmap&) override;
107     sk_sp<SkSpecialImage> makeSpecial(const SkImage*) override;
108     sk_sp<SkSpecialImage> snapSpecial(const SkIRect&, bool forceCopy = false) override;
109 
110     sk_sp<SkDevice> createDevice(const CreateInfo&, const SkPaint*) override;
111 
112     sk_sp<SkSurface> makeSurface(const SkImageInfo&, const SkSurfaceProps&) override;
113 
setImmutable()114     void setImmutable() override { fBitmap.setImmutable(); }
115 
getRasterHandle()116     void* getRasterHandle() const override { return fRasterHandle; }
117 
118 private:
119     friend class SkDraw;
120     friend class SkDrawBase;
121     friend class SkDrawTiler;
122     friend class SkSurface_Raster;
123 
124     class BDDraw;
125 
126     // Used to change the backend's pixels (and possibly config/rowbytes) but cannot change the
127     // width/height, so there should be no change to any clip information.
128     void replaceBitmapBackendForRasterSurface(const SkBitmap&);
129 
130     void onClipShader(sk_sp<SkShader>) override;
131 
132     void onDrawGlyphRunList(SkCanvas*, const sktext::GlyphRunList&, const SkPaint& paint) override;
133 
134     bool onReadPixels(const SkPixmap&, int x, int y) override;
135     bool onWritePixels(const SkPixmap&, int, int) override;
136     bool onPeekPixels(SkPixmap*) override;
137     bool onAccessPixels(SkPixmap*) override;
138 
139     void drawBitmap(const SkBitmap&, const SkMatrix&, const SkRect* dstOrNull,
140                     const SkSamplingOptions&, const SkPaint&);
141 
142     SkBitmap    fBitmap;
143     void*       fRasterHandle = nullptr;
144     SkRasterClipStack  fRCStack;
145     SkGlyphRunListPainterCPU fGlyphPainter;
146 };
147 
148 #endif // SkBitmapDevice_DEFINED
149