blob: f51871489af98ddc8a0ff088175958377399d56f [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
Robert Phillips0c984a02017-03-16 07:51:56 -040042sk_sp<GrTextureProxy> GrBitmapTextureMaker::refOriginalTextureProxy(bool willBeMipped,
Stan Ilievba81af22017-06-08 15:16:53 -040043 AllowedTexGenType onlyIfFast) {
44 if (AllowedTexGenType::kCheap == onlyIfFast) {
45 return nullptr;
46 }
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;
Robert Phillips0c984a02017-03-16 07:51:56 -040050
51 if (fOriginalKey.isValid()) {
Brian Salomon2af3e702019-08-11 19:10:31 -040052 auto colorType = SkColorTypeToGrColorType(fBitmap.colorType());
53 proxy = proxyProvider->findOrCreateProxyByUniqueKey(fOriginalKey, colorType,
54 kTopLeft_GrSurfaceOrigin);
Greg Daniele252f082017-10-23 16:05:23 -040055 if (proxy && (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped())) {
Greg Danielfc5060d2017-10-04 18:36:15 +000056 return proxy;
Robert Phillips0c984a02017-03-16 07:51:56 -040057 }
58 }
Greg Daniel55afd6d2017-09-29 09:32:44 -040059
Greg Danielfc5060d2017-10-04 18:36:15 +000060 if (!proxy) {
Greg Daniel82c6b102020-01-21 10:33:22 -050061 if (this->colorType() != SkColorTypeToGrColorType(fBitmap.info().colorType())) {
62 SkASSERT(this->colorType() == GrColorType::kRGBA_8888);
63 SkBitmap copy8888;
64 if (!copy8888.tryAllocPixels(fBitmap.info().makeColorType(kRGBA_8888_SkColorType)) ||
65 !fBitmap.readPixels(copy8888.pixmap())) {
66 return nullptr;
67 }
68 copy8888.setImmutable();
Greg Daniel6f5441a2020-01-28 17:02:49 -050069 proxy = proxyProvider->createProxyFromBitmap(
70 copy8888, willBeMipped ? GrMipMapped::kYes : GrMipMapped::kNo, fFit);
Greg Daniel82c6b102020-01-21 10:33:22 -050071 } else {
Greg Daniel6f5441a2020-01-28 17:02:49 -050072 proxy = proxyProvider->createProxyFromBitmap(
73 fBitmap, willBeMipped ? GrMipMapped::kYes : GrMipMapped::kNo, fFit);
Greg Daniel82c6b102020-01-21 10:33:22 -050074 }
Greg Danielfc5060d2017-10-04 18:36:15 +000075 if (proxy) {
76 if (fOriginalKey.isValid()) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050077 proxyProvider->assignUniqueKeyToProxy(fOriginalKey, proxy.get());
Greg Danielfc5060d2017-10-04 18:36:15 +000078 }
Greg Daniele252f082017-10-23 16:05:23 -040079 if (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped()) {
Greg Danielfc5060d2017-10-04 18:36:15 +000080 SkASSERT(proxy->origin() == kTopLeft_GrSurfaceOrigin);
Robert Phillips869fe562018-09-17 14:55:49 -040081 if (fOriginalKey.isValid()) {
Brian Salomon238069b2018-07-11 15:58:57 -040082 GrInstallBitmapUniqueKeyInvalidator(
Robert Phillipsa41c6852019-02-07 10:44:10 -050083 fOriginalKey, proxyProvider->contextID(), fBitmap.pixelRef());
Greg Danielfc5060d2017-10-04 18:36:15 +000084 }
85 return proxy;
Greg Danielbb76ace2017-09-29 15:58:22 -040086 }
Greg Daniel55afd6d2017-09-29 09:32:44 -040087 }
Robert Phillips0c984a02017-03-16 07:51:56 -040088 }
Greg Danielfc5060d2017-10-04 18:36:15 +000089
90 if (proxy) {
91 SkASSERT(willBeMipped);
Greg Daniele252f082017-10-23 16:05:23 -040092 SkASSERT(GrMipMapped::kNo == proxy->mipMapped());
Greg Danielfc5060d2017-10-04 18:36:15 +000093 // We need a mipped proxy, but we either found a proxy earlier that wasn't mipped or
94 // generated a non mipped proxy. Thus we generate a new mipped surface and copy the original
95 // proxy into the base layer. We will then let the gpu generate the rest of the mips.
Greg Danielc594e622019-10-15 14:01:49 -040096 GrColorType srcColorType = SkColorTypeToGrColorType(fBitmap.colorType());
97 if (auto mippedProxy = GrCopyBaseMipMapToTextureProxy(this->context(), proxy.get(),
Greg Daniel40903af2020-01-30 14:55:05 -050098 kTopLeft_GrSurfaceOrigin,
Greg Danielc594e622019-10-15 14:01:49 -040099 srcColorType)) {
Greg Danielfc5060d2017-10-04 18:36:15 +0000100 if (fOriginalKey.isValid()) {
101 // In this case we are stealing the key from the original proxy which should only
102 // happen when we have just generated mipmaps for an originally unmipped
103 // proxy/texture. This means that all future uses of the key will access the
104 // mipmapped version. The texture backing the unmipped version will remain in the
105 // resource cache until the last texture proxy referencing it is deleted at which
106 // time it too will be deleted or recycled.
Chris Dalton2de13dd2019-01-03 15:11:59 -0700107 SkASSERT(proxy->getUniqueKey() == fOriginalKey);
108 proxyProvider->removeUniqueKeyFromProxy(proxy.get());
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500109 proxyProvider->assignUniqueKeyToProxy(fOriginalKey, mippedProxy.get());
Robert Phillipsa41c6852019-02-07 10:44:10 -0500110 GrInstallBitmapUniqueKeyInvalidator(fOriginalKey, proxyProvider->contextID(),
Robert Phillips869fe562018-09-17 14:55:49 -0400111 fBitmap.pixelRef());
Greg Danielfc5060d2017-10-04 18:36:15 +0000112 }
113 return mippedProxy;
Greg Daniel87c76ed2017-10-03 13:42:45 +0000114 }
Greg Daniel8f5bbda2018-06-08 17:22:23 -0400115 // We failed to make a mipped proxy with the base copied into it. This could have
116 // been from failure to make the proxy or failure to do the copy. Thus we will fall
117 // back to just using the non mipped proxy; See skbug.com/7094.
118 return proxy;
Greg Daniel87c76ed2017-10-03 13:42:45 +0000119 }
Greg Danielfc5060d2017-10-04 18:36:15 +0000120 return nullptr;
Robert Phillips0c984a02017-03-16 07:51:56 -0400121}
122
Brian Osmanb3f38302018-09-07 15:24:44 -0400123void GrBitmapTextureMaker::makeCopyKey(const CopyParams& copyParams, GrUniqueKey* copyKey) {
Brian Osman61624f02016-12-09 14:51:59 -0500124 // Destination color space is irrelevant - we always upload the bitmap's contents as-is
Brian Osman3b66ab62016-11-28 09:26:31 -0500125 if (fOriginalKey.isValid()) {
126 MakeCopyKeyFromOrigKey(fOriginalKey, copyParams, copyKey);
127 }
128}
129
Brian Salomon238069b2018-07-11 15:58:57 -0400130void GrBitmapTextureMaker::didCacheCopy(const GrUniqueKey& copyKey, uint32_t contextUniqueID) {
131 GrInstallBitmapUniqueKeyInvalidator(copyKey, contextUniqueID, fBitmap.pixelRef());
Brian Osman3b66ab62016-11-28 09:26:31 -0500132}