blob: 8d8e9f15b5c157acb4d337faf076d038b2745ce8 [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"
Robert Phillips7c525e62018-06-12 10:11:12 -04009
10#include "GrContext.h"
11#include "GrContextPriv.h"
Robert Phillips646e4292017-06-13 12:44:56 -040012#include "GrGpu.h"
Robert Phillips7c525e62018-06-12 10:11:12 -040013#include "GrMemoryPool.h"
bsalomon872062c2015-08-18 12:12:35 -070014
15// returns true if the read/written rect intersects the src/dst and false if not.
Robert Phillipsbf25d432017-04-07 10:08:53 -040016static 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) {
bsalomon872062c2015-08-18 12:12:35 -070022 *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
Robert Phillips7c525e62018-06-12 10:11:12 -040066std::unique_ptr<GrOp> GrCopySurfaceOp::Make(GrContext* context,
67 GrSurfaceProxy* dstProxy,
68 GrSurfaceProxy* srcProxy,
Robert Phillipsbf25d432017-04-07 10:08:53 -040069 const SkIRect& srcRect,
Brian Salomonf8334782017-01-03 09:42:58 -050070 const SkIPoint& dstPoint) {
Robert Phillipsbf25d432017-04-07 10:08:53 -040071 SkASSERT(dstProxy);
72 SkASSERT(srcProxy);
bsalomon872062c2015-08-18 12:12:35 -070073 SkIRect clippedSrcRect;
74 SkIPoint clippedDstPoint;
Robert Phillipsbf25d432017-04-07 10:08:53 -040075 // 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)) {
halcanary96fcdcc2015-08-27 07:41:13 -070078 return nullptr;
bsalomon872062c2015-08-18 12:12:35 -070079 }
Jim Van Verth1676cb92019-01-15 13:24:45 -050080 if (GrPixelConfigIsCompressed(dstProxy->config())) {
81 return nullptr;
82 }
Robert Phillipsbf25d432017-04-07 10:08:53 -040083
Robert Phillips9da87e02019-02-04 13:26:26 -050084 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -040085
86 return pool->allocate<GrCopySurfaceOp>(dstProxy, srcProxy, clippedSrcRect, clippedDstPoint);
bsalomon872062c2015-08-18 12:12:35 -070087}
Robert Phillips646e4292017-06-13 12:44:56 -040088
Brian Salomon588cec72018-11-14 13:56:37 -050089void GrCopySurfaceOp::onExecute(GrOpFlushState* state, const SkRect& chainBounds) {
Robert Phillips47c315e2017-11-08 16:26:53 -050090 if (!fSrc.get()->instantiate(state->resourceProvider())) {
Robert Phillips646e4292017-06-13 12:44:56 -040091 return;
92 }
93
Brian Salomonfd98c2c2018-07-31 17:25:29 -040094 state->commandBuffer()->copy(fSrc.get()->peekSurface(), fSrc.get()->origin(), fSrcRect,
95 fDstPoint);
Robert Phillips646e4292017-06-13 12:44:56 -040096}