1 /*
2  * Copyright (C) 2009 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include "third_party/blink/renderer/modules/webgl/webgl_texture.h"
27 
28 #include "gpu/command_buffer/client/gles2_interface.h"
29 #include "third_party/blink/renderer/modules/webgl/webgl_rendering_context_base.h"
30 
31 namespace blink {
32 
WebGLTexture(WebGLRenderingContextBase * ctx)33 WebGLTexture::WebGLTexture(WebGLRenderingContextBase* ctx)
34     : WebGLSharedPlatform3DObject(ctx), target_(0) {
35   GLuint texture;
36   ctx->ContextGL()->GenTextures(1, &texture);
37   SetObject(texture);
38 }
39 
40 WebGLTexture::~WebGLTexture() = default;
41 
SetTarget(GLenum target)42 void WebGLTexture::SetTarget(GLenum target) {
43   if (!Object())
44     return;
45   // Target is finalized the first time bindTexture() is called.
46   if (target_)
47     return;
48   target_ = target;
49 }
50 
DeleteObjectImpl(gpu::gles2::GLES2Interface * gl)51 void WebGLTexture::DeleteObjectImpl(gpu::gles2::GLES2Interface* gl) {
52   gl->DeleteTextures(1, &object_);
53   object_ = 0;
54 }
55 
MapTargetToIndex(GLenum target) const56 int WebGLTexture::MapTargetToIndex(GLenum target) const {
57   if (target_ == GL_TEXTURE_2D) {
58     if (target == GL_TEXTURE_2D)
59       return 0;
60   } else if (target_ == GL_TEXTURE_CUBE_MAP) {
61     switch (target) {
62       case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
63         return 0;
64       case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
65         return 1;
66       case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
67         return 2;
68       case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
69         return 3;
70       case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
71         return 4;
72       case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
73         return 5;
74     }
75   } else if (target_ == GL_TEXTURE_3D) {
76     if (target == GL_TEXTURE_3D)
77       return 0;
78   } else if (target_ == GL_TEXTURE_2D_ARRAY) {
79     if (target == GL_TEXTURE_2D_ARRAY)
80       return 0;
81   }
82   return -1;
83 }
84 
ComputeLevelCount(GLsizei width,GLsizei height,GLsizei depth)85 GLint WebGLTexture::ComputeLevelCount(GLsizei width,
86                                       GLsizei height,
87                                       GLsizei depth) {
88   // return 1 + log2Floor(std::max(width, height));
89   GLsizei n = std::max(std::max(width, height), depth);
90   if (n <= 0)
91     return 0;
92   GLint log = 0;
93   GLsizei value = n;
94   for (int ii = 4; ii >= 0; --ii) {
95     int shift = (1 << ii);
96     GLsizei x = (value >> shift);
97     if (x) {
98       value = x;
99       log += shift;
100     }
101   }
102   DCHECK_EQ(value, 1);
103   return log + 1;
104 }
105 
106 }  // namespace blink
107