1/*
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
14sk_sp<GrRenderTask> GrCopyRenderTask::Make(GrSurfaceProxyView srcView,
15 const SkIRect& srcRect,
16 GrSurfaceProxyView dstView,
17 const SkIPoint& dstPoint,
18 const GrCaps* caps) {
19 SkASSERT(dstView.proxy());
20 SkASSERT(srcView.proxy());
21 SkIRect clippedSrcRect;
22 SkIPoint clippedDstPoint;
23 GrSurfaceProxy* srcProxy = srcView.proxy();
24 GrSurfaceProxy* dstProxy = dstView.proxy();
25 // If the rect is outside the srcProxy or dstProxy then we've already succeeded.
26 if (!GrClipSrcRectAndDstPoint(dstProxy->dimensions(), srcProxy->dimensions(), srcRect, dstPoint,
27 &clippedSrcRect, &clippedDstPoint)) {
28 return nullptr;
29 }
30
31 if (caps->isFormatCompressed(dstProxy->backendFormat())) {
32 return nullptr;
33 }
34
35 SkASSERT(dstView.origin() == srcView.origin());
36 if (srcView.origin() == kBottomLeft_GrSurfaceOrigin) {
37 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(
44 std::move(srcView), clippedSrcRect, std::move(dstView), clippedDstPoint));
45 return std::move(task);
46}
47
48GrCopyRenderTask::GrCopyRenderTask(GrSurfaceProxyView srcView,
49 const SkIRect& srcRect,
50 GrSurfaceProxyView dstView,
51 const SkIPoint& dstPoint)
52 : GrRenderTask(std::move(dstView))
53 , fSrcView(std::move(srcView))
54 , fSrcRect(srcRect)
55 , fDstPoint(dstPoint) {
56 fTargetView.proxy()->setLastRenderTask(this);
57}
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
61 // fEndOfOpsTaskOpIndices will remain in sync), so we create a fake op# to capture the fact that
62 // we read fSrcView and copy to fTargetView.
63 alloc->addInterval(fSrcView.proxy(), alloc->curOp(), alloc->curOp(),
64 GrResourceAllocator::ActualUse::kYes);
65 alloc->addInterval(fTargetView.proxy(), alloc->curOp(), alloc->curOp(),
66 GrResourceAllocator::ActualUse::kYes);
67 alloc->incOps();
68}
69
70bool GrCopyRenderTask::onExecute(GrOpFlushState* flushState) {
71 GrSurfaceProxy* dstProxy = fTargetView.proxy();
72 GrSurfaceProxy* srcProxy = fSrcView.proxy();
73 if (!srcProxy->isInstantiated() || !dstProxy->isInstantiated()) {
74 return false;
75 }
76 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());
81 }
82 if (dstProxy->height() != dstSurface->height()) {
83 fDstPoint.fY = fDstPoint.fY + (dstSurface->height() - dstProxy->height());
84 }
85 }
86 return flushState->gpu()->copySurface(dstSurface, srcSurface, fSrcRect, fDstPoint);
87}
88
89