blob: 989102e740f2811dd428ffc30449cd9b3432206d [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
bsalomonf1ecd212015-12-09 17:06:02 -080020// SkImage's don't have a way of communicating whether they're alpha-only. So we fallback to
21// inspecting the texture.
22static bool tex_image_is_alpha_only(const SkImage_Base& img) {
23 return GrPixelConfigIsAlphaOnly(img.peekTexture()->config());
24}
25
bsalomonc55271f2015-11-09 11:55:57 -080026GrImageTextureAdjuster::GrImageTextureAdjuster(const SkImage_Base* img)
bsalomonf1ecd212015-12-09 17:06:02 -080027 : INHERITED(img->peekTexture(), SkIRect::MakeWH(img->width(), img->height()),
28 tex_image_is_alpha_only(*img))
bsalomonc55271f2015-11-09 11:55:57 -080029 , fImageBase(img) {}
30
31void GrImageTextureAdjuster::makeCopyKey(const CopyParams& params, GrUniqueKey* copyKey) {
32 // By construction this texture adjuster always represents an entire SkImage, so use the
33 // image's width and height for the key's rectangle.
34 GrUniqueKey baseKey;
35 GrMakeKeyFromImageID(&baseKey, fImageBase->uniqueID(),
36 SkIRect::MakeWH(fImageBase->width(), fImageBase->height()));
37 MakeCopyKeyFromOrigKey(baseKey, params, copyKey);
38}
39
40void GrImageTextureAdjuster::didCacheCopy(const GrUniqueKey& copyKey) {
41 // We don't currently have a mechanism for notifications on Images!
42}
bsalomonb1b01992015-11-18 10:56:08 -080043
brianosman42803662016-07-15 06:59:48 -070044SkColorSpace* GrImageTextureAdjuster::getColorSpace() {
45 return fImageBase->onImageInfo().colorSpace();
46}
47
bsalomonb1b01992015-11-18 10:56:08 -080048//////////////////////////////////////////////////////////////////////////////
49
50GrBitmapTextureMaker::GrBitmapTextureMaker(GrContext* context, const SkBitmap& bitmap)
bsalomonf1ecd212015-12-09 17:06:02 -080051 : INHERITED(context, bitmap.width(), bitmap.height(), bmp_is_alpha_only(bitmap))
reedc7ec7c92016-07-25 08:29:10 -070052 , fBitmap(bitmap)
53{
bsalomonb1b01992015-11-18 10:56:08 -080054 if (!bitmap.isVolatile()) {
55 SkIPoint origin = bitmap.pixelRefOrigin();
56 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, bitmap.width(),
57 bitmap.height());
58 GrMakeKeyFromImageID(&fOriginalKey, bitmap.pixelRef()->getGenerationID(), subset);
59 }
60}
61
brianosman982eb7f2016-06-06 13:10:58 -070062GrTexture* GrBitmapTextureMaker::refOriginalTexture(bool willBeMipped,
63 SkSourceGammaTreatment gammaTreatment) {
brianosmaneb3429c2016-03-25 13:03:03 -070064 GrTexture* tex = nullptr;
bsalomonb1b01992015-11-18 10:56:08 -080065
66 if (fOriginalKey.isValid()) {
67 tex = this->context()->textureProvider()->findAndRefTextureByUniqueKey(fOriginalKey);
68 if (tex) {
69 return tex;
70 }
71 }
cblume55f2d2d2016-02-26 13:20:48 -080072 if (willBeMipped) {
brianosman982eb7f2016-06-06 13:10:58 -070073 tex = GrGenerateMipMapsAndUploadToTexture(this->context(), fBitmap, gammaTreatment);
brianosmaneb3429c2016-03-25 13:03:03 -070074 }
75 if (!tex) {
cblume55f2d2d2016-02-26 13:20:48 -080076 tex = GrUploadBitmapToTexture(this->context(), fBitmap);
77 }
bsalomonb1b01992015-11-18 10:56:08 -080078 if (tex && fOriginalKey.isValid()) {
79 tex->resourcePriv().setUniqueKey(fOriginalKey);
80 GrInstallBitmapUniqueKeyInvalidator(fOriginalKey, fBitmap.pixelRef());
81 }
82 return tex;
83}
84
85void GrBitmapTextureMaker::makeCopyKey(const CopyParams& copyParams, GrUniqueKey* copyKey) {
86 if (fOriginalKey.isValid()) {
87 MakeCopyKeyFromOrigKey(fOriginalKey, copyParams, copyKey);
88 }
89}
90
91void GrBitmapTextureMaker::didCacheCopy(const GrUniqueKey& copyKey) {
92 GrInstallBitmapUniqueKeyInvalidator(copyKey, fBitmap.pixelRef());
93}
bsalomon1cf6f9b2015-12-08 10:53:43 -080094
brianosman42803662016-07-15 06:59:48 -070095SkColorSpace* GrBitmapTextureMaker::getColorSpace() {
96 return fBitmap.colorSpace();
97}
98
bsalomon1cf6f9b2015-12-08 10:53:43 -080099//////////////////////////////////////////////////////////////////////////////
bsalomonf1ecd212015-12-09 17:06:02 -0800100static bool cacher_is_alpha_only(const SkImageCacherator& cacher) {
101 return kAlpha_8_SkColorType == cacher.info().colorType();
102}
bsalomon1cf6f9b2015-12-08 10:53:43 -0800103GrImageTextureMaker::GrImageTextureMaker(GrContext* context, SkImageCacherator* cacher,
104 const SkImage* client, SkImage::CachingHint chint)
bsalomonf1ecd212015-12-09 17:06:02 -0800105 : INHERITED(context, cacher->info().width(), cacher->info().height(),
106 cacher_is_alpha_only(*cacher))
bsalomon1cf6f9b2015-12-08 10:53:43 -0800107 , fCacher(cacher)
108 , fClient(client)
109 , fCachingHint(chint) {
110 if (client) {
111 GrMakeKeyFromImageID(&fOriginalKey, client->uniqueID(),
112 SkIRect::MakeWH(this->width(), this->height()));
113 }
114}
115
brianosman982eb7f2016-06-06 13:10:58 -0700116GrTexture* GrImageTextureMaker::refOriginalTexture(bool willBeMipped,
117 SkSourceGammaTreatment gammaTreatment) {
118 return fCacher->lockTexture(this->context(), fOriginalKey, fClient, fCachingHint, willBeMipped,
119 gammaTreatment);
bsalomon1cf6f9b2015-12-08 10:53:43 -0800120}
121
122void GrImageTextureMaker::makeCopyKey(const CopyParams& stretch, GrUniqueKey* paramsCopyKey) {
123 if (fOriginalKey.isValid() && SkImage::kAllow_CachingHint == fCachingHint) {
124 MakeCopyKeyFromOrigKey(fOriginalKey, stretch, paramsCopyKey);
125 }
126}
127
128void GrImageTextureMaker::didCacheCopy(const GrUniqueKey& copyKey) {
129 if (fClient) {
130 as_IB(fClient)->notifyAddedToCache();
131 }
132}
brianosman42803662016-07-15 06:59:48 -0700133
134SkColorSpace* GrImageTextureMaker::getColorSpace() {
135 return fCacher->info().colorSpace();
136}