blob: 1b61e5a7416944c22f4e4c25f665bb6617652884 [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"
Greg Danielf91aeb22019-06-18 09:58:02 -040013#include "src/gpu/GrSurfaceProxy.h"
Brian Osman45580d32016-11-23 09:37:01 -050014
15class GrAuditTrail;
Robert Phillips72152832017-01-25 17:31:35 -050016class GrDrawingManager;
Robert Phillips2de8cfa2017-06-28 10:33:41 -040017class GrOpList;
Robert Phillips69893702019-02-22 11:16:30 -050018class GrRecordingContext;
Robert Phillipsd46697a2017-01-25 12:10:37 -050019class GrRenderTargetContext;
Robert Phillips27341362016-12-14 08:46:47 -050020class GrRenderTargetProxy;
Brian Osman45580d32016-11-23 09:37:01 -050021class GrSingleOwner;
22class GrSurface;
Robert Phillipse305cc1f2016-12-14 12:19:05 -050023class GrSurfaceContextPriv;
Robert Phillips27341362016-12-14 08:46:47 -050024class GrSurfaceProxy;
25class GrTextureProxy;
Brian Osman45580d32016-11-23 09:37:01 -050026struct SkIPoint;
27struct SkIRect;
28
29/**
30 * A helper object to orchestrate commands for a particular surface
31 */
32class SK_API GrSurfaceContext : public SkRefCnt {
33public:
34 ~GrSurfaceContext() override {}
35
Brian Salomonf3569f02017-10-24 12:52:33 -040036 const GrColorSpaceInfo& colorSpaceInfo() const { return fColorSpaceInfo; }
Robert Phillips2c862492017-01-18 10:08:39 -050037
Robert Phillipsd46697a2017-01-25 12:10:37 -050038 // TODO: these two calls would be way cooler if this object had a GrSurfaceProxy pointer
Robert Phillipsf200a902017-01-30 13:27:37 -050039 int width() const { return this->asSurfaceProxy()->width(); }
40 int height() const { return this->asSurfaceProxy()->height(); }
Robert Phillipsd46697a2017-01-25 12:10:37 -050041
Greg Daniel6eb8c242019-06-05 10:22:24 -040042 /**
43 * These flags can be used with the read/write pixels functions below.
44 */
45 enum PixelOpsFlags {
46 /** The src for write or dst read is unpremultiplied. This is only respected if both the
47 config src and dst configs are an RGBA/BGRA 8888 format. */
48 kUnpremul_PixelOpsFlag = 0x4,
49 };
50
Robert Phillips2c862492017-01-18 10:08:39 -050051 /**
52 * Reads a rectangle of pixels from the render target context.
53 * @param dstInfo image info for the destination
54 * @param dstBuffer destination pixels for the read
55 * @param dstRowBytes bytes in a row of 'dstBuffer'
56 * @param x x offset w/in the render target context from which to read
57 * @param y y offset w/in the render target context from which to read
58 *
59 * @return true if the read succeeded, false if not. The read can fail because of an
60 * unsupported pixel config.
61 */
62 bool readPixels(const SkImageInfo& dstInfo, void* dstBuffer, size_t dstRowBytes,
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040063 int x, int y, uint32_t flags = 0);
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
69 * @param srcBuffer source for the write
70 * @param srcRowBytes bytes in a row of 'srcBuffer'
71 * @param x x offset w/in the render target context at which to write
72 * @param y y offset w/in the render target context at which to write
73 *
74 * @return true if the write succeeded, false if not. The write can fail because of an
75 * unsupported pixel config.
76 */
77 bool writePixels(const SkImageInfo& srcInfo, const void* srcBuffer, size_t srcRowBytes,
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040078 int x, int y, uint32_t flags = 0);
Robert Phillips2c862492017-01-18 10:08:39 -050079
Greg Daniel6eb8c242019-06-05 10:22:24 -040080#if GR_TEST_UTILS
81 // Accessors for tests to directly call read/writePixelsImpl
82 bool writePixels(GrContext* direct, int left, int top, int width, int height,
83 GrColorType srcColorType, SkColorSpace* srcColorSpace,
84 const void* srcBuffer, size_t srcRowBytes = 0, uint32_t pixelOpsFlags = 0) {
85 return writePixelsImpl(direct, left, top, width, height, srcColorType, srcColorSpace,
86 srcBuffer, srcRowBytes, pixelOpsFlags);
87 }
88
89 bool readPixels(GrContext* direct, int left, int top, int width, int height,
90 GrColorType dstColorType, SkColorSpace* dstColorSpace, void* buffer,
91 size_t rowBytes = 0, uint32_t pixelOpsFlags = 0) {
92 return readPixelsImpl(direct, left, top, width, height, dstColorType, dstColorSpace,
93 buffer, rowBytes, pixelOpsFlags);
94 }
95#endif
96
Robert Phillips27341362016-12-14 08:46:47 -050097 // TODO: this is virtual b.c. this object doesn't have a pointer to the wrapped GrSurfaceProxy?
Robert Phillipsf200a902017-01-30 13:27:37 -050098 virtual GrSurfaceProxy* asSurfaceProxy() = 0;
99 virtual const GrSurfaceProxy* asSurfaceProxy() const = 0;
100 virtual sk_sp<GrSurfaceProxy> asSurfaceProxyRef() = 0;
101
102 virtual GrTextureProxy* asTextureProxy() = 0;
Greg Daniele252f082017-10-23 16:05:23 -0400103 virtual const GrTextureProxy* asTextureProxy() const = 0;
Robert Phillipsf200a902017-01-30 13:27:37 -0500104 virtual sk_sp<GrTextureProxy> asTextureProxyRef() = 0;
105
106 virtual GrRenderTargetProxy* asRenderTargetProxy() = 0;
107 virtual sk_sp<GrRenderTargetProxy> asRenderTargetProxyRef() = 0;
Robert Phillips27341362016-12-14 08:46:47 -0500108
Robert Phillipsd46697a2017-01-25 12:10:37 -0500109 virtual GrRenderTargetContext* asRenderTargetContext() { return nullptr; }
110
Robert Phillips0d075de2019-03-04 11:08:13 -0500111 GrAuditTrail* auditTrail();
Brian Osman45580d32016-11-23 09:37:01 -0500112
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500113 // Provides access to functions that aren't part of the public API.
114 GrSurfaceContextPriv surfPriv();
115 const GrSurfaceContextPriv surfPriv() const;
116
Greg Daniel46cfbc62019-06-07 11:43:30 -0400117#if GR_TEST_UTILS
118 bool testCopy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
119 return this->copy(src, srcRect, dstPoint);
120 }
121
122 bool testCopy(GrSurfaceProxy* src) {
123 return this->copy(src);
124 }
125#endif
126
127
Brian Osman45580d32016-11-23 09:37:01 -0500128protected:
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500129 friend class GrSurfaceContextPriv;
130
Brian Salomond6287472019-06-24 15:50:07 -0400131 GrSurfaceContext(GrRecordingContext*,
132 GrColorType,
133 SkAlphaType,
134 sk_sp<SkColorSpace>,
135 GrPixelConfig);
Robert Phillips72152832017-01-25 17:31:35 -0500136
Robert Phillips0d075de2019-03-04 11:08:13 -0500137 GrDrawingManager* drawingManager();
138 const GrDrawingManager* drawingManager() const;
Brian Osman45580d32016-11-23 09:37:01 -0500139
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400140 virtual GrOpList* getOpList() = 0;
141 SkDEBUGCODE(virtual void validate() const = 0;)
142
Robert Phillips0d075de2019-03-04 11:08:13 -0500143 SkDEBUGCODE(GrSingleOwner* singleOwner();)
Brian Osman45580d32016-11-23 09:37:01 -0500144
Robert Phillips69893702019-02-22 11:16:30 -0500145 GrRecordingContext* fContext;
Brian Osman45580d32016-11-23 09:37:01 -0500146
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500147private:
Greg Daniel46cfbc62019-06-07 11:43:30 -0400148 friend class GrSurfaceProxy; // for copy
149
150 /**
151 * Copy 'src' into the proxy backing this context. This call will not do any draw fallback.
152 * Currently only writePixels and replaceRenderTarget call this directly. All other copies
153 * should go through GrSurfaceProxy::Copy.
154 * @param src src of pixels
155 * @param srcRect the subset of 'src' to copy
156 * @param dstPoint the origin of the 'srcRect' in the destination coordinate space
157 * @return true if the copy succeeded; false otherwise
158 *
159 * Note: Notionally, 'srcRect' is clipped to 'src's extent with 'dstPoint' being adjusted.
160 * Then the 'srcRect' offset by 'dstPoint' is clipped against the dst's extent.
161 * The end result is only valid src pixels and dst pixels will be touched but the copied
162 * regions will not be shifted. The 'src' must have the same origin as the backing proxy
163 * of fSurfaceContext.
164 */
165 bool copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint);
166
167 bool copy(GrSurfaceProxy* src) {
168 return this->copy(src, SkIRect::MakeWH(src->width(), src->height()), SkIPoint::Make(0, 0));
169 }
170
Greg Daniel6eb8c242019-06-05 10:22:24 -0400171 bool writePixelsImpl(GrContext* direct, int left, int top, int width, int height,
172 GrColorType srcColorType, SkColorSpace* srcColorSpace,
173 const void* srcBuffer, size_t srcRowBytes, uint32_t pixelOpsFlags);
174
175 bool readPixelsImpl(GrContext* direct, int left, int top, int width,
176 int height, GrColorType dstColorType,
177 SkColorSpace* dstColorSpace, void* buffer, size_t rowBytes,
178 uint32_t pixelOpsFlags);
179
Robert Phillips69893702019-02-22 11:16:30 -0500180 GrColorSpaceInfo fColorSpaceInfo;
Brian Salomonf3569f02017-10-24 12:52:33 -0400181
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500182 typedef SkRefCnt INHERITED;
Brian Osman45580d32016-11-23 09:37:01 -0500183};
184
185#endif