Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2015 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
| 7 | // Image.cpp: Implements the egl::Image class representing the EGLimage object. |
| 8 | |
| 9 | #include "libANGLE/Image.h" |
| 10 | |
| 11 | #include "common/debug.h" |
| 12 | #include "common/utilities.h" |
| 13 | #include "libANGLE/angletypes.h" |
Jamie Madill | a3944d4 | 2016-07-22 22:13:26 -0400 | [diff] [blame] | 14 | #include "libANGLE/formatutils.h" |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 15 | #include "libANGLE/Texture.h" |
| 16 | #include "libANGLE/Renderbuffer.h" |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame^] | 17 | #include "libANGLE/renderer/EGLImplFactory.h" |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 18 | #include "libANGLE/renderer/ImageImpl.h" |
| 19 | |
| 20 | namespace egl |
| 21 | { |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame^] | 22 | |
| 23 | namespace |
| 24 | { |
| 25 | gl::ImageIndex GetImageIndex(EGLenum eglTarget, const egl::AttributeMap &attribs) |
| 26 | { |
| 27 | if (eglTarget == EGL_GL_RENDERBUFFER) |
| 28 | { |
| 29 | return gl::ImageIndex::MakeInvalid(); |
| 30 | } |
| 31 | |
| 32 | GLenum target = egl_gl::EGLImageTargetToGLTextureTarget(eglTarget); |
| 33 | GLint mip = static_cast<GLint>(attribs.get(EGL_GL_TEXTURE_LEVEL_KHR, 0)); |
| 34 | GLint layer = static_cast<GLint>(attribs.get(EGL_GL_TEXTURE_ZOFFSET_KHR, 0)); |
| 35 | |
| 36 | if (target == GL_TEXTURE_3D) |
| 37 | { |
| 38 | return gl::ImageIndex::Make3D(mip, layer); |
| 39 | } |
| 40 | else |
| 41 | { |
| 42 | ASSERT(layer == 0); |
| 43 | return gl::ImageIndex::MakeGeneric(target, mip); |
| 44 | } |
| 45 | } |
| 46 | } // anonymous namespace |
| 47 | |
Corentin Wallez | 51706ea | 2015-08-07 14:39:22 -0400 | [diff] [blame] | 48 | ImageSibling::ImageSibling(GLuint id) : RefCountObject(id), mSourcesOf(), mTargetOf() |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 49 | { |
| 50 | } |
| 51 | |
| 52 | ImageSibling::~ImageSibling() |
| 53 | { |
| 54 | // EGL images should hold a ref to their targets and siblings, a Texture should not be deletable |
| 55 | // while it is attached to an EGL image. |
| 56 | ASSERT(mSourcesOf.empty()); |
| 57 | orphanImages(); |
| 58 | } |
| 59 | |
| 60 | void ImageSibling::setTargetImage(egl::Image *imageTarget) |
| 61 | { |
| 62 | ASSERT(imageTarget != nullptr); |
| 63 | mTargetOf.set(imageTarget); |
| 64 | imageTarget->addTargetSibling(this); |
| 65 | } |
| 66 | |
| 67 | gl::Error ImageSibling::orphanImages() |
| 68 | { |
| 69 | if (mTargetOf.get() != nullptr) |
| 70 | { |
| 71 | // Can't be a target and have sources. |
| 72 | ASSERT(mSourcesOf.empty()); |
| 73 | |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame^] | 74 | ANGLE_TRY(mTargetOf->orphanSibling(this)); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 75 | mTargetOf.set(nullptr); |
| 76 | } |
| 77 | else |
| 78 | { |
| 79 | for (auto &sourceImage : mSourcesOf) |
| 80 | { |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame^] | 81 | ANGLE_TRY(sourceImage->orphanSibling(this)); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 82 | } |
| 83 | mSourcesOf.clear(); |
| 84 | } |
| 85 | |
He Yunchao | acd1898 | 2017-01-04 10:46:42 +0800 | [diff] [blame] | 86 | return gl::NoError(); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void ImageSibling::addImageSource(egl::Image *imageSource) |
| 90 | { |
| 91 | ASSERT(imageSource != nullptr); |
| 92 | mSourcesOf.insert(imageSource); |
| 93 | } |
| 94 | |
| 95 | void ImageSibling::removeImageSource(egl::Image *imageSource) |
| 96 | { |
| 97 | ASSERT(mSourcesOf.find(imageSource) != mSourcesOf.end()); |
| 98 | mSourcesOf.erase(imageSource); |
| 99 | } |
| 100 | |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame^] | 101 | ImageState::ImageState(EGLenum target, ImageSibling *buffer, const AttributeMap &attribs) |
| 102 | : imageIndex(GetImageIndex(target, attribs)), source(), targets() |
| 103 | { |
| 104 | source.set(buffer); |
| 105 | } |
| 106 | |
| 107 | Image::Image(rx::EGLImplFactory *factory, |
| 108 | EGLenum target, |
| 109 | ImageSibling *buffer, |
| 110 | const AttributeMap &attribs) |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 111 | : RefCountObject(0), |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame^] | 112 | mState(target, buffer, attribs), |
| 113 | mImplementation(factory->createImage(mState, target, attribs)), |
Jamie Madill | a3944d4 | 2016-07-22 22:13:26 -0400 | [diff] [blame] | 114 | mFormat(gl::Format::Invalid()), |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 115 | mWidth(0), |
| 116 | mHeight(0), |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame^] | 117 | mSamples(0) |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 118 | { |
| 119 | ASSERT(mImplementation != nullptr); |
| 120 | ASSERT(buffer != nullptr); |
| 121 | |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame^] | 122 | mState.source->addImageSource(this); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 123 | |
| 124 | if (IsTextureTarget(target)) |
| 125 | { |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame^] | 126 | gl::Texture *texture = rx::GetAs<gl::Texture>(mState.source.get()); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 127 | GLenum textureTarget = egl_gl::EGLImageTargetToGLTextureTarget(target); |
| 128 | size_t level = attribs.get(EGL_GL_TEXTURE_LEVEL_KHR, 0); |
Jamie Madill | a3944d4 | 2016-07-22 22:13:26 -0400 | [diff] [blame] | 129 | mFormat = texture->getFormat(textureTarget, level); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 130 | mWidth = texture->getWidth(textureTarget, level); |
| 131 | mHeight = texture->getHeight(textureTarget, level); |
| 132 | mSamples = 0; |
| 133 | } |
| 134 | else if (IsRenderbufferTarget(target)) |
| 135 | { |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame^] | 136 | gl::Renderbuffer *renderbuffer = rx::GetAs<gl::Renderbuffer>(mState.source.get()); |
Jamie Madill | a3944d4 | 2016-07-22 22:13:26 -0400 | [diff] [blame] | 137 | mFormat = renderbuffer->getFormat(); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 138 | mWidth = renderbuffer->getWidth(); |
| 139 | mHeight = renderbuffer->getHeight(); |
| 140 | mSamples = renderbuffer->getSamples(); |
| 141 | } |
| 142 | else |
| 143 | { |
| 144 | UNREACHABLE(); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | Image::~Image() |
| 149 | { |
| 150 | SafeDelete(mImplementation); |
| 151 | |
| 152 | // All targets should hold a ref to the egl image and it should not be deleted until there are |
| 153 | // no siblings left. |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame^] | 154 | ASSERT(mState.targets.empty()); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 155 | |
| 156 | // Tell the source that it is no longer used by this image |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame^] | 157 | if (mState.source.get() != nullptr) |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 158 | { |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame^] | 159 | mState.source->removeImageSource(this); |
| 160 | mState.source.set(nullptr); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
| 164 | void Image::addTargetSibling(ImageSibling *sibling) |
| 165 | { |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame^] | 166 | mState.targets.insert(sibling); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | gl::Error Image::orphanSibling(ImageSibling *sibling) |
| 170 | { |
| 171 | // notify impl |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame^] | 172 | ANGLE_TRY(mImplementation->orphan(sibling)); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 173 | |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame^] | 174 | if (mState.source.get() == sibling) |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 175 | { |
| 176 | // If the sibling is the source, it cannot be a target. |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame^] | 177 | ASSERT(mState.targets.find(sibling) == mState.targets.end()); |
| 178 | mState.source.set(nullptr); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 179 | } |
| 180 | else |
| 181 | { |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame^] | 182 | mState.targets.erase(sibling); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 183 | } |
| 184 | |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame^] | 185 | return gl::NoError(); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 186 | } |
| 187 | |
Jamie Madill | a3944d4 | 2016-07-22 22:13:26 -0400 | [diff] [blame] | 188 | const gl::Format &Image::getFormat() const |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 189 | { |
Jamie Madill | a3944d4 | 2016-07-22 22:13:26 -0400 | [diff] [blame] | 190 | return mFormat; |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | size_t Image::getWidth() const |
| 194 | { |
| 195 | return mWidth; |
| 196 | } |
| 197 | |
| 198 | size_t Image::getHeight() const |
| 199 | { |
| 200 | return mHeight; |
| 201 | } |
| 202 | |
| 203 | size_t Image::getSamples() const |
| 204 | { |
| 205 | return mSamples; |
| 206 | } |
| 207 | |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame^] | 208 | rx::ImageImpl *Image::getImplementation() const |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 209 | { |
| 210 | return mImplementation; |
| 211 | } |
| 212 | |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame^] | 213 | Error Image::initialize() |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 214 | { |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame^] | 215 | return mImplementation->initialize(); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 216 | } |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame^] | 217 | |
Jamie Madill | a3944d4 | 2016-07-22 22:13:26 -0400 | [diff] [blame] | 218 | } // namespace egl |