1 /* 2 * Copyright 2016 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 GrSurfaceContext_DEFINED 9 #define GrSurfaceContext_DEFINED 10 11 #include "include/core/SkFilterQuality.h" 12 #include "include/core/SkRect.h" 13 #include "include/core/SkRefCnt.h" 14 #include "include/core/SkSurface.h" 15 #include "src/gpu/GrColorInfo.h" 16 #include "src/gpu/GrDataUtils.h" 17 #include "src/gpu/GrImageInfo.h" 18 #include "src/gpu/GrSurfaceProxy.h" 19 #include "src/gpu/GrSurfaceProxyView.h" 20 21 class GrAuditTrail; 22 class GrDrawingManager; 23 class GrRecordingContext; 24 class GrRenderTargetContext; 25 class GrRenderTargetProxy; 26 class GrSingleOwner; 27 class GrSurface; 28 class GrSurfaceContextPriv; 29 class GrSurfaceProxy; 30 class GrTextureProxy; 31 struct SkIPoint; 32 struct SkIRect; 33 34 /** 35 * A helper object to orchestrate commands for a particular surface 36 */ 37 class GrSurfaceContext { 38 public: 39 // If the passed in GrSurfaceProxy is renderable this will return a GrRenderTargetContext, 40 // otherwise it will return a GrSurfaceContext. 41 static std::unique_ptr<GrSurfaceContext> Make(GrRecordingContext*, 42 GrSurfaceProxyView readView, 43 GrColorType, SkAlphaType, sk_sp<SkColorSpace>); 44 45 static std::unique_ptr<GrSurfaceContext> Make(GrRecordingContext*, SkISize dimensions, 46 const GrBackendFormat&, GrRenderable, 47 int renderTargetSampleCnt, GrMipMapped, 48 GrProtected, GrSurfaceOrigin, GrColorType, 49 SkAlphaType, sk_sp<SkColorSpace>, SkBackingFit, 50 SkBudgeted); 51 52 // If it is known that the GrSurfaceProxy is not renderable, you can directly call the the ctor 53 // here to make a GrSurfaceContext on the stack. 54 GrSurfaceContext(GrRecordingContext*, GrSurfaceProxyView readView, GrColorType, SkAlphaType, 55 sk_sp<SkColorSpace>); 56 57 virtual ~GrSurfaceContext() = default; 58 colorInfo()59 const GrColorInfo& colorInfo() const { return fColorInfo; } imageInfo()60 GrImageInfo imageInfo() const { return {fColorInfo, fReadView.proxy()->dimensions()}; } 61 origin()62 GrSurfaceOrigin origin() const { return fReadView.origin(); } readSwizzle()63 GrSwizzle readSwizzle() const { return fReadView.swizzle(); } 64 // TODO: See if it makes sense for this to return a const& instead and require the callers to 65 // make a copy (which refs the proxy) if needed. readSurfaceView()66 GrSurfaceProxyView readSurfaceView() { return fReadView; } 67 width()68 int width() const { return fReadView.proxy()->width(); } height()69 int height() const { return fReadView.proxy()->height(); } 70 71 const GrCaps* caps() const; 72 73 /** 74 * Reads a rectangle of pixels from the render target context. 75 * @param dstInfo image info for the destination 76 * @param dst destination pixels for the read 77 * @param rowBytes bytes in a row of 'dst' 78 * @param srcPt offset w/in the surface context from which to read 79 * @param direct The direct context to use. If null will use our GrRecordingContext if it 80 * is a GrDirectContext and fail otherwise. 81 */ 82 bool readPixels(const GrImageInfo& dstInfo, void* dst, size_t rowBytes, SkIPoint srcPt, 83 GrContext* direct = nullptr); 84 85 /** 86 * Writes a rectangle of pixels [srcInfo, srcBuffer, srcRowbytes] into the 87 * renderTargetContext at the specified position. 88 * @param srcInfo image info for the source pixels 89 * @param src source for the write 90 * @param rowBytes bytes in a row of 'src' 91 * @param dstPt offset w/in the surface context at which to write 92 * @param direct The direct context to use. If null will use our GrRecordingContext if it 93 * is a GrDirectContext and fail otherwise. 94 */ 95 bool writePixels(const GrImageInfo& srcInfo, const void* src, size_t rowBytes, SkIPoint dstPt, 96 GrContext* direct = nullptr); 97 asSurfaceProxy()98 GrSurfaceProxy* asSurfaceProxy() { return fReadView.proxy(); } asSurfaceProxy()99 const GrSurfaceProxy* asSurfaceProxy() const { return fReadView.proxy(); } asSurfaceProxyRef()100 sk_sp<GrSurfaceProxy> asSurfaceProxyRef() { return fReadView.refProxy(); } 101 asTextureProxy()102 GrTextureProxy* asTextureProxy() { return fReadView.asTextureProxy(); } asTextureProxy()103 const GrTextureProxy* asTextureProxy() const { return fReadView.asTextureProxy(); } asTextureProxyRef()104 sk_sp<GrTextureProxy> asTextureProxyRef() { return fReadView.asTextureProxyRef(); } 105 asRenderTargetProxy()106 GrRenderTargetProxy* asRenderTargetProxy() { return fReadView.asRenderTargetProxy(); } asRenderTargetProxy()107 const GrRenderTargetProxy* asRenderTargetProxy() const { 108 return fReadView.asRenderTargetProxy(); 109 } asRenderTargetProxyRef()110 sk_sp<GrRenderTargetProxy> asRenderTargetProxyRef() { 111 return fReadView.asRenderTargetProxyRef(); 112 } 113 asRenderTargetContext()114 virtual GrRenderTargetContext* asRenderTargetContext() { return nullptr; } 115 116 GrAuditTrail* auditTrail(); 117 118 // Provides access to functions that aren't part of the public API. 119 GrSurfaceContextPriv surfPriv(); 120 const GrSurfaceContextPriv surfPriv() const; 121 122 #if GR_TEST_UTILS testCopy(GrSurfaceProxy * src,GrSurfaceOrigin origin,const SkIRect & srcRect,const SkIPoint & dstPoint)123 bool testCopy(GrSurfaceProxy* src, GrSurfaceOrigin origin, const SkIRect& srcRect, 124 const SkIPoint& dstPoint) { 125 return this->copy(src, origin, srcRect, dstPoint); 126 } 127 testCopy(GrSurfaceProxy * src,GrSurfaceOrigin origin)128 bool testCopy(GrSurfaceProxy* src, GrSurfaceOrigin origin) { 129 return this->copy(src, origin, SkIRect::MakeSize(src->dimensions()), SkIPoint::Make(0, 0)); 130 } 131 #endif 132 133 protected: 134 friend class GrSurfaceContextPriv; 135 136 GrDrawingManager* drawingManager(); 137 const GrDrawingManager* drawingManager() const; 138 139 SkDEBUGCODE(void validate() const;) 140 141 SkDEBUGCODE(GrSingleOwner* singleOwner();) 142 143 GrRecordingContext* fContext; 144 145 GrSurfaceProxyView fReadView; 146 147 // The rescaling step of asyncRescaleAndReadPixels[YUV420](). 148 std::unique_ptr<GrRenderTargetContext> rescale(const SkImageInfo& info, const SkIRect& srcRect, 149 SkSurface::RescaleGamma rescaleGamma, 150 SkFilterQuality rescaleQuality); 151 152 // Inserts a transfer, part of the implementation of asyncReadPixels and 153 // asyncRescaleAndReadPixelsYUV420(). 154 struct PixelTransferResult { 155 using ConversionFn = void(void* dst, const void* mappedBuffer); 156 // If null then the transfer could not be performed. Otherwise this buffer will contain 157 // the pixel data when the transfer is complete. 158 sk_sp<GrGpuBuffer> fTransferBuffer; 159 // If this is null then the transfer buffer will contain the data in the requested 160 // color type. Otherwise, when the transfer is done this must be called to convert 161 // from the transfer buffer's color type to the requested color type. 162 std::function<ConversionFn> fPixelConverter; 163 }; 164 PixelTransferResult transferPixels(GrColorType colorType, const SkIRect& rect); 165 166 private: 167 friend class GrSurfaceProxy; // for copy 168 169 SkDEBUGCODE(virtual void onValidate() const {}) 170 171 /** 172 * Copy 'src' into the proxy backing this context. This call will not do any draw fallback. 173 * Currently only writePixels and replaceRenderTarget call this directly. All other copies 174 * should go through GrSurfaceProxy::Copy. 175 * @param src src of pixels 176 * @param srcRect the subset of 'src' to copy 177 * @param dstPoint the origin of the 'srcRect' in the destination coordinate space 178 * @return true if the copy succeeded; false otherwise 179 * 180 * Note: Notionally, 'srcRect' is clipped to 'src's extent with 'dstPoint' being adjusted. 181 * Then the 'srcRect' offset by 'dstPoint' is clipped against the dst's extent. 182 * The end result is only valid src pixels and dst pixels will be touched but the copied 183 * regions will not be shifted. The 'src' must have the same origin as the backing proxy 184 * of fSurfaceContext. 185 */ 186 bool copy(GrSurfaceProxy* src, GrSurfaceOrigin origin, const SkIRect& srcRect, 187 const SkIPoint& dstPoint); 188 189 GrColorInfo fColorInfo; 190 191 typedef SkRefCnt INHERITED; 192 }; 193 194 #endif 195