blob: 91db08bea5438543313be4c81934bd0a6bcb5f7e [file] [log] [blame]
bsalomon@google.com047696c2012-09-11 13:29:29 +00001/*
2 * Copyright 2012 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 "GrTextureAccess.h"
bsalomon@google.comb8eb2e82013-03-28 13:46:42 +00009#include "GrColor.h"
bsalomon@google.com6d003d12012-09-11 15:45:20 +000010#include "GrTexture.h"
11
12GrTextureAccess::GrTextureAccess() {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000013#ifdef SK_DEBUG
bsalomon@google.com6d003d12012-09-11 15:45:20 +000014 memcpy(fSwizzle, "void", 5);
15 fSwizzleMask = 0xbeeffeed;
16#endif
bsalomon@google.com047696c2012-09-11 13:29:29 +000017}
18
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000019GrTextureAccess::GrTextureAccess(GrTexture* texture, const GrTextureParams& params) {
20 this->reset(texture, params);
bsalomon@google.com6d003d12012-09-11 15:45:20 +000021}
22
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000023GrTextureAccess::GrTextureAccess(GrTexture* texture,
humper@google.comb86add12013-07-25 18:49:07 +000024 GrTextureParams::FilterMode filterMode,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000025 SkShader::TileMode tileXAndY) {
humper@google.comb86add12013-07-25 18:49:07 +000026 this->reset(texture, filterMode, tileXAndY);
bsalomon@google.com6d003d12012-09-11 15:45:20 +000027}
28
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000029GrTextureAccess::GrTextureAccess(GrTexture* texture,
30 const char* swizzle,
31 const GrTextureParams& params) {
32 this->reset(texture, swizzle, params);
33}
34
35GrTextureAccess::GrTextureAccess(GrTexture* texture,
36 const char* swizzle,
humper@google.comb86add12013-07-25 18:49:07 +000037 GrTextureParams::FilterMode filterMode,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000038 SkShader::TileMode tileXAndY) {
humper@google.comb86add12013-07-25 18:49:07 +000039 this->reset(texture, swizzle, filterMode, tileXAndY);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000040}
41
42void GrTextureAccess::reset(GrTexture* texture,
43 const char* swizzle,
44 const GrTextureParams& params) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000045 SkASSERT(NULL != texture);
46 SkASSERT(strlen(swizzle) >= 1 && strlen(swizzle) <= 4);
bsalomon@google.com6d003d12012-09-11 15:45:20 +000047
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000048 fParams = params;
49 fTexture.reset(SkRef(texture));
50 this->setSwizzle(swizzle);
51}
bsalomon@google.com6d003d12012-09-11 15:45:20 +000052
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000053void GrTextureAccess::reset(GrTexture* texture,
54 const char* swizzle,
humper@google.comb86add12013-07-25 18:49:07 +000055 GrTextureParams::FilterMode filterMode,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000056 SkShader::TileMode tileXAndY) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000057 SkASSERT(NULL != texture);
58 SkASSERT(strlen(swizzle) >= 1 && strlen(swizzle) <= 4);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000059
humper@google.comb86add12013-07-25 18:49:07 +000060 fParams.reset(tileXAndY, filterMode);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000061 fTexture.reset(SkRef(texture));
62 this->setSwizzle(swizzle);
63}
64
65void GrTextureAccess::reset(GrTexture* texture,
66 const GrTextureParams& params) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000067 SkASSERT(NULL != texture);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000068 fTexture.reset(SkRef(texture));
69 fParams = params;
70 memcpy(fSwizzle, "rgba", 5);
bsalomon@google.comb8eb2e82013-03-28 13:46:42 +000071 fSwizzleMask = kRGBA_GrColorComponentFlags;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000072}
73
74void GrTextureAccess::reset(GrTexture* texture,
humper@google.comb86add12013-07-25 18:49:07 +000075 GrTextureParams::FilterMode filterMode,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000076 SkShader::TileMode tileXAndY) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000077 SkASSERT(NULL != texture);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000078 fTexture.reset(SkRef(texture));
humper@google.comb86add12013-07-25 18:49:07 +000079 fParams.reset(tileXAndY, filterMode);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000080 memcpy(fSwizzle, "rgba", 5);
bsalomon@google.comb8eb2e82013-03-28 13:46:42 +000081 fSwizzleMask = kRGBA_GrColorComponentFlags;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000082}
83
84void GrTextureAccess::setSwizzle(const char* swizzle) {
bsalomon@google.com6d003d12012-09-11 15:45:20 +000085 fSwizzleMask = 0;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000086 memset(fSwizzle, '\0', 5);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000087 for (int i = 0; i < 4 && '\0' != swizzle[i]; ++i) {
bsalomon@google.com6d003d12012-09-11 15:45:20 +000088 fSwizzle[i] = swizzle[i];
89 switch (swizzle[i]) {
90 case 'r':
bsalomon@google.comb8eb2e82013-03-28 13:46:42 +000091 fSwizzleMask |= kR_GrColorComponentFlag;
bsalomon@google.com6d003d12012-09-11 15:45:20 +000092 break;
93 case 'g':
bsalomon@google.comb8eb2e82013-03-28 13:46:42 +000094 fSwizzleMask |= kG_GrColorComponentFlag;
bsalomon@google.com6d003d12012-09-11 15:45:20 +000095 break;
96 case 'b':
bsalomon@google.comb8eb2e82013-03-28 13:46:42 +000097 fSwizzleMask |= kB_GrColorComponentFlag;
bsalomon@google.com6d003d12012-09-11 15:45:20 +000098 break;
99 case 'a':
bsalomon@google.comb8eb2e82013-03-28 13:46:42 +0000100 fSwizzleMask |= kA_GrColorComponentFlag;
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000101 break;
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000102 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000103 SkFAIL("Unexpected swizzle string character.");
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000104 break;
105 }
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000106 }
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000107}