blob: 291d6b1ddf59b55a5d8cb659ab1672ce20b7e4b4 [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
10#include "GrContext.h"
Greg Daniel55afd6d2017-09-29 09:32:44 -040011#include "GrContextPriv.h"
Brian Osman3b66ab62016-11-28 09:26:31 -050012#include "GrGpuResourcePriv.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050013#include "GrProxyProvider.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
22GrBitmapTextureMaker::GrBitmapTextureMaker(GrContext* context, const SkBitmap& bitmap)
23 : INHERITED(context, bitmap.width(), bitmap.height(), bmp_is_alpha_only(bitmap))
24 , fBitmap(bitmap) {
25 if (!bitmap.isVolatile()) {
26 SkIPoint origin = bitmap.pixelRefOrigin();
27 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, bitmap.width(),
28 bitmap.height());
29 GrMakeKeyFromImageID(&fOriginalKey, bitmap.pixelRef()->getGenerationID(), subset);
30 }
31}
32
Robert Phillips0c984a02017-03-16 07:51:56 -040033sk_sp<GrTextureProxy> GrBitmapTextureMaker::refOriginalTextureProxy(bool willBeMipped,
Stan Ilievba81af22017-06-08 15:16:53 -040034 AllowedTexGenType onlyIfFast) {
35 if (AllowedTexGenType::kCheap == onlyIfFast) {
36 return nullptr;
37 }
38
Robert Phillips1afd4cd2018-01-08 13:40:32 -050039 GrProxyProvider* proxyProvider = this->context()->contextPriv().proxyProvider();
Greg Danielfc5060d2017-10-04 18:36:15 +000040 sk_sp<GrTextureProxy> proxy;
Robert Phillips0c984a02017-03-16 07:51:56 -040041
42 if (fOriginalKey.isValid()) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050043 proxy = proxyProvider->findOrCreateProxyByUniqueKey(fOriginalKey, kTopLeft_GrSurfaceOrigin);
Greg Daniele252f082017-10-23 16:05:23 -040044 if (proxy && (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped())) {
Greg Danielfc5060d2017-10-04 18:36:15 +000045 return proxy;
Robert Phillips0c984a02017-03-16 07:51:56 -040046 }
47 }
Greg Daniel55afd6d2017-09-29 09:32:44 -040048
Greg Danielfc5060d2017-10-04 18:36:15 +000049 if (!proxy) {
50 if (willBeMipped) {
Brian Osman2b23c4b2018-06-01 12:25:08 -040051 proxy = proxyProvider->createMipMapProxyFromBitmap(fBitmap);
Greg Danielfc5060d2017-10-04 18:36:15 +000052 }
53 if (!proxy) {
Brian Osman2b23c4b2018-06-01 12:25:08 -040054 proxy = GrUploadBitmapToTextureProxy(proxyProvider, fBitmap);
Greg Danielfc5060d2017-10-04 18:36:15 +000055 }
56 if (proxy) {
57 if (fOriginalKey.isValid()) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050058 proxyProvider->assignUniqueKeyToProxy(fOriginalKey, proxy.get());
Greg Danielfc5060d2017-10-04 18:36:15 +000059 }
Greg Daniele252f082017-10-23 16:05:23 -040060 if (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped()) {
Greg Danielfc5060d2017-10-04 18:36:15 +000061 SkASSERT(proxy->origin() == kTopLeft_GrSurfaceOrigin);
Robert Phillips869fe562018-09-17 14:55:49 -040062 if (fOriginalKey.isValid()) {
Brian Salomon238069b2018-07-11 15:58:57 -040063 GrInstallBitmapUniqueKeyInvalidator(
64 fOriginalKey, proxyProvider->contextUniqueID(), fBitmap.pixelRef());
Greg Danielfc5060d2017-10-04 18:36:15 +000065 }
66 return proxy;
Greg Danielbb76ace2017-09-29 15:58:22 -040067 }
Greg Daniel55afd6d2017-09-29 09:32:44 -040068 }
Robert Phillips0c984a02017-03-16 07:51:56 -040069 }
Greg Danielfc5060d2017-10-04 18:36:15 +000070
71 if (proxy) {
72 SkASSERT(willBeMipped);
Greg Daniele252f082017-10-23 16:05:23 -040073 SkASSERT(GrMipMapped::kNo == proxy->mipMapped());
Greg Danielfc5060d2017-10-04 18:36:15 +000074 // We need a mipped proxy, but we either found a proxy earlier that wasn't mipped or
75 // generated a non mipped proxy. Thus we generate a new mipped surface and copy the original
76 // proxy into the base layer. We will then let the gpu generate the rest of the mips.
Greg Daniele1da1d92017-10-06 15:59:27 -040077 if (auto mippedProxy = GrCopyBaseMipMapToTextureProxy(this->context(), proxy.get())) {
Greg Danielfc5060d2017-10-04 18:36:15 +000078 SkASSERT(mippedProxy->origin() == kTopLeft_GrSurfaceOrigin);
79 if (fOriginalKey.isValid()) {
80 // In this case we are stealing the key from the original proxy which should only
81 // happen when we have just generated mipmaps for an originally unmipped
82 // proxy/texture. This means that all future uses of the key will access the
83 // mipmapped version. The texture backing the unmipped version will remain in the
84 // resource cache until the last texture proxy referencing it is deleted at which
85 // time it too will be deleted or recycled.
Chris Dalton2de13dd2019-01-03 15:11:59 -070086 SkASSERT(proxy->getUniqueKey() == fOriginalKey);
87 proxyProvider->removeUniqueKeyFromProxy(proxy.get());
Robert Phillips1afd4cd2018-01-08 13:40:32 -050088 proxyProvider->assignUniqueKeyToProxy(fOriginalKey, mippedProxy.get());
Robert Phillips869fe562018-09-17 14:55:49 -040089 GrInstallBitmapUniqueKeyInvalidator(fOriginalKey, proxyProvider->contextUniqueID(),
90 fBitmap.pixelRef());
Greg Danielfc5060d2017-10-04 18:36:15 +000091 }
92 return mippedProxy;
Greg Daniel87c76ed2017-10-03 13:42:45 +000093 }
Greg Daniel8f5bbda2018-06-08 17:22:23 -040094 // We failed to make a mipped proxy with the base copied into it. This could have
95 // been from failure to make the proxy or failure to do the copy. Thus we will fall
96 // back to just using the non mipped proxy; See skbug.com/7094.
97 return proxy;
Greg Daniel87c76ed2017-10-03 13:42:45 +000098 }
Greg Danielfc5060d2017-10-04 18:36:15 +000099 return nullptr;
Robert Phillips0c984a02017-03-16 07:51:56 -0400100}
101
Brian Osmanb3f38302018-09-07 15:24:44 -0400102void GrBitmapTextureMaker::makeCopyKey(const CopyParams& copyParams, GrUniqueKey* copyKey) {
Brian Osman61624f02016-12-09 14:51:59 -0500103 // Destination color space is irrelevant - we always upload the bitmap's contents as-is
Brian Osman3b66ab62016-11-28 09:26:31 -0500104 if (fOriginalKey.isValid()) {
105 MakeCopyKeyFromOrigKey(fOriginalKey, copyParams, copyKey);
106 }
107}
108
Brian Salomon238069b2018-07-11 15:58:57 -0400109void GrBitmapTextureMaker::didCacheCopy(const GrUniqueKey& copyKey, uint32_t contextUniqueID) {
110 GrInstallBitmapUniqueKeyInvalidator(copyKey, contextUniqueID, fBitmap.pixelRef());
Brian Osman3b66ab62016-11-28 09:26:31 -0500111}
112
113SkAlphaType GrBitmapTextureMaker::alphaType() const {
114 return fBitmap.alphaType();
115}
116
Brian Osman6064e1c2018-10-19 14:27:54 -0400117SkColorSpace* GrBitmapTextureMaker::colorSpace() const {
118 return fBitmap.colorSpace();
Brian Osman3b66ab62016-11-28 09:26:31 -0500119}