blob: 5a9281e883851fdfbeb50abd63bdb9e73106627c [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
2 Copyright 2010 Google Inc.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17
18#ifndef GrGpu_DEFINED
19#define GrGpu_DEFINED
20
21#include "GrRect.h"
22#include "GrRefCnt.h"
23#include "GrDrawTarget.h"
24#include "GrGpuVertex.h"
25#include "GrTexture.h"
26#include "GrMemory.h"
27
28
29class GrGpu : public GrDrawTarget {
30
31public:
32 /**
33 * Possible 3D APIs that may be used by Ganesh.
34 */
35 enum Engine {
36 kOpenGL_Shaders_Engine,
37 kOpenGL_Fixed_Engine,
38 kDirect3D9_Engine
39 };
40
41 /**
42 * Platform specific 3D context.
43 * For
44 * kOpenGL_Shaders_Engine use NULL
45 * kOpenGL_Fixed_Engine use NULL
46 * kDirect3D9_Engine use an IDirect3DDevice9*
47 */
48 typedef void* Platform3DContext;
49
50 /**
51 * Create an instance of GrGpu that matches the specified Engine backend.
52 * If the requested engine is not supported (at compile-time or run-time)
53 * this returns NULL.
54 */
55 static GrGpu* Create(Engine, Platform3DContext context3D);
56
57 /**
58 * Describes levels of support for non-power-of-two textures.
59 */
60 enum NPOTTextureTypes {
61 /**
62 * no support for NPOT textures
63 */
64 kNone_NPOTTextureType,
65 /**
66 * only clamp is supported for textures
67 */
68 kNoRepeat_NPOTTextureType,
69 /**
70 * no texture restrictions at all, but rendertargets must be POW2
71 */
72 kNonRendertarget_NPOTTextureType,
73 /**
74 * no POW2 restrictions at all
75 */
76 kFull_NPOTTextureType
77 };
78
79 /**
80 * Used to control the level of antialiasing available for a rendertarget.
81 * Anti-alias quality levels depend on the underlying API/GPU capabilities.
82 */
83 enum AALevels {
84 kNone_AALevel, //<! No antialiasing available.
85 kLow_AALevel, //<! Low quality antialiased rendering. Actual
86 // interpretation is platform-dependent.
87 kMed_AALevel, //<! Medium quality antialiased rendering. Actual
88 // interpretation is platform-dependent.
89 kHigh_AALevel, //<! High quality antialiased rendering. Actual
90 // interpretation is platform-dependent.
91 };
92
93
94 /**
95 * Optional bitfield flags that can be passed to createTexture.
96 */
97 enum TextureFlags {
98 kRenderTarget_TextureFlag = 0x1, //<! Creates a texture that can be
99 // rendered to by calling
100 // GrGpu::setRenderTarget() with
101 // GrTexture::asRenderTarget().
102 kNoPathRendering_TextureFlag = 0x2, //<! If the texture is used as a
103 // rendertarget but paths will not
104 // be rendered to it.
105 kDynamicUpdate_TextureFlag = 0x4 //!< Hint that the CPU may modify
106 // this texture after creation
107 };
108
109 enum {
110 /**
111 * For Index8 pixel config, the colortable must be 256 entries
112 */
113 kColorTableSize = 256 * sizeof(GrColor)
114 };
115 /**
116 * Describes a texture to be created.
117 */
118 struct TextureDesc {
119 uint32_t fFlags; //!< bitfield of TextureFlags
120 GrGpu::AALevels fAALevel;//!< The level of antialiasing available
121 // for a rendertarget texture. Only
122 // flags contains
123 // kRenderTarget_TextureFlag.
124 uint32_t fWidth; //!< Width of the texture
125 uint32_t fHeight; //!< Height of the texture
126 GrTexture::PixelConfig fFormat; //!< Format of source data of the
127 // texture. Not guaraunteed to be the
128 // same as internal format used by
129 // 3D API.
130 };
131
132 /**
133 * Gpu usage statistics.
134 */
135 struct Stats {
136 uint32_t fVertexCnt; //<! Number of vertices drawn
137 uint32_t fIndexCnt; //<! Number of indices drawn
138 uint32_t fDrawCnt; //<! Number of draws
139
140 uint32_t fProgChngCnt;//<! Number of program changes (N/A for fixed)
141
142 /*
143 * Number of times the texture is set in 3D API
144 */
145 uint32_t fTextureChngCnt;
146 /*
147 * Number of times the render target is set in 3D API
148 */
149 uint32_t fRenderTargetChngCnt;
150 /*
151 * Number of textures created (includes textures that are rendertargets).
152 */
153 uint32_t fTextureCreateCnt;
154 /*
155 * Number of rendertargets created.
156 */
157 uint32_t fRenderTargetCreateCnt;
158 };
159
160 ////////////////////////////////////////////////////////////////////////////
161
162 GrGpu();
163 virtual ~GrGpu();
164
165 /**
166 * The GrGpu object normally assumes that no outsider is setting state
167 * within the underlying 3D API's context/device/whatever. This call informs
168 * the GrGpu that the state was modified and it should resend. Shouldn't
169 * be called frequently for good performance.
170 */
171 virtual void resetContext();
172
173 void unimpl(const char[]);
174
175 /**
176 * Creates a texture object
177 *
178 * @param desc describes the texture to be created.
179 * @param srcData texel data to load texture. Begins with full-size
180 * palette data for paletted textures. Contains width*
181 * height texels. If NULL texture data is uninitialized.
182 *
183 * @return The texture object if successful, otherwise NULL.
184 */
185 virtual GrTexture* createTexture(const TextureDesc& desc,
186 const void* srcData, size_t rowBytes) = 0;
187 /**
188 * Wraps an externally-created rendertarget in a GrRenderTarget.
189 * @param platformRenderTarget handle to the the render target in the
190 * underlying 3D API. Interpretation depends on
191 * GrGpu subclass in use.
192 * @param width width of the render target
193 * @param height height of the render target
194 */
195 virtual GrRenderTarget* createPlatformRenderTarget(
196 intptr_t platformRenderTarget,
197 int width, int height) = 0;
198
199 /**
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000200 * Reads the current target object (e.g. FBO or IDirect3DSurface9*) and
201 * viewport state from the underlying 3D API and wraps it in a
202 * GrRenderTarget. The GrRenderTarget will not attempt to delete/destroy the
203 * underlying object in its destructor and it is up to caller to guarantee
204 * that it remains valid while the GrRenderTarget is used.
205 *
206 * @return the newly created GrRenderTarget
207 */
208 virtual GrRenderTarget* createRenderTargetFrom3DApiState() = 0;
209
210 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000211 * Creates a vertex buffer.
212 *
213 * @param size size in bytes of the vertex buffer
214 * @param dynamic hints whether the data will be frequently changed
215 * by either GrVertexBuffer::lock or
216 * GrVertexBuffer::updateData.
217 *
218 * @return The vertex buffer if successful, otherwise NULL.
219 */
220 virtual GrVertexBuffer* createVertexBuffer(uint32_t size, bool dynamic) = 0;
221
222 /**
223 * Creates an index buffer.
224 *
225 * @param size size in bytes of the index buffer
226 * @param dynamic hints whether the data will be frequently changed
227 * by either GrIndexBuffer::lock or
228 * GrIndexBuffer::updateData.
229 *
230 * @return The index buffer if successful, otherwise NULL.
231 */
232 virtual GrIndexBuffer* createIndexBuffer(uint32_t size, bool dynamic) = 0;
233
234 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000235 * Erase the entire render target, ignoring any clips/scissors.
236 *
237 * This is issued to the GPU driver immediately.
238 */
239 virtual void eraseColor(GrColor color) = 0;
240
241 /**
242 * Are 8 bit paletted textures supported.
243 *
244 * @return true if 8bit palette textures are supported, false otherwise
245 */
246 bool supports8BitPalette() const { return f8bitPaletteSupport; }
247
248 /**
249 * If single stencil pass winding is supported then one stencil pass
250 * (kWindingStencil1_PathPass) is required to do winding rule path filling
251 * (or inverse winding rule). Otherwise, two passes are required
252 * (kWindingStencil1_PathPass followed by kWindingStencil2_PathPass).
253 *
254 * @return true if only a single stencil pass is needed.
255 */
256 bool supportsSingleStencilPassWinding() const
257 { return fSingleStencilPassForWinding; }
258
259 /**
260 * Checks whether locking vertex and index buffers is supported.
261 *
262 * @return true if locking is supported.
263 */
264 bool supportsBufferLocking() const { return fBufferLockSupport; }
265
266 /**
267 * Gets the minimum width of a render target. If a texture/rt is created
268 * with a width less than this size the GrGpu object will clamp it to this
269 * value.
270 */
271 int minRenderTargetWidth() const { return fMinRenderTargetWidth; }
272
273 /**
274 * Gets the minimum width of a render target. If a texture/rt is created
275 * with a height less than this size the GrGpu object will clamp it to this
276 * value.
277 */
278 int minRenderTargetHeight() const { return fMinRenderTargetHeight; }
279
280 /**
281 * Retrieves the level of NPOT texture support. Regardless of support level
282 * NPOT textures can always be created, but internally they may be imbedded
283 * in a POT texture. An exception is paletted textures which must be
284 * specified as a POT when npotTextureSupport() is not Full.
285 *
286 * @return the level of NPOT texture support.
287 */
288 NPOTTextureTypes npotTextureSupport() const { return fNPOTTextureSupport; }
289
reed@google.com02a7e6c2011-01-28 21:21:49 +0000290 int maxTextureDimension() const { return fMaxTextureDimension; }
291
reed@google.comac10a2d2010-12-22 21:39:39 +0000292 // GrDrawTarget overrides
293 virtual void drawIndexed(PrimitiveType type,
294 uint32_t startVertex,
295 uint32_t startIndex,
296 uint32_t vertexCount,
297 uint32_t indexCount);
298
299 virtual void drawNonIndexed(PrimitiveType type,
300 uint32_t startVertex,
301 uint32_t vertexCount);
302
303 /**
304 * Determines if blend is effectively disabled.
305 *
306 * @return true if blend can be disabled without changing the rendering
307 * result given the current state including the vertex layout specified
308 * with the vertex source.
309 */
310 bool canDisableBlend() const;
311
312 /**
313 * Returns an index buffer that can be used to render quads.
314 * Indices are 0, 1, 2, 0, 2, 3, etc.
315 * Draw with kTriangles_PrimitiveType
316 */
317 const GrIndexBuffer* quadIndexBuffer() const;
318 /**
319 * Gets the number of quads that can be rendered using quadIndexBuffer.
320 */
321 int maxQuadsInIndexBuffer() const;
322
323 /**
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000324 * Returns a vertex buffer with four position-only vertices [(0,0), (1,0), (1,1), (0,1)]
325 */
326 const GrVertexBuffer* unitSquareVertexBuffer() const;
327
328 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000329 * Ensures that the current render target is actually set in the
330 * underlying 3D API. Used when client wants to use 3D API to directly
331 * render to the RT.
332 */
333 virtual void forceRenderTargetFlush() = 0;
334
335 virtual bool readPixels(int left, int top, int width, int height,
336 GrTexture::PixelConfig, void* buffer) = 0;
337
338
339 const Stats& getStats() const;
340 void resetStats();
341 void printStats() const;
342
343protected:
344 /**
345 * Extensions to GrDrawTarget::StencilPass to implement stencil clipping
346 */
347 enum GpuStencilPass {
348 kSetClip_StencilPass = kDrawTargetCount_StencilPass,
349 /* rendering a hard clip to the stencil
350 buffer. Subsequent draws with other
351 StencilPass values will be clipped
352 if kStencilClip_StateBit is set. */
353 kGpuCount_StencilPass
354 };
355
356 /**
357 * Extensions to GrDrawTarget::StateBits to implement stencil clipping
358 */
359 struct ClipState {
360 bool fClipInStencil;
361 bool fClipIsDirty;
362 GrRenderTarget* fStencilClipTarget;
363 } fClipState;
364
365 virtual void clipWillChange(const GrClip& clip);
366 bool setupClipAndFlushState(PrimitiveType type);
367
368 struct BoundsState {
369 bool fScissorEnabled;
370 GrIRect fScissorRect;
371 GrIRect fViewportRect;
372 };
373
374 // defaults to false, subclass can set true to support palleted textures
375 bool f8bitPaletteSupport;
376
377 // defaults to false, subclass can set higher support level
378 NPOTTextureTypes fNPOTTextureSupport;
379
380 // True if only one stencil pass is required to implement the winding path
381 // fill rule. Subclass responsible for setting this value.
382 bool fSingleStencilPassForWinding;
383
384 // set by subclass to true if index and vertex buffers can be locked, false
385 // otherwise.
386 bool fBufferLockSupport;
387
388 // set by subclass
389 int fMinRenderTargetWidth;
390 int fMinRenderTargetHeight;
reed@google.com02a7e6c2011-01-28 21:21:49 +0000391 int fMaxTextureDimension;
reed@google.comac10a2d2010-12-22 21:39:39 +0000392
393 // overridden by API specific GrGpu-derived class to perform the draw call.
394 virtual void drawIndexedHelper(PrimitiveType type,
395 uint32_t startVertex,
396 uint32_t startIndex,
397 uint32_t vertexCount,
398 uint32_t indexCount) = 0;
399
400 virtual void drawNonIndexedHelper(PrimitiveType type,
401 uint32_t vertexCount,
402 uint32_t numVertices) = 0;
403
404 // called to program the vertex data, indexCount will be 0 if drawing non-
405 // indexed geometry.
406 virtual void setupGeometry(uint32_t startVertex,
407 uint32_t startIndex,
408 uint32_t vertexCount,
409 uint32_t indexCount) = 0;
410
411
412 // The GrGpu typically records the clients requested state and then flushes
413 // deltas from previous state at draw time. This function does the
414 // API-specific flush of the state
415 // returns false if current state is unsupported.
416 virtual bool flushGraphicsState(PrimitiveType type) = 0;
417
418 // Sets the scissor rect, or disables if rect is NULL.
419 virtual void flushScissor(const GrIRect* rect) = 0;
420
421 // GrGpu subclass removes the clip from the stencil buffer
422 virtual void eraseStencilClip() = 0;
423
424 // GrDrawTarget overrides
425 virtual bool acquireGeometryHelper(GrVertexLayout vertexLayout,
426 void** vertices,
427 void** indices);
428 virtual void releaseGeometryHelper();
429
430private:
431 mutable GrIndexBuffer* fQuadIndexBuffer; // mutable so it can be
432 // created on-demand
433
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000434 mutable GrVertexBuffer* fUnitSquareVertexBuffer; // mutable so it can be
435 // created on-demand
436
reed@google.comac10a2d2010-12-22 21:39:39 +0000437 static const int MAX_VERTEX_SIZE = GR_CT_MAX(2*sizeof(GrPoint) + sizeof(GrColor),
438 2*sizeof(GrGpuTextVertex));
439 static const int VERTEX_STORAGE = 16 * MAX_VERTEX_SIZE;
440 static const int INDEX_STORAGE = 32 * sizeof(uint16_t);
441
442protected:
443 GrAutoSMalloc<VERTEX_STORAGE> fVertices;
444 GrAutoSMalloc<INDEX_STORAGE> fIndices;
445
446 Stats fStats;
447
448private:
449 typedef GrRefCnt INHERITED;
450};
451
452#endif
453