| 1 |
/* |
| 2 |
* Copyright 2012 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 "src/gpu/ganesh/GrSurface.h" |
| 9 |
|
| 10 |
#include "include/core/SkTextureCompressionType.h" |
| 11 |
#include "include/gpu/ganesh/GrBackendSurface.h" |
| 12 |
#include "include/gpu/ganesh/GrDirectContext.h" |
| 13 |
#include "src/core/SkCompressedDataUtils.h" |
| 14 |
#include "src/gpu/SkBackingFit.h" |
| 15 |
#include "src/gpu/ganesh/GrBackendUtils.h" |
| 16 |
#include "src/gpu/ganesh/GrDirectContextPriv.h" |
| 17 |
|
| 18 |
#include <utility> |
| 19 |
|
| 20 |
size_t GrSurface::ComputeSize(const GrBackendFormat& format, |
| 21 |
SkISize dimensions, |
| 22 |
int colorSamplesPerPixel, |
| 23 |
skgpu::Mipmapped mipmapped, |
| 24 |
bool binSize) { |
| 25 |
// For external formats we do not actually know the real size of the resource so we just return |
| 26 |
// 0 here to indicate this. |
| 27 |
if (format.textureType() == GrTextureType::kExternal) { |
| 28 |
return 0; |
| 29 |
} |
| 30 |
|
| 31 |
size_t colorSize; |
| 32 |
|
| 33 |
if (binSize) { |
| 34 |
dimensions = skgpu::GetApproxSize(dimensions); |
| 35 |
} |
| 36 |
|
| 37 |
SkTextureCompressionType compressionType = GrBackendFormatToCompressionType(format); |
| 38 |
if (compressionType != SkTextureCompressionType::kNone) { |
| 39 |
colorSize = SkCompressedFormatDataSize( |
| 40 |
compressionType, dimensions, mipmapped == skgpu::Mipmapped::kYes); |
| 41 |
} else { |
| 42 |
colorSize = (size_t)dimensions.width() * dimensions.height() * |
| 43 |
GrBackendFormatBytesPerPixel(format); |
| 44 |
} |
| 45 |
SkASSERT(colorSize > 0); |
| 46 |
|
| 47 |
size_t finalSize = colorSamplesPerPixel * colorSize; |
| 48 |
|
| 49 |
if (skgpu::Mipmapped::kYes == mipmapped) { |
| 50 |
// We don't have to worry about the mipmaps being a different dimensions than |
| 51 |
// we'd expect because we never change fDesc.fWidth/fHeight. |
| 52 |
finalSize += colorSize/3; |
| 53 |
} |
| 54 |
return finalSize; |
| 55 |
} |
| 56 |
|
| 57 |
////////////////////////////////////////////////////////////////////////////// |
| 58 |
|
| 59 |
void GrSurface::setRelease(sk_sp<skgpu::RefCntedCallback> releaseHelper) { |
| 60 |
SkASSERT(this->getContext()); |
| 61 |
fReleaseHelper.reset(new RefCntedReleaseProc(std::move(releaseHelper), |
| 62 |
sk_ref_sp(this->getContext()))); |
| 63 |
this->onSetRelease(fReleaseHelper); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
GrSurface::RefCntedReleaseProc::RefCntedReleaseProc(sk_sp<skgpu::RefCntedCallback> callback, |
| 68 |
sk_sp<GrDirectContext> directContext) |
| 69 |
: fCallback(std::move(callback)) |
| 70 |
, fDirectContext(std::move(directContext)) { |
| 71 |
SkASSERT(fCallback && fDirectContext); |
| 72 |
} |
| 73 |
|
| 74 |
GrSurface::RefCntedReleaseProc::~RefCntedReleaseProc() { |
| 75 |
fDirectContext->priv().setInsideReleaseProc(true); |
| 76 |
fCallback.reset(); |
| 77 |
fDirectContext->priv().setInsideReleaseProc(false); |
| 78 |
} |
| 79 |
|
| 80 |
void GrSurface::onRelease() { |
| 81 |
this->invokeReleaseProc(); |
| 82 |
this->INHERITED::onRelease(); |
| 83 |
} |
| 84 |
|
| 85 |
void GrSurface::onAbandon() { |
| 86 |
this->invokeReleaseProc(); |
| 87 |
this->INHERITED::onAbandon(); |
| 88 |
} |