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
8 #include "GrCopySurfaceOp.h"
9
10 // returns true if the read/written rect intersects the src/dst and false if not.
ClipSrcRectAndDstPoint(const GrSurface * dst,const GrSurface * src,const SkIRect & srcRect,const SkIPoint & dstPoint,SkIRect * clippedSrcRect,SkIPoint * clippedDstPoint)11 bool GrCopySurfaceOp::ClipSrcRectAndDstPoint(const GrSurface* dst,
12 const GrSurface* src,
13 const SkIRect& srcRect,
14 const SkIPoint& dstPoint,
15 SkIRect* clippedSrcRect,
16 SkIPoint* clippedDstPoint) {
17 *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
Make(GrSurface * dst,GrSurface * src,const SkIRect & srcRect,const SkIPoint & dstPoint)61 std::unique_ptr<GrOp> GrCopySurfaceOp::Make(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
62 const SkIPoint& dstPoint) {
63 SkASSERT(dst);
64 SkASSERT(src);
65 if (GrPixelConfigIsSint(dst->config()) != GrPixelConfigIsSint(src->config())) {
66 return nullptr;
67 }
68 if (GrPixelConfigIsCompressed(dst->config())) {
69 return nullptr;
70 }
71 SkIRect clippedSrcRect;
72 SkIPoint clippedDstPoint;
73 // If the rect is outside the src or dst then we've already succeeded.
74 if (!ClipSrcRectAndDstPoint(dst, src, srcRect, dstPoint, &clippedSrcRect, &clippedDstPoint)) {
75 return nullptr;
76 }
77 return std::unique_ptr<GrOp>(new GrCopySurfaceOp(dst, src, clippedSrcRect, clippedDstPoint));
78 }
79