1 /* 2 * Copyright 2015 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 GrRenderTargetContext_DEFINED 9 #define GrRenderTargetContext_DEFINED 10 11 #include "../private/GrRenderTargetProxy.h" 12 #include "GrPaint.h" 13 #include "GrSurfaceContext.h" 14 #include "GrTypesPriv.h" 15 #include "GrXferProcessor.h" 16 #include "SkCanvas.h" 17 #include "SkDrawable.h" 18 #include "SkRefCnt.h" 19 #include "SkSurface.h" 20 #include "SkSurfaceProps.h" 21 #include "text/GrTextTarget.h" 22 23 class GrBackendSemaphore; 24 class GrClip; 25 class GrColorSpaceXform; 26 class GrCoverageCountingPathRenderer; 27 class GrDrawingManager; 28 class GrDrawOp; 29 class GrFixedClip; 30 class GrOp; 31 class GrRenderTarget; 32 class GrRenderTargetContextPriv; 33 class GrRenderTargetOpList; 34 class GrShape; 35 class GrStyle; 36 class GrTextureProxy; 37 struct GrUserStencilSettings; 38 struct SkDrawShadowRec; 39 class SkGlyphRunList; 40 struct SkIPoint; 41 struct SkIRect; 42 class SkLatticeIter; 43 class SkMatrix; 44 class SkPaint; 45 class SkPath; 46 struct SkPoint; 47 struct SkRect; 48 class SkRegion; 49 class SkRRect; 50 struct SkRSXform; 51 class SkTextBlob; 52 class SkVertices; 53 54 /** 55 * A helper object to orchestrate commands (draws, etc...) for GrSurfaces that are GrRenderTargets. 56 */ 57 class SK_API GrRenderTargetContext : public GrSurfaceContext { 58 public: 59 ~GrRenderTargetContext() override; 60 61 virtual void drawGlyphRunList(const GrClip&, const SkMatrix& viewMatrix, const SkGlyphRunList&); 62 63 /** 64 * Provides a perfomance hint that the render target's contents are allowed 65 * to become undefined. 66 */ 67 void discard(); 68 69 enum class CanClearFullscreen : bool { 70 kNo = false, 71 kYes = true 72 }; 73 74 /** 75 * Clear the entire or rect of the render target, ignoring any clips. 76 * @param rect the rect to clear or the whole thing if rect is NULL. 77 * @param color the color to clear to. 78 * @param CanClearFullscreen allows partial clears to be converted to fullscreen clears on 79 * tiling platforms where that is an optimization. 80 */ 81 void clear(const SkIRect* rect, const SkPMColor4f& color, CanClearFullscreen); 82 83 /** 84 * Draw everywhere (respecting the clip) with the paint. 85 */ 86 void drawPaint(const GrClip&, GrPaint&&, const SkMatrix& viewMatrix); 87 88 /** 89 * Draw the rect using a paint. 90 * @param paint describes how to color pixels. 91 * @param GrAA Controls whether rect is antialiased 92 * @param viewMatrix transformation matrix 93 * @param style The style to apply. Null means fill. Currently path effects are not 94 * allowed. 95 * The rects coords are used to access the paint (through texture matrix) 96 */ 97 void drawRect(const GrClip&, 98 GrPaint&& paint, 99 GrAA, 100 const SkMatrix& viewMatrix, 101 const SkRect&, 102 const GrStyle* style = nullptr); 103 104 /** 105 * Maps a rectangle of shader coordinates to a rectangle and fills that rectangle. 106 * 107 * @param paint describes how to color pixels. 108 * @param GrAA Controls whether rect is antialiased 109 * @param viewMatrix transformation matrix which applies to rectToDraw 110 * @param rectToDraw the rectangle to draw 111 * @param localRect the rectangle of shader coordinates applied to rectToDraw 112 */ fillRectToRect(const GrClip & clip,GrPaint && paint,GrAA aa,const SkMatrix & viewMatrix,const SkRect & rectToDraw,const SkRect & localRect)113 void fillRectToRect(const GrClip& clip, 114 GrPaint&& paint, 115 GrAA aa, 116 const SkMatrix& viewMatrix, 117 const SkRect& rectToDraw, 118 const SkRect& localRect) { 119 this->fillRectWithEdgeAA(clip, std::move(paint), aa, 120 aa == GrAA::kYes ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone, 121 viewMatrix, rectToDraw, &localRect); 122 } 123 124 /** 125 * Fills a rect with a paint and a localMatrix. 126 */ 127 void fillRectWithLocalMatrix(const GrClip& clip, 128 GrPaint&& paint, 129 GrAA, 130 const SkMatrix& viewMatrix, 131 const SkRect& rect, 132 const SkMatrix& localMatrix); 133 134 /** 135 * Creates an op that draws a fill rect with per-edge control over anti-aliasing. 136 * 137 * This is a specialized version of fillQuadWithEdgeAA, but is kept separate since knowing 138 * the geometry is a rectangle affords more optimizations. 139 */ 140 void fillRectWithEdgeAA(const GrClip& clip, GrPaint&& paint, GrAA aa, GrQuadAAFlags edgeAA, 141 const SkMatrix& viewMatrix, const SkRect& rect, 142 const SkRect* optionalLocalRect = nullptr); 143 144 /** 145 * Similar to fillRectWithEdgeAA but draws an arbitrary 2D convex quadrilateral transformed 146 * by 'viewMatrix', with per-edge control over anti-aliasing. The quad should follow the 147 * ordering used by SkRect::toQuad(), which determines how the edge AA is applied: 148 * - "top" = points [0] and [1] 149 * - "right" = points[1] and [2] 150 * - "bottom" = points[2] and [3] 151 * - "left" = points[3] and [0] 152 * 153 * The last argument, 'optionalLocalQuad', can be null if no separate local coordinates are 154 * necessary. 155 */ 156 void fillQuadWithEdgeAA(const GrClip& clip, GrPaint&& paint, GrAA aa, GrQuadAAFlags edgeAA, 157 const SkMatrix& viewMatrix, const SkPoint quad[4], 158 const SkPoint optionalLocalQuad[4]); 159 160 /** Used with drawQuadSet */ 161 struct QuadSetEntry { 162 SkRect fRect; 163 SkPMColor4f fColor; // Overrides any color on the GrPaint 164 SkMatrix fLocalMatrix; 165 GrQuadAAFlags fAAFlags; 166 }; 167 168 // TODO(michaelludwig) - remove if the bulk API is not useful for SkiaRenderer 169 void drawQuadSet(const GrClip& clip, GrPaint&& paint, GrAA aa, const SkMatrix& viewMatrix, 170 const QuadSetEntry[], int cnt); 171 172 /** 173 * Creates an op that draws a subrectangle of a texture. The passed color is modulated by the 174 * texture's color. 'srcRect' specifies the rectangle of the texture to draw. 'dstRect' 175 * specifies the rectangle to draw in local coords which will be transformed by 'viewMatrix' to 176 * device space. 177 */ 178 void drawTexture(const GrClip& clip, sk_sp<GrTextureProxy>, GrSamplerState::Filter, 179 SkBlendMode mode, const SkPMColor4f&, const SkRect& srcRect, 180 const SkRect& dstRect, GrAA, GrQuadAAFlags, SkCanvas::SrcRectConstraint, 181 const SkMatrix& viewMatrix, sk_sp<GrColorSpaceXform> texXform); 182 183 /** 184 * Variant of drawTexture that instead draws the texture applied to 'dstQuad' transformed by 185 * 'viewMatrix', using the 'srcQuad' texture coordinates clamped to the optional 'domain'. If 186 * 'domain' is null, it's equivalent to using the fast src rect constraint. If 'domain' is 187 * provided, the strict src rect constraint is applied using 'domain'. 188 */ 189 void drawTextureQuad(const GrClip& clip, sk_sp<GrTextureProxy>, GrSamplerState::Filter, 190 SkBlendMode mode, const SkPMColor4f&, const SkPoint srcQuad[4], 191 const SkPoint dstQuad[4], GrAA, GrQuadAAFlags, const SkRect* domain, 192 const SkMatrix& viewMatrix, sk_sp<GrColorSpaceXform> texXform); 193 194 /** Used with drawTextureSet */ 195 struct TextureSetEntry { 196 sk_sp<GrTextureProxy> fProxy; 197 SkRect fSrcRect; 198 SkRect fDstRect; 199 const SkPoint* fDstClipQuad; // Must be null, or point to an array of 4 points 200 const SkMatrix* fPreViewMatrix; // If not null, entry's CTM is 'viewMatrix' * fPreViewMatrix 201 float fAlpha; 202 GrQuadAAFlags fAAFlags; 203 }; 204 /** 205 * Draws a set of textures with a shared filter, color, view matrix, color xform, and 206 * texture color xform. The textures must all have the same GrTextureType and GrConfig. 207 * 208 * If any entries provide a non-null fDstClip array, it will be read from immediately based on 209 * fDstClipCount, so the pointer can become invalid after this returns. 210 */ 211 void drawTextureSet(const GrClip&, const TextureSetEntry[], int cnt, GrSamplerState::Filter, 212 SkBlendMode mode, GrAA aa, const SkMatrix& viewMatrix, 213 sk_sp<GrColorSpaceXform> texXform); 214 215 /** 216 * Draw a roundrect using a paint. 217 * 218 * @param paint describes how to color pixels. 219 * @param GrAA Controls whether rrect is antialiased. 220 * @param viewMatrix transformation matrix 221 * @param rrect the roundrect to draw 222 * @param style style to apply to the rrect. Currently path effects are not allowed. 223 */ 224 void drawRRect(const GrClip&, 225 GrPaint&&, 226 GrAA, 227 const SkMatrix& viewMatrix, 228 const SkRRect& rrect, 229 const GrStyle& style); 230 231 /** 232 * Use a fast method to render the ambient and spot shadows for a path. 233 * Will return false if not possible for the given path. 234 * 235 * @param viewMatrix transformation matrix 236 * @param path the path to shadow 237 * @param rec parameters for shadow rendering 238 */ 239 bool drawFastShadow(const GrClip&, 240 const SkMatrix& viewMatrix, 241 const SkPath& path, 242 const SkDrawShadowRec& rec); 243 244 /** 245 * Shortcut for filling a SkPath consisting of nested rrects using a paint. The result is 246 * undefined if outer does not contain inner. 247 * 248 * @param paint describes how to color pixels. 249 * @param GrAA Controls whether rrects edges are antialiased 250 * @param viewMatrix transformation matrix 251 * @param outer the outer roundrect 252 * @param inner the inner roundrect 253 */ 254 void drawDRRect(const GrClip&, 255 GrPaint&&, 256 GrAA, 257 const SkMatrix& viewMatrix, 258 const SkRRect& outer, 259 const SkRRect& inner); 260 261 /** 262 * Draws a path. 263 * 264 * @param paint describes how to color pixels. 265 * @param GrAA Controls whether the path is antialiased. 266 * @param viewMatrix transformation matrix 267 * @param path the path to draw 268 * @param style style to apply to the path. 269 */ 270 void drawPath(const GrClip&, 271 GrPaint&&, 272 GrAA, 273 const SkMatrix& viewMatrix, 274 const SkPath&, 275 const GrStyle&); 276 277 /** 278 * Draws a shape. 279 * 280 * @param paint describes how to color pixels. 281 * @param GrAA Controls whether the path is antialiased. 282 * @param viewMatrix transformation matrix 283 * @param shape the shape to draw 284 */ 285 void drawShape(const GrClip&, 286 GrPaint&&, 287 GrAA, 288 const SkMatrix& viewMatrix, 289 const GrShape&); 290 291 292 /** 293 * Draws vertices with a paint. 294 * 295 * @param paint describes how to color pixels. 296 * @param viewMatrix transformation matrix 297 * @param vertices specifies the mesh to draw. 298 * @param bones bone deformation matrices. 299 * @param boneCount number of bone matrices. 300 * @param overridePrimType primitive type to draw. If NULL, derive prim type from vertices. 301 */ 302 void drawVertices(const GrClip&, 303 GrPaint&& paint, 304 const SkMatrix& viewMatrix, 305 sk_sp<SkVertices> vertices, 306 const SkVertices::Bone bones[], 307 int boneCount, 308 GrPrimitiveType* overridePrimType = nullptr); 309 310 /** 311 * Draws textured sprites from an atlas with a paint. This currently does not support AA for the 312 * sprite rectangle edges. 313 * 314 * @param paint describes how to color pixels. 315 * @param viewMatrix transformation matrix 316 * @param spriteCount number of sprites. 317 * @param xform array of compressed transformation data, required. 318 * @param texRect array of texture rectangles used to access the paint. 319 * @param colors optional array of per-sprite colors, supercedes 320 * the paint's color field. 321 */ 322 void drawAtlas(const GrClip&, 323 GrPaint&& paint, 324 const SkMatrix& viewMatrix, 325 int spriteCount, 326 const SkRSXform xform[], 327 const SkRect texRect[], 328 const SkColor colors[]); 329 330 /** 331 * Draws a region. 332 * 333 * @param paint describes how to color pixels 334 * @param viewMatrix transformation matrix 335 * @param aa should the rects of the region be antialiased. 336 * @param region the region to be drawn 337 * @param style style to apply to the region 338 */ 339 void drawRegion(const GrClip&, 340 GrPaint&& paint, 341 GrAA aa, 342 const SkMatrix& viewMatrix, 343 const SkRegion& region, 344 const GrStyle& style, 345 const GrUserStencilSettings* ss = nullptr); 346 347 /** 348 * Draws an oval. 349 * 350 * @param paint describes how to color pixels. 351 * @param GrAA Controls whether the oval is antialiased. 352 * @param viewMatrix transformation matrix 353 * @param oval the bounding rect of the oval. 354 * @param style style to apply to the oval. Currently path effects are not allowed. 355 */ 356 void drawOval(const GrClip&, 357 GrPaint&& paint, 358 GrAA, 359 const SkMatrix& viewMatrix, 360 const SkRect& oval, 361 const GrStyle& style); 362 /** 363 * Draws a partial arc of an oval. 364 * 365 * @param paint describes how to color pixels. 366 * @param GrGrAA Controls whether the arc is antialiased. 367 * @param viewMatrix transformation matrix. 368 * @param oval the bounding rect of the oval. 369 * @param startAngle starting angle in degrees. 370 * @param sweepAngle angle to sweep in degrees. Must be in (-360, 360) 371 * @param useCenter true means that the implied path begins at the oval center, connects as 372 * a line to the point indicated by the start contains the arc indicated by 373 * the sweep angle. If false the line beginning at the center point is 374 * omitted. 375 * @param style style to apply to the oval. 376 */ 377 void drawArc(const GrClip&, 378 GrPaint&& paint, 379 GrAA, 380 const SkMatrix& viewMatrix, 381 const SkRect& oval, 382 SkScalar startAngle, 383 SkScalar sweepAngle, 384 bool useCenter, 385 const GrStyle& style); 386 387 /** 388 * Draw the image as a set of rects, specified by |iter|. 389 */ 390 void drawImageLattice(const GrClip&, 391 GrPaint&&, 392 const SkMatrix& viewMatrix, 393 sk_sp<GrTextureProxy>, 394 sk_sp<GrColorSpaceXform>, 395 GrSamplerState::Filter, 396 std::unique_ptr<SkLatticeIter>, 397 const SkRect& dst); 398 399 /** 400 * Adds the necessary signal and wait semaphores and adds the passed in SkDrawable to the 401 * command stream. 402 */ 403 void drawDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler>, const SkRect& bounds); 404 405 /** 406 * After this returns any pending surface IO will be issued to the backend 3D API and 407 * if the surface has MSAA it will be resolved. 408 */ 409 GrSemaphoresSubmitted prepareForExternalIO(SkSurface::BackendSurfaceAccess access, 410 GrFlushFlags flags, int numSemaphores, 411 GrBackendSemaphore backendSemaphores[], 412 GrGpuFinishedProc finishedProc, 413 GrGpuFinishedContext finishedContext); 414 415 /** 416 * The next time this GrRenderTargetContext is flushed, the gpu will wait on the passed in 417 * semaphores before executing any commands. 418 */ 419 bool waitOnSemaphores(int numSemaphores, const GrBackendSemaphore waitSemaphores[]); 420 421 void insertEventMarker(const SkString&); 422 fsaaType()423 GrFSAAType fsaaType() const { return fRenderTargetProxy->fsaaType(); } 424 const GrCaps* caps() const; width()425 int width() const { return fRenderTargetProxy->width(); } height()426 int height() const { return fRenderTargetProxy->height(); } numColorSamples()427 int numColorSamples() const { return fRenderTargetProxy->numColorSamples(); } numStencilSamples()428 int numStencilSamples() const { return fRenderTargetProxy->numStencilSamples(); } surfaceProps()429 const SkSurfaceProps& surfaceProps() const { return fSurfaceProps; } origin()430 GrSurfaceOrigin origin() const { return fRenderTargetProxy->origin(); } wrapsVkSecondaryCB()431 bool wrapsVkSecondaryCB() const { return fRenderTargetProxy->wrapsVkSecondaryCB(); } 432 GrMipMapped mipMapped() const; 433 setNeedsStencil()434 void setNeedsStencil() { fRenderTargetProxy->setNeedsStencil(); } 435 436 // This entry point should only be called if the backing GPU object is known to be 437 // instantiated. accessRenderTarget()438 GrRenderTarget* accessRenderTarget() { return fRenderTargetProxy->peekRenderTarget(); } 439 asSurfaceProxy()440 GrSurfaceProxy* asSurfaceProxy() override { return fRenderTargetProxy.get(); } asSurfaceProxy()441 const GrSurfaceProxy* asSurfaceProxy() const override { return fRenderTargetProxy.get(); } asSurfaceProxyRef()442 sk_sp<GrSurfaceProxy> asSurfaceProxyRef() override { return fRenderTargetProxy; } 443 444 GrTextureProxy* asTextureProxy() override; 445 const GrTextureProxy* asTextureProxy() const override; 446 sk_sp<GrTextureProxy> asTextureProxyRef() override; 447 asRenderTargetProxy()448 GrRenderTargetProxy* asRenderTargetProxy() override { return fRenderTargetProxy.get(); } asRenderTargetProxyRef()449 sk_sp<GrRenderTargetProxy> asRenderTargetProxyRef() override { return fRenderTargetProxy; } 450 asRenderTargetContext()451 GrRenderTargetContext* asRenderTargetContext() override { return this; } 452 453 // Provides access to functions that aren't part of the public API. 454 GrRenderTargetContextPriv priv(); 455 const GrRenderTargetContextPriv priv() const; 456 textTarget()457 GrTextTarget* textTarget() { return fTextTarget.get(); } 458 459 bool isWrapped_ForTesting() const; 460 461 protected: 462 GrRenderTargetContext(GrRecordingContext*, sk_sp<GrRenderTargetProxy>, 463 sk_sp<SkColorSpace>, const SkSurfaceProps*, 464 bool managedOpList = true); 465 466 SkDEBUGCODE(void validate() const override;) 467 468 private: 469 class TextTarget; 470 chooseAAType(GrAA aa,GrAllowMixedSamples allowMixedSamples)471 inline GrAAType chooseAAType(GrAA aa, GrAllowMixedSamples allowMixedSamples) { 472 return GrChooseAAType(aa, this->fsaaType(), allowMixedSamples, *this->caps()); 473 } 474 475 friend class GrAtlasTextBlob; // for access to add[Mesh]DrawOp 476 friend class GrClipStackClip; // for access to getOpList 477 478 friend class GrDrawingManager; // for ctor 479 friend class GrRenderTargetContextPriv; 480 481 // All the path renderers currently make their own ops 482 friend class GrSoftwarePathRenderer; // for access to add[Mesh]DrawOp 483 friend class GrAAConvexPathRenderer; // for access to add[Mesh]DrawOp 484 friend class GrDashLinePathRenderer; // for access to add[Mesh]DrawOp 485 friend class GrAAHairLinePathRenderer; // for access to add[Mesh]DrawOp 486 friend class GrAALinearizingConvexPathRenderer; // for access to add[Mesh]DrawOp 487 friend class GrSmallPathRenderer; // for access to add[Mesh]DrawOp 488 friend class GrDefaultPathRenderer; // for access to add[Mesh]DrawOp 489 friend class GrStencilAndCoverPathRenderer; // for access to add[Mesh]DrawOp 490 friend class GrTessellatingPathRenderer; // for access to add[Mesh]DrawOp 491 friend class GrCCPerFlushResources; // for access to addDrawOp 492 friend class GrCoverageCountingPathRenderer; // for access to addDrawOp 493 // for a unit test 494 friend void test_draw_op(GrContext*, 495 GrRenderTargetContext*, 496 std::unique_ptr<GrFragmentProcessor>, 497 sk_sp<GrTextureProxy>); 498 499 void internalClear(const GrFixedClip&, const SkPMColor4f&, CanClearFullscreen); 500 void internalStencilClear(const GrFixedClip&, bool insideStencilMask); 501 502 // Only consumes the GrPaint if successful. 503 bool drawFilledDRRect(const GrClip& clip, 504 GrPaint&& paint, 505 GrAA, 506 const SkMatrix& viewMatrix, 507 const SkRRect& origOuter, 508 const SkRRect& origInner); 509 510 void drawFilledRect(const GrClip& clip, 511 GrPaint&& paint, 512 GrAA, 513 const SkMatrix& viewMatrix, 514 const SkRect& rect, 515 const GrUserStencilSettings* ss = nullptr); 516 517 // Only consumes the GrPaint if successful. 518 bool drawFilledRectAsClear(const GrClip& clip, 519 GrPaint&& paint, 520 GrAA aa, 521 const SkMatrix& viewMatrix, 522 const SkRect& rect); 523 524 void drawShapeUsingPathRenderer(const GrClip&, GrPaint&&, GrAA, const SkMatrix&, 525 const GrShape&); 526 527 // Allows caller of addDrawOp to know which op list an op will be added to. 528 using WillAddOpFn = void(GrOp*, uint32_t opListID); 529 // These perform processing specific to GrDrawOp-derived ops before recording them into an 530 // op list. Before adding the op to an op list the WillAddOpFn is called. Note that it 531 // will not be called in the event that the op is discarded. Moreover, the op may merge into 532 // another op after the function is called (either before addDrawOp returns or some time later). 533 void addDrawOp(const GrClip&, std::unique_ptr<GrDrawOp>, 534 const std::function<WillAddOpFn>& = std::function<WillAddOpFn>()); 535 536 // Makes a copy of the proxy if it is necessary for the draw and places the texture that should 537 // be used by GrXferProcessor to access the destination color in 'result'. If the return 538 // value is false then a texture copy could not be made. 539 bool SK_WARN_UNUSED_RESULT setupDstProxy(GrRenderTargetProxy*, 540 const GrClip&, 541 const GrOp& op, 542 GrXferProcessor::DstProxy* result); 543 544 GrRenderTargetOpList* getRTOpList(); 545 GrOpList* getOpList() override; 546 547 std::unique_ptr<GrTextTarget> fTextTarget; 548 sk_sp<GrRenderTargetProxy> fRenderTargetProxy; 549 550 // In MDB-mode the GrOpList can be closed by some other renderTargetContext that has picked 551 // it up. For this reason, the GrOpList should only ever be accessed via 'getOpList'. 552 sk_sp<GrRenderTargetOpList> fOpList; 553 554 SkSurfaceProps fSurfaceProps; 555 bool fManagedOpList; 556 557 typedef GrSurfaceContext INHERITED; 558 }; 559 560 #endif 561