blob: 1571f4eb3d2e8c08019220b10a3c6c49e87dc0f5 [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;
Robert Phillipsd46697a2017-01-25 12:10:37 -050026class GrRenderTargetContext;
Robert Phillips27341362016-12-14 08:46:47 -050027class GrRenderTargetProxy;
Brian Osman45580d32016-11-23 09:37:01 -050028class GrSingleOwner;
29class GrSurface;
Robert Phillipse305cc1f2016-12-14 12:19:05 -050030class GrSurfaceContextPriv;
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:
Greg Danielbfa19c42019-12-19 16:41:40 -050041 // If the passed in GrSurfaceProxy is renderable this will return a GrRenderTargetContext,
42 // otherwise it will return a GrSurfaceContext.
Greg Daniel3912a4b2020-01-14 09:56:04 -050043 static std::unique_ptr<GrSurfaceContext> Make(GrRecordingContext*,
44 GrSurfaceProxyView readView,
Greg Danielbfa19c42019-12-19 16:41:40 -050045 GrColorType, SkAlphaType, sk_sp<SkColorSpace>);
46
Brian Salomona56a7462020-02-07 14:17:25 -050047 static std::unique_ptr<GrSurfaceContext> Make(GrRecordingContext*, SkISize dimensions,
Greg Danielbfa19c42019-12-19 16:41:40 -050048 const GrBackendFormat&, GrRenderable,
49 int renderTargetSampleCnt, GrMipMapped,
50 GrProtected, GrSurfaceOrigin, GrColorType,
51 SkAlphaType, sk_sp<SkColorSpace>, SkBackingFit,
52 SkBudgeted);
53
54 // If it is known that the GrSurfaceProxy is not renderable, you can directly call the the ctor
55 // here to make a GrSurfaceContext on the stack.
Greg Daniel3912a4b2020-01-14 09:56:04 -050056 GrSurfaceContext(GrRecordingContext*, GrSurfaceProxyView readView, GrColorType, SkAlphaType,
57 sk_sp<SkColorSpace>);
Greg Danielbfa19c42019-12-19 16:41:40 -050058
Brian Salomonbf6b9792019-08-21 09:38:10 -040059 virtual ~GrSurfaceContext() = default;
Brian Osman45580d32016-11-23 09:37:01 -050060
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040061 const GrColorInfo& colorInfo() const { return fColorInfo; }
Brian Salomon6aa65052020-01-28 12:16:53 -050062 GrImageInfo imageInfo() const { return {fColorInfo, fReadView.proxy()->dimensions()}; }
63
Greg Daniel3912a4b2020-01-14 09:56:04 -050064 GrSurfaceOrigin origin() const { return fReadView.origin(); }
65 GrSwizzle readSwizzle() const { return fReadView.swizzle(); }
66 // TODO: See if it makes sense for this to return a const& instead and require the callers to
67 // make a copy (which refs the proxy) if needed.
68 GrSurfaceProxyView readSurfaceView() { return fReadView; }
Robert Phillips2c862492017-01-18 10:08:39 -050069
Brian Salomonc5243782020-04-02 12:50:34 -040070 SkISize dimensions() const { return fReadView.dimensions(); }
Greg Daniel3912a4b2020-01-14 09:56:04 -050071 int width() const { return fReadView.proxy()->width(); }
72 int height() const { return fReadView.proxy()->height(); }
Robert Phillipsd46697a2017-01-25 12:10:37 -050073
Brian Salomon4d2d6f42019-07-26 14:15:11 -040074 const GrCaps* caps() const;
75
Robert Phillips2c862492017-01-18 10:08:39 -050076 /**
77 * Reads a rectangle of pixels from the render target context.
78 * @param dstInfo image info for the destination
Brian Salomon1d435302019-07-01 13:05:28 -040079 * @param dst destination pixels for the read
80 * @param rowBytes bytes in a row of 'dst'
81 * @param srcPt offset w/in the surface context from which to read
82 * @param direct The direct context to use. If null will use our GrRecordingContext if it
83 * is a GrDirectContext and fail otherwise.
Robert Phillips2c862492017-01-18 10:08:39 -050084 */
Brian Salomonf2ebdd92019-09-30 12:15:30 -040085 bool readPixels(const GrImageInfo& dstInfo, void* dst, size_t rowBytes, SkIPoint srcPt,
Brian Salomon1d435302019-07-01 13:05:28 -040086 GrContext* direct = nullptr);
Robert Phillips2c862492017-01-18 10:08:39 -050087
Brian Salomon63a0a752020-06-26 13:32:09 -040088 using ReadPixelsCallback = SkImage::ReadPixelsCallback;
89 using ReadPixelsContext = SkImage::ReadPixelsContext;
90 using RescaleGamma = SkImage::RescaleGamma;
91
92 // GPU implementation for SkImage:: and SkSurface::asyncRescaleAndReadPixels.
93 void asyncRescaleAndReadPixels(const SkImageInfo& info,
94 const SkIRect& srcRect,
95 RescaleGamma rescaleGamma,
96 SkFilterQuality rescaleQuality,
97 ReadPixelsCallback callback,
98 ReadPixelsContext context);
99
100 // GPU implementation for SkImage:: and SkSurface::asyncRescaleAndReadPixelsYUV420.
101 void asyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace,
102 sk_sp<SkColorSpace> dstColorSpace,
103 const SkIRect& srcRect,
104 SkISize dstSize,
105 RescaleGamma rescaleGamma,
106 SkFilterQuality rescaleQuality,
107 ReadPixelsCallback callback,
108 ReadPixelsContext context);
109
Robert Phillips2c862492017-01-18 10:08:39 -0500110 /**
Robert Phillipsb726d582017-03-09 16:36:32 -0500111 * Writes a rectangle of pixels [srcInfo, srcBuffer, srcRowbytes] into the
Robert Phillips2c862492017-01-18 10:08:39 -0500112 * renderTargetContext at the specified position.
113 * @param srcInfo image info for the source pixels
Brian Salomon1d435302019-07-01 13:05:28 -0400114 * @param src source for the write
115 * @param rowBytes bytes in a row of 'src'
116 * @param dstPt offset w/in the surface context at which to write
117 * @param direct The direct context to use. If null will use our GrRecordingContext if it
118 * is a GrDirectContext and fail otherwise.
Robert Phillips2c862492017-01-18 10:08:39 -0500119 */
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400120 bool writePixels(const GrImageInfo& srcInfo, const void* src, size_t rowBytes, SkIPoint dstPt,
Brian Salomon1d435302019-07-01 13:05:28 -0400121 GrContext* direct = nullptr);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400122
Greg Daniel3912a4b2020-01-14 09:56:04 -0500123 GrSurfaceProxy* asSurfaceProxy() { return fReadView.proxy(); }
124 const GrSurfaceProxy* asSurfaceProxy() const { return fReadView.proxy(); }
Greg Danielc61d7e32020-02-04 14:27:45 -0500125 sk_sp<GrSurfaceProxy> asSurfaceProxyRef() { return fReadView.refProxy(); }
Robert Phillipsf200a902017-01-30 13:27:37 -0500126
Greg Daniel3912a4b2020-01-14 09:56:04 -0500127 GrTextureProxy* asTextureProxy() { return fReadView.asTextureProxy(); }
128 const GrTextureProxy* asTextureProxy() const { return fReadView.asTextureProxy(); }
129 sk_sp<GrTextureProxy> asTextureProxyRef() { return fReadView.asTextureProxyRef(); }
Robert Phillipsf200a902017-01-30 13:27:37 -0500130
Greg Daniel3912a4b2020-01-14 09:56:04 -0500131 GrRenderTargetProxy* asRenderTargetProxy() { return fReadView.asRenderTargetProxy(); }
Greg Daniel46e366a2019-12-16 14:38:36 -0500132 const GrRenderTargetProxy* asRenderTargetProxy() const {
Greg Daniel3912a4b2020-01-14 09:56:04 -0500133 return fReadView.asRenderTargetProxy();
Greg Daniel46e366a2019-12-16 14:38:36 -0500134 }
135 sk_sp<GrRenderTargetProxy> asRenderTargetProxyRef() {
Greg Daniel3912a4b2020-01-14 09:56:04 -0500136 return fReadView.asRenderTargetProxyRef();
Greg Daniel46e366a2019-12-16 14:38:36 -0500137 }
Robert Phillips27341362016-12-14 08:46:47 -0500138
Robert Phillipsd46697a2017-01-25 12:10:37 -0500139 virtual GrRenderTargetContext* asRenderTargetContext() { return nullptr; }
140
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400141 /**
142 * Rescales the contents of srcRect. The gamma in which the rescaling occurs is controlled by
143 * RescaleGamma. It is always in the original gamut. The result is converted to the color type
144 * and color space of info after rescaling. Note: this currently requires that the info have a
145 * different size than srcRect. Though, it could be relaxed to allow non-scaling color
146 * conversions.
147 */
148 std::unique_ptr<GrRenderTargetContext> rescale(const GrImageInfo& info,
149 GrSurfaceOrigin,
Brian Salomonafd8a6c2020-05-21 12:10:54 -0400150 SkIRect srcRect,
Brian Salomon63a0a752020-06-26 13:32:09 -0400151 SkImage::RescaleGamma,
Brian Salomon11ad4cc2020-05-15 12:07:59 -0400152 SkFilterQuality);
153
Brian Salomon63a0a752020-06-26 13:32:09 -0400154 /**
155 * After this returns any pending surface IO will be issued to the backend 3D API and
156 * if the surface has MSAA it will be resolved.
157 */
158 GrSemaphoresSubmitted flush(SkSurface::BackendSurfaceAccess access,
159 const GrFlushInfo&,
160 const GrBackendSurfaceMutableState*);
161
Robert Phillips0d075de2019-03-04 11:08:13 -0500162 GrAuditTrail* auditTrail();
Brian Osman45580d32016-11-23 09:37:01 -0500163
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500164 // Provides access to functions that aren't part of the public API.
165 GrSurfaceContextPriv surfPriv();
166 const GrSurfaceContextPriv surfPriv() const;
167
Greg Daniel46cfbc62019-06-07 11:43:30 -0400168#if GR_TEST_UTILS
Brian Salomonc5243782020-04-02 12:50:34 -0400169 bool testCopy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
170 return this->copy(src, srcRect, dstPoint);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400171 }
172
Brian Salomonc5243782020-04-02 12:50:34 -0400173 bool testCopy(GrSurfaceProxy* src) {
174 return this->copy(src, SkIRect::MakeSize(src->dimensions()), {0, 0});
Greg Daniel46cfbc62019-06-07 11:43:30 -0400175 }
176#endif
177
Brian Osman45580d32016-11-23 09:37:01 -0500178protected:
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500179 friend class GrSurfaceContextPriv;
Robert Phillips72152832017-01-25 17:31:35 -0500180
Robert Phillips0d075de2019-03-04 11:08:13 -0500181 GrDrawingManager* drawingManager();
182 const GrDrawingManager* drawingManager() const;
Brian Osman45580d32016-11-23 09:37:01 -0500183
Greg Daniel46e366a2019-12-16 14:38:36 -0500184 SkDEBUGCODE(void validate() const;)
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400185
Robert Phillips0d075de2019-03-04 11:08:13 -0500186 SkDEBUGCODE(GrSingleOwner* singleOwner();)
Brian Osman45580d32016-11-23 09:37:01 -0500187
Robert Phillips69893702019-02-22 11:16:30 -0500188 GrRecordingContext* fContext;
Brian Osman45580d32016-11-23 09:37:01 -0500189
Greg Daniel3912a4b2020-01-14 09:56:04 -0500190 GrSurfaceProxyView fReadView;
Greg Daniel901b98e2019-10-22 09:54:02 -0400191
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400192 // Inserts a transfer, part of the implementation of asyncReadPixels and
193 // asyncRescaleAndReadPixelsYUV420().
194 struct PixelTransferResult {
195 using ConversionFn = void(void* dst, const void* mappedBuffer);
196 // If null then the transfer could not be performed. Otherwise this buffer will contain
197 // the pixel data when the transfer is complete.
198 sk_sp<GrGpuBuffer> fTransferBuffer;
199 // If this is null then the transfer buffer will contain the data in the requested
200 // color type. Otherwise, when the transfer is done this must be called to convert
201 // from the transfer buffer's color type to the requested color type.
202 std::function<ConversionFn> fPixelConverter;
203 };
204 PixelTransferResult transferPixels(GrColorType colorType, const SkIRect& rect);
205
Brian Salomon63a0a752020-06-26 13:32:09 -0400206 // The async read step of asyncRescaleAndReadPixels()
207 void asyncReadPixels(const SkIRect& rect,
208 SkColorType colorType,
209 ReadPixelsCallback callback,
210 ReadPixelsContext context);
211
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500212private:
Greg Daniel46cfbc62019-06-07 11:43:30 -0400213 friend class GrSurfaceProxy; // for copy
214
Greg Daniel46e366a2019-12-16 14:38:36 -0500215 SkDEBUGCODE(virtual void onValidate() const {})
216
Greg Daniel46cfbc62019-06-07 11:43:30 -0400217 /**
218 * Copy 'src' into the proxy backing this context. This call will not do any draw fallback.
219 * Currently only writePixels and replaceRenderTarget call this directly. All other copies
220 * should go through GrSurfaceProxy::Copy.
221 * @param src src of pixels
Greg Daniel46cfbc62019-06-07 11:43:30 -0400222 * @param dstPoint the origin of the 'srcRect' in the destination coordinate space
223 * @return true if the copy succeeded; false otherwise
224 *
225 * Note: Notionally, 'srcRect' is clipped to 'src's extent with 'dstPoint' being adjusted.
226 * Then the 'srcRect' offset by 'dstPoint' is clipped against the dst's extent.
227 * The end result is only valid src pixels and dst pixels will be touched but the copied
228 * regions will not be shifted. The 'src' must have the same origin as the backing proxy
229 * of fSurfaceContext.
230 */
Brian Salomonc5243782020-04-02 12:50:34 -0400231 bool copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400232
Brian Salomon63a0a752020-06-26 13:32:09 -0400233 class AsyncReadResult;
234
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400235 GrColorInfo fColorInfo;
Brian Salomonf3569f02017-10-24 12:52:33 -0400236
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500237 typedef SkRefCnt INHERITED;
Brian Osman45580d32016-11-23 09:37:01 -0500238};
239
240#endif