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