blob: 48ff3216f27bb30383679d9d8de9bf2bc575c4fa [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
Brian Salomone7499c72019-06-24 12:12:36 -040020static GrColorSpaceInfo make_info(const SkBitmap& bm) {
Brian Salomond6287472019-06-24 15:50:07 -040021 return GrColorSpaceInfo(SkColorTypeToGrColorType(bm.colorType()), bm.alphaType(),
Brian Salomonbd3d8d32019-07-02 09:16:28 -040022 bm.refColorSpace());
Brian Salomone7499c72019-06-24 12:12:36 -040023}
Brian Osman3b66ab62016-11-28 09:26:31 -050024
Michael Ludwigddeed372019-02-20 16:50:10 -050025GrBitmapTextureMaker::GrBitmapTextureMaker(GrRecordingContext* context, const SkBitmap& bitmap,
26 bool useDecal)
Brian Salomone7499c72019-06-24 12:12:36 -040027 : INHERITED(context, bitmap.width(), bitmap.height(), make_info(bitmap), useDecal)
28 , fBitmap(bitmap) {
Brian Osman3b66ab62016-11-28 09:26:31 -050029 if (!bitmap.isVolatile()) {
30 SkIPoint origin = bitmap.pixelRefOrigin();
31 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, bitmap.width(),
32 bitmap.height());
33 GrMakeKeyFromImageID(&fOriginalKey, bitmap.pixelRef()->getGenerationID(), subset);
34 }
35}
36
Robert Phillips0c984a02017-03-16 07:51:56 -040037sk_sp<GrTextureProxy> GrBitmapTextureMaker::refOriginalTextureProxy(bool willBeMipped,
Stan Ilievba81af22017-06-08 15:16:53 -040038 AllowedTexGenType onlyIfFast) {
39 if (AllowedTexGenType::kCheap == onlyIfFast) {
40 return nullptr;
41 }
42
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;
Robert Phillips0c984a02017-03-16 07:51:56 -040045
46 if (fOriginalKey.isValid()) {
Brian Salomon2af3e702019-08-11 19:10:31 -040047 auto colorType = SkColorTypeToGrColorType(fBitmap.colorType());
48 proxy = proxyProvider->findOrCreateProxyByUniqueKey(fOriginalKey, colorType,
49 kTopLeft_GrSurfaceOrigin);
Greg Daniele252f082017-10-23 16:05:23 -040050 if (proxy && (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped())) {
Greg Danielfc5060d2017-10-04 18:36:15 +000051 return proxy;
Robert Phillips0c984a02017-03-16 07:51:56 -040052 }
53 }
Greg Daniel55afd6d2017-09-29 09:32:44 -040054
Greg Danielfc5060d2017-10-04 18:36:15 +000055 if (!proxy) {
Brian Osmande496652019-03-22 13:42:33 -040056 proxy = proxyProvider->createProxyFromBitmap(fBitmap, willBeMipped ? GrMipMapped::kYes
57 : GrMipMapped::kNo);
Greg Danielfc5060d2017-10-04 18:36:15 +000058 if (proxy) {
59 if (fOriginalKey.isValid()) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050060 proxyProvider->assignUniqueKeyToProxy(fOriginalKey, proxy.get());
Greg Danielfc5060d2017-10-04 18:36:15 +000061 }
Greg Daniele252f082017-10-23 16:05:23 -040062 if (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped()) {
Greg Danielfc5060d2017-10-04 18:36:15 +000063 SkASSERT(proxy->origin() == kTopLeft_GrSurfaceOrigin);
Robert Phillips869fe562018-09-17 14:55:49 -040064 if (fOriginalKey.isValid()) {
Brian Salomon238069b2018-07-11 15:58:57 -040065 GrInstallBitmapUniqueKeyInvalidator(
Robert Phillipsa41c6852019-02-07 10:44:10 -050066 fOriginalKey, proxyProvider->contextID(), fBitmap.pixelRef());
Greg Danielfc5060d2017-10-04 18:36:15 +000067 }
68 return proxy;
Greg Danielbb76ace2017-09-29 15:58:22 -040069 }
Greg Daniel55afd6d2017-09-29 09:32:44 -040070 }
Robert Phillips0c984a02017-03-16 07:51:56 -040071 }
Greg Danielfc5060d2017-10-04 18:36:15 +000072
73 if (proxy) {
74 SkASSERT(willBeMipped);
Greg Daniele252f082017-10-23 16:05:23 -040075 SkASSERT(GrMipMapped::kNo == proxy->mipMapped());
Greg Danielfc5060d2017-10-04 18:36:15 +000076 // We need a mipped proxy, but we either found a proxy earlier that wasn't mipped or
77 // generated a non mipped proxy. Thus we generate a new mipped surface and copy the original
78 // proxy into the base layer. We will then let the gpu generate the rest of the mips.
Greg Daniele1da1d92017-10-06 15:59:27 -040079 if (auto mippedProxy = GrCopyBaseMipMapToTextureProxy(this->context(), proxy.get())) {
Greg Danielfc5060d2017-10-04 18:36:15 +000080 SkASSERT(mippedProxy->origin() == kTopLeft_GrSurfaceOrigin);
81 if (fOriginalKey.isValid()) {
82 // In this case we are stealing the key from the original proxy which should only
83 // happen when we have just generated mipmaps for an originally unmipped
84 // proxy/texture. This means that all future uses of the key will access the
85 // mipmapped version. The texture backing the unmipped version will remain in the
86 // resource cache until the last texture proxy referencing it is deleted at which
87 // time it too will be deleted or recycled.
Chris Dalton2de13dd2019-01-03 15:11:59 -070088 SkASSERT(proxy->getUniqueKey() == fOriginalKey);
89 proxyProvider->removeUniqueKeyFromProxy(proxy.get());
Robert Phillips1afd4cd2018-01-08 13:40:32 -050090 proxyProvider->assignUniqueKeyToProxy(fOriginalKey, mippedProxy.get());
Robert Phillipsa41c6852019-02-07 10:44:10 -050091 GrInstallBitmapUniqueKeyInvalidator(fOriginalKey, proxyProvider->contextID(),
Robert Phillips869fe562018-09-17 14:55:49 -040092 fBitmap.pixelRef());
Greg Danielfc5060d2017-10-04 18:36:15 +000093 }
94 return mippedProxy;
Greg Daniel87c76ed2017-10-03 13:42:45 +000095 }
Greg Daniel8f5bbda2018-06-08 17:22:23 -040096 // We failed to make a mipped proxy with the base copied into it. This could have
97 // been from failure to make the proxy or failure to do the copy. Thus we will fall
98 // back to just using the non mipped proxy; See skbug.com/7094.
99 return proxy;
Greg Daniel87c76ed2017-10-03 13:42:45 +0000100 }
Greg Danielfc5060d2017-10-04 18:36:15 +0000101 return nullptr;
Robert Phillips0c984a02017-03-16 07:51:56 -0400102}
103
Brian Osmanb3f38302018-09-07 15:24:44 -0400104void GrBitmapTextureMaker::makeCopyKey(const CopyParams& copyParams, GrUniqueKey* copyKey) {
Brian Osman61624f02016-12-09 14:51:59 -0500105 // Destination color space is irrelevant - we always upload the bitmap's contents as-is
Brian Osman3b66ab62016-11-28 09:26:31 -0500106 if (fOriginalKey.isValid()) {
107 MakeCopyKeyFromOrigKey(fOriginalKey, copyParams, copyKey);
108 }
109}
110
Brian Salomon238069b2018-07-11 15:58:57 -0400111void GrBitmapTextureMaker::didCacheCopy(const GrUniqueKey& copyKey, uint32_t contextUniqueID) {
112 GrInstallBitmapUniqueKeyInvalidator(copyKey, contextUniqueID, fBitmap.pixelRef());
Brian Osman3b66ab62016-11-28 09:26:31 -0500113}