blob: 62f68cafd6c343a0bb28dee193b6c4d453e35c42 [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 /**
324 * Ensures that the current render target is actually set in the
325 * underlying 3D API. Used when client wants to use 3D API to directly
326 * render to the RT.
327 */
328 virtual void forceRenderTargetFlush() = 0;
329
330 virtual bool readPixels(int left, int top, int width, int height,
331 GrTexture::PixelConfig, void* buffer) = 0;
332
333
334 const Stats& getStats() const;
335 void resetStats();
336 void printStats() const;
337
338protected:
339 /**
340 * Extensions to GrDrawTarget::StencilPass to implement stencil clipping
341 */
342 enum GpuStencilPass {
343 kSetClip_StencilPass = kDrawTargetCount_StencilPass,
344 /* rendering a hard clip to the stencil
345 buffer. Subsequent draws with other
346 StencilPass values will be clipped
347 if kStencilClip_StateBit is set. */
348 kGpuCount_StencilPass
349 };
350
351 /**
352 * Extensions to GrDrawTarget::StateBits to implement stencil clipping
353 */
354 struct ClipState {
355 bool fClipInStencil;
356 bool fClipIsDirty;
357 GrRenderTarget* fStencilClipTarget;
358 } fClipState;
359
360 virtual void clipWillChange(const GrClip& clip);
361 bool setupClipAndFlushState(PrimitiveType type);
362
363 struct BoundsState {
364 bool fScissorEnabled;
365 GrIRect fScissorRect;
366 GrIRect fViewportRect;
367 };
368
369 // defaults to false, subclass can set true to support palleted textures
370 bool f8bitPaletteSupport;
371
372 // defaults to false, subclass can set higher support level
373 NPOTTextureTypes fNPOTTextureSupport;
374
375 // True if only one stencil pass is required to implement the winding path
376 // fill rule. Subclass responsible for setting this value.
377 bool fSingleStencilPassForWinding;
378
379 // set by subclass to true if index and vertex buffers can be locked, false
380 // otherwise.
381 bool fBufferLockSupport;
382
383 // set by subclass
384 int fMinRenderTargetWidth;
385 int fMinRenderTargetHeight;
reed@google.com02a7e6c2011-01-28 21:21:49 +0000386 int fMaxTextureDimension;
reed@google.comac10a2d2010-12-22 21:39:39 +0000387
388 // overridden by API specific GrGpu-derived class to perform the draw call.
389 virtual void drawIndexedHelper(PrimitiveType type,
390 uint32_t startVertex,
391 uint32_t startIndex,
392 uint32_t vertexCount,
393 uint32_t indexCount) = 0;
394
395 virtual void drawNonIndexedHelper(PrimitiveType type,
396 uint32_t vertexCount,
397 uint32_t numVertices) = 0;
398
399 // called to program the vertex data, indexCount will be 0 if drawing non-
400 // indexed geometry.
401 virtual void setupGeometry(uint32_t startVertex,
402 uint32_t startIndex,
403 uint32_t vertexCount,
404 uint32_t indexCount) = 0;
405
406
407 // The GrGpu typically records the clients requested state and then flushes
408 // deltas from previous state at draw time. This function does the
409 // API-specific flush of the state
410 // returns false if current state is unsupported.
411 virtual bool flushGraphicsState(PrimitiveType type) = 0;
412
413 // Sets the scissor rect, or disables if rect is NULL.
414 virtual void flushScissor(const GrIRect* rect) = 0;
415
416 // GrGpu subclass removes the clip from the stencil buffer
417 virtual void eraseStencilClip() = 0;
418
419 // GrDrawTarget overrides
420 virtual bool acquireGeometryHelper(GrVertexLayout vertexLayout,
421 void** vertices,
422 void** indices);
423 virtual void releaseGeometryHelper();
424
425private:
426 mutable GrIndexBuffer* fQuadIndexBuffer; // mutable so it can be
427 // created on-demand
428
429 static const int MAX_VERTEX_SIZE = GR_CT_MAX(2*sizeof(GrPoint) + sizeof(GrColor),
430 2*sizeof(GrGpuTextVertex));
431 static const int VERTEX_STORAGE = 16 * MAX_VERTEX_SIZE;
432 static const int INDEX_STORAGE = 32 * sizeof(uint16_t);
433
434protected:
435 GrAutoSMalloc<VERTEX_STORAGE> fVertices;
436 GrAutoSMalloc<INDEX_STORAGE> fIndices;
437
438 Stats fStats;
439
440private:
441 typedef GrRefCnt INHERITED;
442};
443
444#endif
445