blob: f3971af6e5289804ab21610bcf0c6e2cab34cab0 [file] [log] [blame]
Brian Osman45580d32016-11-23 09:37:01 -05001/*
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 Salomon63a0a752020-06-26 13:32:09 -040011#include "include/core/SkImage.h"
Brian Salomone9ad9982019-07-22 16:17:41 -040012#include "include/core/SkRect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkRefCnt.h"
Mike Reed1efa14d2021-01-02 21:44:59 -050014#include "include/core/SkSamplingOptions.h"
Brian Salomone9ad9982019-07-22 16:17:41 -040015#include "include/core/SkSurface.h"
Brian Salomon63a0a752020-06-26 13:32:09 -040016#include "src/gpu/GrClientMappedBufferManager.h"
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040017#include "src/gpu/GrColorInfo.h"
Brian Salomon1d435302019-07-01 13:05:28 -040018#include "src/gpu/GrDataUtils.h"
Brian Salomon6aa65052020-01-28 12:16:53 -050019#include "src/gpu/GrImageInfo.h"
Brian Salomondd4087d2020-12-23 20:36:44 -050020#include "src/gpu/GrPixmap.h"
Brian Salomond63638b2021-03-05 14:00:07 -050021#include "src/gpu/GrRenderTask.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040022#include "src/gpu/GrSurfaceProxy.h"
Greg Daniel901b98e2019-10-22 09:54:02 -040023#include "src/gpu/GrSurfaceProxyView.h"
Brian Osman45580d32016-11-23 09:37:01 -050024
25class GrAuditTrail;
Robert Phillips72152832017-01-25 17:31:35 -050026class GrDrawingManager;
Robert Phillips69893702019-02-22 11:16:30 -050027class GrRecordingContext;
Robert Phillips27341362016-12-14 08:46:47 -050028class GrRenderTargetProxy;
Brian Osman45580d32016-11-23 09:37:01 -050029class GrSingleOwner;
30class GrSurface;
Brian Salomon590f5672020-12-16 11:44:47 -050031class GrSurfaceDrawContext;
32class GrSurfaceFillContext;
Robert Phillips27341362016-12-14 08:46:47 -050033class GrSurfaceProxy;
34class GrTextureProxy;
Brian Osman45580d32016-11-23 09:37:01 -050035struct SkIPoint;
36struct SkIRect;
37
38/**
39 * A helper object to orchestrate commands for a particular surface
40 */
Brian Salomon57f211b2019-08-21 15:21:09 -040041class GrSurfaceContext {
Brian Osman45580d32016-11-23 09:37:01 -050042public:
Brian Salomoneebe7352020-12-09 16:37:04 -050043 // If the passed in GrSurfaceProxy is renderable this will return a GrSurfaceDrawContext,
Greg Danielbfa19c42019-12-19 16:41:40 -050044 // otherwise it will return a GrSurfaceContext.
Greg Daniel3912a4b2020-01-14 09:56:04 -050045 static std::unique_ptr<GrSurfaceContext> Make(GrRecordingContext*,
46 GrSurfaceProxyView readView,
Brian Salomon14f99fc2020-12-07 12:19:47 -050047 const GrColorInfo&);
Greg Danielbfa19c42019-12-19 16:41:40 -050048
Brian Salomon590f5672020-12-16 11:44:47 -050049 // Makes either a GrSurfaceContext, GrFillDrawContext, or a GrSurfaceDrawContext, depending on
50 // GrRenderable and the GrImageInfo.
Brian Salomon14f99fc2020-12-07 12:19:47 -050051 static std::unique_ptr<GrSurfaceContext> Make(GrRecordingContext*,
52 const GrImageInfo&,
53 const GrBackendFormat&,
54 SkBackingFit = SkBackingFit::kExact,
55 GrSurfaceOrigin = kTopLeft_GrSurfaceOrigin,
56 GrRenderable = GrRenderable::kNo,
57 int renderTargetSampleCnt = 1,
58 GrMipmapped = GrMipmapped::kNo,
59 GrProtected = GrProtected::kNo,
60 SkBudgeted = SkBudgeted::kYes);
61
Brian Salomon590f5672020-12-16 11:44:47 -050062 // Same as the above but chooses the texture format using the default format for the color type.
Brian Salomon14f99fc2020-12-07 12:19:47 -050063 static std::unique_ptr<GrSurfaceContext> Make(GrRecordingContext*,
64 const GrImageInfo&,
65 SkBackingFit = SkBackingFit::kExact,
66 GrSurfaceOrigin = kTopLeft_GrSurfaceOrigin,
67 GrRenderable = GrRenderable::kNo,
68 int renderTargetSampleCnt = 1,
69 GrMipmapped = GrMipmapped::kNo,
70 GrProtected = GrProtected::kNo,
71 SkBudgeted = SkBudgeted::kYes);
Greg Danielbfa19c42019-12-19 16:41:40 -050072
73 // If it is known that the GrSurfaceProxy is not renderable, you can directly call the the ctor
74 // here to make a GrSurfaceContext on the stack.
Brian Salomon14f99fc2020-12-07 12:19:47 -050075 GrSurfaceContext(GrRecordingContext*, GrSurfaceProxyView readView, const GrColorInfo&);
Greg Danielbfa19c42019-12-19 16:41:40 -050076
Brian Salomonbf6b9792019-08-21 09:38:10 -040077 virtual ~GrSurfaceContext() = default;
Brian Osman45580d32016-11-23 09:37:01 -050078
Brian Salomon70fe17e2020-11-30 14:33:58 -050079 GrRecordingContext* recordingContext() { return fContext; }
80
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040081 const GrColorInfo& colorInfo() const { return fColorInfo; }
Brian Salomon6aa65052020-01-28 12:16:53 -050082 GrImageInfo imageInfo() const { return {fColorInfo, fReadView.proxy()->dimensions()}; }
83
Greg Daniel3912a4b2020-01-14 09:56:04 -050084 GrSurfaceOrigin origin() const { return fReadView.origin(); }
85 GrSwizzle readSwizzle() const { return fReadView.swizzle(); }
86 // TODO: See if it makes sense for this to return a const& instead and require the callers to
87 // make a copy (which refs the proxy) if needed.
88 GrSurfaceProxyView readSurfaceView() { return fReadView; }
Robert Phillips2c862492017-01-18 10:08:39 -050089
Brian Salomonc5243782020-04-02 12:50:34 -040090 SkISize dimensions() const { return fReadView.dimensions(); }
Greg Daniel3912a4b2020-01-14 09:56:04 -050091 int width() const { return fReadView.proxy()->width(); }
92 int height() const { return fReadView.proxy()->height(); }
Robert Phillipsd46697a2017-01-25 12:10:37 -050093
Robert Phillips9af0bca2021-05-27 19:17:55 -040094 GrMipmapped mipmapped() const { return fReadView.mipmapped(); }
95
Brian Salomon4d2d6f42019-07-26 14:15:11 -040096 const GrCaps* caps() const;
97
Robert Phillips2c862492017-01-18 10:08:39 -050098 /**
Brian Salomondd4087d2020-12-23 20:36:44 -050099 * Reads a rectangle of pixels from the surface context.
Adlai Hollerc95b5892020-08-11 12:02:22 -0400100 * @param dContext The direct context to use
Brian Salomon1d435302019-07-01 13:05:28 -0400101 * @param dst destination pixels for the read
Brian Salomon1d435302019-07-01 13:05:28 -0400102 * @param srcPt offset w/in the surface context from which to read
Brian Salomon1d435302019-07-01 13:05:28 -0400103 * is a GrDirectContext and fail otherwise.
Robert Phillips2c862492017-01-18 10:08:39 -0500104 */
Brian Salomondd4087d2020-12-23 20:36:44 -0500105 bool readPixels(GrDirectContext* dContext, GrPixmap dst, SkIPoint srcPt);
Robert Phillips2c862492017-01-18 10:08:39 -0500106
Brian Salomon63a0a752020-06-26 13:32:09 -0400107 using ReadPixelsCallback = SkImage::ReadPixelsCallback;
108 using ReadPixelsContext = SkImage::ReadPixelsContext;
109 using RescaleGamma = SkImage::RescaleGamma;
Mike Reed1efa14d2021-01-02 21:44:59 -0500110 using RescaleMode = SkImage::RescaleMode;
Brian Salomon63a0a752020-06-26 13:32:09 -0400111
112 // GPU implementation for SkImage:: and SkSurface::asyncRescaleAndReadPixels.
Adlai Hollerc95b5892020-08-11 12:02:22 -0400113 void asyncRescaleAndReadPixels(GrDirectContext*,
114 const SkImageInfo& info,
Brian Salomon63a0a752020-06-26 13:32:09 -0400115 const SkIRect& srcRect,
116 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -0500117 RescaleMode,
Brian Salomon63a0a752020-06-26 13:32:09 -0400118 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400119 ReadPixelsContext callbackContext);
Brian Salomon63a0a752020-06-26 13:32:09 -0400120
121 // GPU implementation for SkImage:: and SkSurface::asyncRescaleAndReadPixelsYUV420.
Adlai Hollerc95b5892020-08-11 12:02:22 -0400122 void asyncRescaleAndReadPixelsYUV420(GrDirectContext*,
123 SkYUVColorSpace yuvColorSpace,
Brian Salomon63a0a752020-06-26 13:32:09 -0400124 sk_sp<SkColorSpace> dstColorSpace,
125 const SkIRect& srcRect,
126 SkISize dstSize,
127 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -0500128 RescaleMode,
Brian Salomon63a0a752020-06-26 13:32:09 -0400129 ReadPixelsCallback callback,
130 ReadPixelsContext context);
131
Robert Phillips2c862492017-01-18 10:08:39 -0500132 /**
Brian Salomon2c673402021-04-02 14:36:58 -0400133 * Writes a rectangle of pixels from src into the surfaceDrawContext at the specified position.
134 * @param dContext The direct context to use
135 * @param src source for the write
136 * @param dstPt offset w/in the surface context at which to write
Robert Phillips2c862492017-01-18 10:08:39 -0500137 */
Brian Salomonea1d39b2021-04-01 17:06:52 -0400138 bool writePixels(GrDirectContext* dContext,
139 GrCPixmap src,
Brian Salomon75ee7372021-04-06 15:04:35 -0400140 SkIPoint dstPt);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400141
Brian Salomon2c673402021-04-02 14:36:58 -0400142 /**
143 * Fully populates either the base level or all MIP levels of the GrSurface with pixel data.
144 * @param dContext The direct context to use
145 * @param src Array of pixmaps
146 * @param numLevels Number of pixmaps in src. To succeed this must be 1 or the total
147 * number of MIP levels.
148 */
Brian Salomonea1d39b2021-04-01 17:06:52 -0400149 bool writePixels(GrDirectContext* dContext,
150 const GrCPixmap src[],
Brian Salomon75ee7372021-04-06 15:04:35 -0400151 int numLevels);
Brian Salomon2c673402021-04-02 14:36:58 -0400152
Greg Daniel3912a4b2020-01-14 09:56:04 -0500153 GrSurfaceProxy* asSurfaceProxy() { return fReadView.proxy(); }
154 const GrSurfaceProxy* asSurfaceProxy() const { return fReadView.proxy(); }
Greg Danielc61d7e32020-02-04 14:27:45 -0500155 sk_sp<GrSurfaceProxy> asSurfaceProxyRef() { return fReadView.refProxy(); }
Robert Phillipsf200a902017-01-30 13:27:37 -0500156
Greg Daniel3912a4b2020-01-14 09:56:04 -0500157 GrTextureProxy* asTextureProxy() { return fReadView.asTextureProxy(); }
158 const GrTextureProxy* asTextureProxy() const { return fReadView.asTextureProxy(); }
159 sk_sp<GrTextureProxy> asTextureProxyRef() { return fReadView.asTextureProxyRef(); }
Robert Phillipsf200a902017-01-30 13:27:37 -0500160
Greg Daniel3912a4b2020-01-14 09:56:04 -0500161 GrRenderTargetProxy* asRenderTargetProxy() { return fReadView.asRenderTargetProxy(); }
Greg Daniel46e366a2019-12-16 14:38:36 -0500162 const GrRenderTargetProxy* asRenderTargetProxy() const {
Greg Daniel3912a4b2020-01-14 09:56:04 -0500163 return fReadView.asRenderTargetProxy();
Greg Daniel46e366a2019-12-16 14:38:36 -0500164 }
165 sk_sp<GrRenderTargetProxy> asRenderTargetProxyRef() {
Greg Daniel3912a4b2020-01-14 09:56:04 -0500166 return fReadView.asRenderTargetProxyRef();
Greg Daniel46e366a2019-12-16 14:38:36 -0500167 }
Robert Phillips27341362016-12-14 08:46:47 -0500168
Brian Salomoneebe7352020-12-09 16:37:04 -0500169 virtual GrSurfaceDrawContext* asRenderTargetContext() { return nullptr; }
Brian Salomon590f5672020-12-16 11:44:47 -0500170 virtual GrSurfaceFillContext* asFillContext() { return nullptr; }
Robert Phillipsd46697a2017-01-25 12:10:37 -0500171
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400172 /**
173 * Rescales the contents of srcRect. The gamma in which the rescaling occurs is controlled by
174 * RescaleGamma. It is always in the original gamut. The result is converted to the color type
175 * and color space of info after rescaling. Note: this currently requires that the info have a
176 * different size than srcRect. Though, it could be relaxed to allow non-scaling color
177 * conversions.
178 */
Brian Salomonbacbb922021-01-21 19:48:00 -0500179 std::unique_ptr<GrSurfaceFillContext> rescale(const GrImageInfo& info,
Brian Salomoneebe7352020-12-09 16:37:04 -0500180 GrSurfaceOrigin,
181 SkIRect srcRect,
182 SkImage::RescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -0500183 SkImage::RescaleMode);
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400184
Brian Salomon1c86b632020-12-11 12:36:01 -0500185 /**
Brian Salomonbacbb922021-01-21 19:48:00 -0500186 * Like the above but allows the caller ot specify a destination fill context and
Brian Salomon1c86b632020-12-11 12:36:01 -0500187 * rect within that context. The dst rect must be contained by the dst or this will fail.
188 */
Brian Salomonbacbb922021-01-21 19:48:00 -0500189 bool rescaleInto(GrSurfaceFillContext* dst,
Brian Salomon1c86b632020-12-11 12:36:01 -0500190 SkIRect dstRect,
191 SkIRect srcRect,
192 SkImage::RescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -0500193 SkImage::RescaleMode);
Brian Salomon1c86b632020-12-11 12:36:01 -0500194
Robert Phillips0d075de2019-03-04 11:08:13 -0500195 GrAuditTrail* auditTrail();
Brian Osman45580d32016-11-23 09:37:01 -0500196
Greg Daniel46cfbc62019-06-07 11:43:30 -0400197#if GR_TEST_UTILS
Brian Salomon982127b2021-01-21 10:43:35 -0500198 bool testCopy(sk_sp<GrSurfaceProxy> src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
Brian Salomond63638b2021-03-05 14:00:07 -0500199 return this->copy(std::move(src), srcRect, dstPoint) != nullptr;
Greg Daniel46cfbc62019-06-07 11:43:30 -0400200 }
201
Brian Salomon982127b2021-01-21 10:43:35 -0500202 bool testCopy(sk_sp<GrSurfaceProxy> src) {
203 auto rect = SkIRect::MakeSize(src->dimensions());
Brian Salomond63638b2021-03-05 14:00:07 -0500204 return this->copy(std::move(src), rect, {0, 0}) != nullptr;
Greg Daniel46cfbc62019-06-07 11:43:30 -0400205 }
206#endif
207
Brian Osman45580d32016-11-23 09:37:01 -0500208protected:
Robert Phillips0d075de2019-03-04 11:08:13 -0500209 GrDrawingManager* drawingManager();
210 const GrDrawingManager* drawingManager() const;
Brian Osman45580d32016-11-23 09:37:01 -0500211
Greg Daniel46e366a2019-12-16 14:38:36 -0500212 SkDEBUGCODE(void validate() const;)
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400213
Brian Salomon70fe17e2020-11-30 14:33:58 -0500214 SkDEBUGCODE(GrSingleOwner* singleOwner() const;)
Brian Osman45580d32016-11-23 09:37:01 -0500215
Robert Phillips69893702019-02-22 11:16:30 -0500216 GrRecordingContext* fContext;
Brian Osman45580d32016-11-23 09:37:01 -0500217
Greg Daniel3912a4b2020-01-14 09:56:04 -0500218 GrSurfaceProxyView fReadView;
Greg Daniel901b98e2019-10-22 09:54:02 -0400219
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400220 // Inserts a transfer, part of the implementation of asyncReadPixels and
221 // asyncRescaleAndReadPixelsYUV420().
222 struct PixelTransferResult {
223 using ConversionFn = void(void* dst, const void* mappedBuffer);
224 // If null then the transfer could not be performed. Otherwise this buffer will contain
225 // the pixel data when the transfer is complete.
226 sk_sp<GrGpuBuffer> fTransferBuffer;
227 // If this is null then the transfer buffer will contain the data in the requested
228 // color type. Otherwise, when the transfer is done this must be called to convert
229 // from the transfer buffer's color type to the requested color type.
230 std::function<ConversionFn> fPixelConverter;
231 };
232 PixelTransferResult transferPixels(GrColorType colorType, const SkIRect& rect);
233
Brian Salomon63a0a752020-06-26 13:32:09 -0400234 // The async read step of asyncRescaleAndReadPixels()
Adlai Hollerc95b5892020-08-11 12:02:22 -0400235 void asyncReadPixels(GrDirectContext*,
236 const SkIRect& srcRect,
237 SkColorType,
238 ReadPixelsCallback,
239 ReadPixelsContext);
Brian Salomon63a0a752020-06-26 13:32:09 -0400240
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500241private:
Greg Daniel46cfbc62019-06-07 11:43:30 -0400242 friend class GrSurfaceProxy; // for copy
243
Greg Daniel46e366a2019-12-16 14:38:36 -0500244 SkDEBUGCODE(virtual void onValidate() const {})
245
Greg Daniel46cfbc62019-06-07 11:43:30 -0400246 /**
247 * Copy 'src' into the proxy backing this context. This call will not do any draw fallback.
248 * Currently only writePixels and replaceRenderTarget call this directly. All other copies
249 * should go through GrSurfaceProxy::Copy.
250 * @param src src of pixels
Greg Daniel46cfbc62019-06-07 11:43:30 -0400251 * @param dstPoint the origin of the 'srcRect' in the destination coordinate space
Brian Salomond63638b2021-03-05 14:00:07 -0500252 * @return a task (that may be skippable by calling canSkip) if successful and
253 * null otherwise.
Greg Daniel46cfbc62019-06-07 11:43:30 -0400254 *
255 * Note: Notionally, 'srcRect' is clipped to 'src's extent with 'dstPoint' being adjusted.
256 * Then the 'srcRect' offset by 'dstPoint' is clipped against the dst's extent.
257 * The end result is only valid src pixels and dst pixels will be touched but the copied
258 * regions will not be shifted. The 'src' must have the same origin as the backing proxy
259 * of fSurfaceContext.
260 */
Brian Salomond63638b2021-03-05 14:00:07 -0500261 sk_sp<GrRenderTask> copy(sk_sp<GrSurfaceProxy> src, SkIRect srcRect, SkIPoint dstPoint);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400262
Brian Salomon2c673402021-04-02 14:36:58 -0400263 bool internalWritePixels(GrDirectContext* dContext,
264 const GrCPixmap src[],
265 int numLevels,
Brian Salomon75ee7372021-04-06 15:04:35 -0400266 SkIPoint);
Brian Salomon2c673402021-04-02 14:36:58 -0400267
Brian Salomon63a0a752020-06-26 13:32:09 -0400268 class AsyncReadResult;
269
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400270 GrColorInfo fColorInfo;
Brian Salomonf3569f02017-10-24 12:52:33 -0400271
John Stiles7571f9e2020-09-02 22:42:33 -0400272 using INHERITED = SkRefCnt;
Brian Osman45580d32016-11-23 09:37:01 -0500273};
274
275#endif