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