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 #include "GrGpu.h"
11 #include "GrMemoryPool.h"
12 #include "GrRecordingContext.h"
13 #include "GrRecordingContextPriv.h"
14 
15 // returns true if the read/written rect intersects the src/dst and false if not.
clip_src_rect_and_dst_point(const GrSurfaceProxy * dst,const GrSurfaceProxy * src,const SkIRect & srcRect,const SkIPoint & dstPoint,SkIRect * clippedSrcRect,SkIPoint * clippedDstPoint)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 }
65 
Make(GrRecordingContext * context,GrSurfaceProxy * dstProxy,GrSurfaceProxy * srcProxy,const SkIRect & srcRect,const SkIPoint & dstPoint)66 std::unique_ptr<GrOp> GrCopySurfaceOp::Make(GrRecordingContext* context,
67                                             GrSurfaceProxy* dstProxy,
68                                             GrSurfaceProxy* srcProxy,
69                                             const SkIRect& srcRect,
70                                             const SkIPoint& dstPoint) {
71     SkASSERT(dstProxy);
72     SkASSERT(srcProxy);
73     SkIRect clippedSrcRect;
74     SkIPoint clippedDstPoint;
75     // If the rect is outside the srcProxy or dstProxy then we've already succeeded.
76     if (!clip_src_rect_and_dst_point(dstProxy, srcProxy, srcRect, dstPoint,
77                                      &clippedSrcRect, &clippedDstPoint)) {
78         return nullptr;
79     }
80     if (GrPixelConfigIsCompressed(dstProxy->config())) {
81         return nullptr;
82     }
83 
84     GrOpMemoryPool* pool = context->priv().opMemoryPool();
85 
86     return pool->allocate<GrCopySurfaceOp>(dstProxy, srcProxy, clippedSrcRect, clippedDstPoint);
87 }
88 
onExecute(GrOpFlushState * state,const SkRect & chainBounds)89 void GrCopySurfaceOp::onExecute(GrOpFlushState* state, const SkRect& chainBounds) {
90     if (!fSrc.get()->instantiate(state->resourceProvider())) {
91         return;
92     }
93 
94     state->commandBuffer()->copy(fSrc.get()->peekSurface(), fSrc.get()->origin(), fSrcRect,
95                                  fDstPoint);
96 }
97