blob: 87c490b1105f77ad7bbef683287442ff84f9ce84 [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.
Brian Salomon7dae46a2016-12-14 16:21:37 -050011bool GrCopySurfaceOp::ClipSrcRectAndDstPoint(const GrSurface* dst,
12 const GrSurface* 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
Brian Salomonf8334782017-01-03 09:42:58 -050061std::unique_ptr<GrOp> GrCopySurfaceOp::Make(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
62 const SkIPoint& dstPoint) {
bsalomon872062c2015-08-18 12:12:35 -070063 SkASSERT(dst);
64 SkASSERT(src);
Brian Salomonbf7b6202016-11-11 16:08:03 -050065 if (GrPixelConfigIsSint(dst->config()) != GrPixelConfigIsSint(src->config())) {
66 return nullptr;
67 }
68 if (GrPixelConfigIsCompressed(dst->config())) {
69 return nullptr;
70 }
bsalomon872062c2015-08-18 12:12:35 -070071 SkIRect clippedSrcRect;
72 SkIPoint clippedDstPoint;
73 // If the rect is outside the src or dst then we've already succeeded.
bsalomonb8fea972016-02-16 07:34:17 -080074 if (!ClipSrcRectAndDstPoint(dst, src, srcRect, dstPoint, &clippedSrcRect, &clippedDstPoint)) {
halcanary96fcdcc2015-08-27 07:41:13 -070075 return nullptr;
bsalomon872062c2015-08-18 12:12:35 -070076 }
Brian Salomonf8334782017-01-03 09:42:58 -050077 return std::unique_ptr<GrOp>(new GrCopySurfaceOp(dst, src, clippedSrcRect, clippedDstPoint));
bsalomon872062c2015-08-18 12:12:35 -070078}