blob: cc869baf7f2e3486df2537d7631c8ff98037098b [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
55void GrBitmapTextureMaker::makeCopyKey(const CopyParams& copyParams, GrUniqueKey* copyKey) {
56 if (fOriginalKey.isValid()) {
57 MakeCopyKeyFromOrigKey(fOriginalKey, copyParams, copyKey);
58 }
59}
60
61void GrBitmapTextureMaker::didCacheCopy(const GrUniqueKey& copyKey) {
62 GrInstallBitmapUniqueKeyInvalidator(copyKey, fBitmap.pixelRef());
63}
bsalomon1cf6f9b2015-12-08 10:53:43 -080064
brianosman5814b8a2016-08-18 06:43:03 -070065SkAlphaType GrBitmapTextureMaker::alphaType() const {
66 return fBitmap.alphaType();
67}
68
brianosman42803662016-07-15 06:59:48 -070069SkColorSpace* GrBitmapTextureMaker::getColorSpace() {
70 return fBitmap.colorSpace();
71}
72
bsalomon1cf6f9b2015-12-08 10:53:43 -080073//////////////////////////////////////////////////////////////////////////////
bsalomonf1ecd212015-12-09 17:06:02 -080074static bool cacher_is_alpha_only(const SkImageCacherator& cacher) {
75 return kAlpha_8_SkColorType == cacher.info().colorType();
76}
bsalomon1cf6f9b2015-12-08 10:53:43 -080077GrImageTextureMaker::GrImageTextureMaker(GrContext* context, SkImageCacherator* cacher,
78 const SkImage* client, SkImage::CachingHint chint)
bsalomonf1ecd212015-12-09 17:06:02 -080079 : INHERITED(context, cacher->info().width(), cacher->info().height(),
80 cacher_is_alpha_only(*cacher))
bsalomon1cf6f9b2015-12-08 10:53:43 -080081 , fCacher(cacher)
82 , fClient(client)
83 , fCachingHint(chint) {
84 if (client) {
85 GrMakeKeyFromImageID(&fOriginalKey, client->uniqueID(),
86 SkIRect::MakeWH(this->width(), this->height()));
87 }
88}
89
brianosman982eb7f2016-06-06 13:10:58 -070090GrTexture* GrImageTextureMaker::refOriginalTexture(bool willBeMipped,
Brian Osman7b8400d2016-11-08 17:08:54 -050091 SkDestinationSurfaceColorMode colorMode) {
brianosman982eb7f2016-06-06 13:10:58 -070092 return fCacher->lockTexture(this->context(), fOriginalKey, fClient, fCachingHint, willBeMipped,
Brian Osman7b8400d2016-11-08 17:08:54 -050093 colorMode);
bsalomon1cf6f9b2015-12-08 10:53:43 -080094}
95
96void GrImageTextureMaker::makeCopyKey(const CopyParams& stretch, GrUniqueKey* paramsCopyKey) {
97 if (fOriginalKey.isValid() && SkImage::kAllow_CachingHint == fCachingHint) {
98 MakeCopyKeyFromOrigKey(fOriginalKey, stretch, paramsCopyKey);
99 }
100}
101
102void GrImageTextureMaker::didCacheCopy(const GrUniqueKey& copyKey) {
103 if (fClient) {
104 as_IB(fClient)->notifyAddedToCache();
105 }
106}
brianosman42803662016-07-15 06:59:48 -0700107
brianosman5814b8a2016-08-18 06:43:03 -0700108SkAlphaType GrImageTextureMaker::alphaType() const {
109 return fCacher->info().alphaType();
110}
111
brianosman42803662016-07-15 06:59:48 -0700112SkColorSpace* GrImageTextureMaker::getColorSpace() {
113 return fCacher->info().colorSpace();
114}