blob: fe93a3fdec2dea66830447cb8659675fde8e03db [file] [log] [blame]
robertphillips@google.com7d501ab2012-06-21 21:09:06 +00001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/gpu/GrContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/gpu/GrSurface.h"
10#include "include/gpu/GrTexture.h"
Robert Phillips99dead92020-01-27 16:11:57 -050011#include "src/core/SkCompressedDataUtils.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040012#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/GrResourceProvider.h"
14#include "src/gpu/GrSurfacePriv.h"
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000015
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/core/SkMathPriv.h"
17#include "src/gpu/SkGr.h"
commit-bot@chromium.org6c5d9a12013-09-30 18:05:43 +000018
Greg Daniela00bcad2019-10-11 13:21:48 -040019size_t GrSurface::ComputeSize(const GrCaps& caps,
Greg Daniel7fd7a8a2019-10-10 16:10:31 -040020 const GrBackendFormat& format,
Brian Salomon9f2b86c2019-10-22 10:37:46 -040021 SkISize dimensions,
Robert Phillipsd6214d42016-11-07 08:23:48 -050022 int colorSamplesPerPixel,
Greg Daniele252f082017-10-23 16:05:23 -040023 GrMipMapped mipMapped,
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -040024 bool binSize) {
Greg Danielada451c2019-11-26 09:55:20 -050025 // 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
Jim Van Verth1676cb92019-01-15 13:24:45 -050031 size_t colorSize;
32
Brian Salomon9f2b86c2019-10-22 10:37:46 -040033 if (binSize) {
34 dimensions = GrResourceProvider::MakeApprox(dimensions);
35 }
Robert Phillipsb4460882016-11-17 14:43:51 -050036
Robert Phillipsd6df7b52019-12-13 11:17:46 -050037 SkImage::CompressionType compressionType = caps.compressionType(format);
38 if (compressionType != SkImage::CompressionType::kNone) {
Robert Phillips99dead92020-01-27 16:11:57 -050039 colorSize = SkCompressedFormatDataSize(compressionType, dimensions,
40 mipMapped == GrMipMapped::kYes);
Jim Van Verth1676cb92019-01-15 13:24:45 -050041 } else {
Brian Salomon9f2b86c2019-10-22 10:37:46 -040042 colorSize = (size_t)dimensions.width() * dimensions.height() * caps.bytesPerPixel(format);
Jim Van Verth1676cb92019-01-15 13:24:45 -050043 }
Robert Phillipsd6214d42016-11-07 08:23:48 -050044 SkASSERT(colorSize > 0);
45
46 size_t finalSize = colorSamplesPerPixel * colorSize;
47
Greg Daniele252f082017-10-23 16:05:23 -040048 if (GrMipMapped::kYes == mipMapped) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -040049 // We don't have to worry about the mipmaps being a different dimensions than
Robert Phillipsd6214d42016-11-07 08:23:48 -050050 // we'd expect because we never change fDesc.fWidth/fHeight.
51 finalSize += colorSize/3;
52 }
Robert Phillipsd6214d42016-11-07 08:23:48 -050053 return finalSize;
54}
55
bsalomone8d21e82015-07-16 08:23:13 -070056//////////////////////////////////////////////////////////////////////////////
57
reedde499882015-06-18 13:41:40 -070058void GrSurface::onRelease() {
Greg Daniel2d35a1c2019-02-01 14:48:10 -050059 this->invokeReleaseProc();
reedde499882015-06-18 13:41:40 -070060 this->INHERITED::onRelease();
61}
62
63void GrSurface::onAbandon() {
Greg Daniel2d35a1c2019-02-01 14:48:10 -050064 this->invokeReleaseProc();
reedde499882015-06-18 13:41:40 -070065 this->INHERITED::onAbandon();
66}