blob: 3cbb83830c666e1fdd69c8ebffcc401ac1345476 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkRefCnt.h"
12#include "include/private/GrSurfaceProxy.h"
13#include "src/gpu/GrColorSpaceInfo.h"
Brian Osman45580d32016-11-23 09:37:01 -050014
15class GrAuditTrail;
Robert Phillips72152832017-01-25 17:31:35 -050016class GrDrawingManager;
Robert Phillips2de8cfa2017-06-28 10:33:41 -040017class GrOpList;
Robert Phillips69893702019-02-22 11:16:30 -050018class GrRecordingContext;
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
Greg Daniel4c6f9b72019-06-06 13:40:59 -040042 /*
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
Greg Daniel6eb8c242019-06-05 10:22:24 -040062 /**
63 * These flags can be used with the read/write pixels functions below.
64 */
65 enum PixelOpsFlags {
66 /** The src for write or dst read is unpremultiplied. This is only respected if both the
67 config src and dst configs are an RGBA/BGRA 8888 format. */
68 kUnpremul_PixelOpsFlag = 0x4,
69 };
70
Robert Phillips2c862492017-01-18 10:08:39 -050071 /**
72 * Reads a rectangle of pixels from the render target context.
73 * @param dstInfo image info for the destination
74 * @param dstBuffer destination pixels for the read
75 * @param dstRowBytes bytes in a row of 'dstBuffer'
76 * @param x x offset w/in the render target context from which to read
77 * @param y y offset w/in the render target context from which to read
78 *
79 * @return true if the read succeeded, false if not. The read can fail because of an
80 * unsupported pixel config.
81 */
82 bool readPixels(const SkImageInfo& dstInfo, void* dstBuffer, size_t dstRowBytes,
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040083 int x, int y, uint32_t flags = 0);
Robert Phillips2c862492017-01-18 10:08:39 -050084
85 /**
Robert Phillipsb726d582017-03-09 16:36:32 -050086 * Writes a rectangle of pixels [srcInfo, srcBuffer, srcRowbytes] into the
Robert Phillips2c862492017-01-18 10:08:39 -050087 * renderTargetContext at the specified position.
88 * @param srcInfo image info for the source pixels
89 * @param srcBuffer source for the write
90 * @param srcRowBytes bytes in a row of 'srcBuffer'
91 * @param x x offset w/in the render target context at which to write
92 * @param y y offset w/in the render target context at which to write
93 *
94 * @return true if the write succeeded, false if not. The write can fail because of an
95 * unsupported pixel config.
96 */
97 bool writePixels(const SkImageInfo& srcInfo, const void* srcBuffer, size_t srcRowBytes,
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040098 int x, int y, uint32_t flags = 0);
Robert Phillips2c862492017-01-18 10:08:39 -050099
Greg Daniel6eb8c242019-06-05 10:22:24 -0400100#if GR_TEST_UTILS
101 // Accessors for tests to directly call read/writePixelsImpl
102 bool writePixels(GrContext* direct, int left, int top, int width, int height,
103 GrColorType srcColorType, SkColorSpace* srcColorSpace,
104 const void* srcBuffer, size_t srcRowBytes = 0, uint32_t pixelOpsFlags = 0) {
105 return writePixelsImpl(direct, left, top, width, height, srcColorType, srcColorSpace,
106 srcBuffer, srcRowBytes, pixelOpsFlags);
107 }
108
109 bool readPixels(GrContext* direct, int left, int top, int width, int height,
110 GrColorType dstColorType, SkColorSpace* dstColorSpace, void* buffer,
111 size_t rowBytes = 0, uint32_t pixelOpsFlags = 0) {
112 return readPixelsImpl(direct, left, top, width, height, dstColorType, dstColorSpace,
113 buffer, rowBytes, pixelOpsFlags);
114 }
115#endif
116
Robert Phillips27341362016-12-14 08:46:47 -0500117 // TODO: this is virtual b.c. this object doesn't have a pointer to the wrapped GrSurfaceProxy?
Robert Phillipsf200a902017-01-30 13:27:37 -0500118 virtual GrSurfaceProxy* asSurfaceProxy() = 0;
119 virtual const GrSurfaceProxy* asSurfaceProxy() const = 0;
120 virtual sk_sp<GrSurfaceProxy> asSurfaceProxyRef() = 0;
121
122 virtual GrTextureProxy* asTextureProxy() = 0;
Greg Daniele252f082017-10-23 16:05:23 -0400123 virtual const GrTextureProxy* asTextureProxy() const = 0;
Robert Phillipsf200a902017-01-30 13:27:37 -0500124 virtual sk_sp<GrTextureProxy> asTextureProxyRef() = 0;
125
126 virtual GrRenderTargetProxy* asRenderTargetProxy() = 0;
127 virtual sk_sp<GrRenderTargetProxy> asRenderTargetProxyRef() = 0;
Robert Phillips27341362016-12-14 08:46:47 -0500128
Robert Phillipsd46697a2017-01-25 12:10:37 -0500129 virtual GrRenderTargetContext* asRenderTargetContext() { return nullptr; }
130
Robert Phillips0d075de2019-03-04 11:08:13 -0500131 GrAuditTrail* auditTrail();
Brian Osman45580d32016-11-23 09:37:01 -0500132
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500133 // Provides access to functions that aren't part of the public API.
134 GrSurfaceContextPriv surfPriv();
135 const GrSurfaceContextPriv surfPriv() const;
136
Brian Osman45580d32016-11-23 09:37:01 -0500137protected:
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500138 friend class GrSurfaceContextPriv;
139
Robert Phillips0d075de2019-03-04 11:08:13 -0500140 GrSurfaceContext(GrRecordingContext*, GrPixelConfig, sk_sp<SkColorSpace>);
Robert Phillips72152832017-01-25 17:31:35 -0500141
Robert Phillips0d075de2019-03-04 11:08:13 -0500142 GrDrawingManager* drawingManager();
143 const GrDrawingManager* drawingManager() const;
Brian Osman45580d32016-11-23 09:37:01 -0500144
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400145 virtual GrOpList* getOpList() = 0;
146 SkDEBUGCODE(virtual void validate() const = 0;)
147
Robert Phillips0d075de2019-03-04 11:08:13 -0500148 SkDEBUGCODE(GrSingleOwner* singleOwner();)
Brian Osman45580d32016-11-23 09:37:01 -0500149
Robert Phillips69893702019-02-22 11:16:30 -0500150 GrRecordingContext* fContext;
Brian Osman45580d32016-11-23 09:37:01 -0500151
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500152private:
Greg Daniel6eb8c242019-06-05 10:22:24 -0400153 bool writePixelsImpl(GrContext* direct, int left, int top, int width, int height,
154 GrColorType srcColorType, SkColorSpace* srcColorSpace,
155 const void* srcBuffer, size_t srcRowBytes, uint32_t pixelOpsFlags);
156
157 bool readPixelsImpl(GrContext* direct, int left, int top, int width,
158 int height, GrColorType dstColorType,
159 SkColorSpace* dstColorSpace, void* buffer, size_t rowBytes,
160 uint32_t pixelOpsFlags);
161
Robert Phillips69893702019-02-22 11:16:30 -0500162 GrColorSpaceInfo fColorSpaceInfo;
Brian Salomonf3569f02017-10-24 12:52:33 -0400163
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500164 typedef SkRefCnt INHERITED;
Brian Osman45580d32016-11-23 09:37:01 -0500165};
166
167#endif