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 | |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 11 | #include "../private/GrSurfaceProxy.h" |
| 12 | |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 13 | #include "SkRefCnt.h" |
| 14 | |
| 15 | class GrAuditTrail; |
| 16 | class GrContext; |
Robert Phillips | 7215283 | 2017-01-25 17:31:35 -0500 | [diff] [blame] | 17 | class GrDrawingManager; |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 18 | class GrRenderTargetContext; |
Robert Phillips | 2734136 | 2016-12-14 08:46:47 -0500 | [diff] [blame] | 19 | class GrRenderTargetProxy; |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 20 | class GrSingleOwner; |
| 21 | class GrSurface; |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame] | 22 | class GrSurfaceContextPriv; |
Robert Phillips | 2734136 | 2016-12-14 08:46:47 -0500 | [diff] [blame] | 23 | class GrSurfaceProxy; |
| 24 | class GrTextureProxy; |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 25 | struct SkIPoint; |
| 26 | struct SkIRect; |
| 27 | |
| 28 | /** |
| 29 | * A helper object to orchestrate commands for a particular surface |
| 30 | */ |
| 31 | class SK_API GrSurfaceContext : public SkRefCnt { |
| 32 | public: |
| 33 | ~GrSurfaceContext() override {} |
| 34 | |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 35 | SkColorSpace* getColorSpace() const { return fColorSpace.get(); } |
| 36 | sk_sp<SkColorSpace> refColorSpace() const { return fColorSpace; } |
Matt Sarett | f388093 | 2017-03-24 10:06:03 -0400 | [diff] [blame] | 37 | bool isGammaCorrect() const { return fColorSpace; } |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 38 | |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 39 | // 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] | 40 | int width() const { return this->asSurfaceProxy()->width(); } |
| 41 | int height() const { return this->asSurfaceProxy()->height(); } |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 42 | |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 43 | /* |
| 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 Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 64 | |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 65 | /** |
| 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 Phillips | a90aa2b | 2017-04-10 08:19:26 -0400 | [diff] [blame] | 77 | int x, int y, uint32_t flags = 0); |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 78 | |
| 79 | /** |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 80 | * Writes a rectangle of pixels [srcInfo, srcBuffer, srcRowbytes] into the |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 81 | * 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 Phillips | a90aa2b | 2017-04-10 08:19:26 -0400 | [diff] [blame] | 92 | int x, int y, uint32_t flags = 0); |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 93 | |
Robert Phillips | 2734136 | 2016-12-14 08:46:47 -0500 | [diff] [blame] | 94 | // 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] | 95 | 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 Phillips | 2734136 | 2016-12-14 08:46:47 -0500 | [diff] [blame] | 104 | |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 105 | virtual GrRenderTargetContext* asRenderTargetContext() { return nullptr; } |
| 106 | |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 107 | GrAuditTrail* auditTrail() { return fAuditTrail; } |
| 108 | |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame] | 109 | // Provides access to functions that aren't part of the public API. |
| 110 | GrSurfaceContextPriv surfPriv(); |
| 111 | const GrSurfaceContextPriv surfPriv() const; |
| 112 | |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 113 | protected: |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame] | 114 | friend class GrSurfaceContextPriv; |
| 115 | |
Robert Phillips | 7215283 | 2017-01-25 17:31:35 -0500 | [diff] [blame] | 116 | GrSurfaceContext(GrContext*, GrDrawingManager*, |
| 117 | sk_sp<SkColorSpace>, GrAuditTrail*, GrSingleOwner*); |
| 118 | |
| 119 | GrDrawingManager* drawingManager() { return fDrawingManager; } |
| 120 | const GrDrawingManager* drawingManager() const { return fDrawingManager; } |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 121 | |
| 122 | SkDEBUGCODE(GrSingleOwner* singleOwner() { return fSingleOwner; }) |
| 123 | |
| 124 | GrContext* fContext; |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 125 | sk_sp<SkColorSpace> fColorSpace; |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 126 | GrAuditTrail* fAuditTrail; |
| 127 | |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame] | 128 | private: |
Robert Phillips | a90aa2b | 2017-04-10 08:19:26 -0400 | [diff] [blame] | 129 | virtual bool onCopy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) = 0; |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 130 | |
Robert Phillips | 7215283 | 2017-01-25 17:31:35 -0500 | [diff] [blame] | 131 | GrDrawingManager* fDrawingManager; |
| 132 | |
Robert Phillips | a90aa2b | 2017-04-10 08:19:26 -0400 | [diff] [blame] | 133 | // In debug builds we guard against improper thread handling |
| 134 | SkDEBUGCODE(mutable GrSingleOwner* fSingleOwner;) |
| 135 | |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame] | 136 | typedef SkRefCnt INHERITED; |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | #endif |