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. |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 56 | // Child class should orphan images before destruction. |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 57 | ASSERT(mSourcesOf.empty()); |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 58 | ASSERT(mTargetOf.get() == nullptr); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 59 | } |
| 60 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 61 | void ImageSibling::setTargetImage(const gl::Context *context, egl::Image *imageTarget) |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 62 | { |
| 63 | ASSERT(imageTarget != nullptr); |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 64 | mTargetOf.set(context, imageTarget); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 65 | imageTarget->addTargetSibling(this); |
| 66 | } |
| 67 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 68 | gl::Error ImageSibling::orphanImages(const gl::Context *context) |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 69 | { |
| 70 | if (mTargetOf.get() != nullptr) |
| 71 | { |
| 72 | // Can't be a target and have sources. |
| 73 | ASSERT(mSourcesOf.empty()); |
| 74 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 75 | ANGLE_TRY(mTargetOf->orphanSibling(context, this)); |
| 76 | mTargetOf.set(context, nullptr); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 77 | } |
| 78 | else |
| 79 | { |
| 80 | for (auto &sourceImage : mSourcesOf) |
| 81 | { |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 82 | ANGLE_TRY(sourceImage->orphanSibling(context, this)); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 83 | } |
| 84 | mSourcesOf.clear(); |
| 85 | } |
| 86 | |
He Yunchao | acd1898 | 2017-01-04 10:46:42 +0800 | [diff] [blame] | 87 | return gl::NoError(); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void ImageSibling::addImageSource(egl::Image *imageSource) |
| 91 | { |
| 92 | ASSERT(imageSource != nullptr); |
| 93 | mSourcesOf.insert(imageSource); |
| 94 | } |
| 95 | |
| 96 | void ImageSibling::removeImageSource(egl::Image *imageSource) |
| 97 | { |
| 98 | ASSERT(mSourcesOf.find(imageSource) != mSourcesOf.end()); |
| 99 | mSourcesOf.erase(imageSource); |
| 100 | } |
| 101 | |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 102 | ImageState::ImageState(EGLenum target, ImageSibling *buffer, const AttributeMap &attribs) |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 103 | : imageIndex(GetImageIndex(target, attribs)), source(buffer), targets() |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 104 | { |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 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), |
Jamie Madill | d75dd26 | 2017-04-20 17:01:19 -0400 | [diff] [blame] | 113 | mImplementation(factory->createImage(mState, target, attribs)) |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 114 | { |
| 115 | ASSERT(mImplementation != nullptr); |
| 116 | ASSERT(buffer != nullptr); |
| 117 | |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 118 | mState.source->addImageSource(this); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 119 | } |
| 120 | |
Jamie Madill | 71c88b3 | 2017-09-14 22:20:29 -0400 | [diff] [blame^] | 121 | gl::Error Image::onDestroy(const gl::Context *context) |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 122 | { |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 123 | // All targets should hold a ref to the egl image and it should not be deleted until there are |
| 124 | // no siblings left. |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 125 | ASSERT(mState.targets.empty()); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 126 | |
| 127 | // Tell the source that it is no longer used by this image |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 128 | if (mState.source.get() != nullptr) |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 129 | { |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 130 | mState.source->removeImageSource(this); |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 131 | mState.source.set(context, nullptr); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 132 | } |
Jamie Madill | 71c88b3 | 2017-09-14 22:20:29 -0400 | [diff] [blame^] | 133 | return gl::NoError(); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 134 | } |
| 135 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 136 | Image::~Image() |
| 137 | { |
Jamie Madill | 71c88b3 | 2017-09-14 22:20:29 -0400 | [diff] [blame^] | 138 | SafeDelete(mImplementation); |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 139 | } |
| 140 | |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 141 | void Image::addTargetSibling(ImageSibling *sibling) |
| 142 | { |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 143 | mState.targets.insert(sibling); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 144 | } |
| 145 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 146 | gl::Error Image::orphanSibling(const gl::Context *context, ImageSibling *sibling) |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 147 | { |
| 148 | // notify impl |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 149 | ANGLE_TRY(mImplementation->orphan(context, sibling)); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 150 | |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 151 | if (mState.source.get() == sibling) |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 152 | { |
| 153 | // If the sibling is the source, it cannot be a target. |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 154 | ASSERT(mState.targets.find(sibling) == mState.targets.end()); |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 155 | mState.source.set(context, nullptr); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 156 | } |
| 157 | else |
| 158 | { |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 159 | mState.targets.erase(sibling); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 160 | } |
| 161 | |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 162 | return gl::NoError(); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 163 | } |
| 164 | |
Jamie Madill | a3944d4 | 2016-07-22 22:13:26 -0400 | [diff] [blame] | 165 | const gl::Format &Image::getFormat() const |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 166 | { |
Jamie Madill | d75dd26 | 2017-04-20 17:01:19 -0400 | [diff] [blame] | 167 | return mState.source->getAttachmentFormat(GL_NONE, mState.imageIndex); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | size_t Image::getWidth() const |
| 171 | { |
Jamie Madill | d75dd26 | 2017-04-20 17:01:19 -0400 | [diff] [blame] | 172 | return mState.source->getAttachmentSize(mState.imageIndex).width; |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | size_t Image::getHeight() const |
| 176 | { |
Jamie Madill | d75dd26 | 2017-04-20 17:01:19 -0400 | [diff] [blame] | 177 | return mState.source->getAttachmentSize(mState.imageIndex).height; |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | size_t Image::getSamples() const |
| 181 | { |
Jamie Madill | d75dd26 | 2017-04-20 17:01:19 -0400 | [diff] [blame] | 182 | return mState.source->getAttachmentSamples(mState.imageIndex); |
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 | rx::ImageImpl *Image::getImplementation() const |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 186 | { |
| 187 | return mImplementation; |
| 188 | } |
| 189 | |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 190 | Error Image::initialize() |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 191 | { |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 192 | return mImplementation->initialize(); |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 193 | } |
Jamie Madill | 76b8f46 | 2017-04-21 12:23:40 -0400 | [diff] [blame] | 194 | |
Jamie Madill | a3944d4 | 2016-07-22 22:13:26 -0400 | [diff] [blame] | 195 | } // namespace egl |