blob: 1a9a5e0dfadde1a55bb7997060f46eb166eb51bc [file] [log] [blame]
bsalomon872062c2015-08-18 12:12:35 -07001/*
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 Salomon7dae46a2016-12-14 16:21:37 -05008#include "GrCopySurfaceOp.h"
bsalomon872062c2015-08-18 12:12:35 -07009
10// returns true if the read/written rect intersects the src/dst and false if not.
Robert Phillipsbf25d432017-04-07 10:08:53 -040011static bool clip_src_rect_and_dst_point(const GrSurfaceProxy* dst,
12 const GrSurfaceProxy* src,
13 const SkIRect& srcRect,
14 const SkIPoint& dstPoint,
15 SkIRect* clippedSrcRect,
16 SkIPoint* clippedDstPoint) {
bsalomon872062c2015-08-18 12:12:35 -070017 *clippedSrcRect = srcRect;
18 *clippedDstPoint = dstPoint;
19
20 // clip the left edge to src and dst bounds, adjusting dstPoint if necessary
21 if (clippedSrcRect->fLeft < 0) {
22 clippedDstPoint->fX -= clippedSrcRect->fLeft;
23 clippedSrcRect->fLeft = 0;
24 }
25 if (clippedDstPoint->fX < 0) {
26 clippedSrcRect->fLeft -= clippedDstPoint->fX;
27 clippedDstPoint->fX = 0;
28 }
29
30 // clip the top edge to src and dst bounds, adjusting dstPoint if necessary
31 if (clippedSrcRect->fTop < 0) {
32 clippedDstPoint->fY -= clippedSrcRect->fTop;
33 clippedSrcRect->fTop = 0;
34 }
35 if (clippedDstPoint->fY < 0) {
36 clippedSrcRect->fTop -= clippedDstPoint->fY;
37 clippedDstPoint->fY = 0;
38 }
39
40 // clip the right edge to the src and dst bounds.
41 if (clippedSrcRect->fRight > src->width()) {
42 clippedSrcRect->fRight = src->width();
43 }
44 if (clippedDstPoint->fX + clippedSrcRect->width() > dst->width()) {
45 clippedSrcRect->fRight = clippedSrcRect->fLeft + dst->width() - clippedDstPoint->fX;
46 }
47
48 // clip the bottom edge to the src and dst bounds.
49 if (clippedSrcRect->fBottom > src->height()) {
50 clippedSrcRect->fBottom = src->height();
51 }
52 if (clippedDstPoint->fY + clippedSrcRect->height() > dst->height()) {
53 clippedSrcRect->fBottom = clippedSrcRect->fTop + dst->height() - clippedDstPoint->fY;
54 }
55
56 // The above clipping steps may have inverted the rect if it didn't intersect either the src or
57 // dst bounds.
58 return !clippedSrcRect->isEmpty();
59}
60
Robert Phillipsa16f6cb2017-06-01 11:06:13 -040061std::unique_ptr<GrOp> GrCopySurfaceOp::Make(GrSurfaceProxy* dstProxy, GrSurfaceProxy* srcProxy,
Robert Phillipsbf25d432017-04-07 10:08:53 -040062 const SkIRect& srcRect,
Brian Salomonf8334782017-01-03 09:42:58 -050063 const SkIPoint& dstPoint) {
Robert Phillipsbf25d432017-04-07 10:08:53 -040064 SkASSERT(dstProxy);
65 SkASSERT(srcProxy);
66 if (GrPixelConfigIsSint(dstProxy->config()) != GrPixelConfigIsSint(srcProxy->config())) {
Brian Salomonbf7b6202016-11-11 16:08:03 -050067 return nullptr;
68 }
bsalomon872062c2015-08-18 12:12:35 -070069 SkIRect clippedSrcRect;
70 SkIPoint clippedDstPoint;
Robert Phillipsbf25d432017-04-07 10:08:53 -040071 // If the rect is outside the srcProxy or dstProxy then we've already succeeded.
72 if (!clip_src_rect_and_dst_point(dstProxy, srcProxy, srcRect, dstPoint,
73 &clippedSrcRect, &clippedDstPoint)) {
halcanary96fcdcc2015-08-27 07:41:13 -070074 return nullptr;
bsalomon872062c2015-08-18 12:12:35 -070075 }
Robert Phillipsbf25d432017-04-07 10:08:53 -040076
Robert Phillipsa16f6cb2017-06-01 11:06:13 -040077 return std::unique_ptr<GrOp>(new GrCopySurfaceOp(dstProxy, srcProxy,
Robert Phillipsbf25d432017-04-07 10:08:53 -040078 clippedSrcRect, clippedDstPoint));
bsalomon872062c2015-08-18 12:12:35 -070079}