1 | /* |
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 | |
11 | #include "include/core/SkFilterQuality.h" |
12 | #include "include/core/SkImage.h" |
13 | #include "include/core/SkRect.h" |
14 | #include "include/core/SkRefCnt.h" |
15 | #include "include/core/SkSurface.h" |
16 | #include "src/gpu/GrClientMappedBufferManager.h" |
17 | #include "src/gpu/GrColorInfo.h" |
18 | #include "src/gpu/GrDataUtils.h" |
19 | #include "src/gpu/GrImageInfo.h" |
20 | #include "src/gpu/GrSurfaceProxy.h" |
21 | #include "src/gpu/GrSurfaceProxyView.h" |
22 | |
23 | class GrAuditTrail; |
24 | class GrDrawingManager; |
25 | class GrRecordingContext; |
26 | class GrRenderTargetContext; |
27 | class GrRenderTargetProxy; |
28 | class GrSingleOwner; |
29 | class GrSurface; |
30 | class GrSurfaceContextPriv; |
31 | class GrSurfaceProxy; |
32 | class GrTextureProxy; |
33 | struct SkIPoint; |
34 | struct SkIRect; |
35 | |
36 | /** |
37 | * A helper object to orchestrate commands for a particular surface |
38 | */ |
39 | class GrSurfaceContext { |
40 | public: |
41 | // If the passed in GrSurfaceProxy is renderable this will return a GrRenderTargetContext, |
42 | // otherwise it will return a GrSurfaceContext. |
43 | static std::unique_ptr<GrSurfaceContext> Make(GrRecordingContext*, |
44 | GrSurfaceProxyView readView, |
45 | GrColorType, SkAlphaType, sk_sp<SkColorSpace>); |
46 | |
47 | static std::unique_ptr<GrSurfaceContext> Make(GrRecordingContext*, SkISize dimensions, |
48 | const GrBackendFormat&, GrRenderable, |
49 | int renderTargetSampleCnt, GrMipmapped, |
50 | GrProtected, GrSurfaceOrigin, GrColorType, |
51 | SkAlphaType, sk_sp<SkColorSpace>, SkBackingFit, |
52 | SkBudgeted); |
53 | |
54 | // If it is known that the GrSurfaceProxy is not renderable, you can directly call the the ctor |
55 | // here to make a GrSurfaceContext on the stack. |
56 | GrSurfaceContext(GrRecordingContext*, GrSurfaceProxyView readView, GrColorType, SkAlphaType, |
57 | sk_sp<SkColorSpace>); |
58 | |
59 | virtual ~GrSurfaceContext() = default; |
60 | |
61 | const GrColorInfo& colorInfo() const { return fColorInfo; } |
62 | GrImageInfo imageInfo() const { return {fColorInfo, fReadView.proxy()->dimensions()}; } |
63 | |
64 | GrSurfaceOrigin origin() const { return fReadView.origin(); } |
65 | GrSwizzle readSwizzle() const { return fReadView.swizzle(); } |
66 | // TODO: See if it makes sense for this to return a const& instead and require the callers to |
67 | // make a copy (which refs the proxy) if needed. |
68 | GrSurfaceProxyView readSurfaceView() { return fReadView; } |
69 | |
70 | SkISize dimensions() const { return fReadView.dimensions(); } |
71 | int width() const { return fReadView.proxy()->width(); } |
72 | int height() const { return fReadView.proxy()->height(); } |
73 | |
74 | const GrCaps* caps() const; |
75 | |
76 | /** |
77 | * Reads a rectangle of pixels from the render target context. |
78 | * @param dContext The direct context to use |
79 | * @param dstInfo image info for the destination |
80 | * @param dst destination pixels for the read |
81 | * @param rowBytes bytes in a row of 'dst' |
82 | * @param srcPt offset w/in the surface context from which to read |
83 | * is a GrDirectContext and fail otherwise. |
84 | */ |
85 | bool readPixels(GrDirectContext* dContext, |
86 | const GrImageInfo& dstInfo, |
87 | void* dst, |
88 | size_t rowBytes, |
89 | SkIPoint srcPt); |
90 | |
91 | using ReadPixelsCallback = SkImage::ReadPixelsCallback; |
92 | using ReadPixelsContext = SkImage::ReadPixelsContext; |
93 | using RescaleGamma = SkImage::RescaleGamma; |
94 | |
95 | // GPU implementation for SkImage:: and SkSurface::asyncRescaleAndReadPixels. |
96 | void asyncRescaleAndReadPixels(GrDirectContext*, |
97 | const SkImageInfo& info, |
98 | const SkIRect& srcRect, |
99 | RescaleGamma rescaleGamma, |
100 | SkFilterQuality rescaleQuality, |
101 | ReadPixelsCallback callback, |
102 | ReadPixelsContext callbackContext); |
103 | |
104 | // GPU implementation for SkImage:: and SkSurface::asyncRescaleAndReadPixelsYUV420. |
105 | void asyncRescaleAndReadPixelsYUV420(GrDirectContext*, |
106 | SkYUVColorSpace yuvColorSpace, |
107 | sk_sp<SkColorSpace> dstColorSpace, |
108 | const SkIRect& srcRect, |
109 | SkISize dstSize, |
110 | RescaleGamma rescaleGamma, |
111 | SkFilterQuality rescaleQuality, |
112 | ReadPixelsCallback callback, |
113 | ReadPixelsContext context); |
114 | |
115 | /** |
116 | * Writes a rectangle of pixels [srcInfo, srcBuffer, srcRowbytes] into the |
117 | * renderTargetContext at the specified position. |
118 | * @param dContext The direct context to use |
119 | * @param srcInfo image info for the source pixels |
120 | * @param src source for the write |
121 | * @param rowBytes bytes in a row of 'src' |
122 | * @param dstPt offset w/in the surface context at which to write |
123 | */ |
124 | bool writePixels(GrDirectContext* dContext, |
125 | const GrImageInfo& srcInfo, |
126 | const void* src, |
127 | size_t rowBytes, |
128 | SkIPoint dstPt); |
129 | |
130 | GrSurfaceProxy* asSurfaceProxy() { return fReadView.proxy(); } |
131 | const GrSurfaceProxy* asSurfaceProxy() const { return fReadView.proxy(); } |
132 | sk_sp<GrSurfaceProxy> asSurfaceProxyRef() { return fReadView.refProxy(); } |
133 | |
134 | GrTextureProxy* asTextureProxy() { return fReadView.asTextureProxy(); } |
135 | const GrTextureProxy* asTextureProxy() const { return fReadView.asTextureProxy(); } |
136 | sk_sp<GrTextureProxy> asTextureProxyRef() { return fReadView.asTextureProxyRef(); } |
137 | |
138 | GrRenderTargetProxy* asRenderTargetProxy() { return fReadView.asRenderTargetProxy(); } |
139 | const GrRenderTargetProxy* asRenderTargetProxy() const { |
140 | return fReadView.asRenderTargetProxy(); |
141 | } |
142 | sk_sp<GrRenderTargetProxy> asRenderTargetProxyRef() { |
143 | return fReadView.asRenderTargetProxyRef(); |
144 | } |
145 | |
146 | virtual GrRenderTargetContext* asRenderTargetContext() { return nullptr; } |
147 | |
148 | /** |
149 | * Rescales the contents of srcRect. The gamma in which the rescaling occurs is controlled by |
150 | * RescaleGamma. It is always in the original gamut. The result is converted to the color type |
151 | * and color space of info after rescaling. Note: this currently requires that the info have a |
152 | * different size than srcRect. Though, it could be relaxed to allow non-scaling color |
153 | * conversions. |
154 | */ |
155 | std::unique_ptr<GrRenderTargetContext> rescale(const GrImageInfo& info, |
156 | GrSurfaceOrigin, |
157 | SkIRect srcRect, |
158 | SkImage::RescaleGamma, |
159 | SkFilterQuality); |
160 | |
161 | /** |
162 | * After this returns any pending surface IO will be issued to the backend 3D API and |
163 | * if the surface has MSAA it will be resolved. |
164 | */ |
165 | GrSemaphoresSubmitted flush(SkSurface::BackendSurfaceAccess access, |
166 | const GrFlushInfo&, |
167 | const GrBackendSurfaceMutableState*); |
168 | |
169 | GrAuditTrail* auditTrail(); |
170 | |
171 | // Provides access to functions that aren't part of the public API. |
172 | GrSurfaceContextPriv surfPriv(); |
173 | const GrSurfaceContextPriv surfPriv() const; // NOLINT(readability-const-return-type) |
174 | |
175 | #if GR_TEST_UTILS |
176 | bool testCopy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) { |
177 | return this->copy(src, srcRect, dstPoint); |
178 | } |
179 | |
180 | bool testCopy(GrSurfaceProxy* src) { |
181 | return this->copy(src, SkIRect::MakeSize(src->dimensions()), {0, 0}); |
182 | } |
183 | #endif |
184 | |
185 | protected: |
186 | friend class GrSurfaceContextPriv; |
187 | |
188 | GrDrawingManager* drawingManager(); |
189 | const GrDrawingManager* drawingManager() const; |
190 | |
191 | SkDEBUGCODE(void validate() const;) |
192 | |
193 | SkDEBUGCODE(GrSingleOwner* singleOwner();) |
194 | |
195 | GrRecordingContext* fContext; |
196 | |
197 | GrSurfaceProxyView fReadView; |
198 | |
199 | // Inserts a transfer, part of the implementation of asyncReadPixels and |
200 | // asyncRescaleAndReadPixelsYUV420(). |
201 | struct PixelTransferResult { |
202 | using ConversionFn = void(void* dst, const void* mappedBuffer); |
203 | // If null then the transfer could not be performed. Otherwise this buffer will contain |
204 | // the pixel data when the transfer is complete. |
205 | sk_sp<GrGpuBuffer> fTransferBuffer; |
206 | // If this is null then the transfer buffer will contain the data in the requested |
207 | // color type. Otherwise, when the transfer is done this must be called to convert |
208 | // from the transfer buffer's color type to the requested color type. |
209 | std::function<ConversionFn> fPixelConverter; |
210 | }; |
211 | PixelTransferResult transferPixels(GrColorType colorType, const SkIRect& rect); |
212 | |
213 | // The async read step of asyncRescaleAndReadPixels() |
214 | void asyncReadPixels(GrDirectContext*, |
215 | const SkIRect& srcRect, |
216 | SkColorType, |
217 | ReadPixelsCallback, |
218 | ReadPixelsContext); |
219 | |
220 | private: |
221 | friend class GrSurfaceProxy; // for copy |
222 | |
223 | SkDEBUGCODE(virtual void onValidate() const {}) |
224 | |
225 | /** |
226 | * Copy 'src' into the proxy backing this context. This call will not do any draw fallback. |
227 | * Currently only writePixels and replaceRenderTarget call this directly. All other copies |
228 | * should go through GrSurfaceProxy::Copy. |
229 | * @param src src of pixels |
230 | * @param dstPoint the origin of the 'srcRect' in the destination coordinate space |
231 | * @return true if the copy succeeded; false otherwise |
232 | * |
233 | * Note: Notionally, 'srcRect' is clipped to 'src's extent with 'dstPoint' being adjusted. |
234 | * Then the 'srcRect' offset by 'dstPoint' is clipped against the dst's extent. |
235 | * The end result is only valid src pixels and dst pixels will be touched but the copied |
236 | * regions will not be shifted. The 'src' must have the same origin as the backing proxy |
237 | * of fSurfaceContext. |
238 | */ |
239 | bool copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint); |
240 | |
241 | class AsyncReadResult; |
242 | |
243 | GrColorInfo fColorInfo; |
244 | |
245 | typedef SkRefCnt INHERITED; |
246 | }; |
247 | |
248 | #endif |
249 | |