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/GrSwizzle.h" 16 #include "src/gpu/GrTextureProxy.h" 17 18 class GrSurfaceProxyView { 19 public: 20 GrSurfaceProxyView() = default; 21 GrSurfaceProxyView(sk_sp<GrSurfaceProxy> proxy,GrSurfaceOrigin origin,GrSwizzle swizzle)22 GrSurfaceProxyView(sk_sp<GrSurfaceProxy> proxy, GrSurfaceOrigin origin, GrSwizzle 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 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 proxy()48 GrSurfaceProxy* proxy() const { return fProxy.get(); } refProxy()49 sk_sp<GrSurfaceProxy> refProxy() const { return fProxy; } 50 asTextureProxy()51 GrTextureProxy* asTextureProxy() const { 52 if (!fProxy) { 53 return nullptr; 54 } 55 return fProxy->asTextureProxy(); 56 } asTextureProxyRef()57 sk_sp<GrTextureProxy> asTextureProxyRef() const { 58 return sk_ref_sp<GrTextureProxy>(this->asTextureProxy()); 59 } 60 asRenderTargetProxy()61 GrRenderTargetProxy* asRenderTargetProxy() const { 62 if (!fProxy) { 63 return nullptr; 64 } 65 return fProxy->asRenderTargetProxy(); 66 } 67 asRenderTargetProxyRef()68 sk_sp<GrRenderTargetProxy> asRenderTargetProxyRef() const { 69 return sk_ref_sp<GrRenderTargetProxy>(this->asRenderTargetProxy()); 70 } 71 origin()72 GrSurfaceOrigin origin() const { return fOrigin; } swizzle()73 GrSwizzle swizzle() const { return fSwizzle; } 74 concatSwizzle(GrSwizzle swizzle)75 void concatSwizzle(GrSwizzle swizzle) { fSwizzle = GrSwizzle::Concat(fSwizzle, swizzle); } 76 makeSwizzle(GrSwizzle swizzle)77 GrSurfaceProxyView makeSwizzle(GrSwizzle swizzle) const & { 78 return {fProxy, fOrigin, GrSwizzle::Concat(fSwizzle, swizzle)}; 79 } 80 makeSwizzle(GrSwizzle swizzle)81 GrSurfaceProxyView makeSwizzle(GrSwizzle swizzle) && { 82 return {std::move(fProxy), fOrigin, GrSwizzle::Concat(fSwizzle, swizzle)}; 83 } 84 reset()85 void reset() { 86 *this = {}; 87 } 88 89 // Helper that copies a rect of a src view'' proxy and then creates a view for the copy with 90 // the same origin and swizzle as the src view. Copy(GrRecordingContext * context,GrSurfaceProxyView src,GrMipmapped mipMapped,SkIRect srcRect,SkBackingFit fit,SkBudgeted budgeted)91 static GrSurfaceProxyView Copy(GrRecordingContext* context, 92 GrSurfaceProxyView src, 93 GrMipmapped mipMapped, 94 SkIRect srcRect, 95 SkBackingFit fit, 96 SkBudgeted budgeted) { 97 auto copy = GrSurfaceProxy::Copy(context, 98 src.refProxy(), 99 src.origin(), 100 mipMapped, 101 srcRect, 102 fit, 103 budgeted); 104 return {std::move(copy), src.origin(), src.swizzle()}; 105 } 106 Copy(GrRecordingContext * context,GrSurfaceProxyView src,GrMipmapped mipMapped,SkBackingFit fit,SkBudgeted budgeted)107 static GrSurfaceProxyView Copy(GrRecordingContext* context, 108 GrSurfaceProxyView src, 109 GrMipmapped mipMapped, 110 SkBackingFit fit, 111 SkBudgeted budgeted) { 112 auto copy = GrSurfaceProxy::Copy(context, 113 src.refProxy(), 114 src.origin(), 115 mipMapped, 116 fit, 117 budgeted); 118 return {std::move(copy), src.origin(), src.swizzle()}; 119 } 120 121 // This does not reset the origin or swizzle, so the View can still be used to access those 122 // properties associated with the detached proxy. detachProxy()123 sk_sp<GrSurfaceProxy> detachProxy() { 124 return std::move(fProxy); 125 } 126 127 private: 128 sk_sp<GrSurfaceProxy> fProxy; 129 GrSurfaceOrigin fOrigin = kTopLeft_GrSurfaceOrigin; 130 GrSwizzle fSwizzle; 131 }; 132 133 #endif 134 135