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" |
Geoff Lang | 9d05b93 | 2018-07-27 15:47:18 -0400 | [diff] [blame^] | 13 | #include "libANGLE/Context.h" |
Geoff Lang | e1a057e | 2018-06-07 15:09:00 -0400 | [diff] [blame] | 14 | #include "libANGLE/Renderbuffer.h" |
| 15 | #include "libANGLE/Texture.h" |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 16 | #include "libANGLE/angletypes.h" |
Jamie Madill | a3944d4 | 2016-07-22 22:13:26 -0400 | [diff] [blame] | 17 | #include "libANGLE/formatutils.h" |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 18 | #include "libANGLE/renderer/EGLImplFactory.h" |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 19 | #include "libANGLE/renderer/ImageImpl.h" |
| 20 | |
| 21 | namespace egl |
| 22 | { |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 23 | |
| 24 | namespace |
| 25 | { |
| 26 | gl::ImageIndex GetImageIndex(EGLenum eglTarget, const egl::AttributeMap &attribs) |
| 27 | { |
| 28 | if (eglTarget == EGL_GL_RENDERBUFFER) |
| 29 | { |
Jamie Madill | cc12937 | 2018-04-12 09:13:18 -0400 | [diff] [blame] | 30 | return gl::ImageIndex(); |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 31 | } |
| 32 | |
Corentin Wallez | f0e89be | 2017-11-08 14:00:32 -0800 | [diff] [blame] | 33 | gl::TextureTarget target = egl_gl::EGLImageTargetToTextureTarget(eglTarget); |
Geoff Lang | e1a057e | 2018-06-07 15:09:00 -0400 | [diff] [blame] | 34 | GLint mip = static_cast<GLint>(attribs.get(EGL_GL_TEXTURE_LEVEL_KHR, 0)); |
| 35 | GLint layer = static_cast<GLint>(attribs.get(EGL_GL_TEXTURE_ZOFFSET_KHR, 0)); |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 36 | |
Corentin Wallez | f0e89be | 2017-11-08 14:00:32 -0800 | [diff] [blame] | 37 | if (target == gl::TextureTarget::_3D) |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 38 | { |
| 39 | return gl::ImageIndex::Make3D(mip, layer); |
| 40 | } |
| 41 | else |
| 42 | { |
| 43 | ASSERT(layer == 0); |
Jamie Madill | cc12937 | 2018-04-12 09:13:18 -0400 | [diff] [blame] | 44 | return gl::ImageIndex::MakeFromTarget(target, mip); |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 45 | } |
| 46 | } |
Geoff Lang | 9d05b93 | 2018-07-27 15:47:18 -0400 | [diff] [blame^] | 47 | |
| 48 | const Display *DisplayFromContext(const gl::Context *context) |
| 49 | { |
| 50 | return (context ? context->getCurrentDisplay() : nullptr); |
| 51 | } |
| 52 | |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 53 | } // anonymous namespace |
| 54 | |
Geoff Lang | 9d05b93 | 2018-07-27 15:47:18 -0400 | [diff] [blame^] | 55 | ImageSibling::ImageSibling() : FramebufferAttachmentObject(), mSourcesOf(), mTargetOf() |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 56 | { |
| 57 | } |
| 58 | |
| 59 | ImageSibling::~ImageSibling() |
| 60 | { |
| 61 | // EGL images should hold a ref to their targets and siblings, a Texture should not be deletable |
| 62 | // while it is attached to an EGL image. |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 63 | // Child class should orphan images before destruction. |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 64 | ASSERT(mSourcesOf.empty()); |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 65 | ASSERT(mTargetOf.get() == nullptr); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 66 | } |
| 67 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 68 | void ImageSibling::setTargetImage(const gl::Context *context, egl::Image *imageTarget) |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 69 | { |
| 70 | ASSERT(imageTarget != nullptr); |
Geoff Lang | 9d05b93 | 2018-07-27 15:47:18 -0400 | [diff] [blame^] | 71 | mTargetOf.set(DisplayFromContext(context), imageTarget); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 72 | imageTarget->addTargetSibling(this); |
| 73 | } |
| 74 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 75 | gl::Error ImageSibling::orphanImages(const gl::Context *context) |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 76 | { |
| 77 | if (mTargetOf.get() != nullptr) |
| 78 | { |
| 79 | // Can't be a target and have sources. |
| 80 | ASSERT(mSourcesOf.empty()); |
| 81 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 82 | ANGLE_TRY(mTargetOf->orphanSibling(context, this)); |
Geoff Lang | 9d05b93 | 2018-07-27 15:47:18 -0400 | [diff] [blame^] | 83 | mTargetOf.set(DisplayFromContext(context), nullptr); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 84 | } |
| 85 | else |
| 86 | { |
Geoff Lang | 9d05b93 | 2018-07-27 15:47:18 -0400 | [diff] [blame^] | 87 | for (Image *sourceImage : mSourcesOf) |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 88 | { |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 89 | ANGLE_TRY(sourceImage->orphanSibling(context, this)); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 90 | } |
| 91 | mSourcesOf.clear(); |
| 92 | } |
| 93 | |
He Yunchao | acd1898 | 2017-01-04 10:46:42 +0800 | [diff] [blame] | 94 | return gl::NoError(); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void ImageSibling::addImageSource(egl::Image *imageSource) |
| 98 | { |
| 99 | ASSERT(imageSource != nullptr); |
| 100 | mSourcesOf.insert(imageSource); |
| 101 | } |
| 102 | |
| 103 | void ImageSibling::removeImageSource(egl::Image *imageSource) |
| 104 | { |
| 105 | ASSERT(mSourcesOf.find(imageSource) != mSourcesOf.end()); |
| 106 | mSourcesOf.erase(imageSource); |
| 107 | } |
| 108 | |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame] | 109 | bool ImageSibling::isEGLImageTarget() const |
| 110 | { |
| 111 | return (mTargetOf.get() != nullptr); |
| 112 | } |
| 113 | |
| 114 | gl::InitState ImageSibling::sourceEGLImageInitState() const |
| 115 | { |
| 116 | ASSERT(isEGLImageTarget()); |
| 117 | return mTargetOf->sourceInitState(); |
| 118 | } |
| 119 | |
| 120 | void ImageSibling::setSourceEGLImageInitState(gl::InitState initState) const |
| 121 | { |
| 122 | ASSERT(isEGLImageTarget()); |
| 123 | mTargetOf->setInitState(initState); |
| 124 | } |
| 125 | |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 126 | ImageState::ImageState(EGLenum target, ImageSibling *buffer, const AttributeMap &attribs) |
Geoff Lang | 0bc81b6 | 2018-07-30 13:34:50 -0400 | [diff] [blame] | 127 | : label(nullptr), |
| 128 | imageIndex(GetImageIndex(target, attribs)), |
| 129 | source(buffer), |
| 130 | targets(), |
| 131 | format(buffer->getAttachmentFormat(GL_NONE, imageIndex)), |
| 132 | size(buffer->getAttachmentSize(imageIndex)), |
| 133 | samples(buffer->getAttachmentSamples(imageIndex)) |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 134 | { |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 135 | } |
| 136 | |
Jamie Madill | acf2f3a | 2017-11-21 19:22:44 -0500 | [diff] [blame] | 137 | ImageState::~ImageState() |
| 138 | { |
| 139 | } |
| 140 | |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 141 | Image::Image(rx::EGLImplFactory *factory, |
Geoff Lang | cd7cd2a | 2018-07-19 11:25:54 -0400 | [diff] [blame] | 142 | const gl::Context *context, |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 143 | EGLenum target, |
| 144 | ImageSibling *buffer, |
| 145 | const AttributeMap &attribs) |
Geoff Lang | 9d05b93 | 2018-07-27 15:47:18 -0400 | [diff] [blame^] | 146 | : mState(target, buffer, attribs), |
Geoff Lang | cd7cd2a | 2018-07-19 11:25:54 -0400 | [diff] [blame] | 147 | mImplementation(factory->createImage(mState, context, target, attribs)), |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame] | 148 | mOrphanedAndNeedsInit(false) |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 149 | { |
| 150 | ASSERT(mImplementation != nullptr); |
| 151 | ASSERT(buffer != nullptr); |
| 152 | |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 153 | mState.source->addImageSource(this); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 154 | } |
| 155 | |
Geoff Lang | 9d05b93 | 2018-07-27 15:47:18 -0400 | [diff] [blame^] | 156 | Error Image::onDestroy(const Display *display) |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 157 | { |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 158 | // All targets should hold a ref to the egl image and it should not be deleted until there are |
| 159 | // no siblings left. |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 160 | ASSERT(mState.targets.empty()); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 161 | |
| 162 | // Tell the source that it is no longer used by this image |
Geoff Lang | 9d05b93 | 2018-07-27 15:47:18 -0400 | [diff] [blame^] | 163 | if (mState.source != nullptr) |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 164 | { |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 165 | mState.source->removeImageSource(this); |
Geoff Lang | 9d05b93 | 2018-07-27 15:47:18 -0400 | [diff] [blame^] | 166 | mState.source = nullptr; |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 167 | } |
Geoff Lang | 9d05b93 | 2018-07-27 15:47:18 -0400 | [diff] [blame^] | 168 | |
| 169 | return NoError(); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 170 | } |
| 171 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 172 | Image::~Image() |
| 173 | { |
Jamie Madill | 71c88b3 | 2017-09-14 22:20:29 -0400 | [diff] [blame] | 174 | SafeDelete(mImplementation); |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 175 | } |
| 176 | |
Geoff Lang | 7535966 | 2018-04-11 01:42:27 -0400 | [diff] [blame] | 177 | void Image::setLabel(EGLLabelKHR label) |
| 178 | { |
| 179 | mState.label = label; |
| 180 | } |
| 181 | |
| 182 | EGLLabelKHR Image::getLabel() const |
| 183 | { |
| 184 | return mState.label; |
| 185 | } |
| 186 | |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 187 | void Image::addTargetSibling(ImageSibling *sibling) |
| 188 | { |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 189 | mState.targets.insert(sibling); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 190 | } |
| 191 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 192 | gl::Error Image::orphanSibling(const gl::Context *context, ImageSibling *sibling) |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 193 | { |
Geoff Lang | 9d05b93 | 2018-07-27 15:47:18 -0400 | [diff] [blame^] | 194 | ASSERT(sibling != nullptr); |
| 195 | |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 196 | // notify impl |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 197 | ANGLE_TRY(mImplementation->orphan(context, sibling)); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 198 | |
Geoff Lang | 9d05b93 | 2018-07-27 15:47:18 -0400 | [diff] [blame^] | 199 | if (mState.source == sibling) |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 200 | { |
| 201 | // If the sibling is the source, it cannot be a target. |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 202 | ASSERT(mState.targets.find(sibling) == mState.targets.end()); |
Geoff Lang | 9d05b93 | 2018-07-27 15:47:18 -0400 | [diff] [blame^] | 203 | mState.source = nullptr; |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame] | 204 | mOrphanedAndNeedsInit = |
| 205 | (sibling->initState(mState.imageIndex) == gl::InitState::MayNeedInit); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 206 | } |
| 207 | else |
| 208 | { |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 209 | mState.targets.erase(sibling); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 210 | } |
| 211 | |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 212 | return gl::NoError(); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 213 | } |
| 214 | |
Geoff Lang | 0bc81b6 | 2018-07-30 13:34:50 -0400 | [diff] [blame] | 215 | const gl::Format &Image::getFormat() const |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 216 | { |
Geoff Lang | 0bc81b6 | 2018-07-30 13:34:50 -0400 | [diff] [blame] | 217 | return mState.format; |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | size_t Image::getWidth() const |
| 221 | { |
Geoff Lang | 0bc81b6 | 2018-07-30 13:34:50 -0400 | [diff] [blame] | 222 | return mState.size.width; |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | size_t Image::getHeight() const |
| 226 | { |
Geoff Lang | 0bc81b6 | 2018-07-30 13:34:50 -0400 | [diff] [blame] | 227 | return mState.size.height; |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | size_t Image::getSamples() const |
| 231 | { |
Geoff Lang | 0bc81b6 | 2018-07-30 13:34:50 -0400 | [diff] [blame] | 232 | return mState.samples; |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 233 | } |
| 234 | |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 235 | rx::ImageImpl *Image::getImplementation() const |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 236 | { |
| 237 | return mImplementation; |
| 238 | } |
| 239 | |
Geoff Lang | cd7cd2a | 2018-07-19 11:25:54 -0400 | [diff] [blame] | 240 | Error Image::initialize(const Display *display) |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 241 | { |
Geoff Lang | cd7cd2a | 2018-07-19 11:25:54 -0400 | [diff] [blame] | 242 | return mImplementation->initialize(display); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 243 | } |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 244 | |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame] | 245 | bool Image::orphaned() const |
| 246 | { |
Geoff Lang | 9d05b93 | 2018-07-27 15:47:18 -0400 | [diff] [blame^] | 247 | return (mState.source == nullptr); |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | gl::InitState Image::sourceInitState() const |
| 251 | { |
| 252 | if (orphaned()) |
| 253 | { |
| 254 | return mOrphanedAndNeedsInit ? gl::InitState::MayNeedInit : gl::InitState::Initialized; |
| 255 | } |
| 256 | |
| 257 | return mState.source->initState(mState.imageIndex); |
| 258 | } |
| 259 | |
| 260 | void Image::setInitState(gl::InitState initState) |
| 261 | { |
| 262 | if (orphaned()) |
| 263 | { |
| 264 | mOrphanedAndNeedsInit = false; |
| 265 | } |
| 266 | |
| 267 | return mState.source->setInitState(mState.imageIndex, initState); |
| 268 | } |
| 269 | |
Jamie Madill | a3944d4 | 2016-07-22 22:13:26 -0400 | [diff] [blame] | 270 | } // namespace egl |