• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 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 GrSurfaceContext_DEFINED
9 #define GrSurfaceContext_DEFINED
10 
11 #include "include/core/SkFilterQuality.h"
12 #include "include/core/SkImageInfo.h"
13 #include "include/core/SkRect.h"
14 #include "include/core/SkRefCnt.h"
15 #include "include/core/SkSurface.h"
16 #include "src/gpu/GrColorSpaceInfo.h"
17 #include "src/gpu/GrDataUtils.h"
18 #include "src/gpu/GrSurfaceProxy.h"
19 
20 class GrAuditTrail;
21 class GrDrawingManager;
22 class GrOpList;
23 class GrRecordingContext;
24 class GrRenderTargetContext;
25 class GrRenderTargetProxy;
26 class GrSingleOwner;
27 class GrSurface;
28 class GrSurfaceContextPriv;
29 class GrSurfaceProxy;
30 class GrTextureProxy;
31 struct SkIPoint;
32 struct SkIRect;
33 
34 /**
35  * A helper object to orchestrate commands for a particular surface
36  */
37 class SK_API GrSurfaceContext : public SkRefCnt {
38 public:
~GrSurfaceContext()39     ~GrSurfaceContext() override {}
40 
colorSpaceInfo()41     const GrColorSpaceInfo& colorSpaceInfo() const { return fColorSpaceInfo; }
42 
43     // TODO: these two calls would be way cooler if this object had a GrSurfaceProxy pointer
width()44     int width() const { return this->asSurfaceProxy()->width(); }
height()45     int height() const { return this->asSurfaceProxy()->height(); }
46 
47     const GrCaps* caps() const;
48 
49     /**
50      * Reads a rectangle of pixels from the render target context.
51      * @param dstInfo       image info for the destination
52      * @param dst           destination pixels for the read
53      * @param rowBytes      bytes in a row of 'dst'
54      * @param srcPt         offset w/in the surface context from which to read
55      * @param direct        The direct context to use. If null will use our GrRecordingContext if it
56      *                      is a GrDirectContext and fail otherwise.
57      */
58     bool readPixels(const GrPixelInfo& dstInfo, void* dst, size_t rowBytes, SkIPoint srcPt,
59                     GrContext* direct = nullptr);
60 
61     /**
62      * Writes a rectangle of pixels [srcInfo, srcBuffer, srcRowbytes] into the
63      * renderTargetContext at the specified position.
64      * @param srcInfo       image info for the source pixels
65      * @param src           source for the write
66      * @param rowBytes      bytes in a row of 'src'
67      * @param dstPt         offset w/in the surface context at which to write
68      * @param direct        The direct context to use. If null will use our GrRecordingContext if it
69      *                      is a GrDirectContext and fail otherwise.
70      */
71     bool writePixels(const GrPixelInfo& srcInfo, const void* src, size_t rowBytes, SkIPoint dstPt,
72                      GrContext* direct = nullptr);
73 
74     // TODO: this is virtual b.c. this object doesn't have a pointer to the wrapped GrSurfaceProxy?
75     virtual GrSurfaceProxy* asSurfaceProxy() = 0;
76     virtual const GrSurfaceProxy* asSurfaceProxy() const = 0;
77     virtual sk_sp<GrSurfaceProxy> asSurfaceProxyRef() = 0;
78 
79     virtual GrTextureProxy* asTextureProxy() = 0;
80     virtual const GrTextureProxy* asTextureProxy() const = 0;
81     virtual sk_sp<GrTextureProxy> asTextureProxyRef() = 0;
82 
83     virtual GrRenderTargetProxy* asRenderTargetProxy() = 0;
84     virtual sk_sp<GrRenderTargetProxy> asRenderTargetProxyRef() = 0;
85 
asRenderTargetContext()86     virtual GrRenderTargetContext* asRenderTargetContext() { return nullptr; }
87 
88     GrAuditTrail* auditTrail();
89 
90     // Provides access to functions that aren't part of the public API.
91     GrSurfaceContextPriv surfPriv();
92     const GrSurfaceContextPriv surfPriv() const;
93 
94 #if GR_TEST_UTILS
testCopy(GrSurfaceProxy * src,const SkIRect & srcRect,const SkIPoint & dstPoint)95     bool testCopy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
96         return this->copy(src, srcRect, dstPoint);
97     }
98 
testCopy(GrSurfaceProxy * src)99     bool testCopy(GrSurfaceProxy* src) {
100         return this->copy(src);
101     }
102 #endif
103 
104 
105 protected:
106     friend class GrSurfaceContextPriv;
107 
108     GrSurfaceContext(GrRecordingContext*, GrColorType, SkAlphaType, sk_sp<SkColorSpace>);
109 
110     GrDrawingManager* drawingManager();
111     const GrDrawingManager* drawingManager() const;
112 
113     virtual GrOpList* getOpList() = 0;
114     SkDEBUGCODE(virtual void validate() const = 0;)
115 
116     SkDEBUGCODE(GrSingleOwner* singleOwner();)
117 
118     GrRecordingContext* fContext;
119 
120     // The rescaling step of asyncRescaleAndReadPixels[YUV420]().
121     sk_sp<GrRenderTargetContext> rescale(const SkImageInfo& info, const SkIRect& srcRect,
122                                          SkSurface::RescaleGamma rescaleGamma,
123                                          SkFilterQuality rescaleQuality);
124 
125     // Inserts a transfer, part of the implementation of asyncReadPixels and
126     // asyncRescaleAndReadPixelsYUV420().
127     struct PixelTransferResult {
128         using ConversionFn = void(void* dst, const void* mappedBuffer);
129         // If null then the transfer could not be performed. Otherwise this buffer will contain
130         // the pixel data when the transfer is complete.
131         sk_sp<GrGpuBuffer> fTransferBuffer;
132         // If this is null then the transfer buffer will contain the data in the requested
133         // color type. Otherwise, when the transfer is done this must be called to convert
134         // from the transfer buffer's color type to the requested color type.
135         std::function<ConversionFn> fPixelConverter;
136     };
137     PixelTransferResult transferPixels(GrColorType colorType, const SkIRect& rect);
138 
139 private:
140     friend class GrSurfaceProxy; // for copy
141 
142     /**
143      * Copy 'src' into the proxy backing this context. This call will not do any draw fallback.
144      * Currently only writePixels and replaceRenderTarget call this directly. All other copies
145      * should go through GrSurfaceProxy::Copy.
146      * @param src       src of pixels
147      * @param srcRect   the subset of 'src' to copy
148      * @param dstPoint  the origin of the 'srcRect' in the destination coordinate space
149      * @return          true if the copy succeeded; false otherwise
150      *
151      * Note: Notionally, 'srcRect' is clipped to 'src's extent with 'dstPoint' being adjusted.
152      *       Then the 'srcRect' offset by 'dstPoint' is clipped against the dst's extent.
153      *       The end result is only valid src pixels and dst pixels will be touched but the copied
154      *       regions will not be shifted. The 'src' must have the same origin as the backing proxy
155      *       of fSurfaceContext.
156      */
157     bool copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint);
158 
copy(GrSurfaceProxy * src)159     bool copy(GrSurfaceProxy* src) {
160         return this->copy(src, SkIRect::MakeWH(src->width(), src->height()), SkIPoint::Make(0, 0));
161     }
162 
163     GrColorSpaceInfo    fColorSpaceInfo;
164 
165     typedef SkRefCnt INHERITED;
166 };
167 
168 #endif
169