blob: c37c02277589c2b0f4a543cee7b46b068a21aea4 [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"
bsalomonb1b01992015-11-18 10:56:08 -080015#include "SkPixelRef.h"
bsalomonc55271f2015-11-09 11:55:57 -080016
17GrBitmapTextureAdjuster::GrBitmapTextureAdjuster(const SkBitmap* bmp)
18 : INHERITED(bmp->getTexture(), SkIRect::MakeWH(bmp->width(), bmp->height()))
19 , fBmp(bmp) {}
20
21void GrBitmapTextureAdjuster::makeCopyKey(const CopyParams& params, GrUniqueKey* copyKey) {
22 if (fBmp->isVolatile()) {
23 return;
24 }
25 // The content area must represent the whole bitmap. Texture-backed bitmaps don't support
26 // extractSubset(). Therefore, either the bitmap and the texture are the same size or the
27 // content's dimensions are the bitmap's dimensions which is pinned to the upper left
28 // of the texture.
29 GrUniqueKey baseKey;
30 GrMakeKeyFromImageID(&baseKey, fBmp->getGenerationID(),
31 SkIRect::MakeWH(fBmp->width(), fBmp->height()));
32 MakeCopyKeyFromOrigKey(baseKey, params, copyKey);
33}
34
35void GrBitmapTextureAdjuster::didCacheCopy(const GrUniqueKey& copyKey) {
36 GrInstallBitmapUniqueKeyInvalidator(copyKey, fBmp->pixelRef());
37}
38
39//////////////////////////////////////////////////////////////////////////////
40
41GrImageTextureAdjuster::GrImageTextureAdjuster(const SkImage_Base* img)
42 : INHERITED(img->peekTexture(), SkIRect::MakeWH(img->width(), img->height()))
43 , fImageBase(img) {}
44
45void GrImageTextureAdjuster::makeCopyKey(const CopyParams& params, GrUniqueKey* copyKey) {
46 // By construction this texture adjuster always represents an entire SkImage, so use the
47 // image's width and height for the key's rectangle.
48 GrUniqueKey baseKey;
49 GrMakeKeyFromImageID(&baseKey, fImageBase->uniqueID(),
50 SkIRect::MakeWH(fImageBase->width(), fImageBase->height()));
51 MakeCopyKeyFromOrigKey(baseKey, params, copyKey);
52}
53
54void GrImageTextureAdjuster::didCacheCopy(const GrUniqueKey& copyKey) {
55 // We don't currently have a mechanism for notifications on Images!
56}
bsalomonb1b01992015-11-18 10:56:08 -080057
58//////////////////////////////////////////////////////////////////////////////
59
60GrBitmapTextureMaker::GrBitmapTextureMaker(GrContext* context, const SkBitmap& bitmap)
61 : INHERITED(context, bitmap.width(), bitmap.height())
62 , fBitmap(bitmap) {
63 SkASSERT(!bitmap.getTexture());
64 if (!bitmap.isVolatile()) {
65 SkIPoint origin = bitmap.pixelRefOrigin();
66 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, bitmap.width(),
67 bitmap.height());
68 GrMakeKeyFromImageID(&fOriginalKey, bitmap.pixelRef()->getGenerationID(), subset);
69 }
70}
71
72GrTexture* GrBitmapTextureMaker::refOriginalTexture() {
73 GrTexture* tex;
74
75 if (fOriginalKey.isValid()) {
76 tex = this->context()->textureProvider()->findAndRefTextureByUniqueKey(fOriginalKey);
77 if (tex) {
78 return tex;
79 }
80 }
81
82 tex = GrUploadBitmapToTexture(this->context(), fBitmap);
83 if (tex && fOriginalKey.isValid()) {
84 tex->resourcePriv().setUniqueKey(fOriginalKey);
85 GrInstallBitmapUniqueKeyInvalidator(fOriginalKey, fBitmap.pixelRef());
86 }
87 return tex;
88}
89
90void GrBitmapTextureMaker::makeCopyKey(const CopyParams& copyParams, GrUniqueKey* copyKey) {
91 if (fOriginalKey.isValid()) {
92 MakeCopyKeyFromOrigKey(fOriginalKey, copyParams, copyKey);
93 }
94}
95
96void GrBitmapTextureMaker::didCacheCopy(const GrUniqueKey& copyKey) {
97 GrInstallBitmapUniqueKeyInvalidator(copyKey, fBitmap.pixelRef());
98}