1 /*
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 
8 #include "GrSurfaceContext.h"
9 #include "GrContextPriv.h"
10 #include "GrDrawingManager.h"
11 #include "GrOpList.h"
12 #include "SkGr.h"
13 #include "../private/GrAuditTrail.h"
14 
15 #define ASSERT_SINGLE_OWNER \
16     SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
17 #define RETURN_FALSE_IF_ABANDONED  if (this->drawingManager()->wasAbandoned()) { return false; }
18 
19 // In MDB mode the reffing of the 'getLastOpList' call's result allows in-progress
20 // GrOpLists to be picked up and added to by renderTargetContexts lower in the call
21 // stack. When this occurs with a closed GrOpList, a new one will be allocated
22 // when the renderTargetContext attempts to use it (via getOpList).
23 GrSurfaceContext::GrSurfaceContext(GrContext* context,
24                                    GrDrawingManager* drawingMgr,
25                                    GrPixelConfig config,
26                                    sk_sp<SkColorSpace> colorSpace,
27                                    GrAuditTrail* auditTrail,
28                                    GrSingleOwner* singleOwner)
29         : fContext(context)
30         , fAuditTrail(auditTrail)
31         , fColorSpaceInfo(std::move(colorSpace), config)
32         , fDrawingManager(drawingMgr)
33 #ifdef SK_DEBUG
34         , fSingleOwner(singleOwner)
35 #endif
36 {
37 }
38 
39 bool GrSurfaceContext::readPixels(const SkImageInfo& dstInfo, void* dstBuffer,
40                                   size_t dstRowBytes, int x, int y, uint32_t flags) {
41     ASSERT_SINGLE_OWNER
42     RETURN_FALSE_IF_ABANDONED
43     SkDEBUGCODE(this->validate();)
44     GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrSurfaceContext::readPixels");
45 
46     // TODO: this seems to duplicate code in SkImage_Gpu::onReadPixels
47     if (kUnpremul_SkAlphaType == dstInfo.alphaType() &&
48         !GrPixelConfigIsOpaque(this->asSurfaceProxy()->config())) {
49         flags |= GrContextPriv::kUnpremul_PixelOpsFlag;
50     }
51     auto colorType = SkColorTypeToGrColorType(dstInfo.colorType());
52     if (GrColorType::kUnknown == colorType) {
53         return false;
54     }
55     return fContext->contextPriv().readSurfacePixels(this, x, y, dstInfo.width(), dstInfo.height(),
56                                                      colorType, dstInfo.colorSpace(), dstBuffer,
57                                                      dstRowBytes, flags);
58 }
59 
60 bool GrSurfaceContext::writePixels(const SkImageInfo& srcInfo, const void* srcBuffer,
61                                    size_t srcRowBytes, int x, int y, uint32_t flags) {
62     ASSERT_SINGLE_OWNER
63     RETURN_FALSE_IF_ABANDONED
64     SkDEBUGCODE(this->validate();)
65     GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrSurfaceContext::writePixels");
66 
67     if (kUnpremul_SkAlphaType == srcInfo.alphaType()) {
68         flags |= GrContextPriv::kUnpremul_PixelOpsFlag;
69     }
70     auto colorType = SkColorTypeToGrColorType(srcInfo.colorType());
71     if (GrColorType::kUnknown == colorType) {
72         return false;
73     }
74     return fContext->contextPriv().writeSurfacePixels(this, x, y, srcInfo.width(), srcInfo.height(),
75                                                       colorType, srcInfo.colorSpace(), srcBuffer,
76                                                       srcRowBytes, flags);
77 }
78 
79 bool GrSurfaceContext::copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
80     ASSERT_SINGLE_OWNER
81     RETURN_FALSE_IF_ABANDONED
82     SkDEBUGCODE(this->validate();)
83     GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrSurfaceContext::copy");
84 
85     if (!fContext->contextPriv().caps()->canCopySurface(this->asSurfaceProxy(), src, srcRect,
86                                                         dstPoint)) {
87         return false;
88     }
89 
90     return this->getOpList()->copySurface(fContext, this->asSurfaceProxy(),
91                                           src, srcRect, dstPoint);
92 }
93