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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/ops/GrCopySurfaceOp.h" |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/private/GrRecordingContext.h" |
| 11 | #include "src/gpu/GrGpu.h" |
| 12 | #include "src/gpu/GrMemoryPool.h" |
| 13 | #include "src/gpu/GrRecordingContextPriv.h" |
Greg Daniel | 4c6f9b7 | 2019-06-06 13:40:59 -0400 | [diff] [blame] | 14 | |
| 15 | // returns true if the read/written rect intersects the src/dst and false if not. |
| 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) { |
| 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 | } |
bsalomon | 872062c | 2015-08-18 12:12:35 -0700 | [diff] [blame] | 65 | |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 66 | std::unique_ptr<GrOp> GrCopySurfaceOp::Make(GrRecordingContext* context, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 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. |
Greg Daniel | 4c6f9b7 | 2019-06-06 13:40:59 -0400 | [diff] [blame] | 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 | |
Greg Daniel | 4c6f9b7 | 2019-06-06 13:40:59 -0400 | [diff] [blame] | 86 | return pool->allocate<GrCopySurfaceOp>(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 | 12c4629 | 2019-04-23 07:36:17 -0400 | [diff] [blame] | 90 | SkASSERT(fSrc.get()->isInstantiated()); |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 91 | |
Greg Daniel | 4c6f9b7 | 2019-06-06 13:40:59 -0400 | [diff] [blame] | 92 | state->commandBuffer()->copy(fSrc.get()->peekSurface(), fSrc.get()->origin(), fSrcRect, |
| 93 | fDstPoint); |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 94 | } |