blob: 67a32813206b6db38a4936e77d2a6d65ea49fe37 [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
Greg Danielcc104db2020-02-03 14:17:08 -050042GrSurfaceProxyView GrBitmapTextureMaker::refOriginalTextureProxyView(bool willBeMipped,
43 AllowedTexGenType onlyIfFast) {
Stan Ilievba81af22017-06-08 15:16:53 -040044 if (AllowedTexGenType::kCheap == onlyIfFast) {
Greg Danielcc104db2020-02-03 14:17:08 -050045 return {};
Stan Ilievba81af22017-06-08 15:16:53 -040046 }
47
Robert Phillips9da87e02019-02-04 13:26:26 -050048 GrProxyProvider* proxyProvider = this->context()->priv().proxyProvider();
Greg Danielfc5060d2017-10-04 18:36:15 +000049 sk_sp<GrTextureProxy> proxy;
Greg Danielcc104db2020-02-03 14:17:08 -050050 GrSwizzle swizzle;
Robert Phillips0c984a02017-03-16 07:51:56 -040051
52 if (fOriginalKey.isValid()) {
Brian Salomon2af3e702019-08-11 19:10:31 -040053 auto colorType = SkColorTypeToGrColorType(fBitmap.colorType());
54 proxy = proxyProvider->findOrCreateProxyByUniqueKey(fOriginalKey, colorType,
55 kTopLeft_GrSurfaceOrigin);
Greg Danielcc104db2020-02-03 14:17:08 -050056 if (proxy) {
57 swizzle = this->context()->priv().caps()->getReadSwizzle(proxy->backendFormat(),
58 this->colorType());
59 if (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped()) {
60 return GrSurfaceProxyView(std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle);
61 }
Robert Phillips0c984a02017-03-16 07:51:56 -040062 }
63 }
Greg Daniel55afd6d2017-09-29 09:32:44 -040064
Greg Danielfc5060d2017-10-04 18:36:15 +000065 if (!proxy) {
Greg Daniel82c6b102020-01-21 10:33:22 -050066 if (this->colorType() != SkColorTypeToGrColorType(fBitmap.info().colorType())) {
67 SkASSERT(this->colorType() == GrColorType::kRGBA_8888);
68 SkBitmap copy8888;
69 if (!copy8888.tryAllocPixels(fBitmap.info().makeColorType(kRGBA_8888_SkColorType)) ||
70 !fBitmap.readPixels(copy8888.pixmap())) {
Greg Danielcc104db2020-02-03 14:17:08 -050071 return {};
Greg Daniel82c6b102020-01-21 10:33:22 -050072 }
73 copy8888.setImmutable();
Greg Daniel6f5441a2020-01-28 17:02:49 -050074 proxy = proxyProvider->createProxyFromBitmap(
75 copy8888, willBeMipped ? GrMipMapped::kYes : GrMipMapped::kNo, fFit);
Greg Daniel82c6b102020-01-21 10:33:22 -050076 } else {
Greg Daniel6f5441a2020-01-28 17:02:49 -050077 proxy = proxyProvider->createProxyFromBitmap(
78 fBitmap, willBeMipped ? GrMipMapped::kYes : GrMipMapped::kNo, fFit);
Greg Daniel82c6b102020-01-21 10:33:22 -050079 }
Greg Danielfc5060d2017-10-04 18:36:15 +000080 if (proxy) {
Greg Danielcc104db2020-02-03 14:17:08 -050081 swizzle = this->context()->priv().caps()->getReadSwizzle(proxy->backendFormat(),
82 this->colorType());
83 SkASSERT(!willBeMipped || GrMipMapped::kYes == proxy->mipMapped());
Greg Danielfc5060d2017-10-04 18:36:15 +000084 if (fOriginalKey.isValid()) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050085 proxyProvider->assignUniqueKeyToProxy(fOriginalKey, proxy.get());
Greg Danielcc104db2020-02-03 14:17:08 -050086 GrInstallBitmapUniqueKeyInvalidator(
87 fOriginalKey, proxyProvider->contextID(), fBitmap.pixelRef());
Greg Danielfc5060d2017-10-04 18:36:15 +000088 }
Greg Danielcc104db2020-02-03 14:17:08 -050089 return GrSurfaceProxyView(std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle);
Greg Daniel55afd6d2017-09-29 09:32:44 -040090 }
Robert Phillips0c984a02017-03-16 07:51:56 -040091 }
Greg Danielfc5060d2017-10-04 18:36:15 +000092
93 if (proxy) {
94 SkASSERT(willBeMipped);
Greg Daniele252f082017-10-23 16:05:23 -040095 SkASSERT(GrMipMapped::kNo == proxy->mipMapped());
Greg Danielcc104db2020-02-03 14:17:08 -050096 SkASSERT(fOriginalKey.isValid());
97 // We need a mipped proxy, but we found a proxy earlier that wasn't mipped. Thus we generate
98 // a new mipped surface and copy the original proxy into the base layer. We will then let
99 // the gpu generate the rest of the mips.
Greg Danielc594e622019-10-15 14:01:49 -0400100 GrColorType srcColorType = SkColorTypeToGrColorType(fBitmap.colorType());
Greg Danielcc104db2020-02-03 14:17:08 -0500101 auto mippedView = GrCopyBaseMipMapToTextureProxy(this->context(), proxy.get(),
102 kTopLeft_GrSurfaceOrigin, srcColorType);
103 if (auto mippedProxy = mippedView.asTextureProxy()) {
104 // In this case we are stealing the key from the original proxy which should only happen
105 // when we have just generated mipmaps for an originally unmipped proxy/texture. This
106 // means that all future uses of the key will access the mipmapped version. The texture
107 // backing the unmipped version will remain in the resource cache until the last texture
108 // proxy referencing it is deleted at which time it too will be deleted or recycled.
109 SkASSERT(proxy->getUniqueKey() == fOriginalKey);
110 SkASSERT(mippedView.origin() == kTopLeft_GrSurfaceOrigin);
111 SkASSERT(mippedView.swizzle() == swizzle);
112 proxyProvider->removeUniqueKeyFromProxy(proxy.get());
113 proxyProvider->assignUniqueKeyToProxy(fOriginalKey, mippedProxy);
114 GrInstallBitmapUniqueKeyInvalidator(fOriginalKey, proxyProvider->contextID(),
115 fBitmap.pixelRef());
116 return mippedView;
Greg Daniel87c76ed2017-10-03 13:42:45 +0000117 }
Greg Daniel8f5bbda2018-06-08 17:22:23 -0400118 // We failed to make a mipped proxy with the base copied into it. This could have
119 // been from failure to make the proxy or failure to do the copy. Thus we will fall
120 // back to just using the non mipped proxy; See skbug.com/7094.
Greg Danielcc104db2020-02-03 14:17:08 -0500121 return GrSurfaceProxyView(std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle);
Greg Daniel87c76ed2017-10-03 13:42:45 +0000122 }
Greg Danielcc104db2020-02-03 14:17:08 -0500123 return {};
Robert Phillips0c984a02017-03-16 07:51:56 -0400124}
125
Brian Osmanb3f38302018-09-07 15:24:44 -0400126void GrBitmapTextureMaker::makeCopyKey(const CopyParams& copyParams, GrUniqueKey* copyKey) {
Brian Osman61624f02016-12-09 14:51:59 -0500127 // Destination color space is irrelevant - we always upload the bitmap's contents as-is
Brian Osman3b66ab62016-11-28 09:26:31 -0500128 if (fOriginalKey.isValid()) {
129 MakeCopyKeyFromOrigKey(fOriginalKey, copyParams, copyKey);
130 }
131}
132
Brian Salomon238069b2018-07-11 15:58:57 -0400133void GrBitmapTextureMaker::didCacheCopy(const GrUniqueKey& copyKey, uint32_t contextUniqueID) {
134 GrInstallBitmapUniqueKeyInvalidator(copyKey, contextUniqueID, fBitmap.pixelRef());
Brian Osman3b66ab62016-11-28 09:26:31 -0500135}