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