OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2015 Google Inc. | 3 * Copyright 2015 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 // This test only works with the GPU backend. | 9 // This test only works with the GPU backend. |
10 | 10 |
11 #include "gm.h" | 11 #include "gm.h" |
12 | 12 |
13 #if SK_SUPPORT_GPU | 13 #if SK_SUPPORT_GPU |
14 | 14 |
15 #include "GrContext.h" | 15 #include "GrContext.h" |
16 #include "gl/GrGLInterface.h" | |
17 #include "gl/GrGLUtil.h" | |
18 #include "GrTest.h" | 16 #include "GrTest.h" |
19 #include "SkBitmap.h" | 17 #include "SkBitmap.h" |
20 #include "SkGradientShader.h" | 18 #include "SkGradientShader.h" |
21 #include "SkImage.h" | 19 #include "SkImage.h" |
22 | 20 |
23 namespace skiagm { | 21 namespace skiagm { |
24 class ImageFromYUVTextures : public GM { | 22 class ImageFromYUVTextures : public GM { |
25 public: | 23 public: |
26 ImageFromYUVTextures() { | 24 ImageFromYUVTextures() { |
27 this->setBGColor(0xFFFFFFFF); | 25 this->setBGColor(0xFFFFFFFF); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 int uvIndex = j * kBmpSize / 2 + i; | 87 int uvIndex = j * kBmpSize / 2 + i; |
90 uvPixels[0][uvIndex] = static_cast<signed char>( | 88 uvPixels[0][uvIndex] = static_cast<signed char>( |
91 ((-38 * rgb[0] - 74 * rgb[1] + 112 * rgb[2] + 128) >> 8) +
128); | 89 ((-38 * rgb[0] - 74 * rgb[1] + 112 * rgb[2] + 128) >> 8) +
128); |
92 uvPixels[1][uvIndex] = static_cast<signed char>( | 90 uvPixels[1][uvIndex] = static_cast<signed char>( |
93 ((112 * rgb[0] - 94 * rgb[1] - 18 * rgb[2] + 128) >> 8) +
128); | 91 ((112 * rgb[0] - 94 * rgb[1] - 18 * rgb[2] + 128) >> 8) +
128); |
94 } | 92 } |
95 } | 93 } |
96 fRGBImage.reset(SkImage::NewRasterCopy(rgbBmp.info(), rgbColors, rgbBmp.
rowBytes())); | 94 fRGBImage.reset(SkImage::NewRasterCopy(rgbBmp.info(), rgbColors, rgbBmp.
rowBytes())); |
97 } | 95 } |
98 | 96 |
99 void createYUVTextures(GrContext* context, GrGLuint yuvIDs[3]) { | 97 void createYUVTextures(GrContext* context, GrBackendObject yuvIDs[3]) { |
100 GrTestTarget tt; | 98 const GrGpu* gpu = context->getGpu(); |
101 context->getTestTarget(&tt); | 99 if (!gpu) { |
102 if (!tt.target()) { | |
103 SkDEBUGFAIL("Couldn't get Gr test target."); | |
104 return; | 100 return; |
105 } | 101 } |
106 | 102 |
107 // We currently hav only implemented the texture uploads for GL. | |
108 const GrGLInterface* gl = tt.glContext()->interface(); | |
109 if (!gl) { | |
110 return; | |
111 } | |
112 | |
113 GR_GL_CALL(gl, GenTextures(3, yuvIDs)); | |
114 GR_GL_CALL(gl, ActiveTexture(GR_GL_TEXTURE0)); | |
115 GR_GL_CALL(gl, PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1)); | |
116 for (int i = 0; i < 3; ++i) { | 103 for (int i = 0; i < 3; ++i) { |
117 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, yuvIDs[i])); | |
118 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_MAG_FIL
TER, | |
119 GR_GL_NEAREST)); | |
120 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_MIN_FIL
TER, | |
121 GR_GL_NEAREST)); | |
122 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_WRAP_S, | |
123 GR_GL_CLAMP_TO_EDGE)); | |
124 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_WRAP_T, | |
125 GR_GL_CLAMP_TO_EDGE)); | |
126 SkASSERT(fYUVBmps[i].width() == SkToInt(fYUVBmps[i].rowBytes())); | 104 SkASSERT(fYUVBmps[i].width() == SkToInt(fYUVBmps[i].rowBytes())); |
127 GR_GL_CALL(gl, TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RED, fYUVBmps[i
].width(), | 105 yuvIDs[i] = gpu->createBackendTexture(fYUVBmps[i].getPixels(), |
128 fYUVBmps[i].height(), 0, GR_GL_RED, GR_GL_
UNSIGNED_BYTE, | 106 fYUVBmps[i].width(), fYUVBmps[
i].height(), |
129 fYUVBmps[i].getPixels())); | 107 kAlpha_8_GrPixelConfig); |
130 } | 108 } |
131 context->resetContext(); | 109 context->resetContext(); |
132 } | 110 } |
133 | 111 |
134 void deleteYUVTextures(GrContext* context, const GrGLuint yuvIDs[3]) { | 112 void deleteYUVTextures(GrContext* context, const GrBackendObject yuvIDs[3])
{ |
135 GrTestTarget tt; | 113 |
136 context->getTestTarget(&tt); | 114 const GrGpu* gpu = context->getGpu(); |
137 if (!tt.target()) { | 115 if (!gpu) { |
138 SkDEBUGFAIL("Couldn't get Gr test target."); | |
139 return; | 116 return; |
140 } | 117 } |
141 | 118 |
142 const GrGLInterface* gl = tt.glContext()->interface(); | 119 for (int i = 0; i < 3; ++i) { |
143 if (!gl) { | 120 gpu->deleteBackendTexture(yuvIDs[i]); |
144 return; | |
145 } | 121 } |
146 GR_GL_CALL(gl, DeleteTextures(3, yuvIDs)); | 122 |
147 context->resetContext(); | 123 context->resetContext(); |
148 } | 124 } |
149 | 125 |
150 void onDraw(SkCanvas* canvas) override { | 126 void onDraw(SkCanvas* canvas) override { |
151 GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget
(); | 127 GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget
(); |
152 GrContext* context; | 128 GrContext* context; |
153 if (!rt || !(context = rt->getContext())) { | 129 if (!rt || !(context = rt->getContext())) { |
154 this->drawGpuOnlyMessage(canvas); | 130 this->drawGpuOnlyMessage(canvas); |
155 return; | 131 return; |
156 } | 132 } |
157 | 133 |
158 GrGLuint yuvIDs[3]; | 134 GrBackendObject yuvIDs[3]; |
159 this->createYUVTextures(context, yuvIDs); | 135 this->createYUVTextures(context, yuvIDs); |
160 | 136 |
161 static const SkScalar kPad = 10.f; | 137 static const SkScalar kPad = 10.f; |
162 | 138 |
163 GrBackendObject backendTextureObjects[] = { | |
164 static_cast<GrBackendObject>(yuvIDs[0]), | |
165 static_cast<GrBackendObject>(yuvIDs[1]), | |
166 static_cast<GrBackendObject>(yuvIDs[2]) | |
167 }; | |
168 SkISize sizes[] = { | 139 SkISize sizes[] = { |
169 { fYUVBmps[0].width(), fYUVBmps[0].height()}, | 140 { fYUVBmps[0].width(), fYUVBmps[0].height()}, |
170 { fYUVBmps[1].width(), fYUVBmps[1].height()}, | 141 { fYUVBmps[1].width(), fYUVBmps[1].height()}, |
171 { fYUVBmps[2].width(), fYUVBmps[2].height()}, | 142 { fYUVBmps[2].width(), fYUVBmps[2].height()}, |
172 }; | 143 }; |
173 SkTArray<SkImage*> images; | 144 SkTArray<SkImage*> images; |
174 images.push_back(SkRef(fRGBImage.get())); | 145 images.push_back(SkRef(fRGBImage.get())); |
175 for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpa
ce; ++space) { | 146 for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpa
ce; ++space) { |
176 images.push_back(SkImage::NewFromYUVTexturesCopy(context, | 147 images.push_back(SkImage::NewFromYUVTexturesCopy(context, |
177 static_cast<SkYUVCo
lorSpace>(space), | 148 static_cast<SkYUVCo
lorSpace>(space), |
178 backendTextureObjec
ts, sizes, | 149 yuvIDs, sizes, |
179 kTopLeft_GrSurfaceO
rigin)); | 150 kTopLeft_GrSurfaceO
rigin)); |
180 } | 151 } |
181 this->deleteYUVTextures(context, yuvIDs); | 152 this->deleteYUVTextures(context, yuvIDs); |
182 for (int i = 0; i < images.count(); ++ i) { | 153 for (int i = 0; i < images.count(); ++ i) { |
183 SkScalar y = (i + 1) * kPad + i * fYUVBmps[0].height(); | 154 SkScalar y = (i + 1) * kPad + i * fYUVBmps[0].height(); |
184 SkScalar x = kPad; | 155 SkScalar x = kPad; |
185 | 156 |
186 canvas->drawImage(images[i], x, y); | 157 canvas->drawImage(images[i], x, y); |
187 images[i]->unref(); | 158 images[i]->unref(); |
188 images[i] = NULL; | 159 images[i] = NULL; |
189 } | 160 } |
190 } | 161 } |
191 | 162 |
192 private: | 163 private: |
193 SkAutoTUnref<SkImage> fRGBImage; | 164 SkAutoTUnref<SkImage> fRGBImage; |
194 SkBitmap fYUVBmps[3]; | 165 SkBitmap fYUVBmps[3]; |
195 | 166 |
196 static const int kBmpSize = 32; | 167 static const int kBmpSize = 32; |
197 | 168 |
198 typedef GM INHERITED; | 169 typedef GM INHERITED; |
199 }; | 170 }; |
200 | 171 |
201 DEF_GM( return SkNEW(ImageFromYUVTextures); ) | 172 DEF_GM( return SkNEW(ImageFromYUVTextures); ) |
202 } | 173 } |
203 | 174 |
204 #endif | 175 #endif |
OLD | NEW |