blob: f1fdf01d508de61c3873c418cd9ba56a467fb2fe [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 /**
200 * Creates a vertex buffer.
201 *
202 * @param size size in bytes of the vertex buffer
203 * @param dynamic hints whether the data will be frequently changed
204 * by either GrVertexBuffer::lock or
205 * GrVertexBuffer::updateData.
206 *
207 * @return The vertex buffer if successful, otherwise NULL.
208 */
209 virtual GrVertexBuffer* createVertexBuffer(uint32_t size, bool dynamic) = 0;
210
211 /**
212 * Creates an index buffer.
213 *
214 * @param size size in bytes of the index buffer
215 * @param dynamic hints whether the data will be frequently changed
216 * by either GrIndexBuffer::lock or
217 * GrIndexBuffer::updateData.
218 *
219 * @return The index buffer if successful, otherwise NULL.
220 */
221 virtual GrIndexBuffer* createIndexBuffer(uint32_t size, bool dynamic) = 0;
222
223 /**
224 * Gets the default render target. This is the render target set in the
225 * 3D API at the time the GrGpu was created.
226 */
227 virtual GrRenderTarget* defaultRenderTarget() = 0;
228
229 /**
230 * At construction time the GrGpu infers the render target and viewport from
231 * the state of the underlying 3D API. However, a platform-specific resize
232 * event may occur.
233 * @param width new width of the default rendertarget
234 * @param height new height of the default rendertarget
235 */
236 virtual void setDefaultRenderTargetSize(uint32_t width, uint32_t height) = 0;
237
238 /**
239 * Erase the entire render target, ignoring any clips/scissors.
240 *
241 * This is issued to the GPU driver immediately.
242 */
243 virtual void eraseColor(GrColor color) = 0;
244
245 /**
246 * Are 8 bit paletted textures supported.
247 *
248 * @return true if 8bit palette textures are supported, false otherwise
249 */
250 bool supports8BitPalette() const { return f8bitPaletteSupport; }
251
252 /**
253 * If single stencil pass winding is supported then one stencil pass
254 * (kWindingStencil1_PathPass) is required to do winding rule path filling
255 * (or inverse winding rule). Otherwise, two passes are required
256 * (kWindingStencil1_PathPass followed by kWindingStencil2_PathPass).
257 *
258 * @return true if only a single stencil pass is needed.
259 */
260 bool supportsSingleStencilPassWinding() const
261 { return fSingleStencilPassForWinding; }
262
263 /**
264 * Checks whether locking vertex and index buffers is supported.
265 *
266 * @return true if locking is supported.
267 */
268 bool supportsBufferLocking() const { return fBufferLockSupport; }
269
270 /**
271 * Gets the minimum width of a render target. If a texture/rt is created
272 * with a width less than this size the GrGpu object will clamp it to this
273 * value.
274 */
275 int minRenderTargetWidth() const { return fMinRenderTargetWidth; }
276
277 /**
278 * Gets the minimum width of a render target. If a texture/rt is created
279 * with a height less than this size the GrGpu object will clamp it to this
280 * value.
281 */
282 int minRenderTargetHeight() const { return fMinRenderTargetHeight; }
283
284 /**
285 * Retrieves the level of NPOT texture support. Regardless of support level
286 * NPOT textures can always be created, but internally they may be imbedded
287 * in a POT texture. An exception is paletted textures which must be
288 * specified as a POT when npotTextureSupport() is not Full.
289 *
290 * @return the level of NPOT texture support.
291 */
292 NPOTTextureTypes npotTextureSupport() const { return fNPOTTextureSupport; }
293
294 // GrDrawTarget overrides
295 virtual void drawIndexed(PrimitiveType type,
296 uint32_t startVertex,
297 uint32_t startIndex,
298 uint32_t vertexCount,
299 uint32_t indexCount);
300
301 virtual void drawNonIndexed(PrimitiveType type,
302 uint32_t startVertex,
303 uint32_t vertexCount);
304
305 /**
306 * Determines if blend is effectively disabled.
307 *
308 * @return true if blend can be disabled without changing the rendering
309 * result given the current state including the vertex layout specified
310 * with the vertex source.
311 */
312 bool canDisableBlend() const;
313
314 /**
315 * Returns an index buffer that can be used to render quads.
316 * Indices are 0, 1, 2, 0, 2, 3, etc.
317 * Draw with kTriangles_PrimitiveType
318 */
319 const GrIndexBuffer* quadIndexBuffer() const;
320 /**
321 * Gets the number of quads that can be rendered using quadIndexBuffer.
322 */
323 int maxQuadsInIndexBuffer() const;
324
325 /**
326 * Ensures that the current render target is actually set in the
327 * underlying 3D API. Used when client wants to use 3D API to directly
328 * render to the RT.
329 */
330 virtual void forceRenderTargetFlush() = 0;
331
332 virtual bool readPixels(int left, int top, int width, int height,
333 GrTexture::PixelConfig, void* buffer) = 0;
334
335
336 const Stats& getStats() const;
337 void resetStats();
338 void printStats() const;
339
340protected:
341 /**
342 * Extensions to GrDrawTarget::StencilPass to implement stencil clipping
343 */
344 enum GpuStencilPass {
345 kSetClip_StencilPass = kDrawTargetCount_StencilPass,
346 /* rendering a hard clip to the stencil
347 buffer. Subsequent draws with other
348 StencilPass values will be clipped
349 if kStencilClip_StateBit is set. */
350 kGpuCount_StencilPass
351 };
352
353 /**
354 * Extensions to GrDrawTarget::StateBits to implement stencil clipping
355 */
356 struct ClipState {
357 bool fClipInStencil;
358 bool fClipIsDirty;
359 GrRenderTarget* fStencilClipTarget;
360 } fClipState;
361
362 virtual void clipWillChange(const GrClip& clip);
363 bool setupClipAndFlushState(PrimitiveType type);
364
365 struct BoundsState {
366 bool fScissorEnabled;
367 GrIRect fScissorRect;
368 GrIRect fViewportRect;
369 };
370
371 // defaults to false, subclass can set true to support palleted textures
372 bool f8bitPaletteSupport;
373
374 // defaults to false, subclass can set higher support level
375 NPOTTextureTypes fNPOTTextureSupport;
376
377 // True if only one stencil pass is required to implement the winding path
378 // fill rule. Subclass responsible for setting this value.
379 bool fSingleStencilPassForWinding;
380
381 // set by subclass to true if index and vertex buffers can be locked, false
382 // otherwise.
383 bool fBufferLockSupport;
384
385 // set by subclass
386 int fMinRenderTargetWidth;
387 int fMinRenderTargetHeight;
388
389 // overridden by API specific GrGpu-derived class to perform the draw call.
390 virtual void drawIndexedHelper(PrimitiveType type,
391 uint32_t startVertex,
392 uint32_t startIndex,
393 uint32_t vertexCount,
394 uint32_t indexCount) = 0;
395
396 virtual void drawNonIndexedHelper(PrimitiveType type,
397 uint32_t vertexCount,
398 uint32_t numVertices) = 0;
399
400 // called to program the vertex data, indexCount will be 0 if drawing non-
401 // indexed geometry.
402 virtual void setupGeometry(uint32_t startVertex,
403 uint32_t startIndex,
404 uint32_t vertexCount,
405 uint32_t indexCount) = 0;
406
407
408 // The GrGpu typically records the clients requested state and then flushes
409 // deltas from previous state at draw time. This function does the
410 // API-specific flush of the state
411 // returns false if current state is unsupported.
412 virtual bool flushGraphicsState(PrimitiveType type) = 0;
413
414 // Sets the scissor rect, or disables if rect is NULL.
415 virtual void flushScissor(const GrIRect* rect) = 0;
416
417 // GrGpu subclass removes the clip from the stencil buffer
418 virtual void eraseStencilClip() = 0;
419
420 // GrDrawTarget overrides
421 virtual bool acquireGeometryHelper(GrVertexLayout vertexLayout,
422 void** vertices,
423 void** indices);
424 virtual void releaseGeometryHelper();
425
426private:
427 mutable GrIndexBuffer* fQuadIndexBuffer; // mutable so it can be
428 // created on-demand
429
430 static const int MAX_VERTEX_SIZE = GR_CT_MAX(2*sizeof(GrPoint) + sizeof(GrColor),
431 2*sizeof(GrGpuTextVertex));
432 static const int VERTEX_STORAGE = 16 * MAX_VERTEX_SIZE;
433 static const int INDEX_STORAGE = 32 * sizeof(uint16_t);
434
435protected:
436 GrAutoSMalloc<VERTEX_STORAGE> fVertices;
437 GrAutoSMalloc<INDEX_STORAGE> fIndices;
438
439 Stats fStats;
440
441private:
442 typedef GrRefCnt INHERITED;
443};
444
445#endif
446