1 /* 2 * Copyright 2019 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 GrSurfaceProxyView_DEFINED 9 #define GrSurfaceProxyView_DEFINED 10 11 #include "include/core/SkRefCnt.h" 12 #include "include/core/SkSize.h" 13 #include "include/core/SkTypes.h" 14 #include "include/gpu/ganesh/GrTypes.h" 15 #include "include/private/base/SkTo.h" 16 #include "include/private/base/SkTypeTraits.h" 17 #include "src/gpu/Swizzle.h" 18 #include "src/gpu/ganesh/GrSurfaceProxy.h" 19 20 #include <string_view> 21 #include <type_traits> 22 #include <utility> 23 24 class GrRecordingContext; 25 class GrRenderTargetProxy; 26 class GrTextureProxy; 27 enum class SkBackingFit; 28 struct SkIRect; 29 namespace skgpu { 30 enum class Budgeted : bool; 31 enum class Mipmapped : bool; 32 } 33 34 class GrSurfaceProxyView { 35 public: 36 GrSurfaceProxyView() = default; 37 GrSurfaceProxyView(sk_sp<GrSurfaceProxy> proxy,GrSurfaceOrigin origin,skgpu::Swizzle swizzle)38 GrSurfaceProxyView(sk_sp<GrSurfaceProxy> proxy, GrSurfaceOrigin origin, skgpu::Swizzle swizzle) 39 : fProxy(std::move(proxy)), fOrigin(origin), fSwizzle(swizzle) {} 40 41 // This entry point is used when we don't care about the origin or the swizzle. GrSurfaceProxyView(sk_sp<GrSurfaceProxy> proxy)42 explicit GrSurfaceProxyView(sk_sp<GrSurfaceProxy> proxy) 43 : fProxy(std::move(proxy)), fOrigin(kTopLeft_GrSurfaceOrigin) {} 44 45 GrSurfaceProxyView(GrSurfaceProxyView&& view) = default; 46 GrSurfaceProxyView(const GrSurfaceProxyView&) = default; 47 48 explicit operator bool() const { return SkToBool(fProxy.get()); } 49 50 GrSurfaceProxyView& operator=(const GrSurfaceProxyView&) = default; 51 GrSurfaceProxyView& operator=(GrSurfaceProxyView&& view) = default; 52 53 bool operator==(const GrSurfaceProxyView& view) const; 54 bool operator!=(const GrSurfaceProxyView& other) const { return !(*this == other); } 55 width()56 int width() const { return this->proxy()->width(); } height()57 int height() const { return this->proxy()->height(); } dimensions()58 SkISize dimensions() const { return this->proxy()->dimensions(); } 59 60 skgpu::Mipmapped mipmapped() const; 61 proxy()62 GrSurfaceProxy* proxy() const { return fProxy.get(); } refProxy()63 sk_sp<GrSurfaceProxy> refProxy() const { return fProxy; } 64 65 GrTextureProxy* asTextureProxy() const; 66 sk_sp<GrTextureProxy> asTextureProxyRef() const; 67 68 GrRenderTargetProxy* asRenderTargetProxy() const; 69 sk_sp<GrRenderTargetProxy> asRenderTargetProxyRef() const; 70 origin()71 GrSurfaceOrigin origin() const { return fOrigin; } swizzle()72 skgpu::Swizzle swizzle() const { return fSwizzle; } 73 74 void concatSwizzle(skgpu::Swizzle swizzle); 75 76 GrSurfaceProxyView makeSwizzle(skgpu::Swizzle swizzle) const&; 77 78 GrSurfaceProxyView makeSwizzle(skgpu::Swizzle swizzle) &&; 79 80 void reset(); 81 82 // Helper that copies a rect of a src view's proxy and then creates a view for the copy with 83 // the same origin and swizzle as the src view. 84 static GrSurfaceProxyView Copy(GrRecordingContext* context, 85 GrSurfaceProxyView src, 86 skgpu::Mipmapped mipmapped, 87 SkIRect srcRect, 88 SkBackingFit fit, 89 skgpu::Budgeted budgeted, 90 std::string_view label); 91 92 static GrSurfaceProxyView Copy(GrRecordingContext* rContext, 93 GrSurfaceProxyView src, 94 skgpu::Mipmapped mipmapped, 95 SkBackingFit fit, 96 skgpu::Budgeted budgeted, 97 std::string_view label); 98 99 // This does not reset the origin or swizzle, so the View can still be used to access those 100 // properties associated with the detached proxy. detachProxy()101 sk_sp<GrSurfaceProxy> detachProxy() { return std::move(fProxy); } 102 103 using sk_is_trivially_relocatable = std::true_type; 104 105 private: 106 sk_sp<GrSurfaceProxy> fProxy; 107 GrSurfaceOrigin fOrigin = kTopLeft_GrSurfaceOrigin; 108 skgpu::Swizzle fSwizzle; 109 110 static_assert(::sk_is_trivially_relocatable<decltype(fProxy)>::value); 111 static_assert(::sk_is_trivially_relocatable<decltype(fOrigin)>::value); 112 static_assert(::sk_is_trivially_relocatable<decltype(fSwizzle)>::value); 113 }; 114 115 #endif 116