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