Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 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 | |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 11 | #include "include/core/SkFilterQuality.h" |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 12 | #include "include/core/SkImage.h" |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 13 | #include "include/core/SkRect.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "include/core/SkRefCnt.h" |
Brian Salomon | e9ad998 | 2019-07-22 16:17:41 -0400 | [diff] [blame] | 15 | #include "include/core/SkSurface.h" |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 16 | #include "src/gpu/GrClientMappedBufferManager.h" |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 17 | #include "src/gpu/GrColorInfo.h" |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 18 | #include "src/gpu/GrDataUtils.h" |
Brian Salomon | 6aa6505 | 2020-01-28 12:16:53 -0500 | [diff] [blame] | 19 | #include "src/gpu/GrImageInfo.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 20 | #include "src/gpu/GrSurfaceProxy.h" |
Greg Daniel | 901b98e | 2019-10-22 09:54:02 -0400 | [diff] [blame] | 21 | #include "src/gpu/GrSurfaceProxyView.h" |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 22 | |
| 23 | class GrAuditTrail; |
Robert Phillips | 7215283 | 2017-01-25 17:31:35 -0500 | [diff] [blame] | 24 | class GrDrawingManager; |
Robert Phillips | 6989370 | 2019-02-22 11:16:30 -0500 | [diff] [blame] | 25 | class GrRecordingContext; |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 26 | class GrRenderTargetContext; |
Robert Phillips | 2734136 | 2016-12-14 08:46:47 -0500 | [diff] [blame] | 27 | class GrRenderTargetProxy; |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 28 | class GrSingleOwner; |
| 29 | class GrSurface; |
Robert Phillips | 2734136 | 2016-12-14 08:46:47 -0500 | [diff] [blame] | 30 | class GrSurfaceProxy; |
| 31 | class GrTextureProxy; |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 32 | struct SkIPoint; |
| 33 | struct SkIRect; |
| 34 | |
| 35 | /** |
| 36 | * A helper object to orchestrate commands for a particular surface |
| 37 | */ |
Brian Salomon | 57f211b | 2019-08-21 15:21:09 -0400 | [diff] [blame] | 38 | class GrSurfaceContext { |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 39 | public: |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 40 | // If the passed in GrSurfaceProxy is renderable this will return a GrRenderTargetContext, |
| 41 | // otherwise it will return a GrSurfaceContext. |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 42 | static std::unique_ptr<GrSurfaceContext> Make(GrRecordingContext*, |
| 43 | GrSurfaceProxyView readView, |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 44 | GrColorType, SkAlphaType, sk_sp<SkColorSpace>); |
| 45 | |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 46 | static std::unique_ptr<GrSurfaceContext> Make(GrRecordingContext*, SkISize dimensions, |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 47 | const GrBackendFormat&, GrRenderable, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 48 | int renderTargetSampleCnt, GrMipmapped, |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 49 | GrProtected, GrSurfaceOrigin, GrColorType, |
| 50 | SkAlphaType, sk_sp<SkColorSpace>, SkBackingFit, |
| 51 | SkBudgeted); |
| 52 | |
| 53 | // If it is known that the GrSurfaceProxy is not renderable, you can directly call the the ctor |
| 54 | // here to make a GrSurfaceContext on the stack. |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 55 | GrSurfaceContext(GrRecordingContext*, GrSurfaceProxyView readView, GrColorType, SkAlphaType, |
| 56 | sk_sp<SkColorSpace>); |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 57 | |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 58 | virtual ~GrSurfaceContext() = default; |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 59 | |
Brian Salomon | 70fe17e | 2020-11-30 14:33:58 -0500 | [diff] [blame^] | 60 | GrRecordingContext* recordingContext() { return fContext; } |
| 61 | |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 62 | const GrColorInfo& colorInfo() const { return fColorInfo; } |
Brian Salomon | 6aa6505 | 2020-01-28 12:16:53 -0500 | [diff] [blame] | 63 | GrImageInfo imageInfo() const { return {fColorInfo, fReadView.proxy()->dimensions()}; } |
| 64 | |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 65 | GrSurfaceOrigin origin() const { return fReadView.origin(); } |
| 66 | GrSwizzle readSwizzle() const { return fReadView.swizzle(); } |
| 67 | // TODO: See if it makes sense for this to return a const& instead and require the callers to |
| 68 | // make a copy (which refs the proxy) if needed. |
| 69 | GrSurfaceProxyView readSurfaceView() { return fReadView; } |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 70 | |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 71 | SkISize dimensions() const { return fReadView.dimensions(); } |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 72 | int width() const { return fReadView.proxy()->width(); } |
| 73 | int height() const { return fReadView.proxy()->height(); } |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 74 | |
Brian Salomon | 4d2d6f4 | 2019-07-26 14:15:11 -0400 | [diff] [blame] | 75 | const GrCaps* caps() const; |
| 76 | |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 77 | /** |
| 78 | * Reads a rectangle of pixels from the render target context. |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 79 | * @param dContext The direct context to use |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 80 | * @param dstInfo image info for the destination |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 81 | * @param dst destination pixels for the read |
| 82 | * @param rowBytes bytes in a row of 'dst' |
| 83 | * @param srcPt offset w/in the surface context from which to read |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 84 | * is a GrDirectContext and fail otherwise. |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 85 | */ |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 86 | bool readPixels(GrDirectContext* dContext, |
| 87 | const GrImageInfo& dstInfo, |
| 88 | void* dst, |
| 89 | size_t rowBytes, |
| 90 | SkIPoint srcPt); |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 91 | |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 92 | using ReadPixelsCallback = SkImage::ReadPixelsCallback; |
| 93 | using ReadPixelsContext = SkImage::ReadPixelsContext; |
| 94 | using RescaleGamma = SkImage::RescaleGamma; |
| 95 | |
| 96 | // GPU implementation for SkImage:: and SkSurface::asyncRescaleAndReadPixels. |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 97 | void asyncRescaleAndReadPixels(GrDirectContext*, |
| 98 | const SkImageInfo& info, |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 99 | const SkIRect& srcRect, |
| 100 | RescaleGamma rescaleGamma, |
| 101 | SkFilterQuality rescaleQuality, |
| 102 | ReadPixelsCallback callback, |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 103 | ReadPixelsContext callbackContext); |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 104 | |
| 105 | // GPU implementation for SkImage:: and SkSurface::asyncRescaleAndReadPixelsYUV420. |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 106 | void asyncRescaleAndReadPixelsYUV420(GrDirectContext*, |
| 107 | SkYUVColorSpace yuvColorSpace, |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 108 | sk_sp<SkColorSpace> dstColorSpace, |
| 109 | const SkIRect& srcRect, |
| 110 | SkISize dstSize, |
| 111 | RescaleGamma rescaleGamma, |
| 112 | SkFilterQuality rescaleQuality, |
| 113 | ReadPixelsCallback callback, |
| 114 | ReadPixelsContext context); |
| 115 | |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 116 | /** |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 117 | * Writes a rectangle of pixels [srcInfo, srcBuffer, srcRowbytes] into the |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 118 | * renderTargetContext at the specified position. |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 119 | * @param dContext The direct context to use |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 120 | * @param srcInfo image info for the source pixels |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 121 | * @param src source for the write |
| 122 | * @param rowBytes bytes in a row of 'src' |
| 123 | * @param dstPt offset w/in the surface context at which to write |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 124 | */ |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 125 | bool writePixels(GrDirectContext* dContext, |
| 126 | const GrImageInfo& srcInfo, |
| 127 | const void* src, |
| 128 | size_t rowBytes, |
| 129 | SkIPoint dstPt); |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 130 | |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 131 | GrSurfaceProxy* asSurfaceProxy() { return fReadView.proxy(); } |
| 132 | const GrSurfaceProxy* asSurfaceProxy() const { return fReadView.proxy(); } |
Greg Daniel | c61d7e3 | 2020-02-04 14:27:45 -0500 | [diff] [blame] | 133 | sk_sp<GrSurfaceProxy> asSurfaceProxyRef() { return fReadView.refProxy(); } |
Robert Phillips | f200a90 | 2017-01-30 13:27:37 -0500 | [diff] [blame] | 134 | |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 135 | GrTextureProxy* asTextureProxy() { return fReadView.asTextureProxy(); } |
| 136 | const GrTextureProxy* asTextureProxy() const { return fReadView.asTextureProxy(); } |
| 137 | sk_sp<GrTextureProxy> asTextureProxyRef() { return fReadView.asTextureProxyRef(); } |
Robert Phillips | f200a90 | 2017-01-30 13:27:37 -0500 | [diff] [blame] | 138 | |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 139 | GrRenderTargetProxy* asRenderTargetProxy() { return fReadView.asRenderTargetProxy(); } |
Greg Daniel | 46e366a | 2019-12-16 14:38:36 -0500 | [diff] [blame] | 140 | const GrRenderTargetProxy* asRenderTargetProxy() const { |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 141 | return fReadView.asRenderTargetProxy(); |
Greg Daniel | 46e366a | 2019-12-16 14:38:36 -0500 | [diff] [blame] | 142 | } |
| 143 | sk_sp<GrRenderTargetProxy> asRenderTargetProxyRef() { |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 144 | return fReadView.asRenderTargetProxyRef(); |
Greg Daniel | 46e366a | 2019-12-16 14:38:36 -0500 | [diff] [blame] | 145 | } |
Robert Phillips | 2734136 | 2016-12-14 08:46:47 -0500 | [diff] [blame] | 146 | |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 147 | virtual GrRenderTargetContext* asRenderTargetContext() { return nullptr; } |
| 148 | |
Brian Salomon | 11ad4cc | 2020-05-15 12:07:59 -0400 | [diff] [blame] | 149 | /** |
| 150 | * Rescales the contents of srcRect. The gamma in which the rescaling occurs is controlled by |
| 151 | * RescaleGamma. It is always in the original gamut. The result is converted to the color type |
| 152 | * and color space of info after rescaling. Note: this currently requires that the info have a |
| 153 | * different size than srcRect. Though, it could be relaxed to allow non-scaling color |
| 154 | * conversions. |
| 155 | */ |
| 156 | std::unique_ptr<GrRenderTargetContext> rescale(const GrImageInfo& info, |
| 157 | GrSurfaceOrigin, |
Brian Salomon | afd8a6c | 2020-05-21 12:10:54 -0400 | [diff] [blame] | 158 | SkIRect srcRect, |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 159 | SkImage::RescaleGamma, |
Brian Salomon | 11ad4cc | 2020-05-15 12:07:59 -0400 | [diff] [blame] | 160 | SkFilterQuality); |
| 161 | |
Robert Phillips | 0d075de | 2019-03-04 11:08:13 -0500 | [diff] [blame] | 162 | GrAuditTrail* auditTrail(); |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 163 | |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 164 | #if GR_TEST_UTILS |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 165 | bool testCopy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) { |
| 166 | return this->copy(src, srcRect, dstPoint); |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 167 | } |
| 168 | |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 169 | bool testCopy(GrSurfaceProxy* src) { |
| 170 | return this->copy(src, SkIRect::MakeSize(src->dimensions()), {0, 0}); |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 171 | } |
| 172 | #endif |
| 173 | |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 174 | protected: |
Robert Phillips | 0d075de | 2019-03-04 11:08:13 -0500 | [diff] [blame] | 175 | GrDrawingManager* drawingManager(); |
| 176 | const GrDrawingManager* drawingManager() const; |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 177 | |
Greg Daniel | 46e366a | 2019-12-16 14:38:36 -0500 | [diff] [blame] | 178 | SkDEBUGCODE(void validate() const;) |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 179 | |
Brian Salomon | 70fe17e | 2020-11-30 14:33:58 -0500 | [diff] [blame^] | 180 | SkDEBUGCODE(GrSingleOwner* singleOwner() const;) |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 181 | |
Robert Phillips | 6989370 | 2019-02-22 11:16:30 -0500 | [diff] [blame] | 182 | GrRecordingContext* fContext; |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 183 | |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 184 | GrSurfaceProxyView fReadView; |
Greg Daniel | 901b98e | 2019-10-22 09:54:02 -0400 | [diff] [blame] | 185 | |
Brian Salomon | 4d2d6f4 | 2019-07-26 14:15:11 -0400 | [diff] [blame] | 186 | // Inserts a transfer, part of the implementation of asyncReadPixels and |
| 187 | // asyncRescaleAndReadPixelsYUV420(). |
| 188 | struct PixelTransferResult { |
| 189 | using ConversionFn = void(void* dst, const void* mappedBuffer); |
| 190 | // If null then the transfer could not be performed. Otherwise this buffer will contain |
| 191 | // the pixel data when the transfer is complete. |
| 192 | sk_sp<GrGpuBuffer> fTransferBuffer; |
| 193 | // If this is null then the transfer buffer will contain the data in the requested |
| 194 | // color type. Otherwise, when the transfer is done this must be called to convert |
| 195 | // from the transfer buffer's color type to the requested color type. |
| 196 | std::function<ConversionFn> fPixelConverter; |
| 197 | }; |
| 198 | PixelTransferResult transferPixels(GrColorType colorType, const SkIRect& rect); |
| 199 | |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 200 | // The async read step of asyncRescaleAndReadPixels() |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 201 | void asyncReadPixels(GrDirectContext*, |
| 202 | const SkIRect& srcRect, |
| 203 | SkColorType, |
| 204 | ReadPixelsCallback, |
| 205 | ReadPixelsContext); |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 206 | |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame] | 207 | private: |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 208 | friend class GrSurfaceProxy; // for copy |
| 209 | |
Greg Daniel | 46e366a | 2019-12-16 14:38:36 -0500 | [diff] [blame] | 210 | SkDEBUGCODE(virtual void onValidate() const {}) |
| 211 | |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 212 | /** |
| 213 | * Copy 'src' into the proxy backing this context. This call will not do any draw fallback. |
| 214 | * Currently only writePixels and replaceRenderTarget call this directly. All other copies |
| 215 | * should go through GrSurfaceProxy::Copy. |
| 216 | * @param src src of pixels |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 217 | * @param dstPoint the origin of the 'srcRect' in the destination coordinate space |
| 218 | * @return true if the copy succeeded; false otherwise |
| 219 | * |
| 220 | * Note: Notionally, 'srcRect' is clipped to 'src's extent with 'dstPoint' being adjusted. |
| 221 | * Then the 'srcRect' offset by 'dstPoint' is clipped against the dst's extent. |
| 222 | * The end result is only valid src pixels and dst pixels will be touched but the copied |
| 223 | * regions will not be shifted. The 'src' must have the same origin as the backing proxy |
| 224 | * of fSurfaceContext. |
| 225 | */ |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 226 | bool copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint); |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 227 | |
Brian Salomon | 63a0a75 | 2020-06-26 13:32:09 -0400 | [diff] [blame] | 228 | class AsyncReadResult; |
| 229 | |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 230 | GrColorInfo fColorInfo; |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 231 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 232 | using INHERITED = SkRefCnt; |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 233 | }; |
| 234 | |
| 235 | #endif |