blob: 4ff3ed5d956f4e4daec4ee597e175cab2d55d381 [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
bsalomonc55271f2015-11-09 11:55:57 -080020GrBitmapTextureAdjuster::GrBitmapTextureAdjuster(const SkBitmap* bmp)
bsalomonf1ecd212015-12-09 17:06:02 -080021 : INHERITED(bmp->getTexture(),
22 SkIRect::MakeWH(bmp->width(), bmp->height()),
23 bmp_is_alpha_only(*bmp))
bsalomonc55271f2015-11-09 11:55:57 -080024 , fBmp(bmp) {}
25
26void GrBitmapTextureAdjuster::makeCopyKey(const CopyParams& params, GrUniqueKey* copyKey) {
27 if (fBmp->isVolatile()) {
28 return;
29 }
30 // The content area must represent the whole bitmap. Texture-backed bitmaps don't support
31 // extractSubset(). Therefore, either the bitmap and the texture are the same size or the
32 // content's dimensions are the bitmap's dimensions which is pinned to the upper left
33 // of the texture.
34 GrUniqueKey baseKey;
35 GrMakeKeyFromImageID(&baseKey, fBmp->getGenerationID(),
36 SkIRect::MakeWH(fBmp->width(), fBmp->height()));
37 MakeCopyKeyFromOrigKey(baseKey, params, copyKey);
38}
39
40void GrBitmapTextureAdjuster::didCacheCopy(const GrUniqueKey& copyKey) {
41 GrInstallBitmapUniqueKeyInvalidator(copyKey, fBmp->pixelRef());
42}
43
44//////////////////////////////////////////////////////////////////////////////
45
bsalomonf1ecd212015-12-09 17:06:02 -080046// SkImage's don't have a way of communicating whether they're alpha-only. So we fallback to
47// inspecting the texture.
48static bool tex_image_is_alpha_only(const SkImage_Base& img) {
49 return GrPixelConfigIsAlphaOnly(img.peekTexture()->config());
50}
51
bsalomonc55271f2015-11-09 11:55:57 -080052GrImageTextureAdjuster::GrImageTextureAdjuster(const SkImage_Base* img)
bsalomonf1ecd212015-12-09 17:06:02 -080053 : INHERITED(img->peekTexture(), SkIRect::MakeWH(img->width(), img->height()),
54 tex_image_is_alpha_only(*img))
bsalomonc55271f2015-11-09 11:55:57 -080055 , fImageBase(img) {}
56
57void GrImageTextureAdjuster::makeCopyKey(const CopyParams& params, GrUniqueKey* copyKey) {
58 // By construction this texture adjuster always represents an entire SkImage, so use the
59 // image's width and height for the key's rectangle.
60 GrUniqueKey baseKey;
61 GrMakeKeyFromImageID(&baseKey, fImageBase->uniqueID(),
62 SkIRect::MakeWH(fImageBase->width(), fImageBase->height()));
63 MakeCopyKeyFromOrigKey(baseKey, params, copyKey);
64}
65
66void GrImageTextureAdjuster::didCacheCopy(const GrUniqueKey& copyKey) {
67 // We don't currently have a mechanism for notifications on Images!
68}
bsalomonb1b01992015-11-18 10:56:08 -080069
70//////////////////////////////////////////////////////////////////////////////
71
72GrBitmapTextureMaker::GrBitmapTextureMaker(GrContext* context, const SkBitmap& bitmap)
bsalomonf1ecd212015-12-09 17:06:02 -080073 : INHERITED(context, bitmap.width(), bitmap.height(), bmp_is_alpha_only(bitmap))
bsalomonb1b01992015-11-18 10:56:08 -080074 , fBitmap(bitmap) {
75 SkASSERT(!bitmap.getTexture());
76 if (!bitmap.isVolatile()) {
77 SkIPoint origin = bitmap.pixelRefOrigin();
78 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, bitmap.width(),
79 bitmap.height());
80 GrMakeKeyFromImageID(&fOriginalKey, bitmap.pixelRef()->getGenerationID(), subset);
81 }
82}
83
brianosman982eb7f2016-06-06 13:10:58 -070084GrTexture* GrBitmapTextureMaker::refOriginalTexture(bool willBeMipped,
85 SkSourceGammaTreatment gammaTreatment) {
brianosmaneb3429c2016-03-25 13:03:03 -070086 GrTexture* tex = nullptr;
bsalomonb1b01992015-11-18 10:56:08 -080087
88 if (fOriginalKey.isValid()) {
89 tex = this->context()->textureProvider()->findAndRefTextureByUniqueKey(fOriginalKey);
90 if (tex) {
91 return tex;
92 }
93 }
cblume55f2d2d2016-02-26 13:20:48 -080094 if (willBeMipped) {
brianosman982eb7f2016-06-06 13:10:58 -070095 tex = GrGenerateMipMapsAndUploadToTexture(this->context(), fBitmap, gammaTreatment);
brianosmaneb3429c2016-03-25 13:03:03 -070096 }
97 if (!tex) {
cblume55f2d2d2016-02-26 13:20:48 -080098 tex = GrUploadBitmapToTexture(this->context(), fBitmap);
99 }
bsalomonb1b01992015-11-18 10:56:08 -0800100 if (tex && fOriginalKey.isValid()) {
101 tex->resourcePriv().setUniqueKey(fOriginalKey);
102 GrInstallBitmapUniqueKeyInvalidator(fOriginalKey, fBitmap.pixelRef());
103 }
104 return tex;
105}
106
107void GrBitmapTextureMaker::makeCopyKey(const CopyParams& copyParams, GrUniqueKey* copyKey) {
108 if (fOriginalKey.isValid()) {
109 MakeCopyKeyFromOrigKey(fOriginalKey, copyParams, copyKey);
110 }
111}
112
113void GrBitmapTextureMaker::didCacheCopy(const GrUniqueKey& copyKey) {
114 GrInstallBitmapUniqueKeyInvalidator(copyKey, fBitmap.pixelRef());
115}
bsalomon1cf6f9b2015-12-08 10:53:43 -0800116
117//////////////////////////////////////////////////////////////////////////////
bsalomonf1ecd212015-12-09 17:06:02 -0800118static bool cacher_is_alpha_only(const SkImageCacherator& cacher) {
119 return kAlpha_8_SkColorType == cacher.info().colorType();
120}
bsalomon1cf6f9b2015-12-08 10:53:43 -0800121GrImageTextureMaker::GrImageTextureMaker(GrContext* context, SkImageCacherator* cacher,
122 const SkImage* client, SkImage::CachingHint chint)
bsalomonf1ecd212015-12-09 17:06:02 -0800123 : INHERITED(context, cacher->info().width(), cacher->info().height(),
124 cacher_is_alpha_only(*cacher))
bsalomon1cf6f9b2015-12-08 10:53:43 -0800125 , fCacher(cacher)
126 , fClient(client)
127 , fCachingHint(chint) {
128 if (client) {
129 GrMakeKeyFromImageID(&fOriginalKey, client->uniqueID(),
130 SkIRect::MakeWH(this->width(), this->height()));
131 }
132}
133
brianosman982eb7f2016-06-06 13:10:58 -0700134GrTexture* GrImageTextureMaker::refOriginalTexture(bool willBeMipped,
135 SkSourceGammaTreatment gammaTreatment) {
136 return fCacher->lockTexture(this->context(), fOriginalKey, fClient, fCachingHint, willBeMipped,
137 gammaTreatment);
bsalomon1cf6f9b2015-12-08 10:53:43 -0800138}
139
140void GrImageTextureMaker::makeCopyKey(const CopyParams& stretch, GrUniqueKey* paramsCopyKey) {
141 if (fOriginalKey.isValid() && SkImage::kAllow_CachingHint == fCachingHint) {
142 MakeCopyKeyFromOrigKey(fOriginalKey, stretch, paramsCopyKey);
143 }
144}
145
146void GrImageTextureMaker::didCacheCopy(const GrUniqueKey& copyKey) {
147 if (fClient) {
148 as_IB(fClient)->notifyAddedToCache();
149 }
150}