OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkCanvas.h" | 8 #include "SkCanvas.h" |
9 #include "SkData.h" | 9 #include "SkData.h" |
10 #include "SkDevice.h" | 10 #include "SkDevice.h" |
11 #include "SkImageEncoder.h" | 11 #include "SkImageEncoder.h" |
12 #include "SkImage_Base.h" | 12 #include "SkImage_Base.h" |
13 #include "SkRRect.h" | 13 #include "SkRRect.h" |
14 #include "SkSurface.h" | 14 #include "SkSurface.h" |
15 #include "SkUtils.h" | 15 #include "SkUtils.h" |
16 #include "Test.h" | 16 #include "Test.h" |
17 | 17 |
18 #if SK_SUPPORT_GPU | 18 #if SK_SUPPORT_GPU |
19 #include "GrContextFactory.h" | 19 #include "GrContextFactory.h" |
| 20 #include "GrTest.h" |
| 21 #include "gl/GrGLInterface.h" |
| 22 #include "gl/GrGLUtil.h" |
20 #else | 23 #else |
21 class GrContextFactory; | 24 class GrContextFactory; |
22 class GrContext; | 25 class GrContext; |
23 #endif | 26 #endif |
24 | 27 |
25 enum SurfaceType { | 28 enum SurfaceType { |
26 kRaster_SurfaceType, | 29 kRaster_SurfaceType, |
27 kRasterDirect_SurfaceType, | 30 kRasterDirect_SurfaceType, |
28 kGpu_SurfaceType, | 31 kGpu_SurfaceType, |
29 kGpuScratch_SurfaceType, | 32 kGpuScratch_SurfaceType, |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 REPORTER_ASSERT(reporter, NULL == | 93 REPORTER_ASSERT(reporter, NULL == |
91 SkSurface::NewRenderTarget(ctx, SkSurface::kNo_Budgeted,
info, 0, NULL)); | 94 SkSurface::NewRenderTarget(ctx, SkSurface::kNo_Budgeted,
info, 0, NULL)); |
92 } | 95 } |
93 } | 96 } |
94 | 97 |
95 #if SK_SUPPORT_GPU | 98 #if SK_SUPPORT_GPU |
96 static void test_wrapped_texture_surface(skiatest::Reporter* reporter, GrContext
* ctx) { | 99 static void test_wrapped_texture_surface(skiatest::Reporter* reporter, GrContext
* ctx) { |
97 if (NULL == ctx) { | 100 if (NULL == ctx) { |
98 return; | 101 return; |
99 } | 102 } |
100 // Test the wrapped factory for SkSurface by creating a texture using ctx an
d then treat it as | |
101 // an external texture and wrap it in a SkSurface. | |
102 | 103 |
103 GrSurfaceDesc texDesc; | 104 GrTestTarget tt; |
104 texDesc.fConfig = kRGBA_8888_GrPixelConfig; | 105 ctx->getTestTarget(&tt); |
105 texDesc.fFlags = kRenderTarget_GrSurfaceFlag; | 106 if (!tt.target()) { |
106 texDesc.fWidth = texDesc.fHeight = 100; | 107 SkDEBUGFAIL("Couldn't get Gr test target."); |
107 texDesc.fSampleCnt = 0; | |
108 texDesc.fOrigin = kTopLeft_GrSurfaceOrigin; | |
109 SkAutoTUnref<GrSurface> dummySurface(ctx->textureProvider()->createTexture(t
exDesc, false)); | |
110 | |
111 REPORTER_ASSERT(reporter, dummySurface && dummySurface->asTexture() && | |
112 dummySurface->asRenderTarget()); | |
113 if (!dummySurface || !dummySurface->asTexture() || !dummySurface->asRenderTa
rget()) { | |
114 return; | 108 return; |
115 } | 109 } |
116 | 110 |
117 GrBackendObject textureHandle = dummySurface->asTexture()->getTextureHandle(
); | 111 // We currently have only implemented the texture uploads for GL. |
| 112 const GrGLInterface* gl = tt.glInterface(); |
| 113 if (!gl) { |
| 114 return; |
| 115 } |
| 116 |
| 117 // Test the wrapped factory for SkSurface by creating a texture using GL and
then wrap it in |
| 118 // a SkSurface. |
| 119 GrGLuint texID; |
| 120 static const int kW = 100; |
| 121 static const int kH = 100; |
| 122 static const uint32_t kOrigColor = 0xFFAABBCC; |
| 123 SkAutoTArray<uint32_t> pixels(kW * kH); |
| 124 sk_memset32(pixels.get(), kOrigColor, kW * kH); |
| 125 GR_GL_CALL(gl, GenTextures(1, &texID)); |
| 126 GR_GL_CALL(gl, ActiveTexture(GR_GL_TEXTURE0)); |
| 127 GR_GL_CALL(gl, PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1)); |
| 128 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, texID)); |
| 129 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_MAG_FILTER, |
| 130 GR_GL_NEAREST)); |
| 131 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_MIN_FILTER, |
| 132 GR_GL_NEAREST)); |
| 133 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_WRAP_S, |
| 134 GR_GL_CLAMP_TO_EDGE)); |
| 135 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_WRAP_T, |
| 136 GR_GL_CLAMP_TO_EDGE)); |
| 137 GR_GL_CALL(gl, TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RGBA, kW, kH, 0, GR_GL_
RGBA, |
| 138 GR_GL_UNSIGNED_BYTE, |
| 139 pixels.get())); |
118 | 140 |
119 GrBackendTextureDesc wrappedDesc; | 141 GrBackendTextureDesc wrappedDesc; |
120 wrappedDesc.fConfig = dummySurface->config(); | 142 wrappedDesc.fConfig = kRGBA_8888_GrPixelConfig; |
121 wrappedDesc.fWidth = dummySurface->width(); | 143 wrappedDesc.fWidth = kW; |
122 wrappedDesc.fHeight = dummySurface->height(); | 144 wrappedDesc.fHeight = kH; |
123 wrappedDesc.fOrigin = dummySurface->origin(); | 145 wrappedDesc.fOrigin = kBottomLeft_GrSurfaceOrigin; |
124 wrappedDesc.fSampleCnt = dummySurface->asRenderTarget()->numColorSamples(); | 146 wrappedDesc.fSampleCnt = 0; |
125 wrappedDesc.fFlags = kRenderTarget_GrBackendTextureFlag; | 147 wrappedDesc.fFlags = kRenderTarget_GrBackendTextureFlag; |
126 wrappedDesc.fTextureHandle = textureHandle; | 148 wrappedDesc.fTextureHandle = texID; |
127 | 149 |
128 SkAutoTUnref<SkSurface> surface(SkSurface::NewWrappedRenderTarget(ctx, wrapp
edDesc, NULL)); | 150 SkAutoTUnref<SkSurface> surface(SkSurface::NewWrappedRenderTarget(ctx, wrapp
edDesc, NULL)); |
129 REPORTER_ASSERT(reporter, surface); | 151 REPORTER_ASSERT(reporter, surface); |
| 152 if (surface) { |
| 153 // Validate that we can draw to the canvas and that the original texture
color is preserved |
| 154 // in pixels that aren't rendered to via the surface. |
| 155 SkPaint paint; |
| 156 static const SkColor kRectColor = ~kOrigColor | 0xFF000000; |
| 157 paint.setColor(kRectColor); |
| 158 surface->getCanvas()->drawRect(SkRect::MakeWH(SkIntToScalar(kW), SkIntTo
Scalar(kH)/2), |
| 159 paint); |
| 160 SkImageInfo readInfo = SkImageInfo::MakeN32Premul(kW, kH); |
| 161 surface->readPixels(readInfo, pixels.get(), kW * sizeof(uint32_t), 0, 0)
; |
| 162 bool stop = false; |
| 163 SkPMColor origColorPM = SkPackARGB32((kOrigColor >> 24 & 0xFF), |
| 164 (kOrigColor >> 0 & 0xFF), |
| 165 (kOrigColor >> 8 & 0xFF), |
| 166 (kOrigColor >> 16 & 0xFF)); |
| 167 SkPMColor rectColorPM = SkPackARGB32((kRectColor >> 24 & 0xFF), |
| 168 (kRectColor >> 16 & 0xFF), |
| 169 (kRectColor >> 8 & 0xFF), |
| 170 (kRectColor >> 0 & 0xFF)); |
| 171 for (int y = 0; y < kH/2 && !stop; ++y) { |
| 172 for (int x = 0; x < kW && !stop; ++x) { |
| 173 REPORTER_ASSERT(reporter, rectColorPM == pixels[x + y * kW]); |
| 174 if (rectColorPM != pixels[x + y * kW]) { |
| 175 stop = true; |
| 176 } |
| 177 } |
| 178 } |
| 179 stop = false; |
| 180 for (int y = kH/2; y < kH && !stop; ++y) { |
| 181 for (int x = 0; x < kW && !stop; ++x) { |
| 182 REPORTER_ASSERT(reporter, origColorPM == pixels[x + y * kW]); |
| 183 if (origColorPM != pixels[x + y * kW]) { |
| 184 stop = true; |
| 185 } |
| 186 } |
| 187 } |
| 188 } |
130 } | 189 } |
131 #endif | 190 #endif |
132 | 191 |
133 | 192 |
134 static void test_image(skiatest::Reporter* reporter) { | 193 static void test_image(skiatest::Reporter* reporter) { |
135 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1); | 194 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1); |
136 size_t rowBytes = info.minRowBytes(); | 195 size_t rowBytes = info.minRowBytes(); |
137 size_t size = info.getSafeSize(rowBytes); | 196 size_t size = info.getSafeSize(rowBytes); |
138 SkData* data = SkData::NewUninitialized(size); | 197 SkData* data = SkData::NewUninitialized(size); |
139 | 198 |
(...skipping 597 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
737 // Now lets jam new colors into our "external" texture, and see if the image
s notice | 796 // Now lets jam new colors into our "external" texture, and see if the image
s notice |
738 const SkPMColor expected1 = SkPreMultiplyColor(SK_ColorBLUE); | 797 const SkPMColor expected1 = SkPreMultiplyColor(SK_ColorBLUE); |
739 sk_memset32(storage, expected1, w * h); | 798 sk_memset32(storage, expected1, w * h); |
740 tex->writePixels(0, 0, w, h, kSkia8888_GrPixelConfig, storage, GrContext::kF
lushWrites_PixelOp); | 799 tex->writePixels(0, 0, w, h, kSkia8888_GrPixelConfig, storage, GrContext::kF
lushWrites_PixelOp); |
741 | 800 |
742 // We expect the ref'd image to see the new color, but cpy'd one should stil
l see the old color | 801 // We expect the ref'd image to see the new color, but cpy'd one should stil
l see the old color |
743 test_image_color(reporter, refImg, expected1); | 802 test_image_color(reporter, refImg, expected1); |
744 test_image_color(reporter, cpyImg, expected0); | 803 test_image_color(reporter, cpyImg, expected0); |
745 } | 804 } |
746 #endif | 805 #endif |
OLD | NEW |