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/gpu/GrTypes.h" 13 #include "src/gpu/GrRenderTargetProxy.h" 14 #include "src/gpu/GrSurfaceProxy.h" 15 #include "src/gpu/GrTextureProxy.h" 16 #include "src/gpu/Swizzle.h" 17 18 class GrSurfaceProxyView { 19 public: 20 GrSurfaceProxyView() = default; 21 GrSurfaceProxyView(sk_sp<GrSurfaceProxy> proxy,GrSurfaceOrigin origin,skgpu::Swizzle swizzle)22 GrSurfaceProxyView(sk_sp<GrSurfaceProxy> proxy, GrSurfaceOrigin origin, skgpu::Swizzle swizzle) 23 : fProxy(std::move(proxy)), fOrigin(origin), fSwizzle(swizzle) {} 24 25 // This entry point is used when we don't care about the origin or the swizzle. GrSurfaceProxyView(sk_sp<GrSurfaceProxy> proxy)26 explicit GrSurfaceProxyView(sk_sp<GrSurfaceProxy> proxy) 27 : fProxy(std::move(proxy)), fOrigin(kTopLeft_GrSurfaceOrigin) {} 28 29 GrSurfaceProxyView(GrSurfaceProxyView&& view) = default; 30 GrSurfaceProxyView(const GrSurfaceProxyView&) = default; 31 32 explicit operator bool() const { return SkToBool(fProxy.get()); } 33 34 GrSurfaceProxyView& operator=(const GrSurfaceProxyView&) = default; 35 GrSurfaceProxyView& operator=(GrSurfaceProxyView&& view) = default; 36 37 bool operator==(const GrSurfaceProxyView& view) const { 38 return fProxy->uniqueID() == view.fProxy->uniqueID() && 39 fOrigin == view.fOrigin && 40 fSwizzle == view.fSwizzle; 41 } 42 bool operator!=(const GrSurfaceProxyView& other) const { return !(*this == other); } 43 width()44 int width() const { return this->proxy()->width(); } height()45 int height() const { return this->proxy()->height(); } dimensions()46 SkISize dimensions() const { return this->proxy()->dimensions(); } 47 mipmapped()48 GrMipmapped mipmapped() const { 49 if (const GrTextureProxy* proxy = this->asTextureProxy()) { 50 return proxy->mipmapped(); 51 } 52 return GrMipmapped::kNo; 53 } 54 proxy()55 GrSurfaceProxy* proxy() const { return fProxy.get(); } refProxy()56 sk_sp<GrSurfaceProxy> refProxy() const { return fProxy; } 57 asTextureProxy()58 GrTextureProxy* asTextureProxy() const { 59 if (!fProxy) { 60 return nullptr; 61 } 62 return fProxy->asTextureProxy(); 63 } asTextureProxyRef()64 sk_sp<GrTextureProxy> asTextureProxyRef() const { 65 return sk_ref_sp<GrTextureProxy>(this->asTextureProxy()); 66 } 67 asRenderTargetProxy()68 GrRenderTargetProxy* asRenderTargetProxy() const { 69 if (!fProxy) { 70 return nullptr; 71 } 72 return fProxy->asRenderTargetProxy(); 73 } 74 asRenderTargetProxyRef()75 sk_sp<GrRenderTargetProxy> asRenderTargetProxyRef() const { 76 return sk_ref_sp<GrRenderTargetProxy>(this->asRenderTargetProxy()); 77 } 78 origin()79 GrSurfaceOrigin origin() const { return fOrigin; } swizzle()80 skgpu::Swizzle swizzle() const { return fSwizzle; } 81 concatSwizzle(skgpu::Swizzle swizzle)82 void concatSwizzle(skgpu::Swizzle swizzle) { 83 fSwizzle = skgpu::Swizzle::Concat(fSwizzle, swizzle); 84 } 85 makeSwizzle(skgpu::Swizzle swizzle)86 GrSurfaceProxyView makeSwizzle(skgpu::Swizzle swizzle) const & { 87 return {fProxy, fOrigin, skgpu::Swizzle::Concat(fSwizzle, swizzle)}; 88 } 89 makeSwizzle(skgpu::Swizzle swizzle)90 GrSurfaceProxyView makeSwizzle(skgpu::Swizzle swizzle) && { 91 return {std::move(fProxy), fOrigin, skgpu::Swizzle::Concat(fSwizzle, swizzle)}; 92 } 93 reset()94 void reset() { 95 *this = {}; 96 } 97 98 // Helper that copies a rect of a src view'' proxy and then creates a view for the copy with 99 // the same origin and swizzle as the src view. Copy(GrRecordingContext * context,GrSurfaceProxyView src,GrMipmapped mipMapped,SkIRect srcRect,SkBackingFit fit,SkBudgeted budgeted)100 static GrSurfaceProxyView Copy(GrRecordingContext* context, 101 GrSurfaceProxyView src, 102 GrMipmapped mipMapped, 103 SkIRect srcRect, 104 SkBackingFit fit, 105 SkBudgeted budgeted) { 106 auto copy = GrSurfaceProxy::Copy(context, 107 src.refProxy(), 108 src.origin(), 109 mipMapped, 110 srcRect, 111 fit, 112 budgeted); 113 return {std::move(copy), src.origin(), src.swizzle()}; 114 } 115 Copy(GrRecordingContext * rContext,GrSurfaceProxyView src,GrMipmapped mipMapped,SkBackingFit fit,SkBudgeted budgeted)116 static GrSurfaceProxyView Copy(GrRecordingContext* rContext, 117 GrSurfaceProxyView src, 118 GrMipmapped mipMapped, 119 SkBackingFit fit, 120 SkBudgeted budgeted) { 121 auto copy = GrSurfaceProxy::Copy(rContext, 122 src.refProxy(), 123 src.origin(), 124 mipMapped, 125 fit, 126 budgeted); 127 return {std::move(copy), src.origin(), src.swizzle()}; 128 } 129 130 // This does not reset the origin or swizzle, so the View can still be used to access those 131 // properties associated with the detached proxy. detachProxy()132 sk_sp<GrSurfaceProxy> detachProxy() { 133 return std::move(fProxy); 134 } 135 136 private: 137 sk_sp<GrSurfaceProxy> fProxy; 138 GrSurfaceOrigin fOrigin = kTopLeft_GrSurfaceOrigin; 139 skgpu::Swizzle fSwizzle; 140 }; 141 142 #endif 143 144