blob: a9189f63fab88ba161b1821283142564f57d63e0 [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
18GrBitmapTextureAdjuster::GrBitmapTextureAdjuster(const SkBitmap* bmp)
19 : INHERITED(bmp->getTexture(), SkIRect::MakeWH(bmp->width(), bmp->height()))
20 , fBmp(bmp) {}
21
22void GrBitmapTextureAdjuster::makeCopyKey(const CopyParams& params, GrUniqueKey* copyKey) {
23 if (fBmp->isVolatile()) {
24 return;
25 }
26 // The content area must represent the whole bitmap. Texture-backed bitmaps don't support
27 // extractSubset(). Therefore, either the bitmap and the texture are the same size or the
28 // content's dimensions are the bitmap's dimensions which is pinned to the upper left
29 // of the texture.
30 GrUniqueKey baseKey;
31 GrMakeKeyFromImageID(&baseKey, fBmp->getGenerationID(),
32 SkIRect::MakeWH(fBmp->width(), fBmp->height()));
33 MakeCopyKeyFromOrigKey(baseKey, params, copyKey);
34}
35
36void GrBitmapTextureAdjuster::didCacheCopy(const GrUniqueKey& copyKey) {
37 GrInstallBitmapUniqueKeyInvalidator(copyKey, fBmp->pixelRef());
38}
39
40//////////////////////////////////////////////////////////////////////////////
41
42GrImageTextureAdjuster::GrImageTextureAdjuster(const SkImage_Base* img)
43 : INHERITED(img->peekTexture(), SkIRect::MakeWH(img->width(), img->height()))
44 , fImageBase(img) {}
45
46void GrImageTextureAdjuster::makeCopyKey(const CopyParams& params, GrUniqueKey* copyKey) {
47 // By construction this texture adjuster always represents an entire SkImage, so use the
48 // image's width and height for the key's rectangle.
49 GrUniqueKey baseKey;
50 GrMakeKeyFromImageID(&baseKey, fImageBase->uniqueID(),
51 SkIRect::MakeWH(fImageBase->width(), fImageBase->height()));
52 MakeCopyKeyFromOrigKey(baseKey, params, copyKey);
53}
54
55void GrImageTextureAdjuster::didCacheCopy(const GrUniqueKey& copyKey) {
56 // We don't currently have a mechanism for notifications on Images!
57}
bsalomonb1b01992015-11-18 10:56:08 -080058
59//////////////////////////////////////////////////////////////////////////////
60
61GrBitmapTextureMaker::GrBitmapTextureMaker(GrContext* context, const SkBitmap& bitmap)
62 : INHERITED(context, bitmap.width(), bitmap.height())
63 , fBitmap(bitmap) {
64 SkASSERT(!bitmap.getTexture());
65 if (!bitmap.isVolatile()) {
66 SkIPoint origin = bitmap.pixelRefOrigin();
67 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, bitmap.width(),
68 bitmap.height());
69 GrMakeKeyFromImageID(&fOriginalKey, bitmap.pixelRef()->getGenerationID(), subset);
70 }
71}
72
73GrTexture* GrBitmapTextureMaker::refOriginalTexture() {
74 GrTexture* tex;
75
76 if (fOriginalKey.isValid()) {
77 tex = this->context()->textureProvider()->findAndRefTextureByUniqueKey(fOriginalKey);
78 if (tex) {
79 return tex;
80 }
81 }
82
83 tex = GrUploadBitmapToTexture(this->context(), fBitmap);
84 if (tex && fOriginalKey.isValid()) {
85 tex->resourcePriv().setUniqueKey(fOriginalKey);
86 GrInstallBitmapUniqueKeyInvalidator(fOriginalKey, fBitmap.pixelRef());
87 }
88 return tex;
89}
90
91void GrBitmapTextureMaker::makeCopyKey(const CopyParams& copyParams, GrUniqueKey* copyKey) {
92 if (fOriginalKey.isValid()) {
93 MakeCopyKeyFromOrigKey(fOriginalKey, copyParams, copyKey);
94 }
95}
96
97void GrBitmapTextureMaker::didCacheCopy(const GrUniqueKey& copyKey) {
98 GrInstallBitmapUniqueKeyInvalidator(copyKey, fBitmap.pixelRef());
99}
bsalomon1cf6f9b2015-12-08 10:53:43 -0800100
101//////////////////////////////////////////////////////////////////////////////
102
103GrImageTextureMaker::GrImageTextureMaker(GrContext* context, SkImageCacherator* cacher,
104 const SkImage* client, SkImage::CachingHint chint)
105 : INHERITED(context, cacher->info().width(), cacher->info().height())
106 , fCacher(cacher)
107 , fClient(client)
108 , fCachingHint(chint) {
109 if (client) {
110 GrMakeKeyFromImageID(&fOriginalKey, client->uniqueID(),
111 SkIRect::MakeWH(this->width(), this->height()));
112 }
113}
114
115GrTexture* GrImageTextureMaker::refOriginalTexture() {
116 return fCacher->lockTexture(this->context(), fOriginalKey, fClient, fCachingHint);
117}
118
119void GrImageTextureMaker::makeCopyKey(const CopyParams& stretch, GrUniqueKey* paramsCopyKey) {
120 if (fOriginalKey.isValid() && SkImage::kAllow_CachingHint == fCachingHint) {
121 MakeCopyKeyFromOrigKey(fOriginalKey, stretch, paramsCopyKey);
122 }
123}
124
125void GrImageTextureMaker::didCacheCopy(const GrUniqueKey& copyKey) {
126 if (fClient) {
127 as_IB(fClient)->notifyAddedToCache();
128 }
129}