blob: f1efa727e384bfd985cc241104f3b94b7d24f039 [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 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"
Brian Salomone9ad9982019-07-22 16:17:41 -040014#include "include/core/SkSurface.h"
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040015#include "src/gpu/GrColorInfo.h"
Brian Salomon1d435302019-07-01 13:05:28 -040016#include "src/gpu/GrDataUtils.h"
Brian Salomon6aa65052020-01-28 12:16:53 -050017#include "src/gpu/GrImageInfo.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040018#include "src/gpu/GrSurfaceProxy.h"
Greg Daniel901b98e2019-10-22 09:54:02 -040019#include "src/gpu/GrSurfaceProxyView.h"
Brian Osman45580d32016-11-23 09:37:01 -050020
21class GrAuditTrail;
Robert Phillips72152832017-01-25 17:31:35 -050022class GrDrawingManager;
Robert Phillips69893702019-02-22 11:16:30 -050023class GrRecordingContext;
Robert Phillipsd46697a2017-01-25 12:10:37 -050024class GrRenderTargetContext;
Robert Phillips27341362016-12-14 08:46:47 -050025class GrRenderTargetProxy;
Brian Osman45580d32016-11-23 09:37:01 -050026class GrSingleOwner;
27class GrSurface;
Robert Phillipse305cc1f2016-12-14 12:19:05 -050028class GrSurfaceContextPriv;
Robert Phillips27341362016-12-14 08:46:47 -050029class GrSurfaceProxy;
30class GrTextureProxy;
Brian Osman45580d32016-11-23 09:37:01 -050031struct SkIPoint;
32struct SkIRect;
33
34/**
35 * A helper object to orchestrate commands for a particular surface
36 */
Brian Salomon57f211b2019-08-21 15:21:09 -040037class GrSurfaceContext {
Brian Osman45580d32016-11-23 09:37:01 -050038public:
Greg Danielbfa19c42019-12-19 16:41:40 -050039 // If the passed in GrSurfaceProxy is renderable this will return a GrRenderTargetContext,
40 // otherwise it will return a GrSurfaceContext.
Greg Daniel3912a4b2020-01-14 09:56:04 -050041 static std::unique_ptr<GrSurfaceContext> Make(GrRecordingContext*,
42 GrSurfaceProxyView readView,
Greg Danielbfa19c42019-12-19 16:41:40 -050043 GrColorType, SkAlphaType, sk_sp<SkColorSpace>);
44
45 static std::unique_ptr<GrSurfaceContext> Make(GrRecordingContext*, const SkISize& dimensions,
46 const GrBackendFormat&, GrRenderable,
47 int renderTargetSampleCnt, GrMipMapped,
48 GrProtected, GrSurfaceOrigin, GrColorType,
49 SkAlphaType, sk_sp<SkColorSpace>, SkBackingFit,
50 SkBudgeted);
51
52 // If it is known that the GrSurfaceProxy is not renderable, you can directly call the the ctor
53 // here to make a GrSurfaceContext on the stack.
Greg Daniel3912a4b2020-01-14 09:56:04 -050054 GrSurfaceContext(GrRecordingContext*, GrSurfaceProxyView readView, GrColorType, SkAlphaType,
55 sk_sp<SkColorSpace>);
Greg Danielbfa19c42019-12-19 16:41:40 -050056
Brian Salomonbf6b9792019-08-21 09:38:10 -040057 virtual ~GrSurfaceContext() = default;
Brian Osman45580d32016-11-23 09:37:01 -050058
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040059 const GrColorInfo& colorInfo() const { return fColorInfo; }
Brian Salomon6aa65052020-01-28 12:16:53 -050060 GrImageInfo imageInfo() const { return {fColorInfo, fReadView.proxy()->dimensions()}; }
61
Greg Daniel3912a4b2020-01-14 09:56:04 -050062 GrSurfaceOrigin origin() const { return fReadView.origin(); }
63 GrSwizzle readSwizzle() const { return fReadView.swizzle(); }
64 // TODO: See if it makes sense for this to return a const& instead and require the callers to
65 // make a copy (which refs the proxy) if needed.
66 GrSurfaceProxyView readSurfaceView() { return fReadView; }
Robert Phillips2c862492017-01-18 10:08:39 -050067
Greg Daniel3912a4b2020-01-14 09:56:04 -050068 int width() const { return fReadView.proxy()->width(); }
69 int height() const { return fReadView.proxy()->height(); }
Robert Phillipsd46697a2017-01-25 12:10:37 -050070
Brian Salomon4d2d6f42019-07-26 14:15:11 -040071 const GrCaps* caps() const;
72
Robert Phillips2c862492017-01-18 10:08:39 -050073 /**
74 * Reads a rectangle of pixels from the render target context.
75 * @param dstInfo image info for the destination
Brian Salomon1d435302019-07-01 13:05:28 -040076 * @param dst destination pixels for the read
77 * @param rowBytes bytes in a row of 'dst'
78 * @param srcPt offset w/in the surface context from which to read
79 * @param direct The direct context to use. If null will use our GrRecordingContext if it
80 * is a GrDirectContext and fail otherwise.
Robert Phillips2c862492017-01-18 10:08:39 -050081 */
Brian Salomonf2ebdd92019-09-30 12:15:30 -040082 bool readPixels(const GrImageInfo& dstInfo, void* dst, size_t rowBytes, SkIPoint srcPt,
Brian Salomon1d435302019-07-01 13:05:28 -040083 GrContext* direct = nullptr);
Robert Phillips2c862492017-01-18 10:08:39 -050084
85 /**
Robert Phillipsb726d582017-03-09 16:36:32 -050086 * Writes a rectangle of pixels [srcInfo, srcBuffer, srcRowbytes] into the
Robert Phillips2c862492017-01-18 10:08:39 -050087 * renderTargetContext at the specified position.
88 * @param srcInfo image info for the source pixels
Brian Salomon1d435302019-07-01 13:05:28 -040089 * @param src source for the write
90 * @param rowBytes bytes in a row of 'src'
91 * @param dstPt offset w/in the surface context at which to write
92 * @param direct The direct context to use. If null will use our GrRecordingContext if it
93 * is a GrDirectContext and fail otherwise.
Robert Phillips2c862492017-01-18 10:08:39 -050094 */
Brian Salomonf2ebdd92019-09-30 12:15:30 -040095 bool writePixels(const GrImageInfo& srcInfo, const void* src, size_t rowBytes, SkIPoint dstPt,
Brian Salomon1d435302019-07-01 13:05:28 -040096 GrContext* direct = nullptr);
Greg Daniel6eb8c242019-06-05 10:22:24 -040097
Greg Daniel3912a4b2020-01-14 09:56:04 -050098 GrSurfaceProxy* asSurfaceProxy() { return fReadView.proxy(); }
99 const GrSurfaceProxy* asSurfaceProxy() const { return fReadView.proxy(); }
100 sk_sp<GrSurfaceProxy> asSurfaceProxyRef() { return fReadView.proxyRef(); }
Robert Phillipsf200a902017-01-30 13:27:37 -0500101
Greg Daniel3912a4b2020-01-14 09:56:04 -0500102 GrTextureProxy* asTextureProxy() { return fReadView.asTextureProxy(); }
103 const GrTextureProxy* asTextureProxy() const { return fReadView.asTextureProxy(); }
104 sk_sp<GrTextureProxy> asTextureProxyRef() { return fReadView.asTextureProxyRef(); }
Robert Phillipsf200a902017-01-30 13:27:37 -0500105
Greg Daniel3912a4b2020-01-14 09:56:04 -0500106 GrRenderTargetProxy* asRenderTargetProxy() { return fReadView.asRenderTargetProxy(); }
Greg Daniel46e366a2019-12-16 14:38:36 -0500107 const GrRenderTargetProxy* asRenderTargetProxy() const {
Greg Daniel3912a4b2020-01-14 09:56:04 -0500108 return fReadView.asRenderTargetProxy();
Greg Daniel46e366a2019-12-16 14:38:36 -0500109 }
110 sk_sp<GrRenderTargetProxy> asRenderTargetProxyRef() {
Greg Daniel3912a4b2020-01-14 09:56:04 -0500111 return fReadView.asRenderTargetProxyRef();
Greg Daniel46e366a2019-12-16 14:38:36 -0500112 }
Robert Phillips27341362016-12-14 08:46:47 -0500113
Robert Phillipsd46697a2017-01-25 12:10:37 -0500114 virtual GrRenderTargetContext* asRenderTargetContext() { return nullptr; }
115
Robert Phillips0d075de2019-03-04 11:08:13 -0500116 GrAuditTrail* auditTrail();
Brian Osman45580d32016-11-23 09:37:01 -0500117
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500118 // Provides access to functions that aren't part of the public API.
119 GrSurfaceContextPriv surfPriv();
120 const GrSurfaceContextPriv surfPriv() const;
121
Greg Daniel46cfbc62019-06-07 11:43:30 -0400122#if GR_TEST_UTILS
123 bool testCopy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
124 return this->copy(src, srcRect, dstPoint);
125 }
126
127 bool testCopy(GrSurfaceProxy* src) {
128 return this->copy(src);
129 }
130#endif
131
Brian Osman45580d32016-11-23 09:37:01 -0500132protected:
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500133 friend class GrSurfaceContextPriv;
Robert Phillips72152832017-01-25 17:31:35 -0500134
Robert Phillips0d075de2019-03-04 11:08:13 -0500135 GrDrawingManager* drawingManager();
136 const GrDrawingManager* drawingManager() const;
Brian Osman45580d32016-11-23 09:37:01 -0500137
Greg Daniel46e366a2019-12-16 14:38:36 -0500138 SkDEBUGCODE(void validate() const;)
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400139
Robert Phillips0d075de2019-03-04 11:08:13 -0500140 SkDEBUGCODE(GrSingleOwner* singleOwner();)
Brian Osman45580d32016-11-23 09:37:01 -0500141
Robert Phillips69893702019-02-22 11:16:30 -0500142 GrRecordingContext* fContext;
Brian Osman45580d32016-11-23 09:37:01 -0500143
Greg Daniel3912a4b2020-01-14 09:56:04 -0500144 GrSurfaceProxyView fReadView;
Greg Daniel901b98e2019-10-22 09:54:02 -0400145
Brian Salomone9ad9982019-07-22 16:17:41 -0400146 // The rescaling step of asyncRescaleAndReadPixels[YUV420]().
Brian Salomonbf6b9792019-08-21 09:38:10 -0400147 std::unique_ptr<GrRenderTargetContext> rescale(const SkImageInfo& info, const SkIRect& srcRect,
148 SkSurface::RescaleGamma rescaleGamma,
149 SkFilterQuality rescaleQuality);
Brian Salomone9ad9982019-07-22 16:17:41 -0400150
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400151 // Inserts a transfer, part of the implementation of asyncReadPixels and
152 // asyncRescaleAndReadPixelsYUV420().
153 struct PixelTransferResult {
154 using ConversionFn = void(void* dst, const void* mappedBuffer);
155 // If null then the transfer could not be performed. Otherwise this buffer will contain
156 // the pixel data when the transfer is complete.
157 sk_sp<GrGpuBuffer> fTransferBuffer;
158 // If this is null then the transfer buffer will contain the data in the requested
159 // color type. Otherwise, when the transfer is done this must be called to convert
160 // from the transfer buffer's color type to the requested color type.
161 std::function<ConversionFn> fPixelConverter;
162 };
163 PixelTransferResult transferPixels(GrColorType colorType, const SkIRect& rect);
164
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500165private:
Greg Daniel46cfbc62019-06-07 11:43:30 -0400166 friend class GrSurfaceProxy; // for copy
167
Greg Daniel46e366a2019-12-16 14:38:36 -0500168 SkDEBUGCODE(virtual void onValidate() const {})
169
Greg Daniel46cfbc62019-06-07 11:43:30 -0400170 /**
171 * Copy 'src' into the proxy backing this context. This call will not do any draw fallback.
172 * Currently only writePixels and replaceRenderTarget call this directly. All other copies
173 * should go through GrSurfaceProxy::Copy.
174 * @param src src of pixels
175 * @param srcRect the subset of 'src' to copy
176 * @param dstPoint the origin of the 'srcRect' in the destination coordinate space
177 * @return true if the copy succeeded; false otherwise
178 *
179 * Note: Notionally, 'srcRect' is clipped to 'src's extent with 'dstPoint' being adjusted.
180 * Then the 'srcRect' offset by 'dstPoint' is clipped against the dst's extent.
181 * The end result is only valid src pixels and dst pixels will be touched but the copied
182 * regions will not be shifted. The 'src' must have the same origin as the backing proxy
183 * of fSurfaceContext.
184 */
185 bool copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint);
186
187 bool copy(GrSurfaceProxy* src) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400188 return this->copy(src, SkIRect::MakeSize(src->dimensions()), SkIPoint::Make(0, 0));
Greg Daniel46cfbc62019-06-07 11:43:30 -0400189 }
190
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400191 GrColorInfo fColorInfo;
Brian Salomonf3569f02017-10-24 12:52:33 -0400192
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500193 typedef SkRefCnt INHERITED;
Brian Osman45580d32016-11-23 09:37:01 -0500194};
195
196#endif