blob: f25b222902a5b4bd6027e629190f7cd4fb876ff3 [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/GrAuditTrail.h"
11#include "include/private/GrOpList.h"
12#include "include/private/GrRecordingContext.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040013#include "src/core/SkAutoPixmapStorage.h"
14#include "src/gpu/GrClip.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrContextPriv.h"
16#include "src/gpu/GrDrawingManager.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040017#include "src/gpu/GrGpu.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrRecordingContextPriv.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040019#include "src/gpu/GrRenderTargetContext.h"
Greg Daniel46cfbc62019-06-07 11:43:30 -040020#include "src/gpu/GrSurfaceContextPriv.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040021#include "src/gpu/GrSurfacePriv.h"
22#include "src/gpu/GrTextureContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/SkGr.h"
Brian Osman45580d32016-11-23 09:37:01 -050024
Robert Phillips2de8cfa2017-06-28 10:33:41 -040025#define ASSERT_SINGLE_OWNER \
26 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
Robert Phillips69893702019-02-22 11:16:30 -050027#define RETURN_FALSE_IF_ABANDONED if (this->fContext->priv().abandoned()) { return false; }
Brian Osman45580d32016-11-23 09:37:01 -050028
29// In MDB mode the reffing of the 'getLastOpList' call's result allows in-progress
30// GrOpLists to be picked up and added to by renderTargetContexts lower in the call
31// stack. When this occurs with a closed GrOpList, a new one will be allocated
32// when the renderTargetContext attempts to use it (via getOpList).
Robert Phillips69893702019-02-22 11:16:30 -050033GrSurfaceContext::GrSurfaceContext(GrRecordingContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -040034 GrPixelConfig config,
Robert Phillips0d075de2019-03-04 11:08:13 -050035 sk_sp<SkColorSpace> colorSpace)
Brian Salomonf3569f02017-10-24 12:52:33 -040036 : fContext(context)
Robert Phillips0d075de2019-03-04 11:08:13 -050037 , fColorSpaceInfo(std::move(colorSpace), config) {
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040038}
39
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 -040057static bool valid_premul_color_type(GrColorType ct) {
58 switch (ct) {
59 case GrColorType::kUnknown: return false;
60 case GrColorType::kAlpha_8: return false;
61 case GrColorType::kRGB_565: return false;
62 case GrColorType::kABGR_4444: return true;
63 case GrColorType::kRGBA_8888: return true;
64 case GrColorType::kRGB_888x: return false;
65 case GrColorType::kRG_88: return false;
66 case GrColorType::kBGRA_8888: return true;
67 case GrColorType::kRGBA_1010102: return true;
68 case GrColorType::kGray_8: return false;
69 case GrColorType::kAlpha_F16: return false;
70 case GrColorType::kRGBA_F16: return true;
71 case GrColorType::kRGBA_F16_Clamped: return true;
72 case GrColorType::kRG_F32: return false;
73 case GrColorType::kRGBA_F32: return true;
74 case GrColorType::kRGB_ETC1: return false;
Robert Phillipsfe18de52019-06-06 17:21:50 -040075 case GrColorType::kR_16: return false;
76 case GrColorType::kRG_1616: return false;
Robert Phillips15662252019-06-12 13:33:48 -040077 // Experimental (for Y416 and mutant P016/P010)
78 case GrColorType::kRGBA_16161616: return false;
79 case GrColorType::kRG_half: return false;
Greg Daniel6eb8c242019-06-05 10:22:24 -040080 }
81 SK_ABORT("Invalid GrColorType");
82 return false;
83}
84
85// TODO: This will be removed when GrSurfaceContexts are aware of their color types.
86// (skbug.com/6718)
87static bool valid_premul_config(GrPixelConfig config) {
88 switch (config) {
89 case kUnknown_GrPixelConfig: return false;
90 case kAlpha_8_GrPixelConfig: return false;
91 case kGray_8_GrPixelConfig: return false;
92 case kRGB_565_GrPixelConfig: return false;
93 case kRGBA_4444_GrPixelConfig: return true;
94 case kRGBA_8888_GrPixelConfig: return true;
95 case kRGB_888_GrPixelConfig: return false;
96 case kRGB_888X_GrPixelConfig: return false;
97 case kRG_88_GrPixelConfig: return false;
98 case kBGRA_8888_GrPixelConfig: return true;
99 case kSRGBA_8888_GrPixelConfig: return true;
100 case kSBGRA_8888_GrPixelConfig: return true;
101 case kRGBA_1010102_GrPixelConfig: return true;
102 case kRGBA_float_GrPixelConfig: return true;
103 case kRG_float_GrPixelConfig: return false;
104 case kAlpha_half_GrPixelConfig: return false;
105 case kRGBA_half_GrPixelConfig: return true;
106 case kRGBA_half_Clamped_GrPixelConfig: return true;
107 case kRGB_ETC1_GrPixelConfig: return false;
108 case kAlpha_8_as_Alpha_GrPixelConfig: return false;
109 case kAlpha_8_as_Red_GrPixelConfig: return false;
110 case kAlpha_half_as_Red_GrPixelConfig: return false;
111 case kGray_8_as_Lum_GrPixelConfig: return false;
112 case kGray_8_as_Red_GrPixelConfig: return false;
Robert Phillipsfe18de52019-06-06 17:21:50 -0400113 case kR_16_GrPixelConfig: return false;
114 case kRG_1616_GrPixelConfig: return false;
Robert Phillips15662252019-06-12 13:33:48 -0400115 // Experimental (for Y416 and mutant P016/P010)
116 case kRGBA_16161616_GrPixelConfig: return false;
117 case kRG_half_GrPixelConfig: return false;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400118 }
119 SK_ABORT("Invalid GrPixelConfig");
120 return false;
121}
122
123static bool valid_pixel_conversion(GrColorType cpuColorType, GrPixelConfig gpuConfig,
124 bool premulConversion) {
125 // We only allow premul <-> unpremul conversions for some formats
126 if (premulConversion &&
127 (!valid_premul_color_type(cpuColorType) || !valid_premul_config(gpuConfig))) {
128 return false;
129 }
130 return true;
131}
132
133bool GrSurfaceContext::readPixelsImpl(GrContext* direct, int left, int top, int width,
134 int height, GrColorType dstColorType,
135 SkColorSpace* dstColorSpace, void* buffer, size_t rowBytes,
136 uint32_t pixelOpsFlags) {
137 SkASSERT(buffer);
138
139 GrSurfaceProxy* srcProxy = this->asSurfaceProxy();
140
141 // MDB TODO: delay this instantiation until later in the method
142 if (!srcProxy->instantiate(direct->priv().resourceProvider())) {
143 return false;
144 }
145
146 GrSurface* srcSurface = srcProxy->peekSurface();
147
148 if (!GrSurfacePriv::AdjustReadPixelParams(srcSurface->width(), srcSurface->height(),
149 GrColorTypeBytesPerPixel(dstColorType), &left, &top,
150 &width, &height, &buffer, &rowBytes)) {
151 return false;
152 }
153
154 // TODO: Make GrSurfaceContext know its alpha type and pass dst buffer's alpha type.
155 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
156
157 if (!valid_pixel_conversion(dstColorType, srcProxy->config(), unpremul)) {
158 return false;
159 }
160
161 bool needColorConversion =
162 SkColorSpaceXformSteps::Required(this->colorSpaceInfo().colorSpace(), dstColorSpace);
163
164 const GrCaps* caps = direct->priv().caps();
165 // This is the getImageData equivalent to the canvas2D putImageData fast path. We probably don't
166 // care so much about getImageData performance. However, in order to ensure putImageData/
167 // getImageData in "legacy" mode are round-trippable we use the GPU to do the complementary
168 // unpremul step to writeSurfacePixels's premul step (which is determined empirically in
169 // fContext->vaildaPMUPMConversionExists()).
170 bool canvas2DFastPath =
171 unpremul &&
172 !needColorConversion &&
173 (GrColorType::kRGBA_8888 == dstColorType || GrColorType::kBGRA_8888 == dstColorType) &&
174 SkToBool(srcProxy->asTextureProxy()) &&
175 (srcProxy->config() == kRGBA_8888_GrPixelConfig ||
176 srcProxy->config() == kBGRA_8888_GrPixelConfig) &&
177 caps->isConfigRenderable(kRGBA_8888_GrPixelConfig) &&
178 direct->priv().validPMUPMConversionExists();
179
180 if (!caps->surfaceSupportsReadPixels(srcSurface) ||
181 canvas2DFastPath) {
182 GrBackendFormat format;
183 GrPixelConfig config;
184 if (canvas2DFastPath) {
185 config = kRGBA_8888_GrPixelConfig;
186 format = caps->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
187 } else {
188 config = srcProxy->config();
189 format = srcProxy->backendFormat().makeTexture2D();
190 if (!format.isValid()) {
191 return false;
192 }
193 }
194 sk_sp<SkColorSpace> cs = canvas2DFastPath ? nullptr : this->colorSpaceInfo().refColorSpace();
195
196 sk_sp<GrRenderTargetContext> tempCtx = direct->priv().makeDeferredRenderTargetContext(
197 format, SkBackingFit::kApprox, width, height, config, std::move(cs), 1,
198 GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin, nullptr, SkBudgeted::kYes);
199 if (!tempCtx) {
200 return false;
201 }
202
203 std::unique_ptr<GrFragmentProcessor> fp;
204 if (canvas2DFastPath) {
205 fp = direct->priv().createPMToUPMEffect(
206 GrSimpleTextureEffect::Make(sk_ref_sp(srcProxy->asTextureProxy()),
207 SkMatrix::I()));
208 if (dstColorType == GrColorType::kBGRA_8888) {
209 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
210 dstColorType = GrColorType::kRGBA_8888;
211 }
212 } else {
213 fp = GrSimpleTextureEffect::Make(sk_ref_sp(srcProxy->asTextureProxy()), SkMatrix::I());
214 }
215 if (!fp) {
216 return false;
217 }
218 GrPaint paint;
219 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
220 paint.addColorFragmentProcessor(std::move(fp));
221
222 tempCtx->asRenderTargetContext()->fillRectToRect(
223 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
224 SkRect::MakeWH(width, height), SkRect::MakeXYWH(left, top, width, height));
225
226 uint32_t flags = canvas2DFastPath ? 0 : pixelOpsFlags;
227 return tempCtx->readPixelsImpl(direct, 0, 0, width, height, dstColorType, dstColorSpace,
228 buffer, rowBytes, flags);
229 }
230
231 bool convert = unpremul || needColorConversion;
232
233 bool flip = srcProxy->origin() == kBottomLeft_GrSurfaceOrigin;
234 if (flip) {
235 top = srcSurface->height() - top - height;
236 }
237
238 GrColorType allowedColorType = caps->supportedReadPixelsColorType(srcProxy->config(),
239 dstColorType);
240 convert = convert || (dstColorType != allowedColorType);
241
242 SkAutoPixmapStorage tempPixmap;
243 SkPixmap finalPixmap;
244 if (convert) {
245 SkColorType srcSkColorType = GrColorTypeToSkColorType(allowedColorType);
246 SkColorType dstSkColorType = GrColorTypeToSkColorType(dstColorType);
247 bool srcAlwaysOpaque = SkColorTypeIsAlwaysOpaque(srcSkColorType);
248 bool dstAlwaysOpaque = SkColorTypeIsAlwaysOpaque(dstSkColorType);
249 if (kUnknown_SkColorType == srcSkColorType || kUnknown_SkColorType == dstSkColorType) {
250 return false;
251 }
252 auto tempAT = srcAlwaysOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
253 auto tempII = SkImageInfo::Make(width, height, srcSkColorType, tempAT,
254 this->colorSpaceInfo().refColorSpace());
255 SkASSERT(!unpremul || !dstAlwaysOpaque);
256 auto finalAT = (srcAlwaysOpaque || dstAlwaysOpaque)
257 ? kOpaque_SkAlphaType
258 : unpremul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType;
259 auto finalII =
260 SkImageInfo::Make(width, height, dstSkColorType, finalAT, sk_ref_sp(dstColorSpace));
261 if (!SkImageInfoValidConversion(finalII, tempII)) {
262 return false;
263 }
264 if (!tempPixmap.tryAlloc(tempII)) {
265 return false;
266 }
267 finalPixmap.reset(finalII, buffer, rowBytes);
268 buffer = tempPixmap.writable_addr();
269 rowBytes = tempPixmap.rowBytes();
270 // Chrome msan bots require this.
271 sk_bzero(buffer, tempPixmap.computeByteSize());
272 }
273
274 direct->priv().flushSurface(srcProxy);
275
276 if (!direct->priv().getGpu()->readPixels(srcSurface, left, top, width, height, allowedColorType,
277 buffer, rowBytes)) {
278 return false;
279 }
280
281 if (flip) {
282 size_t trimRowBytes = GrColorTypeBytesPerPixel(allowedColorType) * width;
283 std::unique_ptr<char[]> row(new char[trimRowBytes]);
284 char* upper = reinterpret_cast<char*>(buffer);
285 char* lower = reinterpret_cast<char*>(buffer) + (height - 1) * rowBytes;
286 for (int y = 0; y < height / 2; ++y, upper += rowBytes, lower -= rowBytes) {
287 memcpy(row.get(), upper, trimRowBytes);
288 memcpy(upper, lower, trimRowBytes);
289 memcpy(lower, row.get(), trimRowBytes);
290 }
291 }
292 if (convert) {
293 if (!tempPixmap.readPixels(finalPixmap)) {
294 return false;
295 }
296 }
297 return true;
298}
Robert Phillips0d075de2019-03-04 11:08:13 -0500299
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400300bool GrSurfaceContext::readPixels(const SkImageInfo& dstInfo, void* dstBuffer,
301 size_t dstRowBytes, int x, int y, uint32_t flags) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400302 ASSERT_SINGLE_OWNER
303 RETURN_FALSE_IF_ABANDONED
304 SkDEBUGCODE(this->validate();)
Robert Phillips0d075de2019-03-04 11:08:13 -0500305 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::readPixels");
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400306
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400307 // TODO: this seems to duplicate code in SkImage_Gpu::onReadPixels
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400308 if (kUnpremul_SkAlphaType == dstInfo.alphaType() &&
309 !GrPixelConfigIsOpaque(this->asSurfaceProxy()->config())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400310 flags |= kUnpremul_PixelOpsFlag;
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400311 }
Brian Salomonc320b152018-02-20 14:05:36 -0500312 auto colorType = SkColorTypeToGrColorType(dstInfo.colorType());
313 if (GrColorType::kUnknown == colorType) {
314 return false;
315 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500316
317 auto direct = fContext->priv().asDirectContext();
318 if (!direct) {
319 return false;
320 }
321
Greg Daniel6eb8c242019-06-05 10:22:24 -0400322 return this->readPixelsImpl(direct, x, y, dstInfo.width(), dstInfo.height(), colorType,
323 dstInfo.colorSpace(), dstBuffer, dstRowBytes, flags);
324}
325
326bool GrSurfaceContext::writePixelsImpl(GrContext* direct, int left, int top, int width, int height,
327 GrColorType srcColorType, SkColorSpace* srcColorSpace,
328 const void* srcBuffer, size_t srcRowBytes,
329 uint32_t pixelOpsFlags) {
330 if (GrColorType::kUnknown == srcColorType) {
331 return false;
332 }
333
334 GrSurfaceProxy* dstProxy = this->asSurfaceProxy();
335 if (!dstProxy->instantiate(direct->priv().resourceProvider())) {
336 return false;
337 }
338
339 GrSurface* dstSurface = dstProxy->peekSurface();
340
341 if (!GrSurfacePriv::AdjustWritePixelParams(dstSurface->width(), dstSurface->height(),
342 GrColorTypeBytesPerPixel(srcColorType), &left, &top,
343 &width, &height, &srcBuffer, &srcRowBytes)) {
344 return false;
345 }
346
347 // TODO: Make GrSurfaceContext know its alpha type and pass src buffer's alpha type.
348 bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
349
350 bool needColorConversion =
351 SkColorSpaceXformSteps::Required(srcColorSpace, this->colorSpaceInfo().colorSpace());
352
353 const GrCaps* caps = direct->priv().caps();
354 // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs
355 // that are premultiplied on the GPU. This is kept as narrow as possible for now.
356 bool canvas2DFastPath =
357 !caps->avoidWritePixelsFastPath() &&
358 premul &&
359 !needColorConversion &&
360 (srcColorType == GrColorType::kRGBA_8888 || srcColorType == GrColorType::kBGRA_8888) &&
361 SkToBool(this->asRenderTargetContext()) &&
362 (dstProxy->config() == kRGBA_8888_GrPixelConfig ||
363 dstProxy->config() == kBGRA_8888_GrPixelConfig) &&
364 direct->priv().caps()->isConfigTexturable(kRGBA_8888_GrPixelConfig) &&
365 direct->priv().validPMUPMConversionExists();
366
367 if (!caps->surfaceSupportsWritePixels(dstSurface) || canvas2DFastPath) {
368 GrSurfaceDesc desc;
369 desc.fWidth = width;
370 desc.fHeight = height;
371 desc.fSampleCnt = 1;
372
373 GrBackendFormat format;
374 if (canvas2DFastPath) {
375 desc.fConfig = kRGBA_8888_GrPixelConfig;
376 format = caps->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
377 } else {
378 desc.fConfig = dstProxy->config();
379 format = dstProxy->backendFormat().makeTexture2D();
380 if (!format.isValid()) {
381 return false;
382 }
383 }
384
385 auto tempProxy = direct->priv().proxyProvider()->createProxy(
Greg Daniel46cfbc62019-06-07 11:43:30 -0400386 format, desc, dstProxy->origin(), SkBackingFit::kApprox, SkBudgeted::kYes);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400387 if (!tempProxy) {
388 return false;
389 }
390 auto tempCtx = direct->priv().drawingManager()->makeTextureContext(
391 tempProxy, this->colorSpaceInfo().refColorSpace());
392 if (!tempCtx) {
393 return false;
394 }
395 uint32_t flags = canvas2DFastPath ? 0 : pixelOpsFlags;
396
397 // In the fast path we always write the srcData to the temp context as though it were RGBA.
398 // When the data is really BGRA the write will cause the R and B channels to be swapped in
399 // the intermediate surface which gets corrected by a swizzle effect when drawing to the
400 // dst.
401 auto tmpColorType = canvas2DFastPath ? GrColorType::kRGBA_8888 : srcColorType;
402 if (!tempCtx->writePixelsImpl(direct, 0, 0, width, height, tmpColorType, srcColorSpace,
403 srcBuffer, srcRowBytes, flags)) {
404 return false;
405 }
406
407 if (this->asRenderTargetContext()) {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400408 std::unique_ptr<GrFragmentProcessor> fp;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400409 if (canvas2DFastPath) {
410 fp = direct->priv().createUPMToPMEffect(
411 GrSimpleTextureEffect::Make(std::move(tempProxy), SkMatrix::I()));
412 if (srcColorType == GrColorType::kBGRA_8888) {
413 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
414 }
415 } else {
416 fp = GrSimpleTextureEffect::Make(std::move(tempProxy), SkMatrix::I());
417 }
418 if (!fp) {
419 return false;
420 }
421 GrPaint paint;
422 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
423 paint.addColorFragmentProcessor(std::move(fp));
424 this->asRenderTargetContext()->fillRectToRect(
425 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
426 SkRect::MakeXYWH(left, top, width, height), SkRect::MakeWH(width, height));
427 } else {
428 SkIRect srcRect = SkIRect::MakeWH(width, height);
429 SkIPoint dstPoint = SkIPoint::Make(left, top);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400430 if (!this->copy(tempProxy.get(), srcRect, dstPoint)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400431 return false;
432 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400433 }
434 return true;
435 }
436
437 bool convert = premul || needColorConversion;
438
439 if (!valid_pixel_conversion(srcColorType, dstProxy->config(), premul)) {
440 return false;
441 }
442
443 GrColorType allowedColorType = caps->supportedWritePixelsColorType(dstProxy->config(),
444 srcColorType);
445 convert = convert || (srcColorType != allowedColorType);
446
447 std::unique_ptr<char[]> tempBuffer;
448 if (convert) {
449 auto srcSkColorType = GrColorTypeToSkColorType(srcColorType);
450 auto dstSkColorType = GrColorTypeToSkColorType(allowedColorType);
451 if (kUnknown_SkColorType == srcSkColorType || kUnknown_SkColorType == dstSkColorType) {
452 return false;
453 }
454 auto srcAlphaType = SkColorTypeIsAlwaysOpaque(srcSkColorType)
455 ? kOpaque_SkAlphaType
456 : (premul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType);
457 SkPixmap src(SkImageInfo::Make(width, height, srcSkColorType, srcAlphaType,
458 sk_ref_sp(srcColorSpace)),
459 srcBuffer, srcRowBytes);
460 auto tempSrcII = SkImageInfo::Make(width, height, dstSkColorType, kPremul_SkAlphaType,
461 this->colorSpaceInfo().refColorSpace());
462 auto size = tempSrcII.computeMinByteSize();
463 if (!size) {
464 return false;
465 }
466 tempBuffer.reset(new char[size]);
467 SkPixmap tempSrc(tempSrcII, tempBuffer.get(), tempSrcII.minRowBytes());
468 if (!src.readPixels(tempSrc)) {
469 return false;
470 }
471 srcColorType = allowedColorType;
472 srcBuffer = tempSrc.addr();
473 srcRowBytes = tempSrc.rowBytes();
474 if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) {
475 std::unique_ptr<char[]> row(new char[srcRowBytes]);
476 for (int y = 0; y < height / 2; ++y) {
477 memcpy(row.get(), tempSrc.addr(0, y), srcRowBytes);
478 memcpy(tempSrc.writable_addr(0, y), tempSrc.addr(0, height - 1 - y), srcRowBytes);
479 memcpy(tempSrc.writable_addr(0, height - 1 - y), row.get(), srcRowBytes);
480 }
481 top = dstSurface->height() - top - height;
482 }
483 } else if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) {
484 size_t trimRowBytes = GrColorTypeBytesPerPixel(srcColorType) * width;
485 tempBuffer.reset(new char[trimRowBytes * height]);
486 char* dst = reinterpret_cast<char*>(tempBuffer.get()) + trimRowBytes * (height - 1);
487 const char* src = reinterpret_cast<const char*>(srcBuffer);
488 for (int i = 0; i < height; ++i, src += srcRowBytes, dst -= trimRowBytes) {
489 memcpy(dst, src, trimRowBytes);
490 }
491 srcBuffer = tempBuffer.get();
492 srcRowBytes = trimRowBytes;
493 top = dstSurface->height() - top - height;
494 }
495
496 // On platforms that prefer flushes over VRAM use (i.e., ANGLE) we're better off forcing a
497 // complete flush here. On platforms that prefer VRAM use over flushes we're better off
498 // giving the drawing manager the chance of skipping the flush (i.e., by passing in the
499 // destination proxy)
500 // TODO: should this policy decision just be moved into the drawing manager?
501 direct->priv().flushSurface(caps->preferVRAMUseOverFlushes() ? dstProxy : nullptr);
502
503 return direct->priv().getGpu()->writePixels(dstSurface, left, top, width, height, srcColorType,
504 srcBuffer, srcRowBytes);
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400505}
506
507bool GrSurfaceContext::writePixels(const SkImageInfo& srcInfo, const void* srcBuffer,
508 size_t srcRowBytes, int x, int y, uint32_t flags) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400509 ASSERT_SINGLE_OWNER
510 RETURN_FALSE_IF_ABANDONED
511 SkDEBUGCODE(this->validate();)
Robert Phillips0d075de2019-03-04 11:08:13 -0500512 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::writePixels");
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400513
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400514 if (kUnpremul_SkAlphaType == srcInfo.alphaType()) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400515 flags |= kUnpremul_PixelOpsFlag;
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400516 }
Brian Salomonc320b152018-02-20 14:05:36 -0500517 auto colorType = SkColorTypeToGrColorType(srcInfo.colorType());
518 if (GrColorType::kUnknown == colorType) {
519 return false;
520 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500521
522 auto direct = fContext->priv().asDirectContext();
523 if (!direct) {
524 return false;
525 }
526
Greg Danielb3f82dd2019-05-29 14:24:32 -0400527 if (this->asSurfaceProxy()->readOnly()) {
528 return false;
529 }
530
Greg Daniel6eb8c242019-06-05 10:22:24 -0400531 return this->writePixelsImpl(direct, x, y, srcInfo.width(), srcInfo.height(), colorType,
532 srcInfo.colorSpace(), srcBuffer, srcRowBytes, flags);
Brian Osman45580d32016-11-23 09:37:01 -0500533}
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400534
535bool GrSurfaceContext::copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
536 ASSERT_SINGLE_OWNER
537 RETURN_FALSE_IF_ABANDONED
538 SkDEBUGCODE(this->validate();)
Greg Daniel46cfbc62019-06-07 11:43:30 -0400539 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy");
Greg Daniel25af6712018-04-25 10:44:38 -0400540
Greg Daniel46cfbc62019-06-07 11:43:30 -0400541 SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal);
542 SkASSERT(src->origin() == this->asSurfaceProxy()->origin());
543
544 GrSurfaceProxy* dst = this->asSurfaceProxy();
545
546 if (!fContext->priv().caps()->canCopySurface(dst, src, srcRect, dstPoint)) {
Greg Daniel25af6712018-04-25 10:44:38 -0400547 return false;
548 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400549
Greg Daniel46cfbc62019-06-07 11:43:30 -0400550 return this->getOpList()->copySurface(fContext, dst, src, srcRect, dstPoint);
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400551}
Greg Daniel46cfbc62019-06-07 11:43:30 -0400552