Coverage Report

Created: 2021-08-22 09:07

/src/skia/include/gpu/GrBackendSurface.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2017 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 GrBackendSurface_DEFINED
9
#define GrBackendSurface_DEFINED
10
11
#include "include/gpu/GrBackendSurfaceMutableState.h"
12
#include "include/gpu/GrTypes.h"
13
#include "include/gpu/gl/GrGLTypes.h"
14
#include "include/gpu/mock/GrMockTypes.h"
15
#include "include/gpu/vk/GrVkTypes.h"
16
#include "include/private/GrGLTypesPriv.h"
17
#include "include/private/GrVkTypesPriv.h"
18
19
#ifdef SK_DAWN
20
#include "include/gpu/dawn/GrDawnTypes.h"
21
#endif
22
23
class GrBackendSurfaceMutableStateImpl;
24
class GrVkImageLayout;
25
class GrGLTextureParameters;
26
class GrColorFormatDesc;
27
28
#ifdef SK_DAWN
29
#include "dawn/webgpu_cpp.h"
30
#endif
31
32
#ifdef SK_METAL
33
#include "include/gpu/mtl/GrMtlTypes.h"
34
#endif
35
36
#ifdef SK_DIRECT3D
37
#include "include/gpu/d3d/GrD3DTypesMinimal.h"
38
#include "include/private/GrD3DTypesPriv.h"
39
class GrD3DResourceState;
40
#endif
41
42
#if defined(SK_DEBUG) || GR_TEST_UTILS
43
class SkString;
44
#endif
45
46
#if !SK_SUPPORT_GPU
47
48
// SkSurfaceCharacterization always needs a minimal version of this
49
class SK_API GrBackendFormat {
50
public:
51
    bool isValid() const { return false; }
52
};
53
54
// SkSurface and SkImage rely on a minimal version of these always being available
55
class SK_API GrBackendTexture {
56
public:
57
    GrBackendTexture() {}
58
59
    bool isValid() const { return false; }
60
};
61
62
class SK_API GrBackendRenderTarget {
63
public:
64
    GrBackendRenderTarget() {}
65
66
    bool isValid() const { return false; }
67
    bool isFramebufferOnly() const { return false; }
68
};
69
#else
70
71
enum class GrGLFormat;
72
73
class SK_API GrBackendFormat {
74
public:
75
    // Creates an invalid backend format.
76
116k
    GrBackendFormat() {}
77
    GrBackendFormat(const GrBackendFormat&);
78
    GrBackendFormat& operator=(const GrBackendFormat&);
79
80
0
    static GrBackendFormat MakeGL(GrGLenum format, GrGLenum target) {
81
0
        return GrBackendFormat(format, target);
82
0
    }
83
84
0
    static GrBackendFormat MakeVk(VkFormat format) {
85
0
        return GrBackendFormat(format, GrVkYcbcrConversionInfo());
86
0
    }
87
88
    static GrBackendFormat MakeVk(const GrVkYcbcrConversionInfo& ycbcrInfo);
89
90
#ifdef SK_DAWN
91
    static GrBackendFormat MakeDawn(wgpu::TextureFormat format) {
92
        return GrBackendFormat(format);
93
    }
94
#endif
95
96
#ifdef SK_METAL
97
    static GrBackendFormat MakeMtl(GrMTLPixelFormat format) {
98
        return GrBackendFormat(format);
99
    }
100
#endif
101
102
#ifdef SK_DIRECT3D
103
    static GrBackendFormat MakeDxgi(DXGI_FORMAT format) {
104
        return GrBackendFormat(format);
105
    }
106
#endif
107
108
    static GrBackendFormat MakeMock(GrColorType colorType, SkImage::CompressionType compression,
109
                                    bool isStencilFormat = false);
110
111
    bool operator==(const GrBackendFormat& that) const;
112
11.3k
    bool operator!=(const GrBackendFormat& that) const { return !(*this == that); }
113
114
927k
    GrBackendApi backend() const { return fBackend; }
115
510k
    GrTextureType textureType() const { return fTextureType; }
116
117
    /**
118
     * Gets the channels present in the format as a bitfield of SkColorChannelFlag values.
119
     * Luminance channels are reported as kGray_SkColorChannelFlag.
120
     */
121
    uint32_t channelMask() const;
122
123
    GrColorFormatDesc desc() const;
124
125
    /**
126
     * If the backend API is GL this gets the format as a GrGLFormat. Otherwise, returns
127
     * GrGLFormat::kUnknown.
128
     */
129
    GrGLFormat asGLFormat() const;
130
131
    /**
132
     * If the backend API is Vulkan this gets the format as a VkFormat and returns true. Otherwise,
133
     * returns false.
134
     */
135
    bool asVkFormat(VkFormat*) const;
136
137
    const GrVkYcbcrConversionInfo* getVkYcbcrConversionInfo() const;
138
139
#ifdef SK_DAWN
140
    /**
141
     * If the backend API is Dawn this gets the format as a wgpu::TextureFormat and returns true.
142
     * Otherwise, returns false.
143
     */
144
    bool asDawnFormat(wgpu::TextureFormat*) const;
145
#endif
146
147
#ifdef SK_METAL
148
    /**
149
     * If the backend API is Metal this gets the format as a GrMtlPixelFormat. Otherwise,
150
     * Otherwise, returns MTLPixelFormatInvalid.
151
     */
152
    GrMTLPixelFormat asMtlFormat() const;
153
#endif
154
155
#ifdef SK_DIRECT3D
156
    /**
157
     * If the backend API is Direct3D this gets the format as a DXGI_FORMAT and returns true.
158
     * Otherwise, returns false.
159
     */
160
    bool asDxgiFormat(DXGI_FORMAT*) const;
161
#endif
162
163
    /**
164
     * If the backend API is not Mock these three calls will return kUnknown, kNone or false,
165
     * respectively. Otherwise, only one of the following can be true. The GrColorType is not
166
     * kUnknown, the compression type is not kNone, or this is a mock stencil format.
167
     */
168
    GrColorType asMockColorType() const;
169
    SkImage::CompressionType asMockCompressionType() const;
170
    bool isMockStencilFormat() const;
171
172
    // If possible, copies the GrBackendFormat and forces the texture type to be Texture2D. If the
173
    // GrBackendFormat was for Vulkan and it originally had a GrVkYcbcrConversionInfo, we will
174
    // remove the conversion and set the format to be VK_FORMAT_R8G8B8A8_UNORM.
175
    GrBackendFormat makeTexture2D() const;
176
177
    // Returns true if the backend format has been initialized.
178
3.90M
    bool isValid() const { return fValid; }
179
180
#if defined(SK_DEBUG) || GR_TEST_UTILS
181
    SkString toStr() const;
182
#endif
183
184
private:
185
    GrBackendFormat(GrGLenum format, GrGLenum target);
186
187
    GrBackendFormat(const VkFormat vkFormat, const GrVkYcbcrConversionInfo&);
188
189
#ifdef SK_DAWN
190
    GrBackendFormat(wgpu::TextureFormat format);
191
#endif
192
193
#ifdef SK_METAL
194
    GrBackendFormat(const GrMTLPixelFormat mtlFormat);
195
#endif
196
197
#ifdef SK_DIRECT3D
198
    GrBackendFormat(DXGI_FORMAT dxgiFormat);
199
#endif
200
201
    GrBackendFormat(GrColorType, SkImage::CompressionType, bool isStencilFormat);
202
203
#ifdef SK_DEBUG
204
    bool validateMock() const;
205
#endif
206
207
    GrBackendApi fBackend = GrBackendApi::kMock;
208
    bool         fValid = false;
209
210
    union {
211
        GrGLenum fGLFormat; // the sized, internal format of the GL resource
212
        struct {
213
            VkFormat                 fFormat;
214
            GrVkYcbcrConversionInfo  fYcbcrConversionInfo;
215
        } fVk;
216
#ifdef SK_DAWN
217
        wgpu::TextureFormat fDawnFormat;
218
#endif
219
220
#ifdef SK_METAL
221
        GrMTLPixelFormat fMtlFormat;
222
#endif
223
224
#ifdef SK_DIRECT3D
225
        DXGI_FORMAT fDxgiFormat;
226
#endif
227
        struct {
228
            GrColorType              fColorType;
229
            SkImage::CompressionType fCompressionType;
230
            bool                     fIsStencilFormat;
231
        } fMock;
232
    };
233
    GrTextureType fTextureType = GrTextureType::kNone;
234
};
235
236
class SK_API GrBackendTexture {
237
public:
238
    // Creates an invalid backend texture.
239
    GrBackendTexture();
240
241
    // The GrGLTextureInfo must have a valid fFormat.
242
    GrBackendTexture(int width,
243
                     int height,
244
                     GrMipmapped,
245
                     const GrGLTextureInfo& glInfo);
246
247
#ifdef SK_VULKAN
248
    GrBackendTexture(int width,
249
                     int height,
250
                     const GrVkImageInfo& vkInfo);
251
#endif
252
253
#ifdef SK_METAL
254
    GrBackendTexture(int width,
255
                     int height,
256
                     GrMipmapped,
257
                     const GrMtlTextureInfo& mtlInfo);
258
#endif
259
260
#ifdef SK_DIRECT3D
261
    GrBackendTexture(int width,
262
                     int height,
263
                     const GrD3DTextureResourceInfo& d3dInfo);
264
#endif
265
266
#ifdef SK_DAWN
267
    GrBackendTexture(int width,
268
                     int height,
269
                     const GrDawnTextureInfo& dawnInfo);
270
#endif
271
272
    GrBackendTexture(int width,
273
                     int height,
274
                     GrMipmapped,
275
                     const GrMockTextureInfo& mockInfo);
276
277
    GrBackendTexture(const GrBackendTexture& that);
278
279
    ~GrBackendTexture();
280
281
    GrBackendTexture& operator=(const GrBackendTexture& that);
282
283
0
    SkISize dimensions() const { return {fWidth, fHeight}; }
284
0
    int width() const { return fWidth; }
285
0
    int height() const { return fHeight; }
286
0
    GrMipmapped mipmapped() const { return fMipmapped; }
287
0
    bool hasMipmaps() const { return fMipmapped == GrMipmapped::kYes; }
288
    /** deprecated alias of hasMipmaps(). */
289
0
    bool hasMipMaps() const { return this->hasMipmaps(); }
290
0
    GrBackendApi backend() const {return fBackend; }
291
0
    GrTextureType textureType() const { return fTextureType; }
292
293
    // If the backend API is GL, copies a snapshot of the GrGLTextureInfo struct into the passed in
294
    // pointer and returns true. Otherwise returns false if the backend API is not GL.
295
    bool getGLTextureInfo(GrGLTextureInfo*) const;
296
297
    // Call this to indicate that the texture parameters have been modified in the GL context
298
    // externally to GrContext.
299
    void glTextureParametersModified();
300
301
#ifdef SK_DAWN
302
    // If the backend API is Dawn, copies a snapshot of the GrDawnTextureInfo struct into the passed
303
    // in pointer and returns true. Otherwise returns false if the backend API is not Dawn.
304
    bool getDawnTextureInfo(GrDawnTextureInfo*) const;
305
#endif
306
307
    // If the backend API is Vulkan, copies a snapshot of the GrVkImageInfo struct into the passed
308
    // in pointer and returns true. This snapshot will set the fImageLayout to the current layout
309
    // state. Otherwise returns false if the backend API is not Vulkan.
310
    bool getVkImageInfo(GrVkImageInfo*) const;
311
312
    // Anytime the client changes the VkImageLayout of the VkImage captured by this
313
    // GrBackendTexture, they must call this function to notify Skia of the changed layout.
314
    void setVkImageLayout(VkImageLayout);
315
316
#ifdef SK_METAL
317
    // If the backend API is Metal, copies a snapshot of the GrMtlTextureInfo struct into the passed
318
    // in pointer and returns true. Otherwise returns false if the backend API is not Metal.
319
    bool getMtlTextureInfo(GrMtlTextureInfo*) const;
320
#endif
321
322
#ifdef SK_DIRECT3D
323
    // If the backend API is Direct3D, copies a snapshot of the GrD3DTextureResourceInfo struct into
324
    // the passed in pointer and returns true. This snapshot will set the fResourceState to the
325
    // current resource state. Otherwise returns false if the backend API is not D3D.
326
    bool getD3DTextureResourceInfo(GrD3DTextureResourceInfo*) const;
327
328
    // Anytime the client changes the D3D12_RESOURCE_STATES of the D3D12_RESOURCE captured by this
329
    // GrBackendTexture, they must call this function to notify Skia of the changed layout.
330
    void setD3DResourceState(GrD3DResourceStateEnum);
331
#endif
332
333
    // Get the GrBackendFormat for this texture (or an invalid format if this is not valid).
334
    GrBackendFormat getBackendFormat() const;
335
336
    // If the backend API is Mock, copies a snapshot of the GrMockTextureInfo struct into the passed
337
    // in pointer and returns true. Otherwise returns false if the backend API is not Mock.
338
    bool getMockTextureInfo(GrMockTextureInfo*) const;
339
340
    // If the client changes any of the mutable backend of the GrBackendTexture they should call
341
    // this function to inform Skia that those values have changed. The backend API specific state
342
    // that can be set from this function are:
343
    //
344
    // Vulkan: VkImageLayout and QueueFamilyIndex
345
    void setMutableState(const GrBackendSurfaceMutableState&);
346
347
    // Returns true if we are working with protected content.
348
    bool isProtected() const;
349
350
    // Returns true if the backend texture has been initialized.
351
0
    bool isValid() const { return fIsValid; }
352
353
    // Returns true if both textures are valid and refer to the same API texture.
354
    bool isSameTexture(const GrBackendTexture&);
355
356
#if GR_TEST_UTILS
357
    static bool TestingOnly_Equals(const GrBackendTexture& , const GrBackendTexture&);
358
#endif
359
360
private:
361
    friend class GrVkGpu;  // for getMutableState
362
    sk_sp<GrBackendSurfaceMutableStateImpl> getMutableState() const;
363
364
#ifdef SK_GL
365
    friend class GrGLTexture;
366
    friend class GrGLGpu;    // for getGLTextureParams
367
    GrBackendTexture(int width,
368
                     int height,
369
                     GrMipmapped,
370
                     const GrGLTextureInfo,
371
                     sk_sp<GrGLTextureParameters>);
372
    sk_sp<GrGLTextureParameters> getGLTextureParams() const;
373
#endif
374
375
#ifdef SK_VULKAN
376
    friend class GrVkTexture;
377
    GrBackendTexture(int width,
378
                     int height,
379
                     const GrVkImageInfo& vkInfo,
380
                     sk_sp<GrBackendSurfaceMutableStateImpl> mutableState);
381
#endif
382
383
#ifdef SK_DIRECT3D
384
    friend class GrD3DTexture;
385
    friend class GrD3DGpu;     // for getGrD3DResourceState
386
    GrBackendTexture(int width,
387
                     int height,
388
                     const GrD3DTextureResourceInfo& vkInfo,
389
                     sk_sp<GrD3DResourceState> state);
390
    sk_sp<GrD3DResourceState> getGrD3DResourceState() const;
391
#endif
392
393
    // Free and release and resources being held by the GrBackendTexture.
394
    void cleanup();
395
396
    bool fIsValid;
397
    int fWidth;         //<! width in pixels
398
    int fHeight;        //<! height in pixels
399
    GrMipmapped fMipmapped;
400
    GrBackendApi fBackend;
401
    GrTextureType fTextureType;
402
403
    union {
404
#ifdef SK_GL
405
        GrGLBackendTextureInfo fGLInfo;
406
#endif
407
        GrVkBackendSurfaceInfo fVkInfo;
408
        GrMockTextureInfo fMockInfo;
409
#ifdef SK_DIRECT3D
410
        GrD3DBackendSurfaceInfo fD3DInfo;
411
#endif
412
    };
413
#ifdef SK_METAL
414
    GrMtlTextureInfo fMtlInfo;
415
#endif
416
#ifdef SK_DAWN
417
    GrDawnTextureInfo fDawnInfo;
418
#endif
419
420
    sk_sp<GrBackendSurfaceMutableStateImpl> fMutableState;
421
};
422
423
class SK_API GrBackendRenderTarget {
424
public:
425
    // Creates an invalid backend texture.
426
    GrBackendRenderTarget();
427
428
    // The GrGLTextureInfo must have a valid fFormat. If wrapping in an SkSurface we require the
429
    // stencil bits to be either 0, 8 or 16.
430
    GrBackendRenderTarget(int width,
431
                          int height,
432
                          int sampleCnt,
433
                          int stencilBits,
434
                          const GrGLFramebufferInfo& glInfo);
435
436
#ifdef SK_DAWN
437
    // If wrapping in an SkSurface we require the stencil bits to be either 0, 8 or 16.
438
    GrBackendRenderTarget(int width,
439
                          int height,
440
                          int sampleCnt,
441
                          int stencilBits,
442
                          const GrDawnRenderTargetInfo& dawnInfo);
443
#endif
444
445
#ifdef SK_VULKAN
446
    /** Deprecated. Sample count is now part of GrVkImageInfo. */
447
    GrBackendRenderTarget(int width, int height, int sampleCnt, const GrVkImageInfo& vkInfo);
448
449
    GrBackendRenderTarget(int width, int height, const GrVkImageInfo& vkInfo);
450
#endif
451
452
#ifdef SK_METAL
453
    GrBackendRenderTarget(int width,
454
                          int height,
455
                          const GrMtlTextureInfo& mtlInfo);
456
    /** Deprecated. Sample count is ignored and is instead retrieved from the MtlTexture. */
457
    GrBackendRenderTarget(int width,
458
                          int height,
459
                          int sampleCnt,
460
                          const GrMtlTextureInfo& mtlInfo);
461
#endif
462
463
#ifdef SK_DIRECT3D
464
    GrBackendRenderTarget(int width,
465
                          int height,
466
                          const GrD3DTextureResourceInfo& d3dInfo);
467
#endif
468
469
    GrBackendRenderTarget(int width,
470
                          int height,
471
                          int sampleCnt,
472
                          int stencilBits,
473
                          const GrMockRenderTargetInfo& mockInfo);
474
475
    ~GrBackendRenderTarget();
476
477
    GrBackendRenderTarget(const GrBackendRenderTarget& that);
478
    GrBackendRenderTarget& operator=(const GrBackendRenderTarget&);
479
480
0
    SkISize dimensions() const { return {fWidth, fHeight}; }
481
0
    int width() const { return fWidth; }
482
0
    int height() const { return fHeight; }
483
0
    int sampleCnt() const { return fSampleCnt; }
484
0
    int stencilBits() const { return fStencilBits; }
485
0
    GrBackendApi backend() const {return fBackend; }
486
0
    bool isFramebufferOnly() const { return fFramebufferOnly; }
487
488
    // If the backend API is GL, copies a snapshot of the GrGLFramebufferInfo struct into the passed
489
    // in pointer and returns true. Otherwise returns false if the backend API is not GL.
490
    bool getGLFramebufferInfo(GrGLFramebufferInfo*) const;
491
492
#ifdef SK_DAWN
493
    // If the backend API is Dawn, copies a snapshot of the GrDawnRenderTargetInfo struct into the
494
    // passed-in pointer and returns true. Otherwise returns false if the backend API is not Dawn.
495
    bool getDawnRenderTargetInfo(GrDawnRenderTargetInfo*) const;
496
#endif
497
498
    // If the backend API is Vulkan, copies a snapshot of the GrVkImageInfo struct into the passed
499
    // in pointer and returns true. This snapshot will set the fImageLayout to the current layout
500
    // state. Otherwise returns false if the backend API is not Vulkan.
501
    bool getVkImageInfo(GrVkImageInfo*) const;
502
503
    // Anytime the client changes the VkImageLayout of the VkImage captured by this
504
    // GrBackendRenderTarget, they must call this function to notify Skia of the changed layout.
505
    void setVkImageLayout(VkImageLayout);
506
507
#ifdef SK_METAL
508
    // If the backend API is Metal, copies a snapshot of the GrMtlTextureInfo struct into the passed
509
    // in pointer and returns true. Otherwise returns false if the backend API is not Metal.
510
    bool getMtlTextureInfo(GrMtlTextureInfo*) const;
511
#endif
512
513
#ifdef SK_DIRECT3D
514
    // If the backend API is Direct3D, copies a snapshot of the GrMtlTextureInfo struct into the
515
    // passed in pointer and returns true. Otherwise returns false if the backend API is not D3D.
516
    bool getD3DTextureResourceInfo(GrD3DTextureResourceInfo*) const;
517
518
    // Anytime the client changes the D3D12_RESOURCE_STATES of the D3D12_RESOURCE captured by this
519
    // GrBackendTexture, they must call this function to notify Skia of the changed layout.
520
    void setD3DResourceState(GrD3DResourceStateEnum);
521
#endif
522
523
    // Get the GrBackendFormat for this render target (or an invalid format if this is not valid).
524
    GrBackendFormat getBackendFormat() const;
525
526
    // If the backend API is Mock, copies a snapshot of the GrMockTextureInfo struct into the passed
527
    // in pointer and returns true. Otherwise returns false if the backend API is not Mock.
528
    bool getMockRenderTargetInfo(GrMockRenderTargetInfo*) const;
529
530
    // If the client changes any of the mutable backend of the GrBackendTexture they should call
531
    // this function to inform Skia that those values have changed. The backend API specific state
532
    // that can be set from this function are:
533
    //
534
    // Vulkan: VkImageLayout and QueueFamilyIndex
535
    void setMutableState(const GrBackendSurfaceMutableState&);
536
537
    // Returns true if we are working with protected content.
538
    bool isProtected() const;
539
540
    // Returns true if the backend texture has been initialized.
541
0
    bool isValid() const { return fIsValid; }
542
543
544
#if GR_TEST_UTILS
545
    static bool TestingOnly_Equals(const GrBackendRenderTarget&, const GrBackendRenderTarget&);
546
#endif
547
548
private:
549
    friend class GrVkGpu; // for getMutableState
550
    sk_sp<GrBackendSurfaceMutableStateImpl> getMutableState() const;
551
552
#ifdef SK_VULKAN
553
    friend class GrVkRenderTarget;
554
    GrBackendRenderTarget(int width,
555
                          int height,
556
                          const GrVkImageInfo& vkInfo,
557
                          sk_sp<GrBackendSurfaceMutableStateImpl> mutableState);
558
#endif
559
560
#ifdef SK_DIRECT3D
561
    friend class GrD3DGpu;
562
    friend class GrD3DRenderTarget;
563
    GrBackendRenderTarget(int width,
564
                          int height,
565
                          const GrD3DTextureResourceInfo& d3dInfo,
566
                          sk_sp<GrD3DResourceState> state);
567
    sk_sp<GrD3DResourceState> getGrD3DResourceState() const;
568
#endif
569
570
    // Free and release and resources being held by the GrBackendTexture.
571
    void cleanup();
572
573
    bool fIsValid;
574
    bool fFramebufferOnly = false;
575
    int fWidth;         //<! width in pixels
576
    int fHeight;        //<! height in pixels
577
578
    int fSampleCnt;
579
    int fStencilBits;
580
581
    GrBackendApi fBackend;
582
583
    union {
584
#ifdef SK_GL
585
        GrGLFramebufferInfo fGLInfo;
586
#endif
587
        GrVkBackendSurfaceInfo fVkInfo;
588
        GrMockRenderTargetInfo fMockInfo;
589
#ifdef SK_DIRECT3D
590
        GrD3DBackendSurfaceInfo fD3DInfo;
591
#endif
592
    };
593
#ifdef SK_METAL
594
    GrMtlTextureInfo fMtlInfo;
595
#endif
596
#ifdef SK_DAWN
597
    GrDawnRenderTargetInfo  fDawnInfo;
598
#endif
599
    sk_sp<GrBackendSurfaceMutableStateImpl> fMutableState;
600
};
601
602
#endif
603
604
#endif
605