blob: c12cc2e89a1ec59735c548ac1fcd152cb5e39658 [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"
15#include "src/gpu/GrDrawingManager.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040016#include "src/gpu/GrGpu.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040017#include "src/gpu/GrOpList.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;
Greg Daniel48fec762019-06-18 17:06:43 -040061 case GrColorType::kBGR_565: return false;
Greg Daniel6eb8c242019-06-05 10:22:24 -040062 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 Phillips66a46032019-06-18 08:00:42 -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 Phillips66a46032019-06-18 08:00:42 -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
Robert Phillipsc34d9932019-06-18 11:57:12 +0000231 bool convert = unpremul || needColorConversion;
232
Greg Daniel6eb8c242019-06-05 10:22:24 -0400233 bool flip = srcProxy->origin() == kBottomLeft_GrSurfaceOrigin;
Robert Phillipsc34d9932019-06-18 11:57:12 +0000234 if (flip) {
235 top = srcSurface->height() - top - height;
236 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400237
Robert Phillipsc34d9932019-06-18 11:57:12 +0000238 GrColorType allowedColorType = caps->supportedReadPixelsColorType(srcProxy->config(),
239 dstColorType);
240 convert = convert || (dstColorType != allowedColorType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400241
Robert Phillipsc34d9932019-06-18 11:57:12 +0000242 SkAutoPixmapStorage tempPixmap;
243 SkPixmap finalPixmap;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400244 if (convert) {
Robert Phillipsc34d9932019-06-18 11:57:12 +0000245 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());
Greg Daniel6eb8c242019-06-05 10:22:24 -0400272 }
273
274 direct->priv().flushSurface(srcProxy);
275
Robert Phillipsc34d9932019-06-18 11:57:12 +0000276 if (!direct->priv().getGpu()->readPixels(srcSurface, left, top, width, height, allowedColorType,
277 buffer, rowBytes)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400278 return false;
279 }
280
Robert Phillipsc34d9932019-06-18 11:57:12 +0000281 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 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400292 if (convert) {
Robert Phillipsc34d9932019-06-18 11:57:12 +0000293 if (!tempPixmap.readPixels(finalPixmap)) {
294 return false;
295 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400296 }
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
Greg Daniel2e52ad12019-06-13 10:04:16 -0400385 // It is more efficient for us to write pixels into a top left origin so we prefer that.
386 // However, if the final proxy isn't a render target then we must use a copy to move the
387 // data into it which requires the origins to match. If the final proxy is a render target
388 // we can use a draw instead which doesn't have this origin restriction. Thus for render
389 // targets we will use top left and otherwise we will make the origins match.
Robert Phillipsc34d9932019-06-18 11:57:12 +0000390 GrSurfaceOrigin tempOrigin = this->asRenderTargetContext() ? kTopLeft_GrSurfaceOrigin :
391 dstProxy->origin();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400392 auto tempProxy = direct->priv().proxyProvider()->createProxy(
Greg Daniel2e52ad12019-06-13 10:04:16 -0400393 format, desc, tempOrigin, SkBackingFit::kApprox, SkBudgeted::kYes);
394
Greg Daniel6eb8c242019-06-05 10:22:24 -0400395 if (!tempProxy) {
396 return false;
397 }
398 auto tempCtx = direct->priv().drawingManager()->makeTextureContext(
399 tempProxy, this->colorSpaceInfo().refColorSpace());
400 if (!tempCtx) {
401 return false;
402 }
403 uint32_t flags = canvas2DFastPath ? 0 : pixelOpsFlags;
404
405 // In the fast path we always write the srcData to the temp context as though it were RGBA.
406 // When the data is really BGRA the write will cause the R and B channels to be swapped in
407 // the intermediate surface which gets corrected by a swizzle effect when drawing to the
408 // dst.
409 auto tmpColorType = canvas2DFastPath ? GrColorType::kRGBA_8888 : srcColorType;
410 if (!tempCtx->writePixelsImpl(direct, 0, 0, width, height, tmpColorType, srcColorSpace,
411 srcBuffer, srcRowBytes, flags)) {
412 return false;
413 }
414
415 if (this->asRenderTargetContext()) {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400416 std::unique_ptr<GrFragmentProcessor> fp;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400417 if (canvas2DFastPath) {
418 fp = direct->priv().createUPMToPMEffect(
419 GrSimpleTextureEffect::Make(std::move(tempProxy), SkMatrix::I()));
420 if (srcColorType == GrColorType::kBGRA_8888) {
421 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
422 }
423 } else {
424 fp = GrSimpleTextureEffect::Make(std::move(tempProxy), SkMatrix::I());
425 }
426 if (!fp) {
427 return false;
428 }
429 GrPaint paint;
430 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
431 paint.addColorFragmentProcessor(std::move(fp));
432 this->asRenderTargetContext()->fillRectToRect(
433 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
434 SkRect::MakeXYWH(left, top, width, height), SkRect::MakeWH(width, height));
435 } else {
436 SkIRect srcRect = SkIRect::MakeWH(width, height);
437 SkIPoint dstPoint = SkIPoint::Make(left, top);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400438 if (!this->copy(tempProxy.get(), srcRect, dstPoint)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400439 return false;
440 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400441 }
442 return true;
443 }
444
Robert Phillipsc34d9932019-06-18 11:57:12 +0000445 bool convert = premul || needColorConversion;
446
Greg Daniel6eb8c242019-06-05 10:22:24 -0400447 if (!valid_pixel_conversion(srcColorType, dstProxy->config(), premul)) {
448 return false;
449 }
450
451 GrColorType allowedColorType = caps->supportedWritePixelsColorType(dstProxy->config(),
452 srcColorType);
Robert Phillipsc34d9932019-06-18 11:57:12 +0000453 convert = convert || (srcColorType != allowedColorType);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400454
Robert Phillipsc34d9932019-06-18 11:57:12 +0000455 std::unique_ptr<char[]> tempBuffer;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400456 if (convert) {
Robert Phillipsc34d9932019-06-18 11:57:12 +0000457 auto srcSkColorType = GrColorTypeToSkColorType(srcColorType);
458 auto dstSkColorType = GrColorTypeToSkColorType(allowedColorType);
459 if (kUnknown_SkColorType == srcSkColorType || kUnknown_SkColorType == dstSkColorType) {
460 return false;
461 }
462 auto srcAlphaType = SkColorTypeIsAlwaysOpaque(srcSkColorType)
463 ? kOpaque_SkAlphaType
464 : (premul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType);
465 SkPixmap src(SkImageInfo::Make(width, height, srcSkColorType, srcAlphaType,
466 sk_ref_sp(srcColorSpace)),
467 srcBuffer, srcRowBytes);
468 auto tempSrcII = SkImageInfo::Make(width, height, dstSkColorType, kPremul_SkAlphaType,
469 this->colorSpaceInfo().refColorSpace());
470 auto size = tempSrcII.computeMinByteSize();
471 if (!size) {
472 return false;
473 }
474 tempBuffer.reset(new char[size]);
475 SkPixmap tempSrc(tempSrcII, tempBuffer.get(), tempSrcII.minRowBytes());
476 if (!src.readPixels(tempSrc)) {
477 return false;
478 }
479 srcColorType = allowedColorType;
480 srcBuffer = tempSrc.addr();
481 srcRowBytes = tempSrc.rowBytes();
Greg Daniel6eb8c242019-06-05 10:22:24 -0400482 if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) {
Robert Phillipsc34d9932019-06-18 11:57:12 +0000483 std::unique_ptr<char[]> row(new char[srcRowBytes]);
484 for (int y = 0; y < height / 2; ++y) {
485 memcpy(row.get(), tempSrc.addr(0, y), srcRowBytes);
486 memcpy(tempSrc.writable_addr(0, y), tempSrc.addr(0, height - 1 - y), srcRowBytes);
487 memcpy(tempSrc.writable_addr(0, height - 1 - y), row.get(), srcRowBytes);
488 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400489 top = dstSurface->height() - top - height;
490 }
Robert Phillipsc34d9932019-06-18 11:57:12 +0000491 } else if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) {
492 size_t trimRowBytes = GrColorTypeBytesPerPixel(srcColorType) * width;
493 tempBuffer.reset(new char[trimRowBytes * height]);
494 char* dst = reinterpret_cast<char*>(tempBuffer.get()) + trimRowBytes * (height - 1);
495 const char* src = reinterpret_cast<const char*>(srcBuffer);
496 for (int i = 0; i < height; ++i, src += srcRowBytes, dst -= trimRowBytes) {
497 memcpy(dst, src, trimRowBytes);
498 }
499 srcBuffer = tempBuffer.get();
500 srcRowBytes = trimRowBytes;
501 top = dstSurface->height() - top - height;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400502 }
503
504 // On platforms that prefer flushes over VRAM use (i.e., ANGLE) we're better off forcing a
505 // complete flush here. On platforms that prefer VRAM use over flushes we're better off
506 // giving the drawing manager the chance of skipping the flush (i.e., by passing in the
507 // destination proxy)
508 // TODO: should this policy decision just be moved into the drawing manager?
509 direct->priv().flushSurface(caps->preferVRAMUseOverFlushes() ? dstProxy : nullptr);
510
511 return direct->priv().getGpu()->writePixels(dstSurface, left, top, width, height, srcColorType,
512 srcBuffer, srcRowBytes);
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400513}
514
515bool GrSurfaceContext::writePixels(const SkImageInfo& srcInfo, const void* srcBuffer,
516 size_t srcRowBytes, int x, int y, uint32_t flags) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400517 ASSERT_SINGLE_OWNER
518 RETURN_FALSE_IF_ABANDONED
519 SkDEBUGCODE(this->validate();)
Robert Phillips0d075de2019-03-04 11:08:13 -0500520 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::writePixels");
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400521
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400522 if (kUnpremul_SkAlphaType == srcInfo.alphaType()) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400523 flags |= kUnpremul_PixelOpsFlag;
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400524 }
Brian Salomonc320b152018-02-20 14:05:36 -0500525 auto colorType = SkColorTypeToGrColorType(srcInfo.colorType());
526 if (GrColorType::kUnknown == colorType) {
527 return false;
528 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500529
530 auto direct = fContext->priv().asDirectContext();
531 if (!direct) {
532 return false;
533 }
534
Greg Danielb3f82dd2019-05-29 14:24:32 -0400535 if (this->asSurfaceProxy()->readOnly()) {
536 return false;
537 }
538
Greg Daniel6eb8c242019-06-05 10:22:24 -0400539 return this->writePixelsImpl(direct, x, y, srcInfo.width(), srcInfo.height(), colorType,
540 srcInfo.colorSpace(), srcBuffer, srcRowBytes, flags);
Brian Osman45580d32016-11-23 09:37:01 -0500541}
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400542
543bool GrSurfaceContext::copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
544 ASSERT_SINGLE_OWNER
545 RETURN_FALSE_IF_ABANDONED
546 SkDEBUGCODE(this->validate();)
Greg Daniel46cfbc62019-06-07 11:43:30 -0400547 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy");
Greg Daniel25af6712018-04-25 10:44:38 -0400548
Greg Daniel46cfbc62019-06-07 11:43:30 -0400549 SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal);
550 SkASSERT(src->origin() == this->asSurfaceProxy()->origin());
551
552 GrSurfaceProxy* dst = this->asSurfaceProxy();
553
554 if (!fContext->priv().caps()->canCopySurface(dst, src, srcRect, dstPoint)) {
Greg Daniel25af6712018-04-25 10:44:38 -0400555 return false;
556 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400557
Greg Daniel46cfbc62019-06-07 11:43:30 -0400558 return this->getOpList()->copySurface(fContext, dst, src, srcRect, dstPoint);
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400559}
Greg Daniel46cfbc62019-06-07 11:43:30 -0400560