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