blob: 395772b099519ca780c51711b6cac017121b7d4d [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
73 if (!rowBytes) {
74 rowBytes = origDstInfo.minRowBytes();
75 } else if (rowBytes < origDstInfo.minRowBytes()) {
76 return false;
77 }
78
79 if (!origDstInfo.isValid()) {
80 return false;
81 }
Greg Daniel6eb8c242019-06-05 10:22:24 -040082
83 GrSurfaceProxy* srcProxy = this->asSurfaceProxy();
84
85 // MDB TODO: delay this instantiation until later in the method
86 if (!srcProxy->instantiate(direct->priv().resourceProvider())) {
87 return false;
88 }
89
90 GrSurface* srcSurface = srcProxy->peekSurface();
91
Brian Salomon1d435302019-07-01 13:05:28 -040092 auto dstInfo = origDstInfo;
93 if (!dstInfo.clip(this->width(), this->height(), &pt, &dst, rowBytes)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -040094 return false;
95 }
96
Brian Salomon1d435302019-07-01 13:05:28 -040097 bool premul = this->colorSpaceInfo().alphaType() == kUnpremul_SkAlphaType &&
98 dstInfo.alphaType() == kPremul_SkAlphaType;
99 bool unpremul = this->colorSpaceInfo().alphaType() == kPremul_SkAlphaType &&
100 dstInfo.alphaType() == kUnpremul_SkAlphaType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400101
Brian Salomon1d435302019-07-01 13:05:28 -0400102 bool needColorConversion = SkColorSpaceXformSteps::Required(this->colorSpaceInfo().colorSpace(),
103 dstInfo.colorSpace());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400104
105 const GrCaps* caps = direct->priv().caps();
106 // This is the getImageData equivalent to the canvas2D putImageData fast path. We probably don't
107 // care so much about getImageData performance. However, in order to ensure putImageData/
108 // getImageData in "legacy" mode are round-trippable we use the GPU to do the complementary
109 // unpremul step to writeSurfacePixels's premul step (which is determined empirically in
110 // fContext->vaildaPMUPMConversionExists()).
Brian Salomon1d435302019-07-01 13:05:28 -0400111 bool canvas2DFastPath = unpremul && !needColorConversion &&
112 (GrColorType::kRGBA_8888 == dstInfo.colorType() ||
113 GrColorType::kBGRA_8888 == dstInfo.colorType()) &&
114 SkToBool(srcProxy->asTextureProxy()) &&
115 (srcProxy->config() == kRGBA_8888_GrPixelConfig ||
116 srcProxy->config() == kBGRA_8888_GrPixelConfig) &&
117 caps->isConfigRenderable(kRGBA_8888_GrPixelConfig) &&
118 direct->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400119
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400120 auto readFlag = caps->surfaceSupportsReadPixels(srcSurface);
Brian Salomondc0710f2019-07-01 14:59:32 -0400121 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kUnsupported) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400122 return false;
123 }
124
Brian Salomondc0710f2019-07-01 14:59:32 -0400125 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kCopyToTexture2D || canvas2DFastPath) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400126 GrBackendFormat format;
127 GrPixelConfig config;
Brian Salomond6287472019-06-24 15:50:07 -0400128 GrColorType colorType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400129 if (canvas2DFastPath) {
130 config = kRGBA_8888_GrPixelConfig;
131 format = caps->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
Brian Salomond6287472019-06-24 15:50:07 -0400132 colorType = GrColorType::kRGBA_8888;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400133 } else {
134 config = srcProxy->config();
135 format = srcProxy->backendFormat().makeTexture2D();
136 if (!format.isValid()) {
137 return false;
138 }
Brian Salomond6287472019-06-24 15:50:07 -0400139 colorType = this->colorSpaceInfo().colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400140 }
141 sk_sp<SkColorSpace> cs = canvas2DFastPath ? nullptr : this->colorSpaceInfo().refColorSpace();
142
143 sk_sp<GrRenderTargetContext> tempCtx = direct->priv().makeDeferredRenderTargetContext(
Brian Salomon1d435302019-07-01 13:05:28 -0400144 format, SkBackingFit::kApprox, dstInfo.width(), dstInfo.height(), config, colorType,
145 std::move(cs), 1, GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin, nullptr,
146 SkBudgeted::kYes);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400147 if (!tempCtx) {
148 return false;
149 }
150
151 std::unique_ptr<GrFragmentProcessor> fp;
152 if (canvas2DFastPath) {
153 fp = direct->priv().createPMToUPMEffect(
154 GrSimpleTextureEffect::Make(sk_ref_sp(srcProxy->asTextureProxy()),
155 SkMatrix::I()));
Brian Salomon1d435302019-07-01 13:05:28 -0400156 if (dstInfo.colorType() == GrColorType::kBGRA_8888) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400157 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
Brian Salomon1d435302019-07-01 13:05:28 -0400158 dstInfo = dstInfo.makeColorType(GrColorType::kRGBA_8888);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400159 }
Brian Salomon1d435302019-07-01 13:05:28 -0400160 // The render target context is incorrectly tagged as kPremul even though we're writing
161 // unpremul data thanks to the PMToUPM effect. Fake out the dst alpha type so we don't
162 // double unpremul.
163 dstInfo = dstInfo.makeAlphaType(kPremul_SkAlphaType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400164 } else {
165 fp = GrSimpleTextureEffect::Make(sk_ref_sp(srcProxy->asTextureProxy()), SkMatrix::I());
166 }
167 if (!fp) {
168 return false;
169 }
170 GrPaint paint;
171 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
172 paint.addColorFragmentProcessor(std::move(fp));
173
174 tempCtx->asRenderTargetContext()->fillRectToRect(
175 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon1d435302019-07-01 13:05:28 -0400176 SkRect::MakeWH(dstInfo.width(), dstInfo.height()),
177 SkRect::MakeXYWH(pt.fX, pt.fY, dstInfo.width(), dstInfo.height()));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400178
Brian Salomon1d435302019-07-01 13:05:28 -0400179 return tempCtx->readPixels(dstInfo, dst, rowBytes, {0, 0}, direct);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400180 }
181
Greg Daniel6eb8c242019-06-05 10:22:24 -0400182 bool flip = srcProxy->origin() == kBottomLeft_GrSurfaceOrigin;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400183
Brian Salomon1d435302019-07-01 13:05:28 -0400184 auto supportedRead = caps->supportedReadPixelsColorType(
185 srcProxy->config(), srcProxy->backendFormat(), dstInfo.colorType());
186
187 bool convert = unpremul || premul || needColorConversion || flip ||
188 (dstInfo.colorType() != supportedRead.fColorType) ||
Brian Salomonf30b1c12019-06-20 12:25:02 -0400189 supportedRead.fSwizzle != GrSwizzle::RGBA();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400190
Brian Salomonf30b1c12019-06-20 12:25:02 -0400191 std::unique_ptr<char[]> tmpPixels;
192 GrPixelInfo tmpInfo;
Brian Salomon1d435302019-07-01 13:05:28 -0400193 void* readDst = dst;
194 size_t readRB = rowBytes;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400195 if (convert) {
Brian Salomon1d435302019-07-01 13:05:28 -0400196 tmpInfo = {supportedRead.fColorType, this->colorSpaceInfo().alphaType(),
197 this->colorSpaceInfo().refColorSpace(), dstInfo.width(), dstInfo.height()};
198 size_t tmpRB = tmpInfo.minRowBytes();
199 size_t size = tmpRB * tmpInfo.height();
200 // Chrome MSAN bots require the data to be initialized (hence the ()).
201 tmpPixels.reset(new char[size]());
Brian Salomonf30b1c12019-06-20 12:25:02 -0400202
Brian Salomonf30b1c12019-06-20 12:25:02 -0400203 readDst = tmpPixels.get();
Brian Salomon1d435302019-07-01 13:05:28 -0400204 readRB = tmpRB;
205 pt.fY = flip ? srcSurface->height() - pt.fY - dstInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400206 }
207
208 direct->priv().flushSurface(srcProxy);
209
Brian Salomon1d435302019-07-01 13:05:28 -0400210 if (!direct->priv().getGpu()->readPixels(srcSurface, pt.fX, pt.fY, dstInfo.width(),
211 dstInfo.height(), supportedRead.fColorType, readDst,
212 readRB)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400213 return false;
214 }
215
Greg Daniel6eb8c242019-06-05 10:22:24 -0400216 if (convert) {
Brian Salomon1d435302019-07-01 13:05:28 -0400217 return GrConvertPixels(dstInfo, dst, rowBytes, tmpInfo, readDst, readRB, flip,
218 supportedRead.fSwizzle);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400219 }
220 return true;
221}
Robert Phillips0d075de2019-03-04 11:08:13 -0500222
Brian Salomon1d435302019-07-01 13:05:28 -0400223bool GrSurfaceContext::writePixels(const GrPixelInfo& origSrcInfo, const void* src, size_t rowBytes,
224 SkIPoint pt, GrContext* direct) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400225 ASSERT_SINGLE_OWNER
226 RETURN_FALSE_IF_ABANDONED
227 SkDEBUGCODE(this->validate();)
Brian Salomon1d435302019-07-01 13:05:28 -0400228 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::writePixels");
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400229
Brian Salomon1d435302019-07-01 13:05:28 -0400230 if (!direct && !(direct = fContext->priv().asDirectContext())) {
Brian Salomonc320b152018-02-20 14:05:36 -0500231 return false;
232 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500233
Brian Salomon1d435302019-07-01 13:05:28 -0400234 if (this->asSurfaceProxy()->readOnly()) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500235 return false;
236 }
237
Brian Salomon1d435302019-07-01 13:05:28 -0400238 if (!src) {
239 return false;
240 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400241
Brian Salomon1d435302019-07-01 13:05:28 -0400242 if (!rowBytes) {
243 rowBytes = origSrcInfo.minRowBytes();
244 } else if (rowBytes < origSrcInfo.minRowBytes()) {
245 return false;
246 }
247
248 if (!origSrcInfo.isValid()) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400249 return false;
250 }
251
252 GrSurfaceProxy* dstProxy = this->asSurfaceProxy();
253 if (!dstProxy->instantiate(direct->priv().resourceProvider())) {
254 return false;
255 }
256
257 GrSurface* dstSurface = dstProxy->peekSurface();
258
Brian Salomon1d435302019-07-01 13:05:28 -0400259 auto srcInfo = origSrcInfo;
260 if (!srcInfo.clip(this->width(), this->height(), &pt, &src, rowBytes)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400261 return false;
262 }
263
Brian Salomon1d435302019-07-01 13:05:28 -0400264 bool premul = this->colorSpaceInfo().alphaType() == kPremul_SkAlphaType &&
265 srcInfo.alphaType() == kUnpremul_SkAlphaType;
266 bool unpremul = this->colorSpaceInfo().alphaType() == kUnpremul_SkAlphaType &&
267 srcInfo.alphaType() == kPremul_SkAlphaType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400268
Brian Salomon1d435302019-07-01 13:05:28 -0400269 bool needColorConversion = SkColorSpaceXformSteps::Required(
270 srcInfo.colorSpace(), this->colorSpaceInfo().colorSpace());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400271
272 const GrCaps* caps = direct->priv().caps();
273 // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs
274 // that are premultiplied on the GPU. This is kept as narrow as possible for now.
Brian Salomon1d435302019-07-01 13:05:28 -0400275 bool canvas2DFastPath = !caps->avoidWritePixelsFastPath() && premul && !needColorConversion &&
276 (srcInfo.colorType() == GrColorType::kRGBA_8888 ||
277 srcInfo.colorType() == GrColorType::kBGRA_8888) &&
278 SkToBool(this->asRenderTargetContext()) &&
279 (dstProxy->config() == kRGBA_8888_GrPixelConfig ||
280 dstProxy->config() == kBGRA_8888_GrPixelConfig) &&
281 direct->priv().caps()->isConfigTexturable(kRGBA_8888_GrPixelConfig) &&
282 direct->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400283
284 if (!caps->surfaceSupportsWritePixels(dstSurface) || canvas2DFastPath) {
285 GrSurfaceDesc desc;
Brian Salomon1d435302019-07-01 13:05:28 -0400286 desc.fWidth = srcInfo.width();
287 desc.fHeight = srcInfo.height();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400288 desc.fSampleCnt = 1;
Brian Salomond6287472019-06-24 15:50:07 -0400289 GrColorType colorType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400290
291 GrBackendFormat format;
Brian Salomone7499c72019-06-24 12:12:36 -0400292 SkAlphaType alphaType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400293 if (canvas2DFastPath) {
294 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomond6287472019-06-24 15:50:07 -0400295 colorType = GrColorType::kRGBA_8888;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400296 format = caps->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
Brian Salomone7499c72019-06-24 12:12:36 -0400297 alphaType = kUnpremul_SkAlphaType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400298 } else {
299 desc.fConfig = dstProxy->config();
Brian Salomond6287472019-06-24 15:50:07 -0400300 colorType = this->colorSpaceInfo().colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400301 format = dstProxy->backendFormat().makeTexture2D();
302 if (!format.isValid()) {
303 return false;
304 }
Brian Salomone7499c72019-06-24 12:12:36 -0400305 alphaType = this->colorSpaceInfo().alphaType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400306 }
307
Greg Daniel2e52ad12019-06-13 10:04:16 -0400308 // It is more efficient for us to write pixels into a top left origin so we prefer that.
309 // However, if the final proxy isn't a render target then we must use a copy to move the
310 // data into it which requires the origins to match. If the final proxy is a render target
311 // we can use a draw instead which doesn't have this origin restriction. Thus for render
312 // targets we will use top left and otherwise we will make the origins match.
Brian Salomonf30b1c12019-06-20 12:25:02 -0400313 GrSurfaceOrigin tempOrigin =
314 this->asRenderTargetContext() ? kTopLeft_GrSurfaceOrigin : dstProxy->origin();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400315 auto tempProxy = direct->priv().proxyProvider()->createProxy(
Greg Daniel2e52ad12019-06-13 10:04:16 -0400316 format, desc, tempOrigin, SkBackingFit::kApprox, SkBudgeted::kYes);
317
Greg Daniel6eb8c242019-06-05 10:22:24 -0400318 if (!tempProxy) {
319 return false;
320 }
321 auto tempCtx = direct->priv().drawingManager()->makeTextureContext(
Brian Salomond6287472019-06-24 15:50:07 -0400322 tempProxy, colorType, alphaType, this->colorSpaceInfo().refColorSpace());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400323 if (!tempCtx) {
324 return false;
325 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400326
327 // In the fast path we always write the srcData to the temp context as though it were RGBA.
328 // When the data is really BGRA the write will cause the R and B channels to be swapped in
329 // the intermediate surface which gets corrected by a swizzle effect when drawing to the
330 // dst.
Brian Salomon1d435302019-07-01 13:05:28 -0400331 if (canvas2DFastPath) {
332 srcInfo = srcInfo.makeColorType(GrColorType::kRGBA_8888);
333 }
334 if (!tempCtx->writePixels(srcInfo, src, rowBytes, {0, 0}, direct)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400335 return false;
336 }
337
338 if (this->asRenderTargetContext()) {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400339 std::unique_ptr<GrFragmentProcessor> fp;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400340 if (canvas2DFastPath) {
341 fp = direct->priv().createUPMToPMEffect(
342 GrSimpleTextureEffect::Make(std::move(tempProxy), SkMatrix::I()));
Brian Salomon1d435302019-07-01 13:05:28 -0400343 // Important: check the original src color type here!
344 if (origSrcInfo.colorType() == GrColorType::kBGRA_8888) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400345 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
346 }
347 } else {
348 fp = GrSimpleTextureEffect::Make(std::move(tempProxy), SkMatrix::I());
349 }
350 if (!fp) {
351 return false;
352 }
353 GrPaint paint;
354 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
355 paint.addColorFragmentProcessor(std::move(fp));
356 this->asRenderTargetContext()->fillRectToRect(
357 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon1d435302019-07-01 13:05:28 -0400358 SkRect::MakeXYWH(pt.fX, pt.fY, srcInfo.width(), srcInfo.height()),
359 SkRect::MakeWH(srcInfo.width(), srcInfo.height()));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400360 } else {
Brian Salomon1d435302019-07-01 13:05:28 -0400361 SkIRect srcRect = SkIRect::MakeWH(srcInfo.width(), srcInfo.height());
362 SkIPoint dstPoint = SkIPoint::Make(pt.fX, pt.fY);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400363 if (!this->copy(tempProxy.get(), srcRect, dstPoint)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400364 return false;
365 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400366 }
367 return true;
368 }
369
Brian Salomon1d435302019-07-01 13:05:28 -0400370 GrColorType allowedColorType =
371 caps->supportedWritePixelsColorType(dstProxy->config(), srcInfo.colorType());
372 bool flip = dstProxy->origin() == kBottomLeft_GrSurfaceOrigin;
373 bool convert = premul || unpremul || needColorConversion ||
374 (srcInfo.colorType() != allowedColorType) || flip;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400375
Brian Salomonf30b1c12019-06-20 12:25:02 -0400376 std::unique_ptr<char[]> tmpPixels;
Brian Salomon1d435302019-07-01 13:05:28 -0400377 GrColorType srcColorType = srcInfo.colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400378 if (convert) {
Brian Salomon1d435302019-07-01 13:05:28 -0400379 GrPixelInfo tmpInfo(allowedColorType, this->colorSpaceInfo().alphaType(),
380 this->colorSpaceInfo().refColorSpace(), srcInfo.width(),
381 srcInfo.height());
382 auto tmpRB = tmpInfo.minRowBytes();
383 tmpPixels.reset(new char[tmpRB * tmpInfo.height()]);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400384
Brian Salomon1d435302019-07-01 13:05:28 -0400385 GrConvertPixels(tmpInfo, tmpPixels.get(), tmpRB, srcInfo, src, rowBytes, flip);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400386
Brian Salomon1d435302019-07-01 13:05:28 -0400387 srcColorType = tmpInfo.colorType();
388 rowBytes = tmpRB;
389 src = tmpPixels.get();
390 pt.fY = flip ? dstSurface->height() - pt.fY - tmpInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400391 }
392
393 // On platforms that prefer flushes over VRAM use (i.e., ANGLE) we're better off forcing a
394 // complete flush here. On platforms that prefer VRAM use over flushes we're better off
395 // giving the drawing manager the chance of skipping the flush (i.e., by passing in the
396 // destination proxy)
397 // TODO: should this policy decision just be moved into the drawing manager?
398 direct->priv().flushSurface(caps->preferVRAMUseOverFlushes() ? dstProxy : nullptr);
399
Brian Salomon1d435302019-07-01 13:05:28 -0400400 return direct->priv().getGpu()->writePixels(dstSurface, pt.fX, pt.fY, srcInfo.width(),
401 srcInfo.height(), srcColorType, src, rowBytes);
Brian Osman45580d32016-11-23 09:37:01 -0500402}
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400403
404bool GrSurfaceContext::copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
405 ASSERT_SINGLE_OWNER
406 RETURN_FALSE_IF_ABANDONED
407 SkDEBUGCODE(this->validate();)
Greg Daniel46cfbc62019-06-07 11:43:30 -0400408 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy");
Greg Daniel25af6712018-04-25 10:44:38 -0400409
Greg Daniel46cfbc62019-06-07 11:43:30 -0400410 SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal);
411 SkASSERT(src->origin() == this->asSurfaceProxy()->origin());
Greg Daniel2c3398d2019-06-19 11:58:01 -0400412 SkASSERT(src->config() == this->asSurfaceProxy()->config());
Greg Daniel46cfbc62019-06-07 11:43:30 -0400413
414 GrSurfaceProxy* dst = this->asSurfaceProxy();
415
416 if (!fContext->priv().caps()->canCopySurface(dst, src, srcRect, dstPoint)) {
Greg Daniel25af6712018-04-25 10:44:38 -0400417 return false;
418 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400419
Greg Daniel46cfbc62019-06-07 11:43:30 -0400420 return this->getOpList()->copySurface(fContext, dst, src, srcRect, dstPoint);
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400421}
Greg Daniel46cfbc62019-06-07 11:43:30 -0400422