2 * Copyright 2015 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef GrRenderTargetContext_DEFINED
9 #define GrRenderTargetContext_DEFINED
12 #include "GrContext.h"
14 #include "GrSurfaceContext.h"
16 #include "SkSurfaceProps.h"
17 #include "../private/GrInstancedPipelineInfo.h"
18 #include "../private/GrRenderTargetProxy.h"
21 class GrDrawingManager;
24 class GrPipelineBuilder;
26 class GrRenderTargetContextPriv;
27 class GrRenderTargetOpList;
31 struct GrUserStencilSettings;
47 * A helper object to orchestrate commands (draws, etc...) for GrSurfaces that are GrRenderTargets.
49 class SK_API GrRenderTargetContext : public GrSurfaceContext {
51 ~GrRenderTargetContext() override;
53 bool copySurface(GrSurface* src, const SkIRect& srcRect, const SkIPoint& dstPoint) override;
55 // TODO: it is odd that we need both the SkPaint in the following 3 methods.
56 // We should extract the text parameters from SkPaint and pass them separately
57 // akin to GrStyle (GrTextInfo?)
58 virtual void drawText(const GrClip&, const GrPaint&, const SkPaint&,
59 const SkMatrix& viewMatrix, const char text[], size_t byteLength,
60 SkScalar x, SkScalar y, const SkIRect& clipBounds);
61 virtual void drawPosText(const GrClip&, const GrPaint&, const SkPaint&,
62 const SkMatrix& viewMatrix, const char text[], size_t byteLength,
63 const SkScalar pos[], int scalarsPerPosition,
64 const SkPoint& offset, const SkIRect& clipBounds);
65 virtual void drawTextBlob(const GrClip&, const SkPaint&,
66 const SkMatrix& viewMatrix, const SkTextBlob*,
67 SkScalar x, SkScalar y,
68 SkDrawFilter*, const SkIRect& clipBounds);
71 * Provides a perfomance hint that the render target's contents are allowed
72 * to become undefined.
77 * Clear the entire or rect of the render target, ignoring any clips.
78 * @param rect the rect to clear or the whole thing if rect is NULL.
79 * @param color the color to clear to.
80 * @param canIgnoreRect allows partial clears to be converted to whole
81 * clears on platforms for which that is cheap
83 void clear(const SkIRect* rect, GrColor color, bool canIgnoreRect);
86 * Draw everywhere (respecting the clip) with the paint.
88 void drawPaint(const GrClip&, const GrPaint&, const SkMatrix& viewMatrix);
91 * Draw the rect using a paint.
92 * @param paint describes how to color pixels.
93 * @param viewMatrix transformation matrix
94 * @param style The style to apply. Null means fill. Currently path effects are not
96 * The rects coords are used to access the paint (through texture matrix)
98 void drawRect(const GrClip&,
100 const SkMatrix& viewMatrix,
102 const GrStyle* style = nullptr);
105 * Maps a rectangle of shader coordinates to a rectangle and fills that rectangle.
107 * @param paint describes how to color pixels.
108 * @param viewMatrix transformation matrix which applies to rectToDraw
109 * @param rectToDraw the rectangle to draw
110 * @param localRect the rectangle of shader coordinates applied to rectToDraw
112 void fillRectToRect(const GrClip&,
113 const GrPaint& paint,
114 const SkMatrix& viewMatrix,
115 const SkRect& rectToDraw,
116 const SkRect& localRect);
119 * Fills a rect with a paint and a localMatrix.
121 void fillRectWithLocalMatrix(const GrClip& clip,
122 const GrPaint& paint,
123 const SkMatrix& viewMatrix,
125 const SkMatrix& localMatrix);
128 * Draw a roundrect using a paint.
130 * @param paint describes how to color pixels.
131 * @param viewMatrix transformation matrix
132 * @param rrect the roundrect to draw
133 * @param style style to apply to the rrect. Currently path effects are not allowed.
135 void drawRRect(const GrClip&,
137 const SkMatrix& viewMatrix,
138 const SkRRect& rrect,
139 const GrStyle& style);
142 * Draw a roundrect using a paint and a shadow shader. This is separate from drawRRect
143 * because it uses different underlying geometry and GeometryProcessor
145 * @param paint describes how to color pixels.
146 * @param viewMatrix transformation matrix
147 * @param rrect the roundrect to draw
148 * @param blurRadius amount of shadow blur to apply (in device space)
149 * @param style style to apply to the rrect. Currently path effects are not allowed.
151 void drawShadowRRect(const GrClip&,
153 const SkMatrix& viewMatrix,
154 const SkRRect& rrect,
156 const GrStyle& style);
159 * Shortcut for drawing an SkPath consisting of nested rrects using a paint.
160 * Does not support stroking. The result is undefined if outer does not contain
163 * @param paint describes how to color pixels.
164 * @param viewMatrix transformation matrix
165 * @param outer the outer roundrect
166 * @param inner the inner roundrect
168 void drawDRRect(const GrClip&,
170 const SkMatrix& viewMatrix,
171 const SkRRect& outer,
172 const SkRRect& inner);
177 * @param paint describes how to color pixels.
178 * @param viewMatrix transformation matrix
179 * @param path the path to draw
180 * @param style style to apply to the path.
182 void drawPath(const GrClip&,
184 const SkMatrix& viewMatrix,
186 const GrStyle& style);
189 * Draws vertices with a paint.
191 * @param paint describes how to color pixels.
192 * @param viewMatrix transformation matrix
193 * @param primitiveType primitives type to draw.
194 * @param vertexCount number of vertices.
195 * @param positions array of vertex positions, required.
196 * @param texCoords optional array of texture coordinates used
197 * to access the paint.
198 * @param colors optional array of per-vertex colors, supercedes
199 * the paint's color field.
200 * @param indices optional array of indices. If NULL vertices
201 * are drawn non-indexed.
202 * @param indexCount if indices is non-null then this is the
205 void drawVertices(const GrClip&,
206 const GrPaint& paint,
207 const SkMatrix& viewMatrix,
208 GrPrimitiveType primitiveType,
210 const SkPoint positions[],
211 const SkPoint texs[],
212 const GrColor colors[],
213 const uint16_t indices[],
217 * Draws textured sprites from an atlas with a paint.
219 * @param paint describes how to color pixels.
220 * @param viewMatrix transformation matrix
221 * @param spriteCount number of sprites.
222 * @param xform array of compressed transformation data, required.
223 * @param texRect array of texture rectangles used to access the paint.
224 * @param colors optional array of per-sprite colors, supercedes
225 * the paint's color field.
227 void drawAtlas(const GrClip&,
228 const GrPaint& paint,
229 const SkMatrix& viewMatrix,
231 const SkRSXform xform[],
232 const SkRect texRect[],
233 const SkColor colors[]);
238 * @param paint describes how to color pixels
239 * @param viewMatrix transformation matrix
240 * @param region the region to be drawn
241 * @param style style to apply to the region
243 void drawRegion(const GrClip&,
244 const GrPaint& paint,
245 const SkMatrix& viewMatrix,
246 const SkRegion& region,
247 const GrStyle& style);
252 * @param paint describes how to color pixels.
253 * @param viewMatrix transformation matrix
254 * @param oval the bounding rect of the oval.
255 * @param style style to apply to the oval. Currently path effects are not allowed.
257 void drawOval(const GrClip&,
258 const GrPaint& paint,
259 const SkMatrix& viewMatrix,
261 const GrStyle& style);
263 * Draws a partial arc of an oval.
265 * @param paint describes how to color pixels.
266 * @param viewMatrix transformation matrix.
267 * @param oval the bounding rect of the oval.
268 * @param startAngle starting angle in degrees.
269 * @param sweepAngle angle to sweep in degrees. Must be in (-360, 360)
270 * @param useCenter true means that the implied path begins at the oval center, connects as a
271 * line to the point indicated by the start contains the arc indicated by
272 * the sweep angle. If false the line beginning at the center point is
274 * @param style style to apply to the oval.
276 void drawArc(const GrClip&,
277 const GrPaint& paint,
278 const SkMatrix& viewMatrix,
283 const GrStyle& style);
286 * Draw the image as a set of rects, specified by |iter|.
288 void drawImageLattice(const GrClip&,
289 const GrPaint& paint,
290 const SkMatrix& viewMatrix,
293 std::unique_ptr<SkLatticeIter> iter,
297 * After this returns any pending surface IO will be issued to the backend 3D API and
298 * if the surface has MSAA it will be resolved.
300 void prepareForExternalIO();
303 * Reads a rectangle of pixels from the render target context.
304 * @param dstInfo image info for the destination
305 * @param dstBuffer destination pixels for the read
306 * @param dstRowBytes bytes in a row of 'dstBuffer'
307 * @param x x offset w/in the render target context from which to read
308 * @param y y offset w/in the render target context from which to read
310 * @return true if the read succeeded, false if not. The read can fail because of an
311 * unsupported pixel config.
313 bool readPixels(const SkImageInfo& dstInfo, void* dstBuffer, size_t dstRowBytes, int x, int y);
316 * Writes a rectangle of pixels [srcInfo, srcBuffer, srcRowbytes] into the
317 * renderTargetContext at the specified position.
318 * @param srcInfo image info for the source pixels
319 * @param srcBuffer source for the write
320 * @param srcRowBytes bytes in a row of 'srcBuffer'
321 * @param x x offset w/in the render target context at which to write
322 * @param y y offset w/in the render target context at which to write
324 * @return true if the write succeeded, false if not. The write can fail because of an
325 * unsupported pixel config.
327 bool writePixels(const SkImageInfo& srcInfo, const void* srcBuffer, size_t srcRowBytes,
330 bool isStencilBufferMultisampled() const {
331 return fRenderTargetProxy->isStencilBufferMultisampled();
333 bool isUnifiedMultisampled() const { return fRenderTargetProxy->isUnifiedMultisampled(); }
334 bool hasMixedSamples() const { return fRenderTargetProxy->isMixedSampled(); }
336 bool mustUseHWAA(const GrPaint& paint) const {
337 return paint.isAntiAlias() && fRenderTargetProxy->isUnifiedMultisampled();
340 const GrCaps* caps() const { return fContext->caps(); }
341 const GrSurfaceDesc& desc() const { return fRenderTargetProxy->desc(); }
342 int width() const { return fRenderTargetProxy->width(); }
343 int height() const { return fRenderTargetProxy->height(); }
344 GrPixelConfig config() const { return fRenderTargetProxy->config(); }
345 int numColorSamples() const { return fRenderTargetProxy->numColorSamples(); }
346 bool isGammaCorrect() const { return SkToBool(fColorSpace.get()); }
347 const SkSurfaceProps& surfaceProps() const { return fSurfaceProps; }
348 SkColorSpace* getColorSpace() const { return fColorSpace.get(); }
349 GrColorSpaceXform* getColorXformFromSRGB() const { return fColorXformFromSRGB.get(); }
350 GrSurfaceOrigin origin() const { return fRenderTargetProxy->origin(); }
352 bool wasAbandoned() const;
354 GrRenderTarget* instantiate();
356 GrRenderTarget* accessRenderTarget() {
357 // TODO: usage of this entry point needs to be reduced and potentially eliminated
358 // since it ends the deferral of the GrRenderTarget's allocation
359 return fRenderTargetProxy->instantiate(fContext->textureProvider());
362 GrTextureProxy* asDeferredTexture();
364 sk_sp<GrTexture> asTexture() {
365 if (!this->accessRenderTarget()) {
369 // TODO: usage of this entry point needs to be reduced and potentially eliminated
370 // since it ends the deferral of the GrRenderTarget's allocation
371 // It's usage should migrate to asDeferredTexture
372 return sk_ref_sp(this->accessRenderTarget()->asTexture());
375 // Provides access to functions that aren't part of the public API.
376 GrRenderTargetContextPriv priv();
377 const GrRenderTargetContextPriv priv() const;
379 bool isWrapped_ForTesting() const;
382 GrRenderTargetContext(GrContext*, GrDrawingManager*, sk_sp<GrRenderTargetProxy>,
383 sk_sp<SkColorSpace>, const SkSurfaceProps* surfaceProps, GrAuditTrail*,
386 GrDrawingManager* drawingManager() { return fDrawingManager; }
388 SkDEBUGCODE(void validate() const;)
391 friend class GrAtlasTextBlob; // for access to addDrawOp
392 friend class GrStencilAndCoverTextContext; // for access to addDrawOp
394 friend class GrDrawingManager; // for ctor
395 friend class GrRenderTargetContextPriv;
396 friend class GrTestTarget; // for access to getOpList
397 friend class GrSWMaskHelper; // for access to addDrawOp
399 // All the path renderers currently make their own ops
400 friend class GrSoftwarePathRenderer; // for access to addDrawOp
401 friend class GrAAConvexPathRenderer; // for access to addDrawOp
402 friend class GrDashLinePathRenderer; // for access to addDrawOp
403 friend class GrAAHairLinePathRenderer; // for access to addDrawOp
404 friend class GrAALinearizingConvexPathRenderer; // for access to addDrawOp
405 friend class GrAADistanceFieldPathRenderer; // for access to addDrawOp
406 friend class GrDefaultPathRenderer; // for access to addDrawOp
407 friend class GrPLSPathRenderer; // for access to addDrawOp
408 friend class GrMSAAPathRenderer; // for access to addDrawOp
409 friend class GrStencilAndCoverPathRenderer; // for access to addDrawOp
410 friend class GrTessellatingPathRenderer; // for access to addDrawOp
412 void internalClear(const GrFixedClip&, const GrColor, bool canIgnoreClip);
414 bool drawFilledDRRect(const GrClip& clip,
415 const GrPaint& paint,
416 const SkMatrix& viewMatrix,
417 const SkRRect& origOuter,
418 const SkRRect& origInner);
420 bool drawFilledRect(const GrClip& clip,
421 const GrPaint& paint,
422 const SkMatrix& viewMatrix,
424 const GrUserStencilSettings* ss);
426 void drawNonAAFilledRect(const GrClip&,
428 const SkMatrix& viewMatrix,
430 const SkRect* localRect,
431 const SkMatrix* localMatrix,
432 const GrUserStencilSettings* ss,
435 void internalDrawPath(const GrClip& clip,
436 const GrPaint& paint,
437 const SkMatrix& viewMatrix,
439 const GrStyle& style);
441 // This entry point allows the GrTextContext-derived classes to add their ops to the GrOpList.
442 void addDrawOp(const GrPipelineBuilder&, const GrClip&, GrDrawOp*);
444 GrRenderTargetOpList* getOpList();
446 GrDrawingManager* fDrawingManager;
447 sk_sp<GrRenderTargetProxy> fRenderTargetProxy;
449 // In MDB-mode the GrOpList can be closed by some other renderTargetContext that has picked
450 // it up. For this reason, the GrOpList should only ever be accessed via 'getOpList'.
451 GrRenderTargetOpList* fOpList;
452 GrInstancedPipelineInfo fInstancedPipelineInfo;
454 sk_sp<SkColorSpace> fColorSpace;
455 sk_sp<GrColorSpaceXform> fColorXformFromSRGB;
456 SkSurfaceProps fSurfaceProps;