blob: ee93521c92fa9b382474692fa3fb9ff142481cc6 [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
8#include "GrBitmapTextureMaker.h"
9
Brian Osman3b66ab62016-11-28 09:26:31 -050010#include "GrGpuResourcePriv.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050011#include "GrProxyProvider.h"
Robert Phillips9338c602019-02-19 12:52:29 -050012#include "GrRecordingContext.h"
13#include "GrRecordingContextPriv.h"
Greg Daniel55afd6d2017-09-29 09:32:44 -040014#include "GrSurfaceContext.h"
Brian Osman3b66ab62016-11-28 09:26:31 -050015#include "SkBitmap.h"
Brian Osman3b655982017-03-07 16:58:08 -050016#include "SkGr.h"
Greg Daniel55afd6d2017-09-29 09:32:44 -040017#include "SkMipMap.h"
Brian Osman3b66ab62016-11-28 09:26:31 -050018#include "SkPixelRef.h"
19
20static bool bmp_is_alpha_only(const SkBitmap& bm) { return kAlpha_8_SkColorType == bm.colorType(); }
21
Michael Ludwigddeed372019-02-20 16:50:10 -050022GrBitmapTextureMaker::GrBitmapTextureMaker(GrRecordingContext* context, const SkBitmap& bitmap,
23 bool useDecal)
24 : INHERITED(context, bitmap.width(), bitmap.height(), bmp_is_alpha_only(bitmap), useDecal)
Brian Osman3b66ab62016-11-28 09:26:31 -050025 , fBitmap(bitmap) {
26 if (!bitmap.isVolatile()) {
27 SkIPoint origin = bitmap.pixelRefOrigin();
28 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, bitmap.width(),
29 bitmap.height());
30 GrMakeKeyFromImageID(&fOriginalKey, bitmap.pixelRef()->getGenerationID(), subset);
31 }
32}
33
Robert Phillips0c984a02017-03-16 07:51:56 -040034sk_sp<GrTextureProxy> GrBitmapTextureMaker::refOriginalTextureProxy(bool willBeMipped,
Stan Ilievba81af22017-06-08 15:16:53 -040035 AllowedTexGenType onlyIfFast) {
36 if (AllowedTexGenType::kCheap == onlyIfFast) {
37 return nullptr;
38 }
39
Robert Phillips9da87e02019-02-04 13:26:26 -050040 GrProxyProvider* proxyProvider = this->context()->priv().proxyProvider();
Greg Danielfc5060d2017-10-04 18:36:15 +000041 sk_sp<GrTextureProxy> proxy;
Robert Phillips0c984a02017-03-16 07:51:56 -040042
43 if (fOriginalKey.isValid()) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050044 proxy = proxyProvider->findOrCreateProxyByUniqueKey(fOriginalKey, kTopLeft_GrSurfaceOrigin);
Greg Daniele252f082017-10-23 16:05:23 -040045 if (proxy && (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped())) {
Greg Danielfc5060d2017-10-04 18:36:15 +000046 return proxy;
Robert Phillips0c984a02017-03-16 07:51:56 -040047 }
48 }
Greg Daniel55afd6d2017-09-29 09:32:44 -040049
Greg Danielfc5060d2017-10-04 18:36:15 +000050 if (!proxy) {
51 if (willBeMipped) {
Brian Osman2b23c4b2018-06-01 12:25:08 -040052 proxy = proxyProvider->createMipMapProxyFromBitmap(fBitmap);
Greg Danielfc5060d2017-10-04 18:36:15 +000053 }
54 if (!proxy) {
Brian Osman2b23c4b2018-06-01 12:25:08 -040055 proxy = GrUploadBitmapToTextureProxy(proxyProvider, fBitmap);
Greg Danielfc5060d2017-10-04 18:36:15 +000056 }
57 if (proxy) {
58 if (fOriginalKey.isValid()) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050059 proxyProvider->assignUniqueKeyToProxy(fOriginalKey, proxy.get());
Greg Danielfc5060d2017-10-04 18:36:15 +000060 }
Greg Daniele252f082017-10-23 16:05:23 -040061 if (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped()) {
Greg Danielfc5060d2017-10-04 18:36:15 +000062 SkASSERT(proxy->origin() == kTopLeft_GrSurfaceOrigin);
Robert Phillips869fe562018-09-17 14:55:49 -040063 if (fOriginalKey.isValid()) {
Brian Salomon238069b2018-07-11 15:58:57 -040064 GrInstallBitmapUniqueKeyInvalidator(
Robert Phillipsa41c6852019-02-07 10:44:10 -050065 fOriginalKey, proxyProvider->contextID(), fBitmap.pixelRef());
Greg Danielfc5060d2017-10-04 18:36:15 +000066 }
67 return proxy;
Greg Danielbb76ace2017-09-29 15:58:22 -040068 }
Greg Daniel55afd6d2017-09-29 09:32:44 -040069 }
Robert Phillips0c984a02017-03-16 07:51:56 -040070 }
Greg Danielfc5060d2017-10-04 18:36:15 +000071
72 if (proxy) {
73 SkASSERT(willBeMipped);
Greg Daniele252f082017-10-23 16:05:23 -040074 SkASSERT(GrMipMapped::kNo == proxy->mipMapped());
Greg Danielfc5060d2017-10-04 18:36:15 +000075 // We need a mipped proxy, but we either found a proxy earlier that wasn't mipped or
76 // generated a non mipped proxy. Thus we generate a new mipped surface and copy the original
77 // proxy into the base layer. We will then let the gpu generate the rest of the mips.
Greg Daniele1da1d92017-10-06 15:59:27 -040078 if (auto mippedProxy = GrCopyBaseMipMapToTextureProxy(this->context(), proxy.get())) {
Greg Danielfc5060d2017-10-04 18:36:15 +000079 SkASSERT(mippedProxy->origin() == kTopLeft_GrSurfaceOrigin);
80 if (fOriginalKey.isValid()) {
81 // In this case we are stealing the key from the original proxy which should only
82 // happen when we have just generated mipmaps for an originally unmipped
83 // proxy/texture. This means that all future uses of the key will access the
84 // mipmapped version. The texture backing the unmipped version will remain in the
85 // resource cache until the last texture proxy referencing it is deleted at which
86 // time it too will be deleted or recycled.
Chris Dalton2de13dd2019-01-03 15:11:59 -070087 SkASSERT(proxy->getUniqueKey() == fOriginalKey);
88 proxyProvider->removeUniqueKeyFromProxy(proxy.get());
Robert Phillips1afd4cd2018-01-08 13:40:32 -050089 proxyProvider->assignUniqueKeyToProxy(fOriginalKey, mippedProxy.get());
Robert Phillipsa41c6852019-02-07 10:44:10 -050090 GrInstallBitmapUniqueKeyInvalidator(fOriginalKey, proxyProvider->contextID(),
Robert Phillips869fe562018-09-17 14:55:49 -040091 fBitmap.pixelRef());
Greg Danielfc5060d2017-10-04 18:36:15 +000092 }
93 return mippedProxy;
Greg Daniel87c76ed2017-10-03 13:42:45 +000094 }
Greg Daniel8f5bbda2018-06-08 17:22:23 -040095 // We failed to make a mipped proxy with the base copied into it. This could have
96 // been from failure to make the proxy or failure to do the copy. Thus we will fall
97 // back to just using the non mipped proxy; See skbug.com/7094.
98 return proxy;
Greg Daniel87c76ed2017-10-03 13:42:45 +000099 }
Greg Danielfc5060d2017-10-04 18:36:15 +0000100 return nullptr;
Robert Phillips0c984a02017-03-16 07:51:56 -0400101}
102
Brian Osmanb3f38302018-09-07 15:24:44 -0400103void GrBitmapTextureMaker::makeCopyKey(const CopyParams& copyParams, GrUniqueKey* copyKey) {
Brian Osman61624f02016-12-09 14:51:59 -0500104 // Destination color space is irrelevant - we always upload the bitmap's contents as-is
Brian Osman3b66ab62016-11-28 09:26:31 -0500105 if (fOriginalKey.isValid()) {
106 MakeCopyKeyFromOrigKey(fOriginalKey, copyParams, copyKey);
107 }
108}
109
Brian Salomon238069b2018-07-11 15:58:57 -0400110void GrBitmapTextureMaker::didCacheCopy(const GrUniqueKey& copyKey, uint32_t contextUniqueID) {
111 GrInstallBitmapUniqueKeyInvalidator(copyKey, contextUniqueID, fBitmap.pixelRef());
Brian Osman3b66ab62016-11-28 09:26:31 -0500112}
113
114SkAlphaType GrBitmapTextureMaker::alphaType() const {
115 return fBitmap.alphaType();
116}
117
Brian Osman6064e1c2018-10-19 14:27:54 -0400118SkColorSpace* GrBitmapTextureMaker::colorSpace() const {
119 return fBitmap.colorSpace();
Brian Osman3b66ab62016-11-28 09:26:31 -0500120}