blob: 82498dde8412b6017087fafc56c4485d2e427ebb [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,
37 sk_sp<SkColorSpace> colorSpace,
38 GrPixelConfig config)
Brian Salomond6287472019-06-24 15:50:07 -040039 : fContext(context), fColorSpaceInfo(colorType, alphaType, std::move(colorSpace), config) {}
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040040
Robert Phillips0d075de2019-03-04 11:08:13 -050041GrAuditTrail* GrSurfaceContext::auditTrail() {
42 return fContext->priv().auditTrail();
43}
44
45GrDrawingManager* GrSurfaceContext::drawingManager() {
46 return fContext->priv().drawingManager();
47}
48
49const GrDrawingManager* GrSurfaceContext::drawingManager() const {
50 return fContext->priv().drawingManager();
51}
52
53#ifdef SK_DEBUG
54GrSingleOwner* GrSurfaceContext::singleOwner() {
55 return fContext->priv().singleOwner();
56}
57#endif
Greg Daniel6eb8c242019-06-05 10:22:24 -040058
Brian Salomon1d435302019-07-01 13:05:28 -040059bool GrSurfaceContext::readPixels(const GrPixelInfo& origDstInfo, void* dst, size_t rowBytes,
60 SkIPoint pt, GrContext* direct) {
61 ASSERT_SINGLE_OWNER
62 RETURN_FALSE_IF_ABANDONED
63 SkDEBUGCODE(this->validate();)
64 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::readPixels");
Greg Daniel6eb8c242019-06-05 10:22:24 -040065
Brian Salomon1d435302019-07-01 13:05:28 -040066 if (!direct && !(direct = fContext->priv().asDirectContext())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -040067 return false;
68 }
Greg Daniel6eb8c242019-06-05 10:22:24 -040069
Brian Salomon1d435302019-07-01 13:05:28 -040070 if (!dst) {
71 return false;
72 }
73
74 if (!rowBytes) {
75 rowBytes = origDstInfo.minRowBytes();
76 } else if (rowBytes < origDstInfo.minRowBytes()) {
77 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 }
97
Brian Salomon1d435302019-07-01 13:05:28 -040098 bool premul = this->colorSpaceInfo().alphaType() == kUnpremul_SkAlphaType &&
99 dstInfo.alphaType() == kPremul_SkAlphaType;
100 bool unpremul = this->colorSpaceInfo().alphaType() == kPremul_SkAlphaType &&
101 dstInfo.alphaType() == kUnpremul_SkAlphaType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400102
Brian Salomon1d435302019-07-01 13:05:28 -0400103 bool needColorConversion = SkColorSpaceXformSteps::Required(this->colorSpaceInfo().colorSpace(),
104 dstInfo.colorSpace());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400105
106 const GrCaps* caps = direct->priv().caps();
107 // This is the getImageData equivalent to the canvas2D putImageData fast path. We probably don't
108 // care so much about getImageData performance. However, in order to ensure putImageData/
109 // getImageData in "legacy" mode are round-trippable we use the GPU to do the complementary
110 // unpremul step to writeSurfacePixels's premul step (which is determined empirically in
111 // fContext->vaildaPMUPMConversionExists()).
Brian Salomon1d435302019-07-01 13:05:28 -0400112 bool canvas2DFastPath = unpremul && !needColorConversion &&
113 (GrColorType::kRGBA_8888 == dstInfo.colorType() ||
114 GrColorType::kBGRA_8888 == dstInfo.colorType()) &&
115 SkToBool(srcProxy->asTextureProxy()) &&
116 (srcProxy->config() == kRGBA_8888_GrPixelConfig ||
117 srcProxy->config() == kBGRA_8888_GrPixelConfig) &&
118 caps->isConfigRenderable(kRGBA_8888_GrPixelConfig) &&
119 direct->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400120
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400121 auto readFlag = caps->surfaceSupportsReadPixels(srcSurface);
Brian Salomondc0710f2019-07-01 14:59:32 -0400122 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kUnsupported) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400123 return false;
124 }
125
Brian Salomondc0710f2019-07-01 14:59:32 -0400126 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kCopyToTexture2D || canvas2DFastPath) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400127 GrBackendFormat format;
128 GrPixelConfig config;
Brian Salomond6287472019-06-24 15:50:07 -0400129 GrColorType colorType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400130 if (canvas2DFastPath) {
131 config = kRGBA_8888_GrPixelConfig;
132 format = caps->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
Brian Salomond6287472019-06-24 15:50:07 -0400133 colorType = GrColorType::kRGBA_8888;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400134 } else {
135 config = srcProxy->config();
136 format = srcProxy->backendFormat().makeTexture2D();
137 if (!format.isValid()) {
138 return false;
139 }
Brian Salomond6287472019-06-24 15:50:07 -0400140 colorType = this->colorSpaceInfo().colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400141 }
142 sk_sp<SkColorSpace> cs = canvas2DFastPath ? nullptr : this->colorSpaceInfo().refColorSpace();
143
144 sk_sp<GrRenderTargetContext> tempCtx = direct->priv().makeDeferredRenderTargetContext(
Brian Salomon1d435302019-07-01 13:05:28 -0400145 format, SkBackingFit::kApprox, dstInfo.width(), dstInfo.height(), config, colorType,
146 std::move(cs), 1, GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin, nullptr,
147 SkBudgeted::kYes);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400148 if (!tempCtx) {
149 return false;
150 }
151
152 std::unique_ptr<GrFragmentProcessor> fp;
153 if (canvas2DFastPath) {
154 fp = direct->priv().createPMToUPMEffect(
155 GrSimpleTextureEffect::Make(sk_ref_sp(srcProxy->asTextureProxy()),
156 SkMatrix::I()));
Brian Salomon1d435302019-07-01 13:05:28 -0400157 if (dstInfo.colorType() == GrColorType::kBGRA_8888) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400158 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
Brian Salomon1d435302019-07-01 13:05:28 -0400159 dstInfo = dstInfo.makeColorType(GrColorType::kRGBA_8888);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400160 }
Brian Salomon1d435302019-07-01 13:05:28 -0400161 // The render target context is incorrectly tagged as kPremul even though we're writing
162 // unpremul data thanks to the PMToUPM effect. Fake out the dst alpha type so we don't
163 // double unpremul.
164 dstInfo = dstInfo.makeAlphaType(kPremul_SkAlphaType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400165 } else {
166 fp = GrSimpleTextureEffect::Make(sk_ref_sp(srcProxy->asTextureProxy()), SkMatrix::I());
167 }
168 if (!fp) {
169 return false;
170 }
171 GrPaint paint;
172 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
173 paint.addColorFragmentProcessor(std::move(fp));
174
175 tempCtx->asRenderTargetContext()->fillRectToRect(
176 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon1d435302019-07-01 13:05:28 -0400177 SkRect::MakeWH(dstInfo.width(), dstInfo.height()),
178 SkRect::MakeXYWH(pt.fX, pt.fY, dstInfo.width(), dstInfo.height()));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400179
Brian Salomon1d435302019-07-01 13:05:28 -0400180 return tempCtx->readPixels(dstInfo, dst, rowBytes, {0, 0}, direct);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400181 }
182
Greg Daniel6eb8c242019-06-05 10:22:24 -0400183 bool flip = srcProxy->origin() == kBottomLeft_GrSurfaceOrigin;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400184
Brian Salomon1d435302019-07-01 13:05:28 -0400185 auto supportedRead = caps->supportedReadPixelsColorType(
186 srcProxy->config(), srcProxy->backendFormat(), dstInfo.colorType());
187
188 bool convert = unpremul || premul || needColorConversion || flip ||
189 (dstInfo.colorType() != supportedRead.fColorType) ||
Brian Salomonf30b1c12019-06-20 12:25:02 -0400190 supportedRead.fSwizzle != GrSwizzle::RGBA();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400191
Brian Salomonf30b1c12019-06-20 12:25:02 -0400192 std::unique_ptr<char[]> tmpPixels;
193 GrPixelInfo tmpInfo;
Brian Salomon1d435302019-07-01 13:05:28 -0400194 void* readDst = dst;
195 size_t readRB = rowBytes;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400196 if (convert) {
Brian Salomon1d435302019-07-01 13:05:28 -0400197 tmpInfo = {supportedRead.fColorType, this->colorSpaceInfo().alphaType(),
198 this->colorSpaceInfo().refColorSpace(), dstInfo.width(), dstInfo.height()};
199 size_t tmpRB = tmpInfo.minRowBytes();
200 size_t size = tmpRB * tmpInfo.height();
201 // Chrome MSAN bots require the data to be initialized (hence the ()).
202 tmpPixels.reset(new char[size]());
Brian Salomonf30b1c12019-06-20 12:25:02 -0400203
Brian Salomonf30b1c12019-06-20 12:25:02 -0400204 readDst = tmpPixels.get();
Brian Salomon1d435302019-07-01 13:05:28 -0400205 readRB = tmpRB;
206 pt.fY = flip ? srcSurface->height() - pt.fY - dstInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400207 }
208
209 direct->priv().flushSurface(srcProxy);
210
Brian Salomon1d435302019-07-01 13:05:28 -0400211 if (!direct->priv().getGpu()->readPixels(srcSurface, pt.fX, pt.fY, dstInfo.width(),
212 dstInfo.height(), supportedRead.fColorType, readDst,
213 readRB)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400214 return false;
215 }
216
Greg Daniel6eb8c242019-06-05 10:22:24 -0400217 if (convert) {
Brian Salomon1d435302019-07-01 13:05:28 -0400218 return GrConvertPixels(dstInfo, dst, rowBytes, tmpInfo, readDst, readRB, flip,
219 supportedRead.fSwizzle);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400220 }
221 return true;
222}
Robert Phillips0d075de2019-03-04 11:08:13 -0500223
Brian Salomon1d435302019-07-01 13:05:28 -0400224bool GrSurfaceContext::writePixels(const GrPixelInfo& origSrcInfo, const void* src, size_t rowBytes,
225 SkIPoint pt, GrContext* direct) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400226 ASSERT_SINGLE_OWNER
227 RETURN_FALSE_IF_ABANDONED
228 SkDEBUGCODE(this->validate();)
Brian Salomon1d435302019-07-01 13:05:28 -0400229 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::writePixels");
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400230
Brian Salomon1d435302019-07-01 13:05:28 -0400231 if (!direct && !(direct = fContext->priv().asDirectContext())) {
Brian Salomonc320b152018-02-20 14:05:36 -0500232 return false;
233 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500234
Brian Salomon1d435302019-07-01 13:05:28 -0400235 if (this->asSurfaceProxy()->readOnly()) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500236 return false;
237 }
238
Brian Salomon1d435302019-07-01 13:05:28 -0400239 if (!src) {
240 return false;
241 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400242
Brian Salomon1d435302019-07-01 13:05:28 -0400243 if (!rowBytes) {
244 rowBytes = origSrcInfo.minRowBytes();
245 } else if (rowBytes < origSrcInfo.minRowBytes()) {
246 return false;
247 }
248
249 if (!origSrcInfo.isValid()) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400250 return false;
251 }
252
253 GrSurfaceProxy* dstProxy = this->asSurfaceProxy();
254 if (!dstProxy->instantiate(direct->priv().resourceProvider())) {
255 return false;
256 }
257
258 GrSurface* dstSurface = dstProxy->peekSurface();
259
Brian Salomon1d435302019-07-01 13:05:28 -0400260 auto srcInfo = origSrcInfo;
261 if (!srcInfo.clip(this->width(), this->height(), &pt, &src, rowBytes)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400262 return false;
263 }
264
Brian Salomon1d435302019-07-01 13:05:28 -0400265 bool premul = this->colorSpaceInfo().alphaType() == kPremul_SkAlphaType &&
266 srcInfo.alphaType() == kUnpremul_SkAlphaType;
267 bool unpremul = this->colorSpaceInfo().alphaType() == kUnpremul_SkAlphaType &&
268 srcInfo.alphaType() == kPremul_SkAlphaType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400269
Brian Salomon1d435302019-07-01 13:05:28 -0400270 bool needColorConversion = SkColorSpaceXformSteps::Required(
271 srcInfo.colorSpace(), this->colorSpaceInfo().colorSpace());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400272
273 const GrCaps* caps = direct->priv().caps();
274 // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs
275 // that are premultiplied on the GPU. This is kept as narrow as possible for now.
Brian Salomon1d435302019-07-01 13:05:28 -0400276 bool canvas2DFastPath = !caps->avoidWritePixelsFastPath() && premul && !needColorConversion &&
277 (srcInfo.colorType() == GrColorType::kRGBA_8888 ||
278 srcInfo.colorType() == GrColorType::kBGRA_8888) &&
279 SkToBool(this->asRenderTargetContext()) &&
280 (dstProxy->config() == kRGBA_8888_GrPixelConfig ||
281 dstProxy->config() == kBGRA_8888_GrPixelConfig) &&
282 direct->priv().caps()->isConfigTexturable(kRGBA_8888_GrPixelConfig) &&
283 direct->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400284
285 if (!caps->surfaceSupportsWritePixels(dstSurface) || canvas2DFastPath) {
286 GrSurfaceDesc desc;
Brian Salomon1d435302019-07-01 13:05:28 -0400287 desc.fWidth = srcInfo.width();
288 desc.fHeight = srcInfo.height();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400289 desc.fSampleCnt = 1;
Brian Salomond6287472019-06-24 15:50:07 -0400290 GrColorType colorType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400291
292 GrBackendFormat format;
Brian Salomone7499c72019-06-24 12:12:36 -0400293 SkAlphaType alphaType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400294 if (canvas2DFastPath) {
295 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomond6287472019-06-24 15:50:07 -0400296 colorType = GrColorType::kRGBA_8888;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400297 format = caps->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
Brian Salomone7499c72019-06-24 12:12:36 -0400298 alphaType = kUnpremul_SkAlphaType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400299 } else {
300 desc.fConfig = dstProxy->config();
Brian Salomond6287472019-06-24 15:50:07 -0400301 colorType = this->colorSpaceInfo().colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400302 format = dstProxy->backendFormat().makeTexture2D();
303 if (!format.isValid()) {
304 return false;
305 }
Brian Salomone7499c72019-06-24 12:12:36 -0400306 alphaType = this->colorSpaceInfo().alphaType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400307 }
308
Greg Daniel2e52ad12019-06-13 10:04:16 -0400309 // It is more efficient for us to write pixels into a top left origin so we prefer that.
310 // However, if the final proxy isn't a render target then we must use a copy to move the
311 // data into it which requires the origins to match. If the final proxy is a render target
312 // we can use a draw instead which doesn't have this origin restriction. Thus for render
313 // targets we will use top left and otherwise we will make the origins match.
Brian Salomonf30b1c12019-06-20 12:25:02 -0400314 GrSurfaceOrigin tempOrigin =
315 this->asRenderTargetContext() ? kTopLeft_GrSurfaceOrigin : dstProxy->origin();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400316 auto tempProxy = direct->priv().proxyProvider()->createProxy(
Greg Daniel2e52ad12019-06-13 10:04:16 -0400317 format, desc, tempOrigin, SkBackingFit::kApprox, SkBudgeted::kYes);
318
Greg Daniel6eb8c242019-06-05 10:22:24 -0400319 if (!tempProxy) {
320 return false;
321 }
322 auto tempCtx = direct->priv().drawingManager()->makeTextureContext(
Brian Salomond6287472019-06-24 15:50:07 -0400323 tempProxy, colorType, alphaType, this->colorSpaceInfo().refColorSpace());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400324 if (!tempCtx) {
325 return false;
326 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400327
328 // In the fast path we always write the srcData to the temp context as though it were RGBA.
329 // When the data is really BGRA the write will cause the R and B channels to be swapped in
330 // the intermediate surface which gets corrected by a swizzle effect when drawing to the
331 // dst.
Brian Salomon1d435302019-07-01 13:05:28 -0400332 if (canvas2DFastPath) {
333 srcInfo = srcInfo.makeColorType(GrColorType::kRGBA_8888);
334 }
335 if (!tempCtx->writePixels(srcInfo, src, rowBytes, {0, 0}, direct)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400336 return false;
337 }
338
339 if (this->asRenderTargetContext()) {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400340 std::unique_ptr<GrFragmentProcessor> fp;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400341 if (canvas2DFastPath) {
342 fp = direct->priv().createUPMToPMEffect(
343 GrSimpleTextureEffect::Make(std::move(tempProxy), SkMatrix::I()));
Brian Salomon1d435302019-07-01 13:05:28 -0400344 // Important: check the original src color type here!
345 if (origSrcInfo.colorType() == GrColorType::kBGRA_8888) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400346 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
347 }
348 } else {
349 fp = GrSimpleTextureEffect::Make(std::move(tempProxy), SkMatrix::I());
350 }
351 if (!fp) {
352 return false;
353 }
354 GrPaint paint;
355 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
356 paint.addColorFragmentProcessor(std::move(fp));
357 this->asRenderTargetContext()->fillRectToRect(
358 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon1d435302019-07-01 13:05:28 -0400359 SkRect::MakeXYWH(pt.fX, pt.fY, srcInfo.width(), srcInfo.height()),
360 SkRect::MakeWH(srcInfo.width(), srcInfo.height()));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400361 } else {
Brian Salomon1d435302019-07-01 13:05:28 -0400362 SkIRect srcRect = SkIRect::MakeWH(srcInfo.width(), srcInfo.height());
363 SkIPoint dstPoint = SkIPoint::Make(pt.fX, pt.fY);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400364 if (!this->copy(tempProxy.get(), srcRect, dstPoint)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400365 return false;
366 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400367 }
368 return true;
369 }
370
Brian Salomon1d435302019-07-01 13:05:28 -0400371 GrColorType allowedColorType =
372 caps->supportedWritePixelsColorType(dstProxy->config(), srcInfo.colorType());
373 bool flip = dstProxy->origin() == kBottomLeft_GrSurfaceOrigin;
374 bool convert = premul || unpremul || needColorConversion ||
375 (srcInfo.colorType() != allowedColorType) || flip;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400376
Brian Salomonf30b1c12019-06-20 12:25:02 -0400377 std::unique_ptr<char[]> tmpPixels;
Brian Salomon1d435302019-07-01 13:05:28 -0400378 GrColorType srcColorType = srcInfo.colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400379 if (convert) {
Brian Salomon1d435302019-07-01 13:05:28 -0400380 GrPixelInfo tmpInfo(allowedColorType, this->colorSpaceInfo().alphaType(),
381 this->colorSpaceInfo().refColorSpace(), srcInfo.width(),
382 srcInfo.height());
383 auto tmpRB = tmpInfo.minRowBytes();
384 tmpPixels.reset(new char[tmpRB * tmpInfo.height()]);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400385
Brian Salomon1d435302019-07-01 13:05:28 -0400386 GrConvertPixels(tmpInfo, tmpPixels.get(), tmpRB, srcInfo, src, rowBytes, flip);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400387
Brian Salomon1d435302019-07-01 13:05:28 -0400388 srcColorType = tmpInfo.colorType();
389 rowBytes = tmpRB;
390 src = tmpPixels.get();
391 pt.fY = flip ? dstSurface->height() - pt.fY - tmpInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400392 }
393
394 // On platforms that prefer flushes over VRAM use (i.e., ANGLE) we're better off forcing a
395 // complete flush here. On platforms that prefer VRAM use over flushes we're better off
396 // giving the drawing manager the chance of skipping the flush (i.e., by passing in the
397 // destination proxy)
398 // TODO: should this policy decision just be moved into the drawing manager?
399 direct->priv().flushSurface(caps->preferVRAMUseOverFlushes() ? dstProxy : nullptr);
400
Brian Salomon1d435302019-07-01 13:05:28 -0400401 return direct->priv().getGpu()->writePixels(dstSurface, pt.fX, pt.fY, srcInfo.width(),
402 srcInfo.height(), srcColorType, src, rowBytes);
Brian Osman45580d32016-11-23 09:37:01 -0500403}
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400404
405bool GrSurfaceContext::copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
406 ASSERT_SINGLE_OWNER
407 RETURN_FALSE_IF_ABANDONED
408 SkDEBUGCODE(this->validate();)
Greg Daniel46cfbc62019-06-07 11:43:30 -0400409 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy");
Greg Daniel25af6712018-04-25 10:44:38 -0400410
Greg Daniel46cfbc62019-06-07 11:43:30 -0400411 SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal);
412 SkASSERT(src->origin() == this->asSurfaceProxy()->origin());
Greg Daniel2c3398d2019-06-19 11:58:01 -0400413 SkASSERT(src->config() == this->asSurfaceProxy()->config());
Greg Daniel46cfbc62019-06-07 11:43:30 -0400414
415 GrSurfaceProxy* dst = this->asSurfaceProxy();
416
417 if (!fContext->priv().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