blob: 8ef1450da1e908026451c6a64496e8c7448d4527 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkRefCnt.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/GrColorSpaceInfo.h"
Brian Salomon1d435302019-07-01 13:05:28 -040013#include "src/gpu/GrDataUtils.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040014#include "src/gpu/GrSurfaceProxy.h"
Brian Osman45580d32016-11-23 09:37:01 -050015
16class GrAuditTrail;
Robert Phillips72152832017-01-25 17:31:35 -050017class GrDrawingManager;
Robert Phillips2de8cfa2017-06-28 10:33:41 -040018class GrOpList;
Robert Phillips69893702019-02-22 11:16:30 -050019class GrRecordingContext;
Robert Phillipsd46697a2017-01-25 12:10:37 -050020class GrRenderTargetContext;
Robert Phillips27341362016-12-14 08:46:47 -050021class GrRenderTargetProxy;
Brian Osman45580d32016-11-23 09:37:01 -050022class GrSingleOwner;
23class GrSurface;
Robert Phillipse305cc1f2016-12-14 12:19:05 -050024class GrSurfaceContextPriv;
Robert Phillips27341362016-12-14 08:46:47 -050025class GrSurfaceProxy;
26class GrTextureProxy;
Brian Osman45580d32016-11-23 09:37:01 -050027struct SkIPoint;
28struct SkIRect;
29
30/**
31 * A helper object to orchestrate commands for a particular surface
32 */
33class SK_API GrSurfaceContext : public SkRefCnt {
34public:
35 ~GrSurfaceContext() override {}
36
Brian Salomonf3569f02017-10-24 12:52:33 -040037 const GrColorSpaceInfo& colorSpaceInfo() const { return fColorSpaceInfo; }
Robert Phillips2c862492017-01-18 10:08:39 -050038
Robert Phillipsd46697a2017-01-25 12:10:37 -050039 // TODO: these two calls would be way cooler if this object had a GrSurfaceProxy pointer
Robert Phillipsf200a902017-01-30 13:27:37 -050040 int width() const { return this->asSurfaceProxy()->width(); }
41 int height() const { return this->asSurfaceProxy()->height(); }
Robert Phillipsd46697a2017-01-25 12:10:37 -050042
Robert Phillips2c862492017-01-18 10:08:39 -050043 /**
44 * Reads a rectangle of pixels from the render target context.
45 * @param dstInfo image info for the destination
Brian Salomon1d435302019-07-01 13:05:28 -040046 * @param dst destination pixels for the read
47 * @param rowBytes bytes in a row of 'dst'
48 * @param srcPt offset w/in the surface context from which to read
49 * @param direct The direct context to use. If null will use our GrRecordingContext if it
50 * is a GrDirectContext and fail otherwise.
Robert Phillips2c862492017-01-18 10:08:39 -050051 */
Brian Salomon1d435302019-07-01 13:05:28 -040052 bool readPixels(const GrPixelInfo& dstInfo, void* dst, size_t rowBytes, SkIPoint srcPt,
53 GrContext* direct = nullptr);
Robert Phillips2c862492017-01-18 10:08:39 -050054
55 /**
Robert Phillipsb726d582017-03-09 16:36:32 -050056 * Writes a rectangle of pixels [srcInfo, srcBuffer, srcRowbytes] into the
Robert Phillips2c862492017-01-18 10:08:39 -050057 * renderTargetContext at the specified position.
58 * @param srcInfo image info for the source pixels
Brian Salomon1d435302019-07-01 13:05:28 -040059 * @param src source for the write
60 * @param rowBytes bytes in a row of 'src'
61 * @param dstPt offset w/in the surface context at which to write
62 * @param direct The direct context to use. If null will use our GrRecordingContext if it
63 * is a GrDirectContext and fail otherwise.
Robert Phillips2c862492017-01-18 10:08:39 -050064 */
Brian Salomon1d435302019-07-01 13:05:28 -040065 bool writePixels(const GrPixelInfo& srcInfo, const void* src, size_t rowBytes, SkIPoint dstPt,
66 GrContext* direct = nullptr);
Greg Daniel6eb8c242019-06-05 10:22:24 -040067
Robert Phillips27341362016-12-14 08:46:47 -050068 // TODO: this is virtual b.c. this object doesn't have a pointer to the wrapped GrSurfaceProxy?
Robert Phillipsf200a902017-01-30 13:27:37 -050069 virtual GrSurfaceProxy* asSurfaceProxy() = 0;
70 virtual const GrSurfaceProxy* asSurfaceProxy() const = 0;
71 virtual sk_sp<GrSurfaceProxy> asSurfaceProxyRef() = 0;
72
73 virtual GrTextureProxy* asTextureProxy() = 0;
Greg Daniele252f082017-10-23 16:05:23 -040074 virtual const GrTextureProxy* asTextureProxy() const = 0;
Robert Phillipsf200a902017-01-30 13:27:37 -050075 virtual sk_sp<GrTextureProxy> asTextureProxyRef() = 0;
76
77 virtual GrRenderTargetProxy* asRenderTargetProxy() = 0;
78 virtual sk_sp<GrRenderTargetProxy> asRenderTargetProxyRef() = 0;
Robert Phillips27341362016-12-14 08:46:47 -050079
Robert Phillipsd46697a2017-01-25 12:10:37 -050080 virtual GrRenderTargetContext* asRenderTargetContext() { return nullptr; }
81
Robert Phillips0d075de2019-03-04 11:08:13 -050082 GrAuditTrail* auditTrail();
Brian Osman45580d32016-11-23 09:37:01 -050083
Robert Phillipse305cc1f2016-12-14 12:19:05 -050084 // Provides access to functions that aren't part of the public API.
85 GrSurfaceContextPriv surfPriv();
86 const GrSurfaceContextPriv surfPriv() const;
87
Greg Daniel46cfbc62019-06-07 11:43:30 -040088#if GR_TEST_UTILS
89 bool testCopy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
90 return this->copy(src, srcRect, dstPoint);
91 }
92
93 bool testCopy(GrSurfaceProxy* src) {
94 return this->copy(src);
95 }
96#endif
97
98
Brian Osman45580d32016-11-23 09:37:01 -050099protected:
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500100 friend class GrSurfaceContextPriv;
101
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400102 GrSurfaceContext(GrRecordingContext*, GrColorType, SkAlphaType, sk_sp<SkColorSpace>);
Robert Phillips72152832017-01-25 17:31:35 -0500103
Robert Phillips0d075de2019-03-04 11:08:13 -0500104 GrDrawingManager* drawingManager();
105 const GrDrawingManager* drawingManager() const;
Brian Osman45580d32016-11-23 09:37:01 -0500106
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400107 virtual GrOpList* getOpList() = 0;
108 SkDEBUGCODE(virtual void validate() const = 0;)
109
Robert Phillips0d075de2019-03-04 11:08:13 -0500110 SkDEBUGCODE(GrSingleOwner* singleOwner();)
Brian Osman45580d32016-11-23 09:37:01 -0500111
Robert Phillips69893702019-02-22 11:16:30 -0500112 GrRecordingContext* fContext;
Brian Osman45580d32016-11-23 09:37:01 -0500113
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500114private:
Greg Daniel46cfbc62019-06-07 11:43:30 -0400115 friend class GrSurfaceProxy; // for copy
116
117 /**
118 * Copy 'src' into the proxy backing this context. This call will not do any draw fallback.
119 * Currently only writePixels and replaceRenderTarget call this directly. All other copies
120 * should go through GrSurfaceProxy::Copy.
121 * @param src src of pixels
122 * @param srcRect the subset of 'src' to copy
123 * @param dstPoint the origin of the 'srcRect' in the destination coordinate space
124 * @return true if the copy succeeded; false otherwise
125 *
126 * Note: Notionally, 'srcRect' is clipped to 'src's extent with 'dstPoint' being adjusted.
127 * Then the 'srcRect' offset by 'dstPoint' is clipped against the dst's extent.
128 * The end result is only valid src pixels and dst pixels will be touched but the copied
129 * regions will not be shifted. The 'src' must have the same origin as the backing proxy
130 * of fSurfaceContext.
131 */
132 bool copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint);
133
134 bool copy(GrSurfaceProxy* src) {
135 return this->copy(src, SkIRect::MakeWH(src->width(), src->height()), SkIPoint::Make(0, 0));
136 }
137
Robert Phillips69893702019-02-22 11:16:30 -0500138 GrColorSpaceInfo fColorSpaceInfo;
Brian Salomonf3569f02017-10-24 12:52:33 -0400139
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500140 typedef SkRefCnt INHERITED;
Brian Osman45580d32016-11-23 09:37:01 -0500141};
142
143#endif