blob: 39fa454fd4454ee42826d06195e706ea343aa5b1 [file] [log] [blame]
Greg Daniele227fe42019-08-21 13:52:24 -04001/*
2 * Copyright 2019 Google LLC
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 "src/gpu/GrCopyRenderTask.h"
9
10#include "src/gpu/GrGpu.h"
11#include "src/gpu/GrOpFlushState.h"
12#include "src/gpu/GrResourceAllocator.h"
13
Greg Daniel16f5c652019-10-29 11:26:01 -040014sk_sp<GrRenderTask> GrCopyRenderTask::Make(GrSurfaceProxyView srcView,
Greg Daniele227fe42019-08-21 13:52:24 -040015 const SkIRect& srcRect,
Greg Daniel16f5c652019-10-29 11:26:01 -040016 GrSurfaceProxyView dstView,
Brian Salomone4bce012019-09-20 15:34:23 -040017 const SkIPoint& dstPoint,
18 const GrCaps* caps) {
Greg Daniel16f5c652019-10-29 11:26:01 -040019 SkASSERT(dstView.proxy());
20 SkASSERT(srcView.proxy());
Greg Daniele227fe42019-08-21 13:52:24 -040021 SkIRect clippedSrcRect;
22 SkIPoint clippedDstPoint;
Greg Daniel16f5c652019-10-29 11:26:01 -040023 GrSurfaceProxy* srcProxy = srcView.proxy();
24 GrSurfaceProxy* dstProxy = dstView.proxy();
Greg Daniele227fe42019-08-21 13:52:24 -040025 // If the rect is outside the srcProxy or dstProxy then we've already succeeded.
Brian Salomon9f2b86c2019-10-22 10:37:46 -040026 if (!GrClipSrcRectAndDstPoint(dstProxy->dimensions(), srcProxy->dimensions(), srcRect, dstPoint,
Greg Daniele227fe42019-08-21 13:52:24 -040027 &clippedSrcRect, &clippedDstPoint)) {
28 return nullptr;
29 }
Brian Salomone4bce012019-09-20 15:34:23 -040030
31 if (caps->isFormatCompressed(dstProxy->backendFormat())) {
Greg Daniele227fe42019-08-21 13:52:24 -040032 return nullptr;
33 }
34
Greg Daniel16f5c652019-10-29 11:26:01 -040035 SkASSERT(dstView.origin() == srcView.origin());
36 if (srcView.origin() == kBottomLeft_GrSurfaceOrigin) {
Greg Daniele227fe42019-08-21 13:52:24 -040037 int rectHeight = clippedSrcRect.height();
38 clippedSrcRect.fTop = srcProxy->height() - clippedSrcRect.fBottom;
39 clippedSrcRect.fBottom = clippedSrcRect.fTop + rectHeight;
40 clippedDstPoint.fY = dstProxy->height() - clippedDstPoint.fY - rectHeight;
41 }
42
43 sk_sp<GrCopyRenderTask> task(new GrCopyRenderTask(
Greg Daniel16f5c652019-10-29 11:26:01 -040044 std::move(srcView), clippedSrcRect, std::move(dstView), clippedDstPoint));
Greg Daniele227fe42019-08-21 13:52:24 -040045 return task;
46}
47
Greg Daniel16f5c652019-10-29 11:26:01 -040048GrCopyRenderTask::GrCopyRenderTask(GrSurfaceProxyView srcView,
Greg Daniele227fe42019-08-21 13:52:24 -040049 const SkIRect& srcRect,
Greg Daniel16f5c652019-10-29 11:26:01 -040050 GrSurfaceProxyView dstView,
Greg Daniele227fe42019-08-21 13:52:24 -040051 const SkIPoint& dstPoint)
Greg Daniel16f5c652019-10-29 11:26:01 -040052 : GrRenderTask(std::move(dstView))
53 , fSrcView(std::move(srcView))
Greg Daniele227fe42019-08-21 13:52:24 -040054 , fSrcRect(srcRect)
55 , fDstPoint(dstPoint) {
Greg Daniel16f5c652019-10-29 11:26:01 -040056 fTargetView.proxy()->setLastRenderTask(this);
Greg Daniele227fe42019-08-21 13:52:24 -040057}
58
59void GrCopyRenderTask::gatherProxyIntervals(GrResourceAllocator* alloc) const {
60 // This renderTask doesn't have "normal" ops. In this case we still need to add an interval (so
Greg Danielf41b2bd2019-08-22 16:19:24 -040061 // fEndOfOpsTaskOpIndices will remain in sync), so we create a fake op# to capture the fact that
Greg Daniel16f5c652019-10-29 11:26:01 -040062 // we read fSrcView and copy to fTargetView.
63 alloc->addInterval(fSrcView.proxy(), alloc->curOp(), alloc->curOp(),
Greg Daniele227fe42019-08-21 13:52:24 -040064 GrResourceAllocator::ActualUse::kYes);
Greg Daniel16f5c652019-10-29 11:26:01 -040065 alloc->addInterval(fTargetView.proxy(), alloc->curOp(), alloc->curOp(),
Greg Daniele227fe42019-08-21 13:52:24 -040066 GrResourceAllocator::ActualUse::kYes);
67 alloc->incOps();
68}
69
70bool GrCopyRenderTask::onExecute(GrOpFlushState* flushState) {
Greg Daniel16f5c652019-10-29 11:26:01 -040071 GrSurfaceProxy* dstProxy = fTargetView.proxy();
72 GrSurfaceProxy* srcProxy = fSrcView.proxy();
73 if (!srcProxy->isInstantiated() || !dstProxy->isInstantiated()) {
Greg Daniele227fe42019-08-21 13:52:24 -040074 return false;
75 }
Greg Daniel16f5c652019-10-29 11:26:01 -040076 GrSurface* srcSurface = srcProxy->peekSurface();
77 GrSurface* dstSurface = dstProxy->peekSurface();
78 if (fSrcView.origin() == kBottomLeft_GrSurfaceOrigin) {
79 if (srcProxy->height() != srcSurface->height()) {
80 fSrcRect.offset(0, srcSurface->height() - srcProxy->height());
Greg Daniele227fe42019-08-21 13:52:24 -040081 }
Greg Daniel16f5c652019-10-29 11:26:01 -040082 if (dstProxy->height() != dstSurface->height()) {
83 fDstPoint.fY = fDstPoint.fY + (dstSurface->height() - dstProxy->height());
Greg Daniele227fe42019-08-21 13:52:24 -040084 }
85 }
86 return flushState->gpu()->copySurface(dstSurface, srcSurface, fSrcRect, fDstPoint);
87}
88