blob: 1882e3c7703c470f6dd33ca3cd280c11ace3ff0b [file] [log] [blame]
Brian Osman45580d32016-11-23 09:37:01 -05001/*
2 * Copyright 2016 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
Greg Daniel46cfbc62019-06-07 11:43:30 -04008#include "src/gpu/GrSurfaceContext.h"
9
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/private/GrRecordingContext.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040011#include "src/core/SkAutoPixmapStorage.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040012#include "src/gpu/GrAuditTrail.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040013#include "src/gpu/GrClip.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/GrContextPriv.h"
Brian Salomonf30b1c12019-06-20 12:25:02 -040015#include "src/gpu/GrDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/GrDrawingManager.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040017#include "src/gpu/GrGpu.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040018#include "src/gpu/GrOpList.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/GrRecordingContextPriv.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040020#include "src/gpu/GrRenderTargetContext.h"
Greg Daniel46cfbc62019-06-07 11:43:30 -040021#include "src/gpu/GrSurfaceContextPriv.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040022#include "src/gpu/GrSurfacePriv.h"
23#include "src/gpu/GrTextureContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/SkGr.h"
Brian Osman45580d32016-11-23 09:37:01 -050025
Robert Phillips2de8cfa2017-06-28 10:33:41 -040026#define ASSERT_SINGLE_OWNER \
27 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
Robert Phillips69893702019-02-22 11:16:30 -050028#define RETURN_FALSE_IF_ABANDONED if (this->fContext->priv().abandoned()) { return false; }
Brian Osman45580d32016-11-23 09:37:01 -050029
30// In MDB mode the reffing of the 'getLastOpList' call's result allows in-progress
31// GrOpLists to be picked up and added to by renderTargetContexts lower in the call
32// stack. When this occurs with a closed GrOpList, a new one will be allocated
33// when the renderTargetContext attempts to use it (via getOpList).
Robert Phillips69893702019-02-22 11:16:30 -050034GrSurfaceContext::GrSurfaceContext(GrRecordingContext* context,
Brian Salomond6287472019-06-24 15:50:07 -040035 GrColorType colorType,
Brian Salomone7499c72019-06-24 12:12:36 -040036 SkAlphaType alphaType,
Brian Salomonbd3d8d32019-07-02 09:16:28 -040037 sk_sp<SkColorSpace> colorSpace)
38 : fContext(context), fColorSpaceInfo(colorType, alphaType, std::move(colorSpace)) {}
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040039
Robert Phillips0d075de2019-03-04 11:08:13 -050040GrAuditTrail* GrSurfaceContext::auditTrail() {
41 return fContext->priv().auditTrail();
42}
43
44GrDrawingManager* GrSurfaceContext::drawingManager() {
45 return fContext->priv().drawingManager();
46}
47
48const GrDrawingManager* GrSurfaceContext::drawingManager() const {
49 return fContext->priv().drawingManager();
50}
51
52#ifdef SK_DEBUG
53GrSingleOwner* GrSurfaceContext::singleOwner() {
54 return fContext->priv().singleOwner();
55}
56#endif
Greg Daniel6eb8c242019-06-05 10:22:24 -040057
Brian Salomon1d435302019-07-01 13:05:28 -040058bool GrSurfaceContext::readPixels(const GrPixelInfo& origDstInfo, void* dst, size_t rowBytes,
59 SkIPoint pt, GrContext* direct) {
60 ASSERT_SINGLE_OWNER
61 RETURN_FALSE_IF_ABANDONED
62 SkDEBUGCODE(this->validate();)
63 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::readPixels");
Greg Daniel6eb8c242019-06-05 10:22:24 -040064
Brian Salomon1d435302019-07-01 13:05:28 -040065 if (!direct && !(direct = fContext->priv().asDirectContext())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -040066 return false;
67 }
Greg Daniel6eb8c242019-06-05 10:22:24 -040068
Brian Salomon1d435302019-07-01 13:05:28 -040069 if (!dst) {
70 return false;
71 }
72
Brian Salomon1047a492019-07-02 12:25:21 -040073 size_t tightRowBytes = origDstInfo.minRowBytes();
Brian Salomon1d435302019-07-01 13:05:28 -040074 if (!rowBytes) {
Brian Salomon1047a492019-07-02 12:25:21 -040075 rowBytes = tightRowBytes;
76 } else if (rowBytes < tightRowBytes) {
Brian Salomon1d435302019-07-01 13:05:28 -040077 return false;
78 }
79
80 if (!origDstInfo.isValid()) {
81 return false;
82 }
Greg Daniel6eb8c242019-06-05 10:22:24 -040083
84 GrSurfaceProxy* srcProxy = this->asSurfaceProxy();
85
86 // MDB TODO: delay this instantiation until later in the method
87 if (!srcProxy->instantiate(direct->priv().resourceProvider())) {
88 return false;
89 }
90
91 GrSurface* srcSurface = srcProxy->peekSurface();
92
Brian Salomon1d435302019-07-01 13:05:28 -040093 auto dstInfo = origDstInfo;
94 if (!dstInfo.clip(this->width(), this->height(), &pt, &dst, rowBytes)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -040095 return false;
96 }
Brian Salomon1047a492019-07-02 12:25:21 -040097 // Our tight row bytes may have been changed by clipping.
98 tightRowBytes = dstInfo.minRowBytes();
Greg Daniel6eb8c242019-06-05 10:22:24 -040099
Brian Salomon1d435302019-07-01 13:05:28 -0400100 bool premul = this->colorSpaceInfo().alphaType() == kUnpremul_SkAlphaType &&
101 dstInfo.alphaType() == kPremul_SkAlphaType;
102 bool unpremul = this->colorSpaceInfo().alphaType() == kPremul_SkAlphaType &&
103 dstInfo.alphaType() == kUnpremul_SkAlphaType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400104
Brian Salomon1d435302019-07-01 13:05:28 -0400105 bool needColorConversion = SkColorSpaceXformSteps::Required(this->colorSpaceInfo().colorSpace(),
106 dstInfo.colorSpace());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400107
108 const GrCaps* caps = direct->priv().caps();
109 // This is the getImageData equivalent to the canvas2D putImageData fast path. We probably don't
110 // care so much about getImageData performance. However, in order to ensure putImageData/
111 // getImageData in "legacy" mode are round-trippable we use the GPU to do the complementary
112 // unpremul step to writeSurfacePixels's premul step (which is determined empirically in
113 // fContext->vaildaPMUPMConversionExists()).
Brian Salomon1d435302019-07-01 13:05:28 -0400114 bool canvas2DFastPath = unpremul && !needColorConversion &&
115 (GrColorType::kRGBA_8888 == dstInfo.colorType() ||
116 GrColorType::kBGRA_8888 == dstInfo.colorType()) &&
117 SkToBool(srcProxy->asTextureProxy()) &&
118 (srcProxy->config() == kRGBA_8888_GrPixelConfig ||
119 srcProxy->config() == kBGRA_8888_GrPixelConfig) &&
120 caps->isConfigRenderable(kRGBA_8888_GrPixelConfig) &&
121 direct->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400122
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400123 auto readFlag = caps->surfaceSupportsReadPixels(srcSurface);
Brian Salomondc0710f2019-07-01 14:59:32 -0400124 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kUnsupported) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400125 return false;
126 }
127
Brian Salomondc0710f2019-07-01 14:59:32 -0400128 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kCopyToTexture2D || canvas2DFastPath) {
Brian Salomon27ae52c2019-07-03 11:27:44 -0400129 GrColorType colorType = canvas2DFastPath ? GrColorType::kRGBA_8888
130 : this->colorSpaceInfo().colorType();
131 sk_sp<SkColorSpace> cs = canvas2DFastPath ? nullptr
132 : this->colorSpaceInfo().refColorSpace();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400133
134 sk_sp<GrRenderTargetContext> tempCtx = direct->priv().makeDeferredRenderTargetContext(
Brian Salomon27ae52c2019-07-03 11:27:44 -0400135 SkBackingFit::kApprox, dstInfo.width(), dstInfo.height(), colorType, std::move(cs),
136 1, GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin, nullptr, SkBudgeted::kYes);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400137 if (!tempCtx) {
138 return false;
139 }
140
141 std::unique_ptr<GrFragmentProcessor> fp;
142 if (canvas2DFastPath) {
143 fp = direct->priv().createPMToUPMEffect(
144 GrSimpleTextureEffect::Make(sk_ref_sp(srcProxy->asTextureProxy()),
145 SkMatrix::I()));
Brian Salomon1d435302019-07-01 13:05:28 -0400146 if (dstInfo.colorType() == GrColorType::kBGRA_8888) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400147 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
Brian Salomon1d435302019-07-01 13:05:28 -0400148 dstInfo = dstInfo.makeColorType(GrColorType::kRGBA_8888);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400149 }
Brian Salomon1d435302019-07-01 13:05:28 -0400150 // The render target context is incorrectly tagged as kPremul even though we're writing
151 // unpremul data thanks to the PMToUPM effect. Fake out the dst alpha type so we don't
152 // double unpremul.
153 dstInfo = dstInfo.makeAlphaType(kPremul_SkAlphaType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400154 } else {
155 fp = GrSimpleTextureEffect::Make(sk_ref_sp(srcProxy->asTextureProxy()), SkMatrix::I());
156 }
157 if (!fp) {
158 return false;
159 }
160 GrPaint paint;
161 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
162 paint.addColorFragmentProcessor(std::move(fp));
163
164 tempCtx->asRenderTargetContext()->fillRectToRect(
165 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon1d435302019-07-01 13:05:28 -0400166 SkRect::MakeWH(dstInfo.width(), dstInfo.height()),
167 SkRect::MakeXYWH(pt.fX, pt.fY, dstInfo.width(), dstInfo.height()));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400168
Brian Salomon1d435302019-07-01 13:05:28 -0400169 return tempCtx->readPixels(dstInfo, dst, rowBytes, {0, 0}, direct);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400170 }
171
Greg Daniel6eb8c242019-06-05 10:22:24 -0400172 bool flip = srcProxy->origin() == kBottomLeft_GrSurfaceOrigin;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400173
Brian Salomon1d435302019-07-01 13:05:28 -0400174 auto supportedRead = caps->supportedReadPixelsColorType(
Greg Daniel00fb7242019-07-18 14:28:01 -0400175 this->colorSpaceInfo().colorType(), srcProxy->backendFormat(), dstInfo.colorType());
Brian Salomon1d435302019-07-01 13:05:28 -0400176
Brian Salomon1047a492019-07-02 12:25:21 -0400177 bool makeTight = !caps->readPixelsRowBytesSupport() && tightRowBytes != rowBytes;
178
179 bool convert = unpremul || premul || needColorConversion || flip || makeTight ||
Brian Salomon1d435302019-07-01 13:05:28 -0400180 (dstInfo.colorType() != supportedRead.fColorType) ||
Brian Salomonf30b1c12019-06-20 12:25:02 -0400181 supportedRead.fSwizzle != GrSwizzle::RGBA();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400182
Brian Salomonf30b1c12019-06-20 12:25:02 -0400183 std::unique_ptr<char[]> tmpPixels;
184 GrPixelInfo tmpInfo;
Brian Salomon1d435302019-07-01 13:05:28 -0400185 void* readDst = dst;
186 size_t readRB = rowBytes;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400187 if (convert) {
Brian Salomon1d435302019-07-01 13:05:28 -0400188 tmpInfo = {supportedRead.fColorType, this->colorSpaceInfo().alphaType(),
189 this->colorSpaceInfo().refColorSpace(), dstInfo.width(), dstInfo.height()};
190 size_t tmpRB = tmpInfo.minRowBytes();
191 size_t size = tmpRB * tmpInfo.height();
192 // Chrome MSAN bots require the data to be initialized (hence the ()).
193 tmpPixels.reset(new char[size]());
Brian Salomonf30b1c12019-06-20 12:25:02 -0400194
Brian Salomonf30b1c12019-06-20 12:25:02 -0400195 readDst = tmpPixels.get();
Brian Salomon1d435302019-07-01 13:05:28 -0400196 readRB = tmpRB;
197 pt.fY = flip ? srcSurface->height() - pt.fY - dstInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400198 }
199
200 direct->priv().flushSurface(srcProxy);
201
Brian Salomon1d435302019-07-01 13:05:28 -0400202 if (!direct->priv().getGpu()->readPixels(srcSurface, pt.fX, pt.fY, dstInfo.width(),
203 dstInfo.height(), supportedRead.fColorType, readDst,
204 readRB)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400205 return false;
206 }
207
Greg Daniel6eb8c242019-06-05 10:22:24 -0400208 if (convert) {
Brian Salomon1d435302019-07-01 13:05:28 -0400209 return GrConvertPixels(dstInfo, dst, rowBytes, tmpInfo, readDst, readRB, flip,
210 supportedRead.fSwizzle);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400211 }
212 return true;
213}
Robert Phillips0d075de2019-03-04 11:08:13 -0500214
Brian Salomon1d435302019-07-01 13:05:28 -0400215bool GrSurfaceContext::writePixels(const GrPixelInfo& origSrcInfo, const void* src, size_t rowBytes,
216 SkIPoint pt, GrContext* direct) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400217 ASSERT_SINGLE_OWNER
218 RETURN_FALSE_IF_ABANDONED
219 SkDEBUGCODE(this->validate();)
Brian Salomon1d435302019-07-01 13:05:28 -0400220 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::writePixels");
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400221
Brian Salomon1d435302019-07-01 13:05:28 -0400222 if (!direct && !(direct = fContext->priv().asDirectContext())) {
Brian Salomonc320b152018-02-20 14:05:36 -0500223 return false;
224 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500225
Brian Salomon1d435302019-07-01 13:05:28 -0400226 if (this->asSurfaceProxy()->readOnly()) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500227 return false;
228 }
229
Brian Salomon1d435302019-07-01 13:05:28 -0400230 if (!src) {
231 return false;
232 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400233
Brian Salomon1047a492019-07-02 12:25:21 -0400234 size_t tightRowBytes = origSrcInfo.minRowBytes();
Brian Salomon1d435302019-07-01 13:05:28 -0400235 if (!rowBytes) {
Brian Salomon1047a492019-07-02 12:25:21 -0400236 rowBytes = tightRowBytes;
237 } else if (rowBytes < tightRowBytes) {
Brian Salomon1d435302019-07-01 13:05:28 -0400238 return false;
239 }
240
241 if (!origSrcInfo.isValid()) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400242 return false;
243 }
244
245 GrSurfaceProxy* dstProxy = this->asSurfaceProxy();
246 if (!dstProxy->instantiate(direct->priv().resourceProvider())) {
247 return false;
248 }
249
250 GrSurface* dstSurface = dstProxy->peekSurface();
251
Brian Salomon1d435302019-07-01 13:05:28 -0400252 auto srcInfo = origSrcInfo;
253 if (!srcInfo.clip(this->width(), this->height(), &pt, &src, rowBytes)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400254 return false;
255 }
Brian Salomon1047a492019-07-02 12:25:21 -0400256 // Our tight row bytes may have been changed by clipping.
257 tightRowBytes = srcInfo.minRowBytes();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400258
Brian Salomon1d435302019-07-01 13:05:28 -0400259 bool premul = this->colorSpaceInfo().alphaType() == kPremul_SkAlphaType &&
260 srcInfo.alphaType() == kUnpremul_SkAlphaType;
261 bool unpremul = this->colorSpaceInfo().alphaType() == kUnpremul_SkAlphaType &&
262 srcInfo.alphaType() == kPremul_SkAlphaType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400263
Brian Salomon1d435302019-07-01 13:05:28 -0400264 bool needColorConversion = SkColorSpaceXformSteps::Required(
265 srcInfo.colorSpace(), this->colorSpaceInfo().colorSpace());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400266
267 const GrCaps* caps = direct->priv().caps();
268 // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs
269 // that are premultiplied on the GPU. This is kept as narrow as possible for now.
Brian Salomon1d435302019-07-01 13:05:28 -0400270 bool canvas2DFastPath = !caps->avoidWritePixelsFastPath() && premul && !needColorConversion &&
271 (srcInfo.colorType() == GrColorType::kRGBA_8888 ||
272 srcInfo.colorType() == GrColorType::kBGRA_8888) &&
273 SkToBool(this->asRenderTargetContext()) &&
274 (dstProxy->config() == kRGBA_8888_GrPixelConfig ||
275 dstProxy->config() == kBGRA_8888_GrPixelConfig) &&
276 direct->priv().caps()->isConfigTexturable(kRGBA_8888_GrPixelConfig) &&
277 direct->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400278
279 if (!caps->surfaceSupportsWritePixels(dstSurface) || canvas2DFastPath) {
280 GrSurfaceDesc desc;
Brian Salomon1d435302019-07-01 13:05:28 -0400281 desc.fWidth = srcInfo.width();
282 desc.fHeight = srcInfo.height();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400283 desc.fSampleCnt = 1;
Brian Salomond6287472019-06-24 15:50:07 -0400284 GrColorType colorType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400285
286 GrBackendFormat format;
Brian Salomone7499c72019-06-24 12:12:36 -0400287 SkAlphaType alphaType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400288 if (canvas2DFastPath) {
289 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomond6287472019-06-24 15:50:07 -0400290 colorType = GrColorType::kRGBA_8888;
Greg Daniel627d0532019-07-08 16:48:14 -0400291 format = caps->getBackendFormatFromColorType(colorType);
Brian Salomone7499c72019-06-24 12:12:36 -0400292 alphaType = kUnpremul_SkAlphaType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400293 } else {
294 desc.fConfig = dstProxy->config();
Brian Salomond6287472019-06-24 15:50:07 -0400295 colorType = this->colorSpaceInfo().colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400296 format = dstProxy->backendFormat().makeTexture2D();
297 if (!format.isValid()) {
298 return false;
299 }
Brian Salomone7499c72019-06-24 12:12:36 -0400300 alphaType = this->colorSpaceInfo().alphaType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400301 }
302
Greg Daniel2e52ad12019-06-13 10:04:16 -0400303 // It is more efficient for us to write pixels into a top left origin so we prefer that.
304 // However, if the final proxy isn't a render target then we must use a copy to move the
305 // data into it which requires the origins to match. If the final proxy is a render target
306 // we can use a draw instead which doesn't have this origin restriction. Thus for render
307 // targets we will use top left and otherwise we will make the origins match.
Brian Salomonf30b1c12019-06-20 12:25:02 -0400308 GrSurfaceOrigin tempOrigin =
309 this->asRenderTargetContext() ? kTopLeft_GrSurfaceOrigin : dstProxy->origin();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400310 auto tempProxy = direct->priv().proxyProvider()->createProxy(
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400311 format, desc, GrRenderable::kNo, tempOrigin, SkBackingFit::kApprox,
312 SkBudgeted::kYes);
Greg Daniel2e52ad12019-06-13 10:04:16 -0400313
Greg Daniel6eb8c242019-06-05 10:22:24 -0400314 if (!tempProxy) {
315 return false;
316 }
317 auto tempCtx = direct->priv().drawingManager()->makeTextureContext(
Brian Salomond6287472019-06-24 15:50:07 -0400318 tempProxy, colorType, alphaType, this->colorSpaceInfo().refColorSpace());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400319 if (!tempCtx) {
320 return false;
321 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400322
323 // In the fast path we always write the srcData to the temp context as though it were RGBA.
324 // When the data is really BGRA the write will cause the R and B channels to be swapped in
325 // the intermediate surface which gets corrected by a swizzle effect when drawing to the
326 // dst.
Brian Salomon1d435302019-07-01 13:05:28 -0400327 if (canvas2DFastPath) {
328 srcInfo = srcInfo.makeColorType(GrColorType::kRGBA_8888);
329 }
330 if (!tempCtx->writePixels(srcInfo, src, rowBytes, {0, 0}, direct)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400331 return false;
332 }
333
334 if (this->asRenderTargetContext()) {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400335 std::unique_ptr<GrFragmentProcessor> fp;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400336 if (canvas2DFastPath) {
337 fp = direct->priv().createUPMToPMEffect(
338 GrSimpleTextureEffect::Make(std::move(tempProxy), SkMatrix::I()));
Brian Salomon1d435302019-07-01 13:05:28 -0400339 // Important: check the original src color type here!
340 if (origSrcInfo.colorType() == GrColorType::kBGRA_8888) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400341 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
342 }
343 } else {
344 fp = GrSimpleTextureEffect::Make(std::move(tempProxy), SkMatrix::I());
345 }
346 if (!fp) {
347 return false;
348 }
349 GrPaint paint;
350 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
351 paint.addColorFragmentProcessor(std::move(fp));
352 this->asRenderTargetContext()->fillRectToRect(
353 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon1d435302019-07-01 13:05:28 -0400354 SkRect::MakeXYWH(pt.fX, pt.fY, srcInfo.width(), srcInfo.height()),
355 SkRect::MakeWH(srcInfo.width(), srcInfo.height()));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400356 } else {
Brian Salomon1d435302019-07-01 13:05:28 -0400357 SkIRect srcRect = SkIRect::MakeWH(srcInfo.width(), srcInfo.height());
358 SkIPoint dstPoint = SkIPoint::Make(pt.fX, pt.fY);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400359 if (!this->copy(tempProxy.get(), srcRect, dstPoint)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400360 return false;
361 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400362 }
363 return true;
364 }
365
Brian Salomon1d435302019-07-01 13:05:28 -0400366 GrColorType allowedColorType =
367 caps->supportedWritePixelsColorType(dstProxy->config(), srcInfo.colorType());
368 bool flip = dstProxy->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon1047a492019-07-02 12:25:21 -0400369 bool makeTight = !caps->writePixelsRowBytesSupport() && rowBytes != tightRowBytes;
370 bool convert = premul || unpremul || needColorConversion || makeTight ||
Brian Salomon1d435302019-07-01 13:05:28 -0400371 (srcInfo.colorType() != allowedColorType) || flip;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400372
Brian Salomonf30b1c12019-06-20 12:25:02 -0400373 std::unique_ptr<char[]> tmpPixels;
Brian Salomon1d435302019-07-01 13:05:28 -0400374 GrColorType srcColorType = srcInfo.colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400375 if (convert) {
Brian Salomon1d435302019-07-01 13:05:28 -0400376 GrPixelInfo tmpInfo(allowedColorType, this->colorSpaceInfo().alphaType(),
377 this->colorSpaceInfo().refColorSpace(), srcInfo.width(),
378 srcInfo.height());
379 auto tmpRB = tmpInfo.minRowBytes();
380 tmpPixels.reset(new char[tmpRB * tmpInfo.height()]);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400381
Brian Salomon1d435302019-07-01 13:05:28 -0400382 GrConvertPixels(tmpInfo, tmpPixels.get(), tmpRB, srcInfo, src, rowBytes, flip);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400383
Brian Salomon1d435302019-07-01 13:05:28 -0400384 srcColorType = tmpInfo.colorType();
385 rowBytes = tmpRB;
386 src = tmpPixels.get();
387 pt.fY = flip ? dstSurface->height() - pt.fY - tmpInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400388 }
389
390 // On platforms that prefer flushes over VRAM use (i.e., ANGLE) we're better off forcing a
391 // complete flush here. On platforms that prefer VRAM use over flushes we're better off
392 // giving the drawing manager the chance of skipping the flush (i.e., by passing in the
393 // destination proxy)
394 // TODO: should this policy decision just be moved into the drawing manager?
395 direct->priv().flushSurface(caps->preferVRAMUseOverFlushes() ? dstProxy : nullptr);
396
Brian Salomon1d435302019-07-01 13:05:28 -0400397 return direct->priv().getGpu()->writePixels(dstSurface, pt.fX, pt.fY, srcInfo.width(),
398 srcInfo.height(), srcColorType, src, rowBytes);
Brian Osman45580d32016-11-23 09:37:01 -0500399}
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400400
401bool GrSurfaceContext::copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
402 ASSERT_SINGLE_OWNER
403 RETURN_FALSE_IF_ABANDONED
404 SkDEBUGCODE(this->validate();)
Greg Daniel46cfbc62019-06-07 11:43:30 -0400405 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy");
Greg Daniel25af6712018-04-25 10:44:38 -0400406
Brian Salomon947efe22019-07-16 15:36:11 -0400407 const GrCaps* caps = fContext->priv().caps();
408
Greg Daniel46cfbc62019-06-07 11:43:30 -0400409 SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal);
410 SkASSERT(src->origin() == this->asSurfaceProxy()->origin());
Brian Salomon947efe22019-07-16 15:36:11 -0400411 SkASSERT(caps->makeConfigSpecific(src->config(), src->backendFormat()) ==
412 caps->makeConfigSpecific(this->asSurfaceProxy()->config(),
413 this->asSurfaceProxy()->backendFormat()));
Greg Daniel46cfbc62019-06-07 11:43:30 -0400414
415 GrSurfaceProxy* dst = this->asSurfaceProxy();
416
Brian Salomon947efe22019-07-16 15:36:11 -0400417 if (!caps->canCopySurface(dst, src, srcRect, dstPoint)) {
Greg Daniel25af6712018-04-25 10:44:38 -0400418 return false;
419 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400420
Greg Daniel46cfbc62019-06-07 11:43:30 -0400421 return this->getOpList()->copySurface(fContext, dst, src, srcRect, dstPoint);
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400422}
Greg Daniel46cfbc62019-06-07 11:43:30 -0400423