bsalomon | 872062c | 2015-08-18 12:12:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
Brian Salomon | 7dae46a | 2016-12-14 16:21:37 -0500 | [diff] [blame] | 8 | #include "GrCopySurfaceOp.h" |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 9 | |
| 10 | #include "GrContext.h" |
| 11 | #include "GrContextPriv.h" |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 12 | #include "GrGpu.h" |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 13 | #include "GrMemoryPool.h" |
bsalomon | 872062c | 2015-08-18 12:12:35 -0700 | [diff] [blame] | 14 | |
| 15 | // returns true if the read/written rect intersects the src/dst and false if not. |
Robert Phillips | bf25d43 | 2017-04-07 10:08:53 -0400 | [diff] [blame] | 16 | static bool clip_src_rect_and_dst_point(const GrSurfaceProxy* dst, |
| 17 | const GrSurfaceProxy* src, |
| 18 | const SkIRect& srcRect, |
| 19 | const SkIPoint& dstPoint, |
| 20 | SkIRect* clippedSrcRect, |
| 21 | SkIPoint* clippedDstPoint) { |
bsalomon | 872062c | 2015-08-18 12:12:35 -0700 | [diff] [blame] | 22 | *clippedSrcRect = srcRect; |
| 23 | *clippedDstPoint = dstPoint; |
| 24 | |
| 25 | // clip the left edge to src and dst bounds, adjusting dstPoint if necessary |
| 26 | if (clippedSrcRect->fLeft < 0) { |
| 27 | clippedDstPoint->fX -= clippedSrcRect->fLeft; |
| 28 | clippedSrcRect->fLeft = 0; |
| 29 | } |
| 30 | if (clippedDstPoint->fX < 0) { |
| 31 | clippedSrcRect->fLeft -= clippedDstPoint->fX; |
| 32 | clippedDstPoint->fX = 0; |
| 33 | } |
| 34 | |
| 35 | // clip the top edge to src and dst bounds, adjusting dstPoint if necessary |
| 36 | if (clippedSrcRect->fTop < 0) { |
| 37 | clippedDstPoint->fY -= clippedSrcRect->fTop; |
| 38 | clippedSrcRect->fTop = 0; |
| 39 | } |
| 40 | if (clippedDstPoint->fY < 0) { |
| 41 | clippedSrcRect->fTop -= clippedDstPoint->fY; |
| 42 | clippedDstPoint->fY = 0; |
| 43 | } |
| 44 | |
| 45 | // clip the right edge to the src and dst bounds. |
| 46 | if (clippedSrcRect->fRight > src->width()) { |
| 47 | clippedSrcRect->fRight = src->width(); |
| 48 | } |
| 49 | if (clippedDstPoint->fX + clippedSrcRect->width() > dst->width()) { |
| 50 | clippedSrcRect->fRight = clippedSrcRect->fLeft + dst->width() - clippedDstPoint->fX; |
| 51 | } |
| 52 | |
| 53 | // clip the bottom edge to the src and dst bounds. |
| 54 | if (clippedSrcRect->fBottom > src->height()) { |
| 55 | clippedSrcRect->fBottom = src->height(); |
| 56 | } |
| 57 | if (clippedDstPoint->fY + clippedSrcRect->height() > dst->height()) { |
| 58 | clippedSrcRect->fBottom = clippedSrcRect->fTop + dst->height() - clippedDstPoint->fY; |
| 59 | } |
| 60 | |
| 61 | // The above clipping steps may have inverted the rect if it didn't intersect either the src or |
| 62 | // dst bounds. |
| 63 | return !clippedSrcRect->isEmpty(); |
| 64 | } |
| 65 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 66 | std::unique_ptr<GrOp> GrCopySurfaceOp::Make(GrContext* context, |
| 67 | GrSurfaceProxy* dstProxy, |
| 68 | GrSurfaceProxy* srcProxy, |
Robert Phillips | bf25d43 | 2017-04-07 10:08:53 -0400 | [diff] [blame] | 69 | const SkIRect& srcRect, |
Brian Salomon | f833478 | 2017-01-03 09:42:58 -0500 | [diff] [blame] | 70 | const SkIPoint& dstPoint) { |
Robert Phillips | bf25d43 | 2017-04-07 10:08:53 -0400 | [diff] [blame] | 71 | SkASSERT(dstProxy); |
| 72 | SkASSERT(srcProxy); |
bsalomon | 872062c | 2015-08-18 12:12:35 -0700 | [diff] [blame] | 73 | SkIRect clippedSrcRect; |
| 74 | SkIPoint clippedDstPoint; |
Robert Phillips | bf25d43 | 2017-04-07 10:08:53 -0400 | [diff] [blame] | 75 | // If the rect is outside the srcProxy or dstProxy then we've already succeeded. |
| 76 | if (!clip_src_rect_and_dst_point(dstProxy, srcProxy, srcRect, dstPoint, |
| 77 | &clippedSrcRect, &clippedDstPoint)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 78 | return nullptr; |
bsalomon | 872062c | 2015-08-18 12:12:35 -0700 | [diff] [blame] | 79 | } |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 80 | if (GrPixelConfigIsCompressed(dstProxy->config())) { |
| 81 | return nullptr; |
| 82 | } |
Robert Phillips | bf25d43 | 2017-04-07 10:08:53 -0400 | [diff] [blame] | 83 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 84 | GrOpMemoryPool* pool = context->priv().opMemoryPool(); |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 85 | |
| 86 | return pool->allocate<GrCopySurfaceOp>(dstProxy, srcProxy, clippedSrcRect, clippedDstPoint); |
bsalomon | 872062c | 2015-08-18 12:12:35 -0700 | [diff] [blame] | 87 | } |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 88 | |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 89 | void GrCopySurfaceOp::onExecute(GrOpFlushState* state, const SkRect& chainBounds) { |
Robert Phillips | 47c315e | 2017-11-08 16:26:53 -0500 | [diff] [blame] | 90 | if (!fSrc.get()->instantiate(state->resourceProvider())) { |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 91 | return; |
| 92 | } |
| 93 | |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 94 | state->commandBuffer()->copy(fSrc.get()->peekSurface(), fSrc.get()->origin(), fSrcRect, |
| 95 | fDstPoint); |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 96 | } |