blob: 8c66ac97ee43da27aee9212994ef8917f5f6b8bf [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/GrColorSpaceInfo.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"
Brian Osman45580d32016-11-23 09:37:01 -050019
20class GrAuditTrail;
Robert Phillips72152832017-01-25 17:31:35 -050021class GrDrawingManager;
Robert Phillips2de8cfa2017-06-28 10:33:41 -040022class GrOpList;
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 Salomonbf6b9792019-08-21 09:38:10 -040037class SK_API 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 Salomonf3569f02017-10-24 12:52:33 -040041 const GrColorSpaceInfo& colorSpaceInfo() const { return fColorSpaceInfo; }
Robert Phillips2c862492017-01-18 10:08:39 -050042
Robert Phillipsd46697a2017-01-25 12:10:37 -050043 // TODO: these two calls would be way cooler if this object had a GrSurfaceProxy pointer
Robert Phillipsf200a902017-01-30 13:27:37 -050044 int width() const { return this->asSurfaceProxy()->width(); }
45 int height() const { return this->asSurfaceProxy()->height(); }
Robert Phillipsd46697a2017-01-25 12:10:37 -050046
Brian Salomon4d2d6f42019-07-26 14:15:11 -040047 const GrCaps* caps() const;
48
Robert Phillips2c862492017-01-18 10:08:39 -050049 /**
50 * Reads a rectangle of pixels from the render target context.
51 * @param dstInfo image info for the destination
Brian Salomon1d435302019-07-01 13:05:28 -040052 * @param dst destination pixels for the read
53 * @param rowBytes bytes in a row of 'dst'
54 * @param srcPt offset w/in the surface context from which to read
55 * @param direct The direct context to use. If null will use our GrRecordingContext if it
56 * is a GrDirectContext and fail otherwise.
Robert Phillips2c862492017-01-18 10:08:39 -050057 */
Brian Salomon1d435302019-07-01 13:05:28 -040058 bool readPixels(const GrPixelInfo& dstInfo, void* dst, size_t rowBytes, SkIPoint srcPt,
59 GrContext* direct = nullptr);
Robert Phillips2c862492017-01-18 10:08:39 -050060
61 /**
Robert Phillipsb726d582017-03-09 16:36:32 -050062 * Writes a rectangle of pixels [srcInfo, srcBuffer, srcRowbytes] into the
Robert Phillips2c862492017-01-18 10:08:39 -050063 * renderTargetContext at the specified position.
64 * @param srcInfo image info for the source pixels
Brian Salomon1d435302019-07-01 13:05:28 -040065 * @param src source for the write
66 * @param rowBytes bytes in a row of 'src'
67 * @param dstPt offset w/in the surface context at which to write
68 * @param direct The direct context to use. If null will use our GrRecordingContext if it
69 * is a GrDirectContext and fail otherwise.
Robert Phillips2c862492017-01-18 10:08:39 -050070 */
Brian Salomon1d435302019-07-01 13:05:28 -040071 bool writePixels(const GrPixelInfo& srcInfo, const void* src, size_t rowBytes, SkIPoint dstPt,
72 GrContext* direct = nullptr);
Greg Daniel6eb8c242019-06-05 10:22:24 -040073
Robert Phillips27341362016-12-14 08:46:47 -050074 // TODO: this is virtual b.c. this object doesn't have a pointer to the wrapped GrSurfaceProxy?
Robert Phillipsf200a902017-01-30 13:27:37 -050075 virtual GrSurfaceProxy* asSurfaceProxy() = 0;
76 virtual const GrSurfaceProxy* asSurfaceProxy() const = 0;
77 virtual sk_sp<GrSurfaceProxy> asSurfaceProxyRef() = 0;
78
79 virtual GrTextureProxy* asTextureProxy() = 0;
Greg Daniele252f082017-10-23 16:05:23 -040080 virtual const GrTextureProxy* asTextureProxy() const = 0;
Robert Phillipsf200a902017-01-30 13:27:37 -050081 virtual sk_sp<GrTextureProxy> asTextureProxyRef() = 0;
82
83 virtual GrRenderTargetProxy* asRenderTargetProxy() = 0;
84 virtual sk_sp<GrRenderTargetProxy> asRenderTargetProxyRef() = 0;
Robert Phillips27341362016-12-14 08:46:47 -050085
Robert Phillipsd46697a2017-01-25 12:10:37 -050086 virtual GrRenderTargetContext* asRenderTargetContext() { return nullptr; }
87
Robert Phillips0d075de2019-03-04 11:08:13 -050088 GrAuditTrail* auditTrail();
Brian Osman45580d32016-11-23 09:37:01 -050089
Robert Phillipse305cc1f2016-12-14 12:19:05 -050090 // Provides access to functions that aren't part of the public API.
91 GrSurfaceContextPriv surfPriv();
92 const GrSurfaceContextPriv surfPriv() const;
93
Greg Daniel46cfbc62019-06-07 11:43:30 -040094#if GR_TEST_UTILS
95 bool testCopy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
96 return this->copy(src, srcRect, dstPoint);
97 }
98
99 bool testCopy(GrSurfaceProxy* src) {
100 return this->copy(src);
101 }
102#endif
103
104
Brian Osman45580d32016-11-23 09:37:01 -0500105protected:
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500106 friend class GrSurfaceContextPriv;
107
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400108 GrSurfaceContext(GrRecordingContext*, GrColorType, SkAlphaType, sk_sp<SkColorSpace>);
Robert Phillips72152832017-01-25 17:31:35 -0500109
Robert Phillips0d075de2019-03-04 11:08:13 -0500110 GrDrawingManager* drawingManager();
111 const GrDrawingManager* drawingManager() const;
Brian Osman45580d32016-11-23 09:37:01 -0500112
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400113 virtual GrOpList* getOpList() = 0;
114 SkDEBUGCODE(virtual void validate() const = 0;)
115
Robert Phillips0d075de2019-03-04 11:08:13 -0500116 SkDEBUGCODE(GrSingleOwner* singleOwner();)
Brian Osman45580d32016-11-23 09:37:01 -0500117
Robert Phillips69893702019-02-22 11:16:30 -0500118 GrRecordingContext* fContext;
Brian Osman45580d32016-11-23 09:37:01 -0500119
Brian Salomone9ad9982019-07-22 16:17:41 -0400120 // The rescaling step of asyncRescaleAndReadPixels[YUV420]().
Brian Salomonbf6b9792019-08-21 09:38:10 -0400121 std::unique_ptr<GrRenderTargetContext> rescale(const SkImageInfo& info, const SkIRect& srcRect,
122 SkSurface::RescaleGamma rescaleGamma,
123 SkFilterQuality rescaleQuality);
Brian Salomone9ad9982019-07-22 16:17:41 -0400124
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400125 // Inserts a transfer, part of the implementation of asyncReadPixels and
126 // asyncRescaleAndReadPixelsYUV420().
127 struct PixelTransferResult {
128 using ConversionFn = void(void* dst, const void* mappedBuffer);
129 // If null then the transfer could not be performed. Otherwise this buffer will contain
130 // the pixel data when the transfer is complete.
131 sk_sp<GrGpuBuffer> fTransferBuffer;
132 // If this is null then the transfer buffer will contain the data in the requested
133 // color type. Otherwise, when the transfer is done this must be called to convert
134 // from the transfer buffer's color type to the requested color type.
135 std::function<ConversionFn> fPixelConverter;
136 };
137 PixelTransferResult transferPixels(GrColorType colorType, const SkIRect& rect);
138
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500139private:
Greg Daniel46cfbc62019-06-07 11:43:30 -0400140 friend class GrSurfaceProxy; // for copy
141
142 /**
143 * Copy 'src' into the proxy backing this context. This call will not do any draw fallback.
144 * Currently only writePixels and replaceRenderTarget call this directly. All other copies
145 * should go through GrSurfaceProxy::Copy.
146 * @param src src of pixels
147 * @param srcRect the subset of 'src' to copy
148 * @param dstPoint the origin of the 'srcRect' in the destination coordinate space
149 * @return true if the copy succeeded; false otherwise
150 *
151 * Note: Notionally, 'srcRect' is clipped to 'src's extent with 'dstPoint' being adjusted.
152 * Then the 'srcRect' offset by 'dstPoint' is clipped against the dst's extent.
153 * The end result is only valid src pixels and dst pixels will be touched but the copied
154 * regions will not be shifted. The 'src' must have the same origin as the backing proxy
155 * of fSurfaceContext.
156 */
157 bool copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint);
158
159 bool copy(GrSurfaceProxy* src) {
160 return this->copy(src, SkIRect::MakeWH(src->width(), src->height()), SkIPoint::Make(0, 0));
161 }
162
Robert Phillips69893702019-02-22 11:16:30 -0500163 GrColorSpaceInfo fColorSpaceInfo;
Brian Salomonf3569f02017-10-24 12:52:33 -0400164
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500165 typedef SkRefCnt INHERITED;
Brian Osman45580d32016-11-23 09:37:01 -0500166};
167
168#endif