Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkRefCnt.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "src/gpu/GrColorSpaceInfo.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 13 | #include "src/gpu/GrSurfaceProxy.h" |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 14 | |
| 15 | class GrAuditTrail; |
Robert Phillips | 7215283 | 2017-01-25 17:31:35 -0500 | [diff] [blame] | 16 | class GrDrawingManager; |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 17 | class GrOpList; |
Robert Phillips | 6989370 | 2019-02-22 11:16:30 -0500 | [diff] [blame] | 18 | class GrRecordingContext; |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 19 | class GrRenderTargetContext; |
Robert Phillips | 2734136 | 2016-12-14 08:46:47 -0500 | [diff] [blame] | 20 | class GrRenderTargetProxy; |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 21 | class GrSingleOwner; |
| 22 | class GrSurface; |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame] | 23 | class GrSurfaceContextPriv; |
Robert Phillips | 2734136 | 2016-12-14 08:46:47 -0500 | [diff] [blame] | 24 | class GrSurfaceProxy; |
| 25 | class GrTextureProxy; |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 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 | |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 36 | const GrColorSpaceInfo& colorSpaceInfo() const { return fColorSpaceInfo; } |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 37 | |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 38 | // TODO: these two calls would be way cooler if this object had a GrSurfaceProxy pointer |
Robert Phillips | f200a90 | 2017-01-30 13:27:37 -0500 | [diff] [blame] | 39 | int width() const { return this->asSurfaceProxy()->width(); } |
| 40 | int height() const { return this->asSurfaceProxy()->height(); } |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 41 | |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 42 | /** |
| 43 | * These flags can be used with the read/write pixels functions below. |
| 44 | */ |
| 45 | enum PixelOpsFlags { |
| 46 | /** The src for write or dst read is unpremultiplied. This is only respected if both the |
| 47 | config src and dst configs are an RGBA/BGRA 8888 format. */ |
| 48 | kUnpremul_PixelOpsFlag = 0x4, |
| 49 | }; |
| 50 | |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 51 | /** |
| 52 | * Reads a rectangle of pixels from the render target context. |
| 53 | * @param dstInfo image info for the destination |
| 54 | * @param dstBuffer destination pixels for the read |
| 55 | * @param dstRowBytes bytes in a row of 'dstBuffer' |
| 56 | * @param x x offset w/in the render target context from which to read |
| 57 | * @param y y offset w/in the render target context from which to read |
| 58 | * |
| 59 | * @return true if the read succeeded, false if not. The read can fail because of an |
| 60 | * unsupported pixel config. |
| 61 | */ |
| 62 | bool readPixels(const SkImageInfo& dstInfo, void* dstBuffer, size_t dstRowBytes, |
Robert Phillips | a90aa2b | 2017-04-10 08:19:26 -0400 | [diff] [blame] | 63 | int x, int y, uint32_t flags = 0); |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 64 | |
| 65 | /** |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 66 | * Writes a rectangle of pixels [srcInfo, srcBuffer, srcRowbytes] into the |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 67 | * renderTargetContext at the specified position. |
| 68 | * @param srcInfo image info for the source pixels |
| 69 | * @param srcBuffer source for the write |
| 70 | * @param srcRowBytes bytes in a row of 'srcBuffer' |
| 71 | * @param x x offset w/in the render target context at which to write |
| 72 | * @param y y offset w/in the render target context at which to write |
| 73 | * |
| 74 | * @return true if the write succeeded, false if not. The write can fail because of an |
| 75 | * unsupported pixel config. |
| 76 | */ |
| 77 | bool writePixels(const SkImageInfo& srcInfo, const void* srcBuffer, size_t srcRowBytes, |
Robert Phillips | a90aa2b | 2017-04-10 08:19:26 -0400 | [diff] [blame] | 78 | int x, int y, uint32_t flags = 0); |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 79 | |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 80 | #if GR_TEST_UTILS |
| 81 | // Accessors for tests to directly call read/writePixelsImpl |
| 82 | bool writePixels(GrContext* direct, int left, int top, int width, int height, |
| 83 | GrColorType srcColorType, SkColorSpace* srcColorSpace, |
| 84 | const void* srcBuffer, size_t srcRowBytes = 0, uint32_t pixelOpsFlags = 0) { |
| 85 | return writePixelsImpl(direct, left, top, width, height, srcColorType, srcColorSpace, |
| 86 | srcBuffer, srcRowBytes, pixelOpsFlags); |
| 87 | } |
| 88 | |
| 89 | bool readPixels(GrContext* direct, int left, int top, int width, int height, |
| 90 | GrColorType dstColorType, SkColorSpace* dstColorSpace, void* buffer, |
| 91 | size_t rowBytes = 0, uint32_t pixelOpsFlags = 0) { |
| 92 | return readPixelsImpl(direct, left, top, width, height, dstColorType, dstColorSpace, |
| 93 | buffer, rowBytes, pixelOpsFlags); |
| 94 | } |
| 95 | #endif |
| 96 | |
Robert Phillips | 2734136 | 2016-12-14 08:46:47 -0500 | [diff] [blame] | 97 | // TODO: this is virtual b.c. this object doesn't have a pointer to the wrapped GrSurfaceProxy? |
Robert Phillips | f200a90 | 2017-01-30 13:27:37 -0500 | [diff] [blame] | 98 | virtual GrSurfaceProxy* asSurfaceProxy() = 0; |
| 99 | virtual const GrSurfaceProxy* asSurfaceProxy() const = 0; |
| 100 | virtual sk_sp<GrSurfaceProxy> asSurfaceProxyRef() = 0; |
| 101 | |
| 102 | virtual GrTextureProxy* asTextureProxy() = 0; |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 103 | virtual const GrTextureProxy* asTextureProxy() const = 0; |
Robert Phillips | f200a90 | 2017-01-30 13:27:37 -0500 | [diff] [blame] | 104 | virtual sk_sp<GrTextureProxy> asTextureProxyRef() = 0; |
| 105 | |
| 106 | virtual GrRenderTargetProxy* asRenderTargetProxy() = 0; |
| 107 | virtual sk_sp<GrRenderTargetProxy> asRenderTargetProxyRef() = 0; |
Robert Phillips | 2734136 | 2016-12-14 08:46:47 -0500 | [diff] [blame] | 108 | |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 109 | virtual GrRenderTargetContext* asRenderTargetContext() { return nullptr; } |
| 110 | |
Robert Phillips | 0d075de | 2019-03-04 11:08:13 -0500 | [diff] [blame] | 111 | GrAuditTrail* auditTrail(); |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 112 | |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame] | 113 | // Provides access to functions that aren't part of the public API. |
| 114 | GrSurfaceContextPriv surfPriv(); |
| 115 | const GrSurfaceContextPriv surfPriv() const; |
| 116 | |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 117 | #if GR_TEST_UTILS |
| 118 | bool testCopy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) { |
| 119 | return this->copy(src, srcRect, dstPoint); |
| 120 | } |
| 121 | |
| 122 | bool testCopy(GrSurfaceProxy* src) { |
| 123 | return this->copy(src); |
| 124 | } |
| 125 | #endif |
| 126 | |
| 127 | |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 128 | protected: |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame] | 129 | friend class GrSurfaceContextPriv; |
| 130 | |
Brian Salomon | e7499c7 | 2019-06-24 12:12:36 -0400 | [diff] [blame] | 131 | GrSurfaceContext(GrRecordingContext*, SkAlphaType, sk_sp<SkColorSpace>, GrPixelConfig); |
Robert Phillips | 7215283 | 2017-01-25 17:31:35 -0500 | [diff] [blame] | 132 | |
Robert Phillips | 0d075de | 2019-03-04 11:08:13 -0500 | [diff] [blame] | 133 | GrDrawingManager* drawingManager(); |
| 134 | const GrDrawingManager* drawingManager() const; |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 135 | |
Robert Phillips | 2de8cfa | 2017-06-28 10:33:41 -0400 | [diff] [blame] | 136 | virtual GrOpList* getOpList() = 0; |
| 137 | SkDEBUGCODE(virtual void validate() const = 0;) |
| 138 | |
Robert Phillips | 0d075de | 2019-03-04 11:08:13 -0500 | [diff] [blame] | 139 | SkDEBUGCODE(GrSingleOwner* singleOwner();) |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 140 | |
Robert Phillips | 6989370 | 2019-02-22 11:16:30 -0500 | [diff] [blame] | 141 | GrRecordingContext* fContext; |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 142 | |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame] | 143 | private: |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 144 | friend class GrSurfaceProxy; // for copy |
| 145 | |
| 146 | /** |
| 147 | * Copy 'src' into the proxy backing this context. This call will not do any draw fallback. |
| 148 | * Currently only writePixels and replaceRenderTarget call this directly. All other copies |
| 149 | * should go through GrSurfaceProxy::Copy. |
| 150 | * @param src src of pixels |
| 151 | * @param srcRect the subset of 'src' to copy |
| 152 | * @param dstPoint the origin of the 'srcRect' in the destination coordinate space |
| 153 | * @return true if the copy succeeded; false otherwise |
| 154 | * |
| 155 | * Note: Notionally, 'srcRect' is clipped to 'src's extent with 'dstPoint' being adjusted. |
| 156 | * Then the 'srcRect' offset by 'dstPoint' is clipped against the dst's extent. |
| 157 | * The end result is only valid src pixels and dst pixels will be touched but the copied |
| 158 | * regions will not be shifted. The 'src' must have the same origin as the backing proxy |
| 159 | * of fSurfaceContext. |
| 160 | */ |
| 161 | bool copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint); |
| 162 | |
| 163 | bool copy(GrSurfaceProxy* src) { |
| 164 | return this->copy(src, SkIRect::MakeWH(src->width(), src->height()), SkIPoint::Make(0, 0)); |
| 165 | } |
| 166 | |
Greg Daniel | 6eb8c24 | 2019-06-05 10:22:24 -0400 | [diff] [blame] | 167 | bool writePixelsImpl(GrContext* direct, int left, int top, int width, int height, |
| 168 | GrColorType srcColorType, SkColorSpace* srcColorSpace, |
| 169 | const void* srcBuffer, size_t srcRowBytes, uint32_t pixelOpsFlags); |
| 170 | |
| 171 | bool readPixelsImpl(GrContext* direct, int left, int top, int width, |
| 172 | int height, GrColorType dstColorType, |
| 173 | SkColorSpace* dstColorSpace, void* buffer, size_t rowBytes, |
| 174 | uint32_t pixelOpsFlags); |
| 175 | |
Robert Phillips | 6989370 | 2019-02-22 11:16:30 -0500 | [diff] [blame] | 176 | GrColorSpaceInfo fColorSpaceInfo; |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 177 | |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame] | 178 | typedef SkRefCnt INHERITED; |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | #endif |