blob: 47a89409317254c1ec837ba0f3e09b946e19df46 [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 Danielc6dc5cf2019-07-17 16:02:00 -0400175 this->colorSpaceInfo().colorType(), srcProxy->config(), srcProxy->backendFormat(),
176 dstInfo.colorType());
Brian Salomon1d435302019-07-01 13:05:28 -0400177
Brian Salomon1047a492019-07-02 12:25:21 -0400178 bool makeTight = !caps->readPixelsRowBytesSupport() && tightRowBytes != rowBytes;
179
180 bool convert = unpremul || premul || needColorConversion || flip || makeTight ||
Brian Salomon1d435302019-07-01 13:05:28 -0400181 (dstInfo.colorType() != supportedRead.fColorType) ||
Brian Salomonf30b1c12019-06-20 12:25:02 -0400182 supportedRead.fSwizzle != GrSwizzle::RGBA();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400183
Brian Salomonf30b1c12019-06-20 12:25:02 -0400184 std::unique_ptr<char[]> tmpPixels;
185 GrPixelInfo tmpInfo;
Brian Salomon1d435302019-07-01 13:05:28 -0400186 void* readDst = dst;
187 size_t readRB = rowBytes;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400188 if (convert) {
Brian Salomon1d435302019-07-01 13:05:28 -0400189 tmpInfo = {supportedRead.fColorType, this->colorSpaceInfo().alphaType(),
190 this->colorSpaceInfo().refColorSpace(), dstInfo.width(), dstInfo.height()};
191 size_t tmpRB = tmpInfo.minRowBytes();
192 size_t size = tmpRB * tmpInfo.height();
193 // Chrome MSAN bots require the data to be initialized (hence the ()).
194 tmpPixels.reset(new char[size]());
Brian Salomonf30b1c12019-06-20 12:25:02 -0400195
Brian Salomonf30b1c12019-06-20 12:25:02 -0400196 readDst = tmpPixels.get();
Brian Salomon1d435302019-07-01 13:05:28 -0400197 readRB = tmpRB;
198 pt.fY = flip ? srcSurface->height() - pt.fY - dstInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400199 }
200
201 direct->priv().flushSurface(srcProxy);
202
Brian Salomon1d435302019-07-01 13:05:28 -0400203 if (!direct->priv().getGpu()->readPixels(srcSurface, pt.fX, pt.fY, dstInfo.width(),
204 dstInfo.height(), supportedRead.fColorType, readDst,
205 readRB)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400206 return false;
207 }
208
Greg Daniel6eb8c242019-06-05 10:22:24 -0400209 if (convert) {
Brian Salomon1d435302019-07-01 13:05:28 -0400210 return GrConvertPixels(dstInfo, dst, rowBytes, tmpInfo, readDst, readRB, flip,
211 supportedRead.fSwizzle);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400212 }
213 return true;
214}
Robert Phillips0d075de2019-03-04 11:08:13 -0500215
Brian Salomon1d435302019-07-01 13:05:28 -0400216bool GrSurfaceContext::writePixels(const GrPixelInfo& origSrcInfo, const void* src, size_t rowBytes,
217 SkIPoint pt, GrContext* direct) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400218 ASSERT_SINGLE_OWNER
219 RETURN_FALSE_IF_ABANDONED
220 SkDEBUGCODE(this->validate();)
Brian Salomon1d435302019-07-01 13:05:28 -0400221 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::writePixels");
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400222
Brian Salomon1d435302019-07-01 13:05:28 -0400223 if (!direct && !(direct = fContext->priv().asDirectContext())) {
Brian Salomonc320b152018-02-20 14:05:36 -0500224 return false;
225 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500226
Brian Salomon1d435302019-07-01 13:05:28 -0400227 if (this->asSurfaceProxy()->readOnly()) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500228 return false;
229 }
230
Brian Salomon1d435302019-07-01 13:05:28 -0400231 if (!src) {
232 return false;
233 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400234
Brian Salomon1047a492019-07-02 12:25:21 -0400235 size_t tightRowBytes = origSrcInfo.minRowBytes();
Brian Salomon1d435302019-07-01 13:05:28 -0400236 if (!rowBytes) {
Brian Salomon1047a492019-07-02 12:25:21 -0400237 rowBytes = tightRowBytes;
238 } else if (rowBytes < tightRowBytes) {
Brian Salomon1d435302019-07-01 13:05:28 -0400239 return false;
240 }
241
242 if (!origSrcInfo.isValid()) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400243 return false;
244 }
245
246 GrSurfaceProxy* dstProxy = this->asSurfaceProxy();
247 if (!dstProxy->instantiate(direct->priv().resourceProvider())) {
248 return false;
249 }
250
251 GrSurface* dstSurface = dstProxy->peekSurface();
252
Brian Salomon1d435302019-07-01 13:05:28 -0400253 auto srcInfo = origSrcInfo;
254 if (!srcInfo.clip(this->width(), this->height(), &pt, &src, rowBytes)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400255 return false;
256 }
Brian Salomon1047a492019-07-02 12:25:21 -0400257 // Our tight row bytes may have been changed by clipping.
258 tightRowBytes = srcInfo.minRowBytes();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400259
Brian Salomon1d435302019-07-01 13:05:28 -0400260 bool premul = this->colorSpaceInfo().alphaType() == kPremul_SkAlphaType &&
261 srcInfo.alphaType() == kUnpremul_SkAlphaType;
262 bool unpremul = this->colorSpaceInfo().alphaType() == kUnpremul_SkAlphaType &&
263 srcInfo.alphaType() == kPremul_SkAlphaType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400264
Brian Salomon1d435302019-07-01 13:05:28 -0400265 bool needColorConversion = SkColorSpaceXformSteps::Required(
266 srcInfo.colorSpace(), this->colorSpaceInfo().colorSpace());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400267
268 const GrCaps* caps = direct->priv().caps();
269 // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs
270 // that are premultiplied on the GPU. This is kept as narrow as possible for now.
Brian Salomon1d435302019-07-01 13:05:28 -0400271 bool canvas2DFastPath = !caps->avoidWritePixelsFastPath() && premul && !needColorConversion &&
272 (srcInfo.colorType() == GrColorType::kRGBA_8888 ||
273 srcInfo.colorType() == GrColorType::kBGRA_8888) &&
274 SkToBool(this->asRenderTargetContext()) &&
275 (dstProxy->config() == kRGBA_8888_GrPixelConfig ||
276 dstProxy->config() == kBGRA_8888_GrPixelConfig) &&
277 direct->priv().caps()->isConfigTexturable(kRGBA_8888_GrPixelConfig) &&
278 direct->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400279
280 if (!caps->surfaceSupportsWritePixels(dstSurface) || canvas2DFastPath) {
281 GrSurfaceDesc desc;
Brian Salomon1d435302019-07-01 13:05:28 -0400282 desc.fWidth = srcInfo.width();
283 desc.fHeight = srcInfo.height();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400284 desc.fSampleCnt = 1;
Brian Salomond6287472019-06-24 15:50:07 -0400285 GrColorType colorType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400286
287 GrBackendFormat format;
Brian Salomone7499c72019-06-24 12:12:36 -0400288 SkAlphaType alphaType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400289 if (canvas2DFastPath) {
290 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomond6287472019-06-24 15:50:07 -0400291 colorType = GrColorType::kRGBA_8888;
Greg Daniel627d0532019-07-08 16:48:14 -0400292 format = caps->getBackendFormatFromColorType(colorType);
Brian Salomone7499c72019-06-24 12:12:36 -0400293 alphaType = kUnpremul_SkAlphaType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400294 } else {
295 desc.fConfig = dstProxy->config();
Brian Salomond6287472019-06-24 15:50:07 -0400296 colorType = this->colorSpaceInfo().colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400297 format = dstProxy->backendFormat().makeTexture2D();
298 if (!format.isValid()) {
299 return false;
300 }
Brian Salomone7499c72019-06-24 12:12:36 -0400301 alphaType = this->colorSpaceInfo().alphaType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400302 }
303
Greg Daniel2e52ad12019-06-13 10:04:16 -0400304 // It is more efficient for us to write pixels into a top left origin so we prefer that.
305 // However, if the final proxy isn't a render target then we must use a copy to move the
306 // data into it which requires the origins to match. If the final proxy is a render target
307 // we can use a draw instead which doesn't have this origin restriction. Thus for render
308 // targets we will use top left and otherwise we will make the origins match.
Brian Salomonf30b1c12019-06-20 12:25:02 -0400309 GrSurfaceOrigin tempOrigin =
310 this->asRenderTargetContext() ? kTopLeft_GrSurfaceOrigin : dstProxy->origin();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400311 auto tempProxy = direct->priv().proxyProvider()->createProxy(
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400312 format, desc, GrRenderable::kNo, tempOrigin, SkBackingFit::kApprox,
313 SkBudgeted::kYes);
Greg Daniel2e52ad12019-06-13 10:04:16 -0400314
Greg Daniel6eb8c242019-06-05 10:22:24 -0400315 if (!tempProxy) {
316 return false;
317 }
318 auto tempCtx = direct->priv().drawingManager()->makeTextureContext(
Brian Salomond6287472019-06-24 15:50:07 -0400319 tempProxy, colorType, alphaType, this->colorSpaceInfo().refColorSpace());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400320 if (!tempCtx) {
321 return false;
322 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400323
324 // In the fast path we always write the srcData to the temp context as though it were RGBA.
325 // When the data is really BGRA the write will cause the R and B channels to be swapped in
326 // the intermediate surface which gets corrected by a swizzle effect when drawing to the
327 // dst.
Brian Salomon1d435302019-07-01 13:05:28 -0400328 if (canvas2DFastPath) {
329 srcInfo = srcInfo.makeColorType(GrColorType::kRGBA_8888);
330 }
331 if (!tempCtx->writePixels(srcInfo, src, rowBytes, {0, 0}, direct)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400332 return false;
333 }
334
335 if (this->asRenderTargetContext()) {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400336 std::unique_ptr<GrFragmentProcessor> fp;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400337 if (canvas2DFastPath) {
338 fp = direct->priv().createUPMToPMEffect(
339 GrSimpleTextureEffect::Make(std::move(tempProxy), SkMatrix::I()));
Brian Salomon1d435302019-07-01 13:05:28 -0400340 // Important: check the original src color type here!
341 if (origSrcInfo.colorType() == GrColorType::kBGRA_8888) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400342 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
343 }
344 } else {
345 fp = GrSimpleTextureEffect::Make(std::move(tempProxy), SkMatrix::I());
346 }
347 if (!fp) {
348 return false;
349 }
350 GrPaint paint;
351 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
352 paint.addColorFragmentProcessor(std::move(fp));
353 this->asRenderTargetContext()->fillRectToRect(
354 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon1d435302019-07-01 13:05:28 -0400355 SkRect::MakeXYWH(pt.fX, pt.fY, srcInfo.width(), srcInfo.height()),
356 SkRect::MakeWH(srcInfo.width(), srcInfo.height()));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400357 } else {
Brian Salomon1d435302019-07-01 13:05:28 -0400358 SkIRect srcRect = SkIRect::MakeWH(srcInfo.width(), srcInfo.height());
359 SkIPoint dstPoint = SkIPoint::Make(pt.fX, pt.fY);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400360 if (!this->copy(tempProxy.get(), srcRect, dstPoint)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400361 return false;
362 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400363 }
364 return true;
365 }
366
Brian Salomon1d435302019-07-01 13:05:28 -0400367 GrColorType allowedColorType =
368 caps->supportedWritePixelsColorType(dstProxy->config(), srcInfo.colorType());
369 bool flip = dstProxy->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon1047a492019-07-02 12:25:21 -0400370 bool makeTight = !caps->writePixelsRowBytesSupport() && rowBytes != tightRowBytes;
371 bool convert = premul || unpremul || needColorConversion || makeTight ||
Brian Salomon1d435302019-07-01 13:05:28 -0400372 (srcInfo.colorType() != allowedColorType) || flip;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400373
Brian Salomonf30b1c12019-06-20 12:25:02 -0400374 std::unique_ptr<char[]> tmpPixels;
Brian Salomon1d435302019-07-01 13:05:28 -0400375 GrColorType srcColorType = srcInfo.colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400376 if (convert) {
Brian Salomon1d435302019-07-01 13:05:28 -0400377 GrPixelInfo tmpInfo(allowedColorType, this->colorSpaceInfo().alphaType(),
378 this->colorSpaceInfo().refColorSpace(), srcInfo.width(),
379 srcInfo.height());
380 auto tmpRB = tmpInfo.minRowBytes();
381 tmpPixels.reset(new char[tmpRB * tmpInfo.height()]);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400382
Brian Salomon1d435302019-07-01 13:05:28 -0400383 GrConvertPixels(tmpInfo, tmpPixels.get(), tmpRB, srcInfo, src, rowBytes, flip);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400384
Brian Salomon1d435302019-07-01 13:05:28 -0400385 srcColorType = tmpInfo.colorType();
386 rowBytes = tmpRB;
387 src = tmpPixels.get();
388 pt.fY = flip ? dstSurface->height() - pt.fY - tmpInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400389 }
390
391 // On platforms that prefer flushes over VRAM use (i.e., ANGLE) we're better off forcing a
392 // complete flush here. On platforms that prefer VRAM use over flushes we're better off
393 // giving the drawing manager the chance of skipping the flush (i.e., by passing in the
394 // destination proxy)
395 // TODO: should this policy decision just be moved into the drawing manager?
396 direct->priv().flushSurface(caps->preferVRAMUseOverFlushes() ? dstProxy : nullptr);
397
Brian Salomon1d435302019-07-01 13:05:28 -0400398 return direct->priv().getGpu()->writePixels(dstSurface, pt.fX, pt.fY, srcInfo.width(),
399 srcInfo.height(), srcColorType, src, rowBytes);
Brian Osman45580d32016-11-23 09:37:01 -0500400}
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400401
402bool GrSurfaceContext::copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
403 ASSERT_SINGLE_OWNER
404 RETURN_FALSE_IF_ABANDONED
405 SkDEBUGCODE(this->validate();)
Greg Daniel46cfbc62019-06-07 11:43:30 -0400406 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy");
Greg Daniel25af6712018-04-25 10:44:38 -0400407
Brian Salomon947efe22019-07-16 15:36:11 -0400408 const GrCaps* caps = fContext->priv().caps();
409
Greg Daniel46cfbc62019-06-07 11:43:30 -0400410 SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal);
411 SkASSERT(src->origin() == this->asSurfaceProxy()->origin());
Brian Salomon947efe22019-07-16 15:36:11 -0400412 SkASSERT(caps->makeConfigSpecific(src->config(), src->backendFormat()) ==
413 caps->makeConfigSpecific(this->asSurfaceProxy()->config(),
414 this->asSurfaceProxy()->backendFormat()));
Greg Daniel46cfbc62019-06-07 11:43:30 -0400415
416 GrSurfaceProxy* dst = this->asSurfaceProxy();
417
Brian Salomon947efe22019-07-16 15:36:11 -0400418 if (!caps->canCopySurface(dst, src, srcRect, dstPoint)) {
Greg Daniel25af6712018-04-25 10:44:38 -0400419 return false;
420 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400421
Greg Daniel46cfbc62019-06-07 11:43:30 -0400422 return this->getOpList()->copySurface(fContext, dst, src, srcRect, dstPoint);
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400423}
Greg Daniel46cfbc62019-06-07 11:43:30 -0400424