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 SurfaceContext_DEFINED 9 #define SurfaceContext_DEFINED 10 11 #include "include/core/SkImage.h" 12 #include "include/core/SkRect.h" 13 #include "include/core/SkRefCnt.h" 14 #include "include/core/SkSamplingOptions.h" 15 #include "include/core/SkSurface.h" 16 #include "src/gpu/ganesh/GrColorInfo.h" 17 #include "src/gpu/ganesh/GrDataUtils.h" 18 #include "src/gpu/ganesh/GrImageInfo.h" 19 #include "src/gpu/ganesh/GrPixmap.h" 20 #include "src/gpu/ganesh/GrRenderTargetProxy.h" 21 #include "src/gpu/ganesh/GrRenderTask.h" 22 #include "src/gpu/ganesh/GrSurfaceProxy.h" 23 #include "src/gpu/ganesh/GrSurfaceProxyView.h" 24 25 class GrDrawingManager; 26 class GrRecordingContext; 27 class GrRenderTargetProxy; 28 class GrSurface; 29 class GrSurfaceProxy; 30 class GrTextureProxy; 31 struct SkIPoint; 32 struct SkIRect; 33 34 namespace skgpu { 35 class SingleOwner; 36 } 37 38 namespace skgpu::v1 { 39 40 class SurfaceFillContext; 41 42 class SurfaceContext { 43 public: 44 // If it is known that the GrSurfaceProxy is not renderable, you can directly call the ctor 45 // here to make a SurfaceContext on the stack. 46 SurfaceContext(GrRecordingContext*, GrSurfaceProxyView readView, const GrColorInfo&); 47 48 virtual ~SurfaceContext() = default; 49 recordingContext()50 GrRecordingContext* recordingContext() const { return fContext; } 51 colorInfo()52 const GrColorInfo& colorInfo() const { return fColorInfo; } imageInfo()53 GrImageInfo imageInfo() const { return {fColorInfo, fReadView.proxy()->dimensions()}; } 54 origin()55 GrSurfaceOrigin origin() const { return fReadView.origin(); } readSwizzle()56 skgpu::Swizzle readSwizzle() const { return fReadView.swizzle(); } 57 // TODO: See if it makes sense for this to return a const& instead and require the callers to 58 // make a copy (which refs the proxy) if needed. readSurfaceView()59 GrSurfaceProxyView readSurfaceView() { return fReadView; } 60 dimensions()61 SkISize dimensions() const { return fReadView.dimensions(); } width()62 int width() const { return fReadView.proxy()->width(); } height()63 int height() const { return fReadView.proxy()->height(); } 64 mipmapped()65 GrMipmapped mipmapped() const { return fReadView.mipmapped(); } 66 67 const GrCaps* caps() const; 68 69 /** 70 * Reads a rectangle of pixels from the surface context. 71 * @param dContext The direct context to use 72 * @param dst destination pixels for the read 73 * @param srcPt offset w/in the surface context from which to read 74 * is a GrDirectContext and fail otherwise. 75 */ 76 bool readPixels(GrDirectContext* dContext, GrPixmap dst, SkIPoint srcPt); 77 78 using ReadPixelsCallback = SkImage::ReadPixelsCallback; 79 using ReadPixelsContext = SkImage::ReadPixelsContext; 80 using RescaleGamma = SkImage::RescaleGamma; 81 using RescaleMode = SkImage::RescaleMode; 82 83 // GPU implementation for SkImage:: and SkSurface::asyncRescaleAndReadPixels. 84 void asyncRescaleAndReadPixels(GrDirectContext*, 85 const SkImageInfo& info, 86 const SkIRect& srcRect, 87 RescaleGamma rescaleGamma, 88 RescaleMode, 89 ReadPixelsCallback callback, 90 ReadPixelsContext callbackContext); 91 92 // GPU implementation for SkImage:: and SkSurface::asyncRescaleAndReadPixelsYUV420. 93 void asyncRescaleAndReadPixelsYUV420(GrDirectContext*, 94 SkYUVColorSpace yuvColorSpace, 95 sk_sp<SkColorSpace> dstColorSpace, 96 const SkIRect& srcRect, 97 SkISize dstSize, 98 RescaleGamma rescaleGamma, 99 RescaleMode, 100 ReadPixelsCallback callback, 101 ReadPixelsContext context); 102 103 /** 104 * Writes a rectangle of pixels from src into the surfaceDrawContext at the specified position. 105 * @param dContext The direct context to use 106 * @param src source for the write 107 * @param dstPt offset w/in the surface context at which to write 108 */ 109 bool writePixels(GrDirectContext* dContext, 110 GrCPixmap src, 111 SkIPoint dstPt); 112 113 /** 114 * Fully populates either the base level or all MIP levels of the GrSurface with pixel data. 115 * @param dContext The direct context to use 116 * @param src Array of pixmaps 117 * @param numLevels Number of pixmaps in src. To succeed this must be 1 or the total 118 * number of MIP levels. 119 */ 120 bool writePixels(GrDirectContext* dContext, 121 const GrCPixmap src[], 122 int numLevels); 123 asSurfaceProxy()124 GrSurfaceProxy* asSurfaceProxy() { return fReadView.proxy(); } asSurfaceProxy()125 const GrSurfaceProxy* asSurfaceProxy() const { return fReadView.proxy(); } asSurfaceProxyRef()126 sk_sp<GrSurfaceProxy> asSurfaceProxyRef() { return fReadView.refProxy(); } 127 asTextureProxy()128 GrTextureProxy* asTextureProxy() { return fReadView.asTextureProxy(); } asTextureProxy()129 const GrTextureProxy* asTextureProxy() const { return fReadView.asTextureProxy(); } asTextureProxyRef()130 sk_sp<GrTextureProxy> asTextureProxyRef() { return fReadView.asTextureProxyRef(); } 131 asRenderTargetProxy()132 GrRenderTargetProxy* asRenderTargetProxy() { return fReadView.asRenderTargetProxy(); } asRenderTargetProxy()133 const GrRenderTargetProxy* asRenderTargetProxy() const { 134 return fReadView.asRenderTargetProxy(); 135 } asRenderTargetProxyRef()136 sk_sp<GrRenderTargetProxy> asRenderTargetProxyRef() { 137 return fReadView.asRenderTargetProxyRef(); 138 } 139 asFillContext()140 virtual SurfaceFillContext* asFillContext() { return nullptr; } 141 142 /** 143 * Rescales the contents of srcRect. The gamma in which the rescaling occurs is controlled by 144 * RescaleGamma. It is always in the original gamut. The result is converted to the color type 145 * and color space of info after rescaling. Note: this currently requires that the info have a 146 * different size than srcRect. Though, it could be relaxed to allow non-scaling color 147 * conversions. 148 */ 149 std::unique_ptr<SurfaceFillContext> rescale(const GrImageInfo& info, 150 GrSurfaceOrigin, 151 SkIRect srcRect, 152 SkImage::RescaleGamma, 153 SkImage::RescaleMode); 154 155 /** 156 * Like the above but allows the caller ot specify a destination fill context and 157 * rect within that context. The dst rect must be contained by the dst or this will fail. 158 */ 159 bool rescaleInto(SurfaceFillContext* dst, 160 SkIRect dstRect, 161 SkIRect srcRect, 162 SkImage::RescaleGamma, 163 SkImage::RescaleMode); 164 165 #if GR_TEST_UTILS testCopy(sk_sp<GrSurfaceProxy> src,const SkIRect & srcRect,const SkIPoint & dstPoint)166 bool testCopy(sk_sp<GrSurfaceProxy> src, const SkIRect& srcRect, const SkIPoint& dstPoint) { 167 return this->copy(std::move(src), srcRect, dstPoint) != nullptr; 168 } 169 testCopy(sk_sp<GrSurfaceProxy> src)170 bool testCopy(sk_sp<GrSurfaceProxy> src) { 171 auto rect = SkIRect::MakeSize(src->dimensions()); 172 return this->copy(std::move(src), rect, {0, 0}) != nullptr; 173 } 174 #endif 175 176 protected: 177 GrDrawingManager* drawingManager(); 178 const GrDrawingManager* drawingManager() const; 179 180 SkDEBUGCODE(void validate() const;) 181 182 SkDEBUGCODE(skgpu::SingleOwner* singleOwner() const;) 183 184 GrRecordingContext* fContext; 185 186 GrSurfaceProxyView fReadView; 187 188 // Inserts a transfer, part of the implementation of asyncReadPixels and 189 // asyncRescaleAndReadPixelsYUV420(). 190 struct PixelTransferResult { 191 using ConversionFn = void(void* dst, const void* mappedBuffer); 192 // If null then the transfer could not be performed. Otherwise this buffer will contain 193 // the pixel data when the transfer is complete. 194 sk_sp<GrGpuBuffer> fTransferBuffer; 195 // If this is null then the transfer buffer will contain the data in the requested 196 // color type. Otherwise, when the transfer is done this must be called to convert 197 // from the transfer buffer's color type to the requested color type. 198 std::function<ConversionFn> fPixelConverter; 199 }; 200 PixelTransferResult transferPixels(GrColorType colorType, const SkIRect& rect); 201 202 // The async read step of asyncRescaleAndReadPixels() 203 void asyncReadPixels(GrDirectContext*, 204 const SkIRect& srcRect, 205 SkColorType, 206 ReadPixelsCallback, 207 ReadPixelsContext); 208 209 private: 210 friend class ::GrRecordingContextPriv; // for validate 211 friend class ::GrSurfaceProxy; // for copy 212 213 SkDEBUGCODE(virtual void onValidate() const {}) 214 215 /** 216 * Copy 'src' into the proxy backing this context. This call will not do any draw fallback. 217 * Currently only writePixels and replaceRenderTarget call this directly. All other copies 218 * should go through GrSurfaceProxy::Copy. 219 * @param src src of pixels 220 * @param dstPoint the origin of the 'srcRect' in the destination coordinate space 221 * @return a task (that may be skippable by calling canSkip) if successful and 222 * null otherwise. 223 * 224 * Note: Notionally, 'srcRect' is clipped to 'src's extent with 'dstPoint' being adjusted. 225 * Then the 'srcRect' offset by 'dstPoint' is clipped against the dst's extent. 226 * The end result is only valid src pixels and dst pixels will be touched but the copied 227 * regions will not be shifted. The 'src' must have the same origin as the backing proxy 228 * of fSurfaceContext. 229 */ 230 sk_sp<GrRenderTask> copy(sk_sp<GrSurfaceProxy> src, SkIRect srcRect, SkIPoint dstPoint); 231 232 /** 233 * Copy and scale 'src' into the proxy backing this context. This call will not do any draw 234 * fallback. Currently only rescaleInto() calls this directly, which handles drawing fallback 235 * automatically. 236 * 237 * @param src src of pixels 238 * @param srcRect the subset of src that is copied to this proxy 239 * @param dstRect the subset of dst that receives the copied data, possibly with different 240 * dimensions than 'srcRect'. 241 * @param filterMode the filter mode to apply when scaling src 242 * @return a task (that may be skippable by calling canSkip) if successful and 243 * null otherwise. 244 * 245 * Note: Unlike copy(rect,point), 'srcRect' and 'dstRect' are not adjusted to fit within the 246 * surfaces. If they are not contained, then nullptr is returned. The 'src' must have the same 247 * origin as the backing proxy of this context. 248 */ 249 sk_sp<GrRenderTask> copyScaled(sk_sp<GrSurfaceProxy> src, SkIRect srcRect, SkIRect dstRect, 250 GrSamplerState::Filter filterMode); 251 252 bool internalWritePixels(GrDirectContext* dContext, 253 const GrCPixmap src[], 254 int numLevels, 255 SkIPoint); 256 257 GrColorInfo fColorInfo; 258 259 using INHERITED = SkRefCnt; 260 }; 261 262 } // namespace skgpu::v1 263 264 #endif // SurfaceContext_DEFINED 265