blob: 2441e085d8c2e65d9633dc1158537f528a69b568 [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"
12#include "include/core/SkImageInfo.h"
13#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 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"
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:
Brian Salomonbf6b9792019-08-21 09:38:10 -040039 virtual ~GrSurfaceContext() = default;
Brian Osman45580d32016-11-23 09:37:01 -050040
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040041 const GrColorInfo& colorInfo() const { return fColorInfo; }
Greg Daniel901b98e2019-10-22 09:54:02 -040042 GrSurfaceOrigin origin() const { return fOrigin; }
Greg Daniel46e366a2019-12-16 14:38:36 -050043 const GrSwizzle& readSwizzle() const { return fReadSwizzle; }
44 GrSurfaceProxyView readSurfaceView() {
45 return { this->asSurfaceProxyRef(), fOrigin, fReadSwizzle };
Greg Daniel901b98e2019-10-22 09:54:02 -040046 }
Robert Phillips2c862492017-01-18 10:08:39 -050047
Greg Daniel46e366a2019-12-16 14:38:36 -050048 int width() const { return fSurfaceProxy->width(); }
49 int height() const { return fSurfaceProxy->height(); }
Robert Phillipsd46697a2017-01-25 12:10:37 -050050
Brian Salomon4d2d6f42019-07-26 14:15:11 -040051 const GrCaps* caps() const;
52
Robert Phillips2c862492017-01-18 10:08:39 -050053 /**
54 * Reads a rectangle of pixels from the render target context.
55 * @param dstInfo image info for the destination
Brian Salomon1d435302019-07-01 13:05:28 -040056 * @param dst destination pixels for the read
57 * @param rowBytes bytes in a row of 'dst'
58 * @param srcPt offset w/in the surface context from which to read
59 * @param direct The direct context to use. If null will use our GrRecordingContext if it
60 * is a GrDirectContext and fail otherwise.
Robert Phillips2c862492017-01-18 10:08:39 -050061 */
Brian Salomonf2ebdd92019-09-30 12:15:30 -040062 bool readPixels(const GrImageInfo& dstInfo, void* dst, size_t rowBytes, SkIPoint srcPt,
Brian Salomon1d435302019-07-01 13:05:28 -040063 GrContext* direct = nullptr);
Robert Phillips2c862492017-01-18 10:08:39 -050064
65 /**
Robert Phillipsb726d582017-03-09 16:36:32 -050066 * Writes a rectangle of pixels [srcInfo, srcBuffer, srcRowbytes] into the
Robert Phillips2c862492017-01-18 10:08:39 -050067 * renderTargetContext at the specified position.
68 * @param srcInfo image info for the source pixels
Brian Salomon1d435302019-07-01 13:05:28 -040069 * @param src source for the write
70 * @param rowBytes bytes in a row of 'src'
71 * @param dstPt offset w/in the surface context at which to write
72 * @param direct The direct context to use. If null will use our GrRecordingContext if it
73 * is a GrDirectContext and fail otherwise.
Robert Phillips2c862492017-01-18 10:08:39 -050074 */
Brian Salomonf2ebdd92019-09-30 12:15:30 -040075 bool writePixels(const GrImageInfo& srcInfo, const void* src, size_t rowBytes, SkIPoint dstPt,
Brian Salomon1d435302019-07-01 13:05:28 -040076 GrContext* direct = nullptr);
Greg Daniel6eb8c242019-06-05 10:22:24 -040077
Greg Daniel46e366a2019-12-16 14:38:36 -050078 GrSurfaceProxy* asSurfaceProxy() { return fSurfaceProxy.get(); }
79 const GrSurfaceProxy* asSurfaceProxy() const { return fSurfaceProxy.get(); }
80 sk_sp<GrSurfaceProxy> asSurfaceProxyRef() { return fSurfaceProxy; }
Robert Phillipsf200a902017-01-30 13:27:37 -050081
Greg Daniel46e366a2019-12-16 14:38:36 -050082 GrTextureProxy* asTextureProxy() { return fSurfaceProxy->asTextureProxy(); }
83 const GrTextureProxy* asTextureProxy() const { return fSurfaceProxy->asTextureProxy(); }
84 sk_sp<GrTextureProxy> asTextureProxyRef() {
85 return sk_ref_sp(fSurfaceProxy->asTextureProxy());
86 }
Robert Phillipsf200a902017-01-30 13:27:37 -050087
Greg Daniel46e366a2019-12-16 14:38:36 -050088 GrRenderTargetProxy* asRenderTargetProxy() { return fSurfaceProxy->asRenderTargetProxy(); }
89 const GrRenderTargetProxy* asRenderTargetProxy() const {
90 return fSurfaceProxy->asRenderTargetProxy();
91 }
92 sk_sp<GrRenderTargetProxy> asRenderTargetProxyRef() {
93 return sk_ref_sp(fSurfaceProxy->asRenderTargetProxy());
94 }
Robert Phillips27341362016-12-14 08:46:47 -050095
Robert Phillipsd46697a2017-01-25 12:10:37 -050096 virtual GrRenderTargetContext* asRenderTargetContext() { return nullptr; }
97
Robert Phillips0d075de2019-03-04 11:08:13 -050098 GrAuditTrail* auditTrail();
Brian Osman45580d32016-11-23 09:37:01 -050099
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500100 // Provides access to functions that aren't part of the public API.
101 GrSurfaceContextPriv surfPriv();
102 const GrSurfaceContextPriv surfPriv() const;
103
Greg Daniel46cfbc62019-06-07 11:43:30 -0400104#if GR_TEST_UTILS
105 bool testCopy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
106 return this->copy(src, srcRect, dstPoint);
107 }
108
109 bool testCopy(GrSurfaceProxy* src) {
110 return this->copy(src);
111 }
112#endif
113
Brian Osman45580d32016-11-23 09:37:01 -0500114protected:
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500115 friend class GrSurfaceContextPriv;
Greg Daniel46e366a2019-12-16 14:38:36 -0500116 friend class GrDrawingManager; // For ctor
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500117
Greg Daniel46e366a2019-12-16 14:38:36 -0500118 GrSurfaceContext(GrRecordingContext*, sk_sp<GrSurfaceProxy>, GrColorType, SkAlphaType,
119 sk_sp<SkColorSpace>, GrSurfaceOrigin, GrSwizzle readSwizzle);
Robert Phillips72152832017-01-25 17:31:35 -0500120
Robert Phillips0d075de2019-03-04 11:08:13 -0500121 GrDrawingManager* drawingManager();
122 const GrDrawingManager* drawingManager() const;
Brian Osman45580d32016-11-23 09:37:01 -0500123
Greg Daniel46e366a2019-12-16 14:38:36 -0500124 SkDEBUGCODE(void validate() const;)
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400125
Robert Phillips0d075de2019-03-04 11:08:13 -0500126 SkDEBUGCODE(GrSingleOwner* singleOwner();)
Brian Osman45580d32016-11-23 09:37:01 -0500127
Robert Phillips69893702019-02-22 11:16:30 -0500128 GrRecordingContext* fContext;
Brian Osman45580d32016-11-23 09:37:01 -0500129
Greg Daniel46e366a2019-12-16 14:38:36 -0500130 sk_sp<GrSurfaceProxy> fSurfaceProxy;
Greg Daniel901b98e2019-10-22 09:54:02 -0400131 GrSurfaceOrigin fOrigin;
132
Brian Salomone9ad9982019-07-22 16:17:41 -0400133 // The rescaling step of asyncRescaleAndReadPixels[YUV420]().
Brian Salomonbf6b9792019-08-21 09:38:10 -0400134 std::unique_ptr<GrRenderTargetContext> rescale(const SkImageInfo& info, const SkIRect& srcRect,
135 SkSurface::RescaleGamma rescaleGamma,
136 SkFilterQuality rescaleQuality);
Brian Salomone9ad9982019-07-22 16:17:41 -0400137
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400138 // Inserts a transfer, part of the implementation of asyncReadPixels and
139 // asyncRescaleAndReadPixelsYUV420().
140 struct PixelTransferResult {
141 using ConversionFn = void(void* dst, const void* mappedBuffer);
142 // If null then the transfer could not be performed. Otherwise this buffer will contain
143 // the pixel data when the transfer is complete.
144 sk_sp<GrGpuBuffer> fTransferBuffer;
145 // If this is null then the transfer buffer will contain the data in the requested
146 // color type. Otherwise, when the transfer is done this must be called to convert
147 // from the transfer buffer's color type to the requested color type.
148 std::function<ConversionFn> fPixelConverter;
149 };
150 PixelTransferResult transferPixels(GrColorType colorType, const SkIRect& rect);
151
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500152private:
Greg Daniel46cfbc62019-06-07 11:43:30 -0400153 friend class GrSurfaceProxy; // for copy
154
Greg Daniel46e366a2019-12-16 14:38:36 -0500155 SkDEBUGCODE(virtual void onValidate() const {})
156
Greg Daniel46cfbc62019-06-07 11:43:30 -0400157 /**
158 * Copy 'src' into the proxy backing this context. This call will not do any draw fallback.
159 * Currently only writePixels and replaceRenderTarget call this directly. All other copies
160 * should go through GrSurfaceProxy::Copy.
161 * @param src src of pixels
162 * @param srcRect the subset of 'src' to copy
163 * @param dstPoint the origin of the 'srcRect' in the destination coordinate space
164 * @return true if the copy succeeded; false otherwise
165 *
166 * Note: Notionally, 'srcRect' is clipped to 'src's extent with 'dstPoint' being adjusted.
167 * Then the 'srcRect' offset by 'dstPoint' is clipped against the dst's extent.
168 * The end result is only valid src pixels and dst pixels will be touched but the copied
169 * regions will not be shifted. The 'src' must have the same origin as the backing proxy
170 * of fSurfaceContext.
171 */
172 bool copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint);
173
174 bool copy(GrSurfaceProxy* src) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400175 return this->copy(src, SkIRect::MakeSize(src->dimensions()), SkIPoint::Make(0, 0));
Greg Daniel46cfbc62019-06-07 11:43:30 -0400176 }
177
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400178 GrColorInfo fColorInfo;
Greg Daniel46e366a2019-12-16 14:38:36 -0500179 GrSwizzle fReadSwizzle;
Brian Salomonf3569f02017-10-24 12:52:33 -0400180
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500181 typedef SkRefCnt INHERITED;
Brian Osman45580d32016-11-23 09:37:01 -0500182};
183
184#endif