blob: e8f16f8582e79adb0b96e1cc3d7b716ad3472dd3 [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 Salomone9ad9982019-07-22 16:17:41 -040011#include "include/core/SkFilterQuality.h"
Brian Salomon63a0a752020-06-26 13:32:09 -040012#include "include/core/SkImage.h"
Brian Salomone9ad9982019-07-22 16:17:41 -040013#include "include/core/SkRect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/core/SkRefCnt.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"
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
24class GrAuditTrail;
Robert Phillips72152832017-01-25 17:31:35 -050025class GrDrawingManager;
Robert Phillips69893702019-02-22 11:16:30 -050026class GrRecordingContext;
Robert Phillips27341362016-12-14 08:46:47 -050027class GrRenderTargetProxy;
Brian Osman45580d32016-11-23 09:37:01 -050028class GrSingleOwner;
29class GrSurface;
Brian Salomon590f5672020-12-16 11:44:47 -050030class GrSurfaceDrawContext;
31class GrSurfaceFillContext;
Robert Phillips27341362016-12-14 08:46:47 -050032class GrSurfaceProxy;
33class GrTextureProxy;
Brian Osman45580d32016-11-23 09:37:01 -050034struct SkIPoint;
35struct SkIRect;
36
37/**
38 * A helper object to orchestrate commands for a particular surface
39 */
Brian Salomon57f211b2019-08-21 15:21:09 -040040class GrSurfaceContext {
Brian Osman45580d32016-11-23 09:37:01 -050041public:
Brian Salomoneebe7352020-12-09 16:37:04 -050042 // If the passed in GrSurfaceProxy is renderable this will return a GrSurfaceDrawContext,
Greg Danielbfa19c42019-12-19 16:41:40 -050043 // otherwise it will return a GrSurfaceContext.
Greg Daniel3912a4b2020-01-14 09:56:04 -050044 static std::unique_ptr<GrSurfaceContext> Make(GrRecordingContext*,
45 GrSurfaceProxyView readView,
Brian Salomon14f99fc2020-12-07 12:19:47 -050046 const GrColorInfo&);
Greg Danielbfa19c42019-12-19 16:41:40 -050047
Brian Salomon590f5672020-12-16 11:44:47 -050048 // Makes either a GrSurfaceContext, GrFillDrawContext, or a GrSurfaceDrawContext, depending on
49 // GrRenderable and the GrImageInfo.
Brian Salomon14f99fc2020-12-07 12:19:47 -050050 static std::unique_ptr<GrSurfaceContext> Make(GrRecordingContext*,
51 const GrImageInfo&,
52 const GrBackendFormat&,
53 SkBackingFit = SkBackingFit::kExact,
54 GrSurfaceOrigin = kTopLeft_GrSurfaceOrigin,
55 GrRenderable = GrRenderable::kNo,
56 int renderTargetSampleCnt = 1,
57 GrMipmapped = GrMipmapped::kNo,
58 GrProtected = GrProtected::kNo,
59 SkBudgeted = SkBudgeted::kYes);
60
Brian Salomon590f5672020-12-16 11:44:47 -050061 // Same as the above but chooses the texture format using the default format for the color type.
Brian Salomon14f99fc2020-12-07 12:19:47 -050062 static std::unique_ptr<GrSurfaceContext> Make(GrRecordingContext*,
63 const GrImageInfo&,
64 SkBackingFit = SkBackingFit::kExact,
65 GrSurfaceOrigin = kTopLeft_GrSurfaceOrigin,
66 GrRenderable = GrRenderable::kNo,
67 int renderTargetSampleCnt = 1,
68 GrMipmapped = GrMipmapped::kNo,
69 GrProtected = GrProtected::kNo,
70 SkBudgeted = SkBudgeted::kYes);
Greg Danielbfa19c42019-12-19 16:41:40 -050071
72 // If it is known that the GrSurfaceProxy is not renderable, you can directly call the the ctor
73 // here to make a GrSurfaceContext on the stack.
Brian Salomon14f99fc2020-12-07 12:19:47 -050074 GrSurfaceContext(GrRecordingContext*, GrSurfaceProxyView readView, const GrColorInfo&);
Greg Danielbfa19c42019-12-19 16:41:40 -050075
Brian Salomonbf6b9792019-08-21 09:38:10 -040076 virtual ~GrSurfaceContext() = default;
Brian Osman45580d32016-11-23 09:37:01 -050077
Brian Salomon70fe17e2020-11-30 14:33:58 -050078 GrRecordingContext* recordingContext() { return fContext; }
79
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040080 const GrColorInfo& colorInfo() const { return fColorInfo; }
Brian Salomon6aa65052020-01-28 12:16:53 -050081 GrImageInfo imageInfo() const { return {fColorInfo, fReadView.proxy()->dimensions()}; }
82
Greg Daniel3912a4b2020-01-14 09:56:04 -050083 GrSurfaceOrigin origin() const { return fReadView.origin(); }
84 GrSwizzle readSwizzle() const { return fReadView.swizzle(); }
85 // TODO: See if it makes sense for this to return a const& instead and require the callers to
86 // make a copy (which refs the proxy) if needed.
87 GrSurfaceProxyView readSurfaceView() { return fReadView; }
Robert Phillips2c862492017-01-18 10:08:39 -050088
Brian Salomonc5243782020-04-02 12:50:34 -040089 SkISize dimensions() const { return fReadView.dimensions(); }
Greg Daniel3912a4b2020-01-14 09:56:04 -050090 int width() const { return fReadView.proxy()->width(); }
91 int height() const { return fReadView.proxy()->height(); }
Robert Phillipsd46697a2017-01-25 12:10:37 -050092
Brian Salomon4d2d6f42019-07-26 14:15:11 -040093 const GrCaps* caps() const;
94
Robert Phillips2c862492017-01-18 10:08:39 -050095 /**
Brian Salomondd4087d2020-12-23 20:36:44 -050096 * Reads a rectangle of pixels from the surface context.
Adlai Hollerc95b5892020-08-11 12:02:22 -040097 * @param dContext The direct context to use
Brian Salomon1d435302019-07-01 13:05:28 -040098 * @param dst destination pixels for the read
Brian Salomon1d435302019-07-01 13:05:28 -040099 * @param srcPt offset w/in the surface context from which to read
Brian Salomon1d435302019-07-01 13:05:28 -0400100 * is a GrDirectContext and fail otherwise.
Robert Phillips2c862492017-01-18 10:08:39 -0500101 */
Brian Salomondd4087d2020-12-23 20:36:44 -0500102 bool readPixels(GrDirectContext* dContext, GrPixmap dst, SkIPoint srcPt);
Robert Phillips2c862492017-01-18 10:08:39 -0500103
Brian Salomon63a0a752020-06-26 13:32:09 -0400104 using ReadPixelsCallback = SkImage::ReadPixelsCallback;
105 using ReadPixelsContext = SkImage::ReadPixelsContext;
106 using RescaleGamma = SkImage::RescaleGamma;
107
108 // GPU implementation for SkImage:: and SkSurface::asyncRescaleAndReadPixels.
Adlai Hollerc95b5892020-08-11 12:02:22 -0400109 void asyncRescaleAndReadPixels(GrDirectContext*,
110 const SkImageInfo& info,
Brian Salomon63a0a752020-06-26 13:32:09 -0400111 const SkIRect& srcRect,
112 RescaleGamma rescaleGamma,
113 SkFilterQuality rescaleQuality,
114 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400115 ReadPixelsContext callbackContext);
Brian Salomon63a0a752020-06-26 13:32:09 -0400116
117 // GPU implementation for SkImage:: and SkSurface::asyncRescaleAndReadPixelsYUV420.
Adlai Hollerc95b5892020-08-11 12:02:22 -0400118 void asyncRescaleAndReadPixelsYUV420(GrDirectContext*,
119 SkYUVColorSpace yuvColorSpace,
Brian Salomon63a0a752020-06-26 13:32:09 -0400120 sk_sp<SkColorSpace> dstColorSpace,
121 const SkIRect& srcRect,
122 SkISize dstSize,
123 RescaleGamma rescaleGamma,
124 SkFilterQuality rescaleQuality,
125 ReadPixelsCallback callback,
126 ReadPixelsContext context);
127
Robert Phillips2c862492017-01-18 10:08:39 -0500128 /**
Robert Phillipsb726d582017-03-09 16:36:32 -0500129 * Writes a rectangle of pixels [srcInfo, srcBuffer, srcRowbytes] into the
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500130 * surfaceDrawContext at the specified position.
Adlai Hollerc95b5892020-08-11 12:02:22 -0400131 * @param dContext The direct context to use
Brian Salomon1d435302019-07-01 13:05:28 -0400132 * @param src source for the write
Brian Salomon1d435302019-07-01 13:05:28 -0400133 * @param dstPt offset w/in the surface context at which to write
Robert Phillips2c862492017-01-18 10:08:39 -0500134 */
Brian Salomondd4087d2020-12-23 20:36:44 -0500135 bool writePixels(GrDirectContext* dContext, GrPixmap src, SkIPoint dstPt);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400136
Greg Daniel3912a4b2020-01-14 09:56:04 -0500137 GrSurfaceProxy* asSurfaceProxy() { return fReadView.proxy(); }
138 const GrSurfaceProxy* asSurfaceProxy() const { return fReadView.proxy(); }
Greg Danielc61d7e32020-02-04 14:27:45 -0500139 sk_sp<GrSurfaceProxy> asSurfaceProxyRef() { return fReadView.refProxy(); }
Robert Phillipsf200a902017-01-30 13:27:37 -0500140
Greg Daniel3912a4b2020-01-14 09:56:04 -0500141 GrTextureProxy* asTextureProxy() { return fReadView.asTextureProxy(); }
142 const GrTextureProxy* asTextureProxy() const { return fReadView.asTextureProxy(); }
143 sk_sp<GrTextureProxy> asTextureProxyRef() { return fReadView.asTextureProxyRef(); }
Robert Phillipsf200a902017-01-30 13:27:37 -0500144
Greg Daniel3912a4b2020-01-14 09:56:04 -0500145 GrRenderTargetProxy* asRenderTargetProxy() { return fReadView.asRenderTargetProxy(); }
Greg Daniel46e366a2019-12-16 14:38:36 -0500146 const GrRenderTargetProxy* asRenderTargetProxy() const {
Greg Daniel3912a4b2020-01-14 09:56:04 -0500147 return fReadView.asRenderTargetProxy();
Greg Daniel46e366a2019-12-16 14:38:36 -0500148 }
149 sk_sp<GrRenderTargetProxy> asRenderTargetProxyRef() {
Greg Daniel3912a4b2020-01-14 09:56:04 -0500150 return fReadView.asRenderTargetProxyRef();
Greg Daniel46e366a2019-12-16 14:38:36 -0500151 }
Robert Phillips27341362016-12-14 08:46:47 -0500152
Brian Salomoneebe7352020-12-09 16:37:04 -0500153 virtual GrSurfaceDrawContext* asRenderTargetContext() { return nullptr; }
Brian Salomon590f5672020-12-16 11:44:47 -0500154 virtual GrSurfaceFillContext* asFillContext() { return nullptr; }
Robert Phillipsd46697a2017-01-25 12:10:37 -0500155
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400156 /**
157 * Rescales the contents of srcRect. The gamma in which the rescaling occurs is controlled by
158 * RescaleGamma. It is always in the original gamut. The result is converted to the color type
159 * and color space of info after rescaling. Note: this currently requires that the info have a
160 * different size than srcRect. Though, it could be relaxed to allow non-scaling color
161 * conversions.
162 */
Brian Salomoneebe7352020-12-09 16:37:04 -0500163 std::unique_ptr<GrSurfaceDrawContext> rescale(const GrImageInfo& info,
164 GrSurfaceOrigin,
165 SkIRect srcRect,
166 SkImage::RescaleGamma,
167 SkFilterQuality);
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400168
Brian Salomon1c86b632020-12-11 12:36:01 -0500169 /**
Brian Salomondd4087d2020-12-23 20:36:44 -0500170 * Like the above but allows the caller ot specify a destination draw context and
Brian Salomon1c86b632020-12-11 12:36:01 -0500171 * rect within that context. The dst rect must be contained by the dst or this will fail.
172 */
173 bool rescaleInto(GrSurfaceDrawContext* dst,
174 SkIRect dstRect,
175 SkIRect srcRect,
176 SkImage::RescaleGamma,
177 SkFilterQuality);
178
Robert Phillips0d075de2019-03-04 11:08:13 -0500179 GrAuditTrail* auditTrail();
Brian Osman45580d32016-11-23 09:37:01 -0500180
Greg Daniel46cfbc62019-06-07 11:43:30 -0400181#if GR_TEST_UTILS
Brian Salomonc5243782020-04-02 12:50:34 -0400182 bool testCopy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
183 return this->copy(src, srcRect, dstPoint);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400184 }
185
Brian Salomonc5243782020-04-02 12:50:34 -0400186 bool testCopy(GrSurfaceProxy* src) {
187 return this->copy(src, SkIRect::MakeSize(src->dimensions()), {0, 0});
Greg Daniel46cfbc62019-06-07 11:43:30 -0400188 }
189#endif
190
Brian Osman45580d32016-11-23 09:37:01 -0500191protected:
Robert Phillips0d075de2019-03-04 11:08:13 -0500192 GrDrawingManager* drawingManager();
193 const GrDrawingManager* drawingManager() const;
Brian Osman45580d32016-11-23 09:37:01 -0500194
Greg Daniel46e366a2019-12-16 14:38:36 -0500195 SkDEBUGCODE(void validate() const;)
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400196
Brian Salomon70fe17e2020-11-30 14:33:58 -0500197 SkDEBUGCODE(GrSingleOwner* singleOwner() const;)
Brian Osman45580d32016-11-23 09:37:01 -0500198
Robert Phillips69893702019-02-22 11:16:30 -0500199 GrRecordingContext* fContext;
Brian Osman45580d32016-11-23 09:37:01 -0500200
Greg Daniel3912a4b2020-01-14 09:56:04 -0500201 GrSurfaceProxyView fReadView;
Greg Daniel901b98e2019-10-22 09:54:02 -0400202
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400203 // Inserts a transfer, part of the implementation of asyncReadPixels and
204 // asyncRescaleAndReadPixelsYUV420().
205 struct PixelTransferResult {
206 using ConversionFn = void(void* dst, const void* mappedBuffer);
207 // If null then the transfer could not be performed. Otherwise this buffer will contain
208 // the pixel data when the transfer is complete.
209 sk_sp<GrGpuBuffer> fTransferBuffer;
210 // If this is null then the transfer buffer will contain the data in the requested
211 // color type. Otherwise, when the transfer is done this must be called to convert
212 // from the transfer buffer's color type to the requested color type.
213 std::function<ConversionFn> fPixelConverter;
214 };
215 PixelTransferResult transferPixels(GrColorType colorType, const SkIRect& rect);
216
Brian Salomon63a0a752020-06-26 13:32:09 -0400217 // The async read step of asyncRescaleAndReadPixels()
Adlai Hollerc95b5892020-08-11 12:02:22 -0400218 void asyncReadPixels(GrDirectContext*,
219 const SkIRect& srcRect,
220 SkColorType,
221 ReadPixelsCallback,
222 ReadPixelsContext);
Brian Salomon63a0a752020-06-26 13:32:09 -0400223
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500224private:
Greg Daniel46cfbc62019-06-07 11:43:30 -0400225 friend class GrSurfaceProxy; // for copy
226
Greg Daniel46e366a2019-12-16 14:38:36 -0500227 SkDEBUGCODE(virtual void onValidate() const {})
228
Greg Daniel46cfbc62019-06-07 11:43:30 -0400229 /**
230 * Copy 'src' into the proxy backing this context. This call will not do any draw fallback.
231 * Currently only writePixels and replaceRenderTarget call this directly. All other copies
232 * should go through GrSurfaceProxy::Copy.
233 * @param src src of pixels
Greg Daniel46cfbc62019-06-07 11:43:30 -0400234 * @param dstPoint the origin of the 'srcRect' in the destination coordinate space
235 * @return true if the copy succeeded; false otherwise
236 *
237 * Note: Notionally, 'srcRect' is clipped to 'src's extent with 'dstPoint' being adjusted.
238 * Then the 'srcRect' offset by 'dstPoint' is clipped against the dst's extent.
239 * The end result is only valid src pixels and dst pixels will be touched but the copied
240 * regions will not be shifted. The 'src' must have the same origin as the backing proxy
241 * of fSurfaceContext.
242 */
Brian Salomonc5243782020-04-02 12:50:34 -0400243 bool copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400244
Brian Salomon63a0a752020-06-26 13:32:09 -0400245 class AsyncReadResult;
246
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400247 GrColorInfo fColorInfo;
Brian Salomonf3569f02017-10-24 12:52:33 -0400248
John Stiles7571f9e2020-09-02 22:42:33 -0400249 using INHERITED = SkRefCnt;
Brian Osman45580d32016-11-23 09:37:01 -0500250};
251
252#endif