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