• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2010 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 SkGpuDevice_DEFINED
9 #define SkGpuDevice_DEFINED
10 
11 #include "include/core/SkBitmap.h"
12 #include "include/core/SkPicture.h"
13 #include "include/core/SkRegion.h"
14 #include "include/core/SkSurface.h"
15 #include "include/gpu/GrTypes.h"
16 #include "src/gpu/BaseDevice.h"
17 #include "src/gpu/SkGr.h"
18 #include "src/gpu/v1/ClipStack.h"
19 #include "src/gpu/v1/SurfaceDrawContext_v1.h"
20 
21 class SkSpecialImage;
22 class SkSurface;
23 class SkSurface_Gpu;
24 class SkVertices;
25 
26 namespace skgpu::v1 {
27 
28 /**
29  *  Subclass of BaseDevice, which directs all drawing to the GrGpu owned by the canvas.
30  */
31 class Device final : public BaseDevice  {
32 public:
33     bool wait(int numSemaphores,
34               const GrBackendSemaphore* waitSemaphores,
35               bool deleteSemaphoresAfterWait) override;
36 
discard()37     void discard() override {
38         fSurfaceDrawContext->discard();
39     }
40 
41     bool replaceBackingProxy(SkSurface::ContentChangeMode,
42                              sk_sp<GrRenderTargetProxy>,
43                              GrColorType,
44                              sk_sp<SkColorSpace>,
45                              GrSurfaceOrigin,
46                              const SkSurfaceProps&) override;
47     using BaseDevice::replaceBackingProxy;
48 
49     void asyncRescaleAndReadPixels(const SkImageInfo& info,
50                                    const SkIRect& srcRect,
51                                    RescaleGamma rescaleGamma,
52                                    RescaleMode rescaleMode,
53                                    ReadPixelsCallback callback,
54                                    ReadPixelsContext context) override;
55 
56     void asyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace,
57                                          sk_sp<SkColorSpace> dstColorSpace,
58                                          const SkIRect& srcRect,
59                                          SkISize dstSize,
60                                          RescaleGamma rescaleGamma,
61                                          RescaleMode,
62                                          ReadPixelsCallback callback,
63                                          ReadPixelsContext context) override;
64 
65     /**
66      * This factory uses the color space, origin, surface properties, and initialization
67      * method along with the provided proxy to create the gpu device.
68      */
69     static sk_sp<BaseDevice> Make(GrRecordingContext*,
70                                   GrColorType,
71                                   sk_sp<GrSurfaceProxy>,
72                                   sk_sp<SkColorSpace>,
73                                   GrSurfaceOrigin,
74                                   const SkSurfaceProps&,
75                                   InitContents);
76 
77     /**
78      * This factory uses the budgeted, imageInfo, fit, sampleCount, mipmapped, and isProtected
79      * parameters to create a proxy to back the gpu device. The color space (from the image info),
80      * origin, surface properties, and initialization method are then used (with the created proxy)
81      * to create the device.
82      */
83     static sk_sp<BaseDevice> Make(GrRecordingContext*,
84                                   SkBudgeted,
85                                   const SkImageInfo&,
86                                   SkBackingFit,
87                                   int sampleCount,
88                                   GrMipmapped,
89                                   GrProtected,
90                                   GrSurfaceOrigin,
91                                   const SkSurfaceProps&,
92                                   InitContents);
93 
~Device()94     ~Device() override {}
95 
96     SurfaceDrawContext* surfaceDrawContext() override;
97     const SurfaceDrawContext* surfaceDrawContext() const;
98     skgpu::SurfaceFillContext* surfaceFillContext() override;
99 
100     // set all pixels to 0
101     void clearAll();
102 
103     void drawPaint(const SkPaint& paint) override;
104     void drawPoints(SkCanvas::PointMode mode, size_t count, const SkPoint[],
105                     const SkPaint& paint) override;
106     void drawRect(const SkRect& r, const SkPaint& paint) override;
107     void drawRRect(const SkRRect& r, const SkPaint& paint) override;
108     void drawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) override;
109     void drawRegion(const SkRegion& r, const SkPaint& paint) override;
110     void drawOval(const SkRect& oval, const SkPaint& paint) override;
111     void drawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle,
112                  bool useCenter, const SkPaint& paint) override;
113     void drawPath(const SkPath& path, const SkPaint& paint, bool pathIsMutable) override;
114 
115     void onDrawGlyphRunList(const SkGlyphRunList& glyphRunList, const SkPaint& paint) override;
116     void drawVertices(const SkVertices*, SkBlendMode, const SkPaint&) override;
117     void drawShadow(const SkPath&, const SkDrawShadowRec&) override;
118     void drawAtlas(const SkRSXform[], const SkRect[], const SkColor[], int count, SkBlendMode,
119                    const SkPaint&) override;
120 
121     void drawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
122                        const SkSamplingOptions&, const SkPaint&,
123                        SkCanvas::SrcRectConstraint) override;
124     void drawImageLattice(const SkImage*, const SkCanvas::Lattice&,
125                           const SkRect& dst, SkFilterMode, const SkPaint&) override;
126 
127     void drawDrawable(SkDrawable*, const SkMatrix*, SkCanvas* canvas) override;
128 
129     void drawDevice(SkBaseDevice*, const SkSamplingOptions&, const SkPaint&) override;
130     void drawSpecial(SkSpecialImage*, const SkMatrix& localToDevice, const SkSamplingOptions&,
131                      const SkPaint&) override;
132 
133     void drawEdgeAAQuad(const SkRect& rect, const SkPoint clip[4], SkCanvas::QuadAAFlags aaFlags,
134                         const SkColor4f& color, SkBlendMode mode) override;
135     void drawEdgeAAImageSet(const SkCanvas::ImageSetEntry[], int count, const SkPoint dstClips[],
136                             const SkMatrix[], const SkSamplingOptions&, const SkPaint&,
137                             SkCanvas::SrcRectConstraint) override;
138 
139     sk_sp<SkSpecialImage> makeSpecial(const SkBitmap&) override;
140     sk_sp<SkSpecialImage> makeSpecial(const SkImage*) override;
141     sk_sp<SkSpecialImage> snapSpecial(const SkIRect& subset, bool forceCopy = false) override;
142 
143     bool onAccessPixels(SkPixmap*) override;
144 
145     bool android_utils_clipWithStencil() override;
146 
147 protected:
148     bool onReadPixels(const SkPixmap&, int, int) override;
149     bool onWritePixels(const SkPixmap&, int, int) override;
150 
onSave()151     void onSave() override { fClip.save(); }
onRestore()152     void onRestore() override { fClip.restore(); }
153 
onClipRect(const SkRect & rect,SkClipOp op,bool aa)154     void onClipRect(const SkRect& rect, SkClipOp op, bool aa) override {
155         SkASSERT(op == SkClipOp::kIntersect || op == SkClipOp::kDifference);
156         fClip.clipRect(this->localToDevice(), rect, GrAA(aa), op);
157     }
onClipRRect(const SkRRect & rrect,SkClipOp op,bool aa)158     void onClipRRect(const SkRRect& rrect, SkClipOp op, bool aa) override {
159         SkASSERT(op == SkClipOp::kIntersect || op == SkClipOp::kDifference);
160         fClip.clipRRect(this->localToDevice(), rrect, GrAA(aa), op);
161     }
162     void onClipPath(const SkPath& path, SkClipOp op, bool aa) override;
onClipShader(sk_sp<SkShader> shader)163     void onClipShader(sk_sp<SkShader> shader) override {
164         fClip.clipShader(std::move(shader));
165     }
onReplaceClip(const SkIRect & rect)166     void onReplaceClip(const SkIRect& rect) override {
167         // Transform from "global/canvas" coordinates to relative to this device
168         SkRect deviceRect = SkMatrixPriv::MapRect(this->globalToDevice(), SkRect::Make(rect));
169         fClip.replaceClip(deviceRect.round());
170     }
171     void onClipRegion(const SkRegion& globalRgn, SkClipOp op) override;
172     void onAsRgnClip(SkRegion*) const override;
173     ClipType onGetClipType() const override;
174     bool onClipIsAA() const override;
175 
onClipIsWideOpen()176     bool onClipIsWideOpen() const override {
177         return fClip.clipState() == ClipStack::ClipState::kWideOpen;
178     }
onDevClipBounds()179     SkIRect onDevClipBounds() const override { return fClip.getConservativeBounds(); }
180 
181 private:
182     std::unique_ptr<SurfaceDrawContext> fSurfaceDrawContext;
183 
184     ClipStack fClip;
185 
186     static sk_sp<BaseDevice> Make(std::unique_ptr<SurfaceDrawContext>,
187                                   SkAlphaType,
188                                   InitContents);
189 
190     Device(std::unique_ptr<SurfaceDrawContext>, DeviceFlags);
191 
192     SkBaseDevice* onCreateDevice(const CreateInfo&, const SkPaint*) override;
193 
194     sk_sp<SkSurface> makeSurface(const SkImageInfo&, const SkSurfaceProps&) override;
195 
196     SkImageFilterCache* getImageFilterCache() override;
197 
forceConservativeRasterClip()198     bool forceConservativeRasterClip() const override { return true; }
199 
clip()200     const GrClip* clip() const { return &fClip; }
201 
202     // If not null, dstClip must be contained inside dst and will also respect the edge AA flags.
203     // If 'preViewMatrix' is not null, final CTM will be this->ctm() * preViewMatrix.
204     void drawImageQuad(const SkImage*, const SkRect* src, const SkRect* dst,
205                        const SkPoint dstClip[4], GrAA aa, GrQuadAAFlags aaFlags,
206                        const SkMatrix* preViewMatrix, const SkSamplingOptions&,
207                        const SkPaint&, SkCanvas::SrcRectConstraint);
208 
209     // FIXME(michaelludwig) - Should be removed in favor of using drawImageQuad with edge flags to
210     // for every element in the SkLatticeIter.
211     void drawViewLattice(GrSurfaceProxyView,
212                          const GrColorInfo& colorInfo,
213                          std::unique_ptr<SkLatticeIter>,
214                          const SkRect& dst,
215                          SkFilterMode,
216                          const SkPaint&);
217 
218     friend class ::SkSurface_Gpu;      // for access to surfaceProps
219     using INHERITED = BaseDevice;
220 };
221 
222 } // namespace skgpu::v1
223 
224 #undef GR_CLIP_STACK
225 
226 #endif
227