Debian Codegraph @

src/gpu/ganesh/Device.h

raw
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 skgpu_v1_Device_DEFINED
9 #define skgpu_v1_Device_DEFINED
10
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkClipOp.h"
13 #include "include/core/SkColor.h"
14 #include "include/core/SkImage.h"
15 #include "include/core/SkImageInfo.h"
16 #include "include/core/SkRect.h"
17 #include "include/core/SkRefCnt.h"
18 #include "include/core/SkSamplingOptions.h"
19 #include "include/core/SkShader.h"
20 #include "include/core/SkSpan.h"
21 #include "include/core/SkSurface.h"
22 #include "include/gpu/ganesh/GrRecordingContext.h"
23 #include "include/gpu/ganesh/GrTypes.h"
24 #include "include/private/base/SkAssert.h"
25 #include "include/private/base/SkMacros.h"
26 #include "src/core/SkDevice.h"
27 #include "src/core/SkMatrixPriv.h"
28 #include "src/gpu/ganesh/ClipStack.h"
29 #include "src/gpu/ganesh/GrColorInfo.h"
30 #include "src/gpu/ganesh/GrSurfaceProxyView.h"
31 #include "src/text/gpu/SubRunControl.h"
32
33 #include <memory>
34 #include <utility>
35
36 class GrBackendSemaphore;
37 class GrClip;
38 class GrRenderTargetProxy;
39 class GrSurfaceProxy;
40 class SkBlender;
41 class SkColorSpace;
42 class SkDrawable;
43 class SkLatticeIter;
44 class SkMatrix;
45 class SkMesh;
46 class SkPaint;
47 class SkPath;
48 class SkPixmap;
49 class SkRRect;
50 class SkRecorder;
51 class SkRegion;
52 class SkSpecialImage;
53 class SkSurfaceProps;
54 class SkSurface_Ganesh;
55 class SkVertices;
56 enum SkAlphaType : int;
57 enum SkColorType : int;
58 enum class GrAA : bool;
59 enum class GrColorType;
60 enum class SkBackingFit;
61 enum class SkBlendMode;
62 enum class SkTileMode;
63 struct SkArc;
64 struct SkDrawShadowRec;
65 struct SkISize;
66 struct SkPoint;
67 struct SkRSXform;
68 namespace skgpu {
69 enum class Budgeted : bool;
70 enum class Mipmapped : bool;
71 class TiledTextureUtils;
72 }
73 namespace skif {
74 class Backend;
75 }
76 namespace sktext {
77 class GlyphRunList;
78 namespace gpu {
79 class Slug;
80 }}
81
82
83 namespace skgpu::ganesh {
84
85 class SurfaceContext;
86 class SurfaceFillContext;
87 class SurfaceDrawContext;
88
89 /**
90 * Subclass of SkDevice, which directs all drawing to the GrGpu owned by the canvas.
91 */
92 class Device final : public SkDevice {
93 public:
94 enum class InitContents {
95 kClear,
96 kUninit
97 };
98
99 GrSurfaceProxyView readSurfaceView();
100 GrRenderTargetProxy* targetProxy();
101
102 GrRecordingContext* recordingContext() const override { return fContext.get(); }
103 SkRecorder* baseRecorder() const override {
104 SkASSERT(fContext);
105 return fContext->asRecorder();
106 }
107
108 bool wait(int numSemaphores,
109 const GrBackendSemaphore* waitSemaphores,
110 bool deleteSemaphoresAfterWait);
111
112 void discard();
113 void resolveMSAA();
114
115 bool replaceBackingProxy(SkSurface::ContentChangeMode,
116 sk_sp<GrRenderTargetProxy>,
117 GrColorType,
118 sk_sp<SkColorSpace>,
119 GrSurfaceOrigin,
120 const SkSurfaceProps&);
121 bool replaceBackingProxy(SkSurface::ContentChangeMode);
122
123 using RescaleGamma = SkImage::RescaleGamma;
124 using RescaleMode = SkImage::RescaleMode;
125 using ReadPixelsCallback = SkImage::ReadPixelsCallback;
126 using ReadPixelsContext = SkImage::ReadPixelsContext;
127
128 void asyncRescaleAndReadPixels(const SkImageInfo& info,
129 const SkIRect& srcRect,
130 RescaleGamma rescaleGamma,
131 RescaleMode rescaleMode,
132 ReadPixelsCallback callback,
133 ReadPixelsContext context);
134
135 void asyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace,
136 bool readAlpha,
137 sk_sp<SkColorSpace> dstColorSpace,
138 const SkIRect& srcRect,
139 SkISize dstSize,
140 RescaleGamma rescaleGamma,
141 RescaleMode,
142 ReadPixelsCallback callback,
143 ReadPixelsContext context);
144
145 /**
146 * This factory uses the color space, origin, surface properties, and initialization
147 * method along with the provided proxy to create the gpu device.
148 */
149 static sk_sp<Device> Make(GrRecordingContext*,
150 GrColorType,
151 sk_sp<GrSurfaceProxy>,
152 sk_sp<SkColorSpace>,
153 GrSurfaceOrigin,
154 const SkSurfaceProps&,
155 InitContents);
156
157 /**
158 * This factory uses the budgeted, imageInfo, fit, sampleCount, mipmapped, and isProtected
159 * parameters to create a proxy to back the gpu device. The color space (from the image info),
160 * origin, surface properties, and initialization method are then used (with the created proxy)
161 * to create the device.
162 */
163 static sk_sp<Device> Make(GrRecordingContext*,
164 skgpu::Budgeted,
165 const SkImageInfo&,
166 SkBackingFit,
167 int sampleCount,
168 skgpu::Mipmapped,
169 GrProtected,
170 GrSurfaceOrigin,
171 const SkSurfaceProps&,
172 InitContents);
173
174 ~Device() override;
175
176 SurfaceDrawContext* surfaceDrawContext();
177 const SurfaceDrawContext* surfaceDrawContext() const;
178 SurfaceFillContext* surfaceFillContext();
179
180 SkStrikeDeviceInfo strikeDeviceInfo() const override;
181
182 // set all pixels to 0
183 void clearAll();
184
185 void drawPaint(const SkPaint& paint) override;
186 void drawPoints(SkCanvas::PointMode, SkSpan<const SkPoint>, const SkPaint&) override;
187 void drawRect(const SkRect& r, const SkPaint& paint) override;
188 void drawRRect(const SkRRect& r, const SkPaint& paint) override;
189 void drawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) override;
190 void drawRegion(const SkRegion& r, const SkPaint& paint) override;
191 void drawOval(const SkRect& oval, const SkPaint& paint) override;
192 void drawArc(const SkArc& arc, const SkPaint& paint) override;
193 void drawPath(const SkPath& path, const SkPaint& paint) override;
194
195 void drawVertices(const SkVertices*, sk_sp<SkBlender>, const SkPaint&, bool) override;
196 void drawMesh(const SkMesh&, sk_sp<SkBlender>, const SkPaint&) override;
197 #if !defined(SK_ENABLE_OPTIMIZE_SIZE)
198 void drawShadow(SkCanvas*, const SkPath&, const SkDrawShadowRec&) override;
199 #endif
200 void drawAtlas(SkSpan<const SkRSXform>, SkSpan<const SkRect>, SkSpan<const SkColor>,
201 sk_sp<SkBlender>, const SkPaint&) override;
202
203 void drawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
204 const SkSamplingOptions&, const SkPaint&,
205 SkCanvas::SrcRectConstraint) override;
206 bool shouldDrawAsTiledImageRect() const override { return true; }
207 bool drawAsTiledImageRect(SkCanvas*,
208 const SkImage*,
209 const SkRect* src,
210 const SkRect& dst,
211 const SkSamplingOptions&,
212 const SkPaint&,
213 SkCanvas::SrcRectConstraint) override;
214 void drawImageLattice(const SkImage*, const SkCanvas::Lattice&,
215 const SkRect& dst, SkFilterMode, const SkPaint&) override;
216
217 void drawDrawable(SkCanvas*, SkDrawable*, const SkMatrix*) override;
218
219 void drawDevice(SkDevice*, const SkSamplingOptions&, const SkPaint&) override;
220 void drawSpecial(SkSpecialImage*, const SkMatrix& localToDevice, const SkSamplingOptions&,
221 const SkPaint&, SkCanvas::SrcRectConstraint) override;
222
223 void drawCoverageMask(const SkSpecialImage* mask, const SkMatrix& maskToDevice,
224 const SkSamplingOptions& localToDevice, const SkPaint& paint) override;
225 bool drawBlurredRRect(const SkRRect&, const SkPaint&, float deviceSigma) override;
226
227 void drawEdgeAAQuad(const SkRect& rect, const SkPoint clip[4], SkCanvas::QuadAAFlags aaFlags,
228 const SkColor4f& color, SkBlendMode mode) override;
229 void drawEdgeAAImageSet(const SkCanvas::ImageSetEntry[], int count, const SkPoint dstClips[],
230 const SkMatrix preViewMatrices[], const SkSamplingOptions&,
231 const SkPaint&, SkCanvas::SrcRectConstraint) override;
232
233 // Assumes the src and dst rects have already been optimized to fit the proxy.
234 // Only implemented by the gpu devices.
235 // This method is the lowest level draw used for tiled bitmap draws. It doesn't attempt to
236 // modify its parameters (e.g., adjust src & dst) but just draws the image however it can. It
237 // could, almost, be replaced with a drawEdgeAAImageSet call for the tiled bitmap draw use
238 // case but the extra tilemode requirement and the intermediate parameter processing (e.g.,
239 // trying to alter the SrcRectConstraint) currently block that.
240 void drawEdgeAAImage(const SkImage*,
241 const SkRect& src,
242 const SkRect& dst,
243 const SkPoint dstClip[4],
244 SkCanvas::QuadAAFlags,
245 const SkMatrix& localToDevice,
246 const SkSamplingOptions&,
247 const SkPaint&,
248 SkCanvas::SrcRectConstraint,
249 const SkMatrix& srcToDst,
250 SkTileMode);
251
252 sk_sp<sktext::gpu::Slug> convertGlyphRunListToSlug(const sktext::GlyphRunList& glyphRunList,
253 const SkPaint& paint) override;
254
255 void drawSlug(SkCanvas*, const sktext::gpu::Slug* slug, const SkPaint& paint) override;
256
257 sk_sp<SkSpecialImage> snapSpecial(const SkIRect& subset, bool forceCopy = false) override;
258 sk_sp<SkSpecialImage> snapSpecialScaled(const SkIRect& subset, const SkISize& dstDims) override;
259
260 sk_sp<SkDevice> createDevice(const CreateInfo&, const SkPaint*) override;
261
262 sk_sp<SkSurface> makeSurface(const SkImageInfo&, const SkSurfaceProps&) override;
263
264 Device* asGaneshDevice() override { return this; }
265
266 SkIRect devClipBounds() const override { return fClip.getConservativeBounds(); }
267
268 void pushClipStack() override { fClip.save(); }
269 void popClipStack() override { fClip.restore(); }
270
271 void clipRect(const SkRect& rect, SkClipOp op, bool aa) override {
272 SkASSERT(op == SkClipOp::kIntersect || op == SkClipOp::kDifference);
273 fClip.clipRect(this->localToDevice(), rect, GrAA(aa), op);
274 }
275 void clipRRect(const SkRRect& rrect, SkClipOp op, bool aa) override {
276 SkASSERT(op == SkClipOp::kIntersect || op == SkClipOp::kDifference);
277 fClip.clipRRect(this->localToDevice(), rrect, GrAA(aa), op);
278 }
279 void clipPath(const SkPath& path, SkClipOp op, bool aa) override;
280
281 void replaceClip(const SkIRect& rect) override {
282 // Transform from "global/canvas" coordinates to relative to this device
283 SkRect deviceRect = SkMatrixPriv::MapRect(this->globalToDevice(), SkRect::Make(rect));
284 fClip.replaceClip(deviceRect.round());
285 }
286 void clipRegion(const SkRegion& globalRgn, SkClipOp op) override;
287
288 bool isClipAntiAliased() const override;
289
290 bool isClipEmpty() const override {
291 return fClip.clipState() == ClipStack::ClipState::kEmpty;
292 }
293
294 bool isClipRect() const override {
295 return fClip.clipState() == ClipStack::ClipState::kDeviceRect ||
296 fClip.clipState() == ClipStack::ClipState::kWideOpen;
297 }
298
299 bool isClipWideOpen() const override {
300 return fClip.clipState() == ClipStack::ClipState::kWideOpen;
301 }
302
303 void android_utils_clipAsRgn(SkRegion*) const override;
304 bool android_utils_clipWithStencil() override;
305
306 private:
307 enum class DeviceFlags {
308 kNone = 0,
309 kNeedClear = 1 << 0, //!< Surface requires an initial clear
310 kIsOpaque = 1 << 1, //!< Hint from client that rendering to this device will be
311 // opaque even if the config supports alpha.
312 };
313 SK_DECL_BITFIELD_CLASS_OPS_FRIENDS(DeviceFlags);
314
315 static SkImageInfo MakeInfo(SurfaceContext*, DeviceFlags);
316 static bool CheckAlphaTypeAndGetFlags(SkAlphaType, InitContents, DeviceFlags*);
317
318 sk_sp<GrRecordingContext> fContext;
319
320 const sktext::gpu::SubRunControl fSubRunControl;
321
322 std::unique_ptr<SurfaceDrawContext> fSurfaceDrawContext;
323
324 ClipStack fClip;
325
326 static sk_sp<Device> Make(std::unique_ptr<SurfaceDrawContext>,
327 SkAlphaType,
328 InitContents);
329
330 Device(std::unique_ptr<SurfaceDrawContext>, DeviceFlags);
331
332 void onDrawGlyphRunList(SkCanvas*, const sktext::GlyphRunList&, const SkPaint& paint) override;
333
334 bool onReadPixels(const SkPixmap&, int, int) override;
335 bool onWritePixels(const SkPixmap&, int, int) override;
336 bool onAccessPixels(SkPixmap*) override;
337
338 sk_sp<skif::Backend> createImageFilteringBackend(const SkSurfaceProps& surfaceProps,
339 SkColorType colorType) const override;
340
341 void onClipShader(sk_sp<SkShader> shader) override {
342 fClip.clipShader(std::move(shader));
343 }
344
345 const GrClip* clip() const { return &fClip; }
346
347 // If not null, dstClip must be contained inside dst and will also respect the edge AA flags.
348 // If 'preViewMatrix' is not null, final CTM will be this->ctm() * preViewMatrix.
349 void drawImageQuadDirect(const SkImage*,
350 const SkRect& src,
351 const SkRect& dst,
352 const SkPoint dstClip[4],
353 SkCanvas::QuadAAFlags,
354 const SkMatrix* preViewMatrix,
355 const SkSamplingOptions&,
356 const SkPaint&,
357 SkCanvas::SrcRectConstraint);
358
359 // FIXME(michaelludwig) - Should be removed in favor of using drawImageQuad with edge flags to
360 // for every element in the SkLatticeIter.
361 void drawViewLattice(GrSurfaceProxyView,
362 const GrColorInfo& colorInfo,
363 std::unique_ptr<SkLatticeIter>,
364 const SkRect& dst,
365 SkFilterMode,
366 const SkPaint&);
367
368 friend class ::SkSurface_Ganesh; // for access to surfaceProps
369 friend class skgpu::TiledTextureUtils; // for access to clip()
370 };
371
372 SK_MAKE_BITFIELD_CLASS_OPS(Device::DeviceFlags)
373
374 } // namespace skgpu::ganesh
375
376 #endif // skgpu_v1_Device_DEFINED