GrGpu.h revision 6bc1b5fab8554a9cb643277b4867965dd4535cd6
1/* 2 * Copyright 2011 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 GrGpu_DEFINED 9#define GrGpu_DEFINED 10 11#include "GrDrawTarget.h" 12#include "GrPathRendering.h" 13#include "GrProgramDesc.h" 14#include "SkPath.h" 15 16class GrContext; 17class GrIndexBufferAllocPool; 18class GrPath; 19class GrPathRange; 20class GrPathRenderer; 21class GrPathRendererChain; 22class GrPipeline; 23class GrPrimitiveProcessor; 24class GrStencilBuffer; 25class GrVertexBufferAllocPool; 26 27class GrGpu : public SkRefCnt { 28public: 29 /** 30 * Create an instance of GrGpu that matches the specified backend. If the requested backend is 31 * not supported (at compile-time or run-time) this returns NULL. The context will not be 32 * fully constructed and should not be used by GrGpu until after this function returns. 33 */ 34 static GrGpu* Create(GrBackend, GrBackendContext, GrContext* context); 35 36 //////////////////////////////////////////////////////////////////////////// 37 38 GrGpu(GrContext* context); 39 ~GrGpu() SK_OVERRIDE; 40 41 GrContext* getContext() { return fContext; } 42 const GrContext* getContext() const { return fContext; } 43 44 /** 45 * Gets the capabilities of the draw target. 46 */ 47 const GrDrawTargetCaps* caps() const { return fCaps.get(); } 48 49 GrPathRendering* pathRendering() { return fPathRendering.get(); } 50 51 // Called by GrContext when the underlying backend context has been destroyed. 52 // GrGpu should use this to ensure that no backend API calls will be made from 53 // here onward, including in its destructor. Subclasses should call 54 // INHERITED::contextAbandoned() if they override this. 55 virtual void contextAbandoned(); 56 57 /** 58 * The GrGpu object normally assumes that no outsider is setting state 59 * within the underlying 3D API's context/device/whatever. This call informs 60 * the GrGpu that the state was modified and it shouldn't make assumptions 61 * about the state. 62 */ 63 void markContextDirty(uint32_t state = kAll_GrBackendState) { fResetBits |= state; } 64 65 /** 66 * Creates a texture object. If kRenderTarget_GrSurfaceFlag the texture can 67 * be used as a render target by calling GrTexture::asRenderTarget(). Not all 68 * pixel configs can be used as render targets. Support for configs as textures 69 * or render targets can be checked using GrDrawTargetCaps. 70 * 71 * @param desc describes the texture to be created. 72 * @param budgeted does this texture count against the resource cache budget? 73 * @param srcData texel data to load texture. Begins with full-size 74 * palette data for paletted textures. For compressed 75 * formats it contains the compressed pixel data. Otherwise, 76 * it contains width*height texels. If NULL texture data 77 * is uninitialized. 78 * @param rowBytes the number of bytes between consecutive rows. Zero 79 * means rows are tightly packed. This field is ignored 80 * for compressed formats. 81 * 82 * @return The texture object if successful, otherwise NULL. 83 */ 84 GrTexture* createTexture(const GrSurfaceDesc& desc, bool budgeted, 85 const void* srcData, size_t rowBytes); 86 87 /** 88 * Implements GrContext::wrapBackendTexture 89 */ 90 GrTexture* wrapBackendTexture(const GrBackendTextureDesc&); 91 92 /** 93 * Implements GrContext::wrapBackendTexture 94 */ 95 GrRenderTarget* wrapBackendRenderTarget(const GrBackendRenderTargetDesc&); 96 97 /** 98 * Creates a vertex buffer. 99 * 100 * @param size size in bytes of the vertex buffer 101 * @param dynamic hints whether the data will be frequently changed 102 * by either GrVertexBuffer::map() or 103 * GrVertexBuffer::updateData(). 104 * 105 * @return The vertex buffer if successful, otherwise NULL. 106 */ 107 GrVertexBuffer* createVertexBuffer(size_t size, bool dynamic); 108 109 /** 110 * Creates an index buffer. 111 * 112 * @param size size in bytes of the index buffer 113 * @param dynamic hints whether the data will be frequently changed 114 * by either GrIndexBuffer::map() or 115 * GrIndexBuffer::updateData(). 116 * 117 * @return The index buffer if successful, otherwise NULL. 118 */ 119 GrIndexBuffer* createIndexBuffer(size_t size, bool dynamic); 120 121 /** 122 * Creates an index buffer for instance drawing with a specific pattern. 123 * 124 * @param pattern the pattern to repeat 125 * @param patternSize size in bytes of the pattern 126 * @param reps number of times to repeat the pattern 127 * @param vertCount number of vertices the pattern references 128 * @param dynamic hints whether the data will be frequently changed 129 * by either GrIndexBuffer::map() or 130 * GrIndexBuffer::updateData(). 131 * 132 * @return The index buffer if successful, otherwise NULL. 133 */ 134 GrIndexBuffer* createInstancedIndexBuffer(const uint16_t* pattern, 135 int patternSize, 136 int reps, 137 int vertCount, 138 bool isDynamic = false); 139 140 /** 141 * Returns an index buffer that can be used to render quads. 142 * Six indices per quad: 0, 1, 2, 0, 2, 3, etc. 143 * The max number of quads can be queried using GrIndexBuffer::maxQuads(). 144 * Draw with kTriangles_GrPrimitiveType 145 * @ return the quad index buffer 146 */ 147 const GrIndexBuffer* getQuadIndexBuffer() const; 148 149 /** 150 * Resolves MSAA. 151 */ 152 void resolveRenderTarget(GrRenderTarget* target); 153 154 /** 155 * Gets a preferred 8888 config to use for writing/reading pixel data to/from a surface with 156 * config surfaceConfig. The returned config must have at least as many bits per channel as the 157 * readConfig or writeConfig param. 158 */ 159 virtual GrPixelConfig preferredReadPixelsConfig(GrPixelConfig readConfig, 160 GrPixelConfig surfaceConfig) const { 161 return readConfig; 162 } 163 virtual GrPixelConfig preferredWritePixelsConfig(GrPixelConfig writeConfig, 164 GrPixelConfig surfaceConfig) const { 165 return writeConfig; 166 } 167 168 /** 169 * Called before uploading writing pixels to a GrTexture when the src pixel config doesn't 170 * match the texture's config. 171 */ 172 virtual bool canWriteTexturePixels(const GrTexture*, GrPixelConfig srcConfig) const = 0; 173 174 /** 175 * OpenGL's readPixels returns the result bottom-to-top while the skia 176 * API is top-to-bottom. Thus we have to do a y-axis flip. The obvious 177 * solution is to have the subclass do the flip using either the CPU or GPU. 178 * However, the caller (GrContext) may have transformations to apply and can 179 * simply fold in the y-flip for free. On the other hand, the subclass may 180 * be able to do it for free itself. For example, the subclass may have to 181 * do memcpys to handle rowBytes that aren't tight. It could do the y-flip 182 * concurrently. 183 * 184 * This function returns true if a y-flip is required to put the pixels in 185 * top-to-bottom order and the subclass cannot do it for free. 186 * 187 * See read pixels for the params 188 * @return true if calling readPixels with the same set of params will 189 * produce bottom-to-top data 190 */ 191 virtual bool readPixelsWillPayForYFlip(GrRenderTarget* renderTarget, 192 int left, int top, 193 int width, int height, 194 GrPixelConfig config, 195 size_t rowBytes) const = 0; 196 /** 197 * This should return true if reading a NxM rectangle of pixels from a 198 * render target is faster if the target has dimensons N and M and the read 199 * rectangle has its top-left at 0,0. 200 */ 201 virtual bool fullReadPixelsIsFasterThanPartial() const { return false; }; 202 203 /** 204 * Reads a rectangle of pixels from a render target. 205 * 206 * @param renderTarget the render target to read from. NULL means the 207 * current render target. 208 * @param left left edge of the rectangle to read (inclusive) 209 * @param top top edge of the rectangle to read (inclusive) 210 * @param width width of rectangle to read in pixels. 211 * @param height height of rectangle to read in pixels. 212 * @param config the pixel config of the destination buffer 213 * @param buffer memory to read the rectangle into. 214 * @param rowBytes the number of bytes between consecutive rows. Zero 215 * means rows are tightly packed. 216 * @param invertY buffer should be populated bottom-to-top as opposed 217 * to top-to-bottom (skia's usual order) 218 * 219 * @return true if the read succeeded, false if not. The read can fail 220 * because of a unsupported pixel config or because no render 221 * target is currently set. 222 */ 223 bool readPixels(GrRenderTarget* renderTarget, 224 int left, int top, int width, int height, 225 GrPixelConfig config, void* buffer, size_t rowBytes); 226 227 /** 228 * Updates the pixels in a rectangle of a texture. 229 * 230 * @param left left edge of the rectangle to write (inclusive) 231 * @param top top edge of the rectangle to write (inclusive) 232 * @param width width of rectangle to write in pixels. 233 * @param height height of rectangle to write in pixels. 234 * @param config the pixel config of the source buffer 235 * @param buffer memory to read pixels from 236 * @param rowBytes number of bytes between consecutive rows. Zero 237 * means rows are tightly packed. 238 */ 239 bool writeTexturePixels(GrTexture* texture, 240 int left, int top, int width, int height, 241 GrPixelConfig config, const void* buffer, 242 size_t rowBytes); 243 244 /** 245 * Clear the passed in render target. Ignores the draw state and clip. Clears the whole thing if 246 * rect is NULL, otherwise just the rect. If canIgnoreRect is set then the entire render target 247 * can be optionally cleared. 248 */ 249 void clear(const SkIRect* rect, GrColor color, bool canIgnoreRect,GrRenderTarget* renderTarget); 250 251 252 void clearStencilClip(const SkIRect& rect, bool insideClip, GrRenderTarget* renderTarget); 253 254 /** 255 * Discards the contents render target. NULL indicates that the current render target should 256 * be discarded. 257 **/ 258 virtual void discard(GrRenderTarget* = NULL) = 0; 259 260 /** 261 * This is can be called before allocating a texture to be a dst for copySurface. It will 262 * populate the origin, config, and flags fields of the desc such that copySurface can 263 * efficiently succeed. It should only succeed if it can allow copySurface to perform a copy 264 * that would be more effecient than drawing the src to a dst render target. 265 */ 266 virtual bool initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) = 0; 267 268 // After the client interacts directly with the 3D context state the GrGpu 269 // must resync its internal state and assumptions about 3D context state. 270 // Each time this occurs the GrGpu bumps a timestamp. 271 // state of the 3D context 272 // At 10 resets / frame and 60fps a 64bit timestamp will overflow in about 273 // a billion years. 274 typedef uint64_t ResetTimestamp; 275 276 // This timestamp is always older than the current timestamp 277 static const ResetTimestamp kExpiredTimestamp = 0; 278 // Returns a timestamp based on the number of times the context was reset. 279 // This timestamp can be used to lazily detect when cached 3D context state 280 // is dirty. 281 ResetTimestamp getResetTimestamp() const { return fResetTimestamp; } 282 283 virtual void buildProgramDesc(GrProgramDesc*, 284 const GrPrimitiveProcessor&, 285 const GrPipeline&, 286 const GrBatchTracker&) const = 0; 287 288 // Called to determine whether a copySurface call would succeed or not. Derived 289 // classes must keep this consistent with their implementation of onCopySurface(). Fallbacks 290 // to issuing a draw from the src to dst take place at the GrDrawTarget level and this function 291 // should only return true if a faster copy path exists. The rect and point are pre-clipped. The 292 // src rect and implied dst rect are guaranteed to be within the src/dst bounds and non-empty. 293 virtual bool canCopySurface(const GrSurface* dst, 294 const GrSurface* src, 295 const SkIRect& srcRect, 296 const SkIPoint& dstPoint) = 0; 297 298 // Called to perform a surface to surface copy. Fallbacks to issuing a draw from the src to dst 299 // take place at the GrDrawTarget level and this function implement faster copy paths. The rect 300 // and point are pre-clipped. The src rect and implied dst rect are guaranteed to be within the 301 // src/dst bounds and non-empty. 302 virtual bool copySurface(GrSurface* dst, 303 GrSurface* src, 304 const SkIRect& srcRect, 305 const SkIPoint& dstPoint) = 0; 306 307 struct DrawArgs { 308 typedef GrDrawTarget::DrawInfo DrawInfo; 309 DrawArgs(const GrPrimitiveProcessor* primProc, 310 const GrPipeline* pipeline, 311 const GrProgramDesc* desc, 312 const GrBatchTracker* batchTracker) 313 : fPrimitiveProcessor(primProc) 314 , fPipeline(pipeline) 315 , fDesc(desc) 316 , fBatchTracker(batchTracker) { 317 SkASSERT(primProc && pipeline && desc && batchTracker); 318 } 319 const GrPrimitiveProcessor* fPrimitiveProcessor; 320 const GrPipeline* fPipeline; 321 const GrProgramDesc* fDesc; 322 const GrBatchTracker* fBatchTracker; 323 }; 324 325 void draw(const DrawArgs&, const GrDrawTarget::DrawInfo&); 326 327 /** None of these params are optional, pointers used just to avoid making copies. */ 328 struct StencilPathState { 329 bool fUseHWAA; 330 GrRenderTarget* fRenderTarget; 331 const SkMatrix* fViewMatrix; 332 const GrStencilSettings* fStencil; 333 const GrScissorState* fScissor; 334 }; 335 336 void stencilPath(const GrPath*, const StencilPathState&); 337 338 void drawPath(const DrawArgs&, const GrPath*, const GrStencilSettings&); 339 void drawPaths(const DrawArgs&, 340 const GrPathRange*, 341 const void* indices, 342 GrDrawTarget::PathIndexType, 343 const float transformValues[], 344 GrDrawTarget::PathTransformType, 345 int count, 346 const GrStencilSettings&); 347 348 /////////////////////////////////////////////////////////////////////////// 349 // Debugging and Stats 350 351 class Stats { 352 public: 353#if GR_GPU_STATS 354 Stats() { this->reset(); } 355 356 void reset() { 357 fRenderTargetBinds = 0; 358 fShaderCompilations = 0; 359 fTextureCreates = 0; 360 fTextureUploads = 0; 361 } 362 363 int renderTargetBinds() const { return fRenderTargetBinds; } 364 void incRenderTargetBinds() { fRenderTargetBinds++; } 365 int shaderCompilations() const { return fShaderCompilations; } 366 void incShaderCompilations() { fShaderCompilations++; } 367 int textureCreates() const { return fTextureCreates; } 368 void incTextureCreates() { fTextureCreates++; } 369 int textureUploads() const { return fTextureUploads; } 370 void incTextureUploads() { fTextureUploads++; } 371 void dump(SkString*); 372 373 private: 374 int fRenderTargetBinds; 375 int fShaderCompilations; 376 int fTextureCreates; 377 int fTextureUploads; 378#else 379 void dump(SkString*) {}; 380 void incRenderTargetBinds() {} 381 void incShaderCompilations() {} 382 void incTextureCreates() {} 383 void incTextureUploads() {} 384#endif 385 }; 386 387 Stats* stats() { return &fStats; } 388 389 /** 390 * Called at start and end of gpu trace marking 391 * GR_CREATE_GPU_TRACE_MARKER(marker_str, target) will automatically call these at the start 392 * and end of a code block respectively 393 */ 394 void addGpuTraceMarker(const GrGpuTraceMarker* marker); 395 void removeGpuTraceMarker(const GrGpuTraceMarker* marker); 396 397 /** 398 * Takes the current active set of markers and stores them for later use. Any current marker 399 * in the active set is removed from the active set and the targets remove function is called. 400 * These functions do not work as a stack so you cannot call save a second time before calling 401 * restore. Also, it is assumed that when restore is called the current active set of markers 402 * is empty. When the stored markers are added back into the active set, the targets add marker 403 * is called. 404 */ 405 void saveActiveTraceMarkers(); 406 void restoreActiveTraceMarkers(); 407 408 // Given a rt, find or create a stencil buffer and attach it 409 bool attachStencilBufferToRenderTarget(GrRenderTarget* target); 410 411protected: 412 // Functions used to map clip-respecting stencil tests into normal 413 // stencil funcs supported by GPUs. 414 static GrStencilFunc ConvertStencilFunc(bool stencilInClip, 415 GrStencilFunc func); 416 static void ConvertStencilFuncAndMask(GrStencilFunc func, 417 bool clipInStencil, 418 unsigned int clipBit, 419 unsigned int userBits, 420 unsigned int* ref, 421 unsigned int* mask); 422 423 const GrTraceMarkerSet& getActiveTraceMarkers() const { return fActiveTraceMarkers; } 424 425 Stats fStats; 426 SkAutoTDelete<GrPathRendering> fPathRendering; 427 // Subclass must initialize this in its constructor. 428 SkAutoTUnref<const GrDrawTargetCaps> fCaps; 429 430private: 431 // called when the 3D context state is unknown. Subclass should emit any 432 // assumed 3D context state and dirty any state cache. 433 virtual void onResetContext(uint32_t resetBits) = 0; 434 435 // overridden by backend-specific derived class to create objects. 436 virtual GrTexture* onCreateTexture(const GrSurfaceDesc& desc, bool budgeted, 437 const void* srcData, size_t rowBytes) = 0; 438 virtual GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc, bool budgeted, 439 const void* srcData) = 0; 440 virtual GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&) = 0; 441 virtual GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&) = 0; 442 virtual GrVertexBuffer* onCreateVertexBuffer(size_t size, bool dynamic) = 0; 443 virtual GrIndexBuffer* onCreateIndexBuffer(size_t size, bool dynamic) = 0; 444 445 // overridden by backend-specific derived class to perform the clear. 446 virtual void onClear(GrRenderTarget*, const SkIRect* rect, GrColor color, 447 bool canIgnoreRect) = 0; 448 449 450 // Overridden by backend specific classes to perform a clear of the stencil clip bits. This is 451 // ONLY used by the the clip target 452 virtual void onClearStencilClip(GrRenderTarget*, const SkIRect& rect, bool insideClip) = 0; 453 454 // overridden by backend-specific derived class to perform the draw call. 455 virtual void onDraw(const DrawArgs&, const GrDrawTarget::DrawInfo&) = 0; 456 virtual void onStencilPath(const GrPath*, const StencilPathState&) = 0; 457 458 virtual void onDrawPath(const DrawArgs&, const GrPath*, const GrStencilSettings&) = 0; 459 virtual void onDrawPaths(const DrawArgs&, 460 const GrPathRange*, 461 const void* indices, 462 GrDrawTarget::PathIndexType, 463 const float transformValues[], 464 GrDrawTarget::PathTransformType, 465 int count, 466 const GrStencilSettings&) = 0; 467 468 // overridden by backend-specific derived class to perform the read pixels. 469 virtual bool onReadPixels(GrRenderTarget* target, 470 int left, int top, int width, int height, 471 GrPixelConfig, 472 void* buffer, 473 size_t rowBytes) = 0; 474 475 // overridden by backend-specific derived class to perform the texture update 476 virtual bool onWriteTexturePixels(GrTexture* texture, 477 int left, int top, int width, int height, 478 GrPixelConfig config, const void* buffer, 479 size_t rowBytes) = 0; 480 481 // overridden by backend-specific derived class to perform the resolve 482 virtual void onResolveRenderTarget(GrRenderTarget* target) = 0; 483 484 // width and height may be larger than rt (if underlying API allows it). 485 // Should attach the SB to the RT. Returns false if compatible sb could 486 // not be created. 487 virtual bool createStencilBufferForRenderTarget(GrRenderTarget*, int width, int height) = 0; 488 489 // attaches an existing SB to an existing RT. 490 virtual bool attachStencilBufferToRenderTarget(GrStencilBuffer*, GrRenderTarget*) = 0; 491 492 // clears target's entire stencil buffer to 0 493 virtual void clearStencil(GrRenderTarget* target) = 0; 494 495 virtual void didAddGpuTraceMarker() = 0; 496 virtual void didRemoveGpuTraceMarker() = 0; 497 498 void resetContext() { 499 this->onResetContext(fResetBits); 500 fResetBits = 0; 501 ++fResetTimestamp; 502 } 503 504 void handleDirtyContext() { 505 if (fResetBits) { 506 this->resetContext(); 507 } 508 } 509 510 ResetTimestamp fResetTimestamp; 511 uint32_t fResetBits; 512 // these are mutable so they can be created on-demand 513 mutable GrIndexBuffer* fQuadIndexBuffer; 514 // To keep track that we always have at least as many debug marker adds as removes 515 int fGpuTraceMarkerCount; 516 GrTraceMarkerSet fActiveTraceMarkers; 517 GrTraceMarkerSet fStoredTraceMarkers; 518 // The context owns us, not vice-versa, so this ptr is not ref'ed by Gpu. 519 GrContext* fContext; 520 521 typedef SkRefCnt INHERITED; 522}; 523 524#endif 525