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 "../private/GrSurfaceProxy.h"
12 
13 #include "SkRefCnt.h"
14 
15 class GrAuditTrail;
16 class GrContext;
17 class GrDrawingManager;
18 class GrRenderTargetContext;
19 class GrRenderTargetProxy;
20 class GrSingleOwner;
21 class GrSurface;
22 class GrSurfaceContextPriv;
23 class GrSurfaceProxy;
24 class GrTextureProxy;
25 struct SkIPoint;
26 struct SkIRect;
27 
28 /**
29  * A helper object to orchestrate commands for a particular surface
30  */
31 class SK_API GrSurfaceContext : public SkRefCnt {
32 public:
~GrSurfaceContext()33     ~GrSurfaceContext() override {}
34 
getColorSpace()35     SkColorSpace* getColorSpace() const { return fColorSpace.get(); }
refColorSpace()36     sk_sp<SkColorSpace> refColorSpace() const { return fColorSpace; }
isGammaCorrect()37     bool isGammaCorrect() const { return fColorSpace; }
38 
39     // TODO: these two calls would be way cooler if this object had a GrSurfaceProxy pointer
width()40     int width() const { return this->asSurfaceProxy()->width(); }
height()41     int height() const { return this->asSurfaceProxy()->height(); }
42 
43     /*
44      * Copy 'src' into the proxy backing this context
45      * @param src       src of pixels
46      * @param srcRect   the subset of 'src' to copy
47      * @param dstPoint  the origin of the 'srcRect' in the destination coordinate space
48      * @return          true if the copy succeeded; false otherwise
49      *
50      * Note: Notionally, 'srcRect' is clipped to 'src's extent with 'dstPoint' being adjusted.
51      *       Then the 'srcRect' offset by 'dstPoint' is clipped against the dst's extent.
52      *       The end result is only valid src pixels and dst pixels will be touched but the copied
53      *       regions will not be shifted.
54      */
copy(GrSurfaceProxy * src,const SkIRect & srcRect,const SkIPoint & dstPoint)55     bool copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
56         return this->onCopy(src, srcRect, dstPoint);
57     }
58 
copy(GrSurfaceProxy * src)59     bool copy(GrSurfaceProxy* src) {
60         return this->onCopy(src,
61                             SkIRect::MakeWH(src->width(), src->height()),
62                             SkIPoint::Make(0, 0));
63     }
64 
65     /**
66      * Reads a rectangle of pixels from the render target context.
67      * @param dstInfo       image info for the destination
68      * @param dstBuffer     destination pixels for the read
69      * @param dstRowBytes   bytes in a row of 'dstBuffer'
70      * @param x             x offset w/in the render target context from which to read
71      * @param y             y offset w/in the render target context from which to read
72      *
73      * @return true if the read succeeded, false if not. The read can fail because of an
74      *              unsupported pixel config.
75      */
76     bool readPixels(const SkImageInfo& dstInfo, void* dstBuffer, size_t dstRowBytes,
77                     int x, int y, uint32_t flags = 0) {
78         return this->onReadPixels(dstInfo, dstBuffer, dstRowBytes, x, y, flags);
79     }
80 
81     /**
82      * Writes a rectangle of pixels [srcInfo, srcBuffer, srcRowbytes] into the
83      * renderTargetContext at the specified position.
84      * @param srcInfo       image info for the source pixels
85      * @param srcBuffer     source for the write
86      * @param srcRowBytes   bytes in a row of 'srcBuffer'
87      * @param x             x offset w/in the render target context at which to write
88      * @param y             y offset w/in the render target context at which to write
89      *
90      * @return true if the write succeeded, false if not. The write can fail because of an
91      *              unsupported pixel config.
92      */
93     bool writePixels(const SkImageInfo& srcInfo, const void* srcBuffer, size_t srcRowBytes,
94                      int x, int y, uint32_t flags = 0) {
95         return this->onWritePixels(srcInfo, srcBuffer, srcRowBytes, x, y, flags);
96     }
97 
98     // TODO: this is virtual b.c. this object doesn't have a pointer to the wrapped GrSurfaceProxy?
99     virtual GrSurfaceProxy* asSurfaceProxy() = 0;
100     virtual const GrSurfaceProxy* asSurfaceProxy() const = 0;
101     virtual sk_sp<GrSurfaceProxy> asSurfaceProxyRef() = 0;
102 
103     virtual GrTextureProxy* asTextureProxy() = 0;
104     virtual sk_sp<GrTextureProxy> asTextureProxyRef() = 0;
105 
106     virtual GrRenderTargetProxy* asRenderTargetProxy() = 0;
107     virtual sk_sp<GrRenderTargetProxy> asRenderTargetProxyRef() = 0;
108 
asRenderTargetContext()109     virtual GrRenderTargetContext* asRenderTargetContext() { return nullptr; }
110 
auditTrail()111     GrAuditTrail* auditTrail() { return fAuditTrail; }
112 
113     // Provides access to functions that aren't part of the public API.
114     GrSurfaceContextPriv surfPriv();
115     const GrSurfaceContextPriv surfPriv() const;
116 
117 protected:
118     friend class GrSurfaceContextPriv;
119 
120     GrSurfaceContext(GrContext*, GrDrawingManager*,
121                      sk_sp<SkColorSpace>, GrAuditTrail*, GrSingleOwner*);
122 
drawingManager()123     GrDrawingManager* drawingManager() { return fDrawingManager; }
drawingManager()124     const GrDrawingManager* drawingManager() const { return fDrawingManager; }
125 
126     SkDEBUGCODE(GrSingleOwner* singleOwner() { return fSingleOwner; })
127 
128     GrContext*            fContext;
129     sk_sp<SkColorSpace>   fColorSpace;
130     GrAuditTrail*         fAuditTrail;
131 
132     // In debug builds we guard against improper thread handling
133     SkDEBUGCODE(mutable GrSingleOwner* fSingleOwner;)
134 
135 private:
136     virtual bool onCopy(GrSurfaceProxy* src,
137                         const SkIRect& srcRect,
138                         const SkIPoint& dstPoint) = 0;
139     virtual bool onReadPixels(const SkImageInfo& dstInfo, void* dstBuffer,
140                               size_t dstRowBytes, int x, int y, uint32_t flags) = 0;
141     virtual bool onWritePixels(const SkImageInfo& srcInfo, const void* srcBuffer,
142                                size_t srcRowBytes, int x, int y, uint32_t flags) = 0;
143 
144     GrDrawingManager*     fDrawingManager;
145 
146     typedef SkRefCnt INHERITED;
147 };
148 
149 #endif
150