blob: c5f592b8758a582460fe2648ae72cd8f9c8520af [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;
Greg Danield85f97d2017-03-07 13:37:21 -050015class GrSemaphore;
Robert Phillipse2f7d182016-12-15 09:23:05 -050016class GrSurfaceProxy;
Chris Daltonfe199b72017-05-05 11:26:15 -040017class GrOnFlushCallbackObject;
Robert Phillipse2f7d182016-12-15 09:23:05 -050018
robertphillips4fd74ae2016-08-03 14:26:53 -070019/** Class that adds methods to GrContext that are only intended for use internal to Skia.
20 This class is purely a privileged window into GrContext. It should never have additional
21 data members or virtual methods. */
22class GrContextPriv {
23public:
bungeman6bd52842016-10-27 09:30:08 -070024 GrDrawingManager* drawingManager() { return fContext->fDrawingManager.get(); }
csmartdaltonbde96c62016-08-31 12:54:46 -070025
Brian Osman11052242016-10-27 14:47:55 -040026 // Create a renderTargetContext that wraps an existing renderTarget
Robert Phillipsbf25d432017-04-07 10:08:53 -040027 sk_sp<GrRenderTargetContext> makeWrappedRenderTargetContext(sk_sp<GrRenderTarget>,
28 sk_sp<SkColorSpace>,
Brian Osman11052242016-10-27 14:47:55 -040029 const SkSurfaceProps* = nullptr);
robertphillips4fd74ae2016-08-03 14:26:53 -070030
Brian Osman45580d32016-11-23 09:37:01 -050031 // Create a surfaceContext that wraps an existing texture or renderTarget
Robert Phillipsbf25d432017-04-07 10:08:53 -040032 sk_sp<GrSurfaceContext> makeWrappedSurfaceContext(sk_sp<GrSurface>);
Brian Osman45580d32016-11-23 09:37:01 -050033
Robert Phillipsbf25d432017-04-07 10:08:53 -040034 sk_sp<GrSurfaceContext> makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy>, sk_sp<SkColorSpace>);
Robert Phillips31c26082016-12-14 15:12:15 -050035
Robert Phillipsbf25d432017-04-07 10:08:53 -040036 sk_sp<GrSurfaceContext> makeDeferredSurfaceContext(const GrSurfaceDesc&,
37 SkBackingFit,
38 SkBudgeted);
Robert Phillipse305cc1f2016-12-14 12:19:05 -050039
Robert Phillips26caf892017-01-27 10:58:31 -050040 // TODO: Maybe add a 'surfaceProps' param (that is ignored for non-RTs) and remove
41 // makeBackendTextureRenderTargetContext & makeBackendTextureAsRenderTargetRenderTargetContext
Greg Daniel7ef28f32017-04-20 16:41:55 +000042 sk_sp<GrSurfaceContext> makeBackendSurfaceContext(const GrBackendTexture& tex,
43 GrSurfaceOrigin origin,
44 GrBackendTextureFlags flags,
45 int sampleCnt,
Brian Osmanc1e37052017-03-09 14:19:20 -050046 sk_sp<SkColorSpace> colorSpace);
Robert Phillipsd46697a2017-01-25 12:10:37 -050047
Brian Osman11052242016-10-27 14:47:55 -040048 sk_sp<GrRenderTargetContext> makeBackendTextureRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +000049 const GrBackendTexture& tex,
50 GrSurfaceOrigin origin,
51 int sampleCnt,
Brian Osman11052242016-10-27 14:47:55 -040052 sk_sp<SkColorSpace> colorSpace,
Brian Osmanc1e37052017-03-09 14:19:20 -050053 const SkSurfaceProps* = nullptr);
robertphillips4fd74ae2016-08-03 14:26:53 -070054
Brian Osman11052242016-10-27 14:47:55 -040055 sk_sp<GrRenderTargetContext> makeBackendRenderTargetRenderTargetContext(
Greg Danielbcf612b2017-05-01 13:50:58 +000056 const GrBackendRenderTarget&,
57 GrSurfaceOrigin origin,
Brian Osman11052242016-10-27 14:47:55 -040058 sk_sp<SkColorSpace> colorSpace,
59 const SkSurfaceProps* = nullptr);
robertphillips4fd74ae2016-08-03 14:26:53 -070060
Brian Osman11052242016-10-27 14:47:55 -040061 sk_sp<GrRenderTargetContext> makeBackendTextureAsRenderTargetRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +000062 const GrBackendTexture& tex,
63 GrSurfaceOrigin origin,
64 int sampleCnt,
robertphillips4fd74ae2016-08-03 14:26:53 -070065 sk_sp<SkColorSpace> colorSpace,
66 const SkSurfaceProps* = nullptr);
67
Brian Osman46da1cc2017-02-14 14:15:48 -050068 bool disableGpuYUVConversion() const { return fContext->fDisableGpuYUVConversion; }
69
Robert Phillips7ee385e2017-03-30 08:02:11 -040070 /**
71 * Call to ensure all drawing to the context has been issued to the
72 * underlying 3D API.
73 * The 'proxy' parameter is a hint. If it is supplied the context will guarantee that
74 * the draws required for that proxy are flushed but it could do more. If no 'proxy' is
75 * provided then all current work will be flushed.
76 */
77 void flush(GrSurfaceProxy*);
78
Chris Daltonfe199b72017-05-05 11:26:15 -040079 /**
80 * Registers an object for flush-related callbacks. (See GrOnFlushCallbackObject.)
81 *
82 * NOTE: the drawing manager tracks this object as a raw pointer; it is up to the caller to
83 * ensure its lifetime is tied to that of the context.
Robert Phillipseb35f4d2017-03-21 07:56:47 -040084 */
Chris Daltonfe199b72017-05-05 11:26:15 -040085 void addOnFlushCallbackObject(GrOnFlushCallbackObject*);
86
87 void testingOnly_flushAndRemoveOnFlushCallbackObject(GrOnFlushCallbackObject*);
Robert Phillipseb35f4d2017-03-21 07:56:47 -040088
Robert Phillips7ee385e2017-03-30 08:02:11 -040089 /**
90 * After this returns any pending writes to the surface will have been issued to the
91 * backend 3D API.
92 */
93 void flushSurfaceWrites(GrSurfaceProxy*);
94
95 /**
96 * After this returns any pending reads or writes to the surface will have been issued to the
97 * backend 3D API.
98 */
99 void flushSurfaceIO(GrSurfaceProxy*);
100
101 /**
102 * Finalizes all pending reads and writes to the surface and also performs an MSAA resolve
103 * if necessary.
104 *
105 * It is not necessary to call this before reading the render target via Skia/GrContext.
106 * GrContext will detect when it must perform a resolve before reading pixels back from the
107 * surface or using it as a texture.
108 */
109 void prepareSurfaceForExternalIO(GrSurfaceProxy*);
110
Robert Phillipse78b7252017-04-06 07:59:41 -0400111 /**
112 * These flags can be used with the read/write pixels functions below.
113 */
114 enum PixelOpsFlags {
115 /** The GrContext will not be flushed before the surface read or write. This means that
116 the read or write may occur before previous draws have executed. */
117 kDontFlush_PixelOpsFlag = 0x1,
118 /** Any surface writes should be flushed to the backend 3D API after the surface operation
119 is complete */
120 kFlushWrites_PixelOp = 0x2,
121 /** The src for write or dst read is unpremultiplied. This is only respected if both the
122 config src and dst configs are an RGBA/BGRA 8888 format. */
123 kUnpremul_PixelOpsFlag = 0x4,
124 };
125
126 /**
127 * Reads a rectangle of pixels from a surface.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400128 * @param src the surface context to read from.
Robert Phillipse78b7252017-04-06 07:59:41 -0400129 * @param left left edge of the rectangle to read (inclusive)
130 * @param top top edge of the rectangle to read (inclusive)
131 * @param width width of rectangle to read in pixels.
132 * @param height height of rectangle to read in pixels.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400133 * @param dstConfig the pixel config of the destination buffer
Robert Phillipse78b7252017-04-06 07:59:41 -0400134 * @param dstColorSpace color space of the destination buffer
135 * @param buffer memory to read the rectangle into.
136 * @param rowBytes number of bytes bewtween consecutive rows. Zero means rows are tightly
137 * packed.
138 * @param pixelOpsFlags see PixelOpsFlags enum above.
139 *
140 * @return true if the read succeeded, false if not. The read can fail because of an unsupported
141 * pixel configs
142 */
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400143 bool readSurfacePixels(GrSurfaceContext* src,
Robert Phillipse78b7252017-04-06 07:59:41 -0400144 int left, int top, int width, int height,
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400145 GrPixelConfig dstConfig, SkColorSpace* dstColorSpace, void* buffer,
Robert Phillipse78b7252017-04-06 07:59:41 -0400146 size_t rowBytes = 0,
147 uint32_t pixelOpsFlags = 0);
148
149 /**
150 * Writes a rectangle of pixels to a surface.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400151 * @param dst the surface context to write to.
Robert Phillipse78b7252017-04-06 07:59:41 -0400152 * @param left left edge of the rectangle to write (inclusive)
153 * @param top top edge of the rectangle to write (inclusive)
154 * @param width width of rectangle to write in pixels.
155 * @param height height of rectangle to write in pixels.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400156 * @param srcConfig the pixel config of the source buffer
Robert Phillipse78b7252017-04-06 07:59:41 -0400157 * @param srcColorSpace color space of the source buffer
158 * @param buffer memory to read pixels from
159 * @param rowBytes number of bytes between consecutive rows. Zero
160 * means rows are tightly packed.
161 * @param pixelOpsFlags see PixelOpsFlags enum above.
162 * @return true if the write succeeded, false if not. The write can fail because of an
163 * unsupported combination of surface and src configs.
164 */
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400165 bool writeSurfacePixels(GrSurfaceContext* dst,
Robert Phillipse78b7252017-04-06 07:59:41 -0400166 int left, int top, int width, int height,
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400167 GrPixelConfig srcConfig, SkColorSpace* srcColorSpace, const void* buffer,
Robert Phillipse78b7252017-04-06 07:59:41 -0400168 size_t rowBytes,
169 uint32_t pixelOpsFlags = 0);
170
Greg Danielfc978fd2017-04-13 09:54:12 -0400171 GrBackend getBackend() const { return fContext->fBackend; }
172
robertphillips4fd74ae2016-08-03 14:26:53 -0700173private:
174 explicit GrContextPriv(GrContext* context) : fContext(context) {}
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400175 GrContextPriv(const GrContextPriv&); // unimpl
robertphillips4fd74ae2016-08-03 14:26:53 -0700176 GrContextPriv& operator=(const GrContextPriv&); // unimpl
177
178 // No taking addresses of this type.
179 const GrContextPriv* operator&() const;
180 GrContextPriv* operator&();
181
182 GrContext* fContext;
183
184 friend class GrContext; // to construct/copy this type.
185};
186
187inline GrContextPriv GrContext::contextPriv() { return GrContextPriv(this); }
188
189inline const GrContextPriv GrContext::contextPriv () const {
190 return GrContextPriv(const_cast<GrContext*>(this));
191}
192
193#endif