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