blob: c499d0994c13df81a2545e1f00c3db8d0afbdbc9 [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"
Brian Salomonf3569f02017-10-24 12:52:33 -040012#include "GrColorSpaceInfo.h"
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 Phillips2de8cfa2017-06-28 10:33:41 -040018class GrOpList;
Robert Phillipsd46697a2017-01-25 12:10:37 -050019class GrRenderTargetContext;
Robert Phillips27341362016-12-14 08:46:47 -050020class GrRenderTargetProxy;
Brian Osman45580d32016-11-23 09:37:01 -050021class GrSingleOwner;
22class GrSurface;
Robert Phillipse305cc1f2016-12-14 12:19:05 -050023class GrSurfaceContextPriv;
Robert Phillips27341362016-12-14 08:46:47 -050024class GrSurfaceProxy;
25class GrTextureProxy;
Brian Osman45580d32016-11-23 09:37:01 -050026struct 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
Brian Salomonf3569f02017-10-24 12:52:33 -040036 const GrColorSpaceInfo& colorSpaceInfo() const { return fColorSpaceInfo; }
Robert Phillips2c862492017-01-18 10:08:39 -050037
Robert Phillipsd46697a2017-01-25 12:10:37 -050038 // TODO: these two calls would be way cooler if this object had a GrSurfaceProxy pointer
Robert Phillipsf200a902017-01-30 13:27:37 -050039 int width() const { return this->asSurfaceProxy()->width(); }
40 int height() const { return this->asSurfaceProxy()->height(); }
Robert Phillipsd46697a2017-01-25 12:10:37 -050041
Robert Phillipse2f7d182016-12-15 09:23:05 -050042 /*
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 */
Robert Phillips2de8cfa2017-06-28 10:33:41 -040054 bool copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint);
Robert Phillipse2f7d182016-12-15 09:23:05 -050055
56 bool copy(GrSurfaceProxy* src) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -040057 return this->copy(src,
58 SkIRect::MakeWH(src->width(), src->height()),
59 SkIPoint::Make(0, 0));
Robert Phillipse2f7d182016-12-15 09:23:05 -050060 }
Brian Osman45580d32016-11-23 09:37:01 -050061
Robert Phillips2c862492017-01-18 10:08:39 -050062 /**
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,
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040074 int x, int y, uint32_t flags = 0);
Robert Phillips2c862492017-01-18 10:08:39 -050075
76 /**
Robert Phillipsb726d582017-03-09 16:36:32 -050077 * Writes a rectangle of pixels [srcInfo, srcBuffer, srcRowbytes] into the
Robert Phillips2c862492017-01-18 10:08:39 -050078 * 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,
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040089 int x, int y, uint32_t flags = 0);
Robert Phillips2c862492017-01-18 10:08:39 -050090
Robert Phillips27341362016-12-14 08:46:47 -050091 // TODO: this is virtual b.c. this object doesn't have a pointer to the wrapped GrSurfaceProxy?
Robert Phillipsf200a902017-01-30 13:27:37 -050092 virtual GrSurfaceProxy* asSurfaceProxy() = 0;
93 virtual const GrSurfaceProxy* asSurfaceProxy() const = 0;
94 virtual sk_sp<GrSurfaceProxy> asSurfaceProxyRef() = 0;
95
96 virtual GrTextureProxy* asTextureProxy() = 0;
Greg Daniele252f082017-10-23 16:05:23 -040097 virtual const GrTextureProxy* asTextureProxy() const = 0;
Robert Phillipsf200a902017-01-30 13:27:37 -050098 virtual sk_sp<GrTextureProxy> asTextureProxyRef() = 0;
99
100 virtual GrRenderTargetProxy* asRenderTargetProxy() = 0;
101 virtual sk_sp<GrRenderTargetProxy> asRenderTargetProxyRef() = 0;
Robert Phillips27341362016-12-14 08:46:47 -0500102
Robert Phillipsd46697a2017-01-25 12:10:37 -0500103 virtual GrRenderTargetContext* asRenderTargetContext() { return nullptr; }
104
Brian Osman45580d32016-11-23 09:37:01 -0500105 GrAuditTrail* auditTrail() { return fAuditTrail; }
106
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500107 // Provides access to functions that aren't part of the public API.
108 GrSurfaceContextPriv surfPriv();
109 const GrSurfaceContextPriv surfPriv() const;
110
Brian Osman45580d32016-11-23 09:37:01 -0500111protected:
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500112 friend class GrSurfaceContextPriv;
113
Brian Salomonf3569f02017-10-24 12:52:33 -0400114 GrSurfaceContext(GrContext*, GrDrawingManager*, GrPixelConfig, sk_sp<SkColorSpace>,
115 GrAuditTrail*, GrSingleOwner*);
Robert Phillips72152832017-01-25 17:31:35 -0500116
117 GrDrawingManager* drawingManager() { return fDrawingManager; }
118 const GrDrawingManager* drawingManager() const { return fDrawingManager; }
Brian Osman45580d32016-11-23 09:37:01 -0500119
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400120 virtual GrOpList* getOpList() = 0;
121 SkDEBUGCODE(virtual void validate() const = 0;)
122
Brian Osman45580d32016-11-23 09:37:01 -0500123 SkDEBUGCODE(GrSingleOwner* singleOwner() { return fSingleOwner; })
124
Brian Salomonf3569f02017-10-24 12:52:33 -0400125 GrContext* fContext;
126 GrAuditTrail* fAuditTrail;
Brian Osman45580d32016-11-23 09:37:01 -0500127
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500128private:
Brian Salomonf3569f02017-10-24 12:52:33 -0400129 GrColorSpaceInfo fColorSpaceInfo;
130
131 GrDrawingManager* fDrawingManager;
Robert Phillips72152832017-01-25 17:31:35 -0500132
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