blob: bbf67a569ae77743edd187f966108fe8957994cd [file] [log] [blame]
robertphillips4fd74ae2016-08-03 14:26:53 -07001/*
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 GrContextPriv_DEFINED
9#define GrContextPriv_DEFINED
10
11#include "GrContext.h"
Brian Osman45580d32016-11-23 09:37:01 -050012#include "GrSurfaceContext.h"
robertphillips4fd74ae2016-08-03 14:26:53 -070013
Greg Danielbcf612b2017-05-01 13:50:58 +000014class GrBackendRenderTarget;
Brian Salomond17f6582017-07-19 18:28:58 -040015class GrOnFlushCallbackObject;
Greg Danield85f97d2017-03-07 13:37:21 -050016class GrSemaphore;
Robert Phillipse2f7d182016-12-15 09:23:05 -050017class GrSurfaceProxy;
Brian Salomond17f6582017-07-19 18:28:58 -040018class GrTextureContext;
Robert Phillipse2f7d182016-12-15 09:23:05 -050019
robertphillips4fd74ae2016-08-03 14:26:53 -070020/** Class that adds methods to GrContext that are only intended for use internal to Skia.
21 This class is purely a privileged window into GrContext. It should never have additional
22 data members or virtual methods. */
23class GrContextPriv {
24public:
Robert Phillipse42edcc2017-12-13 11:50:22 -050025 /**
26 * Create a GrContext without a resource cache
27 */
28 static sk_sp<GrContext> MakeDDL(GrContextThreadSafeProxy*);
29
bungeman6bd52842016-10-27 09:30:08 -070030 GrDrawingManager* drawingManager() { return fContext->fDrawingManager.get(); }
csmartdaltonbde96c62016-08-31 12:54:46 -070031
Robert Phillipsbf25d432017-04-07 10:08:53 -040032 sk_sp<GrSurfaceContext> makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy>, sk_sp<SkColorSpace>);
Robert Phillips31c26082016-12-14 15:12:15 -050033
Robert Phillipsbf25d432017-04-07 10:08:53 -040034 sk_sp<GrSurfaceContext> makeDeferredSurfaceContext(const GrSurfaceDesc&,
Greg Daniel65c7f662017-10-30 13:39:09 -040035 GrMipMapped,
Robert Phillipsbf25d432017-04-07 10:08:53 -040036 SkBackingFit,
37 SkBudgeted);
Robert Phillipse305cc1f2016-12-14 12:19:05 -050038
Brian Salomond17f6582017-07-19 18:28:58 -040039 sk_sp<GrTextureContext> makeBackendTextureContext(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +000040 GrSurfaceOrigin origin,
Brian Osmanc1e37052017-03-09 14:19:20 -050041 sk_sp<SkColorSpace> colorSpace);
Robert Phillipsd46697a2017-01-25 12:10:37 -050042
Brian Osman11052242016-10-27 14:47:55 -040043 sk_sp<GrRenderTargetContext> makeBackendTextureRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +000044 const GrBackendTexture& tex,
45 GrSurfaceOrigin origin,
46 int sampleCnt,
Brian Osman11052242016-10-27 14:47:55 -040047 sk_sp<SkColorSpace> colorSpace,
Brian Osmanc1e37052017-03-09 14:19:20 -050048 const SkSurfaceProps* = nullptr);
robertphillips4fd74ae2016-08-03 14:26:53 -070049
Brian Osman11052242016-10-27 14:47:55 -040050 sk_sp<GrRenderTargetContext> makeBackendRenderTargetRenderTargetContext(
Greg Danielbcf612b2017-05-01 13:50:58 +000051 const GrBackendRenderTarget&,
52 GrSurfaceOrigin origin,
Brian Osman11052242016-10-27 14:47:55 -040053 sk_sp<SkColorSpace> colorSpace,
54 const SkSurfaceProps* = nullptr);
robertphillips4fd74ae2016-08-03 14:26:53 -070055
Brian Osman11052242016-10-27 14:47:55 -040056 sk_sp<GrRenderTargetContext> makeBackendTextureAsRenderTargetRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +000057 const GrBackendTexture& tex,
58 GrSurfaceOrigin origin,
59 int sampleCnt,
robertphillips4fd74ae2016-08-03 14:26:53 -070060 sk_sp<SkColorSpace> colorSpace,
61 const SkSurfaceProps* = nullptr);
62
Brian Osman46da1cc2017-02-14 14:15:48 -050063 bool disableGpuYUVConversion() const { return fContext->fDisableGpuYUVConversion; }
64
Robert Phillips7ee385e2017-03-30 08:02:11 -040065 /**
66 * Call to ensure all drawing to the context has been issued to the
67 * underlying 3D API.
68 * The 'proxy' parameter is a hint. If it is supplied the context will guarantee that
69 * the draws required for that proxy are flushed but it could do more. If no 'proxy' is
70 * provided then all current work will be flushed.
71 */
72 void flush(GrSurfaceProxy*);
73
Chris Daltonfe199b72017-05-05 11:26:15 -040074 /**
75 * Registers an object for flush-related callbacks. (See GrOnFlushCallbackObject.)
76 *
77 * NOTE: the drawing manager tracks this object as a raw pointer; it is up to the caller to
78 * ensure its lifetime is tied to that of the context.
Robert Phillipseb35f4d2017-03-21 07:56:47 -040079 */
Chris Daltonfe199b72017-05-05 11:26:15 -040080 void addOnFlushCallbackObject(GrOnFlushCallbackObject*);
81
82 void testingOnly_flushAndRemoveOnFlushCallbackObject(GrOnFlushCallbackObject*);
Robert Phillipseb35f4d2017-03-21 07:56:47 -040083
Robert Phillips7ee385e2017-03-30 08:02:11 -040084 /**
85 * After this returns any pending writes to the surface will have been issued to the
86 * backend 3D API.
87 */
88 void flushSurfaceWrites(GrSurfaceProxy*);
89
90 /**
91 * After this returns any pending reads or writes to the surface will have been issued to the
92 * backend 3D API.
93 */
94 void flushSurfaceIO(GrSurfaceProxy*);
95
96 /**
97 * Finalizes all pending reads and writes to the surface and also performs an MSAA resolve
98 * if necessary.
99 *
100 * It is not necessary to call this before reading the render target via Skia/GrContext.
101 * GrContext will detect when it must perform a resolve before reading pixels back from the
102 * surface or using it as a texture.
103 */
104 void prepareSurfaceForExternalIO(GrSurfaceProxy*);
105
Robert Phillipse78b7252017-04-06 07:59:41 -0400106 /**
107 * These flags can be used with the read/write pixels functions below.
108 */
109 enum PixelOpsFlags {
110 /** The GrContext will not be flushed before the surface read or write. This means that
111 the read or write may occur before previous draws have executed. */
112 kDontFlush_PixelOpsFlag = 0x1,
113 /** Any surface writes should be flushed to the backend 3D API after the surface operation
114 is complete */
115 kFlushWrites_PixelOp = 0x2,
116 /** The src for write or dst read is unpremultiplied. This is only respected if both the
117 config src and dst configs are an RGBA/BGRA 8888 format. */
118 kUnpremul_PixelOpsFlag = 0x4,
119 };
120
121 /**
122 * Reads a rectangle of pixels from a surface.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400123 * @param src the surface context to read from.
Robert Phillipse78b7252017-04-06 07:59:41 -0400124 * @param left left edge of the rectangle to read (inclusive)
125 * @param top top edge of the rectangle to read (inclusive)
126 * @param width width of rectangle to read in pixels.
127 * @param height height of rectangle to read in pixels.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400128 * @param dstConfig the pixel config of the destination buffer
Robert Phillipse78b7252017-04-06 07:59:41 -0400129 * @param dstColorSpace color space of the destination buffer
130 * @param buffer memory to read the rectangle into.
131 * @param rowBytes number of bytes bewtween consecutive rows. Zero means rows are tightly
132 * packed.
133 * @param pixelOpsFlags see PixelOpsFlags enum above.
134 *
135 * @return true if the read succeeded, false if not. The read can fail because of an unsupported
136 * pixel configs
137 */
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400138 bool readSurfacePixels(GrSurfaceContext* src,
Robert Phillipse78b7252017-04-06 07:59:41 -0400139 int left, int top, int width, int height,
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400140 GrPixelConfig dstConfig, SkColorSpace* dstColorSpace, void* buffer,
Robert Phillipse78b7252017-04-06 07:59:41 -0400141 size_t rowBytes = 0,
142 uint32_t pixelOpsFlags = 0);
143
144 /**
145 * Writes a rectangle of pixels to a surface.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400146 * @param dst the surface context to write to.
Robert Phillipse78b7252017-04-06 07:59:41 -0400147 * @param left left edge of the rectangle to write (inclusive)
148 * @param top top edge of the rectangle to write (inclusive)
149 * @param width width of rectangle to write in pixels.
150 * @param height height of rectangle to write in pixels.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400151 * @param srcConfig the pixel config of the source buffer
Robert Phillipse78b7252017-04-06 07:59:41 -0400152 * @param srcColorSpace color space of the source buffer
153 * @param buffer memory to read pixels from
154 * @param rowBytes number of bytes between consecutive rows. Zero
155 * means rows are tightly packed.
156 * @param pixelOpsFlags see PixelOpsFlags enum above.
157 * @return true if the write succeeded, false if not. The write can fail because of an
158 * unsupported combination of surface and src configs.
159 */
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400160 bool writeSurfacePixels(GrSurfaceContext* dst,
Robert Phillipse78b7252017-04-06 07:59:41 -0400161 int left, int top, int width, int height,
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400162 GrPixelConfig srcConfig, SkColorSpace* srcColorSpace, const void* buffer,
Robert Phillipse78b7252017-04-06 07:59:41 -0400163 size_t rowBytes,
164 uint32_t pixelOpsFlags = 0);
165
Greg Danielfc978fd2017-04-13 09:54:12 -0400166 GrBackend getBackend() const { return fContext->fBackend; }
167
Brian Osman51279982017-08-23 10:12:00 -0400168 SkTaskGroup* getTaskGroup() { return fContext->fTaskGroup.get(); }
169
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500170 GrProxyProvider* proxyProvider() { return fContext->fProxyProvider; }
171 const GrProxyProvider* proxyProvider() const { return fContext->fProxyProvider; }
172
Robert Phillips6be756b2018-01-16 15:07:54 -0500173 GrResourceProvider* resourceProvider() { return fContext->fResourceProvider; }
174 const GrResourceProvider* resourceProvider() const { return fContext->fResourceProvider; }
175
176 GrResourceCache* getResourceCache() { return fContext->fResourceCache; }
177
robertphillips4fd74ae2016-08-03 14:26:53 -0700178private:
179 explicit GrContextPriv(GrContext* context) : fContext(context) {}
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400180 GrContextPriv(const GrContextPriv&); // unimpl
robertphillips4fd74ae2016-08-03 14:26:53 -0700181 GrContextPriv& operator=(const GrContextPriv&); // unimpl
182
183 // No taking addresses of this type.
184 const GrContextPriv* operator&() const;
185 GrContextPriv* operator&();
186
187 GrContext* fContext;
188
189 friend class GrContext; // to construct/copy this type.
190};
191
192inline GrContextPriv GrContext::contextPriv() { return GrContextPriv(this); }
193
194inline const GrContextPriv GrContext::contextPriv () const {
195 return GrContextPriv(const_cast<GrContext*>(this));
196}
197
198#endif