blob: 069c56226fcc1687111dc788588eb2cb22ad296a [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 Salomon4bc0c1f2019-09-30 15:12:27 -040016#include "src/gpu/GrColorInfo.h"
Brian Salomon1d435302019-07-01 13:05:28 -040017#include "src/gpu/GrDataUtils.h"
Brian Salomon6aa65052020-01-28 12:16:53 -050018#include "src/gpu/GrImageInfo.h"
Brian Salomondd4087d2020-12-23 20:36:44 -050019#include "src/gpu/GrPixmap.h"
Brian Salomond63638b2021-03-05 14:00:07 -050020#include "src/gpu/GrRenderTask.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040021#include "src/gpu/GrSurfaceProxy.h"
Greg Daniel901b98e2019-10-22 09:54:02 -040022#include "src/gpu/GrSurfaceProxyView.h"
Brian Osman45580d32016-11-23 09:37:01 -050023
Robert Phillips72152832017-01-25 17:31:35 -050024class GrDrawingManager;
Robert Phillips69893702019-02-22 11:16:30 -050025class GrRecordingContext;
Robert Phillips27341362016-12-14 08:46:47 -050026class GrRenderTargetProxy;
Brian Osman45580d32016-11-23 09:37:01 -050027class GrSingleOwner;
28class GrSurface;
Robert Phillips4dca8312021-07-28 15:13:20 -040029namespace skgpu { namespace v1 { class SurfaceDrawContext; }}
Brian Salomon590f5672020-12-16 11:44:47 -050030class GrSurfaceFillContext;
Robert Phillips27341362016-12-14 08:46:47 -050031class GrSurfaceProxy;
32class GrTextureProxy;
Brian Osman45580d32016-11-23 09:37:01 -050033struct SkIPoint;
34struct SkIRect;
35
36/**
37 * A helper object to orchestrate commands for a particular surface
38 */
Brian Salomon57f211b2019-08-21 15:21:09 -040039class GrSurfaceContext {
Brian Osman45580d32016-11-23 09:37:01 -050040public:
Robert Phillips4dca8312021-07-28 15:13:20 -040041 // Makes either a GrSurfaceContext, GrFillDrawContext, or a SurfaceDrawContext, depending on
Brian Salomon590f5672020-12-16 11:44:47 -050042 // GrRenderable and the GrImageInfo.
Brian Salomon14f99fc2020-12-07 12:19:47 -050043 static std::unique_ptr<GrSurfaceContext> Make(GrRecordingContext*,
44 const GrImageInfo&,
45 const GrBackendFormat&,
46 SkBackingFit = SkBackingFit::kExact,
47 GrSurfaceOrigin = kTopLeft_GrSurfaceOrigin,
48 GrRenderable = GrRenderable::kNo,
49 int renderTargetSampleCnt = 1,
50 GrMipmapped = GrMipmapped::kNo,
51 GrProtected = GrProtected::kNo,
52 SkBudgeted = SkBudgeted::kYes);
53
Brian Salomon590f5672020-12-16 11:44:47 -050054 // Same as the above but chooses the texture format using the default format for the color type.
Brian Salomon14f99fc2020-12-07 12:19:47 -050055 static std::unique_ptr<GrSurfaceContext> Make(GrRecordingContext*,
56 const GrImageInfo&,
57 SkBackingFit = SkBackingFit::kExact,
58 GrSurfaceOrigin = kTopLeft_GrSurfaceOrigin,
59 GrRenderable = GrRenderable::kNo,
60 int renderTargetSampleCnt = 1,
61 GrMipmapped = GrMipmapped::kNo,
62 GrProtected = GrProtected::kNo,
63 SkBudgeted = SkBudgeted::kYes);
Greg Danielbfa19c42019-12-19 16:41:40 -050064
65 // If it is known that the GrSurfaceProxy is not renderable, you can directly call the the ctor
66 // here to make a GrSurfaceContext on the stack.
Brian Salomon14f99fc2020-12-07 12:19:47 -050067 GrSurfaceContext(GrRecordingContext*, GrSurfaceProxyView readView, const GrColorInfo&);
Greg Danielbfa19c42019-12-19 16:41:40 -050068
Brian Salomonbf6b9792019-08-21 09:38:10 -040069 virtual ~GrSurfaceContext() = default;
Brian Osman45580d32016-11-23 09:37:01 -050070
Chris Dalton66deeb22021-07-23 13:57:03 -060071 GrRecordingContext* recordingContext() const { return fContext; }
Brian Salomon70fe17e2020-11-30 14:33:58 -050072
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040073 const GrColorInfo& colorInfo() const { return fColorInfo; }
Brian Salomon6aa65052020-01-28 12:16:53 -050074 GrImageInfo imageInfo() const { return {fColorInfo, fReadView.proxy()->dimensions()}; }
75
Greg Daniel3912a4b2020-01-14 09:56:04 -050076 GrSurfaceOrigin origin() const { return fReadView.origin(); }
77 GrSwizzle readSwizzle() const { return fReadView.swizzle(); }
78 // TODO: See if it makes sense for this to return a const& instead and require the callers to
79 // make a copy (which refs the proxy) if needed.
80 GrSurfaceProxyView readSurfaceView() { return fReadView; }
Robert Phillips2c862492017-01-18 10:08:39 -050081
Brian Salomonc5243782020-04-02 12:50:34 -040082 SkISize dimensions() const { return fReadView.dimensions(); }
Greg Daniel3912a4b2020-01-14 09:56:04 -050083 int width() const { return fReadView.proxy()->width(); }
84 int height() const { return fReadView.proxy()->height(); }
Robert Phillipsd46697a2017-01-25 12:10:37 -050085
Robert Phillips9af0bca2021-05-27 19:17:55 -040086 GrMipmapped mipmapped() const { return fReadView.mipmapped(); }
87
Brian Salomon4d2d6f42019-07-26 14:15:11 -040088 const GrCaps* caps() const;
89
Robert Phillips2c862492017-01-18 10:08:39 -050090 /**
Brian Salomondd4087d2020-12-23 20:36:44 -050091 * Reads a rectangle of pixels from the surface context.
Adlai Hollerc95b5892020-08-11 12:02:22 -040092 * @param dContext The direct context to use
Brian Salomon1d435302019-07-01 13:05:28 -040093 * @param dst destination pixels for the read
Brian Salomon1d435302019-07-01 13:05:28 -040094 * @param srcPt offset w/in the surface context from which to read
Brian Salomon1d435302019-07-01 13:05:28 -040095 * is a GrDirectContext and fail otherwise.
Robert Phillips2c862492017-01-18 10:08:39 -050096 */
Brian Salomondd4087d2020-12-23 20:36:44 -050097 bool readPixels(GrDirectContext* dContext, GrPixmap dst, SkIPoint srcPt);
Robert Phillips2c862492017-01-18 10:08:39 -050098
Brian Salomon63a0a752020-06-26 13:32:09 -040099 using ReadPixelsCallback = SkImage::ReadPixelsCallback;
100 using ReadPixelsContext = SkImage::ReadPixelsContext;
101 using RescaleGamma = SkImage::RescaleGamma;
Mike Reed1efa14d2021-01-02 21:44:59 -0500102 using RescaleMode = SkImage::RescaleMode;
Brian Salomon63a0a752020-06-26 13:32:09 -0400103
104 // GPU implementation for SkImage:: and SkSurface::asyncRescaleAndReadPixels.
Adlai Hollerc95b5892020-08-11 12:02:22 -0400105 void asyncRescaleAndReadPixels(GrDirectContext*,
106 const SkImageInfo& info,
Brian Salomon63a0a752020-06-26 13:32:09 -0400107 const SkIRect& srcRect,
108 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -0500109 RescaleMode,
Brian Salomon63a0a752020-06-26 13:32:09 -0400110 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400111 ReadPixelsContext callbackContext);
Brian Salomon63a0a752020-06-26 13:32:09 -0400112
113 // GPU implementation for SkImage:: and SkSurface::asyncRescaleAndReadPixelsYUV420.
Adlai Hollerc95b5892020-08-11 12:02:22 -0400114 void asyncRescaleAndReadPixelsYUV420(GrDirectContext*,
115 SkYUVColorSpace yuvColorSpace,
Brian Salomon63a0a752020-06-26 13:32:09 -0400116 sk_sp<SkColorSpace> dstColorSpace,
117 const SkIRect& srcRect,
118 SkISize dstSize,
119 RescaleGamma rescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -0500120 RescaleMode,
Brian Salomon63a0a752020-06-26 13:32:09 -0400121 ReadPixelsCallback callback,
122 ReadPixelsContext context);
123
Robert Phillips2c862492017-01-18 10:08:39 -0500124 /**
Brian Salomon2c673402021-04-02 14:36:58 -0400125 * Writes a rectangle of pixels from src into the surfaceDrawContext at the specified position.
126 * @param dContext The direct context to use
127 * @param src source for the write
128 * @param dstPt offset w/in the surface context at which to write
Robert Phillips2c862492017-01-18 10:08:39 -0500129 */
Brian Salomonea1d39b2021-04-01 17:06:52 -0400130 bool writePixels(GrDirectContext* dContext,
131 GrCPixmap src,
Brian Salomon75ee7372021-04-06 15:04:35 -0400132 SkIPoint dstPt);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400133
Brian Salomon2c673402021-04-02 14:36:58 -0400134 /**
135 * Fully populates either the base level or all MIP levels of the GrSurface with pixel data.
136 * @param dContext The direct context to use
137 * @param src Array of pixmaps
138 * @param numLevels Number of pixmaps in src. To succeed this must be 1 or the total
139 * number of MIP levels.
140 */
Brian Salomonea1d39b2021-04-01 17:06:52 -0400141 bool writePixels(GrDirectContext* dContext,
142 const GrCPixmap src[],
Brian Salomon75ee7372021-04-06 15:04:35 -0400143 int numLevels);
Brian Salomon2c673402021-04-02 14:36:58 -0400144
Greg Daniel3912a4b2020-01-14 09:56:04 -0500145 GrSurfaceProxy* asSurfaceProxy() { return fReadView.proxy(); }
146 const GrSurfaceProxy* asSurfaceProxy() const { return fReadView.proxy(); }
Greg Danielc61d7e32020-02-04 14:27:45 -0500147 sk_sp<GrSurfaceProxy> asSurfaceProxyRef() { return fReadView.refProxy(); }
Robert Phillipsf200a902017-01-30 13:27:37 -0500148
Greg Daniel3912a4b2020-01-14 09:56:04 -0500149 GrTextureProxy* asTextureProxy() { return fReadView.asTextureProxy(); }
150 const GrTextureProxy* asTextureProxy() const { return fReadView.asTextureProxy(); }
151 sk_sp<GrTextureProxy> asTextureProxyRef() { return fReadView.asTextureProxyRef(); }
Robert Phillipsf200a902017-01-30 13:27:37 -0500152
Greg Daniel3912a4b2020-01-14 09:56:04 -0500153 GrRenderTargetProxy* asRenderTargetProxy() { return fReadView.asRenderTargetProxy(); }
Greg Daniel46e366a2019-12-16 14:38:36 -0500154 const GrRenderTargetProxy* asRenderTargetProxy() const {
Greg Daniel3912a4b2020-01-14 09:56:04 -0500155 return fReadView.asRenderTargetProxy();
Greg Daniel46e366a2019-12-16 14:38:36 -0500156 }
157 sk_sp<GrRenderTargetProxy> asRenderTargetProxyRef() {
Greg Daniel3912a4b2020-01-14 09:56:04 -0500158 return fReadView.asRenderTargetProxyRef();
Greg Daniel46e366a2019-12-16 14:38:36 -0500159 }
Robert Phillips27341362016-12-14 08:46:47 -0500160
Brian Salomon590f5672020-12-16 11:44:47 -0500161 virtual GrSurfaceFillContext* asFillContext() { return nullptr; }
Robert Phillipsd46697a2017-01-25 12:10:37 -0500162
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400163 /**
164 * Rescales the contents of srcRect. The gamma in which the rescaling occurs is controlled by
165 * RescaleGamma. It is always in the original gamut. The result is converted to the color type
166 * and color space of info after rescaling. Note: this currently requires that the info have a
167 * different size than srcRect. Though, it could be relaxed to allow non-scaling color
168 * conversions.
169 */
Brian Salomonbacbb922021-01-21 19:48:00 -0500170 std::unique_ptr<GrSurfaceFillContext> rescale(const GrImageInfo& info,
Brian Salomoneebe7352020-12-09 16:37:04 -0500171 GrSurfaceOrigin,
172 SkIRect srcRect,
173 SkImage::RescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -0500174 SkImage::RescaleMode);
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400175
Brian Salomon1c86b632020-12-11 12:36:01 -0500176 /**
Brian Salomonbacbb922021-01-21 19:48:00 -0500177 * Like the above but allows the caller ot specify a destination fill context and
Brian Salomon1c86b632020-12-11 12:36:01 -0500178 * rect within that context. The dst rect must be contained by the dst or this will fail.
179 */
Brian Salomonbacbb922021-01-21 19:48:00 -0500180 bool rescaleInto(GrSurfaceFillContext* dst,
Brian Salomon1c86b632020-12-11 12:36:01 -0500181 SkIRect dstRect,
182 SkIRect srcRect,
183 SkImage::RescaleGamma,
Mike Reed1efa14d2021-01-02 21:44:59 -0500184 SkImage::RescaleMode);
Brian Salomon1c86b632020-12-11 12:36:01 -0500185
Greg Daniel46cfbc62019-06-07 11:43:30 -0400186#if GR_TEST_UTILS
Brian Salomon982127b2021-01-21 10:43:35 -0500187 bool testCopy(sk_sp<GrSurfaceProxy> src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
Brian Salomond63638b2021-03-05 14:00:07 -0500188 return this->copy(std::move(src), srcRect, dstPoint) != nullptr;
Greg Daniel46cfbc62019-06-07 11:43:30 -0400189 }
190
Brian Salomon982127b2021-01-21 10:43:35 -0500191 bool testCopy(sk_sp<GrSurfaceProxy> src) {
192 auto rect = SkIRect::MakeSize(src->dimensions());
Brian Salomond63638b2021-03-05 14:00:07 -0500193 return this->copy(std::move(src), rect, {0, 0}) != nullptr;
Greg Daniel46cfbc62019-06-07 11:43:30 -0400194 }
195#endif
196
Brian Osman45580d32016-11-23 09:37:01 -0500197protected:
Robert Phillips0d075de2019-03-04 11:08:13 -0500198 GrDrawingManager* drawingManager();
199 const GrDrawingManager* drawingManager() const;
Brian Osman45580d32016-11-23 09:37:01 -0500200
Greg Daniel46e366a2019-12-16 14:38:36 -0500201 SkDEBUGCODE(void validate() const;)
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400202
Brian Salomon70fe17e2020-11-30 14:33:58 -0500203 SkDEBUGCODE(GrSingleOwner* singleOwner() const;)
Brian Osman45580d32016-11-23 09:37:01 -0500204
Robert Phillips69893702019-02-22 11:16:30 -0500205 GrRecordingContext* fContext;
Brian Osman45580d32016-11-23 09:37:01 -0500206
Greg Daniel3912a4b2020-01-14 09:56:04 -0500207 GrSurfaceProxyView fReadView;
Greg Daniel901b98e2019-10-22 09:54:02 -0400208
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400209 // Inserts a transfer, part of the implementation of asyncReadPixels and
210 // asyncRescaleAndReadPixelsYUV420().
211 struct PixelTransferResult {
212 using ConversionFn = void(void* dst, const void* mappedBuffer);
213 // If null then the transfer could not be performed. Otherwise this buffer will contain
214 // the pixel data when the transfer is complete.
215 sk_sp<GrGpuBuffer> fTransferBuffer;
216 // If this is null then the transfer buffer will contain the data in the requested
217 // color type. Otherwise, when the transfer is done this must be called to convert
218 // from the transfer buffer's color type to the requested color type.
219 std::function<ConversionFn> fPixelConverter;
220 };
221 PixelTransferResult transferPixels(GrColorType colorType, const SkIRect& rect);
222
Brian Salomon63a0a752020-06-26 13:32:09 -0400223 // The async read step of asyncRescaleAndReadPixels()
Adlai Hollerc95b5892020-08-11 12:02:22 -0400224 void asyncReadPixels(GrDirectContext*,
225 const SkIRect& srcRect,
226 SkColorType,
227 ReadPixelsCallback,
228 ReadPixelsContext);
Brian Salomon63a0a752020-06-26 13:32:09 -0400229
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500230private:
Robert Phillips33bf2b52021-08-02 11:14:38 -0400231 friend class GrRecordingContextPriv; // for validate
Greg Daniel46cfbc62019-06-07 11:43:30 -0400232 friend class GrSurfaceProxy; // for copy
233
Greg Daniel46e366a2019-12-16 14:38:36 -0500234 SkDEBUGCODE(virtual void onValidate() const {})
235
Greg Daniel46cfbc62019-06-07 11:43:30 -0400236 /**
237 * Copy 'src' into the proxy backing this context. This call will not do any draw fallback.
238 * Currently only writePixels and replaceRenderTarget call this directly. All other copies
239 * should go through GrSurfaceProxy::Copy.
240 * @param src src of pixels
Greg Daniel46cfbc62019-06-07 11:43:30 -0400241 * @param dstPoint the origin of the 'srcRect' in the destination coordinate space
Brian Salomond63638b2021-03-05 14:00:07 -0500242 * @return a task (that may be skippable by calling canSkip) if successful and
243 * null otherwise.
Greg Daniel46cfbc62019-06-07 11:43:30 -0400244 *
245 * Note: Notionally, 'srcRect' is clipped to 'src's extent with 'dstPoint' being adjusted.
246 * Then the 'srcRect' offset by 'dstPoint' is clipped against the dst's extent.
247 * The end result is only valid src pixels and dst pixels will be touched but the copied
248 * regions will not be shifted. The 'src' must have the same origin as the backing proxy
249 * of fSurfaceContext.
250 */
Brian Salomond63638b2021-03-05 14:00:07 -0500251 sk_sp<GrRenderTask> copy(sk_sp<GrSurfaceProxy> src, SkIRect srcRect, SkIPoint dstPoint);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400252
Brian Salomon2c673402021-04-02 14:36:58 -0400253 bool internalWritePixels(GrDirectContext* dContext,
254 const GrCPixmap src[],
255 int numLevels,
Brian Salomon75ee7372021-04-06 15:04:35 -0400256 SkIPoint);
Brian Salomon2c673402021-04-02 14:36:58 -0400257
Brian Salomon63a0a752020-06-26 13:32:09 -0400258 class AsyncReadResult;
259
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400260 GrColorInfo fColorInfo;
Brian Salomonf3569f02017-10-24 12:52:33 -0400261
John Stiles7571f9e2020-09-02 22:42:33 -0400262 using INHERITED = SkRefCnt;
Brian Osman45580d32016-11-23 09:37:01 -0500263};
264
265#endif