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 GrContextPriv_DEFINED
9 #define GrContextPriv_DEFINED
10
11 #include "GrContext.h"
12 #include "GrSurfaceContext.h"
13
14 class GrBackendRenderTarget;
15 class GrOnFlushCallbackObject;
16 class GrSemaphore;
17 class GrSurfaceProxy;
18 class GrTextureContext;
19
20 class SkDeferredDisplayList;
21
22 /** Class that adds methods to GrContext that are only intended for use internal to Skia.
23 This class is purely a privileged window into GrContext. It should never have additional
24 data members or virtual methods. */
25 class GrContextPriv {
26 public:
27 /**
28 * Create a GrContext without a resource cache
29 */
30 static sk_sp<GrContext> MakeDDL(GrContextThreadSafeProxy*);
31
drawingManager()32 GrDrawingManager* drawingManager() { return fContext->fDrawingManager.get(); }
33
34 sk_sp<GrSurfaceContext> makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy>,
35 sk_sp<SkColorSpace> = nullptr,
36 const SkSurfaceProps* = nullptr);
37
38 sk_sp<GrSurfaceContext> makeDeferredSurfaceContext(const GrSurfaceDesc&,
39 GrMipMapped,
40 SkBackingFit,
41 SkBudgeted);
42
43 sk_sp<GrTextureContext> makeBackendTextureContext(const GrBackendTexture& tex,
44 GrSurfaceOrigin origin,
45 sk_sp<SkColorSpace> colorSpace);
46
47 sk_sp<GrRenderTargetContext> makeBackendTextureRenderTargetContext(
48 const GrBackendTexture& tex,
49 GrSurfaceOrigin origin,
50 int sampleCnt,
51 sk_sp<SkColorSpace> colorSpace,
52 const SkSurfaceProps* = nullptr);
53
54 sk_sp<GrRenderTargetContext> makeBackendRenderTargetRenderTargetContext(
55 const GrBackendRenderTarget&,
56 GrSurfaceOrigin origin,
57 sk_sp<SkColorSpace> colorSpace,
58 const SkSurfaceProps* = nullptr);
59
60 sk_sp<GrRenderTargetContext> makeBackendTextureAsRenderTargetRenderTargetContext(
61 const GrBackendTexture& tex,
62 GrSurfaceOrigin origin,
63 int sampleCnt,
64 sk_sp<SkColorSpace> colorSpace,
65 const SkSurfaceProps* = nullptr);
66
disableGpuYUVConversion()67 bool disableGpuYUVConversion() const { return fContext->fDisableGpuYUVConversion; }
68
69 /**
70 * Call to ensure all drawing to the context has been issued to the
71 * underlying 3D API.
72 * The 'proxy' parameter is a hint. If it is supplied the context will guarantee that
73 * the draws required for that proxy are flushed but it could do more. If no 'proxy' is
74 * provided then all current work will be flushed.
75 */
76 void flush(GrSurfaceProxy*);
77
78 /**
79 * Registers an object for flush-related callbacks. (See GrOnFlushCallbackObject.)
80 *
81 * NOTE: the drawing manager tracks this object as a raw pointer; it is up to the caller to
82 * ensure its lifetime is tied to that of the context.
83 */
84 void addOnFlushCallbackObject(GrOnFlushCallbackObject*);
85
86 void testingOnly_flushAndRemoveOnFlushCallbackObject(GrOnFlushCallbackObject*);
87
88 /**
89 * After this returns any pending writes to the surface will have been issued to the
90 * backend 3D API.
91 */
92 void flushSurfaceWrites(GrSurfaceProxy*);
93
94 /**
95 * After this returns any pending reads or writes to the surface will have been issued to the
96 * backend 3D API.
97 */
98 void flushSurfaceIO(GrSurfaceProxy*);
99
100 /**
101 * Finalizes all pending reads and writes to the surface and also performs an MSAA resolve
102 * if necessary.
103 *
104 * It is not necessary to call this before reading the render target via Skia/GrContext.
105 * GrContext will detect when it must perform a resolve before reading pixels back from the
106 * surface or using it as a texture.
107 */
108 void prepareSurfaceForExternalIO(GrSurfaceProxy*);
109
110 /**
111 * These flags can be used with the read/write pixels functions below.
112 */
113 enum PixelOpsFlags {
114 /** The GrContext will not be flushed before the surface read or write. This means that
115 the read or write may occur before previous draws have executed. */
116 kDontFlush_PixelOpsFlag = 0x1,
117 /** Any surface writes should be flushed to the backend 3D API after the surface operation
118 is complete */
119 kFlushWrites_PixelOp = 0x2,
120 /** The src for write or dst read is unpremultiplied. This is only respected if both the
121 config src and dst configs are an RGBA/BGRA 8888 format. */
122 kUnpremul_PixelOpsFlag = 0x4,
123 };
124
125 /**
126 * Reads a rectangle of pixels from a surface.
127 * @param src the surface context to read from.
128 * @param left left edge of the rectangle to read (inclusive)
129 * @param top top edge of the rectangle to read (inclusive)
130 * @param width width of rectangle to read in pixels.
131 * @param height height of rectangle to read in pixels.
132 * @param dstConfig the pixel config of the destination buffer
133 * @param dstColorSpace color space of the destination buffer
134 * @param buffer memory to read the rectangle into.
135 * @param rowBytes number of bytes bewtween consecutive rows. Zero means rows are tightly
136 * packed.
137 * @param pixelOpsFlags see PixelOpsFlags enum above.
138 *
139 * @return true if the read succeeded, false if not. The read can fail because of an unsupported
140 * pixel configs
141 */
142 bool readSurfacePixels(GrSurfaceContext* src,
143 int left, int top, int width, int height,
144 GrPixelConfig dstConfig, SkColorSpace* dstColorSpace, void* buffer,
145 size_t rowBytes = 0,
146 uint32_t pixelOpsFlags = 0);
147
148 /**
149 * Writes a rectangle of pixels to a surface.
150 * @param dst the surface context to write to.
151 * @param left left edge of the rectangle to write (inclusive)
152 * @param top top edge of the rectangle to write (inclusive)
153 * @param width width of rectangle to write in pixels.
154 * @param height height of rectangle to write in pixels.
155 * @param srcConfig the pixel config of the source buffer
156 * @param srcColorSpace color space of the source buffer
157 * @param buffer memory to read pixels from
158 * @param rowBytes number of bytes between consecutive rows. Zero
159 * means rows are tightly packed.
160 * @param pixelOpsFlags see PixelOpsFlags enum above.
161 * @return true if the write succeeded, false if not. The write can fail because of an
162 * unsupported combination of surface and src configs.
163 */
164 bool writeSurfacePixels(GrSurfaceContext* dst,
165 int left, int top, int width, int height,
166 GrPixelConfig srcConfig, SkColorSpace* srcColorSpace, const void* buffer,
167 size_t rowBytes,
168 uint32_t pixelOpsFlags = 0);
169
getBackend()170 GrBackend getBackend() const { return fContext->fBackend; }
171
getTaskGroup()172 SkTaskGroup* getTaskGroup() { return fContext->fTaskGroup.get(); }
173
proxyProvider()174 GrProxyProvider* proxyProvider() { return fContext->fProxyProvider; }
proxyProvider()175 const GrProxyProvider* proxyProvider() const { return fContext->fProxyProvider; }
176
resourceProvider()177 GrResourceProvider* resourceProvider() { return fContext->fResourceProvider; }
resourceProvider()178 const GrResourceProvider* resourceProvider() const { return fContext->fResourceProvider; }
179
getResourceCache()180 GrResourceCache* getResourceCache() { return fContext->fResourceCache; }
181
getGpu()182 GrGpu* getGpu() { return fContext->fGpu.get(); }
getGpu()183 const GrGpu* getGpu() const { return fContext->fGpu.get(); }
184
getAtlasGlyphCache()185 GrAtlasGlyphCache* getAtlasGlyphCache() { return fContext->fAtlasGlyphCache; }
getTextBlobCache()186 GrTextBlobCache* getTextBlobCache() { return fContext->fTextBlobCache.get(); }
187
188 void moveOpListsToDDL(SkDeferredDisplayList*);
189 void copyOpListsFromDDL(const SkDeferredDisplayList*, GrRenderTargetProxy* newDest);
190
191 private:
GrContextPriv(GrContext * context)192 explicit GrContextPriv(GrContext* context) : fContext(context) {}
193 GrContextPriv(const GrContextPriv&); // unimpl
194 GrContextPriv& operator=(const GrContextPriv&); // unimpl
195
196 // No taking addresses of this type.
197 const GrContextPriv* operator&() const;
198 GrContextPriv* operator&();
199
200 GrContext* fContext;
201
202 friend class GrContext; // to construct/copy this type.
203 };
204
contextPriv()205 inline GrContextPriv GrContext::contextPriv() { return GrContextPriv(this); }
206
contextPriv()207 inline const GrContextPriv GrContext::contextPriv () const {
208 return GrContextPriv(const_cast<GrContext*>(this));
209 }
210
211 #endif
212