blob: 129f06e933d113f64c47f22fcbdf68260416a6f7 [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 Daniel82c6b102020-01-21 10:33:22 -050030 bool useDecal, bool shouldUseUniqueKey)
31 : INHERITED(context, get_image_info(context, bitmap), useDecal)
32 , fBitmap(bitmap) {
33 if (!bitmap.isVolatile() && shouldUseUniqueKey) {
Brian Osman3b66ab62016-11-28 09:26:31 -050034 SkIPoint origin = bitmap.pixelRefOrigin();
35 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, bitmap.width(),
36 bitmap.height());
37 GrMakeKeyFromImageID(&fOriginalKey, bitmap.pixelRef()->getGenerationID(), subset);
38 }
39}
40
Robert Phillips0c984a02017-03-16 07:51:56 -040041sk_sp<GrTextureProxy> GrBitmapTextureMaker::refOriginalTextureProxy(bool willBeMipped,
Stan Ilievba81af22017-06-08 15:16:53 -040042 AllowedTexGenType onlyIfFast) {
43 if (AllowedTexGenType::kCheap == onlyIfFast) {
44 return nullptr;
45 }
46
Robert Phillips9da87e02019-02-04 13:26:26 -050047 GrProxyProvider* proxyProvider = this->context()->priv().proxyProvider();
Greg Danielfc5060d2017-10-04 18:36:15 +000048 sk_sp<GrTextureProxy> proxy;
Robert Phillips0c984a02017-03-16 07:51:56 -040049
50 if (fOriginalKey.isValid()) {
Brian Salomon2af3e702019-08-11 19:10:31 -040051 auto colorType = SkColorTypeToGrColorType(fBitmap.colorType());
52 proxy = proxyProvider->findOrCreateProxyByUniqueKey(fOriginalKey, colorType,
53 kTopLeft_GrSurfaceOrigin);
Greg Daniele252f082017-10-23 16:05:23 -040054 if (proxy && (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped())) {
Greg Danielfc5060d2017-10-04 18:36:15 +000055 return proxy;
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())) {
65 return nullptr;
66 }
67 copy8888.setImmutable();
68 proxy = proxyProvider->createProxyFromBitmap(copy8888, willBeMipped ? GrMipMapped::kYes
69 : GrMipMapped::kNo);
70 } else {
71 proxy = proxyProvider->createProxyFromBitmap(fBitmap, willBeMipped ? GrMipMapped::kYes
72 : GrMipMapped::kNo);
73 }
Greg Danielfc5060d2017-10-04 18:36:15 +000074 if (proxy) {
75 if (fOriginalKey.isValid()) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050076 proxyProvider->assignUniqueKeyToProxy(fOriginalKey, proxy.get());
Greg Danielfc5060d2017-10-04 18:36:15 +000077 }
Greg Daniele252f082017-10-23 16:05:23 -040078 if (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped()) {
Greg Danielfc5060d2017-10-04 18:36:15 +000079 SkASSERT(proxy->origin() == kTopLeft_GrSurfaceOrigin);
Robert Phillips869fe562018-09-17 14:55:49 -040080 if (fOriginalKey.isValid()) {
Brian Salomon238069b2018-07-11 15:58:57 -040081 GrInstallBitmapUniqueKeyInvalidator(
Robert Phillipsa41c6852019-02-07 10:44:10 -050082 fOriginalKey, proxyProvider->contextID(), fBitmap.pixelRef());
Greg Danielfc5060d2017-10-04 18:36:15 +000083 }
84 return proxy;
Greg Danielbb76ace2017-09-29 15:58:22 -040085 }
Greg Daniel55afd6d2017-09-29 09:32:44 -040086 }
Robert Phillips0c984a02017-03-16 07:51:56 -040087 }
Greg Danielfc5060d2017-10-04 18:36:15 +000088
89 if (proxy) {
90 SkASSERT(willBeMipped);
Greg Daniele252f082017-10-23 16:05:23 -040091 SkASSERT(GrMipMapped::kNo == proxy->mipMapped());
Greg Danielfc5060d2017-10-04 18:36:15 +000092 // We need a mipped proxy, but we either found a proxy earlier that wasn't mipped or
93 // generated a non mipped proxy. Thus we generate a new mipped surface and copy the original
94 // proxy into the base layer. We will then let the gpu generate the rest of the mips.
Greg Danielc594e622019-10-15 14:01:49 -040095 GrColorType srcColorType = SkColorTypeToGrColorType(fBitmap.colorType());
96 if (auto mippedProxy = GrCopyBaseMipMapToTextureProxy(this->context(), proxy.get(),
97 srcColorType)) {
Greg Danielfc5060d2017-10-04 18:36:15 +000098 SkASSERT(mippedProxy->origin() == kTopLeft_GrSurfaceOrigin);
99 if (fOriginalKey.isValid()) {
100 // In this case we are stealing the key from the original proxy which should only
101 // happen when we have just generated mipmaps for an originally unmipped
102 // proxy/texture. This means that all future uses of the key will access the
103 // mipmapped version. The texture backing the unmipped version will remain in the
104 // resource cache until the last texture proxy referencing it is deleted at which
105 // time it too will be deleted or recycled.
Chris Dalton2de13dd2019-01-03 15:11:59 -0700106 SkASSERT(proxy->getUniqueKey() == fOriginalKey);
107 proxyProvider->removeUniqueKeyFromProxy(proxy.get());
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500108 proxyProvider->assignUniqueKeyToProxy(fOriginalKey, mippedProxy.get());
Robert Phillipsa41c6852019-02-07 10:44:10 -0500109 GrInstallBitmapUniqueKeyInvalidator(fOriginalKey, proxyProvider->contextID(),
Robert Phillips869fe562018-09-17 14:55:49 -0400110 fBitmap.pixelRef());
Greg Danielfc5060d2017-10-04 18:36:15 +0000111 }
112 return mippedProxy;
Greg Daniel87c76ed2017-10-03 13:42:45 +0000113 }
Greg Daniel8f5bbda2018-06-08 17:22:23 -0400114 // We failed to make a mipped proxy with the base copied into it. This could have
115 // been from failure to make the proxy or failure to do the copy. Thus we will fall
116 // back to just using the non mipped proxy; See skbug.com/7094.
117 return proxy;
Greg Daniel87c76ed2017-10-03 13:42:45 +0000118 }
Greg Danielfc5060d2017-10-04 18:36:15 +0000119 return nullptr;
Robert Phillips0c984a02017-03-16 07:51:56 -0400120}
121
Brian Osmanb3f38302018-09-07 15:24:44 -0400122void GrBitmapTextureMaker::makeCopyKey(const CopyParams& copyParams, GrUniqueKey* copyKey) {
Brian Osman61624f02016-12-09 14:51:59 -0500123 // Destination color space is irrelevant - we always upload the bitmap's contents as-is
Brian Osman3b66ab62016-11-28 09:26:31 -0500124 if (fOriginalKey.isValid()) {
125 MakeCopyKeyFromOrigKey(fOriginalKey, copyParams, copyKey);
126 }
127}
128
Brian Salomon238069b2018-07-11 15:58:57 -0400129void GrBitmapTextureMaker::didCacheCopy(const GrUniqueKey& copyKey, uint32_t contextUniqueID) {
130 GrInstallBitmapUniqueKeyInvalidator(copyKey, contextUniqueID, fBitmap.pixelRef());
Brian Osman3b66ab62016-11-28 09:26:31 -0500131}