blob: a32ec219d1a5bf56b40c1fff575d11deeccd2aeb [file] [log] [blame]
Brian Osman3b66ab62016-11-28 09:26:31 -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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrBitmapTextureMaker.h"
Brian Osman3b66ab62016-11-28 09:26:31 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkBitmap.h"
11#include "include/core/SkPixelRef.h"
12#include "include/private/GrRecordingContext.h"
13#include "src/core/SkMipMap.h"
14#include "src/gpu/GrGpuResourcePriv.h"
15#include "src/gpu/GrProxyProvider.h"
16#include "src/gpu/GrRecordingContextPriv.h"
17#include "src/gpu/GrSurfaceContext.h"
18#include "src/gpu/SkGr.h"
Brian Osman3b66ab62016-11-28 09:26:31 -050019
Greg Daniel82c6b102020-01-21 10:33:22 -050020static GrImageInfo get_image_info(GrRecordingContext* context, const SkBitmap& bitmap) {
21 GrColorType ct = SkColorTypeToGrColorType(bitmap.info().colorType());
22 GrBackendFormat format = context->priv().caps()->getDefaultBackendFormat(ct, GrRenderable::kNo);
23 if (!format.isValid()) {
24 ct = GrColorType::kRGBA_8888;
25 }
26 return {ct, bitmap.alphaType(), bitmap.refColorSpace(), bitmap.dimensions()};
27}
28
Michael Ludwigddeed372019-02-20 16:50:10 -050029GrBitmapTextureMaker::GrBitmapTextureMaker(GrRecordingContext* context, const SkBitmap& bitmap,
Greg Daniel6f5441a2020-01-28 17:02:49 -050030 Cached cached, SkBackingFit fit, bool useDecal)
Greg Daniel82c6b102020-01-21 10:33:22 -050031 : INHERITED(context, get_image_info(context, bitmap), useDecal)
Greg Daniel6f5441a2020-01-28 17:02:49 -050032 , fBitmap(bitmap)
33 , fFit(fit) {
34 if (!bitmap.isVolatile() && cached == Cached::kYes) {
Brian Osman3b66ab62016-11-28 09:26:31 -050035 SkIPoint origin = bitmap.pixelRefOrigin();
36 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, bitmap.width(),
37 bitmap.height());
38 GrMakeKeyFromImageID(&fOriginalKey, bitmap.pixelRef()->getGenerationID(), subset);
39 }
40}
41
Brian Salomonecbb0fb2020-02-28 18:07:32 -050042GrSurfaceProxyView GrBitmapTextureMaker::refOriginalTextureProxyView(GrMipMapped mipMapped) {
Robert Phillips9da87e02019-02-04 13:26:26 -050043 GrProxyProvider* proxyProvider = this->context()->priv().proxyProvider();
Greg Danielfc5060d2017-10-04 18:36:15 +000044 sk_sp<GrTextureProxy> proxy;
Greg Danielcc104db2020-02-03 14:17:08 -050045 GrSwizzle swizzle;
Robert Phillips0c984a02017-03-16 07:51:56 -040046
47 if (fOriginalKey.isValid()) {
Brian Salomon2af3e702019-08-11 19:10:31 -040048 auto colorType = SkColorTypeToGrColorType(fBitmap.colorType());
Greg Daniel3a365112020-02-14 10:47:18 -050049 proxy = proxyProvider->findOrCreateProxyByUniqueKey(fOriginalKey, colorType);
Greg Danielcc104db2020-02-03 14:17:08 -050050 if (proxy) {
51 swizzle = this->context()->priv().caps()->getReadSwizzle(proxy->backendFormat(),
52 this->colorType());
Brian Salomonecbb0fb2020-02-28 18:07:32 -050053 if (mipMapped == GrMipMapped::kNo || proxy->mipMapped() == GrMipMapped::kYes) {
Greg Danielcc104db2020-02-03 14:17:08 -050054 return GrSurfaceProxyView(std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle);
55 }
Robert Phillips0c984a02017-03-16 07:51:56 -040056 }
57 }
Greg Daniel55afd6d2017-09-29 09:32:44 -040058
Greg Danielfc5060d2017-10-04 18:36:15 +000059 if (!proxy) {
Greg Daniel82c6b102020-01-21 10:33:22 -050060 if (this->colorType() != SkColorTypeToGrColorType(fBitmap.info().colorType())) {
61 SkASSERT(this->colorType() == GrColorType::kRGBA_8888);
62 SkBitmap copy8888;
63 if (!copy8888.tryAllocPixels(fBitmap.info().makeColorType(kRGBA_8888_SkColorType)) ||
64 !fBitmap.readPixels(copy8888.pixmap())) {
Greg Danielcc104db2020-02-03 14:17:08 -050065 return {};
Greg Daniel82c6b102020-01-21 10:33:22 -050066 }
67 copy8888.setImmutable();
Brian Salomonecbb0fb2020-02-28 18:07:32 -050068 proxy = proxyProvider->createProxyFromBitmap(copy8888, mipMapped, fFit);
Greg Daniel82c6b102020-01-21 10:33:22 -050069 } else {
Brian Salomonecbb0fb2020-02-28 18:07:32 -050070 proxy = proxyProvider->createProxyFromBitmap(fBitmap, mipMapped, fFit);
Greg Daniel82c6b102020-01-21 10:33:22 -050071 }
Greg Danielfc5060d2017-10-04 18:36:15 +000072 if (proxy) {
Greg Danielcc104db2020-02-03 14:17:08 -050073 swizzle = this->context()->priv().caps()->getReadSwizzle(proxy->backendFormat(),
74 this->colorType());
Brian Salomonecbb0fb2020-02-28 18:07:32 -050075 SkASSERT(mipMapped == GrMipMapped::kNo || proxy->mipMapped() == GrMipMapped::kYes);
Greg Danielfc5060d2017-10-04 18:36:15 +000076 if (fOriginalKey.isValid()) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050077 proxyProvider->assignUniqueKeyToProxy(fOriginalKey, proxy.get());
Greg Danielcc104db2020-02-03 14:17:08 -050078 GrInstallBitmapUniqueKeyInvalidator(
79 fOriginalKey, proxyProvider->contextID(), fBitmap.pixelRef());
Greg Danielfc5060d2017-10-04 18:36:15 +000080 }
Greg Danielcc104db2020-02-03 14:17:08 -050081 return GrSurfaceProxyView(std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle);
Greg Daniel55afd6d2017-09-29 09:32:44 -040082 }
Robert Phillips0c984a02017-03-16 07:51:56 -040083 }
Greg Danielfc5060d2017-10-04 18:36:15 +000084
85 if (proxy) {
Brian Salomonecbb0fb2020-02-28 18:07:32 -050086 SkASSERT(mipMapped == GrMipMapped::kYes);
87 SkASSERT(proxy->mipMapped() == GrMipMapped::kNo);
Greg Danielcc104db2020-02-03 14:17:08 -050088 SkASSERT(fOriginalKey.isValid());
89 // We need a mipped proxy, but we found a proxy earlier that wasn't mipped. Thus we generate
90 // a new mipped surface and copy the original proxy into the base layer. We will then let
91 // the gpu generate the rest of the mips.
Greg Danielc594e622019-10-15 14:01:49 -040092 GrColorType srcColorType = SkColorTypeToGrColorType(fBitmap.colorType());
Greg Danielcc104db2020-02-03 14:17:08 -050093 auto mippedView = GrCopyBaseMipMapToTextureProxy(this->context(), proxy.get(),
94 kTopLeft_GrSurfaceOrigin, srcColorType);
95 if (auto mippedProxy = mippedView.asTextureProxy()) {
96 // In this case we are stealing the key from the original proxy which should only happen
97 // when we have just generated mipmaps for an originally unmipped proxy/texture. This
98 // means that all future uses of the key will access the mipmapped version. The texture
99 // backing the unmipped version will remain in the resource cache until the last texture
100 // proxy referencing it is deleted at which time it too will be deleted or recycled.
101 SkASSERT(proxy->getUniqueKey() == fOriginalKey);
102 SkASSERT(mippedView.origin() == kTopLeft_GrSurfaceOrigin);
103 SkASSERT(mippedView.swizzle() == swizzle);
104 proxyProvider->removeUniqueKeyFromProxy(proxy.get());
105 proxyProvider->assignUniqueKeyToProxy(fOriginalKey, mippedProxy);
106 GrInstallBitmapUniqueKeyInvalidator(fOriginalKey, proxyProvider->contextID(),
107 fBitmap.pixelRef());
108 return mippedView;
Greg Daniel87c76ed2017-10-03 13:42:45 +0000109 }
Greg Daniel8f5bbda2018-06-08 17:22:23 -0400110 // We failed to make a mipped proxy with the base copied into it. This could have
111 // been from failure to make the proxy or failure to do the copy. Thus we will fall
112 // back to just using the non mipped proxy; See skbug.com/7094.
Greg Danielcc104db2020-02-03 14:17:08 -0500113 return GrSurfaceProxyView(std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle);
Greg Daniel87c76ed2017-10-03 13:42:45 +0000114 }
Greg Danielcc104db2020-02-03 14:17:08 -0500115 return {};
Robert Phillips0c984a02017-03-16 07:51:56 -0400116}