Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 6c6caf4

Browse files
egdanielSkia Commit-Bot
authored andcommitted
Add GrBackendMutableState object to handle shared texture state.
This is will be the main struct used to synchronize changes of certain texture/image between clients and Skia. With this change we implement support for the Vulkan shared state as POC. Bug: skia:10254 Change-Id: I10543357635c347838b193874e4da4496a0dcf06 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/292311 Commit-Queue: Greg Daniel <[email protected]> Reviewed-by: Brian Salomon <[email protected]>
1 parent c453a50 commit 6c6caf4

21 files changed

+592
-288
lines changed

gn/gpu.gni

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ skia_gpu_sources = [
1111
"$_include/gpu/GrBackendDrawableInfo.h",
1212
"$_include/gpu/GrBackendSemaphore.h",
1313
"$_include/gpu/GrBackendSurface.h",
14+
"$_include/gpu/GrBackendSurfaceMutableState.h",
1415
"$_include/gpu/GrConfig.h",
1516
"$_include/gpu/GrContext.h",
1617
"$_include/gpu/GrContextOptions.h",
@@ -35,6 +36,7 @@ skia_gpu_sources = [
3536
"$_src/gpu/GrAuditTrail.h",
3637
"$_src/gpu/GrAutoLocaleSetter.h",
3738
"$_src/gpu/GrBackendSurface.cpp",
39+
"$_src/gpu/GrBackendSurfaceMutableStateImpl.h",
3840
"$_src/gpu/GrBackendTextureImageGenerator.cpp",
3941
"$_src/gpu/GrBackendTextureImageGenerator.h",
4042
"$_src/gpu/GrBaseContextPriv.h",

gn/tests.gni

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ tests_sources = [
1717
"$_tests/AsADashTest.cpp",
1818
"$_tests/BRDTest.cpp",
1919
"$_tests/BackendAllocationTest.cpp",
20+
"$_tests/BackendSurfaceMutableStateTest.cpp",
2021
"$_tests/BadIcoTest.cpp",
2122
"$_tests/BitSetTest.cpp",
2223
"$_tests/BitmapCopyTest.cpp",

include/gpu/GrBackendSurface.h

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#ifndef GrBackendSurface_DEFINED
99
#define GrBackendSurface_DEFINED
1010

11+
#include "include/gpu/GrBackendSurfaceMutableState.h"
1112
#include "include/gpu/GrTypes.h"
1213
#include "include/gpu/gl/GrGLTypes.h"
1314
#include "include/gpu/mock/GrMockTypes.h"
@@ -19,6 +20,7 @@
1920
#include "include/gpu/dawn/GrDawnTypes.h"
2021
#endif
2122

23+
class GrBackendSurfaceMutableStateImpl;
2224
class GrVkImageLayout;
2325
class GrGLTextureParameters;
2426

@@ -224,17 +226,19 @@ class SK_API GrBackendFormat {
224226
class SK_API GrBackendTexture {
225227
public:
226228
// Creates an invalid backend texture.
227-
GrBackendTexture() : fIsValid(false) {}
229+
GrBackendTexture();
228230

229231
// The GrGLTextureInfo must have a valid fFormat.
230232
GrBackendTexture(int width,
231233
int height,
232234
GrMipMapped,
233235
const GrGLTextureInfo& glInfo);
234236

237+
#ifdef SK_VULKAN
235238
GrBackendTexture(int width,
236239
int height,
237240
const GrVkImageInfo& vkInfo);
241+
#endif
238242

239243
#ifdef SK_METAL
240244
GrBackendTexture(int width,
@@ -286,6 +290,7 @@ class SK_API GrBackendTexture {
286290
bool getDawnTextureInfo(GrDawnTextureInfo*) const;
287291
#endif
288292

293+
#ifdef SK_VULKAN
289294
// If the backend API is Vulkan, copies a snapshot of the GrVkImageInfo struct into the passed
290295
// in pointer and returns true. This snapshot will set the fImageLayout to the current layout
291296
// state. Otherwise returns false if the backend API is not Vulkan.
@@ -294,6 +299,7 @@ class SK_API GrBackendTexture {
294299
// Anytime the client changes the VkImageLayout of the VkImage captured by this
295300
// GrBackendTexture, they must call this function to notify Skia of the changed layout.
296301
void setVkImageLayout(VkImageLayout);
302+
#endif
297303

298304
#ifdef SK_METAL
299305
// If the backend API is Metal, copies a snapshot of the GrMtlTextureInfo struct into the passed
@@ -319,6 +325,13 @@ class SK_API GrBackendTexture {
319325
// in pointer and returns true. Otherwise returns false if the backend API is not Mock.
320326
bool getMockTextureInfo(GrMockTextureInfo*) const;
321327

328+
// If the client changes any of the mutable backend of the GrBackendTexture they should call
329+
// this function to inform Skia that those values have changed. The backend API specific state
330+
// that can be set from this function are:
331+
//
332+
// Vulkan: VkImageLayout and QueueFamilyIndex
333+
void setMutableState(const GrBackendSurfaceMutableState&);
334+
322335
// Returns true if we are working with protected content.
323336
bool isProtected() const;
324337

@@ -333,6 +346,8 @@ class SK_API GrBackendTexture {
333346
#endif
334347

335348
private:
349+
friend class GrVkGpu; // for getMutableState
350+
sk_sp<GrBackendSurfaceMutableStateImpl> getMutableState() const;
336351

337352
#ifdef SK_GL
338353
friend class GrGLTexture;
@@ -347,12 +362,10 @@ class SK_API GrBackendTexture {
347362

348363
#ifdef SK_VULKAN
349364
friend class GrVkTexture;
350-
friend class GrVkGpu; // for getGrVkImageLayout
351365
GrBackendTexture(int width,
352366
int height,
353367
const GrVkImageInfo& vkInfo,
354-
sk_sp<GrVkImageLayout> layout);
355-
sk_sp<GrVkImageLayout> getGrVkImageLayout() const;
368+
sk_sp<GrBackendSurfaceMutableStateImpl> mutableState);
356369
#endif
357370

358371
#ifdef SK_DIRECT3D
@@ -390,12 +403,14 @@ class SK_API GrBackendTexture {
390403
#ifdef SK_DAWN
391404
GrDawnTextureInfo fDawnInfo;
392405
#endif
406+
407+
sk_sp<GrBackendSurfaceMutableStateImpl> fMutableState;
393408
};
394409

395410
class SK_API GrBackendRenderTarget {
396411
public:
397412
// Creates an invalid backend texture.
398-
GrBackendRenderTarget() : fIsValid(false) {}
413+
GrBackendRenderTarget();
399414

400415
// The GrGLTextureInfo must have a valid fFormat.
401416
GrBackendRenderTarget(int width,
@@ -412,13 +427,15 @@ class SK_API GrBackendRenderTarget {
412427
const GrDawnRenderTargetInfo& dawnInfo);
413428
#endif
414429

430+
#ifdef SK_VULKAN
415431
/** Deprecated, use version that does not take stencil bits. */
416432
GrBackendRenderTarget(int width,
417433
int height,
418434
int sampleCnt,
419435
int stencilBits,
420436
const GrVkImageInfo& vkInfo);
421437
GrBackendRenderTarget(int width, int height, int sampleCnt, const GrVkImageInfo& vkInfo);
438+
#endif
422439

423440
#ifdef SK_METAL
424441
GrBackendRenderTarget(int width,
@@ -463,6 +480,7 @@ class SK_API GrBackendRenderTarget {
463480
bool getDawnRenderTargetInfo(GrDawnRenderTargetInfo*) const;
464481
#endif
465482

483+
#ifdef SK_VULKAN
466484
// If the backend API is Vulkan, copies a snapshot of the GrVkImageInfo struct into the passed
467485
// in pointer and returns true. This snapshot will set the fImageLayout to the current layout
468486
// state. Otherwise returns false if the backend API is not Vulkan.
@@ -471,6 +489,7 @@ class SK_API GrBackendRenderTarget {
471489
// Anytime the client changes the VkImageLayout of the VkImage captured by this
472490
// GrBackendRenderTarget, they must call this function to notify Skia of the changed layout.
473491
void setVkImageLayout(VkImageLayout);
492+
#endif
474493

475494
#ifdef SK_METAL
476495
// If the backend API is Metal, copies a snapshot of the GrMtlTextureInfo struct into the passed
@@ -495,6 +514,13 @@ class SK_API GrBackendRenderTarget {
495514
// in pointer and returns true. Otherwise returns false if the backend API is not Mock.
496515
bool getMockRenderTargetInfo(GrMockRenderTargetInfo*) const;
497516

517+
// If the client changes any of the mutable backend of the GrBackendTexture they should call
518+
// this function to inform Skia that those values have changed. The backend API specific state
519+
// that can be set from this function are:
520+
//
521+
// Vulkan: VkImageLayout and QueueFamilyIndex
522+
void setMutableState(const GrBackendSurfaceMutableState&);
523+
498524
// Returns true if we are working with protected content.
499525
bool isProtected() const;
500526

@@ -507,12 +533,15 @@ class SK_API GrBackendRenderTarget {
507533
#endif
508534

509535
private:
510-
friend class GrVkGpu; // for getGrVkImageLayout
511-
sk_sp<GrVkImageLayout> getGrVkImageLayout() const;
536+
friend class GrVkGpu; // for getMutableState
537+
sk_sp<GrBackendSurfaceMutableStateImpl> getMutableState() const;
512538

539+
#ifdef SK_VULKAN
513540
friend class GrVkRenderTarget;
514541
GrBackendRenderTarget(int width, int height, int sampleCnt, const GrVkImageInfo& vkInfo,
515-
sk_sp<GrVkImageLayout> layout);
542+
sk_sp<GrBackendSurfaceMutableStateImpl> mutableState);
543+
#endif
544+
516545
#ifdef SK_DIRECT3D
517546
friend class GrD3DGpu;
518547
friend class GrD3DRenderTarget;
@@ -550,6 +579,7 @@ class SK_API GrBackendRenderTarget {
550579
#ifdef SK_DAWN
551580
GrDawnRenderTargetInfo fDawnInfo;
552581
#endif
582+
sk_sp<GrBackendSurfaceMutableStateImpl> fMutableState;
553583
};
554584

555585
#endif
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2020 Google LLC
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 GrBackendSurfaceMutableState_DEFINED
9+
#define GrBackendSurfaceMutableState_DEFINED
10+
11+
#include "include/gpu/GrTypes.h"
12+
13+
#ifdef SK_VULKAN
14+
#include "include/private/GrVkTypesPriv.h"
15+
#endif
16+
17+
/**
18+
* Since Skia and clients can both modify gpu textures and their connected state, Skia needs a way
19+
* for clients to inform us if they have modifiend any of this state. In order to not need setters
20+
* for every single API and state, we use this class to be a generic wrapper around all the mutable
21+
* state. This class is used for calls that inform Skia of these texture/image state changes by the
22+
* client as well as for requesting state changes to be done by Skia. The backend specific state
23+
* that is wrapped by this class are:
24+
*
25+
* Vulkan: VkImageLayout and QueueFamilyIndex
26+
*/
27+
class GrBackendSurfaceMutableState {
28+
public:
29+
#ifdef SK_VULKAN
30+
GrBackendSurfaceMutableState(VkImageLayout layout, uint32_t queueFamilyIndex)
31+
: fVkState(layout, queueFamilyIndex)
32+
, fBackend(GrBackend::kVulkan) {}
33+
#endif
34+
35+
GrBackendSurfaceMutableState& operator=(const GrBackendSurfaceMutableState& that) {
36+
switch (fBackend) {
37+
case GrBackend::kVulkan:
38+
#ifdef SK_VULKAN
39+
SkASSERT(that.fBackend == GrBackend::kVulkan);
40+
fVkState = that.fVkState;
41+
#endif
42+
break;
43+
44+
default:
45+
(void)that;
46+
SkUNREACHABLE;
47+
}
48+
fBackend = that.fBackend;
49+
return *this;
50+
}
51+
52+
private:
53+
friend class GrBackendSurfaceMutableStateImpl;
54+
55+
union {
56+
char fDummy;
57+
#ifdef SK_VULKAN
58+
GrVkSharedImageInfo fVkState;
59+
#endif
60+
};
61+
62+
GrBackend fBackend;
63+
};
64+
65+
#endif

include/gpu/vk/GrVkTypes.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,14 @@ struct GrVkImageInfo {
190190
, fProtected(isProtected)
191191
, fYcbcrConversionInfo(ycbcrConversionInfo) {}
192192

193-
GrVkImageInfo(const GrVkImageInfo& info, VkImageLayout layout)
193+
GrVkImageInfo(const GrVkImageInfo& info, VkImageLayout layout, uint32_t familyQueueIndex)
194194
: fImage(info.fImage)
195195
, fAlloc(info.fAlloc)
196196
, fImageTiling(info.fImageTiling)
197197
, fImageLayout(layout)
198198
, fFormat(info.fFormat)
199199
, fLevelCount(info.fLevelCount)
200-
, fCurrentQueueFamily(info.fCurrentQueueFamily)
200+
, fCurrentQueueFamily(familyQueueIndex)
201201
, fProtected(info.fProtected)
202202
, fYcbcrConversionInfo(info.fYcbcrConversionInfo) {}
203203

include/private/GrVkTypesPriv.h

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "include/core/SkRefCnt.h"
1212
#include "include/gpu/vk/GrVkTypes.h"
1313

14-
class GrVkImageLayout;
14+
class GrBackendSurfaceMutableStateImpl;
1515

1616
// This struct is to used to store the the actual information about the vulkan backend image on the
1717
// GrBackendTexture and GrBackendRenderTarget. When a client calls getVkImageInfo on a
@@ -20,8 +20,7 @@ class GrVkImageLayout;
2020
// current VkImageLayout which can be shared with an internal GrVkImage so that layout updates can
2121
// be seen by all users of the image.
2222
struct GrVkBackendSurfaceInfo {
23-
GrVkBackendSurfaceInfo(GrVkImageInfo info, GrVkImageLayout* layout)
24-
: fImageInfo(info), fLayout(layout) {}
23+
GrVkBackendSurfaceInfo(GrVkImageInfo info) : fImageInfo(info) {}
2524

2625
void cleanup();
2726

@@ -31,11 +30,7 @@ struct GrVkBackendSurfaceInfo {
3130
// attempt to unref the old fLayout on this object.
3231
void assign(const GrVkBackendSurfaceInfo&, bool isValid);
3332

34-
void setImageLayout(VkImageLayout layout);
35-
36-
sk_sp<GrVkImageLayout> getGrVkImageLayout() const;
37-
38-
GrVkImageInfo snapImageInfo() const;
33+
GrVkImageInfo snapImageInfo(const GrBackendSurfaceMutableStateImpl*) const;
3934

4035
bool isProtected() const { return fImageInfo.fProtected == GrProtected::kYes; }
4136
#if GR_TEST_UTILS
@@ -44,7 +39,44 @@ struct GrVkBackendSurfaceInfo {
4439

4540
private:
4641
GrVkImageInfo fImageInfo;
47-
GrVkImageLayout* fLayout;
42+
};
43+
44+
class GrVkSharedImageInfo {
45+
public:
46+
GrVkSharedImageInfo(VkImageLayout layout, uint32_t queueFamilyIndex)
47+
: fLayout(layout)
48+
, fQueueFamilyIndex(queueFamilyIndex) {}
49+
50+
GrVkSharedImageInfo& operator=(const GrVkSharedImageInfo& that) {
51+
fLayout = that.getImageLayout();
52+
fQueueFamilyIndex = that.getQueueFamilyIndex();
53+
return *this;
54+
}
55+
56+
57+
void setImageLayout(VkImageLayout layout) {
58+
// Defaulting to use std::memory_order_seq_cst
59+
fLayout.store(layout);
60+
}
61+
62+
VkImageLayout getImageLayout() const {
63+
// Defaulting to use std::memory_order_seq_cst
64+
return fLayout.load();
65+
}
66+
67+
void setQueueFamilyIndex(uint32_t queueFamilyIndex) {
68+
// Defaulting to use std::memory_order_seq_cst
69+
fQueueFamilyIndex.store(queueFamilyIndex);
70+
}
71+
72+
uint32_t getQueueFamilyIndex() const {
73+
// Defaulting to use std::memory_order_seq_cst
74+
return fQueueFamilyIndex.load();
75+
}
76+
77+
private:
78+
std::atomic<VkImageLayout> fLayout;
79+
std::atomic<uint32_t> fQueueFamilyIndex;
4880
};
4981

5082
#endif

0 commit comments

Comments
 (0)