blob: eb198cb6151342905bd148b43fa637d1e3bd4a05 [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"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040018#include "src/gpu/GrImageInfo.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 Salomone9ad9982019-07-22 16:17:41 -040025#include "src/gpu/effects/GrBicubicEffect.h"
Brian Osman45580d32016-11-23 09:37:01 -050026
Robert Phillips2de8cfa2017-06-28 10:33:41 -040027#define ASSERT_SINGLE_OWNER \
28 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
Robert Phillips69893702019-02-22 11:16:30 -050029#define RETURN_FALSE_IF_ABANDONED if (this->fContext->priv().abandoned()) { return false; }
Brian Osman45580d32016-11-23 09:37:01 -050030
Greg Danielf41b2bd2019-08-22 16:19:24 -040031// In MDB mode the reffing of the 'getLastOpsTask' call's result allows in-progress
32// GrOpsTasks to be picked up and added to by renderTargetContexts lower in the call
33// stack. When this occurs with a closed GrOpsTask, a new one will be allocated
34// when the renderTargetContext attempts to use it (via getOpsTask).
Robert Phillips69893702019-02-22 11:16:30 -050035GrSurfaceContext::GrSurfaceContext(GrRecordingContext* context,
Brian Salomond6287472019-06-24 15:50:07 -040036 GrColorType colorType,
Brian Salomone7499c72019-06-24 12:12:36 -040037 SkAlphaType alphaType,
Brian Salomonbd3d8d32019-07-02 09:16:28 -040038 sk_sp<SkColorSpace> colorSpace)
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040039 : fContext(context), fColorInfo(colorType, alphaType, std::move(colorSpace)) {}
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040040
Brian Salomon4d2d6f42019-07-26 14:15:11 -040041const GrCaps* GrSurfaceContext::caps() const { return fContext->priv().caps(); }
42
Robert Phillips0d075de2019-03-04 11:08:13 -050043GrAuditTrail* GrSurfaceContext::auditTrail() {
44 return fContext->priv().auditTrail();
45}
46
47GrDrawingManager* GrSurfaceContext::drawingManager() {
48 return fContext->priv().drawingManager();
49}
50
51const GrDrawingManager* GrSurfaceContext::drawingManager() const {
52 return fContext->priv().drawingManager();
53}
54
55#ifdef SK_DEBUG
56GrSingleOwner* GrSurfaceContext::singleOwner() {
57 return fContext->priv().singleOwner();
58}
59#endif
Greg Daniel6eb8c242019-06-05 10:22:24 -040060
Brian Salomonf2ebdd92019-09-30 12:15:30 -040061bool GrSurfaceContext::readPixels(const GrImageInfo& origDstInfo, void* dst, size_t rowBytes,
Brian Salomon1d435302019-07-01 13:05:28 -040062 SkIPoint pt, GrContext* direct) {
63 ASSERT_SINGLE_OWNER
64 RETURN_FALSE_IF_ABANDONED
65 SkDEBUGCODE(this->validate();)
66 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::readPixels");
Greg Daniel6eb8c242019-06-05 10:22:24 -040067
Brian Salomon1d435302019-07-01 13:05:28 -040068 if (!direct && !(direct = fContext->priv().asDirectContext())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -040069 return false;
70 }
Greg Daniel6eb8c242019-06-05 10:22:24 -040071
Brian Salomon1d435302019-07-01 13:05:28 -040072 if (!dst) {
73 return false;
74 }
75
Brian Salomon1047a492019-07-02 12:25:21 -040076 size_t tightRowBytes = origDstInfo.minRowBytes();
Brian Salomon1d435302019-07-01 13:05:28 -040077 if (!rowBytes) {
Brian Salomon1047a492019-07-02 12:25:21 -040078 rowBytes = tightRowBytes;
79 } else if (rowBytes < tightRowBytes) {
Brian Salomon1d435302019-07-01 13:05:28 -040080 return false;
81 }
82
83 if (!origDstInfo.isValid()) {
84 return false;
85 }
Greg Daniel6eb8c242019-06-05 10:22:24 -040086
87 GrSurfaceProxy* srcProxy = this->asSurfaceProxy();
88
89 // MDB TODO: delay this instantiation until later in the method
90 if (!srcProxy->instantiate(direct->priv().resourceProvider())) {
91 return false;
92 }
93
94 GrSurface* srcSurface = srcProxy->peekSurface();
95
Brian Salomon1d435302019-07-01 13:05:28 -040096 auto dstInfo = origDstInfo;
97 if (!dstInfo.clip(this->width(), this->height(), &pt, &dst, rowBytes)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -040098 return false;
99 }
Brian Salomon1047a492019-07-02 12:25:21 -0400100 // Our tight row bytes may have been changed by clipping.
101 tightRowBytes = dstInfo.minRowBytes();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400102
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400103 bool premul = this->colorInfo().alphaType() == kUnpremul_SkAlphaType &&
Brian Salomon1d435302019-07-01 13:05:28 -0400104 dstInfo.alphaType() == kPremul_SkAlphaType;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400105 bool unpremul = this->colorInfo().alphaType() == kPremul_SkAlphaType &&
Brian Salomon1d435302019-07-01 13:05:28 -0400106 dstInfo.alphaType() == kUnpremul_SkAlphaType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400107
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400108 bool needColorConversion =
109 SkColorSpaceXformSteps::Required(this->colorInfo().colorSpace(), dstInfo.colorSpace());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400110
111 const GrCaps* caps = direct->priv().caps();
112 // This is the getImageData equivalent to the canvas2D putImageData fast path. We probably don't
113 // care so much about getImageData performance. However, in order to ensure putImageData/
114 // getImageData in "legacy" mode are round-trippable we use the GPU to do the complementary
115 // unpremul step to writeSurfacePixels's premul step (which is determined empirically in
116 // fContext->vaildaPMUPMConversionExists()).
Greg Daniel0258c902019-08-01 13:08:33 -0400117 GrBackendFormat defaultRGBAFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
118 GrRenderable::kYes);
Brian Salomon1d435302019-07-01 13:05:28 -0400119 bool canvas2DFastPath = unpremul && !needColorConversion &&
120 (GrColorType::kRGBA_8888 == dstInfo.colorType() ||
121 GrColorType::kBGRA_8888 == dstInfo.colorType()) &&
122 SkToBool(srcProxy->asTextureProxy()) &&
123 (srcProxy->config() == kRGBA_8888_GrPixelConfig ||
124 srcProxy->config() == kBGRA_8888_GrPixelConfig) &&
Greg Daniel0258c902019-08-01 13:08:33 -0400125 defaultRGBAFormat.isValid() &&
Brian Salomon1d435302019-07-01 13:05:28 -0400126 direct->priv().validPMUPMConversionExists();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400127
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400128 auto readFlag = caps->surfaceSupportsReadPixels(srcSurface);
Brian Salomondc0710f2019-07-01 14:59:32 -0400129 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kUnsupported) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400130 return false;
131 }
132
Brian Salomondc0710f2019-07-01 14:59:32 -0400133 if (readFlag == GrCaps::SurfaceReadPixelsSupport::kCopyToTexture2D || canvas2DFastPath) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400134 GrColorType colorType =
135 canvas2DFastPath ? GrColorType::kRGBA_8888 : this->colorInfo().colorType();
136 sk_sp<SkColorSpace> cs = canvas2DFastPath ? nullptr : this->colorInfo().refColorSpace();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400137
Brian Salomonbf6b9792019-08-21 09:38:10 -0400138 auto tempCtx = direct->priv().makeDeferredRenderTargetContext(
Brian Salomon27ae52c2019-07-03 11:27:44 -0400139 SkBackingFit::kApprox, dstInfo.width(), dstInfo.height(), colorType, std::move(cs),
140 1, GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin, nullptr, SkBudgeted::kYes);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400141 if (!tempCtx) {
142 return false;
143 }
144
145 std::unique_ptr<GrFragmentProcessor> fp;
146 if (canvas2DFastPath) {
147 fp = direct->priv().createPMToUPMEffect(
148 GrSimpleTextureEffect::Make(sk_ref_sp(srcProxy->asTextureProxy()),
149 SkMatrix::I()));
Brian Salomon1d435302019-07-01 13:05:28 -0400150 if (dstInfo.colorType() == GrColorType::kBGRA_8888) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400151 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
Brian Salomon1d435302019-07-01 13:05:28 -0400152 dstInfo = dstInfo.makeColorType(GrColorType::kRGBA_8888);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400153 }
Brian Salomon1d435302019-07-01 13:05:28 -0400154 // The render target context is incorrectly tagged as kPremul even though we're writing
155 // unpremul data thanks to the PMToUPM effect. Fake out the dst alpha type so we don't
156 // double unpremul.
157 dstInfo = dstInfo.makeAlphaType(kPremul_SkAlphaType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400158 } else {
159 fp = GrSimpleTextureEffect::Make(sk_ref_sp(srcProxy->asTextureProxy()), SkMatrix::I());
160 }
161 if (!fp) {
162 return false;
163 }
164 GrPaint paint;
165 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
166 paint.addColorFragmentProcessor(std::move(fp));
167
168 tempCtx->asRenderTargetContext()->fillRectToRect(
169 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon1d435302019-07-01 13:05:28 -0400170 SkRect::MakeWH(dstInfo.width(), dstInfo.height()),
171 SkRect::MakeXYWH(pt.fX, pt.fY, dstInfo.width(), dstInfo.height()));
Greg Daniel6eb8c242019-06-05 10:22:24 -0400172
Brian Salomon1d435302019-07-01 13:05:28 -0400173 return tempCtx->readPixels(dstInfo, dst, rowBytes, {0, 0}, direct);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400174 }
175
Greg Daniel6eb8c242019-06-05 10:22:24 -0400176 bool flip = srcProxy->origin() == kBottomLeft_GrSurfaceOrigin;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400177
Brian Salomon1d435302019-07-01 13:05:28 -0400178 auto supportedRead = caps->supportedReadPixelsColorType(
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400179 this->colorInfo().colorType(), srcProxy->backendFormat(), dstInfo.colorType());
Brian Salomon1d435302019-07-01 13:05:28 -0400180
Brian Salomon1047a492019-07-02 12:25:21 -0400181 bool makeTight = !caps->readPixelsRowBytesSupport() && tightRowBytes != rowBytes;
182
183 bool convert = unpremul || premul || needColorConversion || flip || makeTight ||
Brian Salomon8f8354a2019-07-31 20:12:02 -0400184 (dstInfo.colorType() != supportedRead.fColorType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400185
Brian Salomonf30b1c12019-06-20 12:25:02 -0400186 std::unique_ptr<char[]> tmpPixels;
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400187 GrImageInfo tmpInfo;
Brian Salomon1d435302019-07-01 13:05:28 -0400188 void* readDst = dst;
189 size_t readRB = rowBytes;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400190 if (convert) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400191 tmpInfo = {supportedRead.fColorType, this->colorInfo().alphaType(),
192 this->colorInfo().refColorSpace(), dstInfo.width(), dstInfo.height()};
Brian Salomon1d435302019-07-01 13:05:28 -0400193 size_t tmpRB = tmpInfo.minRowBytes();
194 size_t size = tmpRB * tmpInfo.height();
195 // Chrome MSAN bots require the data to be initialized (hence the ()).
196 tmpPixels.reset(new char[size]());
Brian Salomonf30b1c12019-06-20 12:25:02 -0400197
Brian Salomonf30b1c12019-06-20 12:25:02 -0400198 readDst = tmpPixels.get();
Brian Salomon1d435302019-07-01 13:05:28 -0400199 readRB = tmpRB;
200 pt.fY = flip ? srcSurface->height() - pt.fY - dstInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400201 }
202
203 direct->priv().flushSurface(srcProxy);
204
Brian Salomon1d435302019-07-01 13:05:28 -0400205 if (!direct->priv().getGpu()->readPixels(srcSurface, pt.fX, pt.fY, dstInfo.width(),
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400206 dstInfo.height(), this->colorInfo().colorType(),
Brian Salomonf77c1462019-08-01 15:19:29 -0400207 supportedRead.fColorType, readDst, readRB)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400208 return false;
209 }
210
Greg Daniel6eb8c242019-06-05 10:22:24 -0400211 if (convert) {
Brian Salomon8f8354a2019-07-31 20:12:02 -0400212 return GrConvertPixels(dstInfo, dst, rowBytes, tmpInfo, readDst, readRB, flip);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400213 }
214 return true;
215}
Robert Phillips0d075de2019-03-04 11:08:13 -0500216
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400217bool GrSurfaceContext::writePixels(const GrImageInfo& origSrcInfo, const void* src, size_t rowBytes,
Brian Salomon1d435302019-07-01 13:05:28 -0400218 SkIPoint pt, GrContext* direct) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400219 ASSERT_SINGLE_OWNER
220 RETURN_FALSE_IF_ABANDONED
221 SkDEBUGCODE(this->validate();)
Brian Salomon1d435302019-07-01 13:05:28 -0400222 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::writePixels");
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400223
Brian Salomon1d435302019-07-01 13:05:28 -0400224 if (!direct && !(direct = fContext->priv().asDirectContext())) {
Brian Salomonc320b152018-02-20 14:05:36 -0500225 return false;
226 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500227
Brian Salomon1d435302019-07-01 13:05:28 -0400228 if (this->asSurfaceProxy()->readOnly()) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500229 return false;
230 }
231
Brian Salomon1d435302019-07-01 13:05:28 -0400232 if (!src) {
233 return false;
234 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400235
Brian Salomon1047a492019-07-02 12:25:21 -0400236 size_t tightRowBytes = origSrcInfo.minRowBytes();
Brian Salomon1d435302019-07-01 13:05:28 -0400237 if (!rowBytes) {
Brian Salomon1047a492019-07-02 12:25:21 -0400238 rowBytes = tightRowBytes;
239 } else if (rowBytes < tightRowBytes) {
Brian Salomon1d435302019-07-01 13:05:28 -0400240 return false;
241 }
242
243 if (!origSrcInfo.isValid()) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400244 return false;
245 }
246
247 GrSurfaceProxy* dstProxy = this->asSurfaceProxy();
248 if (!dstProxy->instantiate(direct->priv().resourceProvider())) {
249 return false;
250 }
251
252 GrSurface* dstSurface = dstProxy->peekSurface();
253
Brian Salomon1d435302019-07-01 13:05:28 -0400254 auto srcInfo = origSrcInfo;
255 if (!srcInfo.clip(this->width(), this->height(), &pt, &src, rowBytes)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400256 return false;
257 }
Brian Salomon1047a492019-07-02 12:25:21 -0400258 // Our tight row bytes may have been changed by clipping.
259 tightRowBytes = srcInfo.minRowBytes();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400260
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400261 bool premul = this->colorInfo().alphaType() == kPremul_SkAlphaType &&
262 srcInfo.alphaType() == kUnpremul_SkAlphaType;
263 bool unpremul = this->colorInfo().alphaType() == kUnpremul_SkAlphaType &&
264 srcInfo.alphaType() == kPremul_SkAlphaType;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400265
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400266 bool needColorConversion =
267 SkColorSpaceXformSteps::Required(srcInfo.colorSpace(), this->colorInfo().colorSpace());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400268
269 const GrCaps* caps = direct->priv().caps();
Greg Daniel7bfc9132019-08-14 14:23:53 -0400270
271 auto rgbaDefaultFormat = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
272 GrRenderable::kNo);
273
Greg Daniel6eb8c242019-06-05 10:22:24 -0400274 // 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) &&
Greg Daniel7bfc9132019-08-14 14:23:53 -0400282 rgbaDefaultFormat.isValid() &&
Brian Salomon1d435302019-07-01 13:05:28 -0400283 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();
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 Daniel7bfc9132019-08-14 14:23:53 -0400296 format = rgbaDefaultFormat;
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 Salomon4bc0c1f2019-09-30 15:12:27 -0400300 colorType = this->colorInfo().colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400301 format = dstProxy->backendFormat().makeTexture2D();
302 if (!format.isValid()) {
303 return false;
304 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400305 alphaType = this->colorInfo().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(
Brian Salomonbeb7f522019-08-30 16:19:42 -0400316 format, desc, GrRenderable::kNo, 1, tempOrigin, GrMipMapped::kNo,
317 SkBackingFit::kApprox, SkBudgeted::kYes, GrProtected::kNo);
Greg Daniel2e52ad12019-06-13 10:04:16 -0400318
Greg Daniel6eb8c242019-06-05 10:22:24 -0400319 if (!tempProxy) {
320 return false;
321 }
322 auto tempCtx = direct->priv().drawingManager()->makeTextureContext(
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400323 tempProxy, colorType, alphaType, this->colorInfo().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 =
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400372 caps->supportedWritePixelsColorType(this->colorInfo().colorType(),
Brian Salomon01915c02019-08-02 09:57:21 -0400373 dstProxy->backendFormat(),
374 srcInfo.colorType()).fColorType;
Brian Salomon1d435302019-07-01 13:05:28 -0400375 bool flip = dstProxy->origin() == kBottomLeft_GrSurfaceOrigin;
Brian Salomon1047a492019-07-02 12:25:21 -0400376 bool makeTight = !caps->writePixelsRowBytesSupport() && rowBytes != tightRowBytes;
377 bool convert = premul || unpremul || needColorConversion || makeTight ||
Brian Salomon1d435302019-07-01 13:05:28 -0400378 (srcInfo.colorType() != allowedColorType) || flip;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400379
Brian Salomonf30b1c12019-06-20 12:25:02 -0400380 std::unique_ptr<char[]> tmpPixels;
Brian Salomon1d435302019-07-01 13:05:28 -0400381 GrColorType srcColorType = srcInfo.colorType();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400382 if (convert) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400383 GrImageInfo tmpInfo(allowedColorType, this->colorInfo().alphaType(),
384 this->colorInfo().refColorSpace(), srcInfo.width(), srcInfo.height());
Brian Salomon1d435302019-07-01 13:05:28 -0400385 auto tmpRB = tmpInfo.minRowBytes();
386 tmpPixels.reset(new char[tmpRB * tmpInfo.height()]);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400387
Brian Salomon1d435302019-07-01 13:05:28 -0400388 GrConvertPixels(tmpInfo, tmpPixels.get(), tmpRB, srcInfo, src, rowBytes, flip);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400389
Brian Salomon1d435302019-07-01 13:05:28 -0400390 srcColorType = tmpInfo.colorType();
391 rowBytes = tmpRB;
392 src = tmpPixels.get();
393 pt.fY = flip ? dstSurface->height() - pt.fY - tmpInfo.height() : pt.fY;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400394 }
395
396 // On platforms that prefer flushes over VRAM use (i.e., ANGLE) we're better off forcing a
397 // complete flush here. On platforms that prefer VRAM use over flushes we're better off
398 // giving the drawing manager the chance of skipping the flush (i.e., by passing in the
399 // destination proxy)
400 // TODO: should this policy decision just be moved into the drawing manager?
401 direct->priv().flushSurface(caps->preferVRAMUseOverFlushes() ? dstProxy : nullptr);
402
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400403 return direct->priv().getGpu()->writePixels(dstSurface, pt.fX, pt.fY, srcInfo.width(),
404 srcInfo.height(), this->colorInfo().colorType(),
405 srcColorType, src, rowBytes);
Brian Osman45580d32016-11-23 09:37:01 -0500406}
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400407
408bool GrSurfaceContext::copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
409 ASSERT_SINGLE_OWNER
410 RETURN_FALSE_IF_ABANDONED
411 SkDEBUGCODE(this->validate();)
Greg Daniel46cfbc62019-06-07 11:43:30 -0400412 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy");
Greg Daniel25af6712018-04-25 10:44:38 -0400413
Brian Salomon947efe22019-07-16 15:36:11 -0400414 const GrCaps* caps = fContext->priv().caps();
415
Greg Daniel46cfbc62019-06-07 11:43:30 -0400416 SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal);
417 SkASSERT(src->origin() == this->asSurfaceProxy()->origin());
Brian Salomon947efe22019-07-16 15:36:11 -0400418 SkASSERT(caps->makeConfigSpecific(src->config(), src->backendFormat()) ==
419 caps->makeConfigSpecific(this->asSurfaceProxy()->config(),
420 this->asSurfaceProxy()->backendFormat()));
Greg Daniel46cfbc62019-06-07 11:43:30 -0400421
Chris Daltonf8e5aad2019-08-02 12:55:23 -0600422 if (!caps->canCopySurface(this->asSurfaceProxy(), src, srcRect, dstPoint)) {
Greg Daniel25af6712018-04-25 10:44:38 -0400423 return false;
424 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400425
Greg Daniele227fe42019-08-21 13:52:24 -0400426 return this->drawingManager()->newCopyRenderTask(sk_ref_sp(src), srcRect,
427 this->asSurfaceProxyRef(), dstPoint);
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400428}
Greg Daniel46cfbc62019-06-07 11:43:30 -0400429
Brian Salomonbf6b9792019-08-21 09:38:10 -0400430std::unique_ptr<GrRenderTargetContext> GrSurfaceContext::rescale(
431 const SkImageInfo& info,
432 const SkIRect& srcRect,
433 SkSurface::RescaleGamma rescaleGamma,
434 SkFilterQuality rescaleQuality) {
Brian Salomone9ad9982019-07-22 16:17:41 -0400435 auto direct = fContext->priv().asDirectContext();
436 if (!direct) {
437 return nullptr;
438 }
439 auto rtProxy = this->asRenderTargetProxy();
440 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
441 return nullptr;
442 }
443
444 // We rescale by drawing and don't currently support drawing to a kUnpremul destination.
445 if (info.alphaType() == kUnpremul_SkAlphaType) {
446 return nullptr;
447 }
448
449 int srcW = srcRect.width();
450 int srcH = srcRect.height();
451 int srcX = srcRect.fLeft;
452 int srcY = srcRect.fTop;
453 sk_sp<GrTextureProxy> texProxy = sk_ref_sp(this->asTextureProxy());
454 SkCanvas::SrcRectConstraint constraint = SkCanvas::kStrict_SrcRectConstraint;
455 if (!texProxy) {
456 texProxy = GrSurfaceProxy::Copy(fContext, this->asSurfaceProxy(), GrMipMapped::kNo, srcRect,
457 SkBackingFit::kApprox, SkBudgeted::kNo);
458 if (!texProxy) {
459 return nullptr;
460 }
461 srcX = 0;
462 srcY = 0;
463 constraint = SkCanvas::kFast_SrcRectConstraint;
464 }
465
466 float sx = (float)info.width() / srcW;
467 float sy = (float)info.height() / srcH;
468
469 // How many bilerp/bicubic steps to do in X and Y. + means upscaling, - means downscaling.
470 int stepsX;
471 int stepsY;
472 if (rescaleQuality > kNone_SkFilterQuality) {
473 stepsX = static_cast<int>((sx > 1.f) ? ceil(log2f(sx)) : floor(log2f(sx)));
474 stepsY = static_cast<int>((sy > 1.f) ? ceil(log2f(sy)) : floor(log2f(sy)));
475 } else {
476 stepsX = sx != 1.f;
477 stepsY = sy != 1.f;
478 }
479 SkASSERT(stepsX || stepsY);
Brian Salomonbf6b9792019-08-21 09:38:10 -0400480 // Within a rescaling pass A is the input (if not null) and B is the output. At the end of the
481 // pass B is moved to A. If 'this' is the input on the first pass then tempA is null.
482 std::unique_ptr<GrRenderTargetContext> tempA;
483 std::unique_ptr<GrRenderTargetContext> tempB;
484
Brian Salomone9ad9982019-07-22 16:17:41 -0400485 // Assume we should ignore the rescale linear request if the surface has no color space since
486 // it's unclear how we'd linearize from an unknown color space.
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400487 if (rescaleGamma == SkSurface::kLinear && this->colorInfo().colorSpace() &&
488 !this->colorInfo().colorSpace()->gammaIsLinear()) {
489 auto cs = this->colorInfo().colorSpace()->makeLinearGamma();
490 auto xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(),
491 this->colorInfo().alphaType(), cs.get(),
Brian Salomone9ad9982019-07-22 16:17:41 -0400492 kPremul_SkAlphaType);
493 // We'll fall back to kRGBA_8888 if half float not supported.
494 auto linearRTC = fContext->priv().makeDeferredRenderTargetContextWithFallback(
495 SkBackingFit::kExact, srcW, srcH, GrColorType::kRGBA_F16, cs, 1, GrMipMapped::kNo,
496 kTopLeft_GrSurfaceOrigin);
497 if (!linearRTC) {
498 return nullptr;
499 }
500 linearRTC->drawTexture(GrNoClip(), texProxy, GrSamplerState::Filter::kNearest,
501 SkBlendMode::kSrc, SK_PMColor4fWHITE, SkRect::Make(srcRect),
502 SkRect::MakeWH(srcW, srcH), GrAA::kNo, GrQuadAAFlags::kNone,
503 constraint, SkMatrix::I(), std::move(xform));
504 texProxy = linearRTC->asTextureProxyRef();
Brian Salomonbf6b9792019-08-21 09:38:10 -0400505 tempA = std::move(linearRTC);
Brian Salomone9ad9982019-07-22 16:17:41 -0400506 srcX = 0;
507 srcY = 0;
508 constraint = SkCanvas::kFast_SrcRectConstraint;
509 }
510 while (stepsX || stepsY) {
511 int nextW = info.width();
512 int nextH = info.height();
513 if (stepsX < 0) {
514 nextW = info.width() << (-stepsX - 1);
515 stepsX++;
516 } else if (stepsX != 0) {
517 if (stepsX > 1) {
518 nextW = srcW * 2;
519 }
520 --stepsX;
521 }
522 if (stepsY < 0) {
523 nextH = info.height() << (-stepsY - 1);
524 stepsY++;
525 } else if (stepsY != 0) {
526 if (stepsY > 1) {
527 nextH = srcH * 2;
528 }
529 --stepsY;
530 }
Brian Salomonbf6b9792019-08-21 09:38:10 -0400531 auto input = tempA ? tempA.get() : this;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400532 GrColorType colorType = input->colorInfo().colorType();
533 auto cs = input->colorInfo().refColorSpace();
Brian Salomone9ad9982019-07-22 16:17:41 -0400534 sk_sp<GrColorSpaceXform> xform;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400535 auto prevAlphaType = input->colorInfo().alphaType();
Brian Salomone9ad9982019-07-22 16:17:41 -0400536 if (!stepsX && !stepsY) {
537 // Might as well fold conversion to final info in the last step.
538 cs = info.refColorSpace();
539 colorType = SkColorTypeToGrColorType(info.colorType());
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400540 xform = GrColorSpaceXform::Make(input->colorInfo().colorSpace(),
541 input->colorInfo().alphaType(), cs.get(),
Brian Salomone9ad9982019-07-22 16:17:41 -0400542 info.alphaType());
543 }
Brian Salomonbf6b9792019-08-21 09:38:10 -0400544 tempB = fContext->priv().makeDeferredRenderTargetContextWithFallback(
Brian Salomone9ad9982019-07-22 16:17:41 -0400545 SkBackingFit::kExact, nextW, nextH, colorType, std::move(cs), 1, GrMipMapped::kNo,
546 kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -0400547 if (!tempB) {
Brian Salomone9ad9982019-07-22 16:17:41 -0400548 return nullptr;
549 }
550 auto dstRect = SkRect::MakeWH(nextW, nextH);
551 if (rescaleQuality == kHigh_SkFilterQuality) {
552 SkMatrix matrix;
553 matrix.setScaleTranslate((float)srcW / nextW, (float)srcH / nextH, srcX, srcY);
554 std::unique_ptr<GrFragmentProcessor> fp;
555 auto dir = GrBicubicEffect::Direction::kXY;
556 if (nextW == srcW) {
557 dir = GrBicubicEffect::Direction::kY;
558 } else if (nextH == srcH) {
559 dir = GrBicubicEffect::Direction::kX;
560 }
561 if (srcW != texProxy->width() || srcH != texProxy->height()) {
562 auto domain = GrTextureDomain::MakeTexelDomain(
563 SkIRect::MakeXYWH(srcX, srcY, srcW, srcH), GrTextureDomain::kClamp_Mode);
564 fp = GrBicubicEffect::Make(texProxy, matrix, domain, dir, prevAlphaType);
565 } else {
566 fp = GrBicubicEffect::Make(texProxy, matrix, dir, prevAlphaType);
567 }
568 if (xform) {
569 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(xform));
570 }
571 GrPaint paint;
572 paint.addColorFragmentProcessor(std::move(fp));
573 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonbf6b9792019-08-21 09:38:10 -0400574 tempB->fillRectToRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), dstRect,
Brian Salomone9ad9982019-07-22 16:17:41 -0400575 dstRect);
576 } else {
577 auto filter = rescaleQuality == kNone_SkFilterQuality ? GrSamplerState::Filter::kNearest
578 : GrSamplerState::Filter::kBilerp;
579 auto srcSubset = SkRect::MakeXYWH(srcX, srcY, srcW, srcH);
Brian Salomonbf6b9792019-08-21 09:38:10 -0400580 tempB->drawTexture(GrNoClip(), texProxy, filter, SkBlendMode::kSrc, SK_PMColor4fWHITE,
Brian Salomone9ad9982019-07-22 16:17:41 -0400581 srcSubset, dstRect, GrAA::kNo, GrQuadAAFlags::kNone, constraint,
582 SkMatrix::I(), std::move(xform));
583 }
Brian Salomonbf6b9792019-08-21 09:38:10 -0400584 texProxy = tempB->asTextureProxyRef();
585 tempA = std::move(tempB);
Brian Salomone9ad9982019-07-22 16:17:41 -0400586 srcX = srcY = 0;
587 srcW = nextW;
588 srcH = nextH;
589 constraint = SkCanvas::kFast_SrcRectConstraint;
590 }
Brian Salomonbf6b9792019-08-21 09:38:10 -0400591 SkASSERT(tempA);
592 return tempA;
Brian Salomone9ad9982019-07-22 16:17:41 -0400593}
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400594
595GrSurfaceContext::PixelTransferResult GrSurfaceContext::transferPixels(GrColorType dstCT,
596 const SkIRect& rect) {
597 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
598 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
599 auto direct = fContext->priv().asDirectContext();
600 if (!direct) {
601 return {};
602 }
603 auto rtProxy = this->asRenderTargetProxy();
604 if (rtProxy && rtProxy->wrapsVkSecondaryCB()) {
605 return {};
606 }
607
608 auto proxy = this->asSurfaceProxy();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400609 auto supportedRead = this->caps()->supportedReadPixelsColorType(this->colorInfo().colorType(),
610 proxy->backendFormat(), dstCT);
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400611 // Fail if read color type does not have all of dstCT's color channels and those missing color
612 // channels are in the src.
613 uint32_t dstComponents = GrColorTypeComponentFlags(dstCT);
614 uint32_t legalReadComponents = GrColorTypeComponentFlags(supportedRead.fColorType);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400615 uint32_t srcComponents = GrColorTypeComponentFlags(this->colorInfo().colorType());
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400616 if ((~legalReadComponents & dstComponents) & srcComponents) {
617 return {};
618 }
619
620 if (!this->caps()->transferBufferSupport() ||
621 !supportedRead.fOffsetAlignmentForTransferBuffer) {
622 return {};
623 }
624
625 size_t rowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * rect.width();
626 size_t size = rowBytes * rect.height();
627 auto buffer = direct->priv().resourceProvider()->createBuffer(
628 size, GrGpuBufferType::kXferGpuToCpu, GrAccessPattern::kStream_GrAccessPattern);
629 if (!buffer) {
630 return {};
631 }
632 auto srcRect = rect;
633 bool flip = proxy->origin() == kBottomLeft_GrSurfaceOrigin;
634 if (flip) {
635 srcRect = SkIRect::MakeLTRB(rect.fLeft, this->height() - rect.fBottom, rect.fRight,
636 this->height() - rect.fTop);
637 }
Greg Danielbbfec9d2019-08-20 10:56:51 -0400638 this->drawingManager()->newTransferFromRenderTask(this->asSurfaceProxyRef(), srcRect,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400639 this->colorInfo().colorType(),
Greg Danielbbfec9d2019-08-20 10:56:51 -0400640 supportedRead.fColorType, buffer, 0);
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400641 PixelTransferResult result;
642 result.fTransferBuffer = std::move(buffer);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400643 auto at = this->colorInfo().alphaType();
Brian Salomon8f8354a2019-07-31 20:12:02 -0400644 if (supportedRead.fColorType != dstCT || flip) {
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400645 result.fPixelConverter = [w = rect.width(), h = rect.height(), dstCT, supportedRead, at](
646 void* dst, const void* src) {
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400647 GrImageInfo srcInfo(supportedRead.fColorType, at, nullptr, w, h);
648 GrImageInfo dstInfo(dstCT, at, nullptr, w, h);
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400649 GrConvertPixels(dstInfo, dst, dstInfo.minRowBytes(),
650 srcInfo, src, srcInfo.minRowBytes(),
Brian Salomon8f8354a2019-07-31 20:12:02 -0400651 /* flipY = */ false);
Brian Salomon4d2d6f42019-07-26 14:15:11 -0400652 };
653 }
654 return result;
655}