blob: cdaad4951e30c202d060e37fb220dc0572dab1ef [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) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400129 GrBackendFormat format;
130 GrPixelConfig config;
Brian Salomond6287472019-06-24 15:50:07 -0400131 GrColorType colorType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400132 if (canvas2DFastPath) {
133 config = kRGBA_8888_GrPixelConfig;
134 format = caps->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
Brian Salomond6287472019-06-24 15:50:07 -0400135 colorType = GrColorType::kRGBA_8888;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400136 } else {
137 config = srcProxy->config();
138 format = srcProxy->backendFormat().makeTexture2D();
139 if (!format.isValid()) {
140 return false;
141 }
Brian Salomond6287472019-06-24 15:50:07 -0400142 colorType = this->colorSpaceInfo().colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400143 }
144 sk_sp<SkColorSpace> cs = canvas2DFastPath ? nullptr : this->colorSpaceInfo().refColorSpace();
145
146 sk_sp<GrRenderTargetContext> tempCtx = direct->priv().makeDeferredRenderTargetContext(
Brian Salomon1d435302019-07-01 13:05:28 -0400147 format, SkBackingFit::kApprox, dstInfo.width(), dstInfo.height(), config, colorType,
148 std::move(cs), 1, GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin, nullptr,
149 SkBudgeted::kYes);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400150 if (!tempCtx) {
151 return false;
152 }
153
154 std::unique_ptr<GrFragmentProcessor> fp;
155 if (canvas2DFastPath) {
156 fp = direct->priv().createPMToUPMEffect(
157 GrSimpleTextureEffect::Make(sk_ref_sp(srcProxy->asTextureProxy()),
158 SkMatrix::I()));
Brian Salomon1d435302019-07-01 13:05:28 -0400159 if (dstInfo.colorType() == GrColorType::kBGRA_8888) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400160 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
Brian Salomon1d435302019-07-01 13:05:28 -0400161 dstInfo = dstInfo.makeColorType(GrColorType::kRGBA_8888);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400162 }
Brian Salomon1d435302019-07-01 13:05:28 -0400163 // The render target context is incorrectly tagged as kPremul even though we're writing
164 // unpremul data thanks to the PMToUPM effect. Fake out the dst alpha type so we don't
165 // double unpremul.
166 dstInfo = dstInfo.makeAlphaType(kPremul_SkAlphaType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400167 } else {
168 fp = GrSimpleTextureEffect::Make(sk_ref_sp(srcProxy->asTextureProxy()), SkMatrix::I());
169 }
170 if (!fp) {
171 return false;
172 }
173 GrPaint paint;
174 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
175 paint.addColorFragmentProcessor(std::move(fp));
176
177 tempCtx->asRenderTargetContext()->fillRectToRect(
178 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon1d435302019-07-01 13:05:28 -0400179 SkRect::MakeWH(dstInfo.width(), dstInfo.height()),
180 SkRect::MakeXYWH(pt.fX, pt.fY, dstInfo.width(), dstInfo.height()));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400181
Brian Salomon1d435302019-07-01 13:05:28 -0400182 return tempCtx->readPixels(dstInfo, dst, rowBytes, {0, 0}, direct);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400183 }
184
Greg Daniel6eb8c242019-06-05 10:22:24 -0400185 bool flip = srcProxy->origin() == kBottomLeft_GrSurfaceOrigin;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400186
Brian Salomon1d435302019-07-01 13:05:28 -0400187 auto supportedRead = caps->supportedReadPixelsColorType(
188 srcProxy->config(), srcProxy->backendFormat(), dstInfo.colorType());
189
Brian Salomon1047a492019-07-02 12:25:21 -0400190 bool makeTight = !caps->readPixelsRowBytesSupport() && tightRowBytes != rowBytes;
191
192 bool convert = unpremul || premul || needColorConversion || flip || makeTight ||
Brian Salomon1d435302019-07-01 13:05:28 -0400193 (dstInfo.colorType() != supportedRead.fColorType) ||
Brian Salomonf30b1c12019-06-20 12:25:02 -0400194 supportedRead.fSwizzle != GrSwizzle::RGBA();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400195
Brian Salomonf30b1c12019-06-20 12:25:02 -0400196 std::unique_ptr<char[]> tmpPixels;
197 GrPixelInfo tmpInfo;
Brian Salomon1d435302019-07-01 13:05:28 -0400198 void* readDst = dst;
199 size_t readRB = rowBytes;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400200 if (convert) {
Brian Salomon1d435302019-07-01 13:05:28 -0400201 tmpInfo = {supportedRead.fColorType, this->colorSpaceInfo().alphaType(),
202 this->colorSpaceInfo().refColorSpace(), dstInfo.width(), dstInfo.height()};
203 size_t tmpRB = tmpInfo.minRowBytes();
204 size_t size = tmpRB * tmpInfo.height();
205 // Chrome MSAN bots require the data to be initialized (hence the ()).
206 tmpPixels.reset(new char[size]());
Brian Salomonf30b1c12019-06-20 12:25:02 -0400207
Brian Salomonf30b1c12019-06-20 12:25:02 -0400208 readDst = tmpPixels.get();
Brian Salomon1d435302019-07-01 13:05:28 -0400209 readRB = tmpRB;
210 pt.fY = flip ? srcSurface->height() - pt.fY - dstInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400211 }
212
213 direct->priv().flushSurface(srcProxy);
214
Brian Salomon1d435302019-07-01 13:05:28 -0400215 if (!direct->priv().getGpu()->readPixels(srcSurface, pt.fX, pt.fY, dstInfo.width(),
216 dstInfo.height(), supportedRead.fColorType, readDst,
217 readRB)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400218 return false;
219 }
220
Greg Daniel6eb8c242019-06-05 10:22:24 -0400221 if (convert) {
Brian Salomon1d435302019-07-01 13:05:28 -0400222 return GrConvertPixels(dstInfo, dst, rowBytes, tmpInfo, readDst, readRB, flip,
223 supportedRead.fSwizzle);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400224 }
225 return true;
226}
Robert Phillips0d075de2019-03-04 11:08:13 -0500227
Brian Salomon1d435302019-07-01 13:05:28 -0400228bool GrSurfaceContext::writePixels(const GrPixelInfo& origSrcInfo, const void* src, size_t rowBytes,
229 SkIPoint pt, GrContext* direct) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400230 ASSERT_SINGLE_OWNER
231 RETURN_FALSE_IF_ABANDONED
232 SkDEBUGCODE(this->validate();)
Brian Salomon1d435302019-07-01 13:05:28 -0400233 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::writePixels");
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400234
Brian Salomon1d435302019-07-01 13:05:28 -0400235 if (!direct && !(direct = fContext->priv().asDirectContext())) {
Brian Salomonc320b152018-02-20 14:05:36 -0500236 return false;
237 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500238
Brian Salomon1d435302019-07-01 13:05:28 -0400239 if (this->asSurfaceProxy()->readOnly()) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500240 return false;
241 }
242
Brian Salomon1d435302019-07-01 13:05:28 -0400243 if (!src) {
244 return false;
245 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400246
Brian Salomon1047a492019-07-02 12:25:21 -0400247 size_t tightRowBytes = origSrcInfo.minRowBytes();
Brian Salomon1d435302019-07-01 13:05:28 -0400248 if (!rowBytes) {
Brian Salomon1047a492019-07-02 12:25:21 -0400249 rowBytes = tightRowBytes;
250 } else if (rowBytes < tightRowBytes) {
Brian Salomon1d435302019-07-01 13:05:28 -0400251 return false;
252 }
253
254 if (!origSrcInfo.isValid()) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400255 return false;
256 }
257
258 GrSurfaceProxy* dstProxy = this->asSurfaceProxy();
259 if (!dstProxy->instantiate(direct->priv().resourceProvider())) {
260 return false;
261 }
262
263 GrSurface* dstSurface = dstProxy->peekSurface();
264
Brian Salomon1d435302019-07-01 13:05:28 -0400265 auto srcInfo = origSrcInfo;
266 if (!srcInfo.clip(this->width(), this->height(), &pt, &src, rowBytes)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400267 return false;
268 }
Brian Salomon1047a492019-07-02 12:25:21 -0400269 // Our tight row bytes may have been changed by clipping.
270 tightRowBytes = srcInfo.minRowBytes();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400271
Brian Salomon1d435302019-07-01 13:05:28 -0400272 bool premul = this->colorSpaceInfo().alphaType() == kPremul_SkAlphaType &&
273 srcInfo.alphaType() == kUnpremul_SkAlphaType;
274 bool unpremul = this->colorSpaceInfo().alphaType() == kUnpremul_SkAlphaType &&
275 srcInfo.alphaType() == kPremul_SkAlphaType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400276
Brian Salomon1d435302019-07-01 13:05:28 -0400277 bool needColorConversion = SkColorSpaceXformSteps::Required(
278 srcInfo.colorSpace(), this->colorSpaceInfo().colorSpace());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400279
280 const GrCaps* caps = direct->priv().caps();
281 // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs
282 // that are premultiplied on the GPU. This is kept as narrow as possible for now.
Brian Salomon1d435302019-07-01 13:05:28 -0400283 bool canvas2DFastPath = !caps->avoidWritePixelsFastPath() && premul && !needColorConversion &&
284 (srcInfo.colorType() == GrColorType::kRGBA_8888 ||
285 srcInfo.colorType() == GrColorType::kBGRA_8888) &&
286 SkToBool(this->asRenderTargetContext()) &&
287 (dstProxy->config() == kRGBA_8888_GrPixelConfig ||
288 dstProxy->config() == kBGRA_8888_GrPixelConfig) &&
289 direct->priv().caps()->isConfigTexturable(kRGBA_8888_GrPixelConfig) &&
290 direct->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400291
292 if (!caps->surfaceSupportsWritePixels(dstSurface) || canvas2DFastPath) {
293 GrSurfaceDesc desc;
Brian Salomon1d435302019-07-01 13:05:28 -0400294 desc.fWidth = srcInfo.width();
295 desc.fHeight = srcInfo.height();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400296 desc.fSampleCnt = 1;
Brian Salomond6287472019-06-24 15:50:07 -0400297 GrColorType colorType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400298
299 GrBackendFormat format;
Brian Salomone7499c72019-06-24 12:12:36 -0400300 SkAlphaType alphaType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400301 if (canvas2DFastPath) {
302 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomond6287472019-06-24 15:50:07 -0400303 colorType = GrColorType::kRGBA_8888;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400304 format = caps->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
Brian Salomone7499c72019-06-24 12:12:36 -0400305 alphaType = kUnpremul_SkAlphaType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400306 } else {
307 desc.fConfig = dstProxy->config();
Brian Salomond6287472019-06-24 15:50:07 -0400308 colorType = this->colorSpaceInfo().colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400309 format = dstProxy->backendFormat().makeTexture2D();
310 if (!format.isValid()) {
311 return false;
312 }
Brian Salomone7499c72019-06-24 12:12:36 -0400313 alphaType = this->colorSpaceInfo().alphaType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400314 }
315
Greg Daniel2e52ad12019-06-13 10:04:16 -0400316 // It is more efficient for us to write pixels into a top left origin so we prefer that.
317 // However, if the final proxy isn't a render target then we must use a copy to move the
318 // data into it which requires the origins to match. If the final proxy is a render target
319 // we can use a draw instead which doesn't have this origin restriction. Thus for render
320 // targets we will use top left and otherwise we will make the origins match.
Brian Salomonf30b1c12019-06-20 12:25:02 -0400321 GrSurfaceOrigin tempOrigin =
322 this->asRenderTargetContext() ? kTopLeft_GrSurfaceOrigin : dstProxy->origin();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400323 auto tempProxy = direct->priv().proxyProvider()->createProxy(
Greg Daniel2e52ad12019-06-13 10:04:16 -0400324 format, desc, tempOrigin, SkBackingFit::kApprox, SkBudgeted::kYes);
325
Greg Daniel6eb8c242019-06-05 10:22:24 -0400326 if (!tempProxy) {
327 return false;
328 }
329 auto tempCtx = direct->priv().drawingManager()->makeTextureContext(
Brian Salomond6287472019-06-24 15:50:07 -0400330 tempProxy, colorType, alphaType, this->colorSpaceInfo().refColorSpace());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400331 if (!tempCtx) {
332 return false;
333 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400334
335 // In the fast path we always write the srcData to the temp context as though it were RGBA.
336 // When the data is really BGRA the write will cause the R and B channels to be swapped in
337 // the intermediate surface which gets corrected by a swizzle effect when drawing to the
338 // dst.
Brian Salomon1d435302019-07-01 13:05:28 -0400339 if (canvas2DFastPath) {
340 srcInfo = srcInfo.makeColorType(GrColorType::kRGBA_8888);
341 }
342 if (!tempCtx->writePixels(srcInfo, src, rowBytes, {0, 0}, direct)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400343 return false;
344 }
345
346 if (this->asRenderTargetContext()) {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400347 std::unique_ptr<GrFragmentProcessor> fp;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400348 if (canvas2DFastPath) {
349 fp = direct->priv().createUPMToPMEffect(
350 GrSimpleTextureEffect::Make(std::move(tempProxy), SkMatrix::I()));
Brian Salomon1d435302019-07-01 13:05:28 -0400351 // Important: check the original src color type here!
352 if (origSrcInfo.colorType() == GrColorType::kBGRA_8888) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400353 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
354 }
355 } else {
356 fp = GrSimpleTextureEffect::Make(std::move(tempProxy), SkMatrix::I());
357 }
358 if (!fp) {
359 return false;
360 }
361 GrPaint paint;
362 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
363 paint.addColorFragmentProcessor(std::move(fp));
364 this->asRenderTargetContext()->fillRectToRect(
365 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon1d435302019-07-01 13:05:28 -0400366 SkRect::MakeXYWH(pt.fX, pt.fY, srcInfo.width(), srcInfo.height()),
367 SkRect::MakeWH(srcInfo.width(), srcInfo.height()));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400368 } else {
Brian Salomon1d435302019-07-01 13:05:28 -0400369 SkIRect srcRect = SkIRect::MakeWH(srcInfo.width(), srcInfo.height());
370 SkIPoint dstPoint = SkIPoint::Make(pt.fX, pt.fY);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400371 if (!this->copy(tempProxy.get(), srcRect, dstPoint)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400372 return false;
373 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400374 }
375 return true;
376 }
377
Brian Salomon1d435302019-07-01 13:05:28 -0400378 GrColorType allowedColorType =
379 caps->supportedWritePixelsColorType(dstProxy->config(), srcInfo.colorType());
380 bool flip = dstProxy->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon1047a492019-07-02 12:25:21 -0400381 bool makeTight = !caps->writePixelsRowBytesSupport() && rowBytes != tightRowBytes;
382 bool convert = premul || unpremul || needColorConversion || makeTight ||
Brian Salomon1d435302019-07-01 13:05:28 -0400383 (srcInfo.colorType() != allowedColorType) || flip;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400384
Brian Salomonf30b1c12019-06-20 12:25:02 -0400385 std::unique_ptr<char[]> tmpPixels;
Brian Salomon1d435302019-07-01 13:05:28 -0400386 GrColorType srcColorType = srcInfo.colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400387 if (convert) {
Brian Salomon1d435302019-07-01 13:05:28 -0400388 GrPixelInfo tmpInfo(allowedColorType, this->colorSpaceInfo().alphaType(),
389 this->colorSpaceInfo().refColorSpace(), srcInfo.width(),
390 srcInfo.height());
391 auto tmpRB = tmpInfo.minRowBytes();
392 tmpPixels.reset(new char[tmpRB * tmpInfo.height()]);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400393
Brian Salomon1d435302019-07-01 13:05:28 -0400394 GrConvertPixels(tmpInfo, tmpPixels.get(), tmpRB, srcInfo, src, rowBytes, flip);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400395
Brian Salomon1d435302019-07-01 13:05:28 -0400396 srcColorType = tmpInfo.colorType();
397 rowBytes = tmpRB;
398 src = tmpPixels.get();
399 pt.fY = flip ? dstSurface->height() - pt.fY - tmpInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400400 }
401
402 // On platforms that prefer flushes over VRAM use (i.e., ANGLE) we're better off forcing a
403 // complete flush here. On platforms that prefer VRAM use over flushes we're better off
404 // giving the drawing manager the chance of skipping the flush (i.e., by passing in the
405 // destination proxy)
406 // TODO: should this policy decision just be moved into the drawing manager?
407 direct->priv().flushSurface(caps->preferVRAMUseOverFlushes() ? dstProxy : nullptr);
408
Brian Salomon1d435302019-07-01 13:05:28 -0400409 return direct->priv().getGpu()->writePixels(dstSurface, pt.fX, pt.fY, srcInfo.width(),
410 srcInfo.height(), srcColorType, src, rowBytes);
Brian Osman45580d32016-11-23 09:37:01 -0500411}
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400412
413bool GrSurfaceContext::copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
414 ASSERT_SINGLE_OWNER
415 RETURN_FALSE_IF_ABANDONED
416 SkDEBUGCODE(this->validate();)
Greg Daniel46cfbc62019-06-07 11:43:30 -0400417 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy");
Greg Daniel25af6712018-04-25 10:44:38 -0400418
Greg Daniel46cfbc62019-06-07 11:43:30 -0400419 SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal);
420 SkASSERT(src->origin() == this->asSurfaceProxy()->origin());
Greg Daniel2c3398d2019-06-19 11:58:01 -0400421 SkASSERT(src->config() == this->asSurfaceProxy()->config());
Greg Daniel46cfbc62019-06-07 11:43:30 -0400422
423 GrSurfaceProxy* dst = this->asSurfaceProxy();
424
425 if (!fContext->priv().caps()->canCopySurface(dst, src, srcRect, dstPoint)) {
Greg Daniel25af6712018-04-25 10:44:38 -0400426 return false;
427 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400428
Greg Daniel46cfbc62019-06-07 11:43:30 -0400429 return this->getOpList()->copySurface(fContext, dst, src, srcRect, dstPoint);
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400430}
Greg Daniel46cfbc62019-06-07 11:43:30 -0400431