blob: 958b76c1af2eb7b64fa76cab0305bfa6f3169bde [file] [log] [blame]
erikchen7fec91c2016-02-05 12:10:55 -08001/*
2 * Copyright 2016 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 "Test.h"
9#if SK_SUPPORT_GPU
10#include "gl/GrGLGpu.h"
11#include "GrContext.h"
12#include "SkSurface_Gpu.h"
13#include "../include/gpu/gl/SkGLContext.h"
14#include "../include/gpu/GrTypes.h"
15#include "../include/private/SkTemplates.h"
16
17class TestStorageAllocator {
18 public:
19 static GrTextureStorageAllocator::Result allocateTextureStorage(void* ctx,
20 GrBackendObject texture, unsigned width, unsigned height, GrPixelConfig config,
21 const void* srcData, GrSurfaceOrigin) {
22 TestStorageAllocator* allocator = static_cast<TestStorageAllocator*>(ctx);
23 if (!allocator->m_allowAllocation)
24 return GrTextureStorageAllocator::Result::kFailed;
25 SkAutoTMalloc<uint8_t> pixels(width * height * 4);
26 memset(pixels.get(), 0, width * height * 4);
27
28 GrGLuint id;
29 GrGLenum target = GR_GL_TEXTURE_2D;
30 GR_GL_CALL(allocator->m_gl, GenTextures(1, &id));
31 GR_GL_CALL(allocator->m_gl, BindTexture(target, id));
32 GR_GL_CALL(allocator->m_gl, TexImage2D(target, 0, GR_GL_RGBA, width, height, 0, GR_GL_RGBA,
33 GR_GL_UNSIGNED_BYTE, pixels.get()));
34 GrGLTextureInfo* info = reinterpret_cast<GrGLTextureInfo*>(texture);
35 info->fID = id;
36 info->fTarget = target;
37 allocator->m_mostRecentlyAllocatedStorage = id;
38 return GrTextureStorageAllocator::Result::kSucceededWithoutUpload;
39 }
40 static void deallocateTextureStorage(void* ctx, GrBackendObject texture) {
41 TestStorageAllocator* allocator = static_cast<TestStorageAllocator*>(ctx);
42 GrGLTextureInfo* info = reinterpret_cast<GrGLTextureInfo*>(texture);
43 GR_GL_CALL(allocator->m_gl, DeleteTextures(1, &(info->fID)));
44 }
45
46 GrGLuint m_mostRecentlyAllocatedStorage;
47 const GrGLInterface* m_gl;
48 bool m_allowAllocation;
49};
50
51DEF_GPUTEST_FOR_RENDERING_CONTEXTS(CustomTexture, reporter, context, glContext) {
52 static const int kWidth = 13;
53 static const int kHeight = 13;
54
55 const GrGLInterface* gl = glContext->gl();
56 TestStorageAllocator allocator;
57 allocator.m_allowAllocation = true;
58 allocator.m_gl = gl;
59 GrTextureStorageAllocator grAllocator;
60 grAllocator.fAllocateTextureStorage = &TestStorageAllocator::allocateTextureStorage;
61 grAllocator.fDeallocateTextureStorage= &TestStorageAllocator::deallocateTextureStorage;
62 grAllocator.fCtx = &allocator;
63
64 SkSurface* surface = SkSurface_Gpu::NewRenderTarget(
65 context, SkSurface_Gpu::kNo_Budgeted, SkImageInfo::MakeN32Premul(kWidth, kHeight), 0,
66 NULL, grAllocator);
67 REPORTER_ASSERT(reporter, surface);
68 GrGLuint id = allocator.m_mostRecentlyAllocatedStorage;
69
70 SkImage* image = surface->newImageSnapshot();
71 REPORTER_ASSERT(reporter, image->isTextureBacked());
72 SkImageInfo imageInfo = SkImageInfo::MakeN32Premul(1,1);
73 GrColor dest = 0x11223344;
74 REPORTER_ASSERT(reporter, image->readPixels(imageInfo, &dest, 4 * kWidth, 0, 0));
75 REPORTER_ASSERT(reporter, GrColorUnpackG(dest) == 0);
76
77 surface->getCanvas()->clear(SK_ColorGREEN);
78 SkImage* image2 = surface->newImageSnapshot();
79 REPORTER_ASSERT(reporter, image2->isTextureBacked());
80 REPORTER_ASSERT(reporter, allocator.m_mostRecentlyAllocatedStorage != id);
81
82 REPORTER_ASSERT(reporter, image2->readPixels(imageInfo, &dest, 4 * kWidth, 0, 0));
83 REPORTER_ASSERT(reporter, GrColorUnpackG(dest) == 255);
84}
85
86DEF_GPUTEST_FOR_RENDERING_CONTEXTS(CustomTextureFailure, reporter, context, glContext) {
87 static const int kWidth = 13;
88 static const int kHeight = 13;
89
90 const GrGLInterface* gl = glContext->gl();
91 TestStorageAllocator allocator;
92 allocator.m_allowAllocation = false;
93 allocator.m_gl = gl;
94 GrTextureStorageAllocator grAllocator;
95 grAllocator.fAllocateTextureStorage = &TestStorageAllocator::allocateTextureStorage;
96 grAllocator.fDeallocateTextureStorage= &TestStorageAllocator::deallocateTextureStorage;
97 grAllocator.fCtx = &allocator;
98 SkSurface* surface = SkSurface_Gpu::NewRenderTarget(
99 context, SkSurface_Gpu::kNo_Budgeted, SkImageInfo::MakeN32Premul(kWidth, kHeight), 0,
100 NULL, grAllocator);
101 REPORTER_ASSERT(reporter, !surface);
102}
103
104#endif