blob: 5096d2c06f0df6defff67f2fa2645325b11c8496 [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"
Greg Danielf91aeb22019-06-18 09:58:02 -040020#include "src/gpu/GrSurfaceProxy.h"
Greg Daniel901b98e2019-10-22 09:54:02 -040021#include "src/gpu/GrSurfaceProxyView.h"
Brian Osman45580d32016-11-23 09:37:01 -050022
23class GrAuditTrail;
Robert Phillips72152832017-01-25 17:31:35 -050024class GrDrawingManager;
Robert Phillips69893702019-02-22 11:16:30 -050025class GrRecordingContext;
Brian Salomoneebe7352020-12-09 16:37:04 -050026class GrSurfaceDrawContext;
Robert Phillips27341362016-12-14 08:46:47 -050027class GrRenderTargetProxy;
Brian Osman45580d32016-11-23 09:37:01 -050028class GrSingleOwner;
29class GrSurface;
Robert Phillips27341362016-12-14 08:46:47 -050030class GrSurfaceProxy;
31class GrTextureProxy;
Brian Osman45580d32016-11-23 09:37:01 -050032struct SkIPoint;
33struct SkIRect;
34
35/**
36 * A helper object to orchestrate commands for a particular surface
37 */
Brian Salomon57f211b2019-08-21 15:21:09 -040038class GrSurfaceContext {
Brian Osman45580d32016-11-23 09:37:01 -050039public:
Brian Salomoneebe7352020-12-09 16:37:04 -050040 // If the passed in GrSurfaceProxy is renderable this will return a GrSurfaceDrawContext,
Greg Danielbfa19c42019-12-19 16:41:40 -050041 // otherwise it will return a GrSurfaceContext.
Greg Daniel3912a4b2020-01-14 09:56:04 -050042 static std::unique_ptr<GrSurfaceContext> Make(GrRecordingContext*,
43 GrSurfaceProxyView readView,
Brian Salomon14f99fc2020-12-07 12:19:47 -050044 const GrColorInfo&);
Greg Danielbfa19c42019-12-19 16:41:40 -050045
Brian Salomoneebe7352020-12-09 16:37:04 -050046 // Makes either a GrSurfaceContext or a GrSurfaceDrawContext, depending on GrRenderable.
Brian Salomon14f99fc2020-12-07 12:19:47 -050047 static std::unique_ptr<GrSurfaceContext> Make(GrRecordingContext*,
48 const GrImageInfo&,
49 const GrBackendFormat&,
50 SkBackingFit = SkBackingFit::kExact,
51 GrSurfaceOrigin = kTopLeft_GrSurfaceOrigin,
52 GrRenderable = GrRenderable::kNo,
53 int renderTargetSampleCnt = 1,
54 GrMipmapped = GrMipmapped::kNo,
55 GrProtected = GrProtected::kNo,
56 SkBudgeted = SkBudgeted::kYes);
57
58 static std::unique_ptr<GrSurfaceContext> Make(GrRecordingContext*,
59 const GrImageInfo&,
60 SkBackingFit = SkBackingFit::kExact,
61 GrSurfaceOrigin = kTopLeft_GrSurfaceOrigin,
62 GrRenderable = GrRenderable::kNo,
63 int renderTargetSampleCnt = 1,
64 GrMipmapped = GrMipmapped::kNo,
65 GrProtected = GrProtected::kNo,
66 SkBudgeted = SkBudgeted::kYes);
Greg Danielbfa19c42019-12-19 16:41:40 -050067
68 // If it is known that the GrSurfaceProxy is not renderable, you can directly call the the ctor
69 // here to make a GrSurfaceContext on the stack.
Brian Salomon14f99fc2020-12-07 12:19:47 -050070 GrSurfaceContext(GrRecordingContext*, GrSurfaceProxyView readView, const GrColorInfo&);
Greg Danielbfa19c42019-12-19 16:41:40 -050071
Brian Salomonbf6b9792019-08-21 09:38:10 -040072 virtual ~GrSurfaceContext() = default;
Brian Osman45580d32016-11-23 09:37:01 -050073
Brian Salomon70fe17e2020-11-30 14:33:58 -050074 GrRecordingContext* recordingContext() { return fContext; }
75
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040076 const GrColorInfo& colorInfo() const { return fColorInfo; }
Brian Salomon6aa65052020-01-28 12:16:53 -050077 GrImageInfo imageInfo() const { return {fColorInfo, fReadView.proxy()->dimensions()}; }
78
Greg Daniel3912a4b2020-01-14 09:56:04 -050079 GrSurfaceOrigin origin() const { return fReadView.origin(); }
80 GrSwizzle readSwizzle() const { return fReadView.swizzle(); }
81 // TODO: See if it makes sense for this to return a const& instead and require the callers to
82 // make a copy (which refs the proxy) if needed.
83 GrSurfaceProxyView readSurfaceView() { return fReadView; }
Robert Phillips2c862492017-01-18 10:08:39 -050084
Brian Salomonc5243782020-04-02 12:50:34 -040085 SkISize dimensions() const { return fReadView.dimensions(); }
Greg Daniel3912a4b2020-01-14 09:56:04 -050086 int width() const { return fReadView.proxy()->width(); }
87 int height() const { return fReadView.proxy()->height(); }
Robert Phillipsd46697a2017-01-25 12:10:37 -050088
Brian Salomon4d2d6f42019-07-26 14:15:11 -040089 const GrCaps* caps() const;
90
Robert Phillips2c862492017-01-18 10:08:39 -050091 /**
92 * Reads a rectangle of pixels from the render target context.
Adlai Hollerc95b5892020-08-11 12:02:22 -040093 * @param dContext The direct context to use
Robert Phillips2c862492017-01-18 10:08:39 -050094 * @param dstInfo image info for the destination
Brian Salomon1d435302019-07-01 13:05:28 -040095 * @param dst destination pixels for the read
96 * @param rowBytes bytes in a row of 'dst'
97 * @param srcPt offset w/in the surface context from which to read
Brian Salomon1d435302019-07-01 13:05:28 -040098 * is a GrDirectContext and fail otherwise.
Robert Phillips2c862492017-01-18 10:08:39 -050099 */
Adlai Hollerc95b5892020-08-11 12:02:22 -0400100 bool readPixels(GrDirectContext* dContext,
101 const GrImageInfo& dstInfo,
102 void* dst,
103 size_t rowBytes,
104 SkIPoint srcPt);
Robert Phillips2c862492017-01-18 10:08:39 -0500105
Brian Salomon63a0a752020-06-26 13:32:09 -0400106 using ReadPixelsCallback = SkImage::ReadPixelsCallback;
107 using ReadPixelsContext = SkImage::ReadPixelsContext;
108 using RescaleGamma = SkImage::RescaleGamma;
109
110 // GPU implementation for SkImage:: and SkSurface::asyncRescaleAndReadPixels.
Adlai Hollerc95b5892020-08-11 12:02:22 -0400111 void asyncRescaleAndReadPixels(GrDirectContext*,
112 const SkImageInfo& info,
Brian Salomon63a0a752020-06-26 13:32:09 -0400113 const SkIRect& srcRect,
114 RescaleGamma rescaleGamma,
115 SkFilterQuality rescaleQuality,
116 ReadPixelsCallback callback,
Adlai Hollerc95b5892020-08-11 12:02:22 -0400117 ReadPixelsContext callbackContext);
Brian Salomon63a0a752020-06-26 13:32:09 -0400118
119 // GPU implementation for SkImage:: and SkSurface::asyncRescaleAndReadPixelsYUV420.
Adlai Hollerc95b5892020-08-11 12:02:22 -0400120 void asyncRescaleAndReadPixelsYUV420(GrDirectContext*,
121 SkYUVColorSpace yuvColorSpace,
Brian Salomon63a0a752020-06-26 13:32:09 -0400122 sk_sp<SkColorSpace> dstColorSpace,
123 const SkIRect& srcRect,
124 SkISize dstSize,
125 RescaleGamma rescaleGamma,
126 SkFilterQuality rescaleQuality,
127 ReadPixelsCallback callback,
128 ReadPixelsContext context);
129
Robert Phillips2c862492017-01-18 10:08:39 -0500130 /**
Robert Phillipsb726d582017-03-09 16:36:32 -0500131 * Writes a rectangle of pixels [srcInfo, srcBuffer, srcRowbytes] into the
Robert Phillips2c862492017-01-18 10:08:39 -0500132 * renderTargetContext at the specified position.
Adlai Hollerc95b5892020-08-11 12:02:22 -0400133 * @param dContext The direct context to use
Robert Phillips2c862492017-01-18 10:08:39 -0500134 * @param srcInfo image info for the source pixels
Brian Salomon1d435302019-07-01 13:05:28 -0400135 * @param src source for the write
136 * @param rowBytes bytes in a row of 'src'
137 * @param dstPt offset w/in the surface context at which to write
Robert Phillips2c862492017-01-18 10:08:39 -0500138 */
Adlai Hollerc95b5892020-08-11 12:02:22 -0400139 bool writePixels(GrDirectContext* dContext,
140 const GrImageInfo& srcInfo,
141 const void* src,
142 size_t rowBytes,
143 SkIPoint dstPt);
Greg Daniel6eb8c242019-06-05 10:22:24 -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 Salomoneebe7352020-12-09 16:37:04 -0500161 virtual GrSurfaceDrawContext* asRenderTargetContext() { 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 Salomoneebe7352020-12-09 16:37:04 -0500170 std::unique_ptr<GrSurfaceDrawContext> rescale(const GrImageInfo& info,
171 GrSurfaceOrigin,
172 SkIRect srcRect,
173 SkImage::RescaleGamma,
174 SkFilterQuality);
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400175
Brian Salomon1c86b632020-12-11 12:36:01 -0500176 /**
177 * Like the above but allows the caller ot specify a destination render target context and
178 * rect within that context. The dst rect must be contained by the dst or this will fail.
179 */
180 bool rescaleInto(GrSurfaceDrawContext* dst,
181 SkIRect dstRect,
182 SkIRect srcRect,
183 SkImage::RescaleGamma,
184 SkFilterQuality);
185
Robert Phillips0d075de2019-03-04 11:08:13 -0500186 GrAuditTrail* auditTrail();
Brian Osman45580d32016-11-23 09:37:01 -0500187
Greg Daniel46cfbc62019-06-07 11:43:30 -0400188#if GR_TEST_UTILS
Brian Salomonc5243782020-04-02 12:50:34 -0400189 bool testCopy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
190 return this->copy(src, srcRect, dstPoint);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400191 }
192
Brian Salomonc5243782020-04-02 12:50:34 -0400193 bool testCopy(GrSurfaceProxy* src) {
194 return this->copy(src, SkIRect::MakeSize(src->dimensions()), {0, 0});
Greg Daniel46cfbc62019-06-07 11:43:30 -0400195 }
196#endif
197
Brian Osman45580d32016-11-23 09:37:01 -0500198protected:
Robert Phillips0d075de2019-03-04 11:08:13 -0500199 GrDrawingManager* drawingManager();
200 const GrDrawingManager* drawingManager() const;
Brian Osman45580d32016-11-23 09:37:01 -0500201
Greg Daniel46e366a2019-12-16 14:38:36 -0500202 SkDEBUGCODE(void validate() const;)
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400203
Brian Salomon70fe17e2020-11-30 14:33:58 -0500204 SkDEBUGCODE(GrSingleOwner* singleOwner() const;)
Brian Osman45580d32016-11-23 09:37:01 -0500205
Robert Phillips69893702019-02-22 11:16:30 -0500206 GrRecordingContext* fContext;
Brian Osman45580d32016-11-23 09:37:01 -0500207
Greg Daniel3912a4b2020-01-14 09:56:04 -0500208 GrSurfaceProxyView fReadView;
Greg Daniel901b98e2019-10-22 09:54:02 -0400209
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400210 // Inserts a transfer, part of the implementation of asyncReadPixels and
211 // asyncRescaleAndReadPixelsYUV420().
212 struct PixelTransferResult {
213 using ConversionFn = void(void* dst, const void* mappedBuffer);
214 // If null then the transfer could not be performed. Otherwise this buffer will contain
215 // the pixel data when the transfer is complete.
216 sk_sp<GrGpuBuffer> fTransferBuffer;
217 // If this is null then the transfer buffer will contain the data in the requested
218 // color type. Otherwise, when the transfer is done this must be called to convert
219 // from the transfer buffer's color type to the requested color type.
220 std::function<ConversionFn> fPixelConverter;
221 };
222 PixelTransferResult transferPixels(GrColorType colorType, const SkIRect& rect);
223
Brian Salomon63a0a752020-06-26 13:32:09 -0400224 // The async read step of asyncRescaleAndReadPixels()
Adlai Hollerc95b5892020-08-11 12:02:22 -0400225 void asyncReadPixels(GrDirectContext*,
226 const SkIRect& srcRect,
227 SkColorType,
228 ReadPixelsCallback,
229 ReadPixelsContext);
Brian Salomon63a0a752020-06-26 13:32:09 -0400230
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500231private:
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
242 * @return true if the copy succeeded; false otherwise
243 *
244 * Note: Notionally, 'srcRect' is clipped to 'src's extent with 'dstPoint' being adjusted.
245 * Then the 'srcRect' offset by 'dstPoint' is clipped against the dst's extent.
246 * The end result is only valid src pixels and dst pixels will be touched but the copied
247 * regions will not be shifted. The 'src' must have the same origin as the backing proxy
248 * of fSurfaceContext.
249 */
Brian Salomonc5243782020-04-02 12:50:34 -0400250 bool copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400251
Brian Salomon63a0a752020-06-26 13:32:09 -0400252 class AsyncReadResult;
253
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400254 GrColorInfo fColorInfo;
Brian Salomonf3569f02017-10-24 12:52:33 -0400255
John Stiles7571f9e2020-09-02 22:42:33 -0400256 using INHERITED = SkRefCnt;
Brian Osman45580d32016-11-23 09:37:01 -0500257};
258
259#endif