blob: bab753c069898ab18c087d42eca13fcc0d945240 [file] [log] [blame]
Brian Osman45580d32016-11-23 09:37:01 -05001/*
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
Robert Phillipse2f7d182016-12-15 09:23:05 -050011#include "../private/GrSurfaceProxy.h"
12
Brian Osman45580d32016-11-23 09:37:01 -050013#include "SkRefCnt.h"
14
15class GrAuditTrail;
16class GrContext;
Robert Phillips72152832017-01-25 17:31:35 -050017class GrDrawingManager;
Robert Phillipsd46697a2017-01-25 12:10:37 -050018class GrRenderTargetContext;
Robert Phillips27341362016-12-14 08:46:47 -050019class GrRenderTargetProxy;
Brian Osman45580d32016-11-23 09:37:01 -050020class GrSingleOwner;
21class GrSurface;
Robert Phillipse305cc1f2016-12-14 12:19:05 -050022class GrSurfaceContextPriv;
Robert Phillips27341362016-12-14 08:46:47 -050023class GrSurfaceProxy;
24class GrTextureProxy;
Brian Osman45580d32016-11-23 09:37:01 -050025struct SkIPoint;
26struct SkIRect;
27
28/**
29 * A helper object to orchestrate commands for a particular surface
30 */
31class SK_API GrSurfaceContext : public SkRefCnt {
32public:
33 ~GrSurfaceContext() override {}
34
Robert Phillips2c862492017-01-18 10:08:39 -050035 SkColorSpace* getColorSpace() const { return fColorSpace.get(); }
36 sk_sp<SkColorSpace> refColorSpace() const { return fColorSpace; }
Matt Sarettf3880932017-03-24 10:06:03 -040037 bool isGammaCorrect() const { return fColorSpace; }
Robert Phillips2c862492017-01-18 10:08:39 -050038
Robert Phillipsd46697a2017-01-25 12:10:37 -050039 // TODO: these two calls would be way cooler if this object had a GrSurfaceProxy pointer
Robert Phillipsf200a902017-01-30 13:27:37 -050040 int width() const { return this->asSurfaceProxy()->width(); }
41 int height() const { return this->asSurfaceProxy()->height(); }
Robert Phillipsd46697a2017-01-25 12:10:37 -050042
Robert Phillipse2f7d182016-12-15 09:23:05 -050043 /*
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 */
55 bool copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
56 return this->onCopy(src, srcRect, dstPoint);
57 }
58
59 bool copy(GrSurfaceProxy* src) {
60 return this->onCopy(src,
61 SkIRect::MakeWH(src->width(), src->height()),
62 SkIPoint::Make(0, 0));
63 }
Brian Osman45580d32016-11-23 09:37:01 -050064
Robert Phillips2c862492017-01-18 10:08:39 -050065 /**
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,
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040077 int x, int y, uint32_t flags = 0);
Robert Phillips2c862492017-01-18 10:08:39 -050078
79 /**
Robert Phillipsb726d582017-03-09 16:36:32 -050080 * Writes a rectangle of pixels [srcInfo, srcBuffer, srcRowbytes] into the
Robert Phillips2c862492017-01-18 10:08:39 -050081 * renderTargetContext at the specified position.
82 * @param srcInfo image info for the source pixels
83 * @param srcBuffer source for the write
84 * @param srcRowBytes bytes in a row of 'srcBuffer'
85 * @param x x offset w/in the render target context at which to write
86 * @param y y offset w/in the render target context at which to write
87 *
88 * @return true if the write succeeded, false if not. The write can fail because of an
89 * unsupported pixel config.
90 */
91 bool writePixels(const SkImageInfo& srcInfo, const void* srcBuffer, size_t srcRowBytes,
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040092 int x, int y, uint32_t flags = 0);
Robert Phillips2c862492017-01-18 10:08:39 -050093
Robert Phillips27341362016-12-14 08:46:47 -050094 // TODO: this is virtual b.c. this object doesn't have a pointer to the wrapped GrSurfaceProxy?
Robert Phillipsf200a902017-01-30 13:27:37 -050095 virtual GrSurfaceProxy* asSurfaceProxy() = 0;
96 virtual const GrSurfaceProxy* asSurfaceProxy() const = 0;
97 virtual sk_sp<GrSurfaceProxy> asSurfaceProxyRef() = 0;
98
99 virtual GrTextureProxy* asTextureProxy() = 0;
100 virtual sk_sp<GrTextureProxy> asTextureProxyRef() = 0;
101
102 virtual GrRenderTargetProxy* asRenderTargetProxy() = 0;
103 virtual sk_sp<GrRenderTargetProxy> asRenderTargetProxyRef() = 0;
Robert Phillips27341362016-12-14 08:46:47 -0500104
Robert Phillipsd46697a2017-01-25 12:10:37 -0500105 virtual GrRenderTargetContext* asRenderTargetContext() { return nullptr; }
106
Brian Osman45580d32016-11-23 09:37:01 -0500107 GrAuditTrail* auditTrail() { return fAuditTrail; }
108
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500109 // Provides access to functions that aren't part of the public API.
110 GrSurfaceContextPriv surfPriv();
111 const GrSurfaceContextPriv surfPriv() const;
112
Brian Osman45580d32016-11-23 09:37:01 -0500113protected:
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500114 friend class GrSurfaceContextPriv;
115
Robert Phillips72152832017-01-25 17:31:35 -0500116 GrSurfaceContext(GrContext*, GrDrawingManager*,
117 sk_sp<SkColorSpace>, GrAuditTrail*, GrSingleOwner*);
118
119 GrDrawingManager* drawingManager() { return fDrawingManager; }
120 const GrDrawingManager* drawingManager() const { return fDrawingManager; }
Brian Osman45580d32016-11-23 09:37:01 -0500121
122 SkDEBUGCODE(GrSingleOwner* singleOwner() { return fSingleOwner; })
123
124 GrContext* fContext;
Robert Phillips2c862492017-01-18 10:08:39 -0500125 sk_sp<SkColorSpace> fColorSpace;
Brian Osman45580d32016-11-23 09:37:01 -0500126 GrAuditTrail* fAuditTrail;
127
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500128private:
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400129 virtual bool onCopy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) = 0;
Robert Phillipse2f7d182016-12-15 09:23:05 -0500130
Robert Phillips72152832017-01-25 17:31:35 -0500131 GrDrawingManager* fDrawingManager;
132
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400133 // In debug builds we guard against improper thread handling
134 SkDEBUGCODE(mutable GrSingleOwner* fSingleOwner;)
135
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500136 typedef SkRefCnt INHERITED;
Brian Osman45580d32016-11-23 09:37:01 -0500137};
138
139#endif