blob: 645d1c2a99ef55ffb99166ba1fabf4e36a26cf9a [file] [log] [blame]
bsalomonc55271f2015-11-09 11:55:57 -08001/*
2 * Copyright 2015 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 "GrImageIDTextureAdjuster.h"
9
bsalomonb1b01992015-11-18 10:56:08 -080010#include "GrContext.h"
11#include "GrGpuResourcePriv.h"
bsalomonc55271f2015-11-09 11:55:57 -080012#include "SkBitmap.h"
13#include "SkGrPriv.h"
14#include "SkImage_Base.h"
bsalomon1cf6f9b2015-12-08 10:53:43 -080015#include "SkImageCacherator.h"
bsalomonb1b01992015-11-18 10:56:08 -080016#include "SkPixelRef.h"
bsalomonc55271f2015-11-09 11:55:57 -080017
bsalomonf1ecd212015-12-09 17:06:02 -080018static bool bmp_is_alpha_only(const SkBitmap& bm) { return kAlpha_8_SkColorType == bm.colorType(); }
19
bsalomonb1b01992015-11-18 10:56:08 -080020GrBitmapTextureMaker::GrBitmapTextureMaker(GrContext* context, const SkBitmap& bitmap)
bsalomonf1ecd212015-12-09 17:06:02 -080021 : INHERITED(context, bitmap.width(), bitmap.height(), bmp_is_alpha_only(bitmap))
reedc7ec7c92016-07-25 08:29:10 -070022 , fBitmap(bitmap)
23{
bsalomonb1b01992015-11-18 10:56:08 -080024 if (!bitmap.isVolatile()) {
25 SkIPoint origin = bitmap.pixelRefOrigin();
26 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, bitmap.width(),
27 bitmap.height());
28 GrMakeKeyFromImageID(&fOriginalKey, bitmap.pixelRef()->getGenerationID(), subset);
29 }
30}
31
brianosman982eb7f2016-06-06 13:10:58 -070032GrTexture* GrBitmapTextureMaker::refOriginalTexture(bool willBeMipped,
Brian Osman7b8400d2016-11-08 17:08:54 -050033 SkDestinationSurfaceColorMode colorMode) {
brianosmaneb3429c2016-03-25 13:03:03 -070034 GrTexture* tex = nullptr;
bsalomonb1b01992015-11-18 10:56:08 -080035
36 if (fOriginalKey.isValid()) {
37 tex = this->context()->textureProvider()->findAndRefTextureByUniqueKey(fOriginalKey);
38 if (tex) {
39 return tex;
40 }
41 }
cblume55f2d2d2016-02-26 13:20:48 -080042 if (willBeMipped) {
Brian Osman7b8400d2016-11-08 17:08:54 -050043 tex = GrGenerateMipMapsAndUploadToTexture(this->context(), fBitmap, colorMode);
brianosmaneb3429c2016-03-25 13:03:03 -070044 }
45 if (!tex) {
cblume55f2d2d2016-02-26 13:20:48 -080046 tex = GrUploadBitmapToTexture(this->context(), fBitmap);
47 }
bsalomonb1b01992015-11-18 10:56:08 -080048 if (tex && fOriginalKey.isValid()) {
49 tex->resourcePriv().setUniqueKey(fOriginalKey);
50 GrInstallBitmapUniqueKeyInvalidator(fOriginalKey, fBitmap.pixelRef());
51 }
52 return tex;
53}
54
Brian Osman7992da32016-11-18 11:28:24 -050055void GrBitmapTextureMaker::makeCopyKey(const CopyParams& copyParams, GrUniqueKey* copyKey,
56 SkDestinationSurfaceColorMode colorMode) {
57 // Color mode is irrelevant in this case - we always upload the bitmap's contents as-is
bsalomonb1b01992015-11-18 10:56:08 -080058 if (fOriginalKey.isValid()) {
59 MakeCopyKeyFromOrigKey(fOriginalKey, copyParams, copyKey);
60 }
61}
62
63void GrBitmapTextureMaker::didCacheCopy(const GrUniqueKey& copyKey) {
64 GrInstallBitmapUniqueKeyInvalidator(copyKey, fBitmap.pixelRef());
65}
bsalomon1cf6f9b2015-12-08 10:53:43 -080066
brianosman5814b8a2016-08-18 06:43:03 -070067SkAlphaType GrBitmapTextureMaker::alphaType() const {
68 return fBitmap.alphaType();
69}
70
Brian Osman7992da32016-11-18 11:28:24 -050071sk_sp<SkColorSpace> GrBitmapTextureMaker::getColorSpace(SkDestinationSurfaceColorMode colorMode) {
72 // Color space doesn't depend on mode - it's just whatever is in the bitmap
73 return sk_ref_sp(fBitmap.colorSpace());
brianosman42803662016-07-15 06:59:48 -070074}
75
bsalomon1cf6f9b2015-12-08 10:53:43 -080076//////////////////////////////////////////////////////////////////////////////
bsalomonf1ecd212015-12-09 17:06:02 -080077static bool cacher_is_alpha_only(const SkImageCacherator& cacher) {
78 return kAlpha_8_SkColorType == cacher.info().colorType();
79}
bsalomon1cf6f9b2015-12-08 10:53:43 -080080GrImageTextureMaker::GrImageTextureMaker(GrContext* context, SkImageCacherator* cacher,
81 const SkImage* client, SkImage::CachingHint chint)
bsalomonf1ecd212015-12-09 17:06:02 -080082 : INHERITED(context, cacher->info().width(), cacher->info().height(),
83 cacher_is_alpha_only(*cacher))
bsalomon1cf6f9b2015-12-08 10:53:43 -080084 , fCacher(cacher)
85 , fClient(client)
86 , fCachingHint(chint) {
87 if (client) {
88 GrMakeKeyFromImageID(&fOriginalKey, client->uniqueID(),
89 SkIRect::MakeWH(this->width(), this->height()));
90 }
91}
92
brianosman982eb7f2016-06-06 13:10:58 -070093GrTexture* GrImageTextureMaker::refOriginalTexture(bool willBeMipped,
Brian Osman7b8400d2016-11-08 17:08:54 -050094 SkDestinationSurfaceColorMode colorMode) {
brianosman982eb7f2016-06-06 13:10:58 -070095 return fCacher->lockTexture(this->context(), fOriginalKey, fClient, fCachingHint, willBeMipped,
Brian Osman7b8400d2016-11-08 17:08:54 -050096 colorMode);
bsalomon1cf6f9b2015-12-08 10:53:43 -080097}
98
Brian Osman7992da32016-11-18 11:28:24 -050099void GrImageTextureMaker::makeCopyKey(const CopyParams& stretch, GrUniqueKey* paramsCopyKey,
100 SkDestinationSurfaceColorMode colorMode) {
bsalomon1cf6f9b2015-12-08 10:53:43 -0800101 if (fOriginalKey.isValid() && SkImage::kAllow_CachingHint == fCachingHint) {
Brian Osman7992da32016-11-18 11:28:24 -0500102 SkImageCacherator::CachedFormat cacheFormat =
103 fCacher->chooseCacheFormat(colorMode, this->context()->caps());
104 GrUniqueKey cacheKey;
105 fCacher->makeCacheKeyFromOrigKey(fOriginalKey, cacheFormat, &cacheKey);
106 MakeCopyKeyFromOrigKey(cacheKey, stretch, paramsCopyKey);
bsalomon1cf6f9b2015-12-08 10:53:43 -0800107 }
108}
109
110void GrImageTextureMaker::didCacheCopy(const GrUniqueKey& copyKey) {
111 if (fClient) {
112 as_IB(fClient)->notifyAddedToCache();
113 }
114}
brianosman42803662016-07-15 06:59:48 -0700115
brianosman5814b8a2016-08-18 06:43:03 -0700116SkAlphaType GrImageTextureMaker::alphaType() const {
117 return fCacher->info().alphaType();
118}
Brian Osman7992da32016-11-18 11:28:24 -0500119sk_sp<SkColorSpace> GrImageTextureMaker::getColorSpace(SkDestinationSurfaceColorMode colorMode) {
120 return fCacher->getColorSpace(this->context(), colorMode);
brianosman42803662016-07-15 06:59:48 -0700121}